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