]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_rsurf.c
Moved audio CD initialization / finalization out of the video code so the CD music...
[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                         GL_BlendFunc(GL_ONE, GL_ZERO);
950                         GL_DepthMask(true);
951                         GL_DepthTest(true);
952                         GL_Color(1, 1, 1, 1);
953                         GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
954                         memset(&m, 0, sizeof(m));
955                         m.tex[0] = R_GetTexture(texture->skin.base);
956                         dobase = false;
957                         m.texrgbscale[1] = 2;
958                         dolightmap = false;
959                         if (r_textureunits.integer >= 4 && !doambient && dodetail && doglow)
960                         {
961                                 m.tex[2] = R_GetTexture(texture->skin.detail);
962                                 m.texrgbscale[2] = 2;
963                                 dodetail = false;
964                                 m.tex[3] = R_GetTexture(texture->skin.glow);
965                                 m.texcombinergb[3] = GL_ADD;
966                                 doglow = false;
967                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
968                                 {
969                                         surface = texturesurfacelist[texturesurfaceindex];
970                                         m.tex[1] = R_GetTexture(surface->lightmaptexture);
971                                         m.pointer_vertex = surface->mesh.data_vertex3f;
972                                         m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
973                                         m.pointer_texcoord[1] = surface->mesh.data_texcoordlightmap2f;
974                                         m.pointer_texcoord[2] = surface->mesh.data_texcoorddetail2f;
975                                         m.pointer_texcoord[3] = surface->mesh.data_texcoordtexture2f;
976                                         R_Mesh_State(&m);
977                                         GL_LockArrays(0, surface->mesh.num_vertices);
978                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
979                                         GL_LockArrays(0, 0);
980                                 }
981                         }
982                         else if (r_textureunits.integer >= 3 && !doambient && dodetail)
983                         {
984                                 m.tex[2] = R_GetTexture(texture->skin.detail);
985                                 m.texrgbscale[2] = 2;
986                                 dodetail = false;
987                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
988                                 {
989                                         surface = texturesurfacelist[texturesurfaceindex];
990                                         m.tex[1] = R_GetTexture(surface->lightmaptexture);
991                                         m.pointer_vertex = surface->mesh.data_vertex3f;
992                                         m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
993                                         m.pointer_texcoord[1] = surface->mesh.data_texcoordlightmap2f;
994                                         m.pointer_texcoord[2] = surface->mesh.data_texcoorddetail2f;
995                                         R_Mesh_State(&m);
996                                         GL_LockArrays(0, surface->mesh.num_vertices);
997                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
998                                         GL_LockArrays(0, 0);
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                         else
1021                         {
1022                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1023                                 {
1024                                         surface = texturesurfacelist[texturesurfaceindex];
1025                                         m.tex[1] = R_GetTexture(surface->lightmaptexture);
1026                                         m.pointer_vertex = surface->mesh.data_vertex3f;
1027                                         m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1028                                         m.pointer_texcoord[1] = surface->mesh.data_texcoordlightmap2f;
1029                                         R_Mesh_State(&m);
1030                                         GL_LockArrays(0, surface->mesh.num_vertices);
1031                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1032                                         GL_LockArrays(0, 0);
1033                                 }
1034                         }
1035                 }
1036                 // anything not handled above
1037                 if (dobase)
1038                 {
1039                         GL_BlendFunc(GL_ONE, GL_ZERO);
1040                         GL_DepthMask(true);
1041                         GL_DepthTest(true);
1042                         GL_Color(1, 1, 1, 1);
1043                         if (ent->flags & RENDER_LIGHT)
1044                                 GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
1045                         else
1046                                 GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], 1);
1047                         memset(&m, 0, sizeof(m));
1048                         m.tex[0] = R_GetTexture(texture->skin.base);
1049                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1050                         {
1051                                 surface = texturesurfacelist[texturesurfaceindex];
1052                                 m.pointer_vertex = surface->mesh.data_vertex3f;
1053                                 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1054                                 R_Mesh_State(&m);
1055                                 GL_LockArrays(0, surface->mesh.num_vertices);
1056                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1057                                 GL_LockArrays(0, 0);
1058                         }
1059                 }
1060                 GL_DepthMask(false);
1061                 if (dolightmap)
1062                 {
1063                         GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1064                         GL_DepthMask(false);
1065                         GL_DepthTest(true);
1066                         GL_Color(1, 1, 1, 1);
1067                         memset(&m, 0, sizeof(m));
1068                         m.tex[0] = R_GetTexture(texture->skin.base);
1069                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1070                         {
1071                                 surface = texturesurfacelist[texturesurfaceindex];
1072                                 m.tex[0] = R_GetTexture(surface->lightmaptexture);
1073                                 m.pointer_vertex = surface->mesh.data_vertex3f;
1074                                 m.pointer_texcoord[0] = surface->mesh.data_texcoordlightmap2f;
1075                                 R_Mesh_State(&m);
1076                                 GL_LockArrays(0, surface->mesh.num_vertices);
1077                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1078                                 GL_LockArrays(0, 0);
1079                         }
1080                 }
1081                 if (doambient)
1082                 {
1083                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1084                         GL_DepthMask(false);
1085                         GL_DepthTest(true);
1086                         memset(&m, 0, sizeof(m));
1087                         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);
1088                         m.tex[0] = R_GetTexture(texture->skin.base);
1089                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1090                         {
1091                                 surface = texturesurfacelist[texturesurfaceindex];
1092                                 m.pointer_vertex = surface->mesh.data_vertex3f;
1093                                 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1094                                 R_Mesh_State(&m);
1095                                 GL_LockArrays(0, surface->mesh.num_vertices);
1096                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1097                                 GL_LockArrays(0, 0);
1098                         }
1099                 }
1100                 if (dodetail)
1101                 {
1102                         GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1103                         GL_DepthMask(false);
1104                         GL_DepthTest(true);
1105                         GL_Color(1, 1, 1, 1);
1106                         memset(&m, 0, sizeof(m));
1107                         m.tex[0] = R_GetTexture(texture->skin.detail);
1108                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1109                         {
1110                                 surface = texturesurfacelist[texturesurfaceindex];
1111                                 m.pointer_vertex = surface->mesh.data_vertex3f;
1112                                 m.pointer_texcoord[0] = surface->mesh.data_texcoorddetail2f;
1113                                 R_Mesh_State(&m);
1114                                 GL_LockArrays(0, surface->mesh.num_vertices);
1115                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1116                                 GL_LockArrays(0, 0);
1117                         }
1118                 }
1119                 if (doglow)
1120                 {
1121                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1122                         GL_DepthMask(false);
1123                         GL_DepthTest(true);
1124                         GL_Color(1, 1, 1, 1);
1125                         memset(&m, 0, sizeof(m));
1126                         m.tex[0] = R_GetTexture(texture->skin.glow);
1127                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1128                         {
1129                                 surface = texturesurfacelist[texturesurfaceindex];
1130                                 m.pointer_vertex = surface->mesh.data_vertex3f;
1131                                 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1132                                 R_Mesh_State(&m);
1133                                 GL_LockArrays(0, surface->mesh.num_vertices);
1134                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1135                                 GL_LockArrays(0, 0);
1136                         }
1137                 }
1138                 if (dofog)
1139                 {
1140                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1141                         GL_DepthMask(false);
1142                         GL_DepthTest(true);
1143                         memset(&m, 0, sizeof(m));
1144                         m.pointer_color = varray_color4f;
1145                         m.tex[0] = R_GetTexture(texture->skin.glow);
1146                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1147                         {
1148                                 surface = texturesurfacelist[texturesurfaceindex];
1149                                 m.pointer_vertex = surface->mesh.data_vertex3f;
1150                                 if (m.tex[0])
1151                                         m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1152                                 R_Mesh_State(&m);
1153                                 RSurf_FogPassColors_Vertex3f_Color4f(surface->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], 1, 1, surface->mesh.num_vertices, modelorg);
1154                                 GL_LockArrays(0, surface->mesh.num_vertices);
1155                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1156                                 GL_LockArrays(0, 0);
1157                         }
1158                 }
1159         }
1160         else if (texture->flags & SURF_DRAWTURB)
1161         {
1162                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1163                 {
1164                         surface = texturesurfacelist[texturesurfaceindex];
1165                         RSurfShader_Transparent_Callback(ent, surface - ent->model->brush.data_surfaces);
1166                 }
1167         }
1168         else if (texture->flags & SURF_DRAWSKY)
1169         {
1170                 if (skyrendernow)
1171                 {
1172                         skyrendernow = false;
1173                         if (skyrendermasked)
1174                                 R_Sky();
1175                 }
1176                 // LordHavoc: HalfLife maps have freaky skypolys...
1177                 if (!ent->model->brush.ishlbsp)
1178                 {
1179                         R_Mesh_Matrix(&ent->matrix);
1180                         GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
1181                         if (skyrendermasked)
1182                         {
1183                                 // depth-only (masking)
1184                                 GL_ColorMask(0,0,0,0);
1185                                 // just to make sure that braindead drivers don't draw anything
1186                                 // despite that colormask...
1187                                 GL_BlendFunc(GL_ZERO, GL_ONE);
1188                         }
1189                         else
1190                         {
1191                                 // fog sky
1192                                 GL_BlendFunc(GL_ONE, GL_ZERO);
1193                         }
1194                         GL_DepthMask(true);
1195                         GL_DepthTest(true);
1196                         memset(&m, 0, sizeof(m));
1197                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1198                         {
1199                                 surface = texturesurfacelist[texturesurfaceindex];
1200                                 m.pointer_vertex = surface->mesh.data_vertex3f;
1201                                 R_Mesh_State(&m);
1202                                 GL_LockArrays(0, surface->mesh.num_vertices);
1203                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1204                                 GL_LockArrays(0, 0);
1205                         }
1206                         GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
1207                 }
1208         }
1209 }
1210
1211 void R_DrawSurfaces(entity_render_t *ent, qboolean skysurfaces)
1212 {
1213         int i, j, f, flagsmask;
1214         msurface_t *surface, **surfacechain;
1215         texture_t *t, *texture;
1216         model_t *model = ent->model;
1217         vec3_t modelorg;
1218         const int maxsurfacelist = 1024;
1219         int numsurfacelist = 0;
1220         msurface_t *surfacelist[1024];
1221         if (model == NULL)
1222                 return;
1223         R_Mesh_Matrix(&ent->matrix);
1224         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1225
1226         if (ent != r_refdef.worldentity)
1227         {
1228                 // because bmodels can be reused, we have to clear dlightframe every time
1229                 surface = model->brush.data_surfaces + model->firstmodelsurface;
1230                 for (i = 0;i < model->nummodelsurfaces;i++, surface++)
1231                         surface->dlightframe = -1;
1232         }
1233
1234         // update light styles
1235         if (!skysurfaces)
1236         {
1237                 if (r_dynamic.integer && !r_rtdlight)
1238                         R_MarkLights(ent);
1239                 for (i = 0;i < model->brushq1.light_styles;i++)
1240                 {
1241                         if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1242                         {
1243                                 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1244                                 if ((surfacechain = model->brushq1.light_styleupdatechains[i]))
1245                                         for (;(surface = *surfacechain);surfacechain++)
1246                                                 surface->cached_dlight = true;
1247                         }
1248                 }
1249         }
1250
1251         R_UpdateTextureInfo(ent);
1252         flagsmask = skysurfaces ? SURF_DRAWSKY : (SURF_DRAWTURB | SURF_LIGHTMAP);
1253         f = 0;
1254         t = NULL;
1255         numsurfacelist = 0;
1256         for (i = 0, j = model->firstmodelsurface;i < model->nummodelsurfaces;i++, j++)
1257         {
1258                 if (ent != r_refdef.worldentity || r_worldsurfacevisible[j])
1259                 {
1260                         surface = model->brush.data_surfaces + j;
1261                         if (t != surface->texture)
1262                         {
1263                                 if (numsurfacelist)
1264                                 {
1265                                         R_DrawSurfaceList(ent, texture, numsurfacelist, surfacelist);
1266                                         numsurfacelist = 0;
1267                                 }
1268                                 t = surface->texture;
1269                                 f = t->flags & flagsmask;
1270                                 texture = t->currentframe;
1271                         }
1272                         if (f)
1273                         {
1274                                 // add face to draw list and update lightmap if necessary
1275                                 c_faces++;
1276                                 if (surface->cached_dlight && surface->lightmaptexture != NULL)
1277                                         R_BuildLightMap(ent, surface);
1278                                 surfacelist[numsurfacelist++] = surface;
1279                                 if (numsurfacelist >= maxsurfacelist)
1280                                 {
1281                                         R_DrawSurfaceList(ent, texture, numsurfacelist, surfacelist);
1282                                         numsurfacelist = 0;
1283                                 }
1284                         }
1285                 }
1286         }
1287         if (numsurfacelist)
1288                 R_DrawSurfaceList(ent, texture, numsurfacelist, surfacelist);
1289 }
1290
1291 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1292 {
1293         int i;
1294         float *v;
1295         rmeshstate_t m;
1296         const mportal_t *portal = calldata1;
1297         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1298         GL_DepthMask(false);
1299         GL_DepthTest(true);
1300         R_Mesh_Matrix(&r_identitymatrix);
1301
1302         memset(&m, 0, sizeof(m));
1303         m.pointer_vertex = varray_vertex3f;
1304         R_Mesh_State(&m);
1305
1306         i = calldata2;
1307         GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1308                          ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1309                          ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1310                          0.125f);
1311         if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1312         {
1313                 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1314                         VectorCopy(portal->points[i].position, v);
1315         }
1316         else
1317                 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1318                         VectorCopy(portal->points[i].position, v);
1319         GL_LockArrays(0, portal->numpoints);
1320         R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1321         GL_LockArrays(0, 0);
1322 }
1323
1324 // LordHavoc: this is just a nice debugging tool, very slow
1325 static void R_DrawPortals(void)
1326 {
1327         int i, portalnum;
1328         mportal_t *portal;
1329         float center[3], f;
1330         model_t *model = r_refdef.worldmodel;
1331         if (model == NULL)
1332                 return;
1333         for (portalnum = 0, portal = model->brush.data_portals;portalnum < model->brush.num_portals;portalnum++, portal++)
1334         {
1335                 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1336                 if (!R_CullBox(portal->mins, portal->maxs))
1337                 {
1338                         VectorClear(center);
1339                         for (i = 0;i < portal->numpoints;i++)
1340                                 VectorAdd(center, portal->points[i].position, center);
1341                         f = ixtable[portal->numpoints];
1342                         VectorScale(center, f, center);
1343                         R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, portal, portalnum);
1344                 }
1345         }
1346 }
1347
1348 void R_WorldVisibility(void)
1349 {
1350         int i, j, *mark;
1351         mleaf_t *leaf;
1352         mleaf_t *viewleaf;
1353         model_t *model = r_refdef.worldmodel;
1354
1355         if (!model)
1356                 return;
1357
1358         // if possible find the leaf the view origin is in
1359         viewleaf = model->brushq1.PointInLeaf ? model->brushq1.PointInLeaf(model, r_vieworigin) : NULL;
1360         // if possible fetch the visible cluster bits
1361         if (model->brush.FatPVS)
1362                 model->brush.FatPVS(model, r_vieworigin, 2, r_pvsbits, sizeof(r_pvsbits));
1363
1364         // clear the visible surface and leaf flags arrays
1365         memset(r_worldsurfacevisible, 0, model->brush.num_surfaces);
1366         memset(r_worldleafvisible, 0, model->brush.num_leafs);
1367
1368         // if the user prefers surfaceworldnode (testing?) or the viewleaf could
1369         // not be found, or the viewleaf is not part of the visible world
1370         // (floating around in the void), use the pvs method
1371         if (r_surfaceworldnode.integer || !viewleaf || viewleaf->clusterindex < 0)
1372         {
1373                 // pvs method:
1374                 // similar to quake's RecursiveWorldNode but without cache misses
1375                 for (j = 0, leaf = model->brush.data_leafs;j < model->brush.num_leafs;j++, leaf++)
1376                 {
1377                         // if leaf is in current pvs and on the screen, mark its surfaces
1378                         if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
1379                         {
1380                                 c_leafs++;
1381                                 r_worldleafvisible[j] = true;
1382                                 if (leaf->numleafsurfaces)
1383                                         for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
1384                                                 r_worldsurfacevisible[*mark] = true;
1385                         }
1386                 }
1387         }
1388         else
1389         {
1390                 int leafstackpos;
1391                 mportal_t *p;
1392                 mleaf_t *leafstack[8192];
1393                 // portal method:
1394                 // follows portals leading outward from viewleaf, does not venture
1395                 // offscreen or into leafs that are not visible, faster than Quake's
1396                 // RecursiveWorldNode and vastly better in unvised maps, often culls a
1397                 // lot of surface that pvs alone would miss
1398                 leafstack[0] = viewleaf;
1399                 leafstackpos = 1;
1400                 while (leafstackpos)
1401                 {
1402                         c_leafs++;
1403                         leaf = leafstack[--leafstackpos];
1404                         r_worldleafvisible[leaf - model->brush.data_leafs] = true;
1405                         // mark any surfaces bounding this leaf
1406                         if (leaf->numleafsurfaces)
1407                                 for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
1408                                         r_worldsurfacevisible[*mark] = true;
1409                         // follow portals into other leafs
1410                         // the checks are:
1411                         // if viewer is behind portal (portal faces outward into the scene)
1412                         // and the portal polygon's bounding box is on the screen
1413                         // and the leaf has not been visited yet
1414                         // and the leaf is visible in the pvs
1415                         // (the first two checks won't cause as many cache misses as the leaf checks)
1416                         for (p = leaf->portals;p;p = p->next)
1417                                 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))
1418                                         leafstack[leafstackpos++] = p->past;
1419                 }
1420         }
1421
1422         if (r_drawportals.integer)
1423                 R_DrawPortals();
1424 }
1425
1426 void R_Q1BSP_DrawSky(entity_render_t *ent)
1427 {
1428         if (ent->model == NULL)
1429                 return;
1430         R_DrawSurfaces(ent, true);
1431 }
1432
1433 void R_Q1BSP_Draw(entity_render_t *ent)
1434 {
1435         if (ent->model == NULL)
1436                 return;
1437         c_bmodels++;
1438         R_DrawSurfaces(ent, false);
1439 }
1440
1441 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)
1442 {
1443         model_t *model = ent->model;
1444         vec3_t lightmins, lightmaxs;
1445         int t, leafindex, leafsurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
1446         const int *e;
1447         const float *v[3];
1448         msurface_t *surface;
1449         mleaf_t *leaf;
1450         const qbyte *pvs;
1451         lightmins[0] = relativelightorigin[0] - lightradius;
1452         lightmins[1] = relativelightorigin[1] - lightradius;
1453         lightmins[2] = relativelightorigin[2] - lightradius;
1454         lightmaxs[0] = relativelightorigin[0] + lightradius;
1455         lightmaxs[1] = relativelightorigin[1] + lightradius;
1456         lightmaxs[2] = relativelightorigin[2] + lightradius;
1457         *outnumclusterspointer = 0;
1458         *outnumsurfacespointer = 0;
1459         memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
1460         memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
1461         if (model == NULL)
1462         {
1463                 VectorCopy(lightmins, outmins);
1464                 VectorCopy(lightmaxs, outmaxs);
1465                 return;
1466         }
1467         VectorCopy(relativelightorigin, outmins);
1468         VectorCopy(relativelightorigin, outmaxs);
1469         if (model->brush.GetPVS)
1470                 pvs = model->brush.GetPVS(model, relativelightorigin);
1471         else
1472                 pvs = NULL;
1473         // FIXME: use BSP recursion as lights are often small
1474         for (leafindex = 0, leaf = model->brush.data_leafs;leafindex < model->brush.num_leafs;leafindex++, leaf++)
1475         {
1476                 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
1477                 {
1478                         outmins[0] = min(outmins[0], leaf->mins[0]);
1479                         outmins[1] = min(outmins[1], leaf->mins[1]);
1480                         outmins[2] = min(outmins[2], leaf->mins[2]);
1481                         outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
1482                         outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
1483                         outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
1484                         if (outclusterpvs)
1485                         {
1486                                 if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
1487                                 {
1488                                         SETPVSBIT(outclusterpvs, leaf->clusterindex);
1489                                         outclusterlist[outnumclusters++] = leaf->clusterindex;
1490                                 }
1491                         }
1492                         if (outsurfacepvs)
1493                         {
1494                                 for (leafsurfaceindex = 0;leafsurfaceindex < leaf->numleafsurfaces;leafsurfaceindex++)
1495                                 {
1496                                         surfaceindex = leaf->firstleafsurface[leafsurfaceindex];
1497                                         if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
1498                                         {
1499                                                 surface = model->brush.data_surfaces + surfaceindex;
1500                                                 if (BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs) && (surface->texture->flags & SURF_LIGHTMAP) && !surface->texture->skin.fog)
1501                                                 {
1502                                                         for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
1503                                                         {
1504                                                                 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1505                                                                 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1506                                                                 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1507                                                                 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])))
1508                                                                 {
1509                                                                         SETPVSBIT(outsurfacepvs, surfaceindex);
1510                                                                         outsurfacelist[outnumsurfaces++] = surfaceindex;
1511                                                                         break;
1512                                                                 }
1513                                                         }
1514                                                 }
1515                                         }
1516                                 }
1517                         }
1518                 }
1519         }
1520
1521         // limit combined leaf box to light boundaries
1522         outmins[0] = max(outmins[0], lightmins[0]);
1523         outmins[1] = max(outmins[1], lightmins[1]);
1524         outmins[2] = max(outmins[2], lightmins[2]);
1525         outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
1526         outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
1527         outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
1528
1529         *outnumclusterspointer = outnumclusters;
1530         *outnumsurfacespointer = outnumsurfaces;
1531 }
1532
1533 void R_Q1BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
1534 {
1535         model_t *model = ent->model;
1536         vec3_t lightmins, lightmaxs;
1537         msurface_t *surface;
1538         int surfacelistindex;
1539         if (r_drawcollisionbrushes.integer < 2)
1540         {
1541                 lightmins[0] = relativelightorigin[0] - lightradius;
1542                 lightmins[1] = relativelightorigin[1] - lightradius;
1543                 lightmins[2] = relativelightorigin[2] - lightradius;
1544                 lightmaxs[0] = relativelightorigin[0] + lightradius;
1545                 lightmaxs[1] = relativelightorigin[1] + lightradius;
1546                 lightmaxs[2] = relativelightorigin[2] + lightradius;
1547                 R_Mesh_Matrix(&ent->matrix);
1548                 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
1549                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1550                 {
1551                         surface = model->brush.data_surfaces + surfacelist[surfacelistindex];
1552                         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);
1553                 }
1554                 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);
1555         }
1556 }
1557
1558 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)
1559 {
1560         model_t *model = ent->model;
1561         vec3_t lightmins, lightmaxs, modelorg;
1562         msurface_t *surface;
1563         texture_t *t;
1564         int surfacelistindex;
1565         if (r_drawcollisionbrushes.integer < 2)
1566         {
1567                 lightmins[0] = relativelightorigin[0] - lightradius;
1568                 lightmins[1] = relativelightorigin[1] - lightradius;
1569                 lightmins[2] = relativelightorigin[2] - lightradius;
1570                 lightmaxs[0] = relativelightorigin[0] + lightradius;
1571                 lightmaxs[1] = relativelightorigin[1] + lightradius;
1572                 lightmaxs[2] = relativelightorigin[2] + lightradius;
1573                 R_Mesh_Matrix(&ent->matrix);
1574                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1575                 R_UpdateTextureInfo(ent);
1576                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1577                 {
1578                         surface = model->brush.data_surfaces + surfacelist[surfacelistindex];
1579                         if (r_shadow_compilingrtlight)
1580                         {
1581                                 // if compiling an rtlight, capture the mesh
1582                                 t = surface->texture;
1583                                 if (t->flags & SURF_LIGHTMAP && t->skin.fog == NULL)
1584                                         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);
1585                         }
1586                         else if (ent != r_refdef.worldentity || r_worldsurfacevisible[surfacelist[surfacelistindex]])
1587                         {
1588                                 t = surface->texture->currentframe;
1589                                 // FIXME: transparent surfaces need to be lit later
1590                                 if (t->flags & SURF_LIGHTMAP && t->rendertype == SURFRENDER_OPAQUE)
1591                                         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);
1592                         }
1593                 }
1594         }
1595 }
1596
1597 void R_DrawCollisionBrush(colbrushf_t *brush)
1598 {
1599         int i;
1600         rmeshstate_t m;
1601         memset(&m, 0, sizeof(m));
1602         m.pointer_vertex = brush->points->v;
1603         R_Mesh_State(&m);
1604         i = (int)(((size_t)brush) / sizeof(colbrushf_t));
1605         GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1606         GL_LockArrays(0, brush->numpoints);
1607         R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1608         GL_LockArrays(0, 0);
1609 }
1610
1611 void R_Q3BSP_DrawCollisionSurface(entity_render_t *ent, msurface_t *surface)
1612 {
1613         int i;
1614         rmeshstate_t m;
1615         if (!surface->mesh.num_collisiontriangles)
1616                 return;
1617         memset(&m, 0, sizeof(m));
1618         m.pointer_vertex = surface->mesh.data_collisionvertex3f;
1619         R_Mesh_State(&m);
1620         i = (int)(((size_t)surface) / sizeof(msurface_t));
1621         GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1622         GL_LockArrays(0, surface->mesh.num_collisionvertices);
1623         R_Mesh_Draw(surface->mesh.num_collisionvertices, surface->mesh.num_collisiontriangles, surface->mesh.data_collisionelement3i);
1624         GL_LockArrays(0, 0);
1625 }
1626
1627 void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int surfacenumber)
1628 {
1629         const entity_render_t *ent = voident;
1630         msurface_t *surface = ent->model->brush.data_surfaces + surfacenumber;
1631         rmeshstate_t m;
1632         R_Mesh_Matrix(&ent->matrix);
1633         memset(&m, 0, sizeof(m));
1634         if ((ent->effects & EF_ADDITIVE) || (surface->texture->textureflags & Q3TEXTUREFLAG_ADDITIVE))
1635                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1636         else
1637                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1638         GL_DepthMask(false);
1639         GL_DepthTest(!(ent->effects & EF_NODEPTHTEST));
1640         m.tex[0] = R_GetTexture(surface->texture->skin.base);
1641         m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1642         // LordHavoc: quake3 was not able to do this; lit transparent surfaces
1643         if (gl_combine.integer)
1644         {
1645                 m.texrgbscale[0] = 2;
1646                 if (r_textureunits.integer >= 2)
1647                 {
1648                         m.tex[1] = R_GetTexture(surface->lightmaptexture);
1649                         m.pointer_texcoord[1] = surface->mesh.data_texcoordlightmap2f;
1650                         GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], ent->alpha);
1651                 }
1652                 else
1653                 {
1654                         if (ent->colormod[0] == 1 && ent->colormod[1] == 1 && ent->colormod[2] == 1 && ent->alpha == 1)
1655                                 m.pointer_color = surface->mesh.data_lightmapcolor4f;
1656                         else
1657                         {
1658                                 int i;
1659                                 for (i = 0;i < surface->mesh.num_vertices;i++)
1660                                 {
1661                                         varray_color4f[i*4+0] = surface->mesh.data_lightmapcolor4f[i*4+0] * ent->colormod[0];
1662                                         varray_color4f[i*4+1] = surface->mesh.data_lightmapcolor4f[i*4+1] * ent->colormod[1];
1663                                         varray_color4f[i*4+2] = surface->mesh.data_lightmapcolor4f[i*4+2] * ent->colormod[2];
1664                                         varray_color4f[i*4+3] = surface->mesh.data_lightmapcolor4f[i*4+3] * ent->alpha;
1665                                 }
1666                                 m.pointer_color = varray_color4f;
1667                         }
1668                 }
1669         }
1670         else
1671         {
1672                 int i;
1673                 for (i = 0;i < surface->mesh.num_vertices;i++)
1674                 {
1675                         varray_color4f[i*4+0] = surface->mesh.data_lightmapcolor4f[i*4+0] * ent->colormod[0] * 2.0f;
1676                         varray_color4f[i*4+1] = surface->mesh.data_lightmapcolor4f[i*4+1] * ent->colormod[1] * 2.0f;
1677                         varray_color4f[i*4+2] = surface->mesh.data_lightmapcolor4f[i*4+2] * ent->colormod[2] * 2.0f;
1678                         varray_color4f[i*4+3] = surface->mesh.data_lightmapcolor4f[i*4+3] * ent->alpha;
1679                 }
1680                 m.pointer_color = varray_color4f;
1681         }
1682         if (surface->texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
1683         {
1684                 int i, j;
1685                 float center[3], center2[3], forward[3], right[3], up[3], v[4][3];
1686                 matrix4x4_t matrix1, imatrix1;
1687                 R_Mesh_Matrix(&r_identitymatrix);
1688                 // a single autosprite surface can contain multiple sprites...
1689                 for (j = 0;j < surface->mesh.num_vertices - 3;j += 4)
1690                 {
1691                         VectorClear(center);
1692                         for (i = 0;i < 4;i++)
1693                                 VectorAdd(center, surface->mesh.data_vertex3f + (j+i) * 3, center);
1694                         VectorScale(center, 0.25f, center);
1695                         Matrix4x4_Transform(&ent->matrix, center, center2);
1696                         // FIXME: calculate vectors from triangle edges instead of using texture vectors as an easy way out?
1697                         Matrix4x4_FromVectors(&matrix1, surface->mesh.data_normal3f + j*3, surface->mesh.data_svector3f + j*3, surface->mesh.data_tvector3f + j*3, center);
1698                         Matrix4x4_Invert_Simple(&imatrix1, &matrix1);
1699                         for (i = 0;i < 4;i++)
1700                                 Matrix4x4_Transform(&imatrix1, surface->mesh.data_vertex3f + (j+i)*3, v[i]);
1701                         if (surface->texture->textureflags & Q3TEXTUREFLAG_AUTOSPRITE2)
1702                         {
1703                                 forward[0] = r_vieworigin[0] - center2[0];
1704                                 forward[1] = r_vieworigin[1] - center2[1];
1705                                 forward[2] = 0;
1706                                 VectorNormalize(forward);
1707                                 right[0] = forward[1];
1708                                 right[1] = -forward[0];
1709                                 right[2] = 0;
1710                                 up[0] = 0;
1711                                 up[1] = 0;
1712                                 up[2] = 1;
1713                         }
1714                         else
1715                         {
1716                                 VectorCopy(r_viewforward, forward);
1717                                 VectorCopy(r_viewright, right);
1718                                 VectorCopy(r_viewup, up);
1719                         }
1720                         for (i = 0;i < 4;i++)
1721                                 VectorMAMAMAM(1, center2, v[i][0], forward, v[i][1], right, v[i][2], up, varray_vertex3f + (i+j) * 3);
1722                 }
1723                 m.pointer_vertex = varray_vertex3f;
1724         }
1725         else
1726                 m.pointer_vertex = surface->mesh.data_vertex3f;
1727         R_Mesh_State(&m);
1728         if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1729                 qglDisable(GL_CULL_FACE);
1730         GL_LockArrays(0, surface->mesh.num_vertices);
1731         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1732         GL_LockArrays(0, 0);
1733         if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1734                 qglEnable(GL_CULL_FACE);
1735 }
1736
1737 void R_Q3BSP_DrawFaceList(entity_render_t *ent, texture_t *t, int texturenumsurfaces, msurface_t **texturesurfacelist)
1738 {
1739         int i, texturesurfaceindex;
1740         msurface_t *surface;
1741         qboolean dolightmap;
1742         qboolean dobase;
1743         qboolean doambient;
1744         qboolean doglow;
1745         qboolean dofog;
1746         rmeshstate_t m;
1747         if (!texturenumsurfaces)
1748                 return;
1749         c_faces += texturenumsurfaces;
1750         // gl_lightmaps debugging mode skips normal texturing
1751         if (gl_lightmaps.integer)
1752         {
1753                 GL_DepthMask(true);
1754                 GL_DepthTest(true);
1755                 GL_BlendFunc(GL_ONE, GL_ZERO);
1756                 qglDisable(GL_CULL_FACE);
1757                 memset(&m, 0, sizeof(m));
1758                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1759                 {
1760                         surface = texturesurfacelist[texturesurfaceindex];
1761                         m.tex[0] = R_GetTexture(surface->lightmaptexture);
1762                         m.pointer_texcoord[0] = surface->mesh.data_texcoordlightmap2f;
1763                         if (surface->lightmaptexture)
1764                         {
1765                                 GL_Color(1, 1, 1, 1);
1766                                 m.pointer_color = NULL;
1767                         }
1768                         else
1769                                 m.pointer_color = surface->mesh.data_lightmapcolor4f;
1770                         m.pointer_vertex = surface->mesh.data_vertex3f;
1771                         R_Mesh_State(&m);
1772                         GL_LockArrays(0, surface->mesh.num_vertices);
1773                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1774                         GL_LockArrays(0, 0);
1775                 }
1776                 qglEnable(GL_CULL_FACE);
1777                 return;
1778         }
1779         // transparent surfaces get sorted for later drawing
1780         if ((t->surfaceparms & Q3SURFACEPARM_TRANS) || ent->alpha < 1 || (ent->effects & EF_ADDITIVE))
1781         {
1782                 vec3_t facecenter, center;
1783                 // drawing sky transparently would be too difficult
1784                 if (t->surfaceparms & Q3SURFACEPARM_SKY)
1785                         return;
1786                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1787                 {
1788                         surface = texturesurfacelist[texturesurfaceindex];
1789                         facecenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
1790                         facecenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
1791                         facecenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
1792                         Matrix4x4_Transform(&ent->matrix, facecenter, center);
1793                         R_MeshQueue_AddTransparent(center, R_Q3BSP_DrawFace_TransparentCallback, ent, surface - ent->model->brush.data_surfaces);
1794                 }
1795                 return;
1796         }
1797         // sky surfaces draw sky if needed and render themselves as a depth mask
1798         if (t->surfaceparms & Q3SURFACEPARM_SKY)
1799         {
1800                 if (skyrendernow)
1801                 {
1802                         skyrendernow = false;
1803                         if (skyrendermasked)
1804                                 R_Sky();
1805                 }
1806                 if (!r_q3bsp_renderskydepth.integer)
1807                         return;
1808
1809                 R_Mesh_Matrix(&ent->matrix);
1810
1811                 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
1812                 if (skyrendermasked)
1813                 {
1814                         // depth-only (masking)
1815                         GL_ColorMask(0,0,0,0);
1816                         // just to make sure that braindead drivers don't draw anything
1817                         // despite that colormask...
1818                         GL_BlendFunc(GL_ZERO, GL_ONE);
1819                 }
1820                 else
1821                 {
1822                         // fog sky
1823                         GL_BlendFunc(GL_ONE, GL_ZERO);
1824                 }
1825                 GL_DepthMask(true);
1826                 GL_DepthTest(true);
1827
1828                 memset(&m, 0, sizeof(m));
1829                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1830                 {
1831                         surface = texturesurfacelist[texturesurfaceindex];
1832                         m.pointer_vertex = surface->mesh.data_vertex3f;
1833                         R_Mesh_State(&m);
1834                         GL_LockArrays(0, surface->mesh.num_vertices);
1835                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1836                         GL_LockArrays(0, 0);
1837                 }
1838                 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
1839                 return;
1840         }
1841         // anything else is a typical wall, lightmap * texture + glow
1842         dolightmap = (ent->flags & RENDER_LIGHT);
1843         dobase = true;
1844         doambient = r_ambient.value > 0;
1845         doglow = t->skin.glow != NULL;
1846         dofog = fogenabled;
1847         if (t->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1848                 qglDisable(GL_CULL_FACE);
1849         if (!dolightmap && dobase)
1850         {
1851                 dolightmap = false;
1852                 dobase = false;
1853                 GL_DepthMask(true);
1854                 GL_DepthTest(true);
1855                 GL_BlendFunc(GL_ONE, GL_ZERO);
1856                 GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], 1);
1857                 if (t->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1858                         qglDisable(GL_CULL_FACE);
1859                 memset(&m, 0, sizeof(m));
1860                 m.tex[0] = R_GetTexture(t->skin.base);
1861                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1862                 {
1863                         surface = texturesurfacelist[texturesurfaceindex];
1864                         m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1865                         m.pointer_vertex = surface->mesh.data_vertex3f;
1866                         R_Mesh_State(&m);
1867                         GL_LockArrays(0, surface->mesh.num_vertices);
1868                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1869                         GL_LockArrays(0, 0);
1870                 }
1871         }
1872         if (r_lightmapintensity <= 0 && dolightmap && dobase)
1873         {
1874                 dolightmap = false;
1875                 dobase = false;
1876                 GL_DepthMask(true);
1877                 GL_DepthTest(true);
1878                 GL_BlendFunc(GL_ONE, GL_ZERO);
1879                 GL_Color(0, 0, 0, 1);
1880                 memset(&m, 0, sizeof(m));
1881                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1882                 {
1883                         surface = texturesurfacelist[texturesurfaceindex];
1884                         m.pointer_vertex = surface->mesh.data_vertex3f;
1885                         R_Mesh_State(&m);
1886                         GL_LockArrays(0, surface->mesh.num_vertices);
1887                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1888                         GL_LockArrays(0, 0);
1889                 }
1890         }
1891         if (r_textureunits.integer >= 2 && gl_combine.integer && dolightmap && dobase)
1892         {
1893                 // dualtexture combine
1894                 dolightmap = false;
1895                 dobase = false;
1896                 GL_DepthMask(true);
1897                 GL_DepthTest(true);
1898                 GL_BlendFunc(GL_ONE, GL_ZERO);
1899                 memset(&m, 0, sizeof(m));
1900                 m.tex[0] = R_GetTexture(t->skin.base);
1901                 GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
1902                 m.pointer_color = NULL;
1903                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1904                 {
1905                         surface = texturesurfacelist[texturesurfaceindex];
1906                         if (!surface->lightmaptexture)
1907                                 continue;
1908                         m.tex[1] = R_GetTexture(surface->lightmaptexture);
1909                         m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1910                         m.pointer_texcoord[1] = surface->mesh.data_texcoordlightmap2f;
1911                         m.texrgbscale[1] = 2;
1912                         m.pointer_vertex = surface->mesh.data_vertex3f;
1913                         R_Mesh_State(&m);
1914                         GL_LockArrays(0, surface->mesh.num_vertices);
1915                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1916                         GL_LockArrays(0, 0);
1917                 }
1918                 if (r_lightmapintensity == 1 && ent->colormod[0] == 1 && ent->colormod[1] == 1 && ent->colormod[2] == 1)
1919                 {
1920                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1921                         {
1922                                 surface = texturesurfacelist[texturesurfaceindex];
1923                                 if (surface->lightmaptexture)
1924                                         continue;
1925                                 m.tex[1] = R_GetTexture(surface->lightmaptexture);
1926                                 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1927                                 m.pointer_texcoord[1] = surface->mesh.data_texcoordlightmap2f;
1928                                 m.texrgbscale[1] = 2;
1929                                 m.pointer_color = surface->mesh.data_lightmapcolor4f;
1930                                 m.pointer_vertex = surface->mesh.data_vertex3f;
1931                                 R_Mesh_State(&m);
1932                                 GL_LockArrays(0, surface->mesh.num_vertices);
1933                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1934                                 GL_LockArrays(0, 0);
1935                         }
1936                 }
1937                 else
1938                 {
1939                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1940                         {
1941                                 surface = texturesurfacelist[texturesurfaceindex];
1942                                 if (surface->lightmaptexture)
1943                                         continue;
1944                                 m.tex[1] = R_GetTexture(surface->lightmaptexture);
1945                                 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1946                                 m.pointer_texcoord[1] = surface->mesh.data_texcoordlightmap2f;
1947                                 m.texrgbscale[1] = 2;
1948                                 m.pointer_color = varray_color4f;
1949                                 for (i = 0;i < surface->mesh.num_vertices;i++)
1950                                 {
1951                                         varray_color4f[i*4+0] = surface->mesh.data_lightmapcolor4f[i*4+0] * ent->colormod[0] * r_lightmapintensity;
1952                                         varray_color4f[i*4+1] = surface->mesh.data_lightmapcolor4f[i*4+1] * ent->colormod[1] * r_lightmapintensity;
1953                                         varray_color4f[i*4+2] = surface->mesh.data_lightmapcolor4f[i*4+2] * ent->colormod[2] * r_lightmapintensity;
1954                                         varray_color4f[i*4+3] = surface->mesh.data_lightmapcolor4f[i*4+3];
1955                                 }
1956                                 m.pointer_vertex = surface->mesh.data_vertex3f;
1957                                 R_Mesh_State(&m);
1958                                 GL_LockArrays(0, surface->mesh.num_vertices);
1959                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1960                                 GL_LockArrays(0, 0);
1961                         }
1962                 }
1963         }
1964         // single texture
1965         if (dolightmap)
1966         {
1967                 GL_DepthMask(true);
1968                 GL_DepthTest(true);
1969                 GL_BlendFunc(GL_ONE, GL_ZERO);
1970                 memset(&m, 0, sizeof(m));
1971                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1972                 {
1973                         surface = texturesurfacelist[texturesurfaceindex];
1974                         m.tex[0] = R_GetTexture(surface->lightmaptexture);
1975                         m.pointer_texcoord[0] = surface->mesh.data_texcoordlightmap2f;
1976                         if (surface->lightmaptexture)
1977                                 m.pointer_color = NULL;
1978                         else
1979                                 m.pointer_color = surface->mesh.data_lightmapcolor4f;
1980                         m.pointer_vertex = surface->mesh.data_vertex3f;
1981                         R_Mesh_State(&m);
1982                         GL_LockArrays(0, surface->mesh.num_vertices);
1983                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1984                         GL_LockArrays(0, 0);
1985                 }
1986         }
1987         if (dobase)
1988         {
1989                 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1990                 GL_DepthMask(false);
1991                 GL_DepthTest(true);
1992                 GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
1993                 memset(&m, 0, sizeof(m));
1994                 m.tex[0] = R_GetTexture(t->skin.base);
1995                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1996                 {
1997                         surface = texturesurfacelist[texturesurfaceindex];
1998                         m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1999                         m.pointer_vertex = surface->mesh.data_vertex3f;
2000                         R_Mesh_State(&m);
2001                         GL_LockArrays(0, surface->mesh.num_vertices);
2002                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
2003                         GL_LockArrays(0, 0);
2004                 }
2005         }
2006         if (doambient)
2007         {
2008                 GL_BlendFunc(GL_ONE, GL_ONE);
2009                 GL_DepthMask(false);
2010                 GL_DepthTest(true);
2011                 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);
2012                 memset(&m, 0, sizeof(m));
2013                 m.tex[0] = R_GetTexture(t->skin.base);
2014                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
2015                 {
2016                         surface = texturesurfacelist[texturesurfaceindex];
2017                         m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
2018                         m.pointer_vertex = surface->mesh.data_vertex3f;
2019                         R_Mesh_State(&m);
2020                         GL_LockArrays(0, surface->mesh.num_vertices);
2021                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
2022                         GL_LockArrays(0, 0);
2023                 }
2024         }
2025         if (doglow)
2026         {
2027                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2028                 GL_DepthMask(false);
2029                 GL_DepthTest(true);
2030                 GL_Color(1, 1, 1, 1);
2031                 memset(&m, 0, sizeof(m));
2032                 m.tex[0] = R_GetTexture(t->skin.glow);
2033                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
2034                 {
2035                         surface = texturesurfacelist[texturesurfaceindex];
2036                         m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
2037                         m.pointer_vertex = surface->mesh.data_vertex3f;
2038                         R_Mesh_State(&m);
2039                         GL_LockArrays(0, surface->mesh.num_vertices);
2040                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
2041                         GL_LockArrays(0, 0);
2042                 }
2043         }
2044         if (dofog)
2045         {
2046                 float modelorg[3];
2047                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2048                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2049                 GL_DepthMask(false);
2050                 GL_DepthTest(true);
2051                 GL_Color(1, 1, 1, 1);
2052                 memset(&m, 0, sizeof(m));
2053                 m.tex[0] = R_GetTexture(t->skin.fog);
2054                 m.pointer_color = varray_color4f;
2055                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
2056                 {
2057                         surface = texturesurfacelist[texturesurfaceindex];
2058                         if (m.tex[0])
2059                                 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
2060                         m.pointer_vertex = surface->mesh.data_vertex3f;
2061                         R_Mesh_State(&m);
2062                         RSurf_FogPassColors_Vertex3f_Color4f(surface->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], 1, 1, surface->mesh.num_vertices, modelorg);
2063                         GL_LockArrays(0, surface->mesh.num_vertices);
2064                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
2065                         GL_LockArrays(0, 0);
2066                 }
2067         }
2068         if (t->textureflags & Q3TEXTUREFLAG_TWOSIDED)
2069                 qglEnable(GL_CULL_FACE);
2070 }
2071
2072 void R_Q3BSP_DrawFaces(entity_render_t *ent, int skyfaces)
2073 {
2074         int i, j, f, flagsmask, flags;
2075         msurface_t *surface;
2076         model_t *model = ent->model;
2077         texture_t *t;
2078         const int maxfaces = 1024;
2079         int numsurfaces = 0;
2080         msurface_t *surfacelist[1024];
2081         R_Mesh_Matrix(&ent->matrix);
2082         flagsmask = Q3SURFACEFLAG_NODRAW | Q3SURFACEFLAG_SKY;
2083         if (skyfaces)
2084                 flags = Q3SURFACEFLAG_SKY;
2085         else
2086                 flags = 0;
2087         t = NULL;
2088         f = 0;
2089         numsurfaces = 0;
2090         for (i = 0, j = model->firstmodelsurface;i < model->nummodelsurfaces;i++, j++)
2091         {
2092                 if (ent != r_refdef.worldentity || r_worldsurfacevisible[j])
2093                 {
2094                         surface = model->brush.data_surfaces + j;
2095                         if (t != surface->texture)
2096                         {
2097                                 if (numsurfaces)
2098                                 {
2099                                         R_Q3BSP_DrawFaceList(ent, t, numsurfaces, surfacelist);
2100                                         numsurfaces = 0;
2101                                 }
2102                                 t = surface->texture;
2103                                 f = t->surfaceflags & flagsmask;
2104                         }
2105                         if (f == flags)
2106                         {
2107                                 if (!surface->mesh.num_triangles)
2108                                         continue;
2109                                 surfacelist[numsurfaces++] = surface;
2110                                 if (numsurfaces >= maxfaces)
2111                                 {
2112                                         R_Q3BSP_DrawFaceList(ent, t, numsurfaces, surfacelist);
2113                                         numsurfaces = 0;
2114                                 }
2115                         }
2116                 }
2117         }
2118         if (numsurfaces)
2119                 R_Q3BSP_DrawFaceList(ent, t, numsurfaces, surfacelist);
2120 }
2121
2122 void R_Q3BSP_DrawSky(entity_render_t *ent)
2123 {
2124         if (r_drawcollisionbrushes.integer < 2)
2125                 R_Q3BSP_DrawFaces(ent, true);
2126 }
2127
2128 void R_Q3BSP_Draw(entity_render_t *ent)
2129 {
2130         if (r_drawcollisionbrushes.integer < 2)
2131                 R_Q3BSP_DrawFaces(ent, false);
2132         if (r_drawcollisionbrushes.integer >= 1)
2133         {
2134                 int i;
2135                 model_t *model = ent->model;
2136                 msurface_t *surface;
2137                 q3mbrush_t *brush;
2138                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2139                 GL_DepthMask(false);
2140                 GL_DepthTest(true);
2141                 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
2142                 for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
2143                         if (brush->colbrushf && brush->colbrushf->numtriangles)
2144                                 R_DrawCollisionBrush(brush->colbrushf);
2145                 for (i = 0, surface = model->brush.data_surfaces + model->firstmodelsurface;i < model->nummodelsurfaces;i++, surface++)
2146                         if (surface->mesh.num_collisiontriangles)
2147                                 R_Q3BSP_DrawCollisionSurface(ent, surface);
2148                 qglPolygonOffset(0, 0);
2149         }
2150 }
2151
2152 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)
2153 {
2154         model_t *model = ent->model;
2155         vec3_t lightmins, lightmaxs;
2156         int t, leafindex, leafsurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
2157         const int *e;
2158         const float *v[3];
2159         msurface_t *surface;
2160         mleaf_t *leaf;
2161         const qbyte *pvs;
2162         lightmins[0] = relativelightorigin[0] - lightradius;
2163         lightmins[1] = relativelightorigin[1] - lightradius;
2164         lightmins[2] = relativelightorigin[2] - lightradius;
2165         lightmaxs[0] = relativelightorigin[0] + lightradius;
2166         lightmaxs[1] = relativelightorigin[1] + lightradius;
2167         lightmaxs[2] = relativelightorigin[2] + lightradius;
2168         *outnumclusterspointer = 0;
2169         *outnumsurfacespointer = 0;
2170         memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
2171         memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
2172         if (model == NULL)
2173         {
2174                 VectorCopy(lightmins, outmins);
2175                 VectorCopy(lightmaxs, outmaxs);
2176                 return;
2177         }
2178         VectorCopy(relativelightorigin, outmins);
2179         VectorCopy(relativelightorigin, outmaxs);
2180         if (model->brush.GetPVS)
2181                 pvs = model->brush.GetPVS(model, relativelightorigin);
2182         else
2183                 pvs = NULL;
2184         // FIXME: use BSP recursion as lights are often small
2185         for (leafindex = 0, leaf = model->brush.data_leafs;leafindex < model->brush.num_leafs;leafindex++, leaf++)
2186         {
2187                 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
2188                 {
2189                         outmins[0] = min(outmins[0], leaf->mins[0]);
2190                         outmins[1] = min(outmins[1], leaf->mins[1]);
2191                         outmins[2] = min(outmins[2], leaf->mins[2]);
2192                         outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
2193                         outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
2194                         outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
2195                         if (outclusterpvs)
2196                         {
2197                                 if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
2198                                 {
2199                                         SETPVSBIT(outclusterpvs, leaf->clusterindex);
2200                                         outclusterlist[outnumclusters++] = leaf->clusterindex;
2201                                 }
2202                         }
2203                         if (outsurfacepvs)
2204                         {
2205                                 for (leafsurfaceindex = 0;leafsurfaceindex < leaf->numleafsurfaces;leafsurfaceindex++)
2206                                 {
2207                                         surfaceindex = leaf->firstleafsurface[leafsurfaceindex];
2208                                         surface = model->brush.data_surfaces + surfaceindex;
2209                                         if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
2210                                         {
2211                                                 if (BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs) && !(surface->texture->surfaceparms & Q3SURFACEPARM_TRANS) && !(surface->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW)) && surface->mesh.num_triangles)
2212                                                 {
2213                                                         if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
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 (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                                                         else
2229                                                         {
2230                                                                 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
2231                                                                 {
2232                                                                         v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2233                                                                         v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2234                                                                         v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2235                                                                         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])))
2236                                                                         {
2237                                                                                 SETPVSBIT(outsurfacepvs, surfaceindex);
2238                                                                                 outsurfacelist[outnumsurfaces++] = surfaceindex;
2239                                                                                 break;
2240                                                                         }
2241                                                                 }
2242                                                         }
2243                                                 }
2244                                         }
2245                                 }
2246                         }
2247                 }
2248         }
2249
2250         // limit combined leaf box to light boundaries
2251         outmins[0] = max(outmins[0], lightmins[0]);
2252         outmins[1] = max(outmins[1], lightmins[1]);
2253         outmins[2] = max(outmins[2], lightmins[2]);
2254         outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
2255         outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
2256         outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
2257
2258         *outnumclusterspointer = outnumclusters;
2259         *outnumsurfacespointer = outnumsurfaces;
2260 }
2261
2262 void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
2263 {
2264         model_t *model = ent->model;
2265         vec3_t lightmins, lightmaxs;
2266         msurface_t *surface;
2267         int surfacelistindex;
2268         if (r_drawcollisionbrushes.integer < 2)
2269         {
2270                 lightmins[0] = relativelightorigin[0] - lightradius;
2271                 lightmins[1] = relativelightorigin[1] - lightradius;
2272                 lightmins[2] = relativelightorigin[2] - lightradius;
2273                 lightmaxs[0] = relativelightorigin[0] + lightradius;
2274                 lightmaxs[1] = relativelightorigin[1] + lightradius;
2275                 lightmaxs[2] = relativelightorigin[2] + lightradius;
2276                 R_Mesh_Matrix(&ent->matrix);
2277                 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
2278                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2279                 {
2280                         surface = model->brush.data_surfaces + surfacelist[surfacelistindex];
2281                         // FIXME: check some manner of surface->rendermode here?
2282                         if (!(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && !(surface->texture->surfaceparms & (Q3SURFACEPARM_SKY | Q3SURFACEPARM_TRANS)) && !(surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED))
2283                                 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);
2284                 }
2285                 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);
2286         }
2287 }
2288
2289 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)
2290 {
2291         model_t *model = ent->model;
2292         vec3_t lightmins, lightmaxs, modelorg;
2293         msurface_t *surface;
2294         int surfacelistindex;
2295         if (r_drawcollisionbrushes.integer < 2)
2296         {
2297                 lightmins[0] = relativelightorigin[0] - lightradius;
2298                 lightmins[1] = relativelightorigin[1] - lightradius;
2299                 lightmins[2] = relativelightorigin[2] - lightradius;
2300                 lightmaxs[0] = relativelightorigin[0] + lightradius;
2301                 lightmaxs[1] = relativelightorigin[1] + lightradius;
2302                 lightmaxs[2] = relativelightorigin[2] + lightradius;
2303                 R_Mesh_Matrix(&ent->matrix);
2304                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2305                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2306                 {
2307                         surface = model->brush.data_surfaces + surfacelist[surfacelistindex];
2308                         if (r_shadow_compilingrtlight)
2309                         {
2310                                 // if compiling an rtlight, capture the mesh
2311                                 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);
2312                         }
2313                         else if ((ent != r_refdef.worldentity || r_worldsurfacevisible[surfacelist[surfacelistindex]]) && !(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && surface->mesh.num_triangles)
2314                         {
2315                                 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
2316                                         qglDisable(GL_CULL_FACE);
2317                                 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);
2318                                 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
2319                                         qglEnable(GL_CULL_FACE);
2320                         }
2321                 }
2322         }
2323 }
2324
2325 #if 0
2326 static void gl_surf_start(void)
2327 {
2328 }
2329
2330 static void gl_surf_shutdown(void)
2331 {
2332 }
2333
2334 static void gl_surf_newmap(void)
2335 {
2336 }
2337 #endif
2338
2339 void GL_Surf_Init(void)
2340 {
2341         int i;
2342         dlightdivtable[0] = 4194304;
2343         for (i = 1;i < 32768;i++)
2344                 dlightdivtable[i] = 4194304 / (i << 7);
2345
2346         Cvar_RegisterVariable(&r_ambient);
2347         Cvar_RegisterVariable(&r_drawportals);
2348         Cvar_RegisterVariable(&r_testvis);
2349         Cvar_RegisterVariable(&r_floatbuildlightmap);
2350         Cvar_RegisterVariable(&r_detailtextures);
2351         Cvar_RegisterVariable(&r_surfaceworldnode);
2352         Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
2353         Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
2354         Cvar_RegisterVariable(&r_q3bsp_renderskydepth);
2355         Cvar_RegisterVariable(&gl_lightmaps);
2356
2357         //R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
2358 }
2359