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