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