]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_rsurf.c
eliminated surf->poly_* fields and cleaned up Mod_LoadFaces a great deal
[divverent/darkplaces.git] / gl_rsurf.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // r_surf.c: surface-related refresh code
21
22 #include "quakedef.h"
23 #include "r_shadow.h"
24
25 #define MAX_LIGHTMAP_SIZE 256
26
27 static unsigned int intblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
28 static float floatblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
29
30 static qbyte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
31
32 cvar_t r_ambient = {0, "r_ambient", "0"};
33 cvar_t r_drawportals = {0, "r_drawportals", "0"};
34 cvar_t r_testvis = {0, "r_testvis", "0"};
35 cvar_t r_floatbuildlightmap = {0, "r_floatbuildlightmap", "0"};
36 cvar_t r_detailtextures = {CVAR_SAVE, "r_detailtextures", "1"};
37 cvar_t r_surfaceworldnode = {0, "r_surfaceworldnode", "0"};
38 cvar_t r_drawcollisionbrushes_polygonfactor = {0, "r_drawcollisionbrushes_polygonfactor", "-1"};
39 cvar_t r_drawcollisionbrushes_polygonoffset = {0, "r_drawcollisionbrushes_polygonoffset", "0"};
40 cvar_t r_q3bsp_renderskydepth = {0, "r_q3bsp_renderskydepth", "0"};
41 cvar_t gl_lightmaps = {0, "gl_lightmaps", "0"};
42
43 static int dlightdivtable[32768];
44
45 static int R_IntAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
46 {
47         int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract, k;
48         unsigned int *bl;
49         float dist, impact[3], local[3];
50         dlight_t *light;
51
52         lit = false;
53
54         smax = (surf->extents[0] >> 4) + 1;
55         tmax = (surf->extents[1] >> 4) + 1;
56         smax3 = smax * 3;
57
58         for (lnum = 0, light = r_dlight;lnum < r_numdlights;lnum++, light++)
59         {
60                 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
61                         continue;                                       // not lit by this light
62
63                 Matrix4x4_Transform(matrix, light->origin, local);
64                 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
65
66                 // for comparisons to minimum acceptable light
67                 // compensate for LIGHTOFFSET
68                 maxdist = (int) light->rtlight.lightmap_cullradius2 + LIGHTOFFSET;
69
70                 dist2 = dist * dist;
71                 dist2 += LIGHTOFFSET;
72                 if (dist2 >= maxdist)
73                         continue;
74
75                 if (surf->plane->type < 3)
76                 {
77                         VectorCopy(local, impact);
78                         impact[surf->plane->type] -= dist;
79                 }
80                 else
81                 {
82                         impact[0] = local[0] - surf->plane->normal[0] * dist;
83                         impact[1] = local[1] - surf->plane->normal[1] * dist;
84                         impact[2] = local[2] - surf->plane->normal[2] * dist;
85                 }
86
87                 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
88                 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
89
90                 s = bound(0, impacts, smax * 16) - impacts;
91                 t = bound(0, impactt, tmax * 16) - impactt;
92                 i = s * s + t * t + dist2;
93                 if (i > maxdist)
94                         continue;
95
96                 // reduce calculations
97                 for (s = 0, i = impacts; s < smax; s++, i -= 16)
98                         sdtable[s] = i * i + dist2;
99
100                 maxdist3 = maxdist - dist2;
101
102                 // convert to 8.8 blocklights format
103                 red = light->rtlight.lightmap_light[0] * (1.0f / 128.0f);
104                 green = light->rtlight.lightmap_light[1] * (1.0f / 128.0f);
105                 blue = light->rtlight.lightmap_light[2] * (1.0f / 128.0f);
106                 subtract = (int) (light->rtlight.lightmap_subtract * 4194304.0f);
107                 bl = intblocklights;
108
109                 i = impactt;
110                 for (t = 0;t < tmax;t++, i -= 16)
111                 {
112                         td = i * i;
113                         // make sure some part of it is visible on this line
114                         if (td < maxdist3)
115                         {
116                                 maxdist2 = maxdist - td;
117                                 for (s = 0;s < smax;s++)
118                                 {
119                                         if (sdtable[s] < maxdist2)
120                                         {
121                                                 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
122                                                 if (k > 0)
123                                                 {
124                                                         bl[0] += (red   * k);
125                                                         bl[1] += (green * k);
126                                                         bl[2] += (blue  * k);
127                                                         lit = true;
128                                                 }
129                                         }
130                                         bl += 3;
131                                 }
132                         }
133                         else // skip line
134                                 bl += smax3;
135                 }
136         }
137         return lit;
138 }
139
140 static int R_FloatAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
141 {
142         int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
143         float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
144         dlight_t *light;
145
146         lit = false;
147
148         smax = (surf->extents[0] >> 4) + 1;
149         tmax = (surf->extents[1] >> 4) + 1;
150         smax3 = smax * 3;
151
152         for (lnum = 0, light = r_dlight;lnum < r_numdlights;lnum++, light++)
153         {
154                 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
155                         continue;                                       // not lit by this light
156
157                 Matrix4x4_Transform(matrix, light->origin, local);
158                 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
159
160                 // for comparisons to minimum acceptable light
161                 // compensate for LIGHTOFFSET
162                 maxdist = (int) light->rtlight.lightmap_cullradius2 + LIGHTOFFSET;
163
164                 dist2 = dist * dist;
165                 dist2 += LIGHTOFFSET;
166                 if (dist2 >= maxdist)
167                         continue;
168
169                 if (surf->plane->type < 3)
170                 {
171                         VectorCopy(local, impact);
172                         impact[surf->plane->type] -= dist;
173                 }
174                 else
175                 {
176                         impact[0] = local[0] - surf->plane->normal[0] * dist;
177                         impact[1] = local[1] - surf->plane->normal[1] * dist;
178                         impact[2] = local[2] - surf->plane->normal[2] * dist;
179                 }
180
181                 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
182                 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
183
184                 td = bound(0, impacts, smax * 16) - impacts;
185                 td1 = bound(0, impactt, tmax * 16) - impactt;
186                 td = td * td + td1 * td1 + dist2;
187                 if (td > maxdist)
188                         continue;
189
190                 // reduce calculations
191                 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
192                         sdtable[s] = td1 * td1 + dist2;
193
194                 maxdist3 = maxdist - dist2;
195
196                 // convert to 8.8 blocklights format
197                 red = light->rtlight.lightmap_light[0];
198                 green = light->rtlight.lightmap_light[1];
199                 blue = light->rtlight.lightmap_light[2];
200                 subtract = light->rtlight.lightmap_subtract * 32768.0f;
201                 bl = floatblocklights;
202
203                 td1 = impactt;
204                 for (t = 0;t < tmax;t++, td1 -= 16.0f)
205                 {
206                         td = td1 * td1;
207                         // make sure some part of it is visible on this line
208                         if (td < maxdist3)
209                         {
210                                 maxdist2 = maxdist - td;
211                                 for (s = 0;s < smax;s++)
212                                 {
213                                         if (sdtable[s] < maxdist2)
214                                         {
215                                                 k = (32768.0f / (sdtable[s] + td)) - subtract;
216                                                 bl[0] += red   * k;
217                                                 bl[1] += green * k;
218                                                 bl[2] += blue  * k;
219                                                 lit = true;
220                                         }
221                                         bl += 3;
222                                 }
223                         }
224                         else // skip line
225                                 bl += smax3;
226                 }
227         }
228         return lit;
229 }
230
231 /*
232 ===============
233 R_BuildLightMap
234
235 Combine and scale multiple lightmaps into the 8.8 format in blocklights
236 ===============
237 */
238 static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf)
239 {
240         if (!r_floatbuildlightmap.integer)
241         {
242                 int smax, tmax, i, j, size, size3, maps, stride, l;
243                 unsigned int *bl, scale;
244                 qbyte *lightmap, *out, *stain;
245
246                 // update cached lighting info
247                 surf->cached_dlight = 0;
248
249                 smax = (surf->extents[0]>>4)+1;
250                 tmax = (surf->extents[1]>>4)+1;
251                 size = smax*tmax;
252                 size3 = size*3;
253                 lightmap = surf->samples;
254
255         // set to full bright if no light data
256                 bl = intblocklights;
257                 if (!ent->model->brushq1.lightdata)
258                 {
259                         for (i = 0;i < size3;i++)
260                                 bl[i] = 255*256;
261                 }
262                 else
263                 {
264         // clear to no light
265                         memset(bl, 0, size*3*sizeof(unsigned int));
266
267                         if (surf->dlightframe == r_framecount)
268                         {
269                                 surf->cached_dlight = R_IntAddDynamicLights(&ent->inversematrix, surf);
270                                 if (surf->cached_dlight)
271                                         c_light_polys++;
272                         }
273
274         // add all the lightmaps
275                         if (lightmap)
276                         {
277                                 bl = intblocklights;
278                                 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
279                                         for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
280                                                 bl[i] += lightmap[i] * scale;
281                         }
282                 }
283
284                 stain = surf->stainsamples;
285                 bl = intblocklights;
286                 out = templight;
287                 // the >> 16 shift adjusts down 8 bits to account for the stainmap
288                 // scaling, and remaps the 0-65536 (2x overbright) to 0-256, it will
289                 // be doubled during rendering to achieve 2x overbright
290                 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
291                 if (ent->model->brushq1.lightmaprgba)
292                 {
293                         stride = (surf->lightmaptexturestride - smax) * 4;
294                         for (i = 0;i < tmax;i++, out += stride)
295                         {
296                                 for (j = 0;j < smax;j++)
297                                 {
298                                         l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
299                                         l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
300                                         l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
301                                         *out++ = 255;
302                                 }
303                         }
304                 }
305                 else
306                 {
307                         stride = (surf->lightmaptexturestride - smax) * 3;
308                         for (i = 0;i < tmax;i++, out += stride)
309                         {
310                                 for (j = 0;j < smax;j++)
311                                 {
312                                         l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
313                                         l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
314                                         l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
315                                 }
316                         }
317                 }
318
319                 R_UpdateTexture(surf->lightmaptexture, templight);
320         }
321         else
322         {
323                 int smax, tmax, i, j, size, size3, maps, stride, l;
324                 float *bl, scale;
325                 qbyte *lightmap, *out, *stain;
326
327                 // update cached lighting info
328                 surf->cached_dlight = 0;
329
330                 smax = (surf->extents[0]>>4)+1;
331                 tmax = (surf->extents[1]>>4)+1;
332                 size = smax*tmax;
333                 size3 = size*3;
334                 lightmap = surf->samples;
335
336         // set to full bright if no light data
337                 bl = floatblocklights;
338                 if (!ent->model->brushq1.lightdata)
339                 {
340                         for (i = 0;i < size3;i++)
341                                 bl[i] = 255*256;
342                 }
343                 else
344                 {
345                         memset(bl, 0, size*3*sizeof(float));
346
347                         if (surf->dlightframe == r_framecount)
348                         {
349                                 surf->cached_dlight = R_FloatAddDynamicLights(&ent->inversematrix, surf);
350                                 if (surf->cached_dlight)
351                                         c_light_polys++;
352                         }
353
354                         // add all the lightmaps
355                         if (lightmap)
356                         {
357                                 bl = floatblocklights;
358                                 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
359                                         for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
360                                                 bl[i] += lightmap[i] * scale;
361                         }
362                 }
363
364                 stain = surf->stainsamples;
365                 bl = floatblocklights;
366                 out = templight;
367                 // this scaling adjusts down 8 bits to account for the stainmap
368                 // scaling, and remaps the 0.0-2.0 (2x overbright) to 0-256, it will
369                 // be doubled during rendering to achieve 2x overbright
370                 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
371                 scale = 1.0f / (1 << 16);
372                 if (ent->model->brushq1.lightmaprgba)
373                 {
374                         stride = (surf->lightmaptexturestride - smax) * 4;
375                         for (i = 0;i < tmax;i++, out += stride)
376                         {
377                                 for (j = 0;j < smax;j++)
378                                 {
379                                         l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
380                                         l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
381                                         l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
382                                         *out++ = 255;
383                                 }
384                         }
385                 }
386                 else
387                 {
388                         stride = (surf->lightmaptexturestride - smax) * 3;
389                         for (i = 0;i < tmax;i++, out += stride)
390                         {
391                                 for (j = 0;j < smax;j++)
392                                 {
393                                         l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
394                                         l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
395                                         l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
396                                 }
397                         }
398                 }
399
400                 R_UpdateTexture(surf->lightmaptexture, templight);
401         }
402 }
403
404 void R_StainNode (mnode_t *node, model_t *model, const vec3_t origin, float radius, const float fcolor[8])
405 {
406         float ndist, a, ratio, maxdist, maxdist2, maxdist3, invradius, sdtable[256], td, dist2;
407         msurface_t *surf, *endsurf;
408         int i, s, t, smax, tmax, smax3, impacts, impactt, stained;
409         qbyte *bl;
410         vec3_t impact;
411
412         maxdist = radius * radius;
413         invradius = 1.0f / radius;
414
415 loc0:
416         if (!node->plane)
417                 return;
418         ndist = PlaneDiff(origin, node->plane);
419         if (ndist > radius)
420         {
421                 node = node->children[0];
422                 goto loc0;
423         }
424         if (ndist < -radius)
425         {
426                 node = node->children[1];
427                 goto loc0;
428         }
429
430         dist2 = ndist * ndist;
431         maxdist3 = maxdist - dist2;
432
433         if (node->plane->type < 3)
434         {
435                 VectorCopy(origin, impact);
436                 impact[node->plane->type] -= ndist;
437         }
438         else
439         {
440                 impact[0] = origin[0] - node->plane->normal[0] * ndist;
441                 impact[1] = origin[1] - node->plane->normal[1] * ndist;
442                 impact[2] = origin[2] - node->plane->normal[2] * ndist;
443         }
444
445         for (surf = model->brushq1.surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
446         {
447                 if (surf->stainsamples)
448                 {
449                         smax = (surf->extents[0] >> 4) + 1;
450                         tmax = (surf->extents[1] >> 4) + 1;
451
452                         impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
453                         impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
454
455                         s = bound(0, impacts, smax * 16) - impacts;
456                         t = bound(0, impactt, tmax * 16) - impactt;
457                         i = s * s + t * t + dist2;
458                         if (i > maxdist)
459                                 continue;
460
461                         // reduce calculations
462                         for (s = 0, i = impacts; s < smax; s++, i -= 16)
463                                 sdtable[s] = i * i + dist2;
464
465                         bl = surf->stainsamples;
466                         smax3 = smax * 3;
467                         stained = false;
468
469                         i = impactt;
470                         for (t = 0;t < tmax;t++, i -= 16)
471                         {
472                                 td = i * i;
473                                 // make sure some part of it is visible on this line
474                                 if (td < maxdist3)
475                                 {
476                                         maxdist2 = maxdist - td;
477                                         for (s = 0;s < smax;s++)
478                                         {
479                                                 if (sdtable[s] < maxdist2)
480                                                 {
481                                                         ratio = lhrandom(0.0f, 1.0f);
482                                                         a = (fcolor[3] + ratio * fcolor[7]) * (1.0f - sqrt(sdtable[s] + td) * invradius);
483                                                         if (a >= (1.0f / 64.0f))
484                                                         {
485                                                                 if (a > 1)
486                                                                         a = 1;
487                                                                 bl[0] = (qbyte) ((float) bl[0] + a * ((fcolor[0] + ratio * fcolor[4]) - (float) bl[0]));
488                                                                 bl[1] = (qbyte) ((float) bl[1] + a * ((fcolor[1] + ratio * fcolor[5]) - (float) bl[1]));
489                                                                 bl[2] = (qbyte) ((float) bl[2] + a * ((fcolor[2] + ratio * fcolor[6]) - (float) bl[2]));
490                                                                 stained = true;
491                                                         }
492                                                 }
493                                                 bl += 3;
494                                         }
495                                 }
496                                 else // skip line
497                                         bl += smax3;
498                         }
499                         // force lightmap upload
500                         if (stained)
501                                 surf->cached_dlight = true;
502                 }
503         }
504
505         if (node->children[0]->plane)
506         {
507                 if (node->children[1]->plane)
508                 {
509                         R_StainNode(node->children[0], model, origin, radius, fcolor);
510                         node = node->children[1];
511                         goto loc0;
512                 }
513                 else
514                 {
515                         node = node->children[0];
516                         goto loc0;
517                 }
518         }
519         else if (node->children[1]->plane)
520         {
521                 node = node->children[1];
522                 goto loc0;
523         }
524 }
525
526 void R_Stain (const vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2)
527 {
528         int n;
529         float fcolor[8];
530         entity_render_t *ent;
531         model_t *model;
532         vec3_t org;
533         if (r_refdef.worldmodel == NULL || !r_refdef.worldmodel->brush.data_nodes)
534                 return;
535         fcolor[0] = cr1;
536         fcolor[1] = cg1;
537         fcolor[2] = cb1;
538         fcolor[3] = ca1 * (1.0f / 64.0f);
539         fcolor[4] = cr2 - cr1;
540         fcolor[5] = cg2 - cg1;
541         fcolor[6] = cb2 - cb1;
542         fcolor[7] = (ca2 - ca1) * (1.0f / 64.0f);
543
544         R_StainNode(r_refdef.worldmodel->brush.data_nodes + r_refdef.worldmodel->brushq1.hulls[0].firstclipnode, r_refdef.worldmodel, origin, radius, fcolor);
545
546         // look for embedded bmodels
547         for (n = 0;n < cl_num_brushmodel_entities;n++)
548         {
549                 ent = cl_brushmodel_entities[n];
550                 model = ent->model;
551                 if (model && model->name[0] == '*')
552                 {
553                         Mod_CheckLoaded(model);
554                         if (model->brush.data_nodes)
555                         {
556                                 Matrix4x4_Transform(&ent->inversematrix, origin, org);
557                                 R_StainNode(model->brush.data_nodes + model->brushq1.hulls[0].firstclipnode, model, org, radius, fcolor);
558                         }
559                 }
560         }
561 }
562
563
564 /*
565 =============================================================
566
567         BRUSH MODELS
568
569 =============================================================
570 */
571
572 static void RSurf_AddLightmapToVertexColors_Color4f(const int *lightmapoffsets, float *c, int numverts, const qbyte *samples, int size3, const qbyte *styles)
573 {
574         int i;
575         float scale;
576         const qbyte *lm;
577         if (styles[0] != 255)
578         {
579                 for (i = 0;i < numverts;i++, c += 4)
580                 {
581                         lm = samples + lightmapoffsets[i];
582                         scale = d_lightstylevalue[styles[0]] * (1.0f / 32768.0f);
583                         VectorMA(c, scale, lm, c);
584                         if (styles[1] != 255)
585                         {
586                                 lm += size3;
587                                 scale = d_lightstylevalue[styles[1]] * (1.0f / 32768.0f);
588                                 VectorMA(c, scale, lm, c);
589                                 if (styles[2] != 255)
590                                 {
591                                         lm += size3;
592                                         scale = d_lightstylevalue[styles[2]] * (1.0f / 32768.0f);
593                                         VectorMA(c, scale, lm, c);
594                                         if (styles[3] != 255)
595                                         {
596                                                 lm += size3;
597                                                 scale = d_lightstylevalue[styles[3]] * (1.0f / 32768.0f);
598                                                 VectorMA(c, scale, lm, c);
599                                         }
600                                 }
601                         }
602                 }
603         }
604 }
605
606 static void RSurf_FogColors_Vertex3f_Color4f(const float *v, float *c, float colorscale, int numverts, const float *modelorg)
607 {
608         int i;
609         float diff[3], f;
610         if (fogenabled)
611         {
612                 for (i = 0;i < numverts;i++, v += 3, c += 4)
613                 {
614                         VectorSubtract(v, modelorg, diff);
615                         f = colorscale * (1 - exp(fogdensity/DotProduct(diff, diff)));
616                         VectorScale(c, f, c);
617                 }
618         }
619         else if (colorscale != 1)
620                 for (i = 0;i < numverts;i++, c += 4)
621                         VectorScale(c, colorscale, c);
622 }
623
624 static void RSurf_FoggedColors_Vertex3f_Color4f(const float *v, float *c, float r, float g, float b, float a, float colorscale, int numverts, const float *modelorg)
625 {
626         int i;
627         float diff[3], f;
628         r *= colorscale;
629         g *= colorscale;
630         b *= colorscale;
631         if (fogenabled)
632         {
633                 for (i = 0;i < numverts;i++, v += 3, c += 4)
634                 {
635                         VectorSubtract(v, modelorg, diff);
636                         f = 1 - exp(fogdensity/DotProduct(diff, diff));
637                         c[0] = r * f;
638                         c[1] = g * f;
639                         c[2] = b * f;
640                         c[3] = a;
641                 }
642         }
643         else
644         {
645                 for (i = 0;i < numverts;i++, c += 4)
646                 {
647                         c[0] = r;
648                         c[1] = g;
649                         c[2] = b;
650                         c[3] = a;
651                 }
652         }
653 }
654
655 static void RSurf_FogPassColors_Vertex3f_Color4f(const float *v, float *c, float r, float g, float b, float a, float colorscale, int numverts, const float *modelorg)
656 {
657         int i;
658         float diff[3], f;
659         r *= colorscale;
660         g *= colorscale;
661         b *= colorscale;
662         for (i = 0;i < numverts;i++, v += 3, c += 4)
663         {
664                 VectorSubtract(v, modelorg, diff);
665                 f = exp(fogdensity/DotProduct(diff, diff));
666                 c[0] = r;
667                 c[1] = g;
668                 c[2] = b;
669                 c[3] = a * f;
670         }
671 }
672
673 static int RSurf_LightSeparate_Vertex3f_Color4f(const matrix4x4_t *matrix, const int *dlightbits, int numverts, const float *vert, float *color, float scale)
674 {
675         float f;
676         const float *v;
677         float *c;
678         int i, l, lit = false;
679         const dlight_t *light;
680         vec3_t lightorigin;
681         for (l = 0;l < r_numdlights;l++)
682         {
683                 if (dlightbits[l >> 5] & (1 << (l & 31)))
684                 {
685                         light = &r_dlight[l];
686                         Matrix4x4_Transform(matrix, light->origin, lightorigin);
687                         for (i = 0, v = vert, c = color;i < numverts;i++, v += 3, c += 4)
688                         {
689                                 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
690                                 if (f < light->rtlight.lightmap_cullradius2)
691                                 {
692                                         f = ((1.0f / f) - light->rtlight.lightmap_subtract) * scale;
693                                         VectorMA(c, f, light->rtlight.lightmap_light, c);
694                                         lit = true;
695                                 }
696                         }
697                 }
698         }
699         return lit;
700 }
701
702 static void RSurfShader_Transparent_Callback(const void *calldata1, int calldata2)
703 {
704         const entity_render_t *ent = calldata1;
705         const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
706         rmeshstate_t m;
707         float currentalpha;
708         float base, colorscale;
709         vec3_t modelorg;
710         texture_t *texture;
711         float args[4] = {0.05f,0,0,0.04f};
712         int rendertype, turb, fullbright;
713
714         R_Mesh_Matrix(&ent->matrix);
715         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
716
717         texture = surf->texinfo->texture;
718         if (texture->animated)
719                 texture = texture->anim_frames[ent->frame != 0][(texture->anim_total[ent->frame != 0] >= 2) ? ((int) (r_refdef.time * 5.0f) % texture->anim_total[ent->frame != 0]) : 0];
720         currentalpha = ent->alpha;
721         if (surf->flags & SURF_WATERALPHA)
722                 currentalpha *= r_wateralpha.value;
723
724         GL_DepthTest(!(ent->effects & EF_NODEPTHTEST));
725         if (ent->effects & EF_ADDITIVE)
726         {
727                 rendertype = SURFRENDER_ADD;
728                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
729                 GL_DepthMask(false);
730         }
731         else if (currentalpha < 1 || texture->skin.fog != NULL)
732         {
733                 rendertype = SURFRENDER_ALPHA;
734                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
735                 GL_DepthMask(false);
736         }
737         else
738         {
739                 rendertype = SURFRENDER_OPAQUE;
740                 GL_BlendFunc(GL_ONE, GL_ZERO);
741                 GL_DepthMask(!(ent->effects & EF_NODEPTHTEST));
742         }
743
744         turb = (surf->flags & SURF_DRAWTURB) && r_waterscroll.value;
745         fullbright = !(ent->flags & RENDER_LIGHT) || (surf->flags & SURF_DRAWFULLBRIGHT) || !surf->samples;
746         base = fullbright ? 2.0f : r_ambient.value * (1.0f / 64.0f);
747         if (surf->flags & SURF_DRAWTURB)
748                 base *= 0.5f;
749         if ((surf->flags & SURF_DRAWTURB) && gl_textureshader && r_watershader.value && !fogenabled && fullbright && ent->colormod[0] == 1 && ent->colormod[1] == 1 && ent->colormod[2] == 1)
750         {
751                 // NVIDIA Geforce3 distortion texture shader on water
752                 GL_Color(1, 1, 1, currentalpha);
753                 memset(&m, 0, sizeof(m));
754                 m.pointer_vertex = surf->mesh.data_vertex3f;
755                 m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(r_refdef.time * 16)&63]);
756                 m.tex[1] = R_GetTexture(texture->skin.base);
757                 m.texcombinergb[0] = GL_REPLACE;
758                 m.texcombinergb[1] = GL_REPLACE;
759                 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
760                 m.pointer_texcoord[1] = surf->mesh.data_texcoordtexture2f;
761                 Matrix4x4_CreateFromQuakeEntity(&m.texmatrix[0], 0, 0, 0, 0, 0, 0, r_watershader.value);
762                 Matrix4x4_CreateTranslate(&m.texmatrix[1], sin(r_refdef.time) * 0.025 * r_waterscroll.value, sin(r_refdef.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
763                 R_Mesh_State(&m);
764
765                 GL_ActiveTexture(0);
766                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
767                 GL_ActiveTexture(1);
768                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
769                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
770                 qglTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
771                 qglEnable(GL_TEXTURE_SHADER_NV);
772
773                 GL_LockArrays(0, surf->mesh.num_vertices);
774                 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
775                 GL_LockArrays(0, 0);
776
777                 qglDisable(GL_TEXTURE_SHADER_NV);
778                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
779                 GL_ActiveTexture(0);
780         }
781         else
782         {
783                 memset(&m, 0, sizeof(m));
784                 m.pointer_vertex = surf->mesh.data_vertex3f;
785                 m.pointer_color = varray_color4f;
786                 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
787                 m.tex[0] = R_GetTexture(texture->skin.base);
788                 if (turb)
789                 {
790                         // scrolling in texture matrix
791                         Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(r_refdef.time) * 0.025 * r_waterscroll.value, sin(r_refdef.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
792                 }
793                 colorscale = 1;
794                 if (gl_combine.integer)
795                 {
796                         m.texrgbscale[0] = 4;
797                         colorscale *= 0.25f;
798                 }
799                 R_FillColors(varray_color4f, surf->mesh.num_vertices, base * ent->colormod[0], base * ent->colormod[1], base * ent->colormod[2], currentalpha);
800                 if (!fullbright)
801                 {
802                         if (surf->dlightframe == r_framecount)
803                                 RSurf_LightSeparate_Vertex3f_Color4f(&ent->inversematrix, surf->dlightbits, surf->mesh.num_vertices, surf->mesh.data_vertex3f, varray_color4f, 1);
804                         if (surf->samples)
805                                 RSurf_AddLightmapToVertexColors_Color4f(surf->mesh.data_lightmapoffsets, varray_color4f,surf->mesh.num_vertices, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
806                 }
807                 RSurf_FogColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, colorscale, surf->mesh.num_vertices, modelorg);
808                 R_Mesh_State(&m);
809                 GL_LockArrays(0, surf->mesh.num_vertices);
810                 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
811                 GL_LockArrays(0, 0);
812                 if (texture->skin.glow)
813                 {
814                         memset(&m, 0, sizeof(m));
815                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
816                         GL_DepthMask(false);
817                         m.pointer_color = varray_color4f;
818                         m.tex[0] = R_GetTexture(texture->skin.glow);
819                         m.pointer_vertex = surf->mesh.data_vertex3f;
820                         if (m.tex[0])
821                         {
822                                 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
823                                 if (turb)
824                                 {
825                                         // scrolling in texture matrix
826                                         Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(r_refdef.time) * 0.025 * r_waterscroll.value, sin(r_refdef.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
827                                 }
828                         }
829                         R_Mesh_State(&m);
830                         RSurf_FoggedColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, 1, 1, currentalpha, 1, surf->mesh.num_vertices, modelorg);
831                         GL_LockArrays(0, surf->mesh.num_vertices);
832                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
833                         GL_LockArrays(0, 0);
834                 }
835                 if (fogenabled && rendertype != SURFRENDER_ADD)
836                 {
837                         memset(&m, 0, sizeof(m));
838                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
839                         GL_DepthMask(false);
840                         m.pointer_color = varray_color4f;
841                         m.tex[0] = R_GetTexture(texture->skin.fog);
842                         m.pointer_vertex = surf->mesh.data_vertex3f;
843                         if (m.tex[0])
844                         {
845                                 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
846                                 if (turb)
847                                 {
848                                         // scrolling in texture matrix
849                                         Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(r_refdef.time) * 0.025 * r_waterscroll.value, sin(r_refdef.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
850                                 }
851                         }
852                         R_Mesh_State(&m);
853                         RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, 1, surf->mesh.num_vertices, modelorg);
854                         GL_LockArrays(0, surf->mesh.num_vertices);
855                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
856                         GL_LockArrays(0, 0);
857                 }
858         }
859 }
860
861 void R_UpdateTextureInfo(entity_render_t *ent)
862 {
863         int i, texframe, alttextures;
864         texture_t *t;
865
866         if (!ent->model)
867                 return;
868
869         alttextures = ent->frame != 0;
870         texframe = (int)(r_refdef.time * 5.0f);
871         for (i = 0;i < ent->model->brush.num_textures;i++)
872         {
873                 t = ent->model->brush.data_textures + i;
874                 t->currentalpha = ent->alpha;
875                 if (t->flags & SURF_WATERALPHA)
876                         t->currentalpha *= r_wateralpha.value;
877                 if (ent->effects & EF_ADDITIVE)
878                         t->rendertype = SURFRENDER_ADD;
879                 else if (t->currentalpha < 1 || t->skin.fog != NULL)
880                         t->rendertype = SURFRENDER_ALPHA;
881                 else
882                         t->rendertype = SURFRENDER_OPAQUE;
883                 // we don't need to set currentframe if t->animated is false because
884                 // it was already set up by the texture loader for non-animating
885                 if (t->animated)
886                         t->currentframe = t->anim_frames[alttextures][(t->anim_total[alttextures] >= 2) ? (texframe % t->anim_total[alttextures]) : 0];
887         }
888 }
889
890 void R_DrawSurfaceList(entity_render_t *ent, texture_t *texture, int texturenumsurfaces, msurface_t **texturesurfacelist)
891 {
892         int texturesurfaceindex;
893         vec3_t center, modelorg;
894         rmeshstate_t m;
895         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
896         if (gl_lightmaps.integer)
897         {
898                 GL_BlendFunc(GL_ONE, GL_ZERO);
899                 GL_DepthMask(true);
900                 GL_DepthTest(true);
901                 GL_Color(1, 1, 1, 1);
902                 memset(&m, 0, sizeof(m));
903                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
904                 {
905                         msurface_t *surface = texturesurfacelist[texturesurfaceindex];
906                         m.tex[0] = R_GetTexture(surface->lightmaptexture);
907                         m.pointer_vertex = surface->mesh.data_vertex3f;
908                         m.pointer_texcoord[0] = surface->mesh.data_texcoordlightmap2f;
909                         R_Mesh_State(&m);
910                         GL_LockArrays(0, surface->mesh.num_vertices);
911                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
912                         GL_LockArrays(0, 0);
913                 }
914         }
915         else if (texture->rendertype != SURFRENDER_OPAQUE)
916         {
917                 // transparent vertex shaded from lightmap
918                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
919                 {
920                         msurface_t *surface = texturesurfacelist[texturesurfaceindex];
921                         vec3_t tempcenter;
922                         tempcenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
923                         tempcenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
924                         tempcenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
925                         Matrix4x4_Transform(&ent->matrix, tempcenter, center);
926                         R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_vieworigin : center, RSurfShader_Transparent_Callback, ent, surface - ent->model->brushq1.surfaces);
927                 }
928         }
929         else if (texture->flags & SURF_LIGHTMAP)
930         {
931                 qboolean dolightmap = (ent->flags & RENDER_LIGHT);
932                 qboolean dobase = true;
933                 qboolean doambient = r_ambient.value > 0;
934                 qboolean dodetail = r_detailtextures.integer != 0;
935                 qboolean doglow = texture->skin.glow != NULL;
936                 qboolean dofog = fogenabled;
937                 // multitexture cases
938                 if (r_textureunits.integer >= 2 && gl_combine.integer && dobase && dolightmap)
939                 {
940                         dobase = false;
941                         dolightmap = false;
942                         GL_BlendFunc(GL_ONE, GL_ZERO);
943                         GL_DepthMask(true);
944                         GL_DepthTest(true);
945                         GL_Color(1, 1, 1, 1);
946                         GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
947                         memset(&m, 0, sizeof(m));
948                         m.tex[0] = R_GetTexture(texture->skin.base);
949                         m.texrgbscale[1] = 2;
950                         if (r_textureunits.integer >= 3 && !doambient && dodetail)
951                         {
952                                 m.tex[2] = R_GetTexture(texture->skin.detail);
953                                 m.texrgbscale[2] = 2;
954                                 dodetail = false;
955                                 if (r_textureunits.integer >= 3 && texture->skin.glow)
956                                 {
957                                         m.tex[3] = R_GetTexture(texture->skin.glow);
958                                         m.texcombinergb[3] = GL_ADD;
959                                         doglow = false;
960                                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
961                                         {
962                                                 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
963                                                 m.tex[1] = R_GetTexture(surface->lightmaptexture);
964                                                 m.pointer_vertex = surface->mesh.data_vertex3f;
965                                                 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
966                                                 m.pointer_texcoord[1] = surface->mesh.data_texcoordlightmap2f;
967                                                 m.pointer_texcoord[2] = surface->mesh.data_texcoorddetail2f;
968                                                 m.pointer_texcoord[3] = surface->mesh.data_texcoordtexture2f;
969                                                 R_Mesh_State(&m);
970                                                 GL_LockArrays(0, surface->mesh.num_vertices);
971                                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
972                                                 GL_LockArrays(0, 0);
973                                         }
974                                 }
975                                 else
976                                 {
977                                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
978                                         {
979                                                 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
980                                                 m.tex[1] = R_GetTexture(surface->lightmaptexture);
981                                                 m.pointer_vertex = surface->mesh.data_vertex3f;
982                                                 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
983                                                 m.pointer_texcoord[1] = surface->mesh.data_texcoordlightmap2f;
984                                                 m.pointer_texcoord[2] = surface->mesh.data_texcoorddetail2f;
985                                                 R_Mesh_State(&m);
986                                                 GL_LockArrays(0, surface->mesh.num_vertices);
987                                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
988                                                 GL_LockArrays(0, 0);
989                                         }
990                                 }
991                         }
992                         else if (r_textureunits.integer >= 3 && !doambient && !dodetail && doglow)
993                         {
994                                 m.tex[2] = R_GetTexture(texture->skin.glow);
995                                 m.texcombinergb[2] = GL_ADD;
996                                 doglow = false;
997                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
998                                 {
999                                         msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1000                                         m.tex[1] = R_GetTexture(surface->lightmaptexture);
1001                                         m.pointer_vertex = surface->mesh.data_vertex3f;
1002                                         m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1003                                         m.pointer_texcoord[1] = surface->mesh.data_texcoordlightmap2f;
1004                                         m.pointer_texcoord[2] = surface->mesh.data_texcoordtexture2f;
1005                                         R_Mesh_State(&m);
1006                                         GL_LockArrays(0, surface->mesh.num_vertices);
1007                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1008                                         GL_LockArrays(0, 0);
1009                                 }
1010                         }
1011                 }
1012                 // anything not handled above
1013                 if (dobase)
1014                 {
1015                         GL_BlendFunc(GL_ONE, GL_ZERO);
1016                         GL_DepthMask(true);
1017                         GL_DepthTest(true);
1018                         GL_Color(1, 1, 1, 1);
1019                         if (ent->flags & RENDER_LIGHT)
1020                                 GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
1021                         else
1022                                 GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], 1);
1023                         memset(&m, 0, sizeof(m));
1024                         m.tex[0] = R_GetTexture(texture->skin.base);
1025                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1026                         {
1027                                 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1028                                 m.pointer_vertex = surface->mesh.data_vertex3f;
1029                                 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1030                                 R_Mesh_State(&m);
1031                                 GL_LockArrays(0, surface->mesh.num_vertices);
1032                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1033                                 GL_LockArrays(0, 0);
1034                         }
1035                 }
1036                 GL_DepthMask(false);
1037                 if (dolightmap)
1038                 {
1039                         GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1040                         GL_DepthMask(false);
1041                         GL_DepthTest(true);
1042                         GL_Color(1, 1, 1, 1);
1043                         memset(&m, 0, sizeof(m));
1044                         m.tex[0] = R_GetTexture(texture->skin.base);
1045                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1046                         {
1047                                 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1048                                 m.tex[0] = R_GetTexture(surface->lightmaptexture);
1049                                 m.pointer_vertex = surface->mesh.data_vertex3f;
1050                                 m.pointer_texcoord[0] = surface->mesh.data_texcoordlightmap2f;
1051                                 R_Mesh_State(&m);
1052                                 GL_LockArrays(0, surface->mesh.num_vertices);
1053                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1054                                 GL_LockArrays(0, 0);
1055                         }
1056                 }
1057                 if (doambient)
1058                 {
1059                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1060                         GL_DepthMask(false);
1061                         GL_DepthTest(true);
1062                         memset(&m, 0, sizeof(m));
1063                         GL_Color(r_ambient.value * (1.0f / 128.0f) * ent->colormod[0], r_ambient.value * (1.0f / 128.0f) * ent->colormod[1], r_ambient.value * (1.0f / 128.0f) * ent->colormod[2], 1);
1064                         m.tex[0] = R_GetTexture(texture->skin.base);
1065                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1066                         {
1067                                 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1068                                 m.pointer_vertex = surface->mesh.data_vertex3f;
1069                                 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1070                                 R_Mesh_State(&m);
1071                                 GL_LockArrays(0, surface->mesh.num_vertices);
1072                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1073                                 GL_LockArrays(0, 0);
1074                         }
1075                 }
1076                 if (dodetail)
1077                 {
1078                         GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1079                         GL_DepthMask(false);
1080                         GL_DepthTest(true);
1081                         GL_Color(1, 1, 1, 1);
1082                         memset(&m, 0, sizeof(m));
1083                         m.tex[0] = R_GetTexture(texture->skin.detail);
1084                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1085                         {
1086                                 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1087                                 m.pointer_vertex = surface->mesh.data_vertex3f;
1088                                 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1089                                 R_Mesh_State(&m);
1090                                 GL_LockArrays(0, surface->mesh.num_vertices);
1091                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1092                                 GL_LockArrays(0, 0);
1093                         }
1094                 }
1095                 if (doglow)
1096                 {
1097                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1098                         GL_DepthMask(false);
1099                         GL_DepthTest(true);
1100                         GL_Color(1, 1, 1, 1);
1101                         memset(&m, 0, sizeof(m));
1102                         m.tex[0] = R_GetTexture(texture->skin.glow);
1103                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1104                         {
1105                                 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1106                                 m.pointer_vertex = surface->mesh.data_vertex3f;
1107                                 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1108                                 R_Mesh_State(&m);
1109                                 GL_LockArrays(0, surface->mesh.num_vertices);
1110                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1111                                 GL_LockArrays(0, 0);
1112                         }
1113                 }
1114                 if (dofog)
1115                 {
1116                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1117                         GL_DepthMask(false);
1118                         GL_DepthTest(true);
1119                         memset(&m, 0, sizeof(m));
1120                         m.pointer_color = varray_color4f;
1121                         m.tex[0] = R_GetTexture(texture->skin.glow);
1122                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1123                         {
1124                                 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1125                                 m.pointer_vertex = surface->mesh.data_vertex3f;
1126                                 if (m.tex[0])
1127                                         m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1128                                 R_Mesh_State(&m);
1129                                 RSurf_FogPassColors_Vertex3f_Color4f(surface->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], 1, 1, surface->mesh.num_vertices, modelorg);
1130                                 GL_LockArrays(0, surface->mesh.num_vertices);
1131                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1132                                 GL_LockArrays(0, 0);
1133                         }
1134                 }
1135         }
1136         else if (texture->flags & SURF_DRAWTURB)
1137         {
1138                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1139                 {
1140                         msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1141                         RSurfShader_Transparent_Callback(ent, surface - ent->model->brushq1.surfaces);
1142                 }
1143         }
1144         else if (texture->flags & SURF_DRAWSKY)
1145         {
1146                 if (skyrendernow)
1147                 {
1148                         skyrendernow = false;
1149                         if (skyrendermasked)
1150                                 R_Sky();
1151                 }
1152                 // LordHavoc: HalfLife maps have freaky skypolys...
1153                 if (!ent->model->brush.ishlbsp)
1154                 {
1155                         R_Mesh_Matrix(&ent->matrix);
1156                         GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
1157                         if (skyrendermasked)
1158                         {
1159                                 // depth-only (masking)
1160                                 GL_ColorMask(0,0,0,0);
1161                                 // just to make sure that braindead drivers don't draw anything
1162                                 // despite that colormask...
1163                                 GL_BlendFunc(GL_ZERO, GL_ONE);
1164                         }
1165                         else
1166                         {
1167                                 // fog sky
1168                                 GL_BlendFunc(GL_ONE, GL_ZERO);
1169                         }
1170                         GL_DepthMask(true);
1171                         GL_DepthTest(true);
1172                         memset(&m, 0, sizeof(m));
1173                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1174                         {
1175                                 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1176                                 m.pointer_vertex = surface->mesh.data_vertex3f;
1177                                 R_Mesh_State(&m);
1178                                 GL_LockArrays(0, surface->mesh.num_vertices);
1179                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1180                                 GL_LockArrays(0, 0);
1181                         }
1182                         GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
1183                 }
1184         }
1185 }
1186
1187 void R_DrawSurfaces(entity_render_t *ent, qboolean skysurfaces)
1188 {
1189         int i, j, f, flagsmask;
1190         msurface_t *surface, **surfacechain;
1191         texture_t *t, *texture;
1192         model_t *model = ent->model;
1193         vec3_t modelorg;
1194         const int maxsurfacelist = 1024;
1195         int numsurfacelist = 0;
1196         msurface_t *surfacelist[1024];
1197         if (model == NULL)
1198                 return;
1199         R_Mesh_Matrix(&ent->matrix);
1200         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1201
1202         if (ent != r_refdef.worldentity)
1203         {
1204                 // because bmodels can be reused, we have to clear dlightframe every time
1205                 surface = model->brushq1.surfaces + model->firstmodelsurface;
1206                 for (i = 0;i < model->nummodelsurfaces;i++, surface++)
1207                         surface->dlightframe = -1;
1208         }
1209
1210         // update light styles
1211         if (!skysurfaces)
1212         {
1213                 if (r_dynamic.integer && !r_rtdlight)
1214                         R_MarkLights(ent);
1215                 for (i = 0;i < model->brushq1.light_styles;i++)
1216                 {
1217                         if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1218                         {
1219                                 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1220                                 if ((surfacechain = model->brushq1.light_styleupdatechains[i]))
1221                                         for (;(surface = *surfacechain);surfacechain++)
1222                                                 surface->cached_dlight = true;
1223                         }
1224                 }
1225         }
1226
1227         R_UpdateTextureInfo(ent);
1228         flagsmask = skysurfaces ? SURF_DRAWSKY : (SURF_DRAWTURB | SURF_LIGHTMAP);
1229         f = 0;
1230         t = NULL;
1231         numsurfacelist = 0;
1232         for (i = 0, j = model->firstmodelsurface;i < model->nummodelsurfaces;i++, j++)
1233         {
1234                 if (ent != r_refdef.worldentity || r_worldsurfacevisible[j])
1235                 {
1236                         surface = model->brushq1.surfaces + j;
1237                         if (t != surface->texinfo->texture)
1238                         {
1239                                 if (numsurfacelist)
1240                                 {
1241                                         R_DrawSurfaceList(ent, texture, numsurfacelist, surfacelist);
1242                                         numsurfacelist = 0;
1243                                 }
1244                                 t = surface->texinfo->texture;
1245                                 f = t->flags & flagsmask;
1246                                 texture = t->currentframe;
1247                         }
1248                         if (f)
1249                         {
1250                                 // add face to draw list and update lightmap if necessary
1251                                 c_faces++;
1252                                 if (surface->cached_dlight && surface->lightmaptexture != NULL)
1253                                         R_BuildLightMap(ent, surface);
1254                                 surfacelist[numsurfacelist++] = surface;
1255                                 if (numsurfacelist >= maxsurfacelist)
1256                                 {
1257                                         R_DrawSurfaceList(ent, texture, numsurfacelist, surfacelist);
1258                                         numsurfacelist = 0;
1259                                 }
1260                         }
1261                 }
1262         }
1263         if (numsurfacelist)
1264                 R_DrawSurfaceList(ent, texture, numsurfacelist, surfacelist);
1265 }
1266
1267 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1268 {
1269         int i;
1270         float *v;
1271         rmeshstate_t m;
1272         const mportal_t *portal = calldata1;
1273         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1274         GL_DepthMask(false);
1275         GL_DepthTest(true);
1276         R_Mesh_Matrix(&r_identitymatrix);
1277
1278         memset(&m, 0, sizeof(m));
1279         m.pointer_vertex = varray_vertex3f;
1280         R_Mesh_State(&m);
1281
1282         i = calldata2;
1283         GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1284                          ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1285                          ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1286                          0.125f);
1287         if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1288         {
1289                 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1290                         VectorCopy(portal->points[i].position, v);
1291         }
1292         else
1293                 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1294                         VectorCopy(portal->points[i].position, v);
1295         GL_LockArrays(0, portal->numpoints);
1296         R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1297         GL_LockArrays(0, 0);
1298 }
1299
1300 // LordHavoc: this is just a nice debugging tool, very slow
1301 static void R_DrawPortals(void)
1302 {
1303         int i, portalnum;
1304         mportal_t *portal;
1305         float center[3], f;
1306         model_t *model = r_refdef.worldmodel;
1307         if (model == NULL)
1308                 return;
1309         for (portalnum = 0, portal = model->brush.data_portals;portalnum < model->brush.num_portals;portalnum++, portal++)
1310         {
1311                 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1312                 if (!R_CullBox(portal->mins, portal->maxs))
1313                 {
1314                         VectorClear(center);
1315                         for (i = 0;i < portal->numpoints;i++)
1316                                 VectorAdd(center, portal->points[i].position, center);
1317                         f = ixtable[portal->numpoints];
1318                         VectorScale(center, f, center);
1319                         R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, portal, portalnum);
1320                 }
1321         }
1322 }
1323
1324 void R_WorldVisibility(void)
1325 {
1326         model_t *model = r_refdef.worldmodel;
1327
1328         if (!model)
1329                 return;
1330
1331         if (model->type == mod_brushq3)
1332         {
1333                 int i, j;
1334                 mleaf_t *leaf;
1335                 memset(r_worldsurfacevisible, 0, r_refdef.worldmodel->brushq3.num_faces);
1336                 for (j = 0, leaf = r_refdef.worldmodel->brush.data_leafs;j < r_refdef.worldmodel->brush.num_leafs;j++, leaf++)
1337                 {
1338                         if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
1339                         {
1340                                 c_leafs++;
1341                                 for (i = 0;i < leaf->numleaffaces;i++)
1342                                         r_worldsurfacevisible[leaf->firstleafface[i]] = 1;
1343                         }
1344                 }
1345         }
1346         else if (model->type == mod_brushq1)
1347         {
1348                 int i, j, *mark;
1349                 mleaf_t *leaf;
1350                 mleaf_t *viewleaf;
1351                 int leafstackpos;
1352                 mportal_t *p;
1353                 mleaf_t *leafstack[8192];
1354                 qbyte leafvisited[32768];
1355
1356                 viewleaf = model->brushq1.PointInLeaf(model, r_vieworigin);
1357                 if (!viewleaf)
1358                         return;
1359
1360                 memset(r_worldsurfacevisible, 0, r_refdef.worldmodel->brushq1.numsurfaces);
1361                 if (viewleaf->clusterindex < 0 || r_surfaceworldnode.integer)
1362                 {
1363                         // equivilant to quake's RecursiveWorldNode but faster and more effective
1364                         for (j = 0, leaf = model->brush.data_leafs;j < model->brush.num_leafs;j++, leaf++)
1365                         {
1366                                 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox (leaf->mins, leaf->maxs))
1367                                 {
1368                                         c_leafs++;
1369                                         if (leaf->numleaffaces)
1370                                                 for (i = 0, mark = leaf->firstleafface;i < leaf->numleaffaces;i++, mark++)
1371                                                         r_worldsurfacevisible[*mark] = true;
1372                                 }
1373                         }
1374                 }
1375                 else
1376                 {
1377                         // LordHavoc: portal-passage worldnode with PVS;
1378                         // follows portals leading outward from viewleaf, does not venture
1379                         // offscreen or into leafs that are not visible, faster than Quake's
1380                         // RecursiveWorldNode
1381                         leafstack[0] = viewleaf;
1382                         leafstackpos = 1;
1383                         memset(leafvisited, 0, r_refdef.worldmodel->brush.num_leafs);
1384                         while (leafstackpos)
1385                         {
1386                                 c_leafs++;
1387                                 leaf = leafstack[--leafstackpos];
1388                                 leafvisited[leaf - r_refdef.worldmodel->brush.data_leafs] = 1;
1389                                 // draw any surfaces bounding this leaf
1390                                 if (leaf->numleaffaces)
1391                                         for (i = 0, mark = leaf->firstleafface;i < leaf->numleaffaces;i++, mark++)
1392                                                 r_worldsurfacevisible[*mark] = true;
1393                                 // follow portals into other leafs
1394                                 for (p = leaf->portals;p;p = p->next)
1395                                         if (DotProduct(r_vieworigin, p->plane.normal) < (p->plane.dist + 1) && !leafvisited[p->past - r_refdef.worldmodel->brush.data_leafs] && CHECKPVSBIT(r_pvsbits, p->past->clusterindex) && !R_CullBox(p->mins, p->maxs))
1396                                                 leafstack[leafstackpos++] = p->past;
1397                         }
1398                 }
1399
1400                 if (r_drawportals.integer)
1401                         R_DrawPortals();
1402         }
1403 }
1404
1405 void R_Q1BSP_DrawSky(entity_render_t *ent)
1406 {
1407         if (ent->model == NULL)
1408                 return;
1409         R_DrawSurfaces(ent, true);
1410 }
1411
1412 void R_Q1BSP_Draw(entity_render_t *ent)
1413 {
1414         if (ent->model == NULL)
1415                 return;
1416         c_bmodels++;
1417         R_DrawSurfaces(ent, false);
1418 }
1419
1420 void R_Q1BSP_GetLightInfo(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, vec3_t outmins, vec3_t outmaxs, int *outclusterlist, qbyte *outclusterpvs, int *outnumclusterspointer, int *outsurfacelist, qbyte *outsurfacepvs, int *outnumsurfacespointer)
1421 {
1422         model_t *model = ent->model;
1423         vec3_t lightmins, lightmaxs;
1424         int t, leafindex, leaffaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
1425         const int *e;
1426         const float *v[3];
1427         msurface_t *surface;
1428         mleaf_t *leaf;
1429         const qbyte *pvs;
1430         lightmins[0] = relativelightorigin[0] - lightradius;
1431         lightmins[1] = relativelightorigin[1] - lightradius;
1432         lightmins[2] = relativelightorigin[2] - lightradius;
1433         lightmaxs[0] = relativelightorigin[0] + lightradius;
1434         lightmaxs[1] = relativelightorigin[1] + lightradius;
1435         lightmaxs[2] = relativelightorigin[2] + lightradius;
1436         *outnumclusterspointer = 0;
1437         *outnumsurfacespointer = 0;
1438         memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
1439         memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
1440         if (model == NULL)
1441         {
1442                 VectorCopy(lightmins, outmins);
1443                 VectorCopy(lightmaxs, outmaxs);
1444                 return;
1445         }
1446         VectorCopy(relativelightorigin, outmins);
1447         VectorCopy(relativelightorigin, outmaxs);
1448         if (model->brush.GetPVS)
1449                 pvs = model->brush.GetPVS(model, relativelightorigin);
1450         else
1451                 pvs = NULL;
1452         // FIXME: use BSP recursion as lights are often small
1453         for (leafindex = 0, leaf = model->brush.data_leafs;leafindex < model->brush.num_leafs;leafindex++, leaf++)
1454         {
1455                 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
1456                 {
1457                         outmins[0] = min(outmins[0], leaf->mins[0]);
1458                         outmins[1] = min(outmins[1], leaf->mins[1]);
1459                         outmins[2] = min(outmins[2], leaf->mins[2]);
1460                         outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
1461                         outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
1462                         outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
1463                         if (outclusterpvs)
1464                         {
1465                                 if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
1466                                 {
1467                                         SETPVSBIT(outclusterpvs, leaf->clusterindex);
1468                                         outclusterlist[outnumclusters++] = leaf->clusterindex;
1469                                 }
1470                         }
1471                         if (outsurfacepvs)
1472                         {
1473                                 for (leaffaceindex = 0;leaffaceindex < leaf->numleaffaces;leaffaceindex++)
1474                                 {
1475                                         surfaceindex = leaf->firstleafface[leaffaceindex];
1476                                         if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
1477                                         {
1478                                                 surface = model->brushq1.surfaces + surfaceindex;
1479                                                 if (BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs) && (surface->flags & SURF_LIGHTMAP) && !surface->texinfo->texture->skin.fog)
1480                                                 {
1481                                                         for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
1482                                                         {
1483                                                                 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1484                                                                 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1485                                                                 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1486                                                                 if (PointInfrontOfTriangle(relativelightorigin, v[0], v[1], v[2]) && lightmaxs[0] > min(v[0][0], min(v[1][0], v[2][0])) && lightmins[0] < max(v[0][0], max(v[1][0], v[2][0])) && lightmaxs[1] > min(v[0][1], min(v[1][1], v[2][1])) && lightmins[1] < max(v[0][1], max(v[1][1], v[2][1])) && lightmaxs[2] > min(v[0][2], min(v[1][2], v[2][2])) && lightmins[2] < max(v[0][2], max(v[1][2], v[2][2])))
1487                                                                 {
1488                                                                         SETPVSBIT(outsurfacepvs, surfaceindex);
1489                                                                         outsurfacelist[outnumsurfaces++] = surfaceindex;
1490                                                                         break;
1491                                                                 }
1492                                                         }
1493                                                 }
1494                                         }
1495                                 }
1496                         }
1497                 }
1498         }
1499
1500         // limit combined leaf box to light boundaries
1501         outmins[0] = max(outmins[0], lightmins[0]);
1502         outmins[1] = max(outmins[1], lightmins[1]);
1503         outmins[2] = max(outmins[2], lightmins[2]);
1504         outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
1505         outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
1506         outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
1507
1508         *outnumclusterspointer = outnumclusters;
1509         *outnumsurfacespointer = outnumsurfaces;
1510 }
1511
1512 void R_Q1BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
1513 {
1514         model_t *model = ent->model;
1515         vec3_t lightmins, lightmaxs;
1516         msurface_t *surface;
1517         int surfacelistindex;
1518         if (r_drawcollisionbrushes.integer < 2)
1519         {
1520                 lightmins[0] = relativelightorigin[0] - lightradius;
1521                 lightmins[1] = relativelightorigin[1] - lightradius;
1522                 lightmins[2] = relativelightorigin[2] - lightradius;
1523                 lightmaxs[0] = relativelightorigin[0] + lightradius;
1524                 lightmaxs[1] = relativelightorigin[1] + lightradius;
1525                 lightmaxs[2] = relativelightorigin[2] + lightradius;
1526                 R_Mesh_Matrix(&ent->matrix);
1527                 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
1528                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1529                 {
1530                         surface = model->brushq1.surfaces + surfacelist[surfacelistindex];
1531                         R_Shadow_MarkVolumeFromBox(surface->num_firstshadowmeshtriangle, surface->mesh.num_triangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, relativelightorigin, lightmins, lightmaxs, surface->mins, surface->maxs);
1532                 }
1533                 R_Shadow_VolumeFromList(model->brush.shadowmesh->numverts, model->brush.shadowmesh->numtriangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, model->brush.shadowmesh->neighbor3i, relativelightorigin, lightradius + model->radius + r_shadow_projectdistance.value, numshadowmark, shadowmarklist);
1534         }
1535 }
1536
1537 void R_Q1BSP_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap, vec_t ambientscale, vec_t diffusescale, vec_t specularscale, int numsurfaces, const int *surfacelist)
1538 {
1539         model_t *model = ent->model;
1540         vec3_t lightmins, lightmaxs, modelorg;
1541         msurface_t *surface;
1542         texture_t *t;
1543         int surfacelistindex;
1544         if (r_drawcollisionbrushes.integer < 2)
1545         {
1546                 lightmins[0] = relativelightorigin[0] - lightradius;
1547                 lightmins[1] = relativelightorigin[1] - lightradius;
1548                 lightmins[2] = relativelightorigin[2] - lightradius;
1549                 lightmaxs[0] = relativelightorigin[0] + lightradius;
1550                 lightmaxs[1] = relativelightorigin[1] + lightradius;
1551                 lightmaxs[2] = relativelightorigin[2] + lightradius;
1552                 R_Mesh_Matrix(&ent->matrix);
1553                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1554                 R_UpdateTextureInfo(ent);
1555                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1556                 {
1557                         surface = model->brushq1.surfaces + surfacelist[surfacelistindex];
1558                         if (r_shadow_compilingrtlight)
1559                         {
1560                                 // if compiling an rtlight, capture the mesh
1561                                 t = surface->texinfo->texture;
1562                                 if (t->flags & SURF_LIGHTMAP && t->skin.fog == NULL)
1563                                         Mod_ShadowMesh_AddMesh(r_shadow_mempool, r_shadow_compilingrtlight->static_meshchain_light, surface->texinfo->texture->skin.base, surface->texinfo->texture->skin.gloss, surface->texinfo->texture->skin.nmap, surface->mesh.data_vertex3f, surface->mesh.data_svector3f, surface->mesh.data_tvector3f, surface->mesh.data_normal3f, surface->mesh.data_texcoordtexture2f, surface->mesh.num_triangles, surface->mesh.data_element3i);
1564                         }
1565                         else if (ent != r_refdef.worldentity || r_worldsurfacevisible[surface - ent->model->brushq1.surfaces])
1566                         {
1567                                 t = surface->texinfo->texture->currentframe;
1568                                 // FIXME: transparent surfaces need to be lit later
1569                                 if (t->flags & SURF_LIGHTMAP && t->rendertype == SURFRENDER_OPAQUE)
1570                                         R_Shadow_RenderLighting(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i, surface->mesh.data_vertex3f, surface->mesh.data_svector3f, surface->mesh.data_tvector3f, surface->mesh.data_normal3f, surface->mesh.data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.base, t->skin.nmap, t->skin.gloss, lightcubemap, ambientscale, diffusescale, specularscale);
1571                         }
1572                 }
1573         }
1574 }
1575
1576 void R_DrawCollisionBrush(colbrushf_t *brush)
1577 {
1578         int i;
1579         rmeshstate_t m;
1580         memset(&m, 0, sizeof(m));
1581         m.pointer_vertex = brush->points->v;
1582         R_Mesh_State(&m);
1583         i = (int)(((size_t)brush) / sizeof(colbrushf_t));
1584         GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1585         GL_LockArrays(0, brush->numpoints);
1586         R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1587         GL_LockArrays(0, 0);
1588 }
1589
1590 void R_Q3BSP_DrawCollisionFace(entity_render_t *ent, q3msurface_t *face)
1591 {
1592         int i;
1593         rmeshstate_t m;
1594         if (!face->mesh.num_collisiontriangles)
1595                 return;
1596         memset(&m, 0, sizeof(m));
1597         m.pointer_vertex = face->mesh.data_collisionvertex3f;
1598         R_Mesh_State(&m);
1599         i = (int)(((size_t)face) / sizeof(q3msurface_t));
1600         GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1601         GL_LockArrays(0, face->mesh.num_collisionvertices);
1602         R_Mesh_Draw(face->mesh.num_collisionvertices, face->mesh.num_collisiontriangles, face->mesh.data_collisionelement3i);
1603         GL_LockArrays(0, 0);
1604 }
1605
1606 void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int facenumber)
1607 {
1608         const entity_render_t *ent = voident;
1609         q3msurface_t *face = ent->model->brushq3.data_faces + facenumber;
1610         rmeshstate_t m;
1611         R_Mesh_Matrix(&ent->matrix);
1612         memset(&m, 0, sizeof(m));
1613         if ((ent->effects & EF_ADDITIVE) || (face->texture->textureflags & Q3TEXTUREFLAG_ADDITIVE))
1614                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1615         else
1616                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1617         GL_DepthMask(false);
1618         GL_DepthTest(!(ent->effects & EF_NODEPTHTEST));
1619         m.tex[0] = R_GetTexture(face->texture->skin.base);
1620         m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
1621         // LordHavoc: quake3 was not able to do this; lit transparent surfaces
1622         if (gl_combine.integer)
1623         {
1624                 m.texrgbscale[0] = 2;
1625                 if (r_textureunits.integer >= 2)
1626                 {
1627                         m.tex[1] = R_GetTexture(face->lightmaptexture);
1628                         m.pointer_texcoord[1] = face->mesh.data_texcoordlightmap2f;
1629                         GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], ent->alpha);
1630                 }
1631                 else
1632                 {
1633                         if (ent->colormod[0] == 1 && ent->colormod[1] == 1 && ent->colormod[2] == 1 && ent->alpha == 1)
1634                                 m.pointer_color = face->mesh.data_lightmapcolor4f;
1635                         else
1636                         {
1637                                 int i;
1638                                 for (i = 0;i < face->mesh.num_vertices;i++)
1639                                 {
1640                                         varray_color4f[i*4+0] = face->mesh.data_lightmapcolor4f[i*4+0] * ent->colormod[0];
1641                                         varray_color4f[i*4+1] = face->mesh.data_lightmapcolor4f[i*4+1] * ent->colormod[1];
1642                                         varray_color4f[i*4+2] = face->mesh.data_lightmapcolor4f[i*4+2] * ent->colormod[2];
1643                                         varray_color4f[i*4+3] = face->mesh.data_lightmapcolor4f[i*4+3] * ent->alpha;
1644                                 }
1645                                 m.pointer_color = varray_color4f;
1646                         }
1647                 }
1648         }
1649         else
1650         {
1651                 int i;
1652                 for (i = 0;i < face->mesh.num_vertices;i++)
1653                 {
1654                         varray_color4f[i*4+0] = face->mesh.data_lightmapcolor4f[i*4+0] * ent->colormod[0] * 2.0f;
1655                         varray_color4f[i*4+1] = face->mesh.data_lightmapcolor4f[i*4+1] * ent->colormod[1] * 2.0f;
1656                         varray_color4f[i*4+2] = face->mesh.data_lightmapcolor4f[i*4+2] * ent->colormod[2] * 2.0f;
1657                         varray_color4f[i*4+3] = face->mesh.data_lightmapcolor4f[i*4+3] * ent->alpha;
1658                 }
1659                 m.pointer_color = varray_color4f;
1660         }
1661         if (face->texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
1662         {
1663                 int i, j;
1664                 float center[3], center2[3], forward[3], right[3], up[3], v[4][3];
1665                 matrix4x4_t matrix1, imatrix1;
1666                 R_Mesh_Matrix(&r_identitymatrix);
1667                 // a single autosprite surface can contain multiple sprites...
1668                 for (j = 0;j < face->mesh.num_vertices - 3;j += 4)
1669                 {
1670                         VectorClear(center);
1671                         for (i = 0;i < 4;i++)
1672                                 VectorAdd(center, face->mesh.data_vertex3f + (j+i) * 3, center);
1673                         VectorScale(center, 0.25f, center);
1674                         Matrix4x4_Transform(&ent->matrix, center, center2);
1675                         // FIXME: calculate vectors from triangle edges instead of using texture vectors as an easy way out?
1676                         Matrix4x4_FromVectors(&matrix1, face->mesh.data_normal3f + j*3, face->mesh.data_svector3f + j*3, face->mesh.data_tvector3f + j*3, center);
1677                         Matrix4x4_Invert_Simple(&imatrix1, &matrix1);
1678                         for (i = 0;i < 4;i++)
1679                                 Matrix4x4_Transform(&imatrix1, face->mesh.data_vertex3f + (j+i)*3, v[i]);
1680                         if (face->texture->textureflags & Q3TEXTUREFLAG_AUTOSPRITE2)
1681                         {
1682                                 forward[0] = r_vieworigin[0] - center2[0];
1683                                 forward[1] = r_vieworigin[1] - center2[1];
1684                                 forward[2] = 0;
1685                                 VectorNormalize(forward);
1686                                 right[0] = forward[1];
1687                                 right[1] = -forward[0];
1688                                 right[2] = 0;
1689                                 up[0] = 0;
1690                                 up[1] = 0;
1691                                 up[2] = 1;
1692                         }
1693                         else
1694                         {
1695                                 VectorCopy(r_viewforward, forward);
1696                                 VectorCopy(r_viewright, right);
1697                                 VectorCopy(r_viewup, up);
1698                         }
1699                         for (i = 0;i < 4;i++)
1700                                 VectorMAMAMAM(1, center2, v[i][0], forward, v[i][1], right, v[i][2], up, varray_vertex3f + (i+j) * 3);
1701                 }
1702                 m.pointer_vertex = varray_vertex3f;
1703         }
1704         else
1705                 m.pointer_vertex = face->mesh.data_vertex3f;
1706         R_Mesh_State(&m);
1707         if (face->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1708                 qglDisable(GL_CULL_FACE);
1709         GL_LockArrays(0, face->mesh.num_vertices);
1710         R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1711         GL_LockArrays(0, 0);
1712         if (face->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1713                 qglEnable(GL_CULL_FACE);
1714 }
1715
1716 void R_Q3BSP_DrawFaceList(entity_render_t *ent, texture_t *t, int texturenumfaces, q3msurface_t **texturefacelist)
1717 {
1718         int i, texturefaceindex;
1719         qboolean dolightmap;
1720         qboolean dobase;
1721         qboolean doambient;
1722         qboolean doglow;
1723         qboolean dofog;
1724         rmeshstate_t m;
1725         if (!texturenumfaces)
1726                 return;
1727         c_faces += texturenumfaces;
1728         // gl_lightmaps debugging mode skips normal texturing
1729         if (gl_lightmaps.integer)
1730         {
1731                 GL_DepthMask(true);
1732                 GL_DepthTest(true);
1733                 GL_BlendFunc(GL_ONE, GL_ZERO);
1734                 qglDisable(GL_CULL_FACE);
1735                 memset(&m, 0, sizeof(m));
1736                 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1737                 {
1738                         q3msurface_t *face = texturefacelist[texturefaceindex];
1739                         m.tex[0] = R_GetTexture(face->lightmaptexture);
1740                         m.pointer_texcoord[0] = face->mesh.data_texcoordlightmap2f;
1741                         if (face->lightmaptexture)
1742                         {
1743                                 GL_Color(1, 1, 1, 1);
1744                                 m.pointer_color = NULL;
1745                         }
1746                         else
1747                                 m.pointer_color = face->mesh.data_lightmapcolor4f;
1748                         m.pointer_vertex = face->mesh.data_vertex3f;
1749                         R_Mesh_State(&m);
1750                         GL_LockArrays(0, face->mesh.num_vertices);
1751                         R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1752                         GL_LockArrays(0, 0);
1753                 }
1754                 qglEnable(GL_CULL_FACE);
1755                 return;
1756         }
1757         // transparent surfaces get sorted for later drawing
1758         if ((t->surfaceparms & Q3SURFACEPARM_TRANS) || ent->alpha < 1 || (ent->effects & EF_ADDITIVE))
1759         {
1760                 vec3_t facecenter, center;
1761                 // drawing sky transparently would be too difficult
1762                 if (t->surfaceparms & Q3SURFACEPARM_SKY)
1763                         return;
1764                 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1765                 {
1766                         q3msurface_t *face = texturefacelist[texturefaceindex];
1767                         facecenter[0] = (face->mins[0] + face->maxs[0]) * 0.5f;
1768                         facecenter[1] = (face->mins[1] + face->maxs[1]) * 0.5f;
1769                         facecenter[2] = (face->mins[2] + face->maxs[2]) * 0.5f;
1770                         Matrix4x4_Transform(&ent->matrix, facecenter, center);
1771                         R_MeshQueue_AddTransparent(center, R_Q3BSP_DrawFace_TransparentCallback, ent, face - ent->model->brushq3.data_faces);
1772                 }
1773                 return;
1774         }
1775         // sky surfaces draw sky if needed and render themselves as a depth mask
1776         if (t->surfaceparms & Q3SURFACEPARM_SKY)
1777         {
1778                 if (skyrendernow)
1779                 {
1780                         skyrendernow = false;
1781                         if (skyrendermasked)
1782                                 R_Sky();
1783                 }
1784                 if (!r_q3bsp_renderskydepth.integer)
1785                         return;
1786
1787                 R_Mesh_Matrix(&ent->matrix);
1788
1789                 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
1790                 if (skyrendermasked)
1791                 {
1792                         // depth-only (masking)
1793                         GL_ColorMask(0,0,0,0);
1794                         // just to make sure that braindead drivers don't draw anything
1795                         // despite that colormask...
1796                         GL_BlendFunc(GL_ZERO, GL_ONE);
1797                 }
1798                 else
1799                 {
1800                         // fog sky
1801                         GL_BlendFunc(GL_ONE, GL_ZERO);
1802                 }
1803                 GL_DepthMask(true);
1804                 GL_DepthTest(true);
1805
1806                 memset(&m, 0, sizeof(m));
1807                 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1808                 {
1809                         q3msurface_t *face = texturefacelist[texturefaceindex];
1810                         m.pointer_vertex = face->mesh.data_vertex3f;
1811                         R_Mesh_State(&m);
1812                         GL_LockArrays(0, face->mesh.num_vertices);
1813                         R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1814                         GL_LockArrays(0, 0);
1815                 }
1816                 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
1817                 return;
1818         }
1819         // anything else is a typical wall, lightmap * texture + glow
1820         dolightmap = (ent->flags & RENDER_LIGHT);
1821         dobase = true;
1822         doambient = r_ambient.value > 0;
1823         doglow = t->skin.glow != NULL;
1824         dofog = fogenabled;
1825         if (t->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1826                 qglDisable(GL_CULL_FACE);
1827         if (!dolightmap && dobase)
1828         {
1829                 dolightmap = false;
1830                 dobase = false;
1831                 GL_DepthMask(true);
1832                 GL_DepthTest(true);
1833                 GL_BlendFunc(GL_ONE, GL_ZERO);
1834                 GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], 1);
1835                 if (t->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1836                         qglDisable(GL_CULL_FACE);
1837                 memset(&m, 0, sizeof(m));
1838                 m.tex[0] = R_GetTexture(t->skin.base);
1839                 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1840                 {
1841                         q3msurface_t *face = texturefacelist[texturefaceindex];
1842                         m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
1843                         m.pointer_vertex = face->mesh.data_vertex3f;
1844                         R_Mesh_State(&m);
1845                         GL_LockArrays(0, face->mesh.num_vertices);
1846                         R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1847                         GL_LockArrays(0, 0);
1848                 }
1849         }
1850         if (r_lightmapintensity <= 0 && dolightmap && dobase)
1851         {
1852                 dolightmap = false;
1853                 dobase = false;
1854                 GL_DepthMask(true);
1855                 GL_DepthTest(true);
1856                 GL_BlendFunc(GL_ONE, GL_ZERO);
1857                 GL_Color(0, 0, 0, 1);
1858                 memset(&m, 0, sizeof(m));
1859                 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1860                 {
1861                         q3msurface_t *face = texturefacelist[texturefaceindex];
1862                         m.pointer_vertex = face->mesh.data_vertex3f;
1863                         R_Mesh_State(&m);
1864                         GL_LockArrays(0, face->mesh.num_vertices);
1865                         R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1866                         GL_LockArrays(0, 0);
1867                 }
1868         }
1869         if (r_textureunits.integer >= 2 && gl_combine.integer && dolightmap && dobase)
1870         {
1871                 // dualtexture combine
1872                 dolightmap = false;
1873                 dobase = false;
1874                 GL_DepthMask(true);
1875                 GL_DepthTest(true);
1876                 GL_BlendFunc(GL_ONE, GL_ZERO);
1877                 memset(&m, 0, sizeof(m));
1878                 m.tex[0] = R_GetTexture(t->skin.base);
1879                 GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
1880                 m.pointer_color = NULL;
1881                 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1882                 {
1883                         q3msurface_t *face = texturefacelist[texturefaceindex];
1884                         if (!face->lightmaptexture)
1885                                 continue;
1886                         m.tex[1] = R_GetTexture(face->lightmaptexture);
1887                         m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
1888                         m.pointer_texcoord[1] = face->mesh.data_texcoordlightmap2f;
1889                         m.texrgbscale[1] = 2;
1890                         m.pointer_vertex = face->mesh.data_vertex3f;
1891                         R_Mesh_State(&m);
1892                         GL_LockArrays(0, face->mesh.num_vertices);
1893                         R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1894                         GL_LockArrays(0, 0);
1895                 }
1896                 if (r_lightmapintensity == 1 && ent->colormod[0] == 1 && ent->colormod[1] == 1 && ent->colormod[2] == 1)
1897                 {
1898                         for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1899                         {
1900                                 q3msurface_t *face = texturefacelist[texturefaceindex];
1901                                 if (face->lightmaptexture)
1902                                         continue;
1903                                 m.tex[1] = R_GetTexture(face->lightmaptexture);
1904                                 m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
1905                                 m.pointer_texcoord[1] = face->mesh.data_texcoordlightmap2f;
1906                                 m.texrgbscale[1] = 2;
1907                                 m.pointer_color = face->mesh.data_lightmapcolor4f;
1908                                 m.pointer_vertex = face->mesh.data_vertex3f;
1909                                 R_Mesh_State(&m);
1910                                 GL_LockArrays(0, face->mesh.num_vertices);
1911                                 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1912                                 GL_LockArrays(0, 0);
1913                         }
1914                 }
1915                 else
1916                 {
1917                         for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1918                         {
1919                                 q3msurface_t *face = texturefacelist[texturefaceindex];
1920                                 if (face->lightmaptexture)
1921                                         continue;
1922                                 m.tex[1] = R_GetTexture(face->lightmaptexture);
1923                                 m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
1924                                 m.pointer_texcoord[1] = face->mesh.data_texcoordlightmap2f;
1925                                 m.texrgbscale[1] = 2;
1926                                 m.pointer_color = varray_color4f;
1927                                 for (i = 0;i < face->mesh.num_vertices;i++)
1928                                 {
1929                                         varray_color4f[i*4+0] = face->mesh.data_lightmapcolor4f[i*4+0] * ent->colormod[0] * r_lightmapintensity;
1930                                         varray_color4f[i*4+1] = face->mesh.data_lightmapcolor4f[i*4+1] * ent->colormod[1] * r_lightmapintensity;
1931                                         varray_color4f[i*4+2] = face->mesh.data_lightmapcolor4f[i*4+2] * ent->colormod[2] * r_lightmapintensity;
1932                                         varray_color4f[i*4+3] = face->mesh.data_lightmapcolor4f[i*4+3];
1933                                 }
1934                                 m.pointer_vertex = face->mesh.data_vertex3f;
1935                                 R_Mesh_State(&m);
1936                                 GL_LockArrays(0, face->mesh.num_vertices);
1937                                 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1938                                 GL_LockArrays(0, 0);
1939                         }
1940                 }
1941         }
1942         // single texture
1943         if (dolightmap)
1944         {
1945                 GL_DepthMask(true);
1946                 GL_DepthTest(true);
1947                 GL_BlendFunc(GL_ONE, GL_ZERO);
1948                 memset(&m, 0, sizeof(m));
1949                 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1950                 {
1951                         q3msurface_t *face = texturefacelist[texturefaceindex];
1952                         m.tex[0] = R_GetTexture(face->lightmaptexture);
1953                         m.pointer_texcoord[0] = face->mesh.data_texcoordlightmap2f;
1954                         if (face->lightmaptexture)
1955                                 m.pointer_color = NULL;
1956                         else
1957                                 m.pointer_color = face->mesh.data_lightmapcolor4f;
1958                         m.pointer_vertex = face->mesh.data_vertex3f;
1959                         R_Mesh_State(&m);
1960                         GL_LockArrays(0, face->mesh.num_vertices);
1961                         R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1962                         GL_LockArrays(0, 0);
1963                 }
1964         }
1965         if (dobase)
1966         {
1967                 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1968                 GL_DepthMask(false);
1969                 GL_DepthTest(true);
1970                 GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
1971                 memset(&m, 0, sizeof(m));
1972                 m.tex[0] = R_GetTexture(t->skin.base);
1973                 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1974                 {
1975                         q3msurface_t *face = texturefacelist[texturefaceindex];
1976                         m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
1977                         m.pointer_vertex = face->mesh.data_vertex3f;
1978                         R_Mesh_State(&m);
1979                         GL_LockArrays(0, face->mesh.num_vertices);
1980                         R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1981                         GL_LockArrays(0, 0);
1982                 }
1983         }
1984         if (doambient)
1985         {
1986                 GL_BlendFunc(GL_ONE, GL_ONE);
1987                 GL_DepthMask(false);
1988                 GL_DepthTest(true);
1989                 GL_Color(r_ambient.value * (1.0f / 128.0f) * ent->colormod[0], r_ambient.value * (1.0f / 128.0f) * ent->colormod[1], r_ambient.value * (1.0f / 128.0f) * ent->colormod[2], 1);
1990                 memset(&m, 0, sizeof(m));
1991                 m.tex[0] = R_GetTexture(t->skin.base);
1992                 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1993                 {
1994                         q3msurface_t *face = texturefacelist[texturefaceindex];
1995                         m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
1996                         m.pointer_vertex = face->mesh.data_vertex3f;
1997                         R_Mesh_State(&m);
1998                         GL_LockArrays(0, face->mesh.num_vertices);
1999                         R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
2000                         GL_LockArrays(0, 0);
2001                 }
2002         }
2003         if (doglow)
2004         {
2005                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2006                 GL_DepthMask(false);
2007                 GL_DepthTest(true);
2008                 GL_Color(1, 1, 1, 1);
2009                 memset(&m, 0, sizeof(m));
2010                 m.tex[0] = R_GetTexture(t->skin.glow);
2011                 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
2012                 {
2013                         q3msurface_t *face = texturefacelist[texturefaceindex];
2014                         m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
2015                         m.pointer_vertex = face->mesh.data_vertex3f;
2016                         R_Mesh_State(&m);
2017                         GL_LockArrays(0, face->mesh.num_vertices);
2018                         R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
2019                         GL_LockArrays(0, 0);
2020                 }
2021         }
2022         if (dofog)
2023         {
2024                 float modelorg[3];
2025                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2026                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2027                 GL_DepthMask(false);
2028                 GL_DepthTest(true);
2029                 GL_Color(1, 1, 1, 1);
2030                 memset(&m, 0, sizeof(m));
2031                 m.tex[0] = R_GetTexture(t->skin.fog);
2032                 m.pointer_color = varray_color4f;
2033                 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
2034                 {
2035                         q3msurface_t *face = texturefacelist[texturefaceindex];
2036                         if (m.tex[0])
2037                                 m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
2038                         m.pointer_vertex = face->mesh.data_vertex3f;
2039                         R_Mesh_State(&m);
2040                         RSurf_FogPassColors_Vertex3f_Color4f(face->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], 1, 1, face->mesh.num_vertices, modelorg);
2041                         GL_LockArrays(0, face->mesh.num_vertices);
2042                         R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
2043                         GL_LockArrays(0, 0);
2044                 }
2045         }
2046         if (t->textureflags & Q3TEXTUREFLAG_TWOSIDED)
2047                 qglEnable(GL_CULL_FACE);
2048 }
2049
2050 void R_Q3BSP_DrawFaces(entity_render_t *ent, int skyfaces)
2051 {
2052         int i, j, f, flagsmask, flags;
2053         q3msurface_t *face;
2054         model_t *model = ent->model;
2055         texture_t *t;
2056         const int maxfaces = 1024;
2057         int numfaces = 0;
2058         q3msurface_t *facelist[1024];
2059         R_Mesh_Matrix(&ent->matrix);
2060         flagsmask = Q3SURFACEFLAG_NODRAW | Q3SURFACEFLAG_SKY;
2061         if (skyfaces)
2062                 flags = Q3SURFACEFLAG_SKY;
2063         else
2064                 flags = 0;
2065         t = NULL;
2066         f = 0;
2067         numfaces = 0;
2068         for (i = 0, j = model->firstmodelsurface;i < model->nummodelsurfaces;i++, j++)
2069         {
2070                 if (ent != r_refdef.worldentity || r_worldsurfacevisible[j])
2071                 {
2072                         face = model->brushq3.data_faces + j;
2073                         if (t != face->texture)
2074                         {
2075                                 if (numfaces)
2076                                 {
2077                                         R_Q3BSP_DrawFaceList(ent, t, numfaces, facelist);
2078                                         numfaces = 0;
2079                                 }
2080                                 t = face->texture;
2081                                 f = t->surfaceflags & flagsmask;
2082                         }
2083                         if (f == flags)
2084                         {
2085                                 if (!face->mesh.num_triangles)
2086                                         continue;
2087                                 facelist[numfaces++] = face;
2088                                 if (numfaces >= maxfaces)
2089                                 {
2090                                         R_Q3BSP_DrawFaceList(ent, t, numfaces, facelist);
2091                                         numfaces = 0;
2092                                 }
2093                         }
2094                 }
2095         }
2096         if (numfaces)
2097                 R_Q3BSP_DrawFaceList(ent, t, numfaces, facelist);
2098 }
2099
2100 void R_Q3BSP_DrawSky(entity_render_t *ent)
2101 {
2102         if (r_drawcollisionbrushes.integer < 2)
2103                 R_Q3BSP_DrawFaces(ent, true);
2104 }
2105
2106 void R_Q3BSP_Draw(entity_render_t *ent)
2107 {
2108         if (r_drawcollisionbrushes.integer < 2)
2109                 R_Q3BSP_DrawFaces(ent, false);
2110         if (r_drawcollisionbrushes.integer >= 1)
2111         {
2112                 int i;
2113                 model_t *model = ent->model;
2114                 q3msurface_t *face;
2115                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2116                 GL_DepthMask(false);
2117                 GL_DepthTest(true);
2118                 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
2119                 for (i = 0;i < model->brushq3.data_models[model->brush.submodel].numbrushes;i++)
2120                         if (model->brushq3.data_models[model->brush.submodel].firstbrush[i].colbrushf && model->brushq3.data_models[model->brush.submodel].firstbrush[i].colbrushf->numtriangles)
2121                                 R_DrawCollisionBrush(model->brushq3.data_models[model->brush.submodel].firstbrush[i].colbrushf);
2122                 for (i = 0, face = model->brushq3.data_models[model->brush.submodel].firstface;i < model->brushq3.data_models[model->brush.submodel].numfaces;i++, face++)
2123                         if (face->mesh.num_collisiontriangles)
2124                                 R_Q3BSP_DrawCollisionFace(ent, face);
2125                 qglPolygonOffset(0, 0);
2126         }
2127 }
2128
2129 void R_Q3BSP_GetLightInfo(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, vec3_t outmins, vec3_t outmaxs, int *outclusterlist, qbyte *outclusterpvs, int *outnumclusterspointer, int *outsurfacelist, qbyte *outsurfacepvs, int *outnumsurfacespointer)
2130 {
2131         model_t *model = ent->model;
2132         vec3_t lightmins, lightmaxs;
2133         int t, leafindex, leaffaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
2134         const int *e;
2135         const float *v[3];
2136         q3msurface_t *surface;
2137         mleaf_t *leaf;
2138         const qbyte *pvs;
2139         lightmins[0] = relativelightorigin[0] - lightradius;
2140         lightmins[1] = relativelightorigin[1] - lightradius;
2141         lightmins[2] = relativelightorigin[2] - lightradius;
2142         lightmaxs[0] = relativelightorigin[0] + lightradius;
2143         lightmaxs[1] = relativelightorigin[1] + lightradius;
2144         lightmaxs[2] = relativelightorigin[2] + lightradius;
2145         *outnumclusterspointer = 0;
2146         *outnumsurfacespointer = 0;
2147         memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
2148         memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
2149         if (model == NULL)
2150         {
2151                 VectorCopy(lightmins, outmins);
2152                 VectorCopy(lightmaxs, outmaxs);
2153                 return;
2154         }
2155         VectorCopy(relativelightorigin, outmins);
2156         VectorCopy(relativelightorigin, outmaxs);
2157         if (model->brush.GetPVS)
2158                 pvs = model->brush.GetPVS(model, relativelightorigin);
2159         else
2160                 pvs = NULL;
2161         // FIXME: use BSP recursion as lights are often small
2162         for (leafindex = 0, leaf = model->brush.data_leafs;leafindex < model->brush.num_leafs;leafindex++, leaf++)
2163         {
2164                 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
2165                 {
2166                         outmins[0] = min(outmins[0], leaf->mins[0]);
2167                         outmins[1] = min(outmins[1], leaf->mins[1]);
2168                         outmins[2] = min(outmins[2], leaf->mins[2]);
2169                         outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
2170                         outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
2171                         outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
2172                         if (outclusterpvs)
2173                         {
2174                                 if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
2175                                 {
2176                                         SETPVSBIT(outclusterpvs, leaf->clusterindex);
2177                                         outclusterlist[outnumclusters++] = leaf->clusterindex;
2178                                 }
2179                         }
2180                         if (outsurfacepvs)
2181                         {
2182                                 for (leaffaceindex = 0;leaffaceindex < leaf->numleaffaces;leaffaceindex++)
2183                                 {
2184                                         surfaceindex = leaf->firstleafface[leaffaceindex];
2185                                         surface = model->brushq3.data_faces + surfaceindex;
2186                                         if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
2187                                         {
2188                                                 if (BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs) && !(surface->texture->surfaceparms & Q3SURFACEPARM_TRANS) && !(surface->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW)) && surface->mesh.num_triangles)
2189                                                 {
2190                                                         if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
2191                                                         {
2192                                                                 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
2193                                                                 {
2194                                                                         v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2195                                                                         v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2196                                                                         v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2197                                                                         if (lightmaxs[0] > min(v[0][0], min(v[1][0], v[2][0])) && lightmins[0] < max(v[0][0], max(v[1][0], v[2][0])) && lightmaxs[1] > min(v[0][1], min(v[1][1], v[2][1])) && lightmins[1] < max(v[0][1], max(v[1][1], v[2][1])) && lightmaxs[2] > min(v[0][2], min(v[1][2], v[2][2])) && lightmins[2] < max(v[0][2], max(v[1][2], v[2][2])))
2198                                                                         {
2199                                                                                 SETPVSBIT(outsurfacepvs, surfaceindex);
2200                                                                                 outsurfacelist[outnumsurfaces++] = surfaceindex;
2201                                                                                 break;
2202                                                                         }
2203                                                                 }
2204                                                         }
2205                                                         else
2206                                                         {
2207                                                                 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
2208                                                                 {
2209                                                                         v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2210                                                                         v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2211                                                                         v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2212                                                                         if (PointInfrontOfTriangle(relativelightorigin, v[0], v[1], v[2]) && lightmaxs[0] > min(v[0][0], min(v[1][0], v[2][0])) && lightmins[0] < max(v[0][0], max(v[1][0], v[2][0])) && lightmaxs[1] > min(v[0][1], min(v[1][1], v[2][1])) && lightmins[1] < max(v[0][1], max(v[1][1], v[2][1])) && lightmaxs[2] > min(v[0][2], min(v[1][2], v[2][2])) && lightmins[2] < max(v[0][2], max(v[1][2], v[2][2])))
2213                                                                         {
2214                                                                                 SETPVSBIT(outsurfacepvs, surfaceindex);
2215                                                                                 outsurfacelist[outnumsurfaces++] = surfaceindex;
2216                                                                                 break;
2217                                                                         }
2218                                                                 }
2219                                                         }
2220                                                 }
2221                                         }
2222                                 }
2223                         }
2224                 }
2225         }
2226
2227         // limit combined leaf box to light boundaries
2228         outmins[0] = max(outmins[0], lightmins[0]);
2229         outmins[1] = max(outmins[1], lightmins[1]);
2230         outmins[2] = max(outmins[2], lightmins[2]);
2231         outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
2232         outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
2233         outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
2234
2235         *outnumclusterspointer = outnumclusters;
2236         *outnumsurfacespointer = outnumsurfaces;
2237 }
2238
2239 void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
2240 {
2241         model_t *model = ent->model;
2242         vec3_t lightmins, lightmaxs;
2243         q3msurface_t *surface;
2244         int surfacelistindex;
2245         if (r_drawcollisionbrushes.integer < 2)
2246         {
2247                 lightmins[0] = relativelightorigin[0] - lightradius;
2248                 lightmins[1] = relativelightorigin[1] - lightradius;
2249                 lightmins[2] = relativelightorigin[2] - lightradius;
2250                 lightmaxs[0] = relativelightorigin[0] + lightradius;
2251                 lightmaxs[1] = relativelightorigin[1] + lightradius;
2252                 lightmaxs[2] = relativelightorigin[2] + lightradius;
2253                 R_Mesh_Matrix(&ent->matrix);
2254                 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
2255                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2256                 {
2257                         surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
2258                         // FIXME: check some manner of face->rendermode here?
2259                         if (!(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && !(surface->texture->surfaceparms & (Q3SURFACEPARM_SKY | Q3SURFACEPARM_TRANS)) && !(surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED))
2260                                 R_Shadow_MarkVolumeFromBox(surface->num_firstshadowmeshtriangle, surface->mesh.num_triangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, relativelightorigin, lightmins, lightmaxs, surface->mins, surface->maxs);
2261                 }
2262                 R_Shadow_VolumeFromList(model->brush.shadowmesh->numverts, model->brush.shadowmesh->numtriangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, model->brush.shadowmesh->neighbor3i, relativelightorigin, lightradius + model->radius + r_shadow_projectdistance.value, numshadowmark, shadowmarklist);
2263         }
2264 }
2265
2266 void R_Q3BSP_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap, vec_t ambientscale, vec_t diffusescale, vec_t specularscale, int numsurfaces, const int *surfacelist)
2267 {
2268         model_t *model = ent->model;
2269         vec3_t lightmins, lightmaxs, modelorg;
2270         q3msurface_t *surface;
2271         int surfacelistindex;
2272         if (r_drawcollisionbrushes.integer < 2)
2273         {
2274                 lightmins[0] = relativelightorigin[0] - lightradius;
2275                 lightmins[1] = relativelightorigin[1] - lightradius;
2276                 lightmins[2] = relativelightorigin[2] - lightradius;
2277                 lightmaxs[0] = relativelightorigin[0] + lightradius;
2278                 lightmaxs[1] = relativelightorigin[1] + lightradius;
2279                 lightmaxs[2] = relativelightorigin[2] + lightradius;
2280                 R_Mesh_Matrix(&ent->matrix);
2281                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2282                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2283                 {
2284                         surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
2285                         if (r_shadow_compilingrtlight)
2286                         {
2287                                 // if compiling an rtlight, capture the mesh
2288                                 Mod_ShadowMesh_AddMesh(r_shadow_mempool, r_shadow_compilingrtlight->static_meshchain_light, surface->texture->skin.base, surface->texture->skin.gloss, surface->texture->skin.nmap, surface->mesh.data_vertex3f, surface->mesh.data_svector3f, surface->mesh.data_tvector3f, surface->mesh.data_normal3f, surface->mesh.data_texcoordtexture2f, surface->mesh.num_triangles, surface->mesh.data_element3i);
2289                         }
2290                         else if (!(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && surface->mesh.num_triangles)
2291                         {
2292                                 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
2293                                         qglDisable(GL_CULL_FACE);
2294                                 R_Shadow_RenderLighting(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i, surface->mesh.data_vertex3f, surface->mesh.data_svector3f, surface->mesh.data_tvector3f, surface->mesh.data_normal3f, surface->mesh.data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, surface->texture->skin.base, surface->texture->skin.nmap, surface->texture->skin.gloss, lightcubemap, ambientscale, diffusescale, specularscale);
2295                                 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
2296                                         qglEnable(GL_CULL_FACE);
2297                         }
2298                 }
2299         }
2300 }
2301
2302 #if 0
2303 static void gl_surf_start(void)
2304 {
2305 }
2306
2307 static void gl_surf_shutdown(void)
2308 {
2309 }
2310
2311 static void gl_surf_newmap(void)
2312 {
2313 }
2314 #endif
2315
2316 void GL_Surf_Init(void)
2317 {
2318         int i;
2319         dlightdivtable[0] = 4194304;
2320         for (i = 1;i < 32768;i++)
2321                 dlightdivtable[i] = 4194304 / (i << 7);
2322
2323         Cvar_RegisterVariable(&r_ambient);
2324         Cvar_RegisterVariable(&r_drawportals);
2325         Cvar_RegisterVariable(&r_testvis);
2326         Cvar_RegisterVariable(&r_floatbuildlightmap);
2327         Cvar_RegisterVariable(&r_detailtextures);
2328         Cvar_RegisterVariable(&r_surfaceworldnode);
2329         Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
2330         Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
2331         Cvar_RegisterVariable(&r_q3bsp_renderskydepth);
2332         Cvar_RegisterVariable(&gl_lightmaps);
2333
2334         //R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
2335 }
2336