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