X-Git-Url: http://de.git.xonotic.org/?p=xonotic%2Fdarkplaces.git;a=blobdiff_plain;f=gl_models.c;h=c5c4c712850d963d32de77b0e68afaa4c4619368;hp=6065452e02e9b031d38eb3c52af4b8c8c0e6c4bc;hb=bcdca3f76b1e1020eeebd27e5721ec4fe57523a1;hpb=4d162c39ec059b7f3191fbb4fb18304bc9b1db59 diff --git a/gl_models.c b/gl_models.c index 6065452e..c5c4c712 100644 --- a/gl_models.c +++ b/gl_models.c @@ -1,169 +1,76 @@ #include "quakedef.h" - -cvar_t gl_transform = {"gl_transform", "1"}; -cvar_t gl_lockarrays = {"gl_lockarrays", "1"}; +#include "cl_collision.h" +#include "r_shadow.h" typedef struct { float m[3][4]; } zymbonematrix; -// LordHavoc: vertex array -float *aliasvert; -float *aliasvertnorm; -byte *aliasvertcolor; -byte *aliasvertcolor2; -zymbonematrix *zymbonepose; -int *aliasvertusage; - -rtexture_t *chrometexture; +// LordHavoc: vertex arrays -int arraylocked = false; -void GL_LockArray(int first, int count) -{ - if (gl_supportslockarrays && gl_lockarrays.value) - { - qglLockArraysEXT(first, count); - arraylocked = true; - } -} - -void GL_UnlockArray() -{ - if (arraylocked) - { - qglUnlockArraysEXT(); - arraylocked = false; - } -} +float *aliasvertcolorbuf; +float *aliasvertcolor; // this may point at aliasvertcolorbuf or at vertex arrays in the mesh backend +float *aliasvert_svectors; +float *aliasvert_tvectors; +float *aliasvert_normals; -void GL_SetupModelTransform (vec3_t origin, vec3_t angles, vec_t scale) -{ - glTranslatef (origin[0], origin[1], origin[2]); - - if (scale != 1) - glScalef (scale, scale, scale); - if (angles[1]) - glRotatef (angles[1], 0, 0, 1); - if (angles[0]) - glRotatef (-angles[0], 0, 1, 0); - if (angles[2]) - glRotatef (angles[2], 1, 0, 0); -} - -void makechrometexture() -{ - int i; - byte noise[64*64]; - byte data[64*64][4]; - - fractalnoise(noise, 64, 8); - - // convert to RGBA data - for (i = 0;i < 64*64;i++) - { - data[i][0] = data[i][1] = data[i][2] = noise[i]; - data[i][3] = 255; - } +float *aliasvertcolor2; +int *aliasvertusage; +zymbonematrix *zymbonepose; - chrometexture = R_LoadTexture ("chrometexture", 64, 64, &data[0][0], TEXF_MIPMAP | TEXF_RGBA | TEXF_PRECACHE); -} +mempool_t *gl_models_mempool; -void gl_models_start() +void gl_models_start(void) { // allocate vertex processing arrays - aliasvert = qmalloc(sizeof(float[MD2MAX_VERTS][3])); - aliasvertnorm = qmalloc(sizeof(float[MD2MAX_VERTS][3])); - aliasvertcolor = qmalloc(sizeof(byte[MD2MAX_VERTS][4])); - aliasvertcolor2 = qmalloc(sizeof(byte[MD2MAX_VERTS][4])); // used temporarily for tinted coloring - zymbonepose = qmalloc(sizeof(zymbonematrix[256])); - aliasvertusage = qmalloc(sizeof(int[MD2MAX_VERTS])); - makechrometexture(); + gl_models_mempool = Mem_AllocPool("GL_Models"); + aliasvertcolor = aliasvertcolorbuf = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4])); + aliasvert_svectors = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4])); + aliasvert_tvectors = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4])); + aliasvert_normals = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4])); + aliasvertcolor2 = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4])); // used temporarily for tinted coloring + zymbonepose = Mem_Alloc(gl_models_mempool, sizeof(zymbonematrix[256])); + aliasvertusage = Mem_Alloc(gl_models_mempool, sizeof(int[MD2MAX_VERTS])); } -void gl_models_shutdown() +void gl_models_shutdown(void) { - qfree(aliasvert); - qfree(aliasvertnorm); - qfree(aliasvertcolor); - qfree(aliasvertcolor2); - qfree(zymbonepose); - qfree(aliasvertusage); + Mem_FreePool(&gl_models_mempool); } -void gl_models_newmap() +void gl_models_newmap(void) { } -void GL_Models_Init() +void GL_Models_Init(void) { - Cvar_RegisterVariable(&gl_transform); - Cvar_RegisterVariable(&gl_lockarrays); - R_RegisterModule("GL_Models", gl_models_start, gl_models_shutdown, gl_models_newmap); } -extern vec3_t softwaretransform_x; -extern vec3_t softwaretransform_y; -extern vec3_t softwaretransform_z; -extern vec_t softwaretransform_scale; -extern vec3_t softwaretransform_offset; -void R_AliasTransformVerts(int vertcount) -{ - int i; - vec3_t point, matrix_x, matrix_y, matrix_z; - float *av, *avn; - av = aliasvert; - avn = aliasvertnorm; - matrix_x[0] = softwaretransform_x[0] * softwaretransform_scale; - matrix_x[1] = softwaretransform_y[0] * softwaretransform_scale; - matrix_x[2] = softwaretransform_z[0] * softwaretransform_scale; - matrix_y[0] = softwaretransform_x[1] * softwaretransform_scale; - matrix_y[1] = softwaretransform_y[1] * softwaretransform_scale; - matrix_y[2] = softwaretransform_z[1] * softwaretransform_scale; - matrix_z[0] = softwaretransform_x[2] * softwaretransform_scale; - matrix_z[1] = softwaretransform_y[2] * softwaretransform_scale; - matrix_z[2] = softwaretransform_z[2] * softwaretransform_scale; - for (i = 0;i < vertcount;i++) - { - // rotate, scale, and translate the vertex locations - VectorCopy(av, point); - av[0] = DotProduct(point, matrix_x) + softwaretransform_offset[0]; - av[1] = DotProduct(point, matrix_y) + softwaretransform_offset[1]; - av[2] = DotProduct(point, matrix_z) + softwaretransform_offset[2]; - // rotate the normals - VectorCopy(avn, point); - avn[0] = point[0] * softwaretransform_x[0] + point[1] * softwaretransform_y[0] + point[2] * softwaretransform_z[0]; - avn[1] = point[0] * softwaretransform_x[1] + point[1] * softwaretransform_y[1] + point[2] * softwaretransform_z[1]; - avn[2] = point[0] * softwaretransform_x[2] + point[1] * softwaretransform_y[2] + point[2] * softwaretransform_z[2]; - av += 3; - avn += 3; - } -} - -void R_AliasLerpVerts(int vertcount, - float lerp1, trivertx_t *verts1, vec3_t fscale1, vec3_t translate1, - float lerp2, trivertx_t *verts2, vec3_t fscale2, vec3_t translate2, - float lerp3, trivertx_t *verts3, vec3_t fscale3, vec3_t translate3, - float lerp4, trivertx_t *verts4, vec3_t fscale4, vec3_t translate4) +void R_AliasLerpVerts(int vertcount, float *vertices, float *normals, + float lerp1, const trivertx_t *verts1, const vec3_t fscale1, const vec3_t translate1, + float lerp2, const trivertx_t *verts2, const vec3_t fscale2, const vec3_t translate2, + float lerp3, const trivertx_t *verts3, const vec3_t fscale3, const vec3_t translate3, + float lerp4, const trivertx_t *verts4, const vec3_t fscale4, const vec3_t translate4) { int i; vec3_t scale1, scale2, scale3, scale4, translate; - float *n1, *n2, *n3, *n4; + const float *n1, *n2, *n3, *n4; float *av, *avn; - av = aliasvert; - avn = aliasvertnorm; - VectorScaleQuick(fscale1, lerp1, scale1); + av = vertices; + avn = normals; + VectorScale(fscale1, lerp1, scale1); if (lerp2) { - VectorScaleQuick(fscale2, lerp2, scale2); + VectorScale(fscale2, lerp2, scale2); if (lerp3) { - VectorScaleQuick(fscale3, lerp3, scale3); + VectorScale(fscale3, lerp3, scale3); if (lerp4) { - VectorScaleQuick(fscale4, lerp4, scale4); + VectorScale(fscale4, lerp4, scale4); translate[0] = translate1[0] * lerp1 + translate2[0] * lerp2 + translate3[0] * lerp3 + translate4[0] * lerp4; translate[1] = translate1[1] * lerp1 + translate2[1] * lerp2 + translate3[1] * lerp3 + translate4[1] * lerp4; translate[2] = translate1[2] * lerp1 + translate2[2] * lerp2 + translate3[2] * lerp3 + translate4[2] * lerp4; @@ -180,8 +87,8 @@ void R_AliasLerpVerts(int vertcount, avn[0] = n1[0] * lerp1 + n2[0] * lerp2 + n3[0] * lerp3 + n4[0] * lerp4; avn[1] = n1[1] * lerp1 + n2[1] * lerp2 + n3[1] * lerp3 + n4[1] * lerp4; avn[2] = n1[2] * lerp1 + n2[2] * lerp2 + n3[2] * lerp3 + n4[2] * lerp4; - av += 3; - avn += 3; + av += 4; + avn += 4; verts1++;verts2++;verts3++;verts4++; } } @@ -202,8 +109,8 @@ void R_AliasLerpVerts(int vertcount, avn[0] = n1[0] * lerp1 + n2[0] * lerp2 + n3[0] * lerp3; avn[1] = n1[1] * lerp1 + n2[1] * lerp2 + n3[1] * lerp3; avn[2] = n1[2] * lerp1 + n2[2] * lerp2 + n3[2] * lerp3; - av += 3; - avn += 3; + av += 4; + avn += 4; verts1++;verts2++;verts3++; } } @@ -224,8 +131,8 @@ void R_AliasLerpVerts(int vertcount, avn[0] = n1[0] * lerp1 + n2[0] * lerp2; avn[1] = n1[1] * lerp1 + n2[1] * lerp2; avn[2] = n1[2] * lerp1 + n2[2] * lerp2; - av += 3; - avn += 3; + av += 4; + avn += 4; verts1++;verts2++; } } @@ -248,8 +155,8 @@ void R_AliasLerpVerts(int vertcount, avn[0] = n1[0] * lerp1; avn[1] = n1[1] * lerp1; avn[2] = n1[2] * lerp1; - av += 3; - avn += 3; + av += 4; + avn += 4; verts1++; } } @@ -262,322 +169,449 @@ void R_AliasLerpVerts(int vertcount, av[1] = verts1->v[1] * scale1[1] + translate[1]; av[2] = verts1->v[2] * scale1[2] + translate[2]; VectorCopy(m_bytenormals[verts1->lightnormalindex], avn); - av += 3; - avn += 3; + av += 4; + avn += 4; verts1++; } } } } -void GL_DrawModelMesh(rtexture_t *skin, byte *colors, maliashdr_t *maliashdr) +skinframe_t *R_FetchSkinFrame(const entity_render_t *ent) { - if (!r_render.value) - return; - glBindTexture(GL_TEXTURE_2D, R_GetTexture(skin)); - if (!colors) - { - if (lighthalf) - glColor3f(0.5f, 0.5f, 0.5f); - else - glColor3f(1.0f, 1.0f, 1.0f); - } - if (colors) - { - glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors); - glEnableClientState(GL_COLOR_ARRAY); - } - - glDrawElements(GL_TRIANGLES, maliashdr->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) maliashdr + maliashdr->tridata)); - - if (colors) - glDisableClientState(GL_COLOR_ARRAY); - // leave it in a state for additional passes - glDepthMask(0); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive + model_t *model = ent->model; + unsigned int s = (unsigned int) ent->skinnum; + if (s >= model->numskins) + s = 0; + if (model->skinscenes[s].framecount > 1) + return &model->skinframes[model->skinscenes[s].firstframe + (int) (cl.time * 10) % model->skinscenes[s].framecount]; + else + return &model->skinframes[model->skinscenes[s].firstframe]; } -void R_TintModel(byte *in, byte *out, int verts, byte *color) +void R_LerpMDLMD2Vertices(const entity_render_t *ent, float *vertices, float *normals) { - int i; - byte r = color[0]; - byte g = color[1]; - byte b = color[2]; - for (i = 0;i < verts;i++) - { - out[0] = (byte) ((in[0] * r) >> 8); - out[1] = (byte) ((in[1] * g) >> 8); - out[2] = (byte) ((in[2] * b) >> 8); - out[3] = in[3]; - in += 4; - out += 4; - } + const md2frame_t *frame1, *frame2, *frame3, *frame4; + const trivertx_t *frame1verts, *frame2verts, *frame3verts, *frame4verts; + const model_t *model = ent->model; + + frame1 = &model->mdlmd2data_frames[ent->frameblend[0].frame]; + frame2 = &model->mdlmd2data_frames[ent->frameblend[1].frame]; + frame3 = &model->mdlmd2data_frames[ent->frameblend[2].frame]; + frame4 = &model->mdlmd2data_frames[ent->frameblend[3].frame]; + frame1verts = &model->mdlmd2data_pose[ent->frameblend[0].frame * model->numverts]; + frame2verts = &model->mdlmd2data_pose[ent->frameblend[1].frame * model->numverts]; + frame3verts = &model->mdlmd2data_pose[ent->frameblend[2].frame * model->numverts]; + frame4verts = &model->mdlmd2data_pose[ent->frameblend[3].frame * model->numverts]; + R_AliasLerpVerts(model->numverts, vertices, normals, + ent->frameblend[0].lerp, frame1verts, frame1->scale, frame1->translate, + ent->frameblend[1].lerp, frame2verts, frame2->scale, frame2->translate, + ent->frameblend[2].lerp, frame3verts, frame3->scale, frame3->translate, + ent->frameblend[3].lerp, frame4verts, frame4->scale, frame4->translate); } -/* -================= -R_DrawAliasFrame - -================= -*/ -extern vec3_t lightspot; -void R_LightModel(entity_t *ent, int numverts, vec3_t center, vec3_t basecolor); -void R_DrawAliasFrame (maliashdr_t *maliashdr, float alpha, vec3_t color, entity_t *ent, int shadow, vec3_t org, vec3_t angles, vec_t scale, frameblend_t *blend, rtexture_t **skin, int colormap, int effects, int flags) +void R_DrawQ1Q2AliasModelCallback (const void *calldata1, int calldata2) { - if (gl_transform.value) - { - if (r_render.value) - { - glPushMatrix(); - GL_SetupModelTransform(org, angles, scale); - } - } - // always needed, for model lighting - softwaretransformforentity(ent); - - R_AliasLerpVerts(maliashdr->numverts, - blend[0].lerp, ((trivertx_t *)((int) maliashdr + maliashdr->posedata)) + blend[0].frame * maliashdr->numverts, maliashdr->scale, maliashdr->scale_origin, - blend[1].lerp, ((trivertx_t *)((int) maliashdr + maliashdr->posedata)) + blend[1].frame * maliashdr->numverts, maliashdr->scale, maliashdr->scale_origin, - blend[2].lerp, ((trivertx_t *)((int) maliashdr + maliashdr->posedata)) + blend[2].frame * maliashdr->numverts, maliashdr->scale, maliashdr->scale_origin, - blend[3].lerp, ((trivertx_t *)((int) maliashdr + maliashdr->posedata)) + blend[3].frame * maliashdr->numverts, maliashdr->scale, maliashdr->scale_origin); - if (!gl_transform.value) - R_AliasTransformVerts(maliashdr->numverts); - - // prep the vertex array as early as possible - if (r_render.value) + int i, c, fullbright, pantsfullbright, shirtfullbright, colormapped, tex; + float pantscolor[3], shirtcolor[3]; + float fog, ifog, colorscale; + vec3_t diff; + qbyte *bcolor; + rmeshstate_t m; + model_t *model; + skinframe_t *skinframe; + const entity_render_t *ent = calldata1; + int blendfunc1, blendfunc2; + + R_Mesh_Matrix(&ent->matrix); + + model = ent->model; + R_Mesh_ResizeCheck(model->numverts); + + skinframe = R_FetchSkinFrame(ent); + + fullbright = (ent->effects & EF_FULLBRIGHT) != 0; + + fog = 0; + if (fogenabled) { - glVertexPointer(3, GL_FLOAT, 0, aliasvert); - glEnableClientState(GL_VERTEX_ARRAY); - glTexCoordPointer(2, GL_FLOAT, 0, (void *)((int) maliashdr->texdata + (int) maliashdr)); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - GL_LockArray(0, maliashdr->numverts); + VectorSubtract(ent->origin, r_origin, diff); + fog = DotProduct(diff,diff); + if (fog < 0.01f) + fog = 0.01f; + fog = exp(fogdensity/fog); + if (fog > 1) + fog = 1; + if (fog < 0.01f) + fog = 0; + // fog method: darken, additive fog + // 1. render model as normal, scaled by inverse of fog alpha (darkens it) + // 2. render fog as additive } + ifog = 1 - fog; - R_LightModel(ent, maliashdr->numverts, org, color); - - if (!r_render.value) - return; - glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - glShadeModel(GL_SMOOTH); - if (effects & EF_ADDITIVE) + if (ent->effects & EF_ADDITIVE) { - glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive rendering - glEnable(GL_BLEND); - glDepthMask(0); + blendfunc1 = GL_SRC_ALPHA; + blendfunc2 = GL_ONE; } - else if (alpha != 1.0) + else if (ent->alpha != 1.0 || skinframe->fog != NULL) { - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable(GL_BLEND); - glDepthMask(0); + blendfunc1 = GL_SRC_ALPHA; + blendfunc2 = GL_ONE_MINUS_SRC_ALPHA; } else { - glDisable(GL_BLEND); - glDepthMask(1); + blendfunc1 = GL_ONE; + blendfunc2 = GL_ZERO; } - if (skin[0] || skin[1] || skin[2] || skin[3] || skin[4]) + R_LerpMDLMD2Vertices(ent, varray_vertex, aliasvert_normals); + memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[4])); + if (!skinframe->base && !skinframe->pants && !skinframe->shirt && !skinframe->glow) { - if (colormap >= 0 && (skin[0] || skin[1] || skin[2])) + // untextured + memset(&m, 0, sizeof(m)); + m.blendfunc1 = blendfunc1; + m.blendfunc2 = blendfunc2; + colorscale = r_colorscale; + if (gl_combine.integer) { - int c; - if (skin[0]) - GL_DrawModelMesh(skin[0], aliasvertcolor, maliashdr); - if (skin[1]) - { - c = (colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges - R_TintModel(aliasvertcolor, aliasvertcolor2, maliashdr->numverts, (byte *) (&d_8to24table[c])); - GL_DrawModelMesh(skin[1], aliasvertcolor2, maliashdr); - } - if (skin[2]) - { - c = colormap & 0xF0 ;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges - R_TintModel(aliasvertcolor, aliasvertcolor2, maliashdr->numverts, (byte *) (&d_8to24table[c])); - GL_DrawModelMesh(skin[2], aliasvertcolor2, maliashdr); - } + colorscale *= 0.25f; + m.texrgbscale[0] = 4; } - else + m.tex[0] = R_GetTexture(r_notexture); + R_Mesh_State(&m); + c_alias_polys += model->numtris; + for (i = 0;i < model->numverts * 4;i += 4) { - if (skin[4]) GL_DrawModelMesh(skin[4], aliasvertcolor, maliashdr); - else - { - if (skin[0]) GL_DrawModelMesh(skin[0], aliasvertcolor, maliashdr); - if (skin[1]) GL_DrawModelMesh(skin[1], aliasvertcolor, maliashdr); - if (skin[2]) GL_DrawModelMesh(skin[2], aliasvertcolor, maliashdr); - } + varray_texcoord[0][i + 0] *= 8.0f; + varray_texcoord[0][i + 1] *= 8.0f; } - if (skin[3]) GL_DrawModelMesh(skin[3], NULL, maliashdr); + R_LightModel(ent, model->numverts, varray_vertex, aliasvert_normals, varray_color, colorscale, colorscale, colorscale, false); + GL_UseColorArray(); + R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices); + return; } - else - GL_DrawModelMesh(0, NULL, maliashdr); - if (fogenabled) + colormapped = !skinframe->merged || (ent->colormap >= 0 && skinframe->base && (skinframe->pants || skinframe->shirt)); + if (colormapped) { - vec3_t diff; - glDisable (GL_TEXTURE_2D); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable (GL_BLEND); - glDepthMask(0); // disable zbuffer updates - - VectorSubtract(org, r_refdef.vieworg, diff); - glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff))); - - glDrawElements(GL_TRIANGLES, maliashdr->numtris * 3, GL_UNSIGNED_SHORT, (void *)((int) maliashdr + maliashdr->tridata)); - - glEnable (GL_TEXTURE_2D); - glColor3f (1,1,1); + // 128-224 are backwards ranges + c = (ent->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12; + bcolor = (qbyte *) (&d_8to24table[c]); + pantsfullbright = c >= 224; + VectorScale(bcolor, (1.0f / 255.0f), pantscolor); + c = (ent->colormap & 0xF0);c += (c >= 128 && c < 224) ? 4 : 12; + bcolor = (qbyte *) (&d_8to24table[c]); + shirtfullbright = c >= 224; + VectorScale(bcolor, (1.0f / 255.0f), shirtcolor); + } + else + { + pantscolor[0] = pantscolor[1] = pantscolor[2] = shirtcolor[0] = shirtcolor[1] = shirtcolor[2] = 1; + pantsfullbright = shirtfullbright = false; } - GL_UnlockArray(); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); - glDisableClientState(GL_VERTEX_ARRAY); - - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable (GL_BLEND); - glDepthMask(1); - - glPopMatrix(); -} - -/* -================= -R_DrawQ2AliasFrame - -================= -*/ -void R_DrawQ2AliasFrame (md2mem_t *pheader, float alpha, vec3_t color, entity_t *ent, int shadow, vec3_t org, vec3_t angles, vec_t scale, frameblend_t *blend, rtexture_t *skin, int effects, int flags) -{ - int *order, count; - md2frame_t *frame1, *frame2, *frame3, *frame4; - - if (r_render.value) - glBindTexture(GL_TEXTURE_2D, R_GetTexture(skin)); - if (gl_transform.value) + tex = colormapped ? R_GetTexture(skinframe->base) : R_GetTexture(skinframe->merged); + if (tex) { - if (r_render.value) + memset(&m, 0, sizeof(m)); + m.blendfunc1 = blendfunc1; + m.blendfunc2 = blendfunc2; + colorscale = r_colorscale; + if (gl_combine.integer) + { + colorscale *= 0.25f; + m.texrgbscale[0] = 4; + } + m.tex[0] = tex; + R_Mesh_State(&m); + if (fullbright) + GL_Color(colorscale * ifog, colorscale * ifog, colorscale * ifog, ent->alpha); + else { - glPushMatrix(); - GL_SetupModelTransform(org, angles, scale); + GL_UseColorArray(); + R_LightModel(ent, model->numverts, varray_vertex, aliasvert_normals, varray_color, colorscale * ifog, colorscale * ifog, colorscale * ifog, false); } + R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices); + c_alias_polys += model->numtris; + blendfunc1 = GL_SRC_ALPHA; + blendfunc2 = GL_ONE; } - // always needed, for model lighting - softwaretransformforentity(ent); - - frame1 = (void *)((int) pheader + pheader->ofs_frames + (pheader->framesize * blend[0].frame)); - frame2 = (void *)((int) pheader + pheader->ofs_frames + (pheader->framesize * blend[1].frame)); - frame3 = (void *)((int) pheader + pheader->ofs_frames + (pheader->framesize * blend[2].frame)); - frame4 = (void *)((int) pheader + pheader->ofs_frames + (pheader->framesize * blend[3].frame)); - R_AliasLerpVerts(pheader->num_xyz, - blend[0].lerp, frame1->verts, frame1->scale, frame1->translate, - blend[1].lerp, frame2->verts, frame2->scale, frame2->translate, - blend[2].lerp, frame3->verts, frame3->scale, frame3->translate, - blend[3].lerp, frame4->verts, frame4->scale, frame4->translate); - if (!gl_transform.value) - R_AliasTransformVerts(pheader->num_xyz); - - R_LightModel(ent, pheader->num_xyz, org, color); - - if (!r_render.value) - return; - // LordHavoc: big mess... - // using vertex arrays only slightly, although it is enough to prevent duplicates - // (saving half the transforms) - glVertexPointer(3, GL_FLOAT, 0, aliasvert); - glColorPointer(4, GL_UNSIGNED_BYTE, 0, aliasvertcolor); - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_COLOR_ARRAY); - - order = (int *)((int)pheader + pheader->ofs_glcmds); - while(1) + + if (colormapped) { - if (!(count = *order++)) - break; - if (count > 0) - glBegin(GL_TRIANGLE_STRIP); - else + if (skinframe->pants) { - glBegin(GL_TRIANGLE_FAN); - count = -count; + tex = R_GetTexture(skinframe->pants); + if (tex) + { + memset(&m, 0, sizeof(m)); + m.blendfunc1 = blendfunc1; + m.blendfunc2 = blendfunc2; + colorscale = r_colorscale; + if (gl_combine.integer) + { + colorscale *= 0.25f; + m.texrgbscale[0] = 4; + } + m.tex[0] = tex; + R_Mesh_State(&m); + if (pantsfullbright) + GL_Color(pantscolor[0] * colorscale * ifog, pantscolor[1] * colorscale * ifog, pantscolor[2] * colorscale * ifog, ent->alpha); + else + { + GL_UseColorArray(); + R_LightModel(ent, model->numverts, varray_vertex, aliasvert_normals, varray_color, pantscolor[0] * colorscale * ifog, pantscolor[1] * colorscale * ifog, pantscolor[2] * colorscale * ifog, false); + } + R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices); + c_alias_polys += model->numtris; + blendfunc1 = GL_SRC_ALPHA; + blendfunc2 = GL_ONE; + } } - do + if (skinframe->shirt) { - glTexCoord2f(((float *)order)[0], ((float *)order)[1]); - glArrayElement(order[2]); - order += 3; + tex = R_GetTexture(skinframe->shirt); + if (tex) + { + memset(&m, 0, sizeof(m)); + m.blendfunc1 = blendfunc1; + m.blendfunc2 = blendfunc2; + colorscale = r_colorscale; + if (gl_combine.integer) + { + colorscale *= 0.25f; + m.texrgbscale[0] = 4; + } + m.tex[0] = tex; + R_Mesh_State(&m); + if (shirtfullbright) + GL_Color(shirtcolor[0] * colorscale * ifog, shirtcolor[1] * colorscale * ifog, shirtcolor[2] * colorscale * ifog, ent->alpha); + else + { + GL_UseColorArray(); + R_LightModel(ent, model->numverts, varray_vertex, aliasvert_normals, varray_color, shirtcolor[0] * colorscale * ifog, shirtcolor[1] * colorscale * ifog, shirtcolor[2] * colorscale * ifog, false); + } + R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices); + c_alias_polys += model->numtris; + blendfunc1 = GL_SRC_ALPHA; + blendfunc2 = GL_ONE; + } } - while (count--); } - - glDisableClientState(GL_COLOR_ARRAY); - glDisableClientState(GL_VERTEX_ARRAY); - - if (fogenabled) + if (skinframe->glow) { - glDisable (GL_TEXTURE_2D); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable (GL_BLEND); - glDepthMask(0); // disable zbuffer updates + tex = R_GetTexture(skinframe->glow); + if (tex) { - vec3_t diff; - VectorSubtract(org, r_refdef.vieworg, diff); - glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff))); + memset(&m, 0, sizeof(m)); + m.blendfunc1 = blendfunc1; + m.blendfunc2 = blendfunc2; + m.tex[0] = tex; + R_Mesh_State(&m); + + blendfunc1 = GL_SRC_ALPHA; + blendfunc2 = GL_ONE; + c_alias_polys += model->numtris; + GL_Color(ifog * r_colorscale, ifog * r_colorscale, ifog * r_colorscale, ent->alpha); + R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices); } + } + if (fog) + { + memset(&m, 0, sizeof(m)); + m.blendfunc1 = GL_SRC_ALPHA; + m.blendfunc2 = GL_ONE; + m.tex[0] = R_GetTexture(skinframe->fog); + R_Mesh_State(&m); - // LordHavoc: big mess... - // using vertex arrays only slightly, although it is enough to prevent duplicates - // (saving half the transforms) - glVertexPointer(3, GL_FLOAT, 0, aliasvert); - glEnableClientState(GL_VERTEX_ARRAY); + c_alias_polys += model->numtris; + GL_Color(fogcolor[0] * fog * r_colorscale, fogcolor[1] * fog * r_colorscale, fogcolor[2] * fog * r_colorscale, ent->alpha); + R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices); + } +} - order = (int *)((int)pheader + pheader->ofs_glcmds); - while(1) +void R_Model_Alias_Draw(entity_render_t *ent) +{ + if (ent->alpha < (1.0f / 64.0f)) + return; // basically completely transparent + + c_models++; + + if (ent->effects & EF_ADDITIVE || ent->alpha != 1.0 || R_FetchSkinFrame(ent)->fog != NULL) + R_MeshQueue_AddTransparent(ent->origin, R_DrawQ1Q2AliasModelCallback, ent, 0); + else + R_DrawQ1Q2AliasModelCallback(ent, 0); +} + +extern cvar_t r_shadows; +void R_Model_Alias_DrawFakeShadow (entity_render_t *ent) +{ + int i; + rmeshstate_t m; + model_t *model; + float *v, planenormal[3], planedist, dist, projection[3], floororigin[3], surfnormal[3], lightdirection[3], v2[3]; + + /* + if (r_shadows.integer > 1) + { + float f, lightscale, lightcolor[3]; + vec3_t temp; + mlight_t *sl; + rdlight_t *rd; + memset(&m, 0, sizeof(m)); + m.blendfunc1 = GL_ONE; + m.blendfunc2 = GL_ONE; + R_Mesh_State(&m); + R_Mesh_Matrix(&ent->matrix); + for (i = 0, sl = cl.worldmodel->lights;i < cl.worldmodel->numlights;i++, sl++) { - if (!(count = *order++)) - break; - if (count > 0) - glBegin(GL_TRIANGLE_STRIP); - else + if (d_lightstylevalue[sl->style] > 0) { - glBegin(GL_TRIANGLE_FAN); - count = -count; + VectorSubtract(ent->origin, sl->origin, temp); + f = DotProduct(temp,temp); + if (f < (ent->model->radius2 + sl->cullradius2)) + { + model = ent->model; + R_Mesh_ResizeCheck(model->numverts * 2); + R_LerpMDLMD2Vertices(ent, varray_vertex, aliasvert_normals); + Matrix4x4_Transform(&ent->inversematrix, sl->origin, temp); + GL_Color(0.1 * r_colorscale, 0.025 * r_colorscale, 0.0125 * r_colorscale, 1); + R_Shadow_Volume(model->numverts, model->numtris, varray_vertex, model->mdlmd2data_indices, model->mdlmd2data_triangleneighbors, temp, sl->cullradius + model->radius - sqrt(f), true); + GL_UseColorArray(); + lightscale = d_lightstylevalue[sl->style] * (1.0f / 65536.0f); + VectorScale(sl->light, lightscale, lightcolor); + R_Shadow_VertexLight(model->numverts, varray_vertex, aliasvert_normals, temp, sl->cullradius2, sl->distbias, sl->subtract, lightcolor); + R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices); + } } - do + } + for (i = 0, rd = r_dlight;i < r_numdlights;i++, rd++) + { + if (ent != rd->ent) { - glArrayElement(order[2]); - order += 3; + VectorSubtract(ent->origin, rd->origin, temp); + f = DotProduct(temp,temp); + if (f < (ent->model->radius2 + rd->cullradius2)) + { + model = ent->model; + R_Mesh_ResizeCheck(model->numverts * 2); + R_LerpMDLMD2Vertices(ent, varray_vertex, aliasvert_normals); + Matrix4x4_Transform(&ent->inversematrix, rd->origin, temp); + GL_Color(0.1 * r_colorscale, 0.025 * r_colorscale, 0.0125 * r_colorscale, 1); + R_Shadow_Volume(model->numverts, model->numtris, varray_vertex, model->mdlmd2data_indices, model->mdlmd2data_triangleneighbors, temp, rd->cullradius + model->radius - sqrt(f), true); + GL_UseColorArray(); + R_Shadow_VertexLight(model->numverts, varray_vertex, aliasvert_normals, temp, rd->cullradius2, LIGHTOFFSET, rd->subtract, rd->light); + R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices); + } } - while (count--); } + return; + } + */ + + lightdirection[0] = 0.5; + lightdirection[1] = 0.2; + lightdirection[2] = -1; + VectorNormalizeFast(lightdirection); + + VectorMA(ent->origin, 65536.0f, lightdirection, v2); + if (CL_TraceLine(ent->origin, v2, floororigin, surfnormal, 0, false, NULL) == 1) + return; + + R_Mesh_Matrix(&ent->matrix); + + model = ent->model; + R_Mesh_ResizeCheck(model->numverts); + + memset(&m, 0, sizeof(m)); + m.blendfunc1 = GL_SRC_ALPHA; + m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA; + R_Mesh_State(&m); + + c_alias_polys += model->numtris; + R_LerpMDLMD2Vertices(ent, varray_vertex, aliasvert_normals); + + // put a light direction in the entity's coordinate space + Matrix4x4_Transform3x3(&ent->inversematrix, lightdirection, projection); + VectorNormalizeFast(projection); - glDisableClientState(GL_VERTEX_ARRAY); + // put the plane's normal in the entity's coordinate space + Matrix4x4_Transform3x3(&ent->inversematrix, surfnormal, planenormal); + VectorNormalizeFast(planenormal); - glEnable (GL_TEXTURE_2D); - glColor3f (1,1,1); + // put the plane's distance in the entity's coordinate space + VectorSubtract(floororigin, ent->origin, floororigin); + planedist = DotProduct(floororigin, surfnormal) + 2; + + dist = -1.0f / DotProduct(projection, planenormal); + VectorScale(projection, dist, projection); + for (i = 0, v = varray_vertex;i < model->numverts;i++, v += 4) + { + dist = DotProduct(v, planenormal) - planedist; + if (dist > 0) + //if (i & 1) + VectorMA(v, dist, projection, v); } + GL_Color(0, 0, 0, 0.5); + R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices); +} + +void R_Model_Alias_DrawBaseLighting(entity_render_t *ent) +{ + R_Mesh_Matrix(&ent->matrix); + GL_Color(0, 0, 0, 1); + R_Mesh_ResizeCheck(ent->model->numverts); + R_LerpMDLMD2Vertices(ent, varray_vertex, aliasvert_normals); + R_Mesh_Draw(ent->model->numverts, ent->model->numtris, ent->model->mdlmd2data_indices); +} - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable (GL_BLEND); - glDepthMask(1); +void R_Model_Alias_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int visiblevolume) +{ + float projectdistance; + projectdistance = lightradius + ent->model->radius - sqrt(DotProduct(relativelightorigin, relativelightorigin)); + if (projectdistance > 0.1) + { + R_Mesh_Matrix(&ent->matrix); + R_Mesh_ResizeCheck(ent->model->numverts * 2); + R_LerpMDLMD2Vertices(ent, varray_vertex, aliasvert_normals); + R_Shadow_Volume(ent->model->numverts, ent->model->numtris, varray_vertex, ent->model->mdlmd2data_indices, ent->model->mdlmd2data_triangleneighbors, relativelightorigin, lightradius, projectdistance, visiblevolume); + } +} - if (gl_transform.value) - glPopMatrix(); +void R_Model_Alias_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float lightdistbias, float lightsubtract, float *lightcolor) +{ + skinframe_t *skinframe; + R_Mesh_Matrix(&ent->matrix); + skinframe = R_FetchSkinFrame(ent); + R_Mesh_ResizeCheck(ent->model->numverts); + R_LerpMDLMD2Vertices(ent, varray_vertex, aliasvert_normals); + Mod_BuildTextureVectorsAndNormals(ent->model->numverts, ent->model->numtris, varray_vertex, ent->model->mdlmd2data_texcoords, ent->model->mdlmd2data_indices, aliasvert_svectors, aliasvert_tvectors, aliasvert_normals); + R_Shadow_RenderLighting(ent->model->numverts, ent->model->numtris, ent->model->mdlmd2data_indices, aliasvert_svectors, aliasvert_tvectors, aliasvert_normals, ent->model->mdlmd2data_texcoords, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, skinframe->base, r_notexture, NULL, NULL); } -void ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, zymbone_t *bone, float rootorigin[3], float rootangles[3], float rootscale) +int ZymoticLerpBones(int count, const zymbonematrix *bonebase, const frameblend_t *blend, const zymbone_t *bone) { + int i; float lerp1, lerp2, lerp3, lerp4; - zymbonematrix *out, rootmatrix, m, *bone1, *bone2, *bone3, *bone4; - lerp1 = 1 - lerp2; - out = zymbonepose; - AngleVectors(rootangles, rootmatrix.m[0], rootmatrix.m[1], rootmatrix.m[2]); - VectorScale(rootmatrix.m[0], rootscale, rootmatrix.m[0]); - VectorScale(rootmatrix.m[1], rootscale, rootmatrix.m[1]); - VectorScale(rootmatrix.m[2], rootscale, rootmatrix.m[2]); - rootmatrix.m[0][3] = rootorigin[0]; - rootmatrix.m[1][3] = rootorigin[1]; - rootmatrix.m[2][3] = rootorigin[2]; + zymbonematrix *out, rootmatrix, m; + const zymbonematrix *bone1, *bone2, *bone3, *bone4; + + rootmatrix.m[0][0] = 1; + rootmatrix.m[0][1] = 0; + rootmatrix.m[0][2] = 0; + rootmatrix.m[0][3] = 0; + rootmatrix.m[1][0] = 0; + rootmatrix.m[1][1] = 1; + rootmatrix.m[1][2] = 0; + rootmatrix.m[1][3] = 0; + rootmatrix.m[2][0] = 0; + rootmatrix.m[2][1] = 0; + rootmatrix.m[2][2] = 1; + rootmatrix.m[2][3] = 0; + bone1 = bonebase + blend[0].frame * count; lerp1 = blend[0].lerp; if (blend[1].lerp) @@ -593,7 +627,7 @@ void ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, z // 4 poses bone4 = bonebase + blend[3].frame * count; lerp4 = blend[3].lerp; - while(count--) + for (i = 0, out = zymbonepose;i < count;i++, out++) { // interpolate matrices m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3 + bone4->m[0][0] * lerp4; @@ -609,21 +643,20 @@ void ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, z m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3 + bone4->m[2][2] * lerp4; m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3 + bone4->m[2][3] * lerp4; if (bone->parent >= 0) - R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &m.m[0], &out->m[0]); + R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]); else - R_ConcatTransforms(&rootmatrix.m[0], &m.m[0], &out->m[0]); + R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]); bone1++; bone2++; bone3++; bone4++; bone++; - out++; } } else { // 3 poses - while(count--) + for (i = 0, out = zymbonepose;i < count;i++, out++) { // interpolate matrices m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3; @@ -639,21 +672,20 @@ void ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, z m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3; m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3; if (bone->parent >= 0) - R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &m.m[0], &out->m[0]); + R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]); else - R_ConcatTransforms(&rootmatrix.m[0], &m.m[0], &out->m[0]); + R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]); bone1++; bone2++; bone3++; bone++; - out++; } } } else { // 2 poses - while(count--) + for (i = 0, out = zymbonepose;i < count;i++, out++) { // interpolate matrices m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2; @@ -669,13 +701,12 @@ void ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, z m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2; m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2; if (bone->parent >= 0) - R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &m.m[0], &out->m[0]); + R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]); else - R_ConcatTransforms(&rootmatrix.m[0], &m.m[0], &out->m[0]); + R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]); bone1++; bone2++; bone++; - out++; } } } @@ -685,7 +716,7 @@ void ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, z if (lerp1 != 1) { // lerp != 1.0 - while(count--) + for (i = 0, out = zymbonepose;i < count;i++, out++) { // interpolate matrices m.m[0][0] = bone1->m[0][0] * lerp1; @@ -701,39 +732,40 @@ void ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, z m.m[2][2] = bone1->m[2][2] * lerp1; m.m[2][3] = bone1->m[2][3] * lerp1; if (bone->parent >= 0) - R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &m.m[0], &out->m[0]); + R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]); else - R_ConcatTransforms(&rootmatrix.m[0], &m.m[0], &out->m[0]); + R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]); bone1++; bone++; - out++; } } else { // lerp == 1.0 - while(count--) + for (i = 0, out = zymbonepose;i < count;i++, out++) { if (bone->parent >= 0) - R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &bone1->m[0], &out->m[0]); + R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &bone1->m[0][0], &out->m[0][0]); else - R_ConcatTransforms(&rootmatrix.m[0], &bone1->m[0], &out->m[0]); + R_ConcatTransforms(&rootmatrix.m[0][0], &bone1->m[0][0], &out->m[0][0]); bone1++; bone++; - out++; } } } + return true; } -void ZymoticTransformVerts(int vertcount, int *bonecounts, zymvertex_t *vert) +void ZymoticTransformVerts(int vertcount, float *vertex, int *bonecounts, zymvertex_t *vert) { int c; - float *out = aliasvert; + float *out = vertex; zymbonematrix *matrix; while(vertcount--) { c = *bonecounts++; + // FIXME: validate bonecounts at load time (must be >= 1) + // FIXME: need 4th component in origin, for how much of the translate to blend in if (c == 1) { matrix = &zymbonepose[vert->bonenum]; @@ -754,25 +786,17 @@ void ZymoticTransformVerts(int vertcount, int *bonecounts, zymvertex_t *vert) vert++; } } - out += 3; + out += 4; } } -float ixtable[4096]; - -void ZymoticCalcNormals(int vertcount, int shadercount, int *renderlist) +void ZymoticCalcNormals(int vertcount, float *vertex, float *normals, int shadercount, int *renderlist) { int a, b, c, d; - float *out, v1[3], v2[3], normal[3]; + float *out, v1[3], v2[3], normal[3], s; int *u; - if (!ixtable[1]) - { - ixtable[0] = 0; - for (a = 1;a < 4096;a++) - ixtable[a] = 1.0f / a; - } // clear normals - memset(aliasvertnorm, 0, sizeof(float[3]) * vertcount); + memset(normals, 0, sizeof(float) * vertcount * 3); memset(aliasvertusage, 0, sizeof(int) * vertcount); // parse render list and accumulate surface normals while(shadercount--) @@ -780,210 +804,172 @@ void ZymoticCalcNormals(int vertcount, int shadercount, int *renderlist) d = *renderlist++; while (d--) { - a = renderlist[0]*3; - b = renderlist[1]*3; - c = renderlist[2]*3; - v1[0] = aliasvert[a+0] - aliasvert[b+0]; - v1[1] = aliasvert[a+1] - aliasvert[b+1]; - v1[2] = aliasvert[a+2] - aliasvert[b+2]; - v2[0] = aliasvert[c+0] - aliasvert[b+0]; - v2[1] = aliasvert[c+1] - aliasvert[b+1]; - v2[2] = aliasvert[c+2] - aliasvert[b+2]; + a = renderlist[0]*4; + b = renderlist[1]*4; + c = renderlist[2]*4; + v1[0] = vertex[a+0] - vertex[b+0]; + v1[1] = vertex[a+1] - vertex[b+1]; + v1[2] = vertex[a+2] - vertex[b+2]; + v2[0] = vertex[c+0] - vertex[b+0]; + v2[1] = vertex[c+1] - vertex[b+1]; + v2[2] = vertex[c+2] - vertex[b+2]; CrossProduct(v1, v2, normal); - VectorNormalize(normal); + VectorNormalizeFast(normal); // add surface normal to vertices - aliasvertnorm[a+0] += normal[0]; - aliasvertnorm[a+1] += normal[1]; - aliasvertnorm[a+2] += normal[2]; - aliasvertusage[a]++; - aliasvertnorm[b+0] += normal[0]; - aliasvertnorm[b+1] += normal[1]; - aliasvertnorm[b+2] += normal[2]; - aliasvertusage[b]++; - aliasvertnorm[c+0] += normal[0]; - aliasvertnorm[c+1] += normal[1]; - aliasvertnorm[c+2] += normal[2]; - aliasvertusage[c]++; + a = renderlist[0] * 3; + normals[a+0] += normal[0]; + normals[a+1] += normal[1]; + normals[a+2] += normal[2]; + aliasvertusage[renderlist[0]]++; + a = renderlist[1] * 3; + normals[a+0] += normal[0]; + normals[a+1] += normal[1]; + normals[a+2] += normal[2]; + aliasvertusage[renderlist[1]]++; + a = renderlist[2] * 3; + normals[a+0] += normal[0]; + normals[a+1] += normal[1]; + normals[a+2] += normal[2]; + aliasvertusage[renderlist[2]]++; renderlist += 3; } } + // FIXME: precalc this // average surface normals - out = aliasvertnorm; + out = normals; u = aliasvertusage; while(vertcount--) { if (*u > 1) { - a = ixtable[*u]; - out[0] *= a; - out[1] *= a; - out[2] *= a; + s = ixtable[*u]; + out[0] *= s; + out[1] *= s; + out[2] *= s; } u++; out += 3; } } -void GL_DrawZymoticModelMesh(byte *colors, zymtype1header_t *m) -{ - int i, c, *renderlist; - rtexture_t **texture; - if (!r_render.value) - return; - renderlist = (int *)(m->lump_render.start + (int) m); - texture = (rtexture_t **)(m->lump_shaders.start + (int) m); - glVertexPointer(3, GL_FLOAT, 0, aliasvert); - glEnableClientState(GL_VERTEX_ARRAY); - - glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors); - glEnableClientState(GL_COLOR_ARRAY); - - glTexCoordPointer(2, GL_FLOAT, 0, (float *)(m->lump_texcoords.start + (int) m)); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - - for (i = 0;i < m->numshaders;i++) - { - c = (*renderlist++) * 3; - glBindTexture(GL_TEXTURE_2D, R_GetTexture(*texture)); - texture++; - glDrawElements(GL_TRIANGLES, c, GL_UNSIGNED_INT, renderlist); - renderlist += c; - } - - glDisableClientState(GL_TEXTURE_COORD_ARRAY); - - glDisableClientState(GL_COLOR_ARRAY); - - glDisableClientState(GL_VERTEX_ARRAY); -} - -void GL_DrawZymoticModelMeshFog(vec3_t org, zymtype1header_t *m) +void R_DrawZymoticModelMeshCallback (const void *calldata1, int calldata2) { + float fog, ifog, colorscale; vec3_t diff; - int i, c, *renderlist; - if (!r_render.value) - return; - renderlist = (int *)(m->lump_render.start + (int) m); - glDisable(GL_TEXTURE_2D); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable (GL_BLEND); - glDepthMask(0); // disable zbuffer updates - - VectorSubtract(org, r_refdef.vieworg, diff); - glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], exp(fogdensity/DotProduct(diff,diff))); - - glVertexPointer(3, GL_FLOAT, 0, aliasvert); - glEnableClientState(GL_VERTEX_ARRAY); - - for (i = 0;i < m->numshaders;i++) + int i, *renderlist, *elements; + rtexture_t *texture; + rmeshstate_t mstate; + const entity_render_t *ent = calldata1; + int shadernum = calldata2; + int numverts, numtriangles; + + R_Mesh_Matrix(&ent->matrix); + + // find the vertex index list and texture + renderlist = ent->model->zymdata_renderlist; + for (i = 0;i < shadernum;i++) + renderlist += renderlist[0] * 3 + 1; + texture = ent->model->zymdata_textures[shadernum]; + + numverts = ent->model->zymnum_verts; + numtriangles = *renderlist++; + elements = renderlist; + R_Mesh_ResizeCheck(numverts); + + fog = 0; + if (fogenabled) { - c = (*renderlist++) * 3; - glDrawElements(GL_TRIANGLES, c, GL_UNSIGNED_INT, renderlist); - renderlist += c; + VectorSubtract(ent->origin, r_origin, diff); + fog = DotProduct(diff,diff); + if (fog < 0.01f) + fog = 0.01f; + fog = exp(fogdensity/fog); + if (fog > 1) + fog = 1; + if (fog < 0.01f) + fog = 0; + // fog method: darken, additive fog + // 1. render model as normal, scaled by inverse of fog alpha (darkens it) + // 2. render fog as additive } + ifog = 1 - fog; - glDisableClientState(GL_VERTEX_ARRAY); - - glEnable(GL_TEXTURE_2D); - glColor3f (1,1,1); -} - -/* -================= -R_DrawZymoticFrame -================= -*/ -void R_DrawZymoticFrame (zymtype1header_t *m, float alpha, vec3_t color, entity_t *ent, int shadow, vec3_t org, vec3_t angles, vec_t scale, frameblend_t *blend, int skinblah, int effects, int flags) -{ - ZymoticLerpBones(m->numbones, (zymbonematrix *)(m->lump_poses.start + (int) m), blend, (zymbone_t *)(m->lump_bones.start + (int) m), org, angles, scale); - ZymoticTransformVerts(m->numverts, (int *)(m->lump_vertbonecounts.start + (int) m), (zymvertex_t *)(m->lump_verts.start + (int) m)); - ZymoticCalcNormals(m->numverts, m->numshaders, (int *)(m->lump_render.start + (int) m)); - - R_LightModel(ent, m->numverts, org, color); - - if (!r_render.value) - return; - glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - glShadeModel(GL_SMOOTH); - if (effects & EF_ADDITIVE) + memset(&mstate, 0, sizeof(mstate)); + if (ent->effects & EF_ADDITIVE) { - glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive rendering - glEnable(GL_BLEND); - glDepthMask(0); + mstate.blendfunc1 = GL_SRC_ALPHA; + mstate.blendfunc2 = GL_ONE; } - else if (alpha != 1.0) + else if (ent->alpha != 1.0 || R_TextureHasAlpha(texture)) { - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable(GL_BLEND); - glDepthMask(0); + mstate.blendfunc1 = GL_SRC_ALPHA; + mstate.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA; } else { - glDisable(GL_BLEND); - glDepthMask(1); + mstate.blendfunc1 = GL_ONE; + mstate.blendfunc2 = GL_ZERO; + } + colorscale = r_colorscale; + if (gl_combine.integer) + { + mstate.texrgbscale[0] = 4; + colorscale *= 0.25f; + } + mstate.tex[0] = R_GetTexture(texture); + R_Mesh_State(&mstate); + ZymoticLerpBones(ent->model->zymnum_bones, (zymbonematrix *) ent->model->zymdata_poses, ent->frameblend, ent->model->zymdata_bones); + ZymoticTransformVerts(numverts, varray_vertex, ent->model->zymdata_vertbonecounts, ent->model->zymdata_verts); + ZymoticCalcNormals(numverts, varray_vertex, aliasvert_normals, ent->model->zymnum_shaders, ent->model->zymdata_renderlist); + memcpy(varray_texcoord[0], ent->model->zymdata_texcoords, ent->model->zymnum_verts * sizeof(float[4])); + GL_UseColorArray(); + R_LightModel(ent, numverts, varray_vertex, aliasvert_normals, varray_color, ifog * colorscale, ifog * colorscale, ifog * colorscale, false); + R_Mesh_Draw(numverts, numtriangles, elements); + c_alias_polys += numtriangles; + + if (fog) + { + memset(&mstate, 0, sizeof(mstate)); + mstate.blendfunc1 = GL_SRC_ALPHA; + mstate.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA; + // FIXME: need alpha mask for fogging... + //mstate.tex[0] = R_GetTexture(texture); + R_Mesh_State(&mstate); + GL_Color(fogcolor[0] * r_colorscale, fogcolor[1] * r_colorscale, fogcolor[2] * r_colorscale, ent->alpha * fog); + R_Mesh_Draw(numverts, numtriangles, elements); + c_alias_polys += numtriangles; } - - GL_DrawZymoticModelMesh(aliasvertcolor, m); - - if (fogenabled) - GL_DrawZymoticModelMeshFog(org, m); - - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable (GL_BLEND); - glDepthMask(1); } -/* -================= -R_DrawAliasModel - -================= -*/ -void R_DrawAliasModel (entity_t *ent, int cull, float alpha, model_t *clmodel, frameblend_t *blend, int skin, vec3_t org, vec3_t angles, vec_t scale, int effects, int flags, int colormap) +void R_Model_Zymotic_Draw(entity_render_t *ent) { - int i; - vec3_t mins, maxs, color; - void *modelheader; - rtexture_t **skinset; + int i; - if (alpha < (1.0 / 64.0)) + if (ent->alpha < (1.0f / 64.0f)) return; // basically completely transparent - VectorAdd (org, clmodel->mins, mins); - VectorAdd (org, clmodel->maxs, maxs); - - if (cull && R_CullBox (mins, maxs)) - return; - c_models++; - if (skin < 0 || skin >= clmodel->numskins) + for (i = 0;i < ent->model->zymnum_shaders;i++) { - skin = 0; - Con_DPrintf("invalid skin number %d for model %s\n", skin, clmodel->name); + if (ent->effects & EF_ADDITIVE || ent->alpha != 1.0 || R_TextureHasAlpha(ent->model->zymdata_textures[i])) + R_MeshQueue_AddTransparent(ent->origin, R_DrawZymoticModelMeshCallback, ent, i); + else + R_DrawZymoticModelMeshCallback(ent, i); } +} - modelheader = Mod_Extradata (clmodel); - - { -// int *skinanimrange = (int *) (clmodel->skinanimrange + (int) modelheader) + skin * 2; -// int *skinanim = (int *) (clmodel->skinanim + (int) modelheader); - int *skinanimrange = clmodel->skinanimrange + skin * 2; - rtexture_t **skinanim = clmodel->skinanim; - i = skinanimrange[0]; - if (skinanimrange[1] > 1) // animated - i += ((int) (cl.time * 10) % skinanimrange[1]); - skinset = skinanim + i*5; - } +void R_Model_Zymotic_DrawFakeShadow(entity_render_t *ent) +{ + // FIXME +} - if (r_render.value) - glEnable (GL_TEXTURE_2D); +void R_Model_Zymotic_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, float lightradius2, float lightdistbias, float lightsubtract, float *lightcolor) +{ + // FIXME +} - c_alias_polys += clmodel->numtris; - if (clmodel->aliastype == ALIASTYPE_ZYM) - R_DrawZymoticFrame (modelheader, alpha, color, ent, ent != &cl.viewent, org, angles, scale, blend, 0 , effects, flags); - else if (clmodel->aliastype == ALIASTYPE_MD2) - R_DrawQ2AliasFrame (modelheader, alpha, color, ent, ent != &cl.viewent, org, angles, scale, blend, skinset[0] , effects, flags); - else - R_DrawAliasFrame (modelheader, alpha, color, ent, ent != &cl.viewent, org, angles, scale, blend, skinset , colormap, effects, flags); +void R_Model_Zymotic_DrawOntoLight(entity_render_t *ent) +{ + // FIXME }