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