rewrote memory system entirely (hunk, cache, and zone are gone, memory pools replaced...
[xonotic/darkplaces.git] / gl_models.c
1
2 #include "quakedef.h"
3
4 //cvar_t gl_transform = {0, "gl_transform", "1"};
5 cvar_t gl_lockarrays = {0, "gl_lockarrays", "1"};
6
7 typedef struct
8 {
9         float m[3][4];
10 } zymbonematrix;
11
12 // LordHavoc: vertex array
13 float *aliasvert;
14 float *aliasvertnorm;
15 float *aliasvertcolor;
16 float *aliasvertcolor2;
17 zymbonematrix *zymbonepose;
18 int *aliasvertusage;
19
20 rmeshinfo_t aliasmeshinfo;
21
22 rtexture_t *chrometexture;
23
24 int arraylocked = false;
25 void GL_LockArray(int first, int count)
26 {
27         if (gl_supportslockarrays && gl_lockarrays.integer)
28         {
29                 qglLockArraysEXT(first, count);
30                 arraylocked = true;
31         }
32 }
33
34 void GL_UnlockArray(void)
35 {
36         if (arraylocked)
37         {
38                 qglUnlockArraysEXT();
39                 arraylocked = false;
40         }
41 }
42
43 /*
44 void GL_SetupModelTransform (vec3_t origin, vec3_t angles, vec_t scale)
45 {
46     glTranslatef (origin[0], origin[1], origin[2]);
47
48         if (scale != 1)
49                 glScalef (scale, scale, scale);
50         if (angles[1])
51             glRotatef (angles[1],  0, 0, 1);
52         if (angles[0])
53             glRotatef (-angles[0],  0, 1, 0);
54         if (angles[2])
55             glRotatef (angles[2],  1, 0, 0);
56 }
57 */
58
59 rtexturepool_t *chrometexturepool;
60
61 // currently unused reflection effect texture
62 void makechrometexture(void)
63 {
64         int i;
65         byte noise[64*64];
66         byte data[64*64][4];
67
68         fractalnoise(noise, 64, 8);
69
70         // convert to RGBA data
71         for (i = 0;i < 64*64;i++)
72         {
73                 data[i][0] = data[i][1] = data[i][2] = noise[i];
74                 data[i][3] = 255;
75         }
76
77         chrometexture = R_LoadTexture (chrometexturepool, "chrometexture", 64, 64, &data[0][0], TEXTYPE_RGBA, TEXF_MIPMAP | TEXF_PRECACHE);
78 }
79
80 mempool_t *gl_models_mempool;
81
82 void gl_models_start(void)
83 {
84         // allocate vertex processing arrays
85         gl_models_mempool = Mem_AllocPool("GL_Models");
86         aliasvert = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][3]));
87         aliasvertnorm = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][3]));
88         aliasvertcolor = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4]));
89         aliasvertcolor2 = Mem_Alloc(gl_models_mempool, sizeof(float[MD2MAX_VERTS][4])); // used temporarily for tinted coloring
90         zymbonepose = Mem_Alloc(gl_models_mempool, sizeof(zymbonematrix[256]));
91         aliasvertusage = Mem_Alloc(gl_models_mempool, sizeof(int[MD2MAX_VERTS]));
92         chrometexturepool = R_AllocTexturePool();
93         makechrometexture();
94 }
95
96 void gl_models_shutdown(void)
97 {
98         R_FreeTexturePool(&chrometexturepool);
99         Mem_FreePool(&gl_models_mempool);
100 }
101
102 void gl_models_newmap(void)
103 {
104 }
105
106 void GL_Models_Init(void)
107 {
108 //      Cvar_RegisterVariable(&gl_transform);
109         Cvar_RegisterVariable(&gl_lockarrays);
110
111         R_RegisterModule("GL_Models", gl_models_start, gl_models_shutdown, gl_models_newmap);
112 }
113
114 void R_AliasTransformVerts(int vertcount)
115 {
116         vec3_t point;
117         float *av, *avn;
118         av = aliasvert;
119         avn = aliasvertnorm;
120         while (vertcount >= 4)
121         {
122                 VectorCopy(av, point);softwaretransform(point, av);av += 3;
123                 VectorCopy(av, point);softwaretransform(point, av);av += 3;
124                 VectorCopy(av, point);softwaretransform(point, av);av += 3;
125                 VectorCopy(av, point);softwaretransform(point, av);av += 3;
126                 VectorCopy(avn, point);softwaretransformdirection(point, avn);avn += 3;
127                 VectorCopy(avn, point);softwaretransformdirection(point, avn);avn += 3;
128                 VectorCopy(avn, point);softwaretransformdirection(point, avn);avn += 3;
129                 VectorCopy(avn, point);softwaretransformdirection(point, avn);avn += 3;
130                 vertcount -= 4;
131         }
132         while(vertcount > 0)
133         {
134                 VectorCopy(av, point);softwaretransform(point, av);av += 3;
135                 VectorCopy(avn, point);softwaretransformdirection(point, avn);avn += 3;
136                 vertcount--;
137         }
138 }
139
140 void R_AliasLerpVerts(int vertcount,
141                                           float lerp1, trivertx_t *verts1, vec3_t fscale1, vec3_t translate1,
142                                           float lerp2, trivertx_t *verts2, vec3_t fscale2, vec3_t translate2,
143                                           float lerp3, trivertx_t *verts3, vec3_t fscale3, vec3_t translate3,
144                                           float lerp4, trivertx_t *verts4, vec3_t fscale4, vec3_t translate4)
145 {
146         int i;
147         vec3_t scale1, scale2, scale3, scale4, translate;
148         float *n1, *n2, *n3, *n4;
149         float *av, *avn;
150         av = aliasvert;
151         avn = aliasvertnorm;
152         VectorScale(fscale1, lerp1, scale1);
153         if (lerp2)
154         {
155                 VectorScale(fscale2, lerp2, scale2);
156                 if (lerp3)
157                 {
158                         VectorScale(fscale3, lerp3, scale3);
159                         if (lerp4)
160                         {
161                                 VectorScale(fscale4, lerp4, scale4);
162                                 translate[0] = translate1[0] * lerp1 + translate2[0] * lerp2 + translate3[0] * lerp3 + translate4[0] * lerp4;
163                                 translate[1] = translate1[1] * lerp1 + translate2[1] * lerp2 + translate3[1] * lerp3 + translate4[1] * lerp4;
164                                 translate[2] = translate1[2] * lerp1 + translate2[2] * lerp2 + translate3[2] * lerp3 + translate4[2] * lerp4;
165                                 // generate vertices
166                                 for (i = 0;i < vertcount;i++)
167                                 {
168                                         av[0] = verts1->v[0] * scale1[0] + verts2->v[0] * scale2[0] + verts3->v[0] * scale3[0] + verts4->v[0] * scale4[0] + translate[0];
169                                         av[1] = verts1->v[1] * scale1[1] + verts2->v[1] * scale2[1] + verts3->v[1] * scale3[1] + verts4->v[1] * scale4[1] + translate[1];
170                                         av[2] = verts1->v[2] * scale1[2] + verts2->v[2] * scale2[2] + verts3->v[2] * scale3[2] + verts4->v[2] * scale4[2] + translate[2];
171                                         n1 = m_bytenormals[verts1->lightnormalindex];
172                                         n2 = m_bytenormals[verts2->lightnormalindex];
173                                         n3 = m_bytenormals[verts3->lightnormalindex];
174                                         n4 = m_bytenormals[verts4->lightnormalindex];
175                                         avn[0] = n1[0] * lerp1 + n2[0] * lerp2 + n3[0] * lerp3 + n4[0] * lerp4;
176                                         avn[1] = n1[1] * lerp1 + n2[1] * lerp2 + n3[1] * lerp3 + n4[1] * lerp4;
177                                         avn[2] = n1[2] * lerp1 + n2[2] * lerp2 + n3[2] * lerp3 + n4[2] * lerp4;
178                                         av += 3;
179                                         avn += 3;
180                                         verts1++;verts2++;verts3++;verts4++;
181                                 }
182                         }
183                         else
184                         {
185                                 translate[0] = translate1[0] * lerp1 + translate2[0] * lerp2 + translate3[0] * lerp3;
186                                 translate[1] = translate1[1] * lerp1 + translate2[1] * lerp2 + translate3[1] * lerp3;
187                                 translate[2] = translate1[2] * lerp1 + translate2[2] * lerp2 + translate3[2] * lerp3;
188                                 // generate vertices
189                                 for (i = 0;i < vertcount;i++)
190                                 {
191                                         av[0] = verts1->v[0] * scale1[0] + verts2->v[0] * scale2[0] + verts3->v[0] * scale3[0] + translate[0];
192                                         av[1] = verts1->v[1] * scale1[1] + verts2->v[1] * scale2[1] + verts3->v[1] * scale3[1] + translate[1];
193                                         av[2] = verts1->v[2] * scale1[2] + verts2->v[2] * scale2[2] + verts3->v[2] * scale3[2] + translate[2];
194                                         n1 = m_bytenormals[verts1->lightnormalindex];
195                                         n2 = m_bytenormals[verts2->lightnormalindex];
196                                         n3 = m_bytenormals[verts3->lightnormalindex];
197                                         avn[0] = n1[0] * lerp1 + n2[0] * lerp2 + n3[0] * lerp3;
198                                         avn[1] = n1[1] * lerp1 + n2[1] * lerp2 + n3[1] * lerp3;
199                                         avn[2] = n1[2] * lerp1 + n2[2] * lerp2 + n3[2] * lerp3;
200                                         av += 3;
201                                         avn += 3;
202                                         verts1++;verts2++;verts3++;
203                                 }
204                         }
205                 }
206                 else
207                 {
208                         translate[0] = translate1[0] * lerp1 + translate2[0] * lerp2;
209                         translate[1] = translate1[1] * lerp1 + translate2[1] * lerp2;
210                         translate[2] = translate1[2] * lerp1 + translate2[2] * lerp2;
211                         // generate vertices
212                         for (i = 0;i < vertcount;i++)
213                         {
214                                 av[0] = verts1->v[0] * scale1[0] + verts2->v[0] * scale2[0] + translate[0];
215                                 av[1] = verts1->v[1] * scale1[1] + verts2->v[1] * scale2[1] + translate[1];
216                                 av[2] = verts1->v[2] * scale1[2] + verts2->v[2] * scale2[2] + translate[2];
217                                 n1 = m_bytenormals[verts1->lightnormalindex];
218                                 n2 = m_bytenormals[verts2->lightnormalindex];
219                                 avn[0] = n1[0] * lerp1 + n2[0] * lerp2;
220                                 avn[1] = n1[1] * lerp1 + n2[1] * lerp2;
221                                 avn[2] = n1[2] * lerp1 + n2[2] * lerp2;
222                                 av += 3;
223                                 avn += 3;
224                                 verts1++;verts2++;
225                         }
226                 }
227         }
228         else
229         {
230                 translate[0] = translate1[0] * lerp1;
231                 translate[1] = translate1[1] * lerp1;
232                 translate[2] = translate1[2] * lerp1;
233                 // generate vertices
234                 if (lerp1 != 1)
235                 {
236                         // general but almost never used case
237                         for (i = 0;i < vertcount;i++)
238                         {
239                                 av[0] = verts1->v[0] * scale1[0] + translate[0];
240                                 av[1] = verts1->v[1] * scale1[1] + translate[1];
241                                 av[2] = verts1->v[2] * scale1[2] + translate[2];
242                                 n1 = m_bytenormals[verts1->lightnormalindex];
243                                 avn[0] = n1[0] * lerp1;
244                                 avn[1] = n1[1] * lerp1;
245                                 avn[2] = n1[2] * lerp1;
246                                 av += 3;
247                                 avn += 3;
248                                 verts1++;
249                         }
250                 }
251                 else
252                 {
253                         // fast normal case
254                         for (i = 0;i < vertcount;i++)
255                         {
256                                 av[0] = verts1->v[0] * scale1[0] + translate[0];
257                                 av[1] = verts1->v[1] * scale1[1] + translate[1];
258                                 av[2] = verts1->v[2] * scale1[2] + translate[2];
259                                 VectorCopy(m_bytenormals[verts1->lightnormalindex], avn);
260                                 av += 3;
261                                 avn += 3;
262                                 verts1++;
263                         }
264                 }
265         }
266 }
267
268 void GL_DrawModelMesh(rtexture_t *skin, float *colors, float cred, float cgreen, float cblue)
269 {
270         aliasmeshinfo.tex[0] = R_GetTexture(skin);
271         aliasmeshinfo.color = colors;
272         if (colors == NULL)
273         {
274                 aliasmeshinfo.cr = cred;
275                 aliasmeshinfo.cg = cgreen;
276                 aliasmeshinfo.cb = cblue;
277                 aliasmeshinfo.ca = currentrenderentity->alpha;
278         }
279
280         R_Mesh_Draw(&aliasmeshinfo);
281
282         // leave it in a state for additional passes
283         aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
284         aliasmeshinfo.blendfunc2 = GL_ONE;
285 }
286
287 void R_TintModel(float *in, float *out, int verts, float r, float g, float b)
288 {
289         int i;
290         for (i = 0;i < verts;i++)
291         {
292                 out[0] = in[0] * r;
293                 out[1] = in[1] * g;
294                 out[2] = in[2] * b;
295                 out[3] = in[3];
296                 in += 4;
297                 out += 4;
298         }
299 }
300
301 void R_SetupMDLMD2Frames(skinframe_t **skinframe)
302 {
303         md2frame_t *frame1, *frame2, *frame3, *frame4;
304         trivertx_t *frame1verts, *frame2verts, *frame3verts, *frame4verts;
305         model_t *model;
306         model = currentrenderentity->model;
307
308         if (model->skinscenes[currentrenderentity->skinnum].framecount > 1)
309                 *skinframe = &model->skinframes[model->skinscenes[currentrenderentity->skinnum].firstframe + (int) (cl.time * 10) % model->skinscenes[currentrenderentity->skinnum].framecount];
310         else
311                 *skinframe = &model->skinframes[model->skinscenes[currentrenderentity->skinnum].firstframe];
312
313         softwaretransformforentity(currentrenderentity);
314
315         frame1 = &model->mdlmd2data_frames[currentrenderentity->frameblend[0].frame];
316         frame2 = &model->mdlmd2data_frames[currentrenderentity->frameblend[1].frame];
317         frame3 = &model->mdlmd2data_frames[currentrenderentity->frameblend[2].frame];
318         frame4 = &model->mdlmd2data_frames[currentrenderentity->frameblend[3].frame];
319         frame1verts = &model->mdlmd2data_pose[currentrenderentity->frameblend[0].frame * model->numverts];
320         frame2verts = &model->mdlmd2data_pose[currentrenderentity->frameblend[1].frame * model->numverts];
321         frame3verts = &model->mdlmd2data_pose[currentrenderentity->frameblend[2].frame * model->numverts];
322         frame4verts = &model->mdlmd2data_pose[currentrenderentity->frameblend[3].frame * model->numverts];
323         /*
324         if (currentrenderentity->frameblend[0].lerp)
325                 Con_Printf("frame1: %i/%i %s scale %f %f %f translate %f %f %f\n", currentrenderentity->frameblend[0].frame, model->numframes, frame1->name, frame1->scale[0], frame1->scale[1], frame1->scale[2], frame1->translate[0], frame1->translate[1], frame1->translate[2]);
326         if (currentrenderentity->frameblend[1].lerp)
327                 Con_Printf("frame2: %i/%i %s scale %f %f %f translate %f %f %f\n", currentrenderentity->frameblend[0].frame, model->numframes, frame2->name, frame2->scale[0], frame2->scale[1], frame2->scale[2], frame2->translate[0], frame2->translate[1], frame2->translate[2]);
328         if (currentrenderentity->frameblend[2].lerp)
329                 Con_Printf("frame3: %i/%i %s scale %f %f %f translate %f %f %f\n", currentrenderentity->frameblend[0].frame, model->numframes, frame3->name, frame3->scale[0], frame3->scale[1], frame3->scale[2], frame3->translate[0], frame3->translate[1], frame3->translate[2]);
330         if (currentrenderentity->frameblend[3].lerp)
331                 Con_Printf("frame4: %i/%i %s scale %f %f %f translate %f %f %f\n", currentrenderentity->frameblend[0].frame, model->numframes, frame4->name, frame4->scale[0], frame4->scale[1], frame4->scale[2], frame4->translate[0], frame4->translate[1], frame4->translate[2]);
332         */
333         R_AliasLerpVerts(model->numverts,
334                 currentrenderentity->frameblend[0].lerp, frame1verts, frame1->scale, frame1->translate,
335                 currentrenderentity->frameblend[1].lerp, frame2verts, frame2->scale, frame2->translate,
336                 currentrenderentity->frameblend[2].lerp, frame3verts, frame3->scale, frame3->translate,
337                 currentrenderentity->frameblend[3].lerp, frame4verts, frame4->scale, frame4->translate);
338         R_AliasTransformVerts(model->numverts);
339
340         R_LightModel(model->numverts);
341 }
342
343 void R_DrawQ1AliasModel (void)
344 {
345         float fog;
346         vec3_t diff;
347         model_t *model;
348         skinframe_t *skinframe;
349
350         model = currentrenderentity->model;
351
352         R_SetupMDLMD2Frames(&skinframe);
353
354         memset(&aliasmeshinfo, 0, sizeof(aliasmeshinfo));
355
356         aliasmeshinfo.vertex = aliasvert;
357         aliasmeshinfo.vertexstep = sizeof(float[3]);
358         aliasmeshinfo.numverts = model->numverts;
359         aliasmeshinfo.numtriangles = model->numtris;
360         aliasmeshinfo.index = model->mdldata_indices;
361         aliasmeshinfo.colorstep = sizeof(float[4]);
362         aliasmeshinfo.texcoords[0] = model->mdldata_texcoords;
363         aliasmeshinfo.texcoordstep[0] = sizeof(float[2]);
364
365         fog = 0;
366         if (fogenabled)
367         {
368                 VectorSubtract(currentrenderentity->origin, r_origin, diff);
369                 fog = exp(fogdensity/DotProduct(diff,diff));
370                 if (fog > 1)
371                         fog = 1;
372                 if (fog < 0.01f)
373                         fog = 0;
374                 // fog method: darken, additive fog
375                 // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
376                 // 2. render fog as additive
377         }
378
379         if (currentrenderentity->effects & EF_ADDITIVE)
380         {
381                 aliasmeshinfo.transparent = true;
382                 aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
383                 aliasmeshinfo.blendfunc2 = GL_ONE;
384         }
385         else if (currentrenderentity->alpha != 1.0 || skinframe->fog != NULL)
386         {
387                 aliasmeshinfo.transparent = true;
388                 aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
389                 aliasmeshinfo.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
390         }
391         else
392         {
393                 aliasmeshinfo.transparent = false;
394                 aliasmeshinfo.blendfunc1 = GL_ONE;
395                 aliasmeshinfo.blendfunc2 = GL_ZERO;
396         }
397
398         // darken source
399         if (fog)
400                 R_TintModel(aliasvertcolor, aliasvertcolor, model->numverts, 1 - fog, 1 - fog, 1 - fog);
401
402         if (skinframe->base || skinframe->pants || skinframe->shirt || skinframe->glow || skinframe->merged)
403         {
404                 if (currentrenderentity->colormap >= 0 && (skinframe->base || skinframe->pants || skinframe->shirt))
405                 {
406                         int c;
407                         byte *color;
408                         if (skinframe->base)
409                                 GL_DrawModelMesh(skinframe->base, aliasvertcolor, 0, 0, 0);
410                         if (skinframe->pants)
411                         {
412                                 c = (currentrenderentity->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
413                                 color = (byte *) (&d_8to24table[c]);
414                                 if (c >= 224) // fullbright ranges
415                                         GL_DrawModelMesh(skinframe->pants, NULL, color[0] * (1.0f / 255.0f), color[1] * (1.0f / 255.0f), color[2] * (1.0f / 255.0f));
416                                 else
417                                 {
418                                         R_TintModel(aliasvertcolor, aliasvertcolor2, model->numverts, color[0] * (1.0f / 255.0f), color[1] * (1.0f / 255.0f), color[2] * (1.0f / 255.0f));
419                                         GL_DrawModelMesh(skinframe->pants, aliasvertcolor2, 0, 0, 0);
420                                 }
421                         }
422                         if (skinframe->shirt)
423                         {
424                                 c = currentrenderentity->colormap & 0xF0      ;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
425                                 color = (byte *) (&d_8to24table[c]);
426                                 if (c >= 224) // fullbright ranges
427                                         GL_DrawModelMesh(skinframe->shirt, NULL, color[0] * (1.0f / 255.0f), color[1] * (1.0f / 255.0f), color[2] * (1.0f / 255.0f));
428                                 else
429                                 {
430                                         R_TintModel(aliasvertcolor, aliasvertcolor2, model->numverts, color[0] * (1.0f / 255.0f), color[1] * (1.0f / 255.0f), color[2] * (1.0f / 255.0f));
431                                         GL_DrawModelMesh(skinframe->shirt, aliasvertcolor2, 0, 0, 0);
432                                 }
433                         }
434                 }
435                 else
436                 {
437                         if (skinframe->merged)
438                                 GL_DrawModelMesh(skinframe->merged, aliasvertcolor, 0, 0, 0);
439                         else
440                         {
441                                 if (skinframe->base) GL_DrawModelMesh(skinframe->base, aliasvertcolor, 0, 0, 0);
442                                 if (skinframe->pants) GL_DrawModelMesh(skinframe->pants, aliasvertcolor, 0, 0, 0);
443                                 if (skinframe->shirt) GL_DrawModelMesh(skinframe->shirt, aliasvertcolor, 0, 0, 0);
444                         }
445                 }
446                 if (skinframe->glow) GL_DrawModelMesh(skinframe->glow, NULL, 1 - fog, 1 - fog, 1 - fog);
447         }
448         else
449                 GL_DrawModelMesh(0, NULL, 1 - fog, 1 - fog, 1 - fog);
450
451         if (fog)
452         {
453                 aliasmeshinfo.tex[0] = R_GetTexture(skinframe->fog);
454                 aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
455                 aliasmeshinfo.blendfunc2 = GL_ONE;
456                 aliasmeshinfo.color = NULL;
457
458                 aliasmeshinfo.cr = fogcolor[0];
459                 aliasmeshinfo.cg = fogcolor[1];
460                 aliasmeshinfo.cb = fogcolor[2];
461                 aliasmeshinfo.ca = currentrenderentity->alpha * fog;
462
463                 R_Mesh_Draw(&aliasmeshinfo);
464         }
465 }
466
467 void R_DrawQ2AliasModel (void)
468 {
469         int *order, count;
470         vec3_t diff;
471         skinframe_t *skinframe;
472         model_t *model;
473
474         model = currentrenderentity->model;
475
476         R_SetupMDLMD2Frames(&skinframe);
477
478         if (!r_render.integer)
479                 return;
480
481         // FIXME FIXME FIXME rewrite loader to convert to triangle mesh
482         glBindTexture(GL_TEXTURE_2D, R_GetTexture(skinframe->base));
483
484         if (currentrenderentity->effects & EF_ADDITIVE)
485         {
486                 glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive rendering
487                 glEnable(GL_BLEND);
488                 glDepthMask(0);
489         }
490         else if (currentrenderentity->alpha != 1.0 || R_TextureHasAlpha(skinframe->base))
491         {
492                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
493                 glEnable(GL_BLEND);
494                 glDepthMask(0);
495         }
496         else
497         {
498                 glDisable(GL_BLEND);
499                 glDepthMask(1);
500         }
501
502         // LordHavoc: big mess...
503         // using vertex arrays only slightly, although it is enough to prevent duplicates
504         // (saving half the transforms)
505         glVertexPointer(3, GL_FLOAT, sizeof(float[3]), aliasvert);
506         glColorPointer(4, GL_FLOAT, sizeof(float[4]), aliasvertcolor);
507         glEnableClientState(GL_VERTEX_ARRAY);
508         glEnableClientState(GL_COLOR_ARRAY);
509
510         GL_LockArray(0, model->numverts);
511
512         order = model->md2data_glcmds;
513         while(1)
514         {
515                 if (!(count = *order++))
516                         break;
517                 if (count > 0)
518                         glBegin(GL_TRIANGLE_STRIP);
519                 else
520                 {
521                         glBegin(GL_TRIANGLE_FAN);
522                         count = -count;
523                 }
524                 do
525                 {
526                         glTexCoord2f(((float *)order)[0], ((float *)order)[1]);
527                         glArrayElement(order[2]);
528                         order += 3;
529                 }
530                 while (count--);
531         }
532
533         GL_UnlockArray();
534
535         glDisableClientState(GL_COLOR_ARRAY);
536         glDisableClientState(GL_VERTEX_ARRAY);
537
538         if (fogenabled)
539         {
540                 glDisable (GL_TEXTURE_2D);
541                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
542                 glEnable (GL_BLEND);
543                 glDepthMask(0); // disable zbuffer updates
544
545                 VectorSubtract(currentrenderentity->origin, r_origin, diff);
546                 glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff)));
547
548                 // LordHavoc: big mess...
549                 // using vertex arrays only slightly, although it is enough to prevent duplicates
550                 // (saving half the transforms)
551                 glVertexPointer(3, GL_FLOAT, sizeof(float[3]), aliasvert);
552                 glEnableClientState(GL_VERTEX_ARRAY);
553
554                 GL_LockArray(0, model->numverts);
555
556                 order = model->md2data_glcmds;
557                 while(1)
558                 {
559                         if (!(count = *order++))
560                                 break;
561                         if (count > 0)
562                                 glBegin(GL_TRIANGLE_STRIP);
563                         else
564                         {
565                                 glBegin(GL_TRIANGLE_FAN);
566                                 count = -count;
567                         }
568                         do
569                         {
570                                 glArrayElement(order[2]);
571                                 order += 3;
572                         }
573                         while (count--);
574                 }
575
576                 GL_UnlockArray();
577
578                 glDisableClientState(GL_VERTEX_ARRAY);
579
580                 glEnable (GL_TEXTURE_2D);
581                 glColor3f (1,1,1);
582         }
583
584         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
585         glEnable (GL_BLEND);
586         glDepthMask(1);
587 }
588
589 void ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, zymbone_t *bone, float rootorigin[3], float rootangles[3], float rootscale)
590 {
591         float lerp1, lerp2, lerp3, lerp4;
592         zymbonematrix *out, rootmatrix, m, *bone1, *bone2, *bone3, *bone4;
593         out = zymbonepose;
594         AngleVectors(rootangles, rootmatrix.m[0], rootmatrix.m[1], rootmatrix.m[2]);
595         VectorScale(rootmatrix.m[0], rootscale, rootmatrix.m[0]);
596         VectorScale(rootmatrix.m[1], rootscale, rootmatrix.m[1]);
597         VectorScale(rootmatrix.m[2], rootscale, rootmatrix.m[2]);
598         rootmatrix.m[0][3] = rootorigin[0];
599         rootmatrix.m[1][3] = rootorigin[1];
600         rootmatrix.m[2][3] = rootorigin[2];
601         bone1 = bonebase + blend[0].frame * count;
602         lerp1 = blend[0].lerp;
603         if (blend[1].lerp)
604         {
605                 bone2 = bonebase + blend[1].frame * count;
606                 lerp2 = blend[1].lerp;
607                 if (blend[2].lerp)
608                 {
609                         bone3 = bonebase + blend[2].frame * count;
610                         lerp3 = blend[2].lerp;
611                         if (blend[3].lerp)
612                         {
613                                 // 4 poses
614                                 bone4 = bonebase + blend[3].frame * count;
615                                 lerp4 = blend[3].lerp;
616                                 while(count--)
617                                 {
618                                         // interpolate matrices
619                                         m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3 + bone4->m[0][0] * lerp4;
620                                         m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3 + bone4->m[0][1] * lerp4;
621                                         m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3 + bone4->m[0][2] * lerp4;
622                                         m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3 + bone4->m[0][3] * lerp4;
623                                         m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3 + bone4->m[1][0] * lerp4;
624                                         m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3 + bone4->m[1][1] * lerp4;
625                                         m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3 + bone4->m[1][2] * lerp4;
626                                         m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3 + bone4->m[1][3] * lerp4;
627                                         m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3 + bone4->m[2][0] * lerp4;
628                                         m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3 + bone4->m[2][1] * lerp4;
629                                         m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3 + bone4->m[2][2] * lerp4;
630                                         m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3 + bone4->m[2][3] * lerp4;
631                                         if (bone->parent >= 0)
632                                                 R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &m.m[0], &out->m[0]);
633                                         else
634                                                 R_ConcatTransforms(&rootmatrix.m[0], &m.m[0], &out->m[0]);
635                                         bone1++;
636                                         bone2++;
637                                         bone3++;
638                                         bone4++;
639                                         bone++;
640                                         out++;
641                                 }
642                         }
643                         else
644                         {
645                                 // 3 poses
646                                 while(count--)
647                                 {
648                                         // interpolate matrices
649                                         m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3;
650                                         m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3;
651                                         m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3;
652                                         m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3;
653                                         m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3;
654                                         m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3;
655                                         m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3;
656                                         m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3;
657                                         m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3;
658                                         m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3;
659                                         m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3;
660                                         m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3;
661                                         if (bone->parent >= 0)
662                                                 R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &m.m[0], &out->m[0]);
663                                         else
664                                                 R_ConcatTransforms(&rootmatrix.m[0], &m.m[0], &out->m[0]);
665                                         bone1++;
666                                         bone2++;
667                                         bone3++;
668                                         bone++;
669                                         out++;
670                                 }
671                         }
672                 }
673                 else
674                 {
675                         // 2 poses
676                         while(count--)
677                         {
678                                 // interpolate matrices
679                                 m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2;
680                                 m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2;
681                                 m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2;
682                                 m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2;
683                                 m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2;
684                                 m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2;
685                                 m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2;
686                                 m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2;
687                                 m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2;
688                                 m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2;
689                                 m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2;
690                                 m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2;
691                                 if (bone->parent >= 0)
692                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &m.m[0], &out->m[0]);
693                                 else
694                                         R_ConcatTransforms(&rootmatrix.m[0], &m.m[0], &out->m[0]);
695                                 bone1++;
696                                 bone2++;
697                                 bone++;
698                                 out++;
699                         }
700                 }
701         }
702         else
703         {
704                 // 1 pose
705                 if (lerp1 != 1)
706                 {
707                         // lerp != 1.0
708                         while(count--)
709                         {
710                                 // interpolate matrices
711                                 m.m[0][0] = bone1->m[0][0] * lerp1;
712                                 m.m[0][1] = bone1->m[0][1] * lerp1;
713                                 m.m[0][2] = bone1->m[0][2] * lerp1;
714                                 m.m[0][3] = bone1->m[0][3] * lerp1;
715                                 m.m[1][0] = bone1->m[1][0] * lerp1;
716                                 m.m[1][1] = bone1->m[1][1] * lerp1;
717                                 m.m[1][2] = bone1->m[1][2] * lerp1;
718                                 m.m[1][3] = bone1->m[1][3] * lerp1;
719                                 m.m[2][0] = bone1->m[2][0] * lerp1;
720                                 m.m[2][1] = bone1->m[2][1] * lerp1;
721                                 m.m[2][2] = bone1->m[2][2] * lerp1;
722                                 m.m[2][3] = bone1->m[2][3] * lerp1;
723                                 if (bone->parent >= 0)
724                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &m.m[0], &out->m[0]);
725                                 else
726                                         R_ConcatTransforms(&rootmatrix.m[0], &m.m[0], &out->m[0]);
727                                 bone1++;
728                                 bone++;
729                                 out++;
730                         }
731                 }
732                 else
733                 {
734                         // lerp == 1.0
735                         while(count--)
736                         {
737                                 if (bone->parent >= 0)
738                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &bone1->m[0], &out->m[0]);
739                                 else
740                                         R_ConcatTransforms(&rootmatrix.m[0], &bone1->m[0], &out->m[0]);
741                                 bone1++;
742                                 bone++;
743                                 out++;
744                         }
745                 }
746         }
747 }
748
749 void ZymoticTransformVerts(int vertcount, int *bonecounts, zymvertex_t *vert)
750 {
751         int c;
752         float *out = aliasvert;
753         zymbonematrix *matrix;
754         while(vertcount--)
755         {
756                 c = *bonecounts++;
757                 // FIXME: validate bonecounts at load time (must be >= 1)
758                 if (c == 1)
759                 {
760                         matrix = &zymbonepose[vert->bonenum];
761                         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];
762                         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];
763                         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];
764                         vert++;
765                 }
766                 else
767                 {
768                         VectorClear(out);
769                         while(c--)
770                         {
771                                 matrix = &zymbonepose[vert->bonenum];
772                                 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];
773                                 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];
774                                 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];
775                                 vert++;
776                         }
777                 }
778                 out += 3;
779         }
780 }
781
782 void ZymoticCalcNormals(int vertcount, int shadercount, int *renderlist)
783 {
784         int a, b, c, d;
785         float *out, v1[3], v2[3], normal[3];
786         int *u;
787         // clear normals
788         memset(aliasvertnorm, 0, sizeof(float[3]) * vertcount);
789         memset(aliasvertusage, 0, sizeof(int) * vertcount);
790         // parse render list and accumulate surface normals
791         while(shadercount--)
792         {
793                 d = *renderlist++;
794                 while (d--)
795                 {
796                         a = renderlist[0]*3;
797                         b = renderlist[1]*3;
798                         c = renderlist[2]*3;
799                         v1[0] = aliasvert[a+0] - aliasvert[b+0];
800                         v1[1] = aliasvert[a+1] - aliasvert[b+1];
801                         v1[2] = aliasvert[a+2] - aliasvert[b+2];
802                         v2[0] = aliasvert[c+0] - aliasvert[b+0];
803                         v2[1] = aliasvert[c+1] - aliasvert[b+1];
804                         v2[2] = aliasvert[c+2] - aliasvert[b+2];
805                         CrossProduct(v1, v2, normal);
806                         VectorNormalizeFast(normal);
807                         // add surface normal to vertices
808                         aliasvertnorm[a+0] += normal[0];
809                         aliasvertnorm[a+1] += normal[1];
810                         aliasvertnorm[a+2] += normal[2];
811                         aliasvertusage[a]++;
812                         aliasvertnorm[b+0] += normal[0];
813                         aliasvertnorm[b+1] += normal[1];
814                         aliasvertnorm[b+2] += normal[2];
815                         aliasvertusage[b]++;
816                         aliasvertnorm[c+0] += normal[0];
817                         aliasvertnorm[c+1] += normal[1];
818                         aliasvertnorm[c+2] += normal[2];
819                         aliasvertusage[c]++;
820                         renderlist += 3;
821                 }
822         }
823         // average surface normals
824         out = aliasvertnorm;
825         u = aliasvertusage;
826         while(vertcount--)
827         {
828                 if (*u > 1)
829                 {
830                         a = ixtable[*u];
831                         out[0] *= a;
832                         out[1] *= a;
833                         out[2] *= a;
834                 }
835                 u++;
836                 out += 3;
837         }
838 }
839
840 void GL_DrawZymoticModelMesh(zymtype1header_t *m)
841 {
842         int i, *renderlist;
843         rtexture_t **texture;
844
845         // FIXME: do better fog
846         renderlist = (int *)(m->lump_render.start + (int) m);
847         texture = (rtexture_t **)(m->lump_shaders.start + (int) m);
848
849         aliasmeshinfo.vertex = aliasvert;
850         aliasmeshinfo.vertexstep = sizeof(float[3]);
851         aliasmeshinfo.color = aliasvertcolor;
852         aliasmeshinfo.colorstep = sizeof(float[4]);
853         aliasmeshinfo.texcoords[0] = (float *)(m->lump_texcoords.start + (int) m);
854         aliasmeshinfo.texcoordstep[0] = sizeof(float[2]);
855
856         for (i = 0;i < m->numshaders;i++)
857         {
858                 aliasmeshinfo.tex[0] = R_GetTexture(*texture);
859                 aliasmeshinfo.tex[1] = 0;
860                 if (currentrenderentity->effects & EF_ADDITIVE)
861                 {
862                         aliasmeshinfo.transparent = true;
863                         aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
864                         aliasmeshinfo.blendfunc2 = GL_ONE;
865                 }
866                 else if (currentrenderentity->alpha != 1.0 || R_TextureHasAlpha(*texture))
867                 {
868                         aliasmeshinfo.transparent = true;
869                         aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
870                         aliasmeshinfo.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
871                 }
872                 else
873                 {
874                         aliasmeshinfo.transparent = false;
875                         aliasmeshinfo.blendfunc1 = GL_ONE;
876                         aliasmeshinfo.blendfunc2 = GL_ZERO;
877                 }
878                 aliasmeshinfo.numtriangles = *renderlist++;
879                 aliasmeshinfo.index = renderlist;
880                 R_Mesh_Draw(&aliasmeshinfo);
881                 renderlist += aliasmeshinfo.numtriangles * 3;
882         }
883 }
884
885 void GL_DrawZymoticModelMeshFog(vec3_t org, zymtype1header_t *m)
886 {
887         int i, *renderlist;
888         vec3_t diff;
889
890         // FIXME: do better fog
891         renderlist = (int *)(m->lump_render.start + (int) m);
892
893         aliasmeshinfo.tex[0] = 0;
894         aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
895         aliasmeshinfo.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
896
897         VectorSubtract(org, r_origin, diff);
898         aliasmeshinfo.cr = fogcolor[0];
899         aliasmeshinfo.cg = fogcolor[1];
900         aliasmeshinfo.cb = fogcolor[2];
901         aliasmeshinfo.ca = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
902
903         for (i = 0;i < m->numshaders;i++)
904         {
905                 aliasmeshinfo.numtriangles = *renderlist++;
906                 aliasmeshinfo.index = renderlist;
907                 R_Mesh_Draw(&aliasmeshinfo);
908                 renderlist += aliasmeshinfo.numtriangles * 3;
909         }
910 }
911
912 void R_DrawZymoticModel (void)
913 {
914         zymtype1header_t *m;
915
916         // FIXME: do better fog
917         m = currentrenderentity->model->zymdata_header;
918         ZymoticLerpBones(m->numbones, (zymbonematrix *)(m->lump_poses.start + (int) m), currentrenderentity->frameblend, (zymbone_t *)(m->lump_bones.start + (int) m), currentrenderentity->origin, currentrenderentity->angles, currentrenderentity->scale);
919         ZymoticTransformVerts(m->numverts, (int *)(m->lump_vertbonecounts.start + (int) m), (zymvertex_t *)(m->lump_verts.start + (int) m));
920         ZymoticCalcNormals(m->numverts, m->numshaders, (int *)(m->lump_render.start + (int) m));
921
922         R_LightModel(m->numverts);
923
924         memset(&aliasmeshinfo, 0, sizeof(aliasmeshinfo));
925         aliasmeshinfo.numverts = m->numverts;
926
927         GL_DrawZymoticModelMesh(m);
928
929         if (fogenabled)
930                 GL_DrawZymoticModelMeshFog(currentrenderentity->origin, m);
931 }
932
933 void R_DrawAliasModel (void)
934 {
935         if (currentrenderentity->alpha < (1.0f / 64.0f))
936                 return; // basically completely transparent
937
938         c_models++;
939
940         c_alias_polys += currentrenderentity->model->numtris;
941         if (currentrenderentity->model->aliastype == ALIASTYPE_ZYM)
942                 R_DrawZymoticModel ();
943         else if (currentrenderentity->model->aliastype == ALIASTYPE_MD2)
944                 R_DrawQ2AliasModel ();
945         else
946                 R_DrawQ1AliasModel   ();
947 }