view model is no longer fully fogged (it was a divide by zero)
[divverent/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 = DotProduct(diff,diff);
370                 if (fog < 0.01f)
371                         fog = 0.01f;
372                 fog = exp(fogdensity/fog);
373                 if (fog > 1)
374                         fog = 1;
375                 if (fog < 0.01f)
376                         fog = 0;
377                 // fog method: darken, additive fog
378                 // 1. render model as normal, scaled by inverse of fog alpha (darkens it)
379                 // 2. render fog as additive
380         }
381
382         if (currentrenderentity->effects & EF_ADDITIVE)
383         {
384                 aliasmeshinfo.transparent = true;
385                 aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
386                 aliasmeshinfo.blendfunc2 = GL_ONE;
387         }
388         else if (currentrenderentity->alpha != 1.0 || skinframe->fog != NULL)
389         {
390                 aliasmeshinfo.transparent = true;
391                 aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
392                 aliasmeshinfo.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
393         }
394         else
395         {
396                 aliasmeshinfo.transparent = false;
397                 aliasmeshinfo.blendfunc1 = GL_ONE;
398                 aliasmeshinfo.blendfunc2 = GL_ZERO;
399         }
400
401         // darken source
402         if (fog)
403                 R_TintModel(aliasvertcolor, aliasvertcolor, model->numverts, 1 - fog, 1 - fog, 1 - fog);
404
405         if (skinframe->base || skinframe->pants || skinframe->shirt || skinframe->glow || skinframe->merged)
406         {
407                 if (currentrenderentity->colormap >= 0 && (skinframe->base || skinframe->pants || skinframe->shirt))
408                 {
409                         int c;
410                         byte *color;
411                         if (skinframe->base)
412                                 GL_DrawModelMesh(skinframe->base, aliasvertcolor, 0, 0, 0);
413                         if (skinframe->pants)
414                         {
415                                 c = (currentrenderentity->colormap & 0xF) << 4;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
416                                 color = (byte *) (&d_8to24table[c]);
417                                 if (c >= 224) // fullbright ranges
418                                         GL_DrawModelMesh(skinframe->pants, NULL, color[0] * (1.0f / 255.0f), color[1] * (1.0f / 255.0f), color[2] * (1.0f / 255.0f));
419                                 else
420                                 {
421                                         R_TintModel(aliasvertcolor, aliasvertcolor2, model->numverts, color[0] * (1.0f / 255.0f), color[1] * (1.0f / 255.0f), color[2] * (1.0f / 255.0f));
422                                         GL_DrawModelMesh(skinframe->pants, aliasvertcolor2, 0, 0, 0);
423                                 }
424                         }
425                         if (skinframe->shirt)
426                         {
427                                 c = currentrenderentity->colormap & 0xF0      ;c += (c >= 128 && c < 224) ? 4 : 12; // 128-224 are backwards ranges
428                                 color = (byte *) (&d_8to24table[c]);
429                                 if (c >= 224) // fullbright ranges
430                                         GL_DrawModelMesh(skinframe->shirt, NULL, color[0] * (1.0f / 255.0f), color[1] * (1.0f / 255.0f), color[2] * (1.0f / 255.0f));
431                                 else
432                                 {
433                                         R_TintModel(aliasvertcolor, aliasvertcolor2, model->numverts, color[0] * (1.0f / 255.0f), color[1] * (1.0f / 255.0f), color[2] * (1.0f / 255.0f));
434                                         GL_DrawModelMesh(skinframe->shirt, aliasvertcolor2, 0, 0, 0);
435                                 }
436                         }
437                 }
438                 else
439                 {
440                         if (skinframe->merged)
441                                 GL_DrawModelMesh(skinframe->merged, aliasvertcolor, 0, 0, 0);
442                         else
443                         {
444                                 if (skinframe->base) GL_DrawModelMesh(skinframe->base, aliasvertcolor, 0, 0, 0);
445                                 if (skinframe->pants) GL_DrawModelMesh(skinframe->pants, aliasvertcolor, 0, 0, 0);
446                                 if (skinframe->shirt) GL_DrawModelMesh(skinframe->shirt, aliasvertcolor, 0, 0, 0);
447                         }
448                 }
449                 if (skinframe->glow) GL_DrawModelMesh(skinframe->glow, NULL, 1 - fog, 1 - fog, 1 - fog);
450         }
451         else
452                 GL_DrawModelMesh(0, NULL, 1 - fog, 1 - fog, 1 - fog);
453
454         if (fog)
455         {
456                 aliasmeshinfo.tex[0] = R_GetTexture(skinframe->fog);
457                 aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
458                 aliasmeshinfo.blendfunc2 = GL_ONE;
459                 aliasmeshinfo.color = NULL;
460
461                 aliasmeshinfo.cr = fogcolor[0];
462                 aliasmeshinfo.cg = fogcolor[1];
463                 aliasmeshinfo.cb = fogcolor[2];
464                 aliasmeshinfo.ca = currentrenderentity->alpha * fog;
465
466                 R_Mesh_Draw(&aliasmeshinfo);
467         }
468 }
469
470 void R_DrawQ2AliasModel (void)
471 {
472         int *order, count;
473         vec3_t diff;
474         skinframe_t *skinframe;
475         model_t *model;
476
477         model = currentrenderentity->model;
478
479         R_SetupMDLMD2Frames(&skinframe);
480
481         if (!r_render.integer)
482                 return;
483
484         // FIXME FIXME FIXME rewrite loader to convert to triangle mesh
485         glBindTexture(GL_TEXTURE_2D, R_GetTexture(skinframe->base));
486
487         if (currentrenderentity->effects & EF_ADDITIVE)
488         {
489                 glBlendFunc(GL_SRC_ALPHA, GL_ONE); // additive rendering
490                 glEnable(GL_BLEND);
491                 glDepthMask(0);
492         }
493         else if (currentrenderentity->alpha != 1.0 || R_TextureHasAlpha(skinframe->base))
494         {
495                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
496                 glEnable(GL_BLEND);
497                 glDepthMask(0);
498         }
499         else
500         {
501                 glDisable(GL_BLEND);
502                 glDepthMask(1);
503         }
504
505         // LordHavoc: big mess...
506         // using vertex arrays only slightly, although it is enough to prevent duplicates
507         // (saving half the transforms)
508         glVertexPointer(3, GL_FLOAT, sizeof(float[3]), aliasvert);
509         glColorPointer(4, GL_FLOAT, sizeof(float[4]), aliasvertcolor);
510         glEnableClientState(GL_VERTEX_ARRAY);
511         glEnableClientState(GL_COLOR_ARRAY);
512
513         GL_LockArray(0, model->numverts);
514
515         order = model->md2data_glcmds;
516         while(1)
517         {
518                 if (!(count = *order++))
519                         break;
520                 if (count > 0)
521                         glBegin(GL_TRIANGLE_STRIP);
522                 else
523                 {
524                         glBegin(GL_TRIANGLE_FAN);
525                         count = -count;
526                 }
527                 do
528                 {
529                         glTexCoord2f(((float *)order)[0], ((float *)order)[1]);
530                         glArrayElement(order[2]);
531                         order += 3;
532                 }
533                 while (count--);
534         }
535
536         GL_UnlockArray();
537
538         glDisableClientState(GL_COLOR_ARRAY);
539         glDisableClientState(GL_VERTEX_ARRAY);
540
541         if (fogenabled)
542         {
543                 glDisable (GL_TEXTURE_2D);
544                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
545                 glEnable (GL_BLEND);
546                 glDepthMask(0); // disable zbuffer updates
547
548                 VectorSubtract(currentrenderentity->origin, r_origin, diff);
549                 glColor4f(fogcolor[0], fogcolor[1], fogcolor[2], currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff)));
550
551                 // LordHavoc: big mess...
552                 // using vertex arrays only slightly, although it is enough to prevent duplicates
553                 // (saving half the transforms)
554                 glVertexPointer(3, GL_FLOAT, sizeof(float[3]), aliasvert);
555                 glEnableClientState(GL_VERTEX_ARRAY);
556
557                 GL_LockArray(0, model->numverts);
558
559                 order = model->md2data_glcmds;
560                 while(1)
561                 {
562                         if (!(count = *order++))
563                                 break;
564                         if (count > 0)
565                                 glBegin(GL_TRIANGLE_STRIP);
566                         else
567                         {
568                                 glBegin(GL_TRIANGLE_FAN);
569                                 count = -count;
570                         }
571                         do
572                         {
573                                 glArrayElement(order[2]);
574                                 order += 3;
575                         }
576                         while (count--);
577                 }
578
579                 GL_UnlockArray();
580
581                 glDisableClientState(GL_VERTEX_ARRAY);
582
583                 glEnable (GL_TEXTURE_2D);
584                 glColor3f (1,1,1);
585         }
586
587         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
588         glEnable (GL_BLEND);
589         glDepthMask(1);
590 }
591
592 void ZymoticLerpBones(int count, zymbonematrix *bonebase, frameblend_t *blend, zymbone_t *bone, float rootorigin[3], float rootangles[3], float rootscale)
593 {
594         float lerp1, lerp2, lerp3, lerp4;
595         zymbonematrix *out, rootmatrix, m, *bone1, *bone2, *bone3, *bone4;
596         out = zymbonepose;
597         AngleVectors(rootangles, rootmatrix.m[0], rootmatrix.m[1], rootmatrix.m[2]);
598         VectorScale(rootmatrix.m[0], rootscale, rootmatrix.m[0]);
599         VectorScale(rootmatrix.m[1], rootscale, rootmatrix.m[1]);
600         VectorScale(rootmatrix.m[2], rootscale, rootmatrix.m[2]);
601         rootmatrix.m[0][3] = rootorigin[0];
602         rootmatrix.m[1][3] = rootorigin[1];
603         rootmatrix.m[2][3] = rootorigin[2];
604         bone1 = bonebase + blend[0].frame * count;
605         lerp1 = blend[0].lerp;
606         if (blend[1].lerp)
607         {
608                 bone2 = bonebase + blend[1].frame * count;
609                 lerp2 = blend[1].lerp;
610                 if (blend[2].lerp)
611                 {
612                         bone3 = bonebase + blend[2].frame * count;
613                         lerp3 = blend[2].lerp;
614                         if (blend[3].lerp)
615                         {
616                                 // 4 poses
617                                 bone4 = bonebase + blend[3].frame * count;
618                                 lerp4 = blend[3].lerp;
619                                 while(count--)
620                                 {
621                                         // interpolate matrices
622                                         m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3 + bone4->m[0][0] * lerp4;
623                                         m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3 + bone4->m[0][1] * lerp4;
624                                         m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3 + bone4->m[0][2] * lerp4;
625                                         m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3 + bone4->m[0][3] * lerp4;
626                                         m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3 + bone4->m[1][0] * lerp4;
627                                         m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3 + bone4->m[1][1] * lerp4;
628                                         m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3 + bone4->m[1][2] * lerp4;
629                                         m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3 + bone4->m[1][3] * lerp4;
630                                         m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3 + bone4->m[2][0] * lerp4;
631                                         m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3 + bone4->m[2][1] * lerp4;
632                                         m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3 + bone4->m[2][2] * lerp4;
633                                         m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3 + bone4->m[2][3] * lerp4;
634                                         if (bone->parent >= 0)
635                                                 R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &m.m[0], &out->m[0]);
636                                         else
637                                                 R_ConcatTransforms(&rootmatrix.m[0], &m.m[0], &out->m[0]);
638                                         bone1++;
639                                         bone2++;
640                                         bone3++;
641                                         bone4++;
642                                         bone++;
643                                         out++;
644                                 }
645                         }
646                         else
647                         {
648                                 // 3 poses
649                                 while(count--)
650                                 {
651                                         // interpolate matrices
652                                         m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2 + bone3->m[0][0] * lerp3;
653                                         m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2 + bone3->m[0][1] * lerp3;
654                                         m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2 + bone3->m[0][2] * lerp3;
655                                         m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2 + bone3->m[0][3] * lerp3;
656                                         m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2 + bone3->m[1][0] * lerp3;
657                                         m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2 + bone3->m[1][1] * lerp3;
658                                         m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2 + bone3->m[1][2] * lerp3;
659                                         m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2 + bone3->m[1][3] * lerp3;
660                                         m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2 + bone3->m[2][0] * lerp3;
661                                         m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2 + bone3->m[2][1] * lerp3;
662                                         m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2 + bone3->m[2][2] * lerp3;
663                                         m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2 + bone3->m[2][3] * lerp3;
664                                         if (bone->parent >= 0)
665                                                 R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &m.m[0], &out->m[0]);
666                                         else
667                                                 R_ConcatTransforms(&rootmatrix.m[0], &m.m[0], &out->m[0]);
668                                         bone1++;
669                                         bone2++;
670                                         bone3++;
671                                         bone++;
672                                         out++;
673                                 }
674                         }
675                 }
676                 else
677                 {
678                         // 2 poses
679                         while(count--)
680                         {
681                                 // interpolate matrices
682                                 m.m[0][0] = bone1->m[0][0] * lerp1 + bone2->m[0][0] * lerp2;
683                                 m.m[0][1] = bone1->m[0][1] * lerp1 + bone2->m[0][1] * lerp2;
684                                 m.m[0][2] = bone1->m[0][2] * lerp1 + bone2->m[0][2] * lerp2;
685                                 m.m[0][3] = bone1->m[0][3] * lerp1 + bone2->m[0][3] * lerp2;
686                                 m.m[1][0] = bone1->m[1][0] * lerp1 + bone2->m[1][0] * lerp2;
687                                 m.m[1][1] = bone1->m[1][1] * lerp1 + bone2->m[1][1] * lerp2;
688                                 m.m[1][2] = bone1->m[1][2] * lerp1 + bone2->m[1][2] * lerp2;
689                                 m.m[1][3] = bone1->m[1][3] * lerp1 + bone2->m[1][3] * lerp2;
690                                 m.m[2][0] = bone1->m[2][0] * lerp1 + bone2->m[2][0] * lerp2;
691                                 m.m[2][1] = bone1->m[2][1] * lerp1 + bone2->m[2][1] * lerp2;
692                                 m.m[2][2] = bone1->m[2][2] * lerp1 + bone2->m[2][2] * lerp2;
693                                 m.m[2][3] = bone1->m[2][3] * lerp1 + bone2->m[2][3] * lerp2;
694                                 if (bone->parent >= 0)
695                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &m.m[0], &out->m[0]);
696                                 else
697                                         R_ConcatTransforms(&rootmatrix.m[0], &m.m[0], &out->m[0]);
698                                 bone1++;
699                                 bone2++;
700                                 bone++;
701                                 out++;
702                         }
703                 }
704         }
705         else
706         {
707                 // 1 pose
708                 if (lerp1 != 1)
709                 {
710                         // lerp != 1.0
711                         while(count--)
712                         {
713                                 // interpolate matrices
714                                 m.m[0][0] = bone1->m[0][0] * lerp1;
715                                 m.m[0][1] = bone1->m[0][1] * lerp1;
716                                 m.m[0][2] = bone1->m[0][2] * lerp1;
717                                 m.m[0][3] = bone1->m[0][3] * lerp1;
718                                 m.m[1][0] = bone1->m[1][0] * lerp1;
719                                 m.m[1][1] = bone1->m[1][1] * lerp1;
720                                 m.m[1][2] = bone1->m[1][2] * lerp1;
721                                 m.m[1][3] = bone1->m[1][3] * lerp1;
722                                 m.m[2][0] = bone1->m[2][0] * lerp1;
723                                 m.m[2][1] = bone1->m[2][1] * lerp1;
724                                 m.m[2][2] = bone1->m[2][2] * lerp1;
725                                 m.m[2][3] = bone1->m[2][3] * lerp1;
726                                 if (bone->parent >= 0)
727                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &m.m[0], &out->m[0]);
728                                 else
729                                         R_ConcatTransforms(&rootmatrix.m[0], &m.m[0], &out->m[0]);
730                                 bone1++;
731                                 bone++;
732                                 out++;
733                         }
734                 }
735                 else
736                 {
737                         // lerp == 1.0
738                         while(count--)
739                         {
740                                 if (bone->parent >= 0)
741                                         R_ConcatTransforms(&zymbonepose[bone->parent].m[0], &bone1->m[0], &out->m[0]);
742                                 else
743                                         R_ConcatTransforms(&rootmatrix.m[0], &bone1->m[0], &out->m[0]);
744                                 bone1++;
745                                 bone++;
746                                 out++;
747                         }
748                 }
749         }
750 }
751
752 void ZymoticTransformVerts(int vertcount, int *bonecounts, zymvertex_t *vert)
753 {
754         int c;
755         float *out = aliasvert;
756         zymbonematrix *matrix;
757         while(vertcount--)
758         {
759                 c = *bonecounts++;
760                 // FIXME: validate bonecounts at load time (must be >= 1)
761                 if (c == 1)
762                 {
763                         matrix = &zymbonepose[vert->bonenum];
764                         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];
765                         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];
766                         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];
767                         vert++;
768                 }
769                 else
770                 {
771                         VectorClear(out);
772                         while(c--)
773                         {
774                                 matrix = &zymbonepose[vert->bonenum];
775                                 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];
776                                 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];
777                                 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];
778                                 vert++;
779                         }
780                 }
781                 out += 3;
782         }
783 }
784
785 void ZymoticCalcNormals(int vertcount, int shadercount, int *renderlist)
786 {
787         int a, b, c, d;
788         float *out, v1[3], v2[3], normal[3];
789         int *u;
790         // clear normals
791         memset(aliasvertnorm, 0, sizeof(float[3]) * vertcount);
792         memset(aliasvertusage, 0, sizeof(int) * vertcount);
793         // parse render list and accumulate surface normals
794         while(shadercount--)
795         {
796                 d = *renderlist++;
797                 while (d--)
798                 {
799                         a = renderlist[0]*3;
800                         b = renderlist[1]*3;
801                         c = renderlist[2]*3;
802                         v1[0] = aliasvert[a+0] - aliasvert[b+0];
803                         v1[1] = aliasvert[a+1] - aliasvert[b+1];
804                         v1[2] = aliasvert[a+2] - aliasvert[b+2];
805                         v2[0] = aliasvert[c+0] - aliasvert[b+0];
806                         v2[1] = aliasvert[c+1] - aliasvert[b+1];
807                         v2[2] = aliasvert[c+2] - aliasvert[b+2];
808                         CrossProduct(v1, v2, normal);
809                         VectorNormalizeFast(normal);
810                         // add surface normal to vertices
811                         aliasvertnorm[a+0] += normal[0];
812                         aliasvertnorm[a+1] += normal[1];
813                         aliasvertnorm[a+2] += normal[2];
814                         aliasvertusage[a]++;
815                         aliasvertnorm[b+0] += normal[0];
816                         aliasvertnorm[b+1] += normal[1];
817                         aliasvertnorm[b+2] += normal[2];
818                         aliasvertusage[b]++;
819                         aliasvertnorm[c+0] += normal[0];
820                         aliasvertnorm[c+1] += normal[1];
821                         aliasvertnorm[c+2] += normal[2];
822                         aliasvertusage[c]++;
823                         renderlist += 3;
824                 }
825         }
826         // average surface normals
827         out = aliasvertnorm;
828         u = aliasvertusage;
829         while(vertcount--)
830         {
831                 if (*u > 1)
832                 {
833                         a = ixtable[*u];
834                         out[0] *= a;
835                         out[1] *= a;
836                         out[2] *= a;
837                 }
838                 u++;
839                 out += 3;
840         }
841 }
842
843 void GL_DrawZymoticModelMesh(zymtype1header_t *m)
844 {
845         int i, *renderlist;
846         rtexture_t **texture;
847
848         // FIXME: do better fog
849         renderlist = (int *)(m->lump_render.start + (int) m);
850         texture = (rtexture_t **)(m->lump_shaders.start + (int) m);
851
852         aliasmeshinfo.vertex = aliasvert;
853         aliasmeshinfo.vertexstep = sizeof(float[3]);
854         aliasmeshinfo.color = aliasvertcolor;
855         aliasmeshinfo.colorstep = sizeof(float[4]);
856         aliasmeshinfo.texcoords[0] = (float *)(m->lump_texcoords.start + (int) m);
857         aliasmeshinfo.texcoordstep[0] = sizeof(float[2]);
858
859         for (i = 0;i < m->numshaders;i++)
860         {
861                 aliasmeshinfo.tex[0] = R_GetTexture(*texture);
862                 aliasmeshinfo.tex[1] = 0;
863                 if (currentrenderentity->effects & EF_ADDITIVE)
864                 {
865                         aliasmeshinfo.transparent = true;
866                         aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
867                         aliasmeshinfo.blendfunc2 = GL_ONE;
868                 }
869                 else if (currentrenderentity->alpha != 1.0 || R_TextureHasAlpha(*texture))
870                 {
871                         aliasmeshinfo.transparent = true;
872                         aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
873                         aliasmeshinfo.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
874                 }
875                 else
876                 {
877                         aliasmeshinfo.transparent = false;
878                         aliasmeshinfo.blendfunc1 = GL_ONE;
879                         aliasmeshinfo.blendfunc2 = GL_ZERO;
880                 }
881                 aliasmeshinfo.numtriangles = *renderlist++;
882                 aliasmeshinfo.index = renderlist;
883                 R_Mesh_Draw(&aliasmeshinfo);
884                 renderlist += aliasmeshinfo.numtriangles * 3;
885         }
886 }
887
888 void GL_DrawZymoticModelMeshFog(vec3_t org, zymtype1header_t *m)
889 {
890         int i, *renderlist;
891         vec3_t diff;
892
893         // FIXME: do better fog
894         renderlist = (int *)(m->lump_render.start + (int) m);
895
896         aliasmeshinfo.tex[0] = 0;
897         aliasmeshinfo.blendfunc1 = GL_SRC_ALPHA;
898         aliasmeshinfo.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
899
900         VectorSubtract(org, r_origin, diff);
901         aliasmeshinfo.cr = fogcolor[0];
902         aliasmeshinfo.cg = fogcolor[1];
903         aliasmeshinfo.cb = fogcolor[2];
904         aliasmeshinfo.ca = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
905
906         for (i = 0;i < m->numshaders;i++)
907         {
908                 aliasmeshinfo.numtriangles = *renderlist++;
909                 aliasmeshinfo.index = renderlist;
910                 R_Mesh_Draw(&aliasmeshinfo);
911                 renderlist += aliasmeshinfo.numtriangles * 3;
912         }
913 }
914
915 void R_DrawZymoticModel (void)
916 {
917         zymtype1header_t *m;
918
919         // FIXME: do better fog
920         m = currentrenderentity->model->zymdata_header;
921         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);
922         ZymoticTransformVerts(m->numverts, (int *)(m->lump_vertbonecounts.start + (int) m), (zymvertex_t *)(m->lump_verts.start + (int) m));
923         ZymoticCalcNormals(m->numverts, m->numshaders, (int *)(m->lump_render.start + (int) m));
924
925         R_LightModel(m->numverts);
926
927         memset(&aliasmeshinfo, 0, sizeof(aliasmeshinfo));
928         aliasmeshinfo.numverts = m->numverts;
929
930         GL_DrawZymoticModelMesh(m);
931
932         if (fogenabled)
933                 GL_DrawZymoticModelMeshFog(currentrenderentity->origin, m);
934 }
935
936 void R_DrawAliasModel (void)
937 {
938         if (currentrenderentity->alpha < (1.0f / 64.0f))
939                 return; // basically completely transparent
940
941         c_models++;
942
943         c_alias_polys += currentrenderentity->model->numtris;
944         if (currentrenderentity->model->aliastype == ALIASTYPE_ZYM)
945                 R_DrawZymoticModel ();
946         else if (currentrenderentity->model->aliastype == ALIASTYPE_MD2)
947                 R_DrawQ2AliasModel ();
948         else
949                 R_DrawQ1AliasModel   ();
950 }