X-Git-Url: http://de.git.xonotic.org/?a=blobdiff_plain;f=gl_models.c;h=36e172e1b2d7982486a63793754a0e217b1896b1;hb=ef94579586d37fab4d379781c10da8f10648186d;hp=8b40edda055c07a8d567dc765702371f5c367d41;hpb=90fa50f4e170192cce8b5b3321fbf7c4d9be7a41;p=xonotic%2Fdarkplaces.git diff --git a/gl_models.c b/gl_models.c index 8b40edda..36e172e1 100644 --- a/gl_models.c +++ b/gl_models.c @@ -1,7 +1,7 @@ #include "quakedef.h" - -cvar_t r_quickmodels = {0, "r_quickmodels", "1"}; +#include "cl_collision.h" +#include "r_shadow.h" typedef struct { @@ -10,13 +10,13 @@ typedef struct // LordHavoc: vertex arrays -float *aliasvertbuf; float *aliasvertcolorbuf; -float *aliasvert; // this may point at aliasvertbuf or at vertex arrays in the mesh backend float *aliasvertcolor; // this may point at aliasvertcolorbuf or at vertex arrays in the mesh backend +float *aliasvert_svectors; +float *aliasvert_tvectors; +float *aliasvert_normals; float *aliasvertcolor2; -float *aliasvertnorm; int *aliasvertusage; zymbonematrix *zymbonepose; @@ -26,9 +26,10 @@ void gl_models_start(void) { // allocate vertex processing arrays gl_models_mempool = Mem_AllocPool("GL_Models"); - aliasvert = aliasvertbuf = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4])); aliasvertcolor = aliasvertcolorbuf = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4])); - aliasvertnorm = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][3])); + 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])); @@ -45,43 +46,21 @@ void gl_models_newmap(void) void GL_Models_Init(void) { - Cvar_RegisterVariable(&r_quickmodels); - R_RegisterModule("GL_Models", gl_models_start, gl_models_shutdown, gl_models_newmap); } -void R_AliasTransformVerts(int vertcount) -{ - vec3_t point; - float *av; - av = aliasvert; - while (vertcount >= 4) - { - VectorCopy(av, point);softwaretransform(point, av);av += 4; - VectorCopy(av, point);softwaretransform(point, av);av += 4; - VectorCopy(av, point);softwaretransform(point, av);av += 4; - VectorCopy(av, point);softwaretransform(point, av);av += 4; - vertcount -= 4; - } - while(vertcount > 0) - { - VectorCopy(av, point);softwaretransform(point, av);av += 4; - vertcount--; - } -} - -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; + av = vertices; + avn = normals; VectorScale(fscale1, lerp1, scale1); if (lerp2) { @@ -109,7 +88,7 @@ void R_AliasLerpVerts(int vertcount, 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 += 4; - avn += 3; + avn += 4; verts1++;verts2++;verts3++;verts4++; } } @@ -131,7 +110,7 @@ void R_AliasLerpVerts(int vertcount, avn[1] = n1[1] * lerp1 + n2[1] * lerp2 + n3[1] * lerp3; avn[2] = n1[2] * lerp1 + n2[2] * lerp2 + n3[2] * lerp3; av += 4; - avn += 3; + avn += 4; verts1++;verts2++;verts3++; } } @@ -153,7 +132,7 @@ void R_AliasLerpVerts(int vertcount, avn[1] = n1[1] * lerp1 + n2[1] * lerp2; avn[2] = n1[2] * lerp1 + n2[2] * lerp2; av += 4; - avn += 3; + avn += 4; verts1++;verts2++; } } @@ -177,7 +156,7 @@ void R_AliasLerpVerts(int vertcount, avn[1] = n1[1] * lerp1; avn[2] = n1[2] * lerp1; av += 4; - avn += 3; + avn += 4; verts1++; } } @@ -191,116 +170,139 @@ void R_AliasLerpVerts(int vertcount, av[2] = verts1->v[2] * scale1[2] + translate[2]; VectorCopy(m_bytenormals[verts1->lightnormalindex], avn); av += 4; - avn += 3; + avn += 4; verts1++; } } } } -skinframe_t *R_FetchSkinFrame(void) +skinframe_t *R_FetchSkinFrame(const entity_render_t *ent) { - model_t *model = currentrenderentity->model; - if (model->skinscenes[currentrenderentity->skinnum].framecount > 1) - return &model->skinframes[model->skinscenes[currentrenderentity->skinnum].firstframe + (int) (cl.time * 10) % model->skinscenes[currentrenderentity->skinnum].framecount]; + 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[currentrenderentity->skinnum].firstframe]; + return &model->skinframes[model->skinscenes[s].firstframe]; } -void R_SetupMDLMD2Frames(float colorr, float colorg, float colorb) +void R_LerpMDLMD2Vertices(const entity_render_t *ent, float *vertices, float *normals) { - md2frame_t *frame1, *frame2, *frame3, *frame4; - trivertx_t *frame1verts, *frame2verts, *frame3verts, *frame4verts; - model_t *model; - model = currentrenderentity->model; - - frame1 = &model->mdlmd2data_frames[currentrenderentity->frameblend[0].frame]; - frame2 = &model->mdlmd2data_frames[currentrenderentity->frameblend[1].frame]; - frame3 = &model->mdlmd2data_frames[currentrenderentity->frameblend[2].frame]; - frame4 = &model->mdlmd2data_frames[currentrenderentity->frameblend[3].frame]; - frame1verts = &model->mdlmd2data_pose[currentrenderentity->frameblend[0].frame * model->numverts]; - frame2verts = &model->mdlmd2data_pose[currentrenderentity->frameblend[1].frame * model->numverts]; - frame3verts = &model->mdlmd2data_pose[currentrenderentity->frameblend[2].frame * model->numverts]; - frame4verts = &model->mdlmd2data_pose[currentrenderentity->frameblend[3].frame * model->numverts]; - R_AliasLerpVerts(model->numverts, - currentrenderentity->frameblend[0].lerp, frame1verts, frame1->scale, frame1->translate, - currentrenderentity->frameblend[1].lerp, frame2verts, frame2->scale, frame2->translate, - currentrenderentity->frameblend[2].lerp, frame3verts, frame3->scale, frame3->translate, - currentrenderentity->frameblend[3].lerp, frame4verts, frame4->scale, frame4->translate); - - R_LightModel(model->numverts, colorr, colorg, colorb, false); - - R_AliasTransformVerts(model->numverts); + 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); } -void R_DrawQ1Q2AliasModel (float fog) +void R_DrawQ1Q2AliasModelCallback (const void *calldata1, int calldata2) { - int c, pantsfullbright, shirtfullbright, colormapped; + int i, c, fullbright, pantsfullbright, shirtfullbright, colormapped, tex; float pantscolor[3], shirtcolor[3]; + float fog, ifog, colorscale; + vec3_t diff; qbyte *bcolor; - rmeshbufferinfo_t m; + rmeshstate_t m; model_t *model; skinframe_t *skinframe; + const entity_render_t *ent = calldata1; + int blendfunc1, blendfunc2; + + R_Mesh_Matrix(&ent->matrix); - model = currentrenderentity->model; + model = ent->model; + R_Mesh_ResizeCheck(model->numverts); + + skinframe = R_FetchSkinFrame(ent); + + fullbright = (ent->effects & EF_FULLBRIGHT) != 0; + + fog = 0; + if (fogenabled) + { + 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; - skinframe = R_FetchSkinFrame(); + if (ent->effects & EF_ADDITIVE) + { + blendfunc1 = GL_SRC_ALPHA; + blendfunc2 = GL_ONE; + } + else if (ent->alpha != 1.0 || skinframe->fog != NULL) + { + blendfunc1 = GL_SRC_ALPHA; + blendfunc2 = GL_ONE_MINUS_SRC_ALPHA; + } + else + { + blendfunc1 = GL_ONE; + blendfunc2 = GL_ZERO; + } - colormapped = !skinframe->merged || (currentrenderentity->colormap >= 0 && skinframe->base && (skinframe->pants || skinframe->shirt)); - if (!colormapped && !fog && !skinframe->glow && !skinframe->fog) + 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) { - // fastpath for the normal situation (one texture) + // untextured memset(&m, 0, sizeof(m)); - if (currentrenderentity->effects & EF_ADDITIVE) - { - m.transparent = true; - m.blendfunc1 = GL_SRC_ALPHA; - m.blendfunc2 = GL_ONE; - } - else if (currentrenderentity->alpha != 1.0 || skinframe->fog != NULL) - { - m.transparent = true; - m.blendfunc1 = GL_SRC_ALPHA; - m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA; - } - else + m.blendfunc1 = blendfunc1; + m.blendfunc2 = blendfunc2; + colorscale = r_colorscale; + if (gl_combine.integer) { - m.transparent = false; - m.blendfunc1 = GL_ONE; - m.blendfunc2 = GL_ZERO; + colorscale *= 0.25f; + m.texrgbscale[0] = 4; } - m.numtriangles = model->numtris; - m.numverts = model->numverts; - m.tex[0] = R_GetTexture(skinframe->merged); - - c_alias_polys += m.numtriangles; - if (R_Mesh_Draw_GetBuffer(&m, true)) + 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) { - memcpy(m.index, model->mdlmd2data_indices, m.numtriangles * sizeof(int[3])); - memcpy(m.texcoords[0], model->mdlmd2data_texcoords, m.numverts * sizeof(float[2])); - - aliasvert = m.vertex; - aliasvertcolor = m.color; - R_SetupMDLMD2Frames(m.colorscale * (1 - fog), m.colorscale * (1 - fog), m.colorscale * (1 - fog)); - aliasvert = aliasvertbuf; - aliasvertcolor = aliasvertcolorbuf; - - R_Mesh_Render(); + varray_texcoord[0][i + 0] *= 8.0f; + varray_texcoord[0][i + 1] *= 8.0f; } + R_LightModel(ent, model->numverts, varray_vertex, aliasvert_normals, varray_color, colorscale, colorscale, colorscale, false); + R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices); return; } - R_SetupMDLMD2Frames(1 - fog, 1 - fog, 1 - fog); - + colormapped = !skinframe->merged || (ent->colormap >= 0 && skinframe->base && (skinframe->pants || skinframe->shirt)); if (colormapped) { // 128-224 are backwards ranges - c = (currentrenderentity->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12; - bcolor = (qbyte *) (&d_8to24table[c]); + c = (ent->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12; + bcolor = (qbyte *) (&palette_complete[c]); pantsfullbright = c >= 224; VectorScale(bcolor, (1.0f / 255.0f), pantscolor); - c = (currentrenderentity->colormap & 0xF0);c += (c >= 128 && c < 224) ? 4 : 12; - bcolor = (qbyte *) (&d_8to24table[c]); + c = (ent->colormap & 0xF0);c += (c >= 128 && c < 224) ? 4 : 12; + bcolor = (qbyte *) (&palette_complete[c]); shirtfullbright = c >= 224; VectorScale(bcolor, (1.0f / 255.0f), shirtcolor); } @@ -310,144 +312,271 @@ void R_DrawQ1Q2AliasModel (float fog) pantsfullbright = shirtfullbright = false; } - memset(&m, 0, sizeof(m)); - if (currentrenderentity->effects & EF_ADDITIVE) - { - m.transparent = true; - m.blendfunc1 = GL_SRC_ALPHA; - m.blendfunc2 = GL_ONE; - } - else if (currentrenderentity->alpha != 1.0 || skinframe->fog != NULL) - { - m.transparent = true; - m.blendfunc1 = GL_SRC_ALPHA; - m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA; - } - else - { - m.transparent = false; - m.blendfunc1 = GL_ONE; - m.blendfunc2 = GL_ZERO; - } - m.numtriangles = model->numtris; - m.numverts = model->numverts; - m.tex[0] = colormapped ? R_GetTexture(skinframe->base) : R_GetTexture(skinframe->merged); - if (R_Mesh_Draw_GetBuffer(&m, true)) + tex = colormapped ? R_GetTexture(skinframe->base) : R_GetTexture(skinframe->merged); + if (tex) { - c_alias_polys += m.numtriangles; - R_ModulateColors(aliasvertcolor, m.color, m.numverts, m.colorscale, m.colorscale, m.colorscale); - memcpy(m.index, model->mdlmd2data_indices, m.numtriangles * sizeof(int[3])); - memcpy(m.vertex, aliasvert, m.numverts * sizeof(float[4])); - memcpy(m.texcoords[0], model->mdlmd2data_texcoords, m.numverts * sizeof(float[2])); - R_Mesh_Render(); + 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 + 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; } if (colormapped) { if (skinframe->pants) { - memset(&m, 0, sizeof(m)); - m.transparent = currentrenderentity->effects & EF_ADDITIVE || currentrenderentity->alpha != 1.0 || skinframe->fog != NULL; - m.blendfunc1 = GL_SRC_ALPHA; - m.blendfunc2 = GL_ONE; - m.numtriangles = model->numtris; - m.numverts = model->numverts; - m.tex[0] = R_GetTexture(skinframe->pants); - if (R_Mesh_Draw_GetBuffer(&m, true)) + tex = R_GetTexture(skinframe->pants); + if (tex) { - c_alias_polys += m.numtriangles; + 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) - R_FillColors(m.color, m.numverts, pantscolor[0] * m.colorscale, pantscolor[1] * m.colorscale, pantscolor[2] * m.colorscale, currentrenderentity->alpha); + GL_Color(pantscolor[0] * colorscale * ifog, pantscolor[1] * colorscale * ifog, pantscolor[2] * colorscale * ifog, ent->alpha); else - R_ModulateColors(aliasvertcolor, m.color, m.numverts, pantscolor[0] * m.colorscale, pantscolor[1] * m.colorscale, pantscolor[2] * m.colorscale); - memcpy(m.index, model->mdlmd2data_indices, m.numtriangles * sizeof(int[3])); - memcpy(m.vertex, aliasvert, m.numverts * sizeof(float[4])); - memcpy(m.texcoords[0], model->mdlmd2data_texcoords, m.numverts * sizeof(float[2])); - R_Mesh_Render(); + 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; } } if (skinframe->shirt) { - memset(&m, 0, sizeof(m)); - m.transparent = currentrenderentity->effects & EF_ADDITIVE || currentrenderentity->alpha != 1.0 || skinframe->fog != NULL; - m.blendfunc1 = GL_SRC_ALPHA; - m.blendfunc2 = GL_ONE; - m.numtriangles = model->numtris; - m.numverts = model->numverts; - m.tex[0] = R_GetTexture(skinframe->shirt); - if (R_Mesh_Draw_GetBuffer(&m, true)) + tex = R_GetTexture(skinframe->shirt); + if (tex) { - c_alias_polys += m.numtriangles; + 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) - R_FillColors(m.color, m.numverts, shirtcolor[0] * m.colorscale, shirtcolor[1] * m.colorscale, shirtcolor[2] * m.colorscale, currentrenderentity->alpha); + GL_Color(shirtcolor[0] * colorscale * ifog, shirtcolor[1] * colorscale * ifog, shirtcolor[2] * colorscale * ifog, ent->alpha); else - R_ModulateColors(aliasvertcolor, m.color, m.numverts, shirtcolor[0] * m.colorscale, shirtcolor[1] * m.colorscale, shirtcolor[2] * m.colorscale); - memcpy(m.index, model->mdlmd2data_indices, m.numtriangles * sizeof(int[3])); - memcpy(m.vertex, aliasvert, m.numverts * sizeof(float[4])); - memcpy(m.texcoords[0], model->mdlmd2data_texcoords, m.numverts * sizeof(float[2])); - R_Mesh_Render(); + 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; } } } if (skinframe->glow) { - memset(&m, 0, sizeof(m)); - m.transparent = currentrenderentity->effects & EF_ADDITIVE || currentrenderentity->alpha != 1.0 || skinframe->fog != NULL; - m.blendfunc1 = GL_SRC_ALPHA; - m.blendfunc2 = GL_ONE; - m.numtriangles = model->numtris; - m.numverts = model->numverts; - m.tex[0] = R_GetTexture(skinframe->glow); - if (R_Mesh_Draw_GetBuffer(&m, true)) + tex = R_GetTexture(skinframe->glow); + if (tex) { - c_alias_polys += m.numtriangles; - R_FillColors(m.color, m.numverts, (1 - fog) * m.colorscale, (1 - fog) * m.colorscale, (1 - fog) * m.colorscale, currentrenderentity->alpha); - memcpy(m.index, model->mdlmd2data_indices, m.numtriangles * sizeof(int[3])); - memcpy(m.vertex, aliasvert, m.numverts * sizeof(float[4])); - memcpy(m.texcoords[0], model->mdlmd2data_texcoords, m.numverts * sizeof(float[2])); - R_Mesh_Render(); + 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.transparent = currentrenderentity->effects & EF_ADDITIVE || currentrenderentity->alpha != 1.0 || skinframe->fog != NULL; m.blendfunc1 = GL_SRC_ALPHA; m.blendfunc2 = GL_ONE; - m.numtriangles = model->numtris; - m.numverts = model->numverts; m.tex[0] = R_GetTexture(skinframe->fog); - if (R_Mesh_Draw_GetBuffer(&m, false)) + R_Mesh_State(&m); + + 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); + } +} + +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); +} + +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]; + + 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); + + // put the plane's normal in the entity's coordinate space + Matrix4x4_Transform3x3(&ent->inversematrix, surfnormal, planenormal); + VectorNormalizeFast(planenormal); + + // 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_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius) +{ + 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, ent->model->mdlmd2data_indices, ent->model->mdlmd2data_triangleneighbors, relativelightorigin, lightradius, projectdistance); + } +} + +void R_Model_Alias_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor) +{ + int c; + float lightcolor2[3]; + qbyte *bcolor; + skinframe_t *skinframe; + R_Mesh_Matrix(&ent->matrix); + 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); + skinframe = R_FetchSkinFrame(ent); + + // note: to properly handle fog this should scale the lightcolor into lightcolor2 according to 1-fog scaling + + R_Shadow_SpecularLighting(ent->model->numverts, ent->model->numtris, ent->model->mdlmd2data_indices, aliasvert_svectors, aliasvert_tvectors, aliasvert_normals, ent->model->mdlmd2data_texcoords, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, NULL, NULL, NULL); + + if (!skinframe->base && !skinframe->pants && !skinframe->shirt && !skinframe->glow) + { + R_Shadow_DiffuseLighting(ent->model->numverts, ent->model->numtris, ent->model->mdlmd2data_indices, aliasvert_svectors, aliasvert_tvectors, aliasvert_normals, ent->model->mdlmd2data_texcoords, relativelightorigin, lightradius, lightcolor, r_notexture, NULL, NULL); + return; + } + + if (!skinframe->merged || (ent->colormap >= 0 && skinframe->base && (skinframe->pants || skinframe->shirt))) + { + // 128-224 are backwards ranges + // we only render non-fullbright ranges here + if (skinframe->pants && (ent->colormap & 0xF) < 0xE) { - c_alias_polys += m.numtriangles; - R_FillColors(m.color, m.numverts, fog * m.colorscale, fog * m.colorscale, fog * m.colorscale, currentrenderentity->alpha); - memcpy(m.index, model->mdlmd2data_indices, m.numtriangles * sizeof(int[3])); - memcpy(m.vertex, aliasvert, m.numverts * sizeof(float[4])); - memcpy(m.texcoords[0], model->mdlmd2data_texcoords, m.numverts * sizeof(float[2])); - R_Mesh_Render(); + c = (ent->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12; + bcolor = (qbyte *) (&palette_complete[c]); + lightcolor2[0] = lightcolor[0] * bcolor[0] * (1.0f / 255.0f); + lightcolor2[1] = lightcolor[1] * bcolor[1] * (1.0f / 255.0f); + lightcolor2[2] = lightcolor[2] * bcolor[2] * (1.0f / 255.0f); + R_Shadow_DiffuseLighting(ent->model->numverts, ent->model->numtris, ent->model->mdlmd2data_indices, aliasvert_svectors, aliasvert_tvectors, aliasvert_normals, ent->model->mdlmd2data_texcoords, relativelightorigin, lightradius, lightcolor2, skinframe->pants, skinframe->nmap, NULL); } + + // we only render non-fullbright ranges here + if (skinframe->shirt && (ent->colormap & 0xF0) < 0xE0) + { + c = (ent->colormap & 0xF0);c += (c >= 128 && c < 224) ? 4 : 12; + bcolor = (qbyte *) (&palette_complete[c]); + lightcolor2[0] = lightcolor[0] * bcolor[0] * (1.0f / 255.0f); + lightcolor2[1] = lightcolor[1] * bcolor[1] * (1.0f / 255.0f); + lightcolor2[2] = lightcolor[2] * bcolor[2] * (1.0f / 255.0f); + R_Shadow_DiffuseLighting(ent->model->numverts, ent->model->numtris, ent->model->mdlmd2data_indices, aliasvert_svectors, aliasvert_tvectors, aliasvert_normals, ent->model->mdlmd2data_texcoords, relativelightorigin, lightradius, lightcolor2, skinframe->shirt, skinframe->nmap, NULL); + } + + if (skinframe->base) + R_Shadow_DiffuseLighting(ent->model->numverts, ent->model->numtris, ent->model->mdlmd2data_indices, aliasvert_svectors, aliasvert_tvectors, aliasvert_normals, ent->model->mdlmd2data_texcoords, relativelightorigin, lightradius, lightcolor, skinframe->base, skinframe->nmap, NULL); } + else + if (skinframe->merged) + R_Shadow_DiffuseLighting(ent->model->numverts, ent->model->numtris, ent->model->mdlmd2data_indices, aliasvert_svectors, aliasvert_tvectors, aliasvert_normals, ent->model->mdlmd2data_texcoords, relativelightorigin, lightradius, lightcolor, skinframe->merged, skinframe->nmap, NULL); } -int ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, zymbone_t *bone) +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; - - // LordHavoc: combine transform from zym coordinate space to quake coordinate space with model to world transform matrix - rootmatrix.m[0][0] = softwaretransform_matrix[0][1]; - rootmatrix.m[0][1] = -softwaretransform_matrix[0][0]; - rootmatrix.m[0][2] = softwaretransform_matrix[0][2]; - rootmatrix.m[0][3] = softwaretransform_matrix[0][3]; - rootmatrix.m[1][0] = softwaretransform_matrix[1][1]; - rootmatrix.m[1][1] = -softwaretransform_matrix[1][0]; - rootmatrix.m[1][2] = softwaretransform_matrix[1][2]; - rootmatrix.m[1][3] = softwaretransform_matrix[1][3]; - rootmatrix.m[2][0] = softwaretransform_matrix[2][1]; - rootmatrix.m[2][1] = -softwaretransform_matrix[2][0]; - rootmatrix.m[2][2] = softwaretransform_matrix[2][2]; - rootmatrix.m[2][3] = softwaretransform_matrix[2][3]; + 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; @@ -593,10 +722,10 @@ int ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, zy 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--) { @@ -627,13 +756,13 @@ void ZymoticTransformVerts(int vertcount, int *bonecounts, zymvertex_t *vert) } } -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], s; int *u; // clear normals - memset(aliasvertnorm, 0, sizeof(float) * vertcount * 3); + memset(normals, 0, sizeof(float) * vertcount * 3); memset(aliasvertusage, 0, sizeof(int) * vertcount); // parse render list and accumulate surface normals while(shadercount--) @@ -644,36 +773,36 @@ void ZymoticCalcNormals(int vertcount, int shadercount, int *renderlist) a = renderlist[0]*4; b = renderlist[1]*4; c = renderlist[2]*4; - 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]; + 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); VectorNormalizeFast(normal); // add surface normal to vertices a = renderlist[0] * 3; - aliasvertnorm[a+0] += normal[0]; - aliasvertnorm[a+1] += normal[1]; - aliasvertnorm[a+2] += normal[2]; + normals[a+0] += normal[0]; + normals[a+1] += normal[1]; + normals[a+2] += normal[2]; aliasvertusage[renderlist[0]]++; a = renderlist[1] * 3; - aliasvertnorm[a+0] += normal[0]; - aliasvertnorm[a+1] += normal[1]; - aliasvertnorm[a+2] += normal[2]; + normals[a+0] += normal[0]; + normals[a+1] += normal[1]; + normals[a+2] += normal[2]; aliasvertusage[renderlist[1]]++; a = renderlist[2] * 3; - aliasvertnorm[a+0] += normal[0]; - aliasvertnorm[a+1] += normal[1]; - aliasvertnorm[a+2] += normal[2]; + 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--) { @@ -689,110 +818,34 @@ void ZymoticCalcNormals(int vertcount, int shadercount, int *renderlist) } } -void R_DrawZymoticModelMesh(zymtype1header_t *m, float fog) -{ - rmeshbufferinfo_t mbuf; - int i, *renderlist; - rtexture_t **texture; - - texture = (rtexture_t **)(m->lump_shaders.start + (int) m); - - renderlist = (int *)(m->lump_render.start + (int) m); - for (i = 0;i < m->numshaders;i++) - { - memset(&mbuf, 0, sizeof(mbuf)); - mbuf.numverts = m->numverts; - mbuf.numtriangles = *renderlist++; - if (currentrenderentity->effects & EF_ADDITIVE) - { - mbuf.transparent = true; - mbuf.blendfunc1 = GL_SRC_ALPHA; - mbuf.blendfunc2 = GL_ONE; - } - else if (currentrenderentity->alpha != 1.0 || R_TextureHasAlpha(texture[i])) - { - mbuf.transparent = true; - mbuf.blendfunc1 = GL_SRC_ALPHA; - mbuf.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA; - } - else - { - mbuf.transparent = false; - mbuf.blendfunc1 = GL_ONE; - mbuf.blendfunc2 = GL_ZERO; - } - mbuf.tex[0] = R_GetTexture(texture[i]); - if (R_Mesh_Draw_GetBuffer(&mbuf, true)) - { - c_alias_polys += mbuf.numtriangles; - memcpy(mbuf.index, renderlist, mbuf.numtriangles * sizeof(int[3])); - memcpy(mbuf.vertex, aliasvert, mbuf.numverts * sizeof(float[4])); - R_ModulateColors(aliasvertcolor, mbuf.color, mbuf.numverts, mbuf.colorscale, mbuf.colorscale, mbuf.colorscale); - //memcpy(mbuf.color, aliasvertcolor, mbuf.numverts * sizeof(float[4])); - memcpy(mbuf.texcoords[0], (float *)(m->lump_texcoords.start + (int) m), mbuf.numverts * sizeof(float[2])); - R_Mesh_Render(); - } - renderlist += mbuf.numtriangles * 3; - } - - if (fog) - { - renderlist = (int *)(m->lump_render.start + (int) m); - for (i = 0;i < m->numshaders;i++) - { - memset(&mbuf, 0, sizeof(mbuf)); - mbuf.numverts = m->numverts; - mbuf.numtriangles = *renderlist++; - mbuf.transparent = currentrenderentity->effects & EF_ADDITIVE || currentrenderentity->alpha != 1.0 || R_TextureHasAlpha(texture[i]); - mbuf.blendfunc1 = GL_SRC_ALPHA; - mbuf.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA; - // FIXME: need alpha mask for fogging... - //mbuf.tex[0] = R_GetTexture(texture[i]); - if (R_Mesh_Draw_GetBuffer(&mbuf, false)) - { - c_alias_polys += mbuf.numtriangles; - memcpy(mbuf.index, renderlist, mbuf.numtriangles * sizeof(int[3])); - memcpy(mbuf.vertex, aliasvert, mbuf.numverts * sizeof(float[4])); - R_FillColors(mbuf.color, mbuf.numverts, fogcolor[0] * mbuf.colorscale, fogcolor[1] * mbuf.colorscale, fogcolor[2] * mbuf.colorscale, currentrenderentity->alpha * fog); - //memcpy(mbuf.texcoords[0], (float *)(m->lump_texcoords.start + (int) m), mbuf.numverts * sizeof(float[2])); - R_Mesh_Render(); - } - renderlist += mbuf.numtriangles * 3; - } - } -} - -void R_DrawZymoticModel (float fog) -{ - zymtype1header_t *m; - - // FIXME: do better fog - m = currentrenderentity->model->zymdata_header; - ZymoticLerpBones(m->numbones, (zymbonematrix *)(m->lump_poses.start + (int) m), currentrenderentity->frameblend, (zymbone_t *)(m->lump_bones.start + (int) m)); - 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(m->numverts, 1 - fog, 1 - fog, 1 - fog, true); - - R_DrawZymoticModelMesh(m, fog); -} - -void R_DrawAliasModel (void) +void R_DrawZymoticModelMeshCallback (const void *calldata1, int calldata2) { - float fog; + float fog, ifog, colorscale; vec3_t diff; - - if (currentrenderentity->alpha < (1.0f / 64.0f)) - return; // basically completely transparent - - c_models++; - - softwaretransformforentity(currentrenderentity); + 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) { - VectorSubtract(currentrenderentity->origin, r_origin, diff); + VectorSubtract(ent->origin, r_origin, diff); fog = DotProduct(diff,diff); if (fog < 0.01f) fog = 0.01f; @@ -805,10 +858,84 @@ void R_DrawAliasModel (void) // 1. render model as normal, scaled by inverse of fog alpha (darkens it) // 2. render fog as additive } + ifog = 1 - fog; - if (currentrenderentity->model->aliastype == ALIASTYPE_ZYM) - R_DrawZymoticModel(fog); + memset(&mstate, 0, sizeof(mstate)); + if (ent->effects & EF_ADDITIVE) + { + mstate.blendfunc1 = GL_SRC_ALPHA; + mstate.blendfunc2 = GL_ONE; + } + else if (ent->alpha != 1.0 || R_TextureHasAlpha(texture)) + { + mstate.blendfunc1 = GL_SRC_ALPHA; + mstate.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA; + } else - R_DrawQ1Q2AliasModel(fog); + { + 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; + } +} + +void R_Model_Zymotic_Draw(entity_render_t *ent) +{ + int i; + + if (ent->alpha < (1.0f / 64.0f)) + return; // basically completely transparent + + c_models++; + + for (i = 0;i < ent->model->zymnum_shaders;i++) + { + 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); + } +} + +void R_Model_Zymotic_DrawFakeShadow(entity_render_t *ent) +{ + // FIXME +} + +void R_Model_Zymotic_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, float lightradius2, float lightdistbias, float lightsubtract, float *lightcolor) +{ + // FIXME } +void R_Model_Zymotic_DrawOntoLight(entity_render_t *ent) +{ + // FIXME +}