4 cvar_t r_quickmodels = {0, "r_quickmodels", "1"};
11 // LordHavoc: vertex arrays
14 float *aliasvertcolorbuf;
15 float *aliasvert; // this may point at aliasvertbuf or at vertex arrays in the mesh backend
16 float *aliasvertcolor; // this may point at aliasvertcolorbuf or at vertex arrays in the mesh backend
18 float *aliasvertcolor2;
21 zymbonematrix *zymbonepose;
23 rmeshinfo_t aliasmeshinfo;
25 mempool_t *gl_models_mempool;
27 void gl_models_start(void)
29 // allocate vertex processing arrays
30 gl_models_mempool = Mem_AllocPool("GL_Models");
31 aliasvert = aliasvertbuf = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4]));
32 aliasvertcolor = aliasvertcolorbuf = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4]));
33 aliasvertnorm = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][3]));
34 aliasvertcolor2 = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4])); // used temporarily for tinted coloring
35 zymbonepose = Mem_Alloc(gl_models_mempool, sizeof(zymbonematrix[256]));
36 aliasvertusage = Mem_Alloc(gl_models_mempool, sizeof(int[MD2MAX_VERTS]));
39 void gl_models_shutdown(void)
41 Mem_FreePool(&gl_models_mempool);
44 void gl_models_newmap(void)
48 void GL_Models_Init(void)
50 Cvar_RegisterVariable(&r_quickmodels);
52 R_RegisterModule("GL_Models", gl_models_start, gl_models_shutdown, gl_models_newmap);
55 void R_AliasTransformVerts(int vertcount)
60 while (vertcount >= 4)
62 VectorCopy(av, point);softwaretransform(point, av);av += 4;
63 VectorCopy(av, point);softwaretransform(point, av);av += 4;
64 VectorCopy(av, point);softwaretransform(point, av);av += 4;
65 VectorCopy(av, point);softwaretransform(point, av);av += 4;
70 VectorCopy(av, point);softwaretransform(point, av);av += 4;
75 void R_AliasLerpVerts(int vertcount,
76 float lerp1, trivertx_t *verts1, vec3_t fscale1, vec3_t translate1,
77 float lerp2, trivertx_t *verts2, vec3_t fscale2, vec3_t translate2,
78 float lerp3, trivertx_t *verts3, vec3_t fscale3, vec3_t translate3,
79 float lerp4, trivertx_t *verts4, vec3_t fscale4, vec3_t translate4)
82 vec3_t scale1, scale2, scale3, scale4, translate;
83 float *n1, *n2, *n3, *n4;
87 VectorScale(fscale1, lerp1, scale1);
90 VectorScale(fscale2, lerp2, scale2);
93 VectorScale(fscale3, lerp3, scale3);
96 VectorScale(fscale4, lerp4, scale4);
97 translate[0] = translate1[0] * lerp1 + translate2[0] * lerp2 + translate3[0] * lerp3 + translate4[0] * lerp4;
98 translate[1] = translate1[1] * lerp1 + translate2[1] * lerp2 + translate3[1] * lerp3 + translate4[1] * lerp4;
99 translate[2] = translate1[2] * lerp1 + translate2[2] * lerp2 + translate3[2] * lerp3 + translate4[2] * lerp4;
101 for (i = 0;i < vertcount;i++)
103 av[0] = verts1->v[0] * scale1[0] + verts2->v[0] * scale2[0] + verts3->v[0] * scale3[0] + verts4->v[0] * scale4[0] + translate[0];
104 av[1] = verts1->v[1] * scale1[1] + verts2->v[1] * scale2[1] + verts3->v[1] * scale3[1] + verts4->v[1] * scale4[1] + translate[1];
105 av[2] = verts1->v[2] * scale1[2] + verts2->v[2] * scale2[2] + verts3->v[2] * scale3[2] + verts4->v[2] * scale4[2] + translate[2];
106 n1 = m_bytenormals[verts1->lightnormalindex];
107 n2 = m_bytenormals[verts2->lightnormalindex];
108 n3 = m_bytenormals[verts3->lightnormalindex];
109 n4 = m_bytenormals[verts4->lightnormalindex];
110 avn[0] = n1[0] * lerp1 + n2[0] * lerp2 + n3[0] * lerp3 + n4[0] * lerp4;
111 avn[1] = n1[1] * lerp1 + n2[1] * lerp2 + n3[1] * lerp3 + n4[1] * lerp4;
112 avn[2] = n1[2] * lerp1 + n2[2] * lerp2 + n3[2] * lerp3 + n4[2] * lerp4;
115 verts1++;verts2++;verts3++;verts4++;
120 translate[0] = translate1[0] * lerp1 + translate2[0] * lerp2 + translate3[0] * lerp3;
121 translate[1] = translate1[1] * lerp1 + translate2[1] * lerp2 + translate3[1] * lerp3;
122 translate[2] = translate1[2] * lerp1 + translate2[2] * lerp2 + translate3[2] * lerp3;
124 for (i = 0;i < vertcount;i++)
126 av[0] = verts1->v[0] * scale1[0] + verts2->v[0] * scale2[0] + verts3->v[0] * scale3[0] + translate[0];
127 av[1] = verts1->v[1] * scale1[1] + verts2->v[1] * scale2[1] + verts3->v[1] * scale3[1] + translate[1];
128 av[2] = verts1->v[2] * scale1[2] + verts2->v[2] * scale2[2] + verts3->v[2] * scale3[2] + translate[2];
129 n1 = m_bytenormals[verts1->lightnormalindex];
130 n2 = m_bytenormals[verts2->lightnormalindex];
131 n3 = m_bytenormals[verts3->lightnormalindex];
132 avn[0] = n1[0] * lerp1 + n2[0] * lerp2 + n3[0] * lerp3;
133 avn[1] = n1[1] * lerp1 + n2[1] * lerp2 + n3[1] * lerp3;
134 avn[2] = n1[2] * lerp1 + n2[2] * lerp2 + n3[2] * lerp3;
137 verts1++;verts2++;verts3++;
143 translate[0] = translate1[0] * lerp1 + translate2[0] * lerp2;
144 translate[1] = translate1[1] * lerp1 + translate2[1] * lerp2;
145 translate[2] = translate1[2] * lerp1 + translate2[2] * lerp2;
147 for (i = 0;i < vertcount;i++)
149 av[0] = verts1->v[0] * scale1[0] + verts2->v[0] * scale2[0] + translate[0];
150 av[1] = verts1->v[1] * scale1[1] + verts2->v[1] * scale2[1] + translate[1];
151 av[2] = verts1->v[2] * scale1[2] + verts2->v[2] * scale2[2] + translate[2];
152 n1 = m_bytenormals[verts1->lightnormalindex];
153 n2 = m_bytenormals[verts2->lightnormalindex];
154 avn[0] = n1[0] * lerp1 + n2[0] * lerp2;
155 avn[1] = n1[1] * lerp1 + n2[1] * lerp2;
156 avn[2] = n1[2] * lerp1 + n2[2] * lerp2;
165 translate[0] = translate1[0] * lerp1;
166 translate[1] = translate1[1] * lerp1;
167 translate[2] = translate1[2] * lerp1;
171 // general but almost never used case
172 for (i = 0;i < vertcount;i++)
174 av[0] = verts1->v[0] * scale1[0] + translate[0];
175 av[1] = verts1->v[1] * scale1[1] + translate[1];
176 av[2] = verts1->v[2] * scale1[2] + translate[2];
177 n1 = m_bytenormals[verts1->lightnormalindex];
178 avn[0] = n1[0] * lerp1;
179 avn[1] = n1[1] * lerp1;
180 avn[2] = n1[2] * lerp1;
189 for (i = 0;i < vertcount;i++)
191 av[0] = verts1->v[0] * scale1[0] + translate[0];
192 av[1] = verts1->v[1] * scale1[1] + translate[1];
193 av[2] = verts1->v[2] * scale1[2] + translate[2];
194 VectorCopy(m_bytenormals[verts1->lightnormalindex], avn);
203 void R_DrawModelMesh(rtexture_t *skin, float *colors, float cred, float cgreen, float cblue)
205 aliasmeshinfo.tex[0] = R_GetTexture(skin);
206 aliasmeshinfo.color = colors;
209 aliasmeshinfo.cr = cred;
210 aliasmeshinfo.cg = cgreen;
211 aliasmeshinfo.cb = cblue;
212 aliasmeshinfo.ca = currentrenderentity->alpha;
215 c_alias_polys += aliasmeshinfo.numtriangles;
216 R_Mesh_Draw(&aliasmeshinfo);
218 // leave it in a state for additional passes
219 aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
220 aliasmeshinfo.blendfunc2 = GL_ONE;
223 void R_TintModel(float *in, float *out, int verts, float r, float g, float b)
226 for (i = 0;i < verts;i++)
237 skinframe_t *R_FetchSkinFrame(void)
239 model_t *model = currentrenderentity->model;
240 if (model->skinscenes[currentrenderentity->skinnum].framecount > 1)
241 return &model->skinframes[model->skinscenes[currentrenderentity->skinnum].firstframe + (int) (cl.time * 10) % model->skinscenes[currentrenderentity->skinnum].framecount];
243 return &model->skinframes[model->skinscenes[currentrenderentity->skinnum].firstframe];
246 void R_SetupMDLMD2Frames(float colorr, float colorg, float colorb)
248 md2frame_t *frame1, *frame2, *frame3, *frame4;
249 trivertx_t *frame1verts, *frame2verts, *frame3verts, *frame4verts;
251 model = currentrenderentity->model;
253 frame1 = &model->mdlmd2data_frames[currentrenderentity->frameblend[0].frame];
254 frame2 = &model->mdlmd2data_frames[currentrenderentity->frameblend[1].frame];
255 frame3 = &model->mdlmd2data_frames[currentrenderentity->frameblend[2].frame];
256 frame4 = &model->mdlmd2data_frames[currentrenderentity->frameblend[3].frame];
257 frame1verts = &model->mdlmd2data_pose[currentrenderentity->frameblend[0].frame * model->numverts];
258 frame2verts = &model->mdlmd2data_pose[currentrenderentity->frameblend[1].frame * model->numverts];
259 frame3verts = &model->mdlmd2data_pose[currentrenderentity->frameblend[2].frame * model->numverts];
260 frame4verts = &model->mdlmd2data_pose[currentrenderentity->frameblend[3].frame * model->numverts];
261 R_AliasLerpVerts(model->numverts,
262 currentrenderentity->frameblend[0].lerp, frame1verts, frame1->scale, frame1->translate,
263 currentrenderentity->frameblend[1].lerp, frame2verts, frame2->scale, frame2->translate,
264 currentrenderentity->frameblend[2].lerp, frame3verts, frame3->scale, frame3->translate,
265 currentrenderentity->frameblend[3].lerp, frame4verts, frame4->scale, frame4->translate);
267 R_LightModel(model->numverts, colorr, colorg, colorb, false);
269 R_AliasTransformVerts(model->numverts);
272 void R_DrawQ1Q2AliasModel (float fog)
275 skinframe_t *skinframe;
277 model = currentrenderentity->model;
279 skinframe = R_FetchSkinFrame();
280 if (fog && !(currentrenderentity->effects & EF_ADDITIVE))
282 R_SetupMDLMD2Frames(1 - fog, 1 - fog, 1 - fog);
284 memset(&aliasmeshinfo, 0, sizeof(aliasmeshinfo));
286 aliasmeshinfo.vertex = aliasvert;
287 aliasmeshinfo.vertexstep = sizeof(float[4]);
288 aliasmeshinfo.numverts = model->numverts;
289 aliasmeshinfo.numtriangles = model->numtris;
290 aliasmeshinfo.index = model->mdlmd2data_indices;
291 aliasmeshinfo.colorstep = sizeof(float[4]);
292 aliasmeshinfo.texcoords[0] = model->mdlmd2data_texcoords;
293 aliasmeshinfo.texcoordstep[0] = sizeof(float[2]);
295 if (currentrenderentity->effects & EF_ADDITIVE)
297 aliasmeshinfo.transparent = true;
298 aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
299 aliasmeshinfo.blendfunc2 = GL_ONE;
301 else if (currentrenderentity->alpha != 1.0 || skinframe->fog != NULL)
303 aliasmeshinfo.transparent = true;
304 aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
305 aliasmeshinfo.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
309 aliasmeshinfo.transparent = false;
310 aliasmeshinfo.blendfunc1 = GL_ONE;
311 aliasmeshinfo.blendfunc2 = GL_ZERO;
314 if (skinframe->base || skinframe->pants || skinframe->shirt || skinframe->glow || skinframe->merged)
316 if (currentrenderentity->colormap >= 0 && (skinframe->base || skinframe->pants || skinframe->shirt))
321 R_DrawModelMesh(skinframe->base, aliasvertcolor, 0, 0, 0);
322 if (skinframe->pants)
324 c = (currentrenderentity->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
325 color = (qbyte *) (&d_8to24table[c]);
326 if (c >= 224) // fullbright ranges
327 R_DrawModelMesh(skinframe->pants, NULL, color[0] * (1.0f / 255.0f), color[1] * (1.0f / 255.0f), color[2] * (1.0f / 255.0f));
330 R_TintModel(aliasvertcolor, aliasvertcolor2, model->numverts, color[0] * (1.0f / 255.0f), color[1] * (1.0f / 255.0f), color[2] * (1.0f / 255.0f));
331 R_DrawModelMesh(skinframe->pants, aliasvertcolor2, 0, 0, 0);
334 if (skinframe->shirt)
336 c = currentrenderentity->colormap & 0xF0 ;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
337 color = (qbyte *) (&d_8to24table[c]);
338 if (c >= 224) // fullbright ranges
339 R_DrawModelMesh(skinframe->shirt, NULL, color[0] * (1.0f / 255.0f), color[1] * (1.0f / 255.0f), color[2] * (1.0f / 255.0f));
342 R_TintModel(aliasvertcolor, aliasvertcolor2, model->numverts, color[0] * (1.0f / 255.0f), color[1] * (1.0f / 255.0f), color[2] * (1.0f / 255.0f));
343 R_DrawModelMesh(skinframe->shirt, aliasvertcolor2, 0, 0, 0);
349 if (skinframe->merged)
350 R_DrawModelMesh(skinframe->merged, aliasvertcolor, 0, 0, 0);
353 if (skinframe->base) R_DrawModelMesh(skinframe->base, aliasvertcolor, 0, 0, 0);
354 if (skinframe->pants) R_DrawModelMesh(skinframe->pants, aliasvertcolor, 0, 0, 0);
355 if (skinframe->shirt) R_DrawModelMesh(skinframe->shirt, aliasvertcolor, 0, 0, 0);
358 if (skinframe->glow) R_DrawModelMesh(skinframe->glow, NULL, 1 - fog, 1 - fog, 1 - fog);
361 R_DrawModelMesh(0, NULL, 1 - fog, 1 - fog, 1 - fog);
363 aliasmeshinfo.tex[0] = R_GetTexture(skinframe->fog);
364 aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
365 aliasmeshinfo.blendfunc2 = GL_ONE;
366 aliasmeshinfo.color = NULL;
368 aliasmeshinfo.cr = fogcolor[0];
369 aliasmeshinfo.cg = fogcolor[1];
370 aliasmeshinfo.cb = fogcolor[2];
371 aliasmeshinfo.ca = currentrenderentity->alpha * fog;
373 c_alias_polys += aliasmeshinfo.numtriangles;
374 R_Mesh_Draw(&aliasmeshinfo);
376 else if (currentrenderentity->colormap >= 0 || !skinframe->merged || skinframe->glow || !r_quickmodels.integer)
378 R_SetupMDLMD2Frames(1 - fog, 1 - fog, 1 - fog);
380 memset(&aliasmeshinfo, 0, sizeof(aliasmeshinfo));
382 aliasmeshinfo.vertex = aliasvert;
383 aliasmeshinfo.vertexstep = sizeof(float[4]);
384 aliasmeshinfo.numverts = model->numverts;
385 aliasmeshinfo.numtriangles = model->numtris;
386 aliasmeshinfo.index = model->mdlmd2data_indices;
387 aliasmeshinfo.colorstep = sizeof(float[4]);
388 aliasmeshinfo.texcoords[0] = model->mdlmd2data_texcoords;
389 aliasmeshinfo.texcoordstep[0] = sizeof(float[2]);
391 if (currentrenderentity->effects & EF_ADDITIVE)
393 aliasmeshinfo.transparent = true;
394 aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
395 aliasmeshinfo.blendfunc2 = GL_ONE;
397 else if (currentrenderentity->alpha != 1.0 || skinframe->fog != NULL)
399 aliasmeshinfo.transparent = true;
400 aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
401 aliasmeshinfo.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
405 aliasmeshinfo.transparent = false;
406 aliasmeshinfo.blendfunc1 = GL_ONE;
407 aliasmeshinfo.blendfunc2 = GL_ZERO;
410 if (skinframe->base || skinframe->pants || skinframe->shirt || skinframe->glow || skinframe->merged)
412 if (currentrenderentity->colormap >= 0 && (skinframe->base || skinframe->pants || skinframe->shirt))
417 R_DrawModelMesh(skinframe->base, aliasvertcolor, 0, 0, 0);
418 if (skinframe->pants)
420 c = (currentrenderentity->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
421 color = (qbyte *) (&d_8to24table[c]);
422 if (c >= 224) // fullbright ranges
423 R_DrawModelMesh(skinframe->pants, NULL, color[0] * (1.0f / 255.0f), color[1] * (1.0f / 255.0f), color[2] * (1.0f / 255.0f));
426 R_TintModel(aliasvertcolor, aliasvertcolor2, model->numverts, color[0] * (1.0f / 255.0f), color[1] * (1.0f / 255.0f), color[2] * (1.0f / 255.0f));
427 R_DrawModelMesh(skinframe->pants, aliasvertcolor2, 0, 0, 0);
430 if (skinframe->shirt)
432 c = currentrenderentity->colormap & 0xF0 ;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
433 color = (qbyte *) (&d_8to24table[c]);
434 if (c >= 224) // fullbright ranges
435 R_DrawModelMesh(skinframe->shirt, NULL, color[0] * (1.0f / 255.0f), color[1] * (1.0f / 255.0f), color[2] * (1.0f / 255.0f));
438 R_TintModel(aliasvertcolor, aliasvertcolor2, model->numverts, color[0] * (1.0f / 255.0f), color[1] * (1.0f / 255.0f), color[2] * (1.0f / 255.0f));
439 R_DrawModelMesh(skinframe->shirt, aliasvertcolor2, 0, 0, 0);
445 if (skinframe->merged)
446 R_DrawModelMesh(skinframe->merged, aliasvertcolor, 0, 0, 0);
449 if (skinframe->base) R_DrawModelMesh(skinframe->base, aliasvertcolor, 0, 0, 0);
450 if (skinframe->pants) R_DrawModelMesh(skinframe->pants, aliasvertcolor, 0, 0, 0);
451 if (skinframe->shirt) R_DrawModelMesh(skinframe->shirt, aliasvertcolor, 0, 0, 0);
454 if (skinframe->glow) R_DrawModelMesh(skinframe->glow, NULL, 1 - fog, 1 - fog, 1 - fog);
457 R_DrawModelMesh(0, NULL, 1 - fog, 1 - fog, 1 - fog);
461 rmeshbufferinfo_t bufmesh;
462 memset(&bufmesh, 0, sizeof(bufmesh));
463 if (currentrenderentity->effects & EF_ADDITIVE)
465 bufmesh.transparent = true;
466 bufmesh.blendfunc1 = GL_SRC_ALPHA;
467 bufmesh.blendfunc2 = GL_ONE;
469 else if (currentrenderentity->alpha != 1.0 || skinframe->fog != NULL)
471 bufmesh.transparent = true;
472 bufmesh.blendfunc1 = GL_SRC_ALPHA;
473 bufmesh.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
477 bufmesh.transparent = false;
478 bufmesh.blendfunc1 = GL_ONE;
479 bufmesh.blendfunc2 = GL_ZERO;
481 bufmesh.numtriangles = model->numtris;
482 bufmesh.numverts = model->numverts;
483 bufmesh.tex[0] = R_GetTexture(skinframe->merged);
485 R_Mesh_Draw_GetBuffer(&bufmesh, true);
487 aliasvert = bufmesh.vertex;
488 aliasvertcolor = bufmesh.color;
489 memcpy(bufmesh.index, model->mdlmd2data_indices, bufmesh.numtriangles * sizeof(int[3]));
490 memcpy(bufmesh.texcoords[0], model->mdlmd2data_texcoords, bufmesh.numverts * sizeof(float[2]));
492 fog = bufmesh.colorscale * (1 - fog);
493 R_SetupMDLMD2Frames(fog, fog, fog);
495 aliasvert = aliasvertbuf;
496 aliasvertcolor = aliasvertcolorbuf;
500 int ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, zymbone_t *bone)
503 float lerp1, lerp2, lerp3, lerp4;
504 zymbonematrix *out, rootmatrix, m, *bone1, *bone2, *bone3, *bone4;
506 // LordHavoc: combine transform from zym coordinate space to quake coordinate space with model to world transform matrix
507 rootmatrix.m[0][0] = softwaretransform_matrix[0][1];
508 rootmatrix.m[0][1] = -softwaretransform_matrix[0][0];
509 rootmatrix.m[0][2] = softwaretransform_matrix[0][2];
510 rootmatrix.m[0][3] = softwaretransform_matrix[0][3];
511 rootmatrix.m[1][0] = softwaretransform_matrix[1][1];
512 rootmatrix.m[1][1] = -softwaretransform_matrix[1][0];
513 rootmatrix.m[1][2] = softwaretransform_matrix[1][2];
514 rootmatrix.m[1][3] = softwaretransform_matrix[1][3];
515 rootmatrix.m[2][0] = softwaretransform_matrix[2][1];
516 rootmatrix.m[2][1] = -softwaretransform_matrix[2][0];
517 rootmatrix.m[2][2] = softwaretransform_matrix[2][2];
518 rootmatrix.m[2][3] = softwaretransform_matrix[2][3];
520 bone1 = bonebase + blend[0].frame * count;
521 lerp1 = blend[0].lerp;
524 bone2 = bonebase + blend[1].frame * count;
525 lerp2 = blend[1].lerp;
528 bone3 = bonebase + blend[2].frame * count;
529 lerp3 = blend[2].lerp;
533 bone4 = bonebase + blend[3].frame * count;
534 lerp4 = blend[3].lerp;
535 for (i = 0, out = zymbonepose;i < count;i++, out++)
537 // interpolate matrices
538 m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3 + bone4->m[0][0] * lerp4;
539 m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3 + bone4->m[0][1] * lerp4;
540 m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3 + bone4->m[0][2] * lerp4;
541 m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3 + bone4->m[0][3] * lerp4;
542 m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3 + bone4->m[1][0] * lerp4;
543 m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3 + bone4->m[1][1] * lerp4;
544 m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3 + bone4->m[1][2] * lerp4;
545 m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3 + bone4->m[1][3] * lerp4;
546 m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3 + bone4->m[2][0] * lerp4;
547 m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3 + bone4->m[2][1] * lerp4;
548 m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3 + bone4->m[2][2] * lerp4;
549 m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3 + bone4->m[2][3] * lerp4;
550 if (bone->parent >= 0)
551 R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
553 R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
564 for (i = 0, out = zymbonepose;i < count;i++, out++)
566 // interpolate matrices
567 m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3;
568 m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3;
569 m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3;
570 m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3;
571 m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3;
572 m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3;
573 m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3;
574 m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3;
575 m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3;
576 m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3;
577 m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3;
578 m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3;
579 if (bone->parent >= 0)
580 R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
582 R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
593 for (i = 0, out = zymbonepose;i < count;i++, out++)
595 // interpolate matrices
596 m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2;
597 m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2;
598 m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2;
599 m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2;
600 m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2;
601 m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2;
602 m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2;
603 m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2;
604 m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2;
605 m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2;
606 m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2;
607 m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2;
608 if (bone->parent >= 0)
609 R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
611 R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
624 for (i = 0, out = zymbonepose;i < count;i++, out++)
626 // interpolate matrices
627 m.m[0][0] = bone1->m[0][0] * lerp1;
628 m.m[0][1] = bone1->m[0][1] * lerp1;
629 m.m[0][2] = bone1->m[0][2] * lerp1;
630 m.m[0][3] = bone1->m[0][3] * lerp1;
631 m.m[1][0] = bone1->m[1][0] * lerp1;
632 m.m[1][1] = bone1->m[1][1] * lerp1;
633 m.m[1][2] = bone1->m[1][2] * lerp1;
634 m.m[1][3] = bone1->m[1][3] * lerp1;
635 m.m[2][0] = bone1->m[2][0] * lerp1;
636 m.m[2][1] = bone1->m[2][1] * lerp1;
637 m.m[2][2] = bone1->m[2][2] * lerp1;
638 m.m[2][3] = bone1->m[2][3] * lerp1;
639 if (bone->parent >= 0)
640 R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
642 R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
650 for (i = 0, out = zymbonepose;i < count;i++, out++)
652 if (bone->parent >= 0)
653 R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &bone1->m[0][0], &out->m[0][0]);
655 R_ConcatTransforms(&rootmatrix.m[0][0], &bone1->m[0][0], &out->m[0][0]);
664 void ZymoticTransformVerts(int vertcount, int *bonecounts, zymvertex_t *vert)
667 float *out = aliasvert;
668 zymbonematrix *matrix;
672 // FIXME: validate bonecounts at load time (must be >= 1)
673 // FIXME: need 4th component in origin, for how much of the translate to blend in
676 matrix = &zymbonepose[vert->bonenum];
677 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];
678 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];
679 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];
687 matrix = &zymbonepose[vert->bonenum];
688 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];
689 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];
690 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];
698 void ZymoticCalcNormals(int vertcount, int shadercount, int *renderlist)
701 float *out, v1[3], v2[3], normal[3], s;
704 memset(aliasvertnorm, 0, sizeof(float) * vertcount * 3);
705 memset(aliasvertusage, 0, sizeof(int) * vertcount);
706 // parse render list and accumulate surface normals
715 v1[0] = aliasvert[a+0] - aliasvert[b+0];
716 v1[1] = aliasvert[a+1] - aliasvert[b+1];
717 v1[2] = aliasvert[a+2] - aliasvert[b+2];
718 v2[0] = aliasvert[c+0] - aliasvert[b+0];
719 v2[1] = aliasvert[c+1] - aliasvert[b+1];
720 v2[2] = aliasvert[c+2] - aliasvert[b+2];
721 CrossProduct(v1, v2, normal);
722 VectorNormalizeFast(normal);
723 // add surface normal to vertices
724 a = renderlist[0] * 3;
725 aliasvertnorm[a+0] += normal[0];
726 aliasvertnorm[a+1] += normal[1];
727 aliasvertnorm[a+2] += normal[2];
728 aliasvertusage[renderlist[0]]++;
729 a = renderlist[1] * 3;
730 aliasvertnorm[a+0] += normal[0];
731 aliasvertnorm[a+1] += normal[1];
732 aliasvertnorm[a+2] += normal[2];
733 aliasvertusage[renderlist[1]]++;
734 a = renderlist[2] * 3;
735 aliasvertnorm[a+0] += normal[0];
736 aliasvertnorm[a+1] += normal[1];
737 aliasvertnorm[a+2] += normal[2];
738 aliasvertusage[renderlist[2]]++;
742 // FIXME: precalc this
743 // average surface normals
760 void R_DrawZymoticModelMesh(zymtype1header_t *m)
763 rtexture_t **texture;
765 // FIXME: do better fog
766 renderlist = (int *)(m->lump_render.start + (int) m);
767 texture = (rtexture_t **)(m->lump_shaders.start + (int) m);
769 aliasmeshinfo.vertex = aliasvert;
770 aliasmeshinfo.vertexstep = sizeof(float[4]);
771 aliasmeshinfo.color = aliasvertcolor;
772 aliasmeshinfo.colorstep = sizeof(float[4]);
773 aliasmeshinfo.texcoords[0] = (float *)(m->lump_texcoords.start + (int) m);
774 aliasmeshinfo.texcoordstep[0] = sizeof(float[2]);
776 for (i = 0;i < m->numshaders;i++)
778 aliasmeshinfo.tex[0] = R_GetTexture(texture[i]);
779 if (currentrenderentity->effects & EF_ADDITIVE)
781 aliasmeshinfo.transparent = true;
782 aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
783 aliasmeshinfo.blendfunc2 = GL_ONE;
785 else if (currentrenderentity->alpha != 1.0 || R_TextureHasAlpha(texture[i]))
787 aliasmeshinfo.transparent = true;
788 aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
789 aliasmeshinfo.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
793 aliasmeshinfo.transparent = false;
794 aliasmeshinfo.blendfunc1 = GL_ONE;
795 aliasmeshinfo.blendfunc2 = GL_ZERO;
797 aliasmeshinfo.numtriangles = *renderlist++;
798 aliasmeshinfo.index = renderlist;
799 c_alias_polys += aliasmeshinfo.numtriangles;
800 R_Mesh_Draw(&aliasmeshinfo);
801 renderlist += aliasmeshinfo.numtriangles * 3;
805 void R_DrawZymoticModelMeshFog(vec3_t org, zymtype1header_t *m, float fog)
809 // FIXME: do better fog
810 renderlist = (int *)(m->lump_render.start + (int) m);
812 aliasmeshinfo.tex[0] = 0;
813 aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
814 aliasmeshinfo.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
816 aliasmeshinfo.cr = fogcolor[0];
817 aliasmeshinfo.cg = fogcolor[1];
818 aliasmeshinfo.cb = fogcolor[2];
819 aliasmeshinfo.ca = currentrenderentity->alpha * fog;
821 for (i = 0;i < m->numshaders;i++)
823 aliasmeshinfo.numtriangles = *renderlist++;
824 aliasmeshinfo.index = renderlist;
825 c_alias_polys += aliasmeshinfo.numtriangles;
826 R_Mesh_Draw(&aliasmeshinfo);
827 renderlist += aliasmeshinfo.numtriangles * 3;
831 void R_DrawZymoticModel (float fog)
835 // FIXME: do better fog
836 m = currentrenderentity->model->zymdata_header;
837 ZymoticLerpBones(m->numbones, (zymbonematrix *)(m->lump_poses.start + (int) m), currentrenderentity->frameblend, (zymbone_t *)(m->lump_bones.start + (int) m));
838 ZymoticTransformVerts(m->numverts, (int *)(m->lump_vertbonecounts.start + (int) m), (zymvertex_t *)(m->lump_verts.start + (int) m));
839 ZymoticCalcNormals(m->numverts, m->numshaders, (int *)(m->lump_render.start + (int) m));
841 R_LightModel(m->numverts, 1 - fog, 1 - fog, 1 - fog, true);
843 memset(&aliasmeshinfo, 0, sizeof(aliasmeshinfo));
844 aliasmeshinfo.numverts = m->numverts;
846 R_DrawZymoticModelMesh(m);
849 R_DrawZymoticModelMeshFog(currentrenderentity->origin, m, fog);
852 void R_DrawAliasModel (void)
857 if (currentrenderentity->alpha < (1.0f / 64.0f))
858 return; // basically completely transparent
862 softwaretransformforentity(currentrenderentity);
867 VectorSubtract(currentrenderentity->origin, r_origin, diff);
868 fog = DotProduct(diff,diff);
871 fog = exp(fogdensity/fog);
876 // fog method: darken, additive fog
877 // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
878 // 2. render fog as additive
881 if (currentrenderentity->model->aliastype == ALIASTYPE_ZYM)
882 R_DrawZymoticModel(fog);
884 R_DrawQ1Q2AliasModel(fog);