transform matrix is now separate from R_Mesh_State, a new function called R_Mesh_Matr...
[divverent/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         rmeshstate_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         R_Mesh_Matrix(&ent->matrix);
255
256         fog = 0;
257         if (fogenabled)
258         {
259                 VectorSubtract(ent->origin, r_origin, diff);
260                 fog = DotProduct(diff,diff);
261                 if (fog < 0.01f)
262                         fog = 0.01f;
263                 fog = exp(fogdensity/fog);
264                 if (fog > 1)
265                         fog = 1;
266                 if (fog < 0.01f)
267                         fog = 0;
268                 // fog method: darken, additive fog
269                 // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
270                 // 2. render fog as additive
271         }
272
273         model = ent->model;
274         R_Mesh_ResizeCheck(model->numverts, model->numtris);
275
276         skinframe = R_FetchSkinFrame(ent);
277
278         if (ent->effects & EF_ADDITIVE)
279         {
280                 blendfunc1 = GL_SRC_ALPHA;
281                 blendfunc2 = GL_ONE;
282         }
283         else if (ent->alpha != 1.0 || skinframe->fog != NULL)
284         {
285                 blendfunc1 = GL_SRC_ALPHA;
286                 blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
287         }
288         else
289         {
290                 blendfunc1 = GL_ONE;
291                 blendfunc2 = GL_ZERO;
292         }
293
294         if (!skinframe->base && !skinframe->pants && !skinframe->shirt && !skinframe->glow)
295         {
296                 // untextured
297                 memset(&m, 0, sizeof(m));
298                 m.blendfunc1 = blendfunc1;
299                 m.blendfunc2 = blendfunc2;
300                 m.wantoverbright = true;
301                 m.tex[0] = R_GetTexture(r_notexture);
302                 R_Mesh_State(&m);
303
304                 c_alias_polys += model->numtris;
305                 memcpy(varray_element, model->mdlmd2data_indices, model->numtris * sizeof(int[3]));
306                 for (i = 0;i < model->numverts * 2;i++)
307                         varray_texcoord[0][i] = model->mdlmd2data_texcoords[i] * 8.0f;
308                 aliasvert = varray_vertex;
309                 aliasvertcolor = varray_color;
310                 R_SetupMDLMD2Frames(ent, mesh_colorscale, mesh_colorscale, mesh_colorscale);
311                 aliasvert = aliasvertbuf;
312                 aliasvertcolor = aliasvertcolorbuf;
313                 R_Mesh_Draw(model->numverts, model->numtris);
314                 return;
315         }
316
317
318         colormapped = !skinframe->merged || (ent->colormap >= 0 && skinframe->base && (skinframe->pants || skinframe->shirt));
319         if (!colormapped && !fog && !skinframe->glow && !skinframe->fog)
320         {
321                 // fastpath for the normal situation (one texture)
322                 memset(&m, 0, sizeof(m));
323                 m.blendfunc1 = blendfunc1;
324                 m.blendfunc2 = blendfunc2;
325                 m.wantoverbright = true;
326                 m.tex[0] = R_GetTexture(skinframe->merged);
327                 R_Mesh_State(&m);
328
329                 c_alias_polys += model->numtris;
330                 memcpy(varray_element, model->mdlmd2data_indices, model->numtris * sizeof(int[3]));
331                 memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
332                 aliasvert = varray_vertex;
333                 aliasvertcolor = varray_color;
334                 R_SetupMDLMD2Frames(ent, mesh_colorscale, mesh_colorscale, mesh_colorscale);
335                 aliasvert = aliasvertbuf;
336                 aliasvertcolor = aliasvertcolorbuf;
337                 R_Mesh_Draw(model->numverts, model->numtris);
338                 return;
339         }
340
341         R_SetupMDLMD2Frames(ent, 1 - fog, 1 - fog, 1 - fog);
342
343         if (colormapped)
344         {
345                 // 128-224 are backwards ranges
346                 c = (ent->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12;
347                 bcolor = (qbyte *) (&d_8to24table[c]);
348                 pantsfullbright = c >= 224;
349                 VectorScale(bcolor, (1.0f / 255.0f), pantscolor);
350                 c = (ent->colormap & 0xF0);c += (c >= 128 && c < 224) ? 4 : 12;
351                 bcolor = (qbyte *) (&d_8to24table[c]);
352                 shirtfullbright = c >= 224;
353                 VectorScale(bcolor, (1.0f / 255.0f), shirtcolor);
354         }
355         else
356         {
357                 pantscolor[0] = pantscolor[1] = pantscolor[2] = shirtcolor[0] = shirtcolor[1] = shirtcolor[2] = 1;
358                 pantsfullbright = shirtfullbright = false;
359         }
360
361         memset(&m, 0, sizeof(m));
362         m.blendfunc1 = blendfunc1;
363         m.blendfunc2 = blendfunc2;
364         m.wantoverbright = true;
365         m.tex[0] = colormapped ? R_GetTexture(skinframe->base) : R_GetTexture(skinframe->merged);
366         if (m.tex[0])
367         {
368                 R_Mesh_State(&m);
369
370                 blendfunc1 = GL_SRC_ALPHA;
371                 blendfunc2 = GL_ONE;
372                 c_alias_polys += model->numtris;
373                 R_ModulateColors(aliasvertcolor, varray_color, model->numverts, mesh_colorscale, mesh_colorscale, mesh_colorscale);
374                 memcpy(varray_element, model->mdlmd2data_indices, model->numtris * sizeof(int[3]));
375                 memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
376                 memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
377                 R_Mesh_Draw(model->numverts, model->numtris);
378         }
379
380         if (colormapped)
381         {
382                 if (skinframe->pants)
383                 {
384                         memset(&m, 0, sizeof(m));
385                         m.blendfunc1 = blendfunc1;
386                         m.blendfunc2 = blendfunc2;
387                         m.wantoverbright = true;
388                         m.tex[0] = R_GetTexture(skinframe->pants);
389                         if (m.tex[0])
390                         {
391                                 R_Mesh_State(&m);
392
393                                 blendfunc1 = GL_SRC_ALPHA;
394                                 blendfunc2 = GL_ONE;
395                                 c_alias_polys += model->numtris;
396                                 if (pantsfullbright)
397                                         R_FillColors(varray_color, model->numverts, pantscolor[0] * mesh_colorscale, pantscolor[1] * mesh_colorscale, pantscolor[2] * mesh_colorscale, ent->alpha);
398                                 else
399                                         R_ModulateColors(aliasvertcolor, varray_color, model->numverts, pantscolor[0] * mesh_colorscale, pantscolor[1] * mesh_colorscale, pantscolor[2] * mesh_colorscale);
400                                 memcpy(varray_element, model->mdlmd2data_indices, model->numtris * sizeof(int[3]));
401                                 memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
402                                 memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
403                                 R_Mesh_Draw(model->numverts, model->numtris);
404                         }
405                 }
406                 if (skinframe->shirt)
407                 {
408                         memset(&m, 0, sizeof(m));
409                         m.blendfunc1 = blendfunc1;
410                         m.blendfunc2 = blendfunc2;
411                         m.wantoverbright = true;
412                         m.tex[0] = R_GetTexture(skinframe->shirt);
413                         if (m.tex[0])
414                         {
415                                 R_Mesh_State(&m);
416
417                                 blendfunc1 = GL_SRC_ALPHA;
418                                 blendfunc2 = GL_ONE;
419                                 c_alias_polys += model->numtris;
420                                 if (shirtfullbright)
421                                         R_FillColors(varray_color, model->numverts, shirtcolor[0] * mesh_colorscale, shirtcolor[1] * mesh_colorscale, shirtcolor[2] * mesh_colorscale, ent->alpha);
422                                 else
423                                         R_ModulateColors(aliasvertcolor, varray_color, model->numverts, shirtcolor[0] * mesh_colorscale, shirtcolor[1] * mesh_colorscale, shirtcolor[2] * mesh_colorscale);
424                                 memcpy(varray_element, model->mdlmd2data_indices, model->numtris * sizeof(int[3]));
425                                 memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
426                                 memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
427                                 R_Mesh_Draw(model->numverts, model->numtris);
428                         }
429                 }
430         }
431         if (skinframe->glow)
432         {
433                 memset(&m, 0, sizeof(m));
434                 m.blendfunc1 = blendfunc1;
435                 m.blendfunc2 = blendfunc2;
436                 m.wantoverbright = true;
437                 m.tex[0] = R_GetTexture(skinframe->glow);
438                 if (m.tex[0])
439                 {
440                         R_Mesh_State(&m);
441
442                         blendfunc1 = GL_SRC_ALPHA;
443                         blendfunc2 = GL_ONE;
444                         c_alias_polys += model->numtris;
445                         R_FillColors(varray_color, model->numverts, (1 - fog) * mesh_colorscale, (1 - fog) * mesh_colorscale, (1 - fog) * mesh_colorscale, ent->alpha);
446                         memcpy(varray_element, model->mdlmd2data_indices, model->numtris * sizeof(int[3]));
447                         memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
448                         memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
449                         R_Mesh_Draw(model->numverts, model->numtris);
450                 }
451         }
452         if (fog)
453         {
454                 memset(&m, 0, sizeof(m));
455                 m.blendfunc1 = GL_SRC_ALPHA;
456                 m.blendfunc2 = GL_ONE;
457                 m.wantoverbright = false;
458                 m.tex[0] = R_GetTexture(skinframe->fog);
459                 R_Mesh_State(&m);
460
461                 c_alias_polys += model->numtris;
462                 R_FillColors(varray_color, model->numverts, fogcolor[0] * fog * mesh_colorscale, fogcolor[1] * fog * mesh_colorscale, fogcolor[2] * fog * mesh_colorscale, ent->alpha);
463                 memcpy(varray_element, model->mdlmd2data_indices, model->numtris * sizeof(int[3]));
464                 memcpy(varray_vertex, aliasvert, model->numverts * sizeof(float[4]));
465                 memcpy(varray_texcoord[0], model->mdlmd2data_texcoords, model->numverts * sizeof(float[2]));
466                 R_Mesh_Draw(model->numverts, model->numtris);
467         }
468 }
469
470 int ZymoticLerpBones(int count, const zymbonematrix *bonebase, const frameblend_t *blend, const zymbone_t *bone)
471 {
472         int i;
473         float lerp1, lerp2, lerp3, lerp4;
474         zymbonematrix *out, rootmatrix, m;
475         const zymbonematrix *bone1, *bone2, *bone3, *bone4;
476
477         /*
478         // LordHavoc: combine transform from zym coordinate space to quake coordinate space with model to world transform matrix
479         rootmatrix.m[0][0] = softwaretransform_matrix[0][1];
480         rootmatrix.m[0][1] = -softwaretransform_matrix[0][0];
481         rootmatrix.m[0][2] = softwaretransform_matrix[0][2];
482         rootmatrix.m[0][3] = softwaretransform_matrix[0][3];
483         rootmatrix.m[1][0] = softwaretransform_matrix[1][1];
484         rootmatrix.m[1][1] = -softwaretransform_matrix[1][0];
485         rootmatrix.m[1][2] = softwaretransform_matrix[1][2];
486         rootmatrix.m[1][3] = softwaretransform_matrix[1][3];
487         rootmatrix.m[2][0] = softwaretransform_matrix[2][1];
488         rootmatrix.m[2][1] = -softwaretransform_matrix[2][0];
489         rootmatrix.m[2][2] = softwaretransform_matrix[2][2];
490         rootmatrix.m[2][3] = softwaretransform_matrix[2][3];
491         */
492         rootmatrix.m[0][0] = 1;
493         rootmatrix.m[0][1] = 0;
494         rootmatrix.m[0][2] = 0;
495         rootmatrix.m[0][3] = 0;
496         rootmatrix.m[1][0] = 0;
497         rootmatrix.m[1][1] = 1;
498         rootmatrix.m[1][2] = 0;
499         rootmatrix.m[1][3] = 0;
500         rootmatrix.m[2][0] = 0;
501         rootmatrix.m[2][1] = 0;
502         rootmatrix.m[2][2] = 1;
503         rootmatrix.m[2][3] = 0;
504
505         bone1 = bonebase + blend[0].frame * count;
506         lerp1 = blend[0].lerp;
507         if (blend[1].lerp)
508         {
509                 bone2 = bonebase + blend[1].frame * count;
510                 lerp2 = blend[1].lerp;
511                 if (blend[2].lerp)
512                 {
513                         bone3 = bonebase + blend[2].frame * count;
514                         lerp3 = blend[2].lerp;
515                         if (blend[3].lerp)
516                         {
517                                 // 4 poses
518                                 bone4 = bonebase + blend[3].frame * count;
519                                 lerp4 = blend[3].lerp;
520                                 for (i = 0, out = zymbonepose;i < count;i++, out++)
521                                 {
522                                         // interpolate matrices
523                                         m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3 + bone4->m[0][0] * lerp4;
524                                         m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3 + bone4->m[0][1] * lerp4;
525                                         m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3 + bone4->m[0][2] * lerp4;
526                                         m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3 + bone4->m[0][3] * lerp4;
527                                         m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3 + bone4->m[1][0] * lerp4;
528                                         m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3 + bone4->m[1][1] * lerp4;
529                                         m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3 + bone4->m[1][2] * lerp4;
530                                         m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3 + bone4->m[1][3] * lerp4;
531                                         m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3 + bone4->m[2][0] * lerp4;
532                                         m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3 + bone4->m[2][1] * lerp4;
533                                         m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3 + bone4->m[2][2] * lerp4;
534                                         m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3 + bone4->m[2][3] * lerp4;
535                                         if (bone->parent >= 0)
536                                                 R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
537                                         else
538                                                 R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
539                                         bone1++;
540                                         bone2++;
541                                         bone3++;
542                                         bone4++;
543                                         bone++;
544                                 }
545                         }
546                         else
547                         {
548                                 // 3 poses
549                                 for (i = 0, out = zymbonepose;i < count;i++, out++)
550                                 {
551                                         // interpolate matrices
552                                         m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3;
553                                         m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3;
554                                         m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3;
555                                         m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3;
556                                         m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3;
557                                         m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3;
558                                         m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3;
559                                         m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3;
560                                         m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3;
561                                         m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3;
562                                         m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3;
563                                         m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3;
564                                         if (bone->parent >= 0)
565                                                 R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
566                                         else
567                                                 R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
568                                         bone1++;
569                                         bone2++;
570                                         bone3++;
571                                         bone++;
572                                 }
573                         }
574                 }
575                 else
576                 {
577                         // 2 poses
578                         for (i = 0, out = zymbonepose;i < count;i++, out++)
579                         {
580                                 // interpolate matrices
581                                 m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2;
582                                 m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2;
583                                 m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2;
584                                 m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2;
585                                 m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2;
586                                 m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2;
587                                 m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2;
588                                 m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2;
589                                 m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2;
590                                 m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2;
591                                 m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2;
592                                 m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2;
593                                 if (bone->parent >= 0)
594                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
595                                 else
596                                         R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
597                                 bone1++;
598                                 bone2++;
599                                 bone++;
600                         }
601                 }
602         }
603         else
604         {
605                 // 1 pose
606                 if (lerp1 != 1)
607                 {
608                         // lerp != 1.0
609                         for (i = 0, out = zymbonepose;i < count;i++, out++)
610                         {
611                                 // interpolate matrices
612                                 m.m[0][0] = bone1->m[0][0] * lerp1;
613                                 m.m[0][1] = bone1->m[0][1] * lerp1;
614                                 m.m[0][2] = bone1->m[0][2] * lerp1;
615                                 m.m[0][3] = bone1->m[0][3] * lerp1;
616                                 m.m[1][0] = bone1->m[1][0] * lerp1;
617                                 m.m[1][1] = bone1->m[1][1] * lerp1;
618                                 m.m[1][2] = bone1->m[1][2] * lerp1;
619                                 m.m[1][3] = bone1->m[1][3] * lerp1;
620                                 m.m[2][0] = bone1->m[2][0] * lerp1;
621                                 m.m[2][1] = bone1->m[2][1] * lerp1;
622                                 m.m[2][2] = bone1->m[2][2] * lerp1;
623                                 m.m[2][3] = bone1->m[2][3] * lerp1;
624                                 if (bone->parent >= 0)
625                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &m.m[0][0], &out->m[0][0]);
626                                 else
627                                         R_ConcatTransforms(&rootmatrix.m[0][0], &m.m[0][0], &out->m[0][0]);
628                                 bone1++;
629                                 bone++;
630                         }
631                 }
632                 else
633                 {
634                         // lerp == 1.0
635                         for (i = 0, out = zymbonepose;i < count;i++, out++)
636                         {
637                                 if (bone->parent >= 0)
638                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0][0], &bone1->m[0][0], &out->m[0][0]);
639                                 else
640                                         R_ConcatTransforms(&rootmatrix.m[0][0], &bone1->m[0][0], &out->m[0][0]);
641                                 bone1++;
642                                 bone++;
643                         }
644                 }
645         }
646         return true;
647 }
648
649 void ZymoticTransformVerts(int vertcount, int *bonecounts, zymvertex_t *vert)
650 {
651         int c;
652         float *out = aliasvert;
653         zymbonematrix *matrix;
654         while(vertcount--)
655         {
656                 c = *bonecounts++;
657                 // FIXME: validate bonecounts at load time (must be >= 1)
658                 // FIXME: need 4th component in origin, for how much of the translate to blend in
659                 if (c == 1)
660                 {
661                         matrix = &zymbonepose[vert->bonenum];
662                         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];
663                         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];
664                         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];
665                         vert++;
666                 }
667                 else
668                 {
669                         VectorClear(out);
670                         while(c--)
671                         {
672                                 matrix = &zymbonepose[vert->bonenum];
673                                 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];
674                                 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];
675                                 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];
676                                 vert++;
677                         }
678                 }
679                 out += 4;
680         }
681 }
682
683 void ZymoticCalcNormals(int vertcount, int shadercount, int *renderlist)
684 {
685         int a, b, c, d;
686         float *out, v1[3], v2[3], normal[3], s;
687         int *u;
688         // clear normals
689         memset(aliasvertnorm, 0, sizeof(float) * vertcount * 3);
690         memset(aliasvertusage, 0, sizeof(int) * vertcount);
691         // parse render list and accumulate surface normals
692         while(shadercount--)
693         {
694                 d = *renderlist++;
695                 while (d--)
696                 {
697                         a = renderlist[0]*4;
698                         b = renderlist[1]*4;
699                         c = renderlist[2]*4;
700                         v1[0] = aliasvert[a+0] - aliasvert[b+0];
701                         v1[1] = aliasvert[a+1] - aliasvert[b+1];
702                         v1[2] = aliasvert[a+2] - aliasvert[b+2];
703                         v2[0] = aliasvert[c+0] - aliasvert[b+0];
704                         v2[1] = aliasvert[c+1] - aliasvert[b+1];
705                         v2[2] = aliasvert[c+2] - aliasvert[b+2];
706                         CrossProduct(v1, v2, normal);
707                         VectorNormalizeFast(normal);
708                         // add surface normal to vertices
709                         a = renderlist[0] * 3;
710                         aliasvertnorm[a+0] += normal[0];
711                         aliasvertnorm[a+1] += normal[1];
712                         aliasvertnorm[a+2] += normal[2];
713                         aliasvertusage[renderlist[0]]++;
714                         a = renderlist[1] * 3;
715                         aliasvertnorm[a+0] += normal[0];
716                         aliasvertnorm[a+1] += normal[1];
717                         aliasvertnorm[a+2] += normal[2];
718                         aliasvertusage[renderlist[1]]++;
719                         a = renderlist[2] * 3;
720                         aliasvertnorm[a+0] += normal[0];
721                         aliasvertnorm[a+1] += normal[1];
722                         aliasvertnorm[a+2] += normal[2];
723                         aliasvertusage[renderlist[2]]++;
724                         renderlist += 3;
725                 }
726         }
727         // FIXME: precalc this
728         // average surface normals
729         out = aliasvertnorm;
730         u = aliasvertusage;
731         while(vertcount--)
732         {
733                 if (*u > 1)
734                 {
735                         s = ixtable[*u];
736                         out[0] *= s;
737                         out[1] *= s;
738                         out[2] *= s;
739                 }
740                 u++;
741                 out += 3;
742         }
743 }
744
745 void R_DrawZymoticModelMeshCallback (const void *calldata1, int calldata2)
746 {
747         float fog;
748         vec3_t diff;
749         int i, *renderlist;
750         zymtype1header_t *m;
751         rtexture_t *texture;
752         rmeshstate_t mstate;
753         const entity_render_t *ent = calldata1;
754         int shadernum = calldata2;
755         int numverts, numtriangles;
756
757         R_Mesh_Matrix(&ent->matrix);
758
759         // find the vertex index list and texture
760         m = ent->model->zymdata_header;
761         renderlist = (int *)(m->lump_render.start + (int) m);
762         for (i = 0;i < shadernum;i++)
763                 renderlist += renderlist[0] * 3 + 1;
764         texture = ((rtexture_t **)(m->lump_shaders.start + (int) m))[shadernum];
765
766         numverts = m->numverts;
767         numtriangles = renderlist[0];
768         R_Mesh_ResizeCheck(numverts, numtriangles);
769
770         fog = 0;
771         if (fogenabled)
772         {
773                 VectorSubtract(ent->origin, r_origin, diff);
774                 fog = DotProduct(diff,diff);
775                 if (fog < 0.01f)
776                         fog = 0.01f;
777                 fog = exp(fogdensity/fog);
778                 if (fog > 1)
779                         fog = 1;
780                 if (fog < 0.01f)
781                         fog = 0;
782                 // fog method: darken, additive fog
783                 // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
784                 // 2. render fog as additive
785         }
786
787         ZymoticLerpBones(m->numbones, (zymbonematrix *)(m->lump_poses.start + (int) m), ent->frameblend, (zymbone_t *)(m->lump_bones.start + (int) m));
788         ZymoticTransformVerts(numverts, (int *)(m->lump_vertbonecounts.start + (int) m), (zymvertex_t *)(m->lump_verts.start + (int) m));
789         ZymoticCalcNormals(numverts, m->numshaders, (int *)(m->lump_render.start + (int) m));
790
791         R_LightModel(ent, numverts, 1 - fog, 1 - fog, 1 - fog, false);
792
793         memset(&mstate, 0, sizeof(mstate));
794         mstate.wantoverbright = true;
795         if (ent->effects & EF_ADDITIVE)
796         {
797                 mstate.blendfunc1 = GL_SRC_ALPHA;
798                 mstate.blendfunc2 = GL_ONE;
799         }
800         else if (ent->alpha != 1.0 || R_TextureHasAlpha(texture))
801         {
802                 mstate.blendfunc1 = GL_SRC_ALPHA;
803                 mstate.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
804         }
805         else
806         {
807                 mstate.blendfunc1 = GL_ONE;
808                 mstate.blendfunc2 = GL_ZERO;
809         }
810         mstate.tex[0] = R_GetTexture(texture);
811         R_Mesh_State(&mstate);
812
813         c_alias_polys += numtriangles;
814         memcpy(varray_element, renderlist + 1, numtriangles * sizeof(int[3]));
815         memcpy(varray_vertex, aliasvert, numverts * sizeof(float[4]));
816         R_ModulateColors(aliasvertcolor, varray_color, numverts, mesh_colorscale, mesh_colorscale, mesh_colorscale);
817         memcpy(varray_texcoord[0], (float *)(m->lump_texcoords.start + (int) m), numverts * sizeof(float[2]));
818         R_Mesh_Draw(numverts, numtriangles);
819
820         if (fog)
821         {
822                 memset(&mstate, 0, sizeof(mstate));
823                 mstate.wantoverbright = false;
824                 mstate.blendfunc1 = GL_SRC_ALPHA;
825                 mstate.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
826                 // FIXME: need alpha mask for fogging...
827                 //mstate.tex[0] = R_GetTexture(texture);
828                 R_Mesh_State(&mstate);
829
830                 c_alias_polys += numtriangles;
831                 memcpy(varray_element, renderlist + 1, numtriangles * sizeof(int[3]));
832                 memcpy(varray_vertex, aliasvert, numverts * sizeof(float[4]));
833                 R_FillColors(varray_color, numverts, fogcolor[0] * mesh_colorscale, fogcolor[1] * mesh_colorscale, fogcolor[2] * mesh_colorscale, ent->alpha * fog);
834                 //memcpy(mesh_texcoord[0], (float *)(m->lump_texcoords.start + (int) m), numverts * sizeof(float[2]));
835                 R_Mesh_Draw(numverts, numtriangles);
836         }
837 }
838
839 void R_DrawZymoticModel (entity_render_t *ent)
840 {
841         int i;
842         zymtype1header_t *m;
843         rtexture_t *texture;
844
845         if (ent->alpha < (1.0f / 64.0f))
846                 return; // basically completely transparent
847
848         c_models++;
849
850         m = ent->model->zymdata_header;
851         for (i = 0;i < m->numshaders;i++)
852         {
853                 texture = ((rtexture_t **)(m->lump_shaders.start + (int) m))[i];
854                 if (ent->effects & EF_ADDITIVE || ent->alpha != 1.0 || R_TextureHasAlpha(texture))
855                         R_MeshQueue_AddTransparent(ent->origin, R_DrawZymoticModelMeshCallback, ent, i);
856                 else
857                         R_DrawZymoticModelMeshCallback(ent, i);
858         }
859 }
860
861 void R_DrawQ1Q2AliasModel(entity_render_t *ent)
862 {
863         if (ent->alpha < (1.0f / 64.0f))
864                 return; // basically completely transparent
865
866         c_models++;
867
868         if (ent->effects & EF_ADDITIVE || ent->alpha != 1.0 || R_FetchSkinFrame(ent)->fog != NULL)
869                 R_MeshQueue_AddTransparent(ent->origin, R_DrawQ1Q2AliasModelCallback, ent, 0);
870         else
871                 R_DrawQ1Q2AliasModelCallback(ent, 0);
872 }
873