-#include "quakedef.h"
-#include "cl_collision.h"
-#include "r_shadow.h"
-
-typedef struct
-{
- float m[3][4];
-} zymbonematrix;
-
-// LordHavoc: vertex arrays
-
-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;
-
-float *aliasvertcolor2;
-int *aliasvertusage;
-zymbonematrix *zymbonepose;
-
-mempool_t *gl_models_mempool;
-
-void gl_models_start(void)
-{
- // allocate vertex processing arrays
- 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)
-{
- Mem_FreePool(&gl_models_mempool);
-}
-
-void gl_models_newmap(void)
-{
-}
-
-void GL_Models_Init(void)
-{
- R_RegisterModule("GL_Models", gl_models_start, gl_models_shutdown, gl_models_newmap);
-}
-
-void R_Model_Alias_GetVerts(const entity_render_t *ent, float *vertices, float *normals, float *svectors, float *tvectors)
-{
- int i, vertcount;
- float lerp1, lerp2, lerp3, lerp4;
- const aliasvertex_t *verts1, *verts2, *verts3, *verts4;
-
- if (vertices == NULL)
- Host_Error("R_Model_Alias_GetVerts: vertices == NULL.\n");
- if (svectors != NULL && (tvectors == NULL || normals == NULL))
- Host_Error("R_Model_Alias_GetVerts: svectors requires tvectors and normals.\n");
- if (tvectors != NULL && (svectors == NULL || normals == NULL))
- Host_Error("R_Model_Alias_GetVerts: tvectors requires svectors and normals.\n");
-
- vertcount = ent->model->numverts;
- verts1 = ent->model->mdlmd2data_pose + ent->frameblend[0].frame * vertcount;
- lerp1 = ent->frameblend[0].lerp;
- if (ent->frameblend[1].lerp)
- {
- verts2 = ent->model->mdlmd2data_pose + ent->frameblend[1].frame * vertcount;
- lerp2 = ent->frameblend[1].lerp;
- if (ent->frameblend[2].lerp)
- {
- verts3 = ent->model->mdlmd2data_pose + ent->frameblend[2].frame * vertcount;
- lerp3 = ent->frameblend[2].lerp;
- if (ent->frameblend[3].lerp)
- {
- verts4 = ent->model->mdlmd2data_pose + ent->frameblend[3].frame * vertcount;
- lerp4 = ent->frameblend[3].lerp;
- // generate vertices
- if (svectors != NULL)
- {
- for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, svectors += 4, tvectors += 4, verts1++, verts2++, verts3++, verts4++)
- {
- VectorMAMAMAM(lerp1, verts1->origin, lerp2, verts2->origin, lerp3, verts3->origin, lerp4, verts4->origin, vertices);
- VectorMAMAMAM(lerp1, verts1->normal, lerp2, verts2->normal, lerp3, verts3->normal, lerp4, verts4->normal, normals);
- VectorMAMAMAM(lerp1, verts1->svector, lerp2, verts2->svector, lerp3, verts3->svector, lerp4, verts4->svector, svectors);
- CrossProduct(svectors, normals, tvectors);
- }
- }
- else if (normals != NULL)
- {
- for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, verts1++, verts2++, verts3++, verts4++)
- {
- VectorMAMAMAM(lerp1, verts1->origin, lerp2, verts2->origin, lerp3, verts3->origin, lerp4, verts4->origin, vertices);
- VectorMAMAMAM(lerp1, verts1->normal, lerp2, verts2->normal, lerp3, verts3->normal, lerp4, verts4->normal, normals);
- }
- }
- else
- for (i = 0;i < vertcount;i++, vertices += 4, verts1++, verts2++, verts3++, verts4++)
- VectorMAMAMAM(lerp1, verts1->origin, lerp2, verts2->origin, lerp3, verts3->origin, lerp4, verts4->origin, vertices);
- }
- else
- {
- // generate vertices
- if (svectors != NULL)
- {
- for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, svectors += 4, tvectors += 4, verts1++, verts2++, verts3++)
- {
- VectorMAMAM(lerp1, verts1->origin, lerp2, verts2->origin, lerp3, verts3->origin, vertices);
- VectorMAMAM(lerp1, verts1->normal, lerp2, verts2->normal, lerp3, verts3->normal, normals);
- VectorMAMAM(lerp1, verts1->svector, lerp2, verts2->svector, lerp3, verts3->svector, svectors);
- CrossProduct(svectors, normals, tvectors);
- }
- }
- else if (normals != NULL)
- {
- for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, verts1++, verts2++, verts3++)
- {
- VectorMAMAM(lerp1, verts1->origin, lerp2, verts2->origin, lerp3, verts3->origin, vertices);
- VectorMAMAM(lerp1, verts1->normal, lerp2, verts2->normal, lerp3, verts3->normal, normals);
- }
- }
- else
- for (i = 0;i < vertcount;i++, vertices += 4, verts1++, verts2++, verts3++)
- VectorMAMAM(lerp1, verts1->origin, lerp2, verts2->origin, lerp3, verts3->origin, vertices);
- }
- }
- else
- {
- // generate vertices
- if (svectors != NULL)
- {
- for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, svectors += 4, tvectors += 4, verts1++, verts2++)
- {
- VectorMAM(lerp1, verts1->origin, lerp2, verts2->origin, vertices);
- VectorMAM(lerp1, verts1->normal, lerp2, verts2->normal, normals);
- VectorMAM(lerp1, verts1->svector, lerp2, verts2->svector, svectors);
- CrossProduct(svectors, normals, tvectors);
- }
- }
- else if (normals != NULL)
- {
- for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, verts1++, verts2++)
- {
- VectorMAM(lerp1, verts1->origin, lerp2, verts2->origin, vertices);
- VectorMAM(lerp1, verts1->normal, lerp2, verts2->normal, normals);
- }
- }
- else
- for (i = 0;i < vertcount;i++, vertices += 4, verts1++, verts2++)
- VectorMAM(lerp1, verts1->origin, lerp2, verts2->origin, vertices);
- }
- }
- else
- {
- // generate vertices
- if (svectors != NULL)
- {
- for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, svectors += 4, tvectors += 4, verts1++)
- {
- VectorM(lerp1, verts1->origin, vertices);
- VectorM(lerp1, verts1->normal, normals);
- VectorM(lerp1, verts1->svector, svectors);
- CrossProduct(svectors, normals, tvectors);
- }
- }
- else if (normals != NULL)
- {
- for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, verts1++)
- {
- VectorM(lerp1, verts1->origin, vertices);
- VectorM(lerp1, verts1->normal, normals);
- }
- }
- else
- for (i = 0;i < vertcount;i++, vertices += 4, verts1++)
- VectorM(lerp1, verts1->origin, vertices);
- }
-}
-
-skinframe_t *R_FetchSkinFrame(const entity_render_t *ent)
-{
- model_t *model = ent->model;
- int s = ent->skinnum;
- if ((unsigned int)s >= (unsigned int)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_DrawAliasModelCallback (const void *calldata1, int calldata2)
-{
- 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)
- {
- 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;
-
- 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;
- }
-
- R_Model_Alias_GetVerts(ent, varray_vertex, aliasvert_normals, NULL, NULL);
- memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[4]));
- if (!skinframe->base && !skinframe->pants && !skinframe->shirt && !skinframe->glow)
- {
- // untextured
- 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] = R_GetTexture(r_notexture);
- R_Mesh_State(&m);
- c_alias_polys += model->numtris;
- for (i = 0;i < model->numverts * 4;i += 4)
- {
- 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;
- }
-
- colormapped = !skinframe->merged || (ent->colormap >= 0 && skinframe->base && (skinframe->pants || skinframe->shirt));
- if (colormapped)
- {
- // 128-224 are backwards ranges
- 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 = (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);
- }
- else
- {
- pantscolor[0] = pantscolor[1] = pantscolor[2] = shirtcolor[0] = shirtcolor[1] = shirtcolor[2] = 1;
- pantsfullbright = shirtfullbright = false;
- }
-
- tex = colormapped ? R_GetTexture(skinframe->base) : R_GetTexture(skinframe->merged);
- 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 (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)
- {
- 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
- 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)
- {
- 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
- 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)
- {
- tex = R_GetTexture(skinframe->glow);
- if (tex)
- {
- 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);
-
- 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_DrawAliasModelCallback, ent, 0);
- else
- R_DrawAliasModelCallback(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_Model_Alias_GetVerts(ent, varray_vertex, NULL, NULL, NULL);
-
- // 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_Model_Alias_GetVerts(ent, varray_vertex, NULL, NULL, NULL);
- 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_Model_Alias_GetVerts(ent, varray_vertex, aliasvert_normals, aliasvert_svectors, aliasvert_tvectors);
- 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 = (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, const zymbonematrix *bonebase, const frameblend_t *blend, const zymbone_t *bone)
-{
- int i;
- float lerp1, lerp2, lerp3, lerp4;
- 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)
- {
- bone2 = bonebase + blend[1].frame * count;
- lerp2 = blend[1].lerp;
- if (blend[2].lerp)
- {
- bone3 = bonebase + blend[2].frame * count;
- lerp3 = blend[2].lerp;
- if (blend[3].lerp)
- {
- // 4 poses
- bone4 = bonebase + blend[3].frame * count;
- lerp4 = blend[3].lerp;
- 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;
- m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3 + bone4->m[0][1] * lerp4;
- m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3 + bone4->m[0][2] * lerp4;
- m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3 + bone4->m[0][3] * lerp4;
- m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3 + bone4->m[1][0] * lerp4;
- m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3 + bone4->m[1][1] * lerp4;
- m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3 + bone4->m[1][2] * lerp4;
- m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3 + bone4->m[1][3] * lerp4;
- m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3 + bone4->m[2][0] * lerp4;
- m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3 + bone4->m[2][1] * lerp4;
- 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][0], &m.m[0][0], &out->m[0][0]);
- else
- R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
- bone1++;
- bone2++;
- bone3++;
- bone4++;
- bone++;
- }
- }
- else
- {
- // 3 poses
- 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;
- m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3;
- m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3;
- m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3;
- m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3;
- m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3;
- m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3;
- m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3;
- m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3;
- m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3;
- 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][0], &m.m[0][0], &out->m[0][0]);
- else
- R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
- bone1++;
- bone2++;
- bone3++;
- bone++;
- }
- }
- }
- else
- {
- // 2 poses
- 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;
- m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2;
- m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2;
- m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2;
- m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2;
- m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2;
- m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2;
- m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2;
- m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2;
- m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2;
- 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][0], &m.m[0][0], &out->m[0][0]);
- else
- R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
- bone1++;
- bone2++;
- bone++;
- }
- }
- }
- else
- {
- // 1 pose
- if (lerp1 != 1)
- {
- // lerp != 1.0
- for (i = 0, out = zymbonepose;i < count;i++, out++)
- {
- // interpolate matrices
- m.m[0][0] = bone1->m[0][0] * lerp1;
- m.m[0][1] = bone1->m[0][1] * lerp1;
- m.m[0][2] = bone1->m[0][2] * lerp1;
- m.m[0][3] = bone1->m[0][3] * lerp1;
- m.m[1][0] = bone1->m[1][0] * lerp1;
- m.m[1][1] = bone1->m[1][1] * lerp1;
- m.m[1][2] = bone1->m[1][2] * lerp1;
- m.m[1][3] = bone1->m[1][3] * lerp1;
- m.m[2][0] = bone1->m[2][0] * lerp1;
- m.m[2][1] = bone1->m[2][1] * lerp1;
- 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][0], &m.m[0][0], &out->m[0][0]);
- else
- R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
- bone1++;
- bone++;
- }
- }
- else
- {
- // lerp == 1.0
- for (i = 0, out = zymbonepose;i < count;i++, out++)
- {
- if (bone->parent >= 0)
- R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &bone1->m[0][0], &out->m[0][0]);
- else
- R_ConcatTransforms(&rootmatrix.m[0][0], &bone1->m[0][0], &out->m[0][0]);
- bone1++;
- bone++;
- }
- }
- }
- return true;
-}
-
-void ZymoticTransformVerts(int vertcount, float *vertex, int *bonecounts, zymvertex_t *vert)
-{
- int c;
- 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];
- out[0] = vert->origin[0] * matrix->m[0][0] + vert->origin[1] * matrix->m[0][1] + vert->origin[2] * matrix->m[0][2] + matrix->m[0][3];
- out[1] = vert->origin[0] * matrix->m[1][0] + vert->origin[1] * matrix->m[1][1] + vert->origin[2] * matrix->m[1][2] + matrix->m[1][3];
- out[2] = vert->origin[0] * matrix->m[2][0] + vert->origin[1] * matrix->m[2][1] + vert->origin[2] * matrix->m[2][2] + matrix->m[2][3];
- vert++;
- }
- else
- {
- VectorClear(out);
- while(c--)
- {
- matrix = &zymbonepose[vert->bonenum];
- out[0] += vert->origin[0] * matrix->m[0][0] + vert->origin[1] * matrix->m[0][1] + vert->origin[2] * matrix->m[0][2] + matrix->m[0][3];
- out[1] += vert->origin[0] * matrix->m[1][0] + vert->origin[1] * matrix->m[1][1] + vert->origin[2] * matrix->m[1][2] + matrix->m[1][3];
- out[2] += vert->origin[0] * matrix->m[2][0] + vert->origin[1] * matrix->m[2][1] + vert->origin[2] * matrix->m[2][2] + matrix->m[2][3];
- vert++;
- }
- }
- out += 4;
- }
-}
-
-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(normals, 0, sizeof(float) * vertcount * 3);
- memset(aliasvertusage, 0, sizeof(int) * vertcount);
- // parse render list and accumulate surface normals
- while(shadercount--)
- {
- d = *renderlist++;
- while (d--)
- {
- 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);
- VectorNormalizeFast(normal);
- // add surface normal to vertices
- 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 = normals;
- u = aliasvertusage;
- while(vertcount--)
- {
- if (*u > 1)
- {
- s = ixtable[*u];
- out[0] *= s;
- out[1] *= s;
- out[2] *= s;
- }
- u++;
- out += 3;
- }
-}
-
-void R_DrawZymoticModelMeshCallback (const void *calldata1, int calldata2)
-{
- float fog, ifog, colorscale;
- vec3_t diff;
- 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(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;
-
- 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
- {
- 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
-}