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