got rid of R_Mesh_Draw and R_Mesh_DrawPolygon, now all 3D geometry is drawn using...
[xonotic/darkplaces.git] / gl_models.c
1
2 #include "quakedef.h"
3
4 cvar_t r_quickmodels = {0, "r_quickmodels", "1"};
5
6 typedef struct
7 {
8         float m[3][4];
9 } zymbonematrix;
10
11 // LordHavoc: vertex arrays
12
13 float *aliasvertbuf;
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
17
18 float *aliasvertcolor2;
19 float *aliasvertnorm;
20 int *aliasvertusage;
21 zymbonematrix *zymbonepose;
22
23 mempool_t *gl_models_mempool;
24
25 void gl_models_start(void)
26 {
27         // allocate vertex processing arrays
28         gl_models_mempool = Mem_AllocPool("GL_Models");
29         aliasvert = aliasvertbuf = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4]));
30         aliasvertcolor = aliasvertcolorbuf = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4]));
31         aliasvertnorm = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][3]));
32         aliasvertcolor2 = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4])); // used temporarily for tinted coloring
33         zymbonepose = Mem_Alloc(gl_models_mempool, sizeof(zymbonematrix[256]));
34         aliasvertusage = Mem_Alloc(gl_models_mempool, sizeof(int[MD2MAX_VERTS]));
35 }
36
37 void gl_models_shutdown(void)
38 {
39         Mem_FreePool(&gl_models_mempool);
40 }
41
42 void gl_models_newmap(void)
43 {
44 }
45
46 void GL_Models_Init(void)
47 {
48         Cvar_RegisterVariable(&r_quickmodels);
49
50         R_RegisterModule("GL_Models", gl_models_start, gl_models_shutdown, gl_models_newmap);
51 }
52
53 void R_AliasTransformVerts(int vertcount)
54 {
55         vec3_t point;
56         float *av;
57         av = aliasvert;
58         while (vertcount >= 4)
59         {
60                 VectorCopy(av, point);softwaretransform(point, av);av += 4;
61                 VectorCopy(av, point);softwaretransform(point, av);av += 4;
62                 VectorCopy(av, point);softwaretransform(point, av);av += 4;
63                 VectorCopy(av, point);softwaretransform(point, av);av += 4;
64                 vertcount -= 4;
65         }
66         while(vertcount > 0)
67         {
68                 VectorCopy(av, point);softwaretransform(point, av);av += 4;
69                 vertcount--;
70         }
71 }
72
73 void R_AliasLerpVerts(int vertcount,
74                 float lerp1, trivertx_t *verts1, vec3_t fscale1, vec3_t translate1,
75                 float lerp2, trivertx_t *verts2, vec3_t fscale2, vec3_t translate2,
76                 float lerp3, trivertx_t *verts3, vec3_t fscale3, vec3_t translate3,
77                 float lerp4, trivertx_t *verts4, vec3_t fscale4, vec3_t translate4)
78 {
79         int i;
80         vec3_t scale1, scale2, scale3, scale4, translate;
81         float *n1, *n2, *n3, *n4;
82         float *av, *avn;
83         av = aliasvert;
84         avn = aliasvertnorm;
85         VectorScale(fscale1, lerp1, scale1);
86         if (lerp2)
87         {
88                 VectorScale(fscale2, lerp2, scale2);
89                 if (lerp3)
90                 {
91                         VectorScale(fscale3, lerp3, scale3);
92                         if (lerp4)
93                         {
94                                 VectorScale(fscale4, lerp4, scale4);
95                                 translate[0] = translate1[0] * lerp1 + translate2[0] * lerp2 + translate3[0] * lerp3 + translate4[0] * lerp4;
96                                 translate[1] = translate1[1] * lerp1 + translate2[1] * lerp2 + translate3[1] * lerp3 + translate4[1] * lerp4;
97                                 translate[2] = translate1[2] * lerp1 + translate2[2] * lerp2 + translate3[2] * lerp3 + translate4[2] * lerp4;
98                                 // generate vertices
99                                 for (i = 0;i < vertcount;i++)
100                                 {
101                                         av[0] = verts1->v[0] * scale1[0] + verts2->v[0] * scale2[0] + verts3->v[0] * scale3[0] + verts4->v[0] * scale4[0] + translate[0];
102                                         av[1] = verts1->v[1] * scale1[1] + verts2->v[1] * scale2[1] + verts3->v[1] * scale3[1] + verts4->v[1] * scale4[1] + translate[1];
103                                         av[2] = verts1->v[2] * scale1[2] + verts2->v[2] * scale2[2] + verts3->v[2] * scale3[2] + verts4->v[2] * scale4[2] + translate[2];
104                                         n1 = m_bytenormals[verts1->lightnormalindex];
105                                         n2 = m_bytenormals[verts2->lightnormalindex];
106                                         n3 = m_bytenormals[verts3->lightnormalindex];
107                                         n4 = m_bytenormals[verts4->lightnormalindex];
108                                         avn[0] = n1[0] * lerp1 + n2[0] * lerp2 + n3[0] * lerp3 + n4[0] * lerp4;
109                                         avn[1] = n1[1] * lerp1 + n2[1] * lerp2 + n3[1] * lerp3 + n4[1] * lerp4;
110                                         avn[2] = n1[2] * lerp1 + n2[2] * lerp2 + n3[2] * lerp3 + n4[2] * lerp4;
111                                         av += 4;
112                                         avn += 3;
113                                         verts1++;verts2++;verts3++;verts4++;
114                                 }
115                         }
116                         else
117                         {
118                                 translate[0] = translate1[0] * lerp1 + translate2[0] * lerp2 + translate3[0] * lerp3;
119                                 translate[1] = translate1[1] * lerp1 + translate2[1] * lerp2 + translate3[1] * lerp3;
120                                 translate[2] = translate1[2] * lerp1 + translate2[2] * lerp2 + translate3[2] * lerp3;
121                                 // generate vertices
122                                 for (i = 0;i < vertcount;i++)
123                                 {
124                                         av[0] = verts1->v[0] * scale1[0] + verts2->v[0] * scale2[0] + verts3->v[0] * scale3[0] + translate[0];
125                                         av[1] = verts1->v[1] * scale1[1] + verts2->v[1] * scale2[1] + verts3->v[1] * scale3[1] + translate[1];
126                                         av[2] = verts1->v[2] * scale1[2] + verts2->v[2] * scale2[2] + verts3->v[2] * scale3[2] + translate[2];
127                                         n1 = m_bytenormals[verts1->lightnormalindex];
128                                         n2 = m_bytenormals[verts2->lightnormalindex];
129                                         n3 = m_bytenormals[verts3->lightnormalindex];
130                                         avn[0] = n1[0] * lerp1 + n2[0] * lerp2 + n3[0] * lerp3;
131                                         avn[1] = n1[1] * lerp1 + n2[1] * lerp2 + n3[1] * lerp3;
132                                         avn[2] = n1[2] * lerp1 + n2[2] * lerp2 + n3[2] * lerp3;
133                                         av += 4;
134                                         avn += 3;
135                                         verts1++;verts2++;verts3++;
136                                 }
137                         }
138                 }
139                 else
140                 {
141                         translate[0] = translate1[0] * lerp1 + translate2[0] * lerp2;
142                         translate[1] = translate1[1] * lerp1 + translate2[1] * lerp2;
143                         translate[2] = translate1[2] * lerp1 + translate2[2] * lerp2;
144                         // generate vertices
145                         for (i = 0;i < vertcount;i++)
146                         {
147                                 av[0] = verts1->v[0] * scale1[0] + verts2->v[0] * scale2[0] + translate[0];
148                                 av[1] = verts1->v[1] * scale1[1] + verts2->v[1] * scale2[1] + translate[1];
149                                 av[2] = verts1->v[2] * scale1[2] + verts2->v[2] * scale2[2] + translate[2];
150                                 n1 = m_bytenormals[verts1->lightnormalindex];
151                                 n2 = m_bytenormals[verts2->lightnormalindex];
152                                 avn[0] = n1[0] * lerp1 + n2[0] * lerp2;
153                                 avn[1] = n1[1] * lerp1 + n2[1] * lerp2;
154                                 avn[2] = n1[2] * lerp1 + n2[2] * lerp2;
155                                 av += 4;
156                                 avn += 3;
157                                 verts1++;verts2++;
158                         }
159                 }
160         }
161         else
162         {
163                 translate[0] = translate1[0] * lerp1;
164                 translate[1] = translate1[1] * lerp1;
165                 translate[2] = translate1[2] * lerp1;
166                 // generate vertices
167                 if (lerp1 != 1)
168                 {
169                         // general but almost never used case
170                         for (i = 0;i < vertcount;i++)
171                         {
172                                 av[0] = verts1->v[0] * scale1[0] + translate[0];
173                                 av[1] = verts1->v[1] * scale1[1] + translate[1];
174                                 av[2] = verts1->v[2] * scale1[2] + translate[2];
175                                 n1 = m_bytenormals[verts1->lightnormalindex];
176                                 avn[0] = n1[0] * lerp1;
177                                 avn[1] = n1[1] * lerp1;
178                                 avn[2] = n1[2] * lerp1;
179                                 av += 4;
180                                 avn += 3;
181                                 verts1++;
182                         }
183                 }
184                 else
185                 {
186                         // fast normal case
187                         for (i = 0;i < vertcount;i++)
188                         {
189                                 av[0] = verts1->v[0] * scale1[0] + translate[0];
190                                 av[1] = verts1->v[1] * scale1[1] + translate[1];
191                                 av[2] = verts1->v[2] * scale1[2] + translate[2];
192                                 VectorCopy(m_bytenormals[verts1->lightnormalindex], avn);
193                                 av += 4;
194                                 avn += 3;
195                                 verts1++;
196                         }
197                 }
198         }
199 }
200
201 skinframe_t *R_FetchSkinFrame(void)
202 {
203         model_t *model = currentrenderentity->model;
204         if (model->skinscenes[currentrenderentity->skinnum].framecount > 1)
205                 return &model->skinframes[model->skinscenes[currentrenderentity->skinnum].firstframe + (int) (cl.time * 10) % model->skinscenes[currentrenderentity->skinnum].framecount];
206         else
207                 return &model->skinframes[model->skinscenes[currentrenderentity->skinnum].firstframe];
208 }
209
210 void R_SetupMDLMD2Frames(float colorr, float colorg, float colorb)
211 {
212         md2frame_t *frame1, *frame2, *frame3, *frame4;
213         trivertx_t *frame1verts, *frame2verts, *frame3verts, *frame4verts;
214         model_t *model;
215         model = currentrenderentity->model;
216
217         frame1 = &model->mdlmd2data_frames[currentrenderentity->frameblend[0].frame];
218         frame2 = &model->mdlmd2data_frames[currentrenderentity->frameblend[1].frame];
219         frame3 = &model->mdlmd2data_frames[currentrenderentity->frameblend[2].frame];
220         frame4 = &model->mdlmd2data_frames[currentrenderentity->frameblend[3].frame];
221         frame1verts = &model->mdlmd2data_pose[currentrenderentity->frameblend[0].frame * model->numverts];
222         frame2verts = &model->mdlmd2data_pose[currentrenderentity->frameblend[1].frame * model->numverts];
223         frame3verts = &model->mdlmd2data_pose[currentrenderentity->frameblend[2].frame * model->numverts];
224         frame4verts = &model->mdlmd2data_pose[currentrenderentity->frameblend[3].frame * model->numverts];
225         R_AliasLerpVerts(model->numverts,
226                 currentrenderentity->frameblend[0].lerp, frame1verts, frame1->scale, frame1->translate,
227                 currentrenderentity->frameblend[1].lerp, frame2verts, frame2->scale, frame2->translate,
228                 currentrenderentity->frameblend[2].lerp, frame3verts, frame3->scale, frame3->translate,
229                 currentrenderentity->frameblend[3].lerp, frame4verts, frame4->scale, frame4->translate);
230
231         R_LightModel(model->numverts, colorr, colorg, colorb, false);
232
233         R_AliasTransformVerts(model->numverts);
234 }
235
236 void R_DrawQ1Q2AliasModel (float fog)
237 {
238         int c, pantsfullbright, shirtfullbright, colormapped;
239         float pantscolor[3], shirtcolor[3];
240         qbyte *bcolor;
241         rmeshbufferinfo_t m;
242         model_t *model;
243         skinframe_t *skinframe;
244
245         model = currentrenderentity->model;
246
247         skinframe = R_FetchSkinFrame();
248
249         colormapped = !skinframe->merged || (currentrenderentity->colormap >= 0 && skinframe->base && (skinframe->pants || skinframe->shirt));
250         if (!colormapped && !fog && !skinframe->glow && !skinframe->fog)
251         {
252                 // fastpath for the normal situation (one texture)
253                 memset(&m, 0, sizeof(m));
254                 if (currentrenderentity->effects & EF_ADDITIVE)
255                 {
256                         m.transparent = true;
257                         m.blendfunc1 = GL_SRC_ALPHA;
258                         m.blendfunc2 = GL_ONE;
259                 }
260                 else if (currentrenderentity->alpha != 1.0 || skinframe->fog != NULL)
261                 {
262                         m.transparent = true;
263                         m.blendfunc1 = GL_SRC_ALPHA;
264                         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
265                 }
266                 else
267                 {
268                         m.transparent = false;
269                         m.blendfunc1 = GL_ONE;
270                         m.blendfunc2 = GL_ZERO;
271                 }
272                 m.numtriangles = model->numtris;
273                 m.numverts = model->numverts;
274                 m.tex[0] = R_GetTexture(skinframe->merged);
275
276                 c_alias_polys += m.numtriangles;
277                 if (R_Mesh_Draw_GetBuffer(&m, true))
278                 {
279                         memcpy(m.index, model->mdlmd2data_indices, m.numtriangles * sizeof(int[3]));
280                         memcpy(m.texcoords[0], model->mdlmd2data_texcoords, m.numverts * sizeof(float[2]));
281
282                         aliasvert = m.vertex;
283                         aliasvertcolor = m.color;
284                         R_SetupMDLMD2Frames(m.colorscale * (1 - fog), m.colorscale * (1 - fog), m.colorscale * (1 - fog));
285                         aliasvert = aliasvertbuf;
286                         aliasvertcolor = aliasvertcolorbuf;
287
288                         R_Mesh_Render();
289                 }
290                 return;
291         }
292
293         R_SetupMDLMD2Frames(1 - fog, 1 - fog, 1 - fog);
294
295         if (colormapped)
296         {
297                 // 128-224 are backwards ranges
298                 c = (currentrenderentity->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12;
299                 bcolor = (qbyte *) (&d_8to24table[c]);
300                 pantsfullbright = c >= 224;
301                 VectorScale(bcolor, (1.0f / 255.0f), pantscolor);
302                 c = (currentrenderentity->colormap & 0xF0);c += (c >= 128 && c < 224) ? 4 : 12;
303                 bcolor = (qbyte *) (&d_8to24table[c]);
304                 shirtfullbright = c >= 224;
305                 VectorScale(bcolor, (1.0f / 255.0f), shirtcolor);
306         }
307         else
308         {
309                 pantscolor[0] = pantscolor[1] = pantscolor[2] = shirtcolor[0] = shirtcolor[1] = shirtcolor[2] = 1;
310                 pantsfullbright = shirtfullbright = false;
311         }
312
313         memset(&m, 0, sizeof(m));
314         if (currentrenderentity->effects & EF_ADDITIVE)
315         {
316                 m.transparent = true;
317                 m.blendfunc1 = GL_SRC_ALPHA;
318                 m.blendfunc2 = GL_ONE;
319         }
320         else if (currentrenderentity->alpha != 1.0 || skinframe->fog != NULL)
321         {
322                 m.transparent = true;
323                 m.blendfunc1 = GL_SRC_ALPHA;
324                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
325         }
326         else
327         {
328                 m.transparent = false;
329                 m.blendfunc1 = GL_ONE;
330                 m.blendfunc2 = GL_ZERO;
331         }
332         m.numtriangles = model->numtris;
333         m.numverts = model->numverts;
334         m.tex[0] = colormapped ? R_GetTexture(skinframe->base) : R_GetTexture(skinframe->merged);
335         if (R_Mesh_Draw_GetBuffer(&m, true))
336         {
337                 c_alias_polys += m.numtriangles;
338                 R_ModulateColors(aliasvertcolor, m.color, m.numverts, m.colorscale, m.colorscale, m.colorscale);
339                 memcpy(m.index, model->mdlmd2data_indices, m.numtriangles * sizeof(int[3]));
340                 memcpy(m.vertex, aliasvert, m.numverts * sizeof(float[4]));
341                 memcpy(m.texcoords[0], model->mdlmd2data_texcoords, m.numverts * sizeof(float[2]));
342                 R_Mesh_Render();
343         }
344
345         if (colormapped)
346         {
347                 if (skinframe->pants)
348                 {
349                         memset(&m, 0, sizeof(m));
350                         m.transparent = currentrenderentity->effects & EF_ADDITIVE || currentrenderentity->alpha != 1.0 || skinframe->fog != NULL;
351                         m.blendfunc1 = GL_SRC_ALPHA;
352                         m.blendfunc2 = GL_ONE;
353                         m.numtriangles = model->numtris;
354                         m.numverts = model->numverts;
355                         m.tex[0] = R_GetTexture(skinframe->pants);
356                         if (R_Mesh_Draw_GetBuffer(&m, true))
357                         {
358                                 c_alias_polys += m.numtriangles;
359                                 if (pantsfullbright)
360                                         R_FillColors(m.color, m.numverts, pantscolor[0] * m.colorscale, pantscolor[1] * m.colorscale, pantscolor[2] * m.colorscale, currentrenderentity->alpha);
361                                 else
362                                         R_ModulateColors(aliasvertcolor, m.color, m.numverts, pantscolor[0] * m.colorscale, pantscolor[1] * m.colorscale, pantscolor[2] * m.colorscale);
363                                 memcpy(m.index, model->mdlmd2data_indices, m.numtriangles * sizeof(int[3]));
364                                 memcpy(m.vertex, aliasvert, m.numverts * sizeof(float[4]));
365                                 memcpy(m.texcoords[0], model->mdlmd2data_texcoords, m.numverts * sizeof(float[2]));
366                                 R_Mesh_Render();
367                         }
368                 }
369                 if (skinframe->shirt)
370                 {
371                         memset(&m, 0, sizeof(m));
372                         m.transparent = currentrenderentity->effects & EF_ADDITIVE || currentrenderentity->alpha != 1.0 || skinframe->fog != NULL;
373                         m.blendfunc1 = GL_SRC_ALPHA;
374                         m.blendfunc2 = GL_ONE;
375                         m.numtriangles = model->numtris;
376                         m.numverts = model->numverts;
377                         m.tex[0] = R_GetTexture(skinframe->shirt);
378                         if (R_Mesh_Draw_GetBuffer(&m, true))
379                         {
380                                 c_alias_polys += m.numtriangles;
381                                 if (shirtfullbright)
382                                         R_FillColors(m.color, m.numverts, shirtcolor[0] * m.colorscale, shirtcolor[1] * m.colorscale, shirtcolor[2] * m.colorscale, currentrenderentity->alpha);
383                                 else
384                                         R_ModulateColors(aliasvertcolor, m.color, m.numverts, shirtcolor[0] * m.colorscale, shirtcolor[1] * m.colorscale, shirtcolor[2] * m.colorscale);
385                                 memcpy(m.index, model->mdlmd2data_indices, m.numtriangles * sizeof(int[3]));
386                                 memcpy(m.vertex, aliasvert, m.numverts * sizeof(float[4]));
387                                 memcpy(m.texcoords[0], model->mdlmd2data_texcoords, m.numverts * sizeof(float[2]));
388                                 R_Mesh_Render();
389                         }
390                 }
391         }
392         if (skinframe->glow)
393         {
394                 memset(&m, 0, sizeof(m));
395                 m.transparent = currentrenderentity->effects & EF_ADDITIVE || currentrenderentity->alpha != 1.0 || skinframe->fog != NULL;
396                 m.blendfunc1 = GL_SRC_ALPHA;
397                 m.blendfunc2 = GL_ONE;
398                 m.numtriangles = model->numtris;
399                 m.numverts = model->numverts;
400                 m.tex[0] = R_GetTexture(skinframe->glow);
401                 if (R_Mesh_Draw_GetBuffer(&m, true))
402                 {
403                         c_alias_polys += m.numtriangles;
404                         R_FillColors(m.color, m.numverts, (1 - fog) * m.colorscale, (1 - fog) * m.colorscale, (1 - fog) * m.colorscale, currentrenderentity->alpha);
405                         memcpy(m.index, model->mdlmd2data_indices, m.numtriangles * sizeof(int[3]));
406                         memcpy(m.vertex, aliasvert, m.numverts * sizeof(float[4]));
407                         memcpy(m.texcoords[0], model->mdlmd2data_texcoords, m.numverts * sizeof(float[2]));
408                         R_Mesh_Render();
409                 }
410         }
411         if (fog)
412         {
413                 memset(&m, 0, sizeof(m));
414                 m.transparent = currentrenderentity->effects & EF_ADDITIVE || currentrenderentity->alpha != 1.0 || skinframe->fog != NULL;
415                 m.blendfunc1 = GL_SRC_ALPHA;
416                 m.blendfunc2 = GL_ONE;
417                 m.numtriangles = model->numtris;
418                 m.numverts = model->numverts;
419                 m.tex[0] = R_GetTexture(skinframe->fog);
420                 if (R_Mesh_Draw_GetBuffer(&m, false))
421                 {
422                         c_alias_polys += m.numtriangles;
423                         R_FillColors(m.color, m.numverts, fog * m.colorscale, fog * m.colorscale, fog * m.colorscale, currentrenderentity->alpha);
424                         memcpy(m.index, model->mdlmd2data_indices, m.numtriangles * sizeof(int[3]));
425                         memcpy(m.vertex, aliasvert, m.numverts * sizeof(float[4]));
426                         memcpy(m.texcoords[0], model->mdlmd2data_texcoords, m.numverts * sizeof(float[2]));
427                         R_Mesh_Render();
428                 }
429         }
430 }
431
432 int ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, zymbone_t *bone)
433 {
434         int i;
435         float lerp1, lerp2, lerp3, lerp4;
436         zymbonematrix *out, rootmatrix, m, *bone1, *bone2, *bone3, *bone4;
437
438         // LordHavoc: combine transform from zym coordinate space to quake coordinate space with model to world transform matrix
439         rootmatrix.m[0][0] = softwaretransform_matrix[0][1];
440         rootmatrix.m[0][1] = -softwaretransform_matrix[0][0];
441         rootmatrix.m[0][2] = softwaretransform_matrix[0][2];
442         rootmatrix.m[0][3] = softwaretransform_matrix[0][3];
443         rootmatrix.m[1][0] = softwaretransform_matrix[1][1];
444         rootmatrix.m[1][1] = -softwaretransform_matrix[1][0];
445         rootmatrix.m[1][2] = softwaretransform_matrix[1][2];
446         rootmatrix.m[1][3] = softwaretransform_matrix[1][3];
447         rootmatrix.m[2][0] = softwaretransform_matrix[2][1];
448         rootmatrix.m[2][1] = -softwaretransform_matrix[2][0];
449         rootmatrix.m[2][2] = softwaretransform_matrix[2][2];
450         rootmatrix.m[2][3] = softwaretransform_matrix[2][3];
451
452         bone1 = bonebase + blend[0].frame * count;
453         lerp1 = blend[0].lerp;
454         if (blend[1].lerp)
455         {
456                 bone2 = bonebase + blend[1].frame * count;
457                 lerp2 = blend[1].lerp;
458                 if (blend[2].lerp)
459                 {
460                         bone3 = bonebase + blend[2].frame * count;
461                         lerp3 = blend[2].lerp;
462                         if (blend[3].lerp)
463                         {
464                                 // 4 poses
465                                 bone4 = bonebase + blend[3].frame * count;
466                                 lerp4 = blend[3].lerp;
467                                 for (i = 0, out = zymbonepose;i < count;i++, out++)
468                                 {
469                                         // interpolate matrices
470                                         m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3 + bone4->m[0][0] * lerp4;
471                                         m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3 + bone4->m[0][1] * lerp4;
472                                         m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3 + bone4->m[0][2] * lerp4;
473                                         m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3 + bone4->m[0][3] * lerp4;
474                                         m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3 + bone4->m[1][0] * lerp4;
475                                         m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3 + bone4->m[1][1] * lerp4;
476                                         m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3 + bone4->m[1][2] * lerp4;
477                                         m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3 + bone4->m[1][3] * lerp4;
478                                         m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3 + bone4->m[2][0] * lerp4;
479                                         m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3 + bone4->m[2][1] * lerp4;
480                                         m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3 + bone4->m[2][2] * lerp4;
481                                         m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3 + bone4->m[2][3] * lerp4;
482                                         if (bone->parent >= 0)
483                                                 R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
484                                         else
485                                                 R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
486                                         bone1++;
487                                         bone2++;
488                                         bone3++;
489                                         bone4++;
490                                         bone++;
491                                 }
492                         }
493                         else
494                         {
495                                 // 3 poses
496                                 for (i = 0, out = zymbonepose;i < count;i++, out++)
497                                 {
498                                         // interpolate matrices
499                                         m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3;
500                                         m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3;
501                                         m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3;
502                                         m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3;
503                                         m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3;
504                                         m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3;
505                                         m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3;
506                                         m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3;
507                                         m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3;
508                                         m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3;
509                                         m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3;
510                                         m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3;
511                                         if (bone->parent >= 0)
512                                                 R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
513                                         else
514                                                 R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
515                                         bone1++;
516                                         bone2++;
517                                         bone3++;
518                                         bone++;
519                                 }
520                         }
521                 }
522                 else
523                 {
524                         // 2 poses
525                         for (i = 0, out = zymbonepose;i < count;i++, out++)
526                         {
527                                 // interpolate matrices
528                                 m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2;
529                                 m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2;
530                                 m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2;
531                                 m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2;
532                                 m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2;
533                                 m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2;
534                                 m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2;
535                                 m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2;
536                                 m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2;
537                                 m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2;
538                                 m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2;
539                                 m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2;
540                                 if (bone->parent >= 0)
541                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
542                                 else
543                                         R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
544                                 bone1++;
545                                 bone2++;
546                                 bone++;
547                         }
548                 }
549         }
550         else
551         {
552                 // 1 pose
553                 if (lerp1 != 1)
554                 {
555                         // lerp != 1.0
556                         for (i = 0, out = zymbonepose;i < count;i++, out++)
557                         {
558                                 // interpolate matrices
559                                 m.m[0][0] = bone1->m[0][0] * lerp1;
560                                 m.m[0][1] = bone1->m[0][1] * lerp1;
561                                 m.m[0][2] = bone1->m[0][2] * lerp1;
562                                 m.m[0][3] = bone1->m[0][3] * lerp1;
563                                 m.m[1][0] = bone1->m[1][0] * lerp1;
564                                 m.m[1][1] = bone1->m[1][1] * lerp1;
565                                 m.m[1][2] = bone1->m[1][2] * lerp1;
566                                 m.m[1][3] = bone1->m[1][3] * lerp1;
567                                 m.m[2][0] = bone1->m[2][0] * lerp1;
568                                 m.m[2][1] = bone1->m[2][1] * lerp1;
569                                 m.m[2][2] = bone1->m[2][2] * lerp1;
570                                 m.m[2][3] = bone1->m[2][3] * lerp1;
571                                 if (bone->parent >= 0)
572                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
573                                 else
574                                         R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
575                                 bone1++;
576                                 bone++;
577                         }
578                 }
579                 else
580                 {
581                         // lerp == 1.0
582                         for (i = 0, out = zymbonepose;i < count;i++, out++)
583                         {
584                                 if (bone->parent >= 0)
585                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &bone1->m[0][0], &out->m[0][0]);
586                                 else
587                                         R_ConcatTransforms(&rootmatrix.m[0][0], &bone1->m[0][0], &out->m[0][0]);
588                                 bone1++;
589                                 bone++;
590                         }
591                 }
592         }
593         return true;
594 }
595
596 void ZymoticTransformVerts(int vertcount, int *bonecounts, zymvertex_t *vert)
597 {
598         int c;
599         float *out = aliasvert;
600         zymbonematrix *matrix;
601         while(vertcount--)
602         {
603                 c = *bonecounts++;
604                 // FIXME: validate bonecounts at load time (must be >= 1)
605                 // FIXME: need 4th component in origin, for how much of the translate to blend in
606                 if (c == 1)
607                 {
608                         matrix = &zymbonepose[vert->bonenum];
609                         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];
610                         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];
611                         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];
612                         vert++;
613                 }
614                 else
615                 {
616                         VectorClear(out);
617                         while(c--)
618                         {
619                                 matrix = &zymbonepose[vert->bonenum];
620                                 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];
621                                 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];
622                                 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];
623                                 vert++;
624                         }
625                 }
626                 out += 4;
627         }
628 }
629
630 void ZymoticCalcNormals(int vertcount, int shadercount, int *renderlist)
631 {
632         int a, b, c, d;
633         float *out, v1[3], v2[3], normal[3], s;
634         int *u;
635         // clear normals
636         memset(aliasvertnorm, 0, sizeof(float) * vertcount * 3);
637         memset(aliasvertusage, 0, sizeof(int) * vertcount);
638         // parse render list and accumulate surface normals
639         while(shadercount--)
640         {
641                 d = *renderlist++;
642                 while (d--)
643                 {
644                         a = renderlist[0]*4;
645                         b = renderlist[1]*4;
646                         c = renderlist[2]*4;
647                         v1[0] = aliasvert[a+0] - aliasvert[b+0];
648                         v1[1] = aliasvert[a+1] - aliasvert[b+1];
649                         v1[2] = aliasvert[a+2] - aliasvert[b+2];
650                         v2[0] = aliasvert[c+0] - aliasvert[b+0];
651                         v2[1] = aliasvert[c+1] - aliasvert[b+1];
652                         v2[2] = aliasvert[c+2] - aliasvert[b+2];
653                         CrossProduct(v1, v2, normal);
654                         VectorNormalizeFast(normal);
655                         // add surface normal to vertices
656                         a = renderlist[0] * 3;
657                         aliasvertnorm[a+0] += normal[0];
658                         aliasvertnorm[a+1] += normal[1];
659                         aliasvertnorm[a+2] += normal[2];
660                         aliasvertusage[renderlist[0]]++;
661                         a = renderlist[1] * 3;
662                         aliasvertnorm[a+0] += normal[0];
663                         aliasvertnorm[a+1] += normal[1];
664                         aliasvertnorm[a+2] += normal[2];
665                         aliasvertusage[renderlist[1]]++;
666                         a = renderlist[2] * 3;
667                         aliasvertnorm[a+0] += normal[0];
668                         aliasvertnorm[a+1] += normal[1];
669                         aliasvertnorm[a+2] += normal[2];
670                         aliasvertusage[renderlist[2]]++;
671                         renderlist += 3;
672                 }
673         }
674         // FIXME: precalc this
675         // average surface normals
676         out = aliasvertnorm;
677         u = aliasvertusage;
678         while(vertcount--)
679         {
680                 if (*u > 1)
681                 {
682                         s = ixtable[*u];
683                         out[0] *= s;
684                         out[1] *= s;
685                         out[2] *= s;
686                 }
687                 u++;
688                 out += 3;
689         }
690 }
691
692 void R_DrawZymoticModelMesh(zymtype1header_t *m, float fog)
693 {
694         rmeshbufferinfo_t mbuf;
695         int i, *renderlist;
696         rtexture_t **texture;
697
698         texture = (rtexture_t **)(m->lump_shaders.start + (int) m);
699
700         renderlist = (int *)(m->lump_render.start + (int) m);
701         for (i = 0;i < m->numshaders;i++)
702         {
703                 memset(&mbuf, 0, sizeof(mbuf));
704                 mbuf.numverts = m->numverts;
705                 mbuf.numtriangles = *renderlist++;
706                 if (currentrenderentity->effects & EF_ADDITIVE)
707                 {
708                         mbuf.transparent = true;
709                         mbuf.blendfunc1 = GL_SRC_ALPHA;
710                         mbuf.blendfunc2 = GL_ONE;
711                 }
712                 else if (currentrenderentity->alpha != 1.0 || R_TextureHasAlpha(texture[i]))
713                 {
714                         mbuf.transparent = true;
715                         mbuf.blendfunc1 = GL_SRC_ALPHA;
716                         mbuf.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
717                 }
718                 else
719                 {
720                         mbuf.transparent = false;
721                         mbuf.blendfunc1 = GL_ONE;
722                         mbuf.blendfunc2 = GL_ZERO;
723                 }
724                 mbuf.tex[0] = R_GetTexture(texture[i]);
725                 if (R_Mesh_Draw_GetBuffer(&mbuf, true))
726                 {
727                         c_alias_polys += mbuf.numtriangles;
728                         memcpy(mbuf.index, renderlist, mbuf.numtriangles * sizeof(int[3]));
729                         memcpy(mbuf.vertex, aliasvert, mbuf.numverts * sizeof(float[4]));
730                         R_ModulateColors(aliasvertcolor, mbuf.color, mbuf.numverts, mbuf.colorscale, mbuf.colorscale, mbuf.colorscale);
731                         //memcpy(mbuf.color, aliasvertcolor, mbuf.numverts * sizeof(float[4]));
732                         memcpy(mbuf.texcoords[0], (float *)(m->lump_texcoords.start + (int) m), mbuf.numverts * sizeof(float[2]));
733                         R_Mesh_Render();
734                 }
735                 renderlist += mbuf.numtriangles * 3;
736         }
737
738         if (fog)
739         {
740                 renderlist = (int *)(m->lump_render.start + (int) m);
741                 for (i = 0;i < m->numshaders;i++)
742                 {
743                         memset(&mbuf, 0, sizeof(mbuf));
744                         mbuf.numverts = m->numverts;
745                         mbuf.numtriangles = *renderlist++;
746                         mbuf.transparent = currentrenderentity->effects & EF_ADDITIVE || currentrenderentity->alpha != 1.0 || R_TextureHasAlpha(texture[i]);
747                         mbuf.blendfunc1 = GL_SRC_ALPHA;
748                         mbuf.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
749                         // FIXME: need alpha mask for fogging...
750                         //mbuf.tex[0] = R_GetTexture(texture[i]);
751                         if (R_Mesh_Draw_GetBuffer(&mbuf, false))
752                         {
753                                 c_alias_polys += mbuf.numtriangles;
754                                 memcpy(mbuf.index, renderlist, mbuf.numtriangles * sizeof(int[3]));
755                                 memcpy(mbuf.vertex, aliasvert, mbuf.numverts * sizeof(float[4]));
756                                 R_FillColors(mbuf.color, mbuf.numverts, fogcolor[0] * mbuf.colorscale, fogcolor[1] * mbuf.colorscale, fogcolor[2] * mbuf.colorscale, currentrenderentity->alpha * fog);
757                                 //memcpy(mbuf.texcoords[0], (float *)(m->lump_texcoords.start + (int) m), mbuf.numverts * sizeof(float[2]));
758                                 R_Mesh_Render();
759                         }
760                         renderlist += mbuf.numtriangles * 3;
761                 }
762         }
763 }
764
765 void R_DrawZymoticModel (float fog)
766 {
767         zymtype1header_t *m;
768
769         // FIXME: do better fog
770         m = currentrenderentity->model->zymdata_header;
771         ZymoticLerpBones(m->numbones, (zymbonematrix *)(m->lump_poses.start + (int) m), currentrenderentity->frameblend, (zymbone_t *)(m->lump_bones.start + (int) m));
772         ZymoticTransformVerts(m->numverts, (int *)(m->lump_vertbonecounts.start + (int) m), (zymvertex_t *)(m->lump_verts.start + (int) m));
773         ZymoticCalcNormals(m->numverts, m->numshaders, (int *)(m->lump_render.start + (int) m));
774
775         R_LightModel(m->numverts, 1 - fog, 1 - fog, 1 - fog, true);
776
777         R_DrawZymoticModelMesh(m, fog);
778 }
779
780 void R_DrawAliasModel (void)
781 {
782         float fog;
783         vec3_t diff;
784
785         if (currentrenderentity->alpha < (1.0f / 64.0f))
786                 return; // basically completely transparent
787
788         c_models++;
789
790         softwaretransformforentity(currentrenderentity);
791
792         fog = 0;
793         if (fogenabled)
794         {
795                 VectorSubtract(currentrenderentity->origin, r_origin, diff);
796                 fog = DotProduct(diff,diff);
797                 if (fog < 0.01f)
798                         fog = 0.01f;
799                 fog = exp(fogdensity/fog);
800                 if (fog > 1)
801                         fog = 1;
802                 if (fog < 0.01f)
803                         fog = 0;
804                 // fog method: darken, additive fog
805                 // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
806                 // 2. render fog as additive
807         }
808
809         if (currentrenderentity->model->aliastype == ALIASTYPE_ZYM)
810                 R_DrawZymoticModel(fog);
811         else
812                 R_DrawQ1Q2AliasModel(fog);
813 }
814