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