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