3 #include "cl_collision.h"
11 // LordHavoc: vertex arrays
13 float *aliasvertcolorbuf;
14 float *aliasvertcolor; // this may point at aliasvertcolorbuf or at vertex arrays in the mesh backend
15 float *aliasvert_svectors;
16 float *aliasvert_tvectors;
17 float *aliasvert_normals;
19 float *aliasvertcolor2;
21 zymbonematrix *zymbonepose;
23 mempool_t *gl_models_mempool;
25 void gl_models_start(void)
27 // allocate vertex processing arrays
28 gl_models_mempool = Mem_AllocPool("GL_Models");
29 aliasvertcolor = aliasvertcolorbuf = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4]));
30 aliasvert_svectors = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4]));
31 aliasvert_tvectors = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4]));
32 aliasvert_normals = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4]));
33 aliasvertcolor2 = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4])); // used temporarily for tinted coloring
34 zymbonepose = Mem_Alloc(gl_models_mempool, sizeof(zymbonematrix[256]));
35 aliasvertusage = Mem_Alloc(gl_models_mempool, sizeof(int[MD2MAX_VERTS]));
38 void gl_models_shutdown(void)
40 Mem_FreePool(&gl_models_mempool);
43 void gl_models_newmap(void)
47 void GL_Models_Init(void)
49 R_RegisterModule("GL_Models", gl_models_start, gl_models_shutdown, gl_models_newmap);
52 void R_Model_Alias_GetVerts(const entity_render_t *ent, float *vertices, float *normals, float *svectors, float *tvectors)
55 float vlerp1, nlerp1, vlerp2, nlerp2, vlerp3, nlerp3, vlerp4, nlerp4;
56 const aliasvertex_t *verts1, *verts2, *verts3, *verts4;
59 Host_Error("R_Model_Alias_GetVerts: vertices == NULL.\n");
60 if (svectors != NULL && (tvectors == NULL || normals == NULL))
61 Host_Error("R_Model_Alias_GetVerts: svectors requires tvectors and normals.\n");
62 if (tvectors != NULL && (svectors == NULL || normals == NULL))
63 Host_Error("R_Model_Alias_GetVerts: tvectors requires svectors and normals.\n");
65 vertcount = ent->model->numverts;
66 verts1 = ent->model->mdlmd2data_pose + ent->frameblend[0].frame * vertcount;
67 vlerp1 = ent->frameblend[0].lerp * (1.0f / 16.0f);
68 nlerp1 = ent->frameblend[0].lerp * (1.0f / 127.0f);
69 if (ent->frameblend[1].lerp)
71 verts2 = ent->model->mdlmd2data_pose + ent->frameblend[1].frame * vertcount;
72 vlerp2 = ent->frameblend[1].lerp * (1.0f / 16.0f);
73 nlerp2 = ent->frameblend[1].lerp * (1.0f / 127.0f);
74 if (ent->frameblend[2].lerp)
76 verts3 = ent->model->mdlmd2data_pose + ent->frameblend[2].frame * vertcount;
77 vlerp3 = ent->frameblend[2].lerp * (1.0f / 16.0f);
78 nlerp3 = ent->frameblend[2].lerp * (1.0f / 127.0f);
79 if (ent->frameblend[3].lerp)
81 verts4 = ent->model->mdlmd2data_pose + ent->frameblend[3].frame * vertcount;
82 vlerp4 = ent->frameblend[3].lerp * (1.0f / 16.0f);
83 nlerp4 = ent->frameblend[3].lerp * (1.0f / 127.0f);
87 for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, svectors += 4, tvectors += 4, verts1++, verts2++, verts3++, verts4++)
89 VectorMAMAMAM(vlerp1, verts1->origin, vlerp2, verts2->origin, vlerp3, verts3->origin, vlerp4, verts4->origin, vertices);
90 VectorMAMAMAM(nlerp1, verts1->normal, nlerp2, verts2->normal, nlerp3, verts3->normal, nlerp4, verts4->normal, normals);
91 VectorMAMAMAM(nlerp1, verts1->svector, nlerp2, verts2->svector, nlerp3, verts3->svector, nlerp4, verts4->svector, svectors);
92 CrossProduct(svectors, normals, tvectors);
95 else if (normals != NULL)
97 for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, verts1++, verts2++, verts3++, verts4++)
99 VectorMAMAMAM(vlerp1, verts1->origin, vlerp2, verts2->origin, vlerp3, verts3->origin, vlerp4, verts4->origin, vertices);
100 VectorMAMAMAM(nlerp1, verts1->normal, nlerp2, verts2->normal, nlerp3, verts3->normal, nlerp4, verts4->normal, normals);
104 for (i = 0;i < vertcount;i++, vertices += 4, verts1++, verts2++, verts3++, verts4++)
105 VectorMAMAMAM(vlerp1, verts1->origin, vlerp2, verts2->origin, vlerp3, verts3->origin, vlerp4, verts4->origin, vertices);
110 if (svectors != NULL)
112 for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, svectors += 4, tvectors += 4, verts1++, verts2++, verts3++)
114 VectorMAMAM(vlerp1, verts1->origin, vlerp2, verts2->origin, vlerp3, verts3->origin, vertices);
115 VectorMAMAM(nlerp1, verts1->normal, nlerp2, verts2->normal, nlerp3, verts3->normal, normals);
116 VectorMAMAM(nlerp1, verts1->svector, nlerp2, verts2->svector, nlerp3, verts3->svector, svectors);
117 CrossProduct(svectors, normals, tvectors);
120 else if (normals != NULL)
122 for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, verts1++, verts2++, verts3++)
124 VectorMAMAM(vlerp1, verts1->origin, vlerp2, verts2->origin, vlerp3, verts3->origin, vertices);
125 VectorMAMAM(nlerp1, verts1->normal, nlerp2, verts2->normal, nlerp3, verts3->normal, normals);
129 for (i = 0;i < vertcount;i++, vertices += 4, verts1++, verts2++, verts3++)
130 VectorMAMAM(vlerp1, verts1->origin, vlerp2, verts2->origin, vlerp3, verts3->origin, vertices);
136 if (svectors != NULL)
138 for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, svectors += 4, tvectors += 4, verts1++, verts2++)
140 VectorMAM(vlerp1, verts1->origin, vlerp2, verts2->origin, vertices);
141 VectorMAM(nlerp1, verts1->normal, nlerp2, verts2->normal, normals);
142 VectorMAM(nlerp1, verts1->svector, nlerp2, verts2->svector, svectors);
143 CrossProduct(svectors, normals, tvectors);
146 else if (normals != NULL)
148 for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, verts1++, verts2++)
150 VectorMAM(vlerp1, verts1->origin, vlerp2, verts2->origin, vertices);
151 VectorMAM(nlerp1, verts1->normal, nlerp2, verts2->normal, normals);
155 for (i = 0;i < vertcount;i++, vertices += 4, verts1++, verts2++)
156 VectorMAM(vlerp1, verts1->origin, vlerp2, verts2->origin, vertices);
162 if (svectors != NULL)
164 for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, svectors += 4, tvectors += 4, verts1++)
166 VectorM(vlerp1, verts1->origin, vertices);
167 VectorM(nlerp1, verts1->normal, normals);
168 VectorM(nlerp1, verts1->svector, svectors);
169 CrossProduct(svectors, normals, tvectors);
172 else if (normals != NULL)
174 for (i = 0;i < vertcount;i++, vertices += 4, normals += 4, verts1++)
176 VectorM(vlerp1, verts1->origin, vertices);
177 VectorM(nlerp1, verts1->normal, normals);
181 for (i = 0;i < vertcount;i++, vertices += 4, verts1++)
182 VectorM(vlerp1, verts1->origin, vertices);
186 skinframe_t *R_FetchSkinFrame(const entity_render_t *ent)
188 model_t *model = ent->model;
189 int s = ent->skinnum;
190 if ((unsigned int)s >= (unsigned int)model->numskins)
192 if (model->skinscenes[s].framecount > 1)
193 return &model->skinframes[model->skinscenes[s].firstframe + (int) (cl.time * 10) % model->skinscenes[s].framecount];
195 return &model->skinframes[model->skinscenes[s].firstframe];
198 void R_DrawAliasModelCallback (const void *calldata1, int calldata2)
200 int i, c, fullbright, pantsfullbright, shirtfullbright, colormapped, tex;
201 float pantscolor[3], shirtcolor[3];
202 float fog, ifog, colorscale;
207 skinframe_t *skinframe;
208 const entity_render_t *ent = calldata1;
209 int blendfunc1, blendfunc2;
211 R_Mesh_Matrix(&ent->matrix);
214 R_Mesh_ResizeCheck(model->numverts);
216 skinframe = R_FetchSkinFrame(ent);
218 fullbright = (ent->effects & EF_FULLBRIGHT) != 0;
223 VectorSubtract(ent->origin, r_origin, diff);
224 fog = DotProduct(diff,diff);
227 fog = exp(fogdensity/fog);
232 // fog method: darken, additive fog
233 // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
234 // 2. render fog as additive
238 if (ent->effects & EF_ADDITIVE)
240 blendfunc1 = GL_SRC_ALPHA;
243 else if (ent->alpha != 1.0 || skinframe->fog != NULL)
245 blendfunc1 = GL_SRC_ALPHA;
246 blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
251 blendfunc2 = GL_ZERO;
254 R_Model_Alias_GetVerts(ent, varray_vertex, aliasvert_normals, NULL, NULL);
255 memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[4]));
256 if (!skinframe->base && !skinframe->pants && !skinframe->shirt && !skinframe->glow)
259 memset(&m, 0, sizeof(m));
260 m.blendfunc1 = blendfunc1;
261 m.blendfunc2 = blendfunc2;
262 colorscale = r_colorscale;
263 if (gl_combine.integer)
266 m.texrgbscale[0] = 4;
268 m.tex[0] = R_GetTexture(r_notexture);
270 c_alias_polys += model->numtris;
271 for (i = 0;i < model->numverts * 4;i += 4)
273 varray_texcoord[0][i + 0] *= 8.0f;
274 varray_texcoord[0][i + 1] *= 8.0f;
276 R_LightModel(ent, model->numverts, varray_vertex, aliasvert_normals, varray_color, colorscale, colorscale, colorscale, false);
277 R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
281 colormapped = !skinframe->merged || (ent->colormap >= 0 && skinframe->base && (skinframe->pants || skinframe->shirt));
284 // 128-224 are backwards ranges
285 c = (ent->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12;
286 bcolor = (qbyte *) (&palette_complete[c]);
287 pantsfullbright = c >= 224;
288 VectorScale(bcolor, (1.0f / 255.0f), pantscolor);
289 c = (ent->colormap & 0xF0);c += (c >= 128 && c < 224) ? 4 : 12;
290 bcolor = (qbyte *) (&palette_complete[c]);
291 shirtfullbright = c >= 224;
292 VectorScale(bcolor, (1.0f / 255.0f), shirtcolor);
296 pantscolor[0] = pantscolor[1] = pantscolor[2] = shirtcolor[0] = shirtcolor[1] = shirtcolor[2] = 1;
297 pantsfullbright = shirtfullbright = false;
300 tex = colormapped ? R_GetTexture(skinframe->base) : R_GetTexture(skinframe->merged);
303 memset(&m, 0, sizeof(m));
304 m.blendfunc1 = blendfunc1;
305 m.blendfunc2 = blendfunc2;
306 colorscale = r_colorscale;
307 if (gl_combine.integer)
310 m.texrgbscale[0] = 4;
315 GL_Color(colorscale * ifog, colorscale * ifog, colorscale * ifog, ent->alpha);
317 R_LightModel(ent, model->numverts, varray_vertex, aliasvert_normals, varray_color, colorscale * ifog, colorscale * ifog, colorscale * ifog, false);
318 R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
319 c_alias_polys += model->numtris;
320 blendfunc1 = GL_SRC_ALPHA;
326 if (skinframe->pants)
328 tex = R_GetTexture(skinframe->pants);
331 memset(&m, 0, sizeof(m));
332 m.blendfunc1 = blendfunc1;
333 m.blendfunc2 = blendfunc2;
334 colorscale = r_colorscale;
335 if (gl_combine.integer)
338 m.texrgbscale[0] = 4;
343 GL_Color(pantscolor[0] * colorscale * ifog, pantscolor[1] * colorscale * ifog, pantscolor[2] * colorscale * ifog, ent->alpha);
345 R_LightModel(ent, model->numverts, varray_vertex, aliasvert_normals, varray_color, pantscolor[0] * colorscale * ifog, pantscolor[1] * colorscale * ifog, pantscolor[2] * colorscale * ifog, false);
346 R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
347 c_alias_polys += model->numtris;
348 blendfunc1 = GL_SRC_ALPHA;
352 if (skinframe->shirt)
354 tex = R_GetTexture(skinframe->shirt);
357 memset(&m, 0, sizeof(m));
358 m.blendfunc1 = blendfunc1;
359 m.blendfunc2 = blendfunc2;
360 colorscale = r_colorscale;
361 if (gl_combine.integer)
364 m.texrgbscale[0] = 4;
369 GL_Color(shirtcolor[0] * colorscale * ifog, shirtcolor[1] * colorscale * ifog, shirtcolor[2] * colorscale * ifog, ent->alpha);
371 R_LightModel(ent, model->numverts, varray_vertex, aliasvert_normals, varray_color, shirtcolor[0] * colorscale * ifog, shirtcolor[1] * colorscale * ifog, shirtcolor[2] * colorscale * ifog, false);
372 R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
373 c_alias_polys += model->numtris;
374 blendfunc1 = GL_SRC_ALPHA;
381 tex = R_GetTexture(skinframe->glow);
384 memset(&m, 0, sizeof(m));
385 m.blendfunc1 = blendfunc1;
386 m.blendfunc2 = blendfunc2;
390 blendfunc1 = GL_SRC_ALPHA;
392 c_alias_polys += model->numtris;
393 GL_Color(ifog * r_colorscale, ifog * r_colorscale, ifog * r_colorscale, ent->alpha);
394 R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
399 memset(&m, 0, sizeof(m));
400 m.blendfunc1 = GL_SRC_ALPHA;
401 m.blendfunc2 = GL_ONE;
402 m.tex[0] = R_GetTexture(skinframe->fog);
405 c_alias_polys += model->numtris;
406 GL_Color(fogcolor[0] * fog * r_colorscale, fogcolor[1] * fog * r_colorscale, fogcolor[2] * fog * r_colorscale, ent->alpha);
407 R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
411 void R_Model_Alias_Draw(entity_render_t *ent)
413 if (ent->alpha < (1.0f / 64.0f))
414 return; // basically completely transparent
418 if (ent->effects & EF_ADDITIVE || ent->alpha != 1.0 || R_FetchSkinFrame(ent)->fog != NULL)
419 R_MeshQueue_AddTransparent(ent->origin, R_DrawAliasModelCallback, ent, 0);
421 R_DrawAliasModelCallback(ent, 0);
424 void R_Model_Alias_DrawFakeShadow (entity_render_t *ent)
429 float *v, planenormal[3], planedist, dist, projection[3], floororigin[3], surfnormal[3], lightdirection[3], v2[3];
431 lightdirection[0] = 0.5;
432 lightdirection[1] = 0.2;
433 lightdirection[2] = -1;
434 VectorNormalizeFast(lightdirection);
436 VectorMA(ent->origin, 65536.0f, lightdirection, v2);
437 if (CL_TraceLine(ent->origin, v2, floororigin, surfnormal, 0, false, NULL) == 1)
440 R_Mesh_Matrix(&ent->matrix);
443 R_Mesh_ResizeCheck(model->numverts);
445 memset(&m, 0, sizeof(m));
446 m.blendfunc1 = GL_SRC_ALPHA;
447 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
450 c_alias_polys += model->numtris;
451 R_Model_Alias_GetVerts(ent, varray_vertex, NULL, NULL, NULL);
453 // put a light direction in the entity's coordinate space
454 Matrix4x4_Transform3x3(&ent->inversematrix, lightdirection, projection);
455 VectorNormalizeFast(projection);
457 // put the plane's normal in the entity's coordinate space
458 Matrix4x4_Transform3x3(&ent->inversematrix, surfnormal, planenormal);
459 VectorNormalizeFast(planenormal);
461 // put the plane's distance in the entity's coordinate space
462 VectorSubtract(floororigin, ent->origin, floororigin);
463 planedist = DotProduct(floororigin, surfnormal) + 2;
465 dist = -1.0f / DotProduct(projection, planenormal);
466 VectorScale(projection, dist, projection);
467 for (i = 0, v = varray_vertex;i < model->numverts;i++, v += 4)
469 dist = DotProduct(v, planenormal) - planedist;
472 VectorMA(v, dist, projection, v);
474 GL_Color(0, 0, 0, 0.5);
475 R_Mesh_Draw(model->numverts, model->numtris, model->mdlmd2data_indices);
478 void R_Model_Alias_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
480 float projectdistance;
481 projectdistance = lightradius + ent->model->radius - sqrt(DotProduct(relativelightorigin, relativelightorigin));
482 if (projectdistance > 0.1)
484 R_Mesh_Matrix(&ent->matrix);
485 R_Mesh_ResizeCheck(ent->model->numverts * 2);
486 R_Model_Alias_GetVerts(ent, varray_vertex, NULL, NULL, NULL);
487 R_Shadow_Volume(ent->model->numverts, ent->model->numtris, ent->model->mdlmd2data_indices, ent->model->mdlmd2data_triangleneighbors, relativelightorigin, lightradius, projectdistance);
491 void R_Model_Alias_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor)
494 float lightcolor2[3];
496 skinframe_t *skinframe;
497 R_Mesh_Matrix(&ent->matrix);
498 R_Mesh_ResizeCheck(ent->model->numverts);
499 R_Model_Alias_GetVerts(ent, varray_vertex, aliasvert_normals, aliasvert_svectors, aliasvert_tvectors);
500 skinframe = R_FetchSkinFrame(ent);
502 // note: to properly handle fog this should scale the lightcolor into lightcolor2 according to 1-fog scaling
504 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);
506 if (!skinframe->base && !skinframe->pants && !skinframe->shirt && !skinframe->glow)
508 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);
512 if (!skinframe->merged || (ent->colormap >= 0 && skinframe->base && (skinframe->pants || skinframe->shirt)))
514 // 128-224 are backwards ranges
515 // we only render non-fullbright ranges here
516 if (skinframe->pants && (ent->colormap & 0xF) < 0xE)
518 c = (ent->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12;
519 bcolor = (qbyte *) (&palette_complete[c]);
520 lightcolor2[0] = lightcolor[0] * bcolor[0] * (1.0f / 255.0f);
521 lightcolor2[1] = lightcolor[1] * bcolor[1] * (1.0f / 255.0f);
522 lightcolor2[2] = lightcolor[2] * bcolor[2] * (1.0f / 255.0f);
523 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);
526 // we only render non-fullbright ranges here
527 if (skinframe->shirt && (ent->colormap & 0xF0) < 0xE0)
529 c = (ent->colormap & 0xF0);c += (c >= 128 && c < 224) ? 4 : 12;
530 bcolor = (qbyte *) (&palette_complete[c]);
531 lightcolor2[0] = lightcolor[0] * bcolor[0] * (1.0f / 255.0f);
532 lightcolor2[1] = lightcolor[1] * bcolor[1] * (1.0f / 255.0f);
533 lightcolor2[2] = lightcolor[2] * bcolor[2] * (1.0f / 255.0f);
534 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);
538 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);
541 if (skinframe->merged)
542 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);
545 int ZymoticLerpBones(int count, const zymbonematrix *bonebase, const frameblend_t *blend, const zymbone_t *bone)
548 float lerp1, lerp2, lerp3, lerp4;
549 zymbonematrix *out, rootmatrix, m;
550 const zymbonematrix *bone1, *bone2, *bone3, *bone4;
552 rootmatrix.m[0][0] = 1;
553 rootmatrix.m[0][1] = 0;
554 rootmatrix.m[0][2] = 0;
555 rootmatrix.m[0][3] = 0;
556 rootmatrix.m[1][0] = 0;
557 rootmatrix.m[1][1] = 1;
558 rootmatrix.m[1][2] = 0;
559 rootmatrix.m[1][3] = 0;
560 rootmatrix.m[2][0] = 0;
561 rootmatrix.m[2][1] = 0;
562 rootmatrix.m[2][2] = 1;
563 rootmatrix.m[2][3] = 0;
565 bone1 = bonebase + blend[0].frame * count;
566 lerp1 = blend[0].lerp;
569 bone2 = bonebase + blend[1].frame * count;
570 lerp2 = blend[1].lerp;
573 bone3 = bonebase + blend[2].frame * count;
574 lerp3 = blend[2].lerp;
578 bone4 = bonebase + blend[3].frame * count;
579 lerp4 = blend[3].lerp;
580 for (i = 0, out = zymbonepose;i < count;i++, out++)
582 // interpolate matrices
583 m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3 + bone4->m[0][0] * lerp4;
584 m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3 + bone4->m[0][1] * lerp4;
585 m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3 + bone4->m[0][2] * lerp4;
586 m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3 + bone4->m[0][3] * lerp4;
587 m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3 + bone4->m[1][0] * lerp4;
588 m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3 + bone4->m[1][1] * lerp4;
589 m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3 + bone4->m[1][2] * lerp4;
590 m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3 + bone4->m[1][3] * lerp4;
591 m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3 + bone4->m[2][0] * lerp4;
592 m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3 + bone4->m[2][1] * lerp4;
593 m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3 + bone4->m[2][2] * lerp4;
594 m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3 + bone4->m[2][3] * lerp4;
595 if (bone->parent >= 0)
596 R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
598 R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
609 for (i = 0, out = zymbonepose;i < count;i++, out++)
611 // interpolate matrices
612 m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3;
613 m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3;
614 m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3;
615 m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3;
616 m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3;
617 m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3;
618 m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3;
619 m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3;
620 m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3;
621 m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3;
622 m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3;
623 m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3;
624 if (bone->parent >= 0)
625 R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
627 R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
638 for (i = 0, out = zymbonepose;i < count;i++, out++)
640 // interpolate matrices
641 m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2;
642 m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2;
643 m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2;
644 m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2;
645 m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2;
646 m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2;
647 m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2;
648 m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2;
649 m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2;
650 m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2;
651 m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2;
652 m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2;
653 if (bone->parent >= 0)
654 R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
656 R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
669 for (i = 0, out = zymbonepose;i < count;i++, out++)
671 // interpolate matrices
672 m.m[0][0] = bone1->m[0][0] * lerp1;
673 m.m[0][1] = bone1->m[0][1] * lerp1;
674 m.m[0][2] = bone1->m[0][2] * lerp1;
675 m.m[0][3] = bone1->m[0][3] * lerp1;
676 m.m[1][0] = bone1->m[1][0] * lerp1;
677 m.m[1][1] = bone1->m[1][1] * lerp1;
678 m.m[1][2] = bone1->m[1][2] * lerp1;
679 m.m[1][3] = bone1->m[1][3] * lerp1;
680 m.m[2][0] = bone1->m[2][0] * lerp1;
681 m.m[2][1] = bone1->m[2][1] * lerp1;
682 m.m[2][2] = bone1->m[2][2] * lerp1;
683 m.m[2][3] = bone1->m[2][3] * lerp1;
684 if (bone->parent >= 0)
685 R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
687 R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
695 for (i = 0, out = zymbonepose;i < count;i++, out++)
697 if (bone->parent >= 0)
698 R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &bone1->m[0][0], &out->m[0][0]);
700 R_ConcatTransforms(&rootmatrix.m[0][0], &bone1->m[0][0], &out->m[0][0]);
709 void ZymoticTransformVerts(int vertcount, float *vertex, int *bonecounts, zymvertex_t *vert)
713 zymbonematrix *matrix;
717 // FIXME: validate bonecounts at load time (must be >= 1)
718 // FIXME: need 4th component in origin, for how much of the translate to blend in
721 matrix = &zymbonepose[vert->bonenum];
722 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];
723 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];
724 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];
732 matrix = &zymbonepose[vert->bonenum];
733 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];
734 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];
735 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];
743 void ZymoticCalcNormals(int vertcount, float *vertex, float *normals, int shadercount, int *renderlist)
746 float *out, v1[3], v2[3], normal[3], s;
749 memset(normals, 0, sizeof(float) * vertcount * 3);
750 memset(aliasvertusage, 0, sizeof(int) * vertcount);
751 // parse render list and accumulate surface normals
760 v1[0] = vertex[a+0] - vertex[b+0];
761 v1[1] = vertex[a+1] - vertex[b+1];
762 v1[2] = vertex[a+2] - vertex[b+2];
763 v2[0] = vertex[c+0] - vertex[b+0];
764 v2[1] = vertex[c+1] - vertex[b+1];
765 v2[2] = vertex[c+2] - vertex[b+2];
766 CrossProduct(v1, v2, normal);
767 VectorNormalizeFast(normal);
768 // add surface normal to vertices
769 a = renderlist[0] * 3;
770 normals[a+0] += normal[0];
771 normals[a+1] += normal[1];
772 normals[a+2] += normal[2];
773 aliasvertusage[renderlist[0]]++;
774 a = renderlist[1] * 3;
775 normals[a+0] += normal[0];
776 normals[a+1] += normal[1];
777 normals[a+2] += normal[2];
778 aliasvertusage[renderlist[1]]++;
779 a = renderlist[2] * 3;
780 normals[a+0] += normal[0];
781 normals[a+1] += normal[1];
782 normals[a+2] += normal[2];
783 aliasvertusage[renderlist[2]]++;
787 // FIXME: precalc this
788 // average surface normals
805 void R_DrawZymoticModelMeshCallback (const void *calldata1, int calldata2)
807 float fog, ifog, colorscale;
809 int i, *renderlist, *elements;
812 const entity_render_t *ent = calldata1;
813 int shadernum = calldata2;
814 int numverts, numtriangles;
816 R_Mesh_Matrix(&ent->matrix);
818 // find the vertex index list and texture
819 renderlist = ent->model->zymdata_renderlist;
820 for (i = 0;i < shadernum;i++)
821 renderlist += renderlist[0] * 3 + 1;
822 texture = ent->model->zymdata_textures[shadernum];
824 numverts = ent->model->zymnum_verts;
825 numtriangles = *renderlist++;
826 elements = renderlist;
827 R_Mesh_ResizeCheck(numverts);
832 VectorSubtract(ent->origin, r_origin, diff);
833 fog = DotProduct(diff,diff);
836 fog = exp(fogdensity/fog);
841 // fog method: darken, additive fog
842 // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
843 // 2. render fog as additive
847 memset(&mstate, 0, sizeof(mstate));
848 if (ent->effects & EF_ADDITIVE)
850 mstate.blendfunc1 = GL_SRC_ALPHA;
851 mstate.blendfunc2 = GL_ONE;
853 else if (ent->alpha != 1.0 || R_TextureHasAlpha(texture))
855 mstate.blendfunc1 = GL_SRC_ALPHA;
856 mstate.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
860 mstate.blendfunc1 = GL_ONE;
861 mstate.blendfunc2 = GL_ZERO;
863 colorscale = r_colorscale;
864 if (gl_combine.integer)
866 mstate.texrgbscale[0] = 4;
869 mstate.tex[0] = R_GetTexture(texture);
870 R_Mesh_State(&mstate);
871 ZymoticLerpBones(ent->model->zymnum_bones, (zymbonematrix *) ent->model->zymdata_poses, ent->frameblend, ent->model->zymdata_bones);
872 ZymoticTransformVerts(numverts, varray_vertex, ent->model->zymdata_vertbonecounts, ent->model->zymdata_verts);
873 ZymoticCalcNormals(numverts, varray_vertex, aliasvert_normals, ent->model->zymnum_shaders, ent->model->zymdata_renderlist);
874 memcpy(varray_texcoord[0], ent->model->zymdata_texcoords, ent->model->zymnum_verts * sizeof(float[4]));
876 R_LightModel(ent, numverts, varray_vertex, aliasvert_normals, varray_color, ifog * colorscale, ifog * colorscale, ifog * colorscale, false);
877 R_Mesh_Draw(numverts, numtriangles, elements);
878 c_alias_polys += numtriangles;
882 memset(&mstate, 0, sizeof(mstate));
883 mstate.blendfunc1 = GL_SRC_ALPHA;
884 mstate.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
885 // FIXME: need alpha mask for fogging...
886 //mstate.tex[0] = R_GetTexture(texture);
887 R_Mesh_State(&mstate);
888 GL_Color(fogcolor[0] * r_colorscale, fogcolor[1] * r_colorscale, fogcolor[2] * r_colorscale, ent->alpha * fog);
889 R_Mesh_Draw(numverts, numtriangles, elements);
890 c_alias_polys += numtriangles;
894 void R_Model_Zymotic_Draw(entity_render_t *ent)
898 if (ent->alpha < (1.0f / 64.0f))
899 return; // basically completely transparent
903 for (i = 0;i < ent->model->zymnum_shaders;i++)
905 if (ent->effects & EF_ADDITIVE || ent->alpha != 1.0 || R_TextureHasAlpha(ent->model->zymdata_textures[i]))
906 R_MeshQueue_AddTransparent(ent->origin, R_DrawZymoticModelMeshCallback, ent, i);
908 R_DrawZymoticModelMeshCallback(ent, i);
912 void R_Model_Zymotic_DrawFakeShadow(entity_render_t *ent)
917 void R_Model_Zymotic_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, float lightradius2, float lightdistbias, float lightsubtract, float *lightcolor)
922 void R_Model_Zymotic_DrawOntoLight(entity_render_t *ent)