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