changed rtlight compiled cluster list/pvs to leaf list/pvs to enable slightly more...
[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 cvar_t r_ambient = {0, "r_ambient", "0"};
28 cvar_t r_drawportals = {0, "r_drawportals", "0"};
29 cvar_t r_testvis = {0, "r_testvis", "0"};
30 cvar_t r_detailtextures = {CVAR_SAVE, "r_detailtextures", "1"};
31 cvar_t r_surfaceworldnode = {0, "r_surfaceworldnode", "0"};
32 cvar_t r_drawcollisionbrushes_polygonfactor = {0, "r_drawcollisionbrushes_polygonfactor", "-1"};
33 cvar_t r_drawcollisionbrushes_polygonoffset = {0, "r_drawcollisionbrushes_polygonoffset", "0"};
34 cvar_t r_q3bsp_renderskydepth = {0, "r_q3bsp_renderskydepth", "0"};
35 cvar_t gl_lightmaps = {0, "gl_lightmaps", "0"};
36
37 // flag arrays used for visibility checking on world model
38 // (all other entities have no per-surface/per-leaf visibility checks)
39 // TODO: dynamic resize according to r_refdef.worldmodel->brush.num_clusters
40 qbyte r_pvsbits[(32768+7)>>3];
41 // TODO: dynamic resize according to r_refdef.worldmodel->brush.num_leafs
42 qbyte r_worldleafvisible[32768];
43 // TODO: dynamic resize according to r_refdef.worldmodel->brush.num_surfaces
44 qbyte r_worldsurfacevisible[262144];
45
46 /*
47 ===============
48 R_BuildLightMap
49
50 Combine and scale multiple lightmaps into the 8.8 format in blocklights
51 ===============
52 */
53 static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surface)
54 {
55         int smax, tmax, i, j, size, size3, maps, stride, l;
56         unsigned int *bl, scale;
57         qbyte *lightmap, *out, *stain;
58         static unsigned int intblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
59         static qbyte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
60
61         // update cached lighting info
62         surface->cached_dlight = 0;
63
64         smax = (surface->lightmapinfo->extents[0]>>4)+1;
65         tmax = (surface->lightmapinfo->extents[1]>>4)+1;
66         size = smax*tmax;
67         size3 = size*3;
68         lightmap = surface->lightmapinfo->samples;
69
70 // set to full bright if no light data
71         bl = intblocklights;
72         if (!ent->model->brushq1.lightdata)
73         {
74                 for (i = 0;i < size3;i++)
75                         bl[i] = 255*256;
76         }
77         else
78         {
79 // clear to no light
80                 memset(bl, 0, size*3*sizeof(unsigned int));
81
82 // add all the lightmaps
83                 if (lightmap)
84                 {
85                         bl = intblocklights;
86                         for (maps = 0;maps < MAXLIGHTMAPS && surface->lightmapinfo->styles[maps] != 255;maps++, lightmap += size3)
87                                 for (scale = d_lightstylevalue[surface->lightmapinfo->styles[maps]], i = 0;i < size3;i++)
88                                         bl[i] += lightmap[i] * scale;
89                 }
90         }
91
92         stain = surface->lightmapinfo->stainsamples;
93         bl = intblocklights;
94         out = templight;
95         // the >> 16 shift adjusts down 8 bits to account for the stainmap
96         // scaling, and remaps the 0-65536 (2x overbright) to 0-256, it will
97         // be doubled during rendering to achieve 2x overbright
98         // (0 = 0.0, 128 = 1.0, 256 = 2.0)
99         if (ent->model->brushq1.lightmaprgba)
100         {
101                 stride = (surface->lightmapinfo->lightmaptexturestride - smax) * 4;
102                 for (i = 0;i < tmax;i++, out += stride)
103                 {
104                         for (j = 0;j < smax;j++)
105                         {
106                                 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
107                                 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
108                                 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
109                                 *out++ = 255;
110                         }
111                 }
112         }
113         else
114         {
115                 stride = (surface->lightmapinfo->lightmaptexturestride - smax) * 3;
116                 for (i = 0;i < tmax;i++, out += stride)
117                 {
118                         for (j = 0;j < smax;j++)
119                         {
120                                 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
121                                 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
122                                 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
123                         }
124                 }
125         }
126
127         R_UpdateTexture(surface->lightmaptexture, templight);
128 }
129
130 void R_StainNode (mnode_t *node, model_t *model, const vec3_t origin, float radius, const float fcolor[8])
131 {
132         float ndist, a, ratio, maxdist, maxdist2, maxdist3, invradius, sdtable[256], td, dist2;
133         msurface_t *surface, *endsurface;
134         int i, s, t, smax, tmax, smax3, impacts, impactt, stained;
135         qbyte *bl;
136         vec3_t impact;
137
138         maxdist = radius * radius;
139         invradius = 1.0f / radius;
140
141 loc0:
142         if (!node->plane)
143                 return;
144         ndist = PlaneDiff(origin, node->plane);
145         if (ndist > radius)
146         {
147                 node = node->children[0];
148                 goto loc0;
149         }
150         if (ndist < -radius)
151         {
152                 node = node->children[1];
153                 goto loc0;
154         }
155
156         dist2 = ndist * ndist;
157         maxdist3 = maxdist - dist2;
158
159         if (node->plane->type < 3)
160         {
161                 VectorCopy(origin, impact);
162                 impact[node->plane->type] -= ndist;
163         }
164         else
165         {
166                 impact[0] = origin[0] - node->plane->normal[0] * ndist;
167                 impact[1] = origin[1] - node->plane->normal[1] * ndist;
168                 impact[2] = origin[2] - node->plane->normal[2] * ndist;
169         }
170
171         for (surface = model->brush.data_surfaces + node->firstsurface, endsurface = surface + node->numsurfaces;surface < endsurface;surface++)
172         {
173                 if (surface->lightmapinfo->stainsamples)
174                 {
175                         smax = (surface->lightmapinfo->extents[0] >> 4) + 1;
176                         tmax = (surface->lightmapinfo->extents[1] >> 4) + 1;
177
178                         impacts = DotProduct (impact, surface->lightmapinfo->texinfo->vecs[0]) + surface->lightmapinfo->texinfo->vecs[0][3] - surface->lightmapinfo->texturemins[0];
179                         impactt = DotProduct (impact, surface->lightmapinfo->texinfo->vecs[1]) + surface->lightmapinfo->texinfo->vecs[1][3] - surface->lightmapinfo->texturemins[1];
180
181                         s = bound(0, impacts, smax * 16) - impacts;
182                         t = bound(0, impactt, tmax * 16) - impactt;
183                         i = s * s + t * t + dist2;
184                         if (i > maxdist)
185                                 continue;
186
187                         // reduce calculations
188                         for (s = 0, i = impacts; s < smax; s++, i -= 16)
189                                 sdtable[s] = i * i + dist2;
190
191                         bl = surface->lightmapinfo->stainsamples;
192                         smax3 = smax * 3;
193                         stained = false;
194
195                         i = impactt;
196                         for (t = 0;t < tmax;t++, i -= 16)
197                         {
198                                 td = i * i;
199                                 // make sure some part of it is visible on this line
200                                 if (td < maxdist3)
201                                 {
202                                         maxdist2 = maxdist - td;
203                                         for (s = 0;s < smax;s++)
204                                         {
205                                                 if (sdtable[s] < maxdist2)
206                                                 {
207                                                         ratio = lhrandom(0.0f, 1.0f);
208                                                         a = (fcolor[3] + ratio * fcolor[7]) * (1.0f - sqrt(sdtable[s] + td) * invradius);
209                                                         if (a >= (1.0f / 64.0f))
210                                                         {
211                                                                 if (a > 1)
212                                                                         a = 1;
213                                                                 bl[0] = (qbyte) ((float) bl[0] + a * ((fcolor[0] + ratio * fcolor[4]) - (float) bl[0]));
214                                                                 bl[1] = (qbyte) ((float) bl[1] + a * ((fcolor[1] + ratio * fcolor[5]) - (float) bl[1]));
215                                                                 bl[2] = (qbyte) ((float) bl[2] + a * ((fcolor[2] + ratio * fcolor[6]) - (float) bl[2]));
216                                                                 stained = true;
217                                                         }
218                                                 }
219                                                 bl += 3;
220                                         }
221                                 }
222                                 else // skip line
223                                         bl += smax3;
224                         }
225                         // force lightmap upload
226                         if (stained)
227                                 surface->cached_dlight = true;
228                 }
229         }
230
231         if (node->children[0]->plane)
232         {
233                 if (node->children[1]->plane)
234                 {
235                         R_StainNode(node->children[0], model, origin, radius, fcolor);
236                         node = node->children[1];
237                         goto loc0;
238                 }
239                 else
240                 {
241                         node = node->children[0];
242                         goto loc0;
243                 }
244         }
245         else if (node->children[1]->plane)
246         {
247                 node = node->children[1];
248                 goto loc0;
249         }
250 }
251
252 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)
253 {
254         int n;
255         float fcolor[8];
256         entity_render_t *ent;
257         model_t *model;
258         vec3_t org;
259         if (r_refdef.worldmodel == NULL || !r_refdef.worldmodel->brush.data_nodes || !r_refdef.worldmodel->brushq1.lightdata)
260                 return;
261         fcolor[0] = cr1;
262         fcolor[1] = cg1;
263         fcolor[2] = cb1;
264         fcolor[3] = ca1 * (1.0f / 64.0f);
265         fcolor[4] = cr2 - cr1;
266         fcolor[5] = cg2 - cg1;
267         fcolor[6] = cb2 - cb1;
268         fcolor[7] = (ca2 - ca1) * (1.0f / 64.0f);
269
270         R_StainNode(r_refdef.worldmodel->brush.data_nodes + r_refdef.worldmodel->brushq1.hulls[0].firstclipnode, r_refdef.worldmodel, origin, radius, fcolor);
271
272         // look for embedded bmodels
273         for (n = 0;n < cl_num_brushmodel_entities;n++)
274         {
275                 ent = &cl_entities[cl_brushmodel_entities[n]].render;
276                 model = ent->model;
277                 if (model && model->name[0] == '*')
278                 {
279                         Mod_CheckLoaded(model);
280                         if (model->brush.data_nodes)
281                         {
282                                 Matrix4x4_Transform(&ent->inversematrix, origin, org);
283                                 R_StainNode(model->brush.data_nodes + model->brushq1.hulls[0].firstclipnode, model, org, radius, fcolor);
284                         }
285                 }
286         }
287 }
288
289
290 /*
291 =============================================================
292
293         BRUSH MODELS
294
295 =============================================================
296 */
297
298 static void RSurf_DeformVertices(const entity_render_t *ent, const texture_t *texture, const msurface_t *surface, const vec3_t modelorg)
299 {
300         int i, j;
301         float center[3], forward[3], right[3], up[3], v[4][3];
302         matrix4x4_t matrix1, imatrix1;
303         if (texture->textureflags & Q3TEXTUREFLAG_AUTOSPRITE2)
304         {
305                 // a single autosprite surface can contain multiple sprites...
306                 VectorClear(forward);
307                 VectorClear(right);
308                 VectorSet(up, 0, 0, 1);
309                 for (j = 0;j < surface->num_vertices - 3;j += 4)
310                 {
311                         VectorClear(center);
312                         for (i = 0;i < 4;i++)
313                                 VectorAdd(center, (surface->groupmesh->data_vertex3f + 3 * surface->num_firstvertex) + (j+i) * 3, center);
314                         VectorScale(center, 0.25f, center);
315                         // FIXME: calculate vectors from triangle edges instead of using texture vectors as an easy way out?
316                         Matrix4x4_FromVectors(&matrix1, (surface->groupmesh->data_normal3f + 3 * surface->num_firstvertex) + j*3, (surface->groupmesh->data_svector3f + 3 * surface->num_firstvertex) + j*3, (surface->groupmesh->data_tvector3f + 3 * surface->num_firstvertex) + j*3, center);
317                         Matrix4x4_Invert_Simple(&imatrix1, &matrix1);
318                         for (i = 0;i < 4;i++)
319                                 Matrix4x4_Transform(&imatrix1, (surface->groupmesh->data_vertex3f + 3 * surface->num_firstvertex) + (j+i)*3, v[i]);
320                         forward[0] = modelorg[0] - center[0];
321                         forward[1] = modelorg[1] - center[1];
322                         VectorNormalize(forward);
323                         right[0] = forward[1];
324                         right[1] = -forward[0];
325                         for (i = 0;i < 4;i++)
326                                 VectorMAMAMAM(1, center, v[i][0], forward, v[i][1], right, v[i][2], up, varray_vertex3f + (surface->num_firstvertex+i+j) * 3);
327                 }
328         }
329         else if (texture->textureflags & Q3TEXTUREFLAG_AUTOSPRITE)
330         {
331                 Matrix4x4_Transform(&ent->inversematrix, r_viewforward, forward);
332                 Matrix4x4_Transform(&ent->inversematrix, r_viewright, right);
333                 Matrix4x4_Transform(&ent->inversematrix, r_viewup, up);
334                 // a single autosprite surface can contain multiple sprites...
335                 for (j = 0;j < surface->num_vertices - 3;j += 4)
336                 {
337                         VectorClear(center);
338                         for (i = 0;i < 4;i++)
339                                 VectorAdd(center, (surface->groupmesh->data_vertex3f + 3 * surface->num_firstvertex) + (j+i) * 3, center);
340                         VectorScale(center, 0.25f, center);
341                         // FIXME: calculate vectors from triangle edges instead of using texture vectors as an easy way out?
342                         Matrix4x4_FromVectors(&matrix1, (surface->groupmesh->data_normal3f + 3 * surface->num_firstvertex) + j*3, (surface->groupmesh->data_svector3f + 3 * surface->num_firstvertex) + j*3, (surface->groupmesh->data_tvector3f + 3 * surface->num_firstvertex) + j*3, center);
343                         Matrix4x4_Invert_Simple(&imatrix1, &matrix1);
344                         for (i = 0;i < 4;i++)
345                                 Matrix4x4_Transform(&imatrix1, (surface->groupmesh->data_vertex3f + 3 * surface->num_firstvertex) + (j+i)*3, v[i]);
346                         for (i = 0;i < 4;i++)
347                                 VectorMAMAMAM(1, center, v[i][0], forward, v[i][1], right, v[i][2], up, varray_vertex3f + (surface->num_firstvertex+i+j) * 3);
348                 }
349         }
350         else
351                 memcpy((varray_vertex3f + 3 * surface->num_firstvertex), (surface->groupmesh->data_vertex3f + 3 * surface->num_firstvertex), sizeof(float[3]) * surface->num_vertices);
352 }
353
354 // any sort of deformvertices call is *VERY* rare, so this must be optimized
355 // to skip deformvertices quickly!
356 #if 1
357 #define RSurf_GetVertexPointer(ent, texture, surface, modelorg) ((texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2)) ? (RSurf_DeformVertices(ent, texture, surface, modelorg), varray_vertex3f) : surface->groupmesh->data_vertex3f)
358 #else
359 static float *RSurf_GetVertexPointer(const entity_render_t *ent, const texture_t *texture, const msurface_t *surface, const vec3_t modelorg)
360 {
361         if (texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
362         {
363                 RSurf_DeformVertices(ent, texture, surface, modelorg);
364                 return varray_vertex3f;
365         }
366         else
367                 return surface->groupmesh->data_vertex3f;
368 }
369 #endif
370
371 void R_UpdateTextureInfo(const entity_render_t *ent, texture_t *t)
372 {
373         // we don't need to set currentframe if t->animated is false because
374         // it was already set up by the texture loader for non-animating
375         if (t->animated)
376         {
377                 t->currentframe = t->anim_frames[ent->frame != 0][(t->anim_total[ent->frame != 0] >= 2) ? ((int)(r_refdef.time * 5.0f) % t->anim_total[ent->frame != 0]) : 0];
378                 t = t->currentframe;
379         }
380         t->currentmaterialflags = t->basematerialflags;
381         t->currentalpha = ent->alpha;
382         if (t->basematerialflags & MATERIALFLAG_WATERALPHA)
383                 t->currentalpha *= r_wateralpha.value;
384         if (!(ent->flags & RENDER_LIGHT))
385                 t->currentmaterialflags |= MATERIALFLAG_FULLBRIGHT;
386         if (ent->effects & EF_ADDITIVE)
387                 t->currentmaterialflags |= MATERIALFLAG_ADD | MATERIALFLAG_TRANSPARENT;
388         else if (t->currentalpha < 1)
389                 t->currentmaterialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_TRANSPARENT;
390 }
391
392 matrix4x4_t r_surf_waterscrollmatrix;
393
394 void R_UpdateAllTextureInfo(entity_render_t *ent)
395 {
396         int i;
397         Matrix4x4_CreateTranslate(&r_surf_waterscrollmatrix, sin(r_refdef.time) * 0.025 * r_waterscroll.value, sin(r_refdef.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
398         if (ent->model)
399                 for (i = 0;i < ent->model->brush.num_textures;i++)
400                         R_UpdateTextureInfo(ent, ent->model->brush.data_textures + i);
401 }
402
403 static void R_DrawSurfaceList(const entity_render_t *ent, texture_t *texture, int texturenumsurfaces, const msurface_t **texturesurfacelist, const vec3_t modelorg)
404 {
405         int i;
406         int texturesurfaceindex;
407         const float *v, *vertex3f;
408         float *c;
409         float diff[3];
410         float f, r, g, b, a, base, colorscale;
411         const msurface_t *surface;
412         qboolean dolightmap;
413         qboolean dobase;
414         qboolean doambient;
415         qboolean dodetail;
416         qboolean doglow;
417         qboolean dofogpass;
418         qboolean fogallpasses;
419         qboolean waterscrolling;
420         surfmesh_t *groupmesh;
421         rtexture_t *lightmaptexture;
422         rmeshstate_t m;
423         texture = texture->currentframe;
424         if (texture->currentmaterialflags & MATERIALFLAG_NODRAW)
425                 return;
426         c_faces += texturenumsurfaces;
427         // gl_lightmaps debugging mode skips normal texturing
428         if (gl_lightmaps.integer)
429         {
430                 GL_BlendFunc(GL_ONE, GL_ZERO);
431                 GL_DepthMask(true);
432                 GL_DepthTest(true);
433                 qglDisable(GL_CULL_FACE);
434                 GL_Color(1, 1, 1, 1);
435                 memset(&m, 0, sizeof(m));
436                 R_Mesh_State(&m);
437                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
438                 {
439                         surface = texturesurfacelist[texturesurfaceindex];
440                         R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
441                         R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordlightmap2f);
442                         R_Mesh_ColorPointer(surface->lightmaptexture ? NULL : surface->groupmesh->data_lightmapcolor4f);
443                         R_Mesh_VertexPointer(surface->groupmesh->data_vertex3f);
444                         GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
445                         R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
446                         GL_LockArrays(0, 0);
447                 }
448                 qglEnable(GL_CULL_FACE);
449                 return;
450         }
451         GL_DepthTest(!(texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
452         GL_DepthMask(!(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT));
453         if (texture->currentmaterialflags & MATERIALFLAG_ADD)
454                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
455         else if (texture->currentmaterialflags & MATERIALFLAG_ALPHA)
456                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
457         else
458                 GL_BlendFunc(GL_ONE, GL_ZERO);
459         // water waterscrolling in texture matrix
460         waterscrolling = (texture->currentmaterialflags & MATERIALFLAG_WATER) && r_waterscroll.value != 0;
461         if (texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
462                 qglDisable(GL_CULL_FACE);
463         if (texture->currentmaterialflags & MATERIALFLAG_SKY)
464         {
465                 if (skyrendernow)
466                 {
467                         skyrendernow = false;
468                         if (skyrendermasked)
469                                 R_Sky();
470                 }
471                 // LordHavoc: HalfLife maps have freaky skypolys...
472                 //if (!ent->model->brush.ishlbsp)
473                 {
474                         R_Mesh_Matrix(&ent->matrix);
475                         GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
476                         if (skyrendermasked)
477                         {
478                                 // depth-only (masking)
479                                 GL_ColorMask(0,0,0,0);
480                                 // just to make sure that braindead drivers don't draw anything
481                                 // despite that colormask...
482                                 GL_BlendFunc(GL_ZERO, GL_ONE);
483                         }
484                         else
485                         {
486                                 // fog sky
487                                 GL_BlendFunc(GL_ONE, GL_ZERO);
488                         }
489                         GL_DepthMask(true);
490                         GL_DepthTest(true);
491                         memset(&m, 0, sizeof(m));
492                         R_Mesh_State(&m);
493                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
494                         {
495                                 surface = texturesurfacelist[texturesurfaceindex];
496                                 R_Mesh_VertexPointer(surface->groupmesh->data_vertex3f);
497                                 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
498                                 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
499                                 GL_LockArrays(0, 0);
500                         }
501                         GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
502                 }
503         }
504         else if ((texture->currentmaterialflags & MATERIALFLAG_WATER) && r_watershader.value && gl_textureshader && !texture->skin.glow && !fogenabled && ent->colormod[0] == 1 && ent->colormod[1] == 1 && ent->colormod[2] == 1)
505         {
506                 // NVIDIA Geforce3 distortion texture shader on water
507                 float args[4] = {0.05f,0,0,0.04f};
508                 memset(&m, 0, sizeof(m));
509                 m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(r_refdef.time * 16)&63]);
510                 m.tex[1] = R_GetTexture(texture->skin.base);
511                 m.texcombinergb[0] = GL_REPLACE;
512                 m.texcombinergb[1] = GL_REPLACE;
513                 Matrix4x4_CreateFromQuakeEntity(&m.texmatrix[0], 0, 0, 0, 0, 0, 0, r_watershader.value);
514                 m.texmatrix[1] = r_surf_waterscrollmatrix;
515                 R_Mesh_State(&m);
516
517                 GL_Color(1, 1, 1, texture->currentalpha);
518                 GL_ActiveTexture(0);
519                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
520                 GL_ActiveTexture(1);
521                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
522                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
523                 qglTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
524                 qglEnable(GL_TEXTURE_SHADER_NV);
525
526                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
527                 {
528                         surface = texturesurfacelist[texturesurfaceindex];
529                         R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, texture, surface, modelorg));
530                         R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
531                         R_Mesh_TexCoordPointer(1, 2, surface->groupmesh->data_texcoordtexture2f);
532                         GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
533                         R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
534                         GL_LockArrays(0, 0);
535                 }
536
537                 qglDisable(GL_TEXTURE_SHADER_NV);
538                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
539                 GL_ActiveTexture(0);
540         }
541         else if (texture->currentmaterialflags & (MATERIALFLAG_WATER | MATERIALFLAG_WALL))
542         {
543                 // normal surface (wall or water)
544                 dobase = true;
545                 dolightmap = !(texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT);
546                 doambient = r_ambient.value >= (1/64.0f);
547                 dodetail = r_detailtextures.integer && texture->skin.detail != NULL && !(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT);
548                 doglow = texture->skin.glow != NULL;
549                 dofogpass = fogenabled && !(texture->currentmaterialflags & MATERIALFLAG_ADD);
550                 fogallpasses = fogenabled && !(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT);
551                 if (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT)
552                 {
553                         if (dobase && dolightmap && gl_combine.integer)
554                         {
555                                 dobase = false;
556                                 memset(&m, 0, sizeof(m));
557                                 m.tex[1] = R_GetTexture(texture->skin.base);
558                                 if (waterscrolling)
559                                         m.texmatrix[1] = r_surf_waterscrollmatrix;
560                                 m.texrgbscale[1] = 2;
561                                 m.pointer_color = varray_color4f;
562                                 R_Mesh_State(&m);
563                                 colorscale = 1;
564                                 r = ent->colormod[0] * colorscale;
565                                 g = ent->colormod[1] * colorscale;
566                                 b = ent->colormod[2] * colorscale;
567                                 a = texture->currentalpha;
568                                 base = r_ambient.value * (1.0f / 64.0f);
569                                 // q3bsp has no lightmap updates, so the lightstylevalue that
570                                 // would normally be baked into the lightmaptexture must be
571                                 // applied to the color
572                                 if (ent->model->brushq1.lightdata)
573                                 {
574                                         float scale = d_lightstylevalue[0] * (1.0f / 128.0f);
575                                         r *= scale;
576                                         g *= scale;
577                                         b *= scale;
578                                 }
579                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
580                                 {
581                                         surface = texturesurfacelist[texturesurfaceindex];
582                                         vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
583                                         R_Mesh_VertexPointer(vertex3f);
584                                         R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordlightmap2f);
585                                         R_Mesh_TexCoordPointer(1, 2, surface->groupmesh->data_texcoordtexture2f);
586                                         if (surface->lightmaptexture)
587                                         {
588                                                 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
589                                                 if (fogallpasses)
590                                                 {
591                                                         R_Mesh_ColorPointer(varray_color4f);
592                                                         for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
593                                                         {
594                                                                 VectorSubtract(v, modelorg, diff);
595                                                                 f = 1 - exp(fogdensity/DotProduct(diff, diff));
596                                                                 c[0] = f * r;
597                                                                 c[1] = f * g;
598                                                                 c[2] = f * b;
599                                                                 c[3] = a;
600                                                         }
601                                                 }
602                                                 else
603                                                 {
604                                                         R_Mesh_ColorPointer(NULL);
605                                                         GL_Color(r, g, b, a);
606                                                 }
607                                         }
608                                         else
609                                         {
610                                                 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
611                                                 R_Mesh_ColorPointer(varray_color4f);
612                                                 if (!surface->lightmaptexture)
613                                                 {
614                                                         for (i = 0, c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, c += 4)
615                                                         {
616                                                                 c[0] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+0] * r;
617                                                                 c[1] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+1] * g;
618                                                                 c[2] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+2] * b;
619                                                                 c[3] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+3] * a;
620                                                         }
621                                                         if (fogallpasses)
622                                                         {
623                                                                 for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
624                                                                 {
625                                                                         VectorSubtract(v, modelorg, diff);
626                                                                         f = 1 - exp(fogdensity/DotProduct(diff, diff));
627                                                                         VectorScale(c, f, c);
628                                                                 }
629                                                         }
630                                                 }
631                                                 else
632                                                 {
633                                                         R_Mesh_ColorPointer(NULL);
634                                                         GL_Color(0, 0, 0, a);
635                                                 }
636                                         }
637                                         GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
638                                         R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
639                                         GL_LockArrays(0, 0);
640                                 }
641                         }
642                         if (dobase)
643                         {
644                                 dobase = false;
645                                 memset(&m, 0, sizeof(m));
646                                 m.tex[0] = R_GetTexture(texture->skin.base);
647                                 if (waterscrolling)
648                                         m.texmatrix[0] = r_surf_waterscrollmatrix;
649                                 m.pointer_color = varray_color4f;
650                                 colorscale = 1;
651                                 if (gl_combine.integer)
652                                 {
653                                         m.texrgbscale[0] = 4;
654                                         colorscale *= 0.25f;
655                                 }
656                                 R_Mesh_State(&m);
657                                 r = ent->colormod[0] * colorscale;
658                                 g = ent->colormod[1] * colorscale;
659                                 b = ent->colormod[2] * colorscale;
660                                 a = texture->currentalpha;
661                                 if (dolightmap)
662                                 {
663                                         // q3bsp has no lightmap updates, so the lightstylevalue that
664                                         // would normally be baked into the lightmaptexture must be
665                                         // applied to the color
666                                         if (!ent->model->brushq1.lightdata)
667                                         {
668                                                 float scale = d_lightstylevalue[0] * (1.0f / 128.0f);
669                                                 r *= scale;
670                                                 g *= scale;
671                                                 b *= scale;
672                                         }
673                                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
674                                         {
675                                                 surface = texturesurfacelist[texturesurfaceindex];
676                                                 vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
677                                                 R_Mesh_VertexPointer(vertex3f);
678                                                 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
679                                                 for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
680                                                 {
681                                                         c[0] = 0;
682                                                         c[1] = 0;
683                                                         c[2] = 0;
684                                                         if (!surface->lightmapinfo)
685                                                                 VectorCopy((surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex) + i*4, c);
686                                                         else //if (surface->lightmapinfo)
687                                                         {
688                                                                 const qbyte *lm = surface->lightmapinfo->samples + (surface->groupmesh->data_lightmapoffsets + surface->num_firstvertex)[i];
689                                                                 float scale = d_lightstylevalue[surface->lightmapinfo->styles[0]] * (1.0f / 32768.0f);
690                                                                 VectorMA(c, scale, lm, c);
691                                                                 if (surface->lightmapinfo->styles[1] != 255)
692                                                                 {
693                                                                         int size3 = ((surface->lightmapinfo->extents[0]>>4)+1)*((surface->lightmapinfo->extents[1]>>4)+1)*3;
694                                                                         lm += size3;
695                                                                         scale = d_lightstylevalue[surface->lightmapinfo->styles[1]] * (1.0f / 32768.0f);
696                                                                         VectorMA(c, scale, lm, c);
697                                                                         if (surface->lightmapinfo->styles[2] != 255)
698                                                                         {
699                                                                                 lm += size3;
700                                                                                 scale = d_lightstylevalue[surface->lightmapinfo->styles[2]] * (1.0f / 32768.0f);
701                                                                                 VectorMA(c, scale, lm, c);
702                                                                                 if (surface->lightmapinfo->styles[3] != 255)
703                                                                                 {
704                                                                                         lm += size3;
705                                                                                         scale = d_lightstylevalue[surface->lightmapinfo->styles[3]] * (1.0f / 32768.0f);
706                                                                                         VectorMA(c, scale, lm, c);
707                                                                                 }
708                                                                         }
709                                                                 }
710                                                         }
711                                                         c[0] *= r;
712                                                         c[1] *= g;
713                                                         c[2] *= b;
714                                                         if (fogallpasses)
715                                                         {
716                                                                 VectorSubtract(v, modelorg, diff);
717                                                                 f = 1 - exp(fogdensity/DotProduct(diff, diff));
718                                                                 VectorScale(c, f, c);
719                                                         }
720                                                         if (!surface->lightmapinfo && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
721                                                                 c[3] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+3] * a;
722                                                         else
723                                                                 c[3] = a;
724                                                 }
725                                                 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
726                                                 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
727                                                 GL_LockArrays(0, 0);
728                                         }
729                                 }
730                                 else
731                                 {
732                                         if (fogallpasses)
733                                         {
734                                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
735                                                 {
736                                                         surface = texturesurfacelist[texturesurfaceindex];
737                                                         vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
738                                                         R_Mesh_VertexPointer(vertex3f);
739                                                         R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
740                                                         if (!surface->lightmapinfo && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
741                                                         {
742                                                                 R_Mesh_ColorPointer(varray_color4f);
743                                                                 for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
744                                                                 {
745                                                                         VectorSubtract(v, modelorg, diff);
746                                                                         f = 1 - exp(fogdensity/DotProduct(diff, diff));
747                                                                         c[0] = r * f;
748                                                                         c[1] = g * f;
749                                                                         c[2] = b * f;
750                                                                         c[3] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+3] * a;
751                                                                 }
752                                                         }
753                                                         else
754                                                         {
755                                                                 R_Mesh_ColorPointer(varray_color4f);
756                                                                 for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
757                                                                 {
758                                                                         VectorSubtract(v, modelorg, diff);
759                                                                         f = 1 - exp(fogdensity/DotProduct(diff, diff));
760                                                                         c[0] = r * f;
761                                                                         c[1] = g * f;
762                                                                         c[2] = b * f;
763                                                                         c[3] = a;
764                                                                 }
765                                                         }
766                                                         GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
767                                                         R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
768                                                         GL_LockArrays(0, 0);
769                                                 }
770                                         }
771                                         else
772                                         {
773                                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
774                                                 {
775                                                         surface = texturesurfacelist[texturesurfaceindex];
776                                                         vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
777                                                         R_Mesh_VertexPointer(vertex3f);
778                                                         R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
779                                                         if (!surface->lightmaptexture && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
780                                                         {
781                                                                 R_Mesh_ColorPointer(varray_color4f);
782                                                                 for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
783                                                                 {
784                                                                         c[0] = r;
785                                                                         c[1] = g;
786                                                                         c[2] = b;
787                                                                         c[3] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+3] * a;
788                                                                 }
789                                                         }
790                                                         else
791                                                         {
792                                                                 R_Mesh_ColorPointer(NULL);
793                                                                 GL_Color(r, g, b, a);
794                                                         }
795                                                         GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
796                                                         R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
797                                                         GL_LockArrays(0, 0);
798                                                 }
799                                         }
800                                 }
801                         }
802                 }
803                 else
804                 {
805                         if (!dolightmap && dobase)
806                         {
807                                 dolightmap = false;
808                                 dobase = false;
809                                 GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], 1);
810                                 memset(&m, 0, sizeof(m));
811                                 m.tex[0] = R_GetTexture(texture->skin.base);
812                                 if (waterscrolling)
813                                         m.texmatrix[0] = r_surf_waterscrollmatrix;
814                                 R_Mesh_State(&m);
815                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
816                                 {
817                                         surface = texturesurfacelist[texturesurfaceindex];
818                                         R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, texture, surface, modelorg));
819                                         R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
820                                         GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
821                                         R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
822                                         GL_LockArrays(0, 0);
823                                 }
824                         }
825                         if (r_lightmapintensity <= 0 && dolightmap && dobase)
826                         {
827                                 dolightmap = false;
828                                 dobase = false;
829                                 GL_Color(0, 0, 0, 1);
830                                 memset(&m, 0, sizeof(m));
831                                 R_Mesh_State(&m);
832                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
833                                 {
834                                         surface = texturesurfacelist[texturesurfaceindex];
835                                         R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, texture, surface, modelorg));
836                                         GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
837                                         R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
838                                         GL_LockArrays(0, 0);
839                                 }
840                         }
841                         if (r_textureunits.integer >= 2 && gl_combine.integer && dolightmap && dobase)
842                         {
843                                 // dualtexture combine
844                                 GL_BlendFunc(GL_ONE, GL_ZERO);
845                                 GL_DepthMask(true);
846                                 dolightmap = false;
847                                 dobase = false;
848                                 memset(&m, 0, sizeof(m));
849                                 m.tex[1] = R_GetTexture(texture->skin.base);
850                                 if (waterscrolling)
851                                         m.texmatrix[1] = r_surf_waterscrollmatrix;
852                                 m.texrgbscale[1] = 2;
853                                 R_Mesh_State(&m);
854                                 r = ent->colormod[0] * r_lightmapintensity;
855                                 g = ent->colormod[1] * r_lightmapintensity;
856                                 b = ent->colormod[2] * r_lightmapintensity;
857                                 GL_Color(r, g, b, 1);
858                                 if (texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
859                                 {
860                                         R_Mesh_VertexPointer(varray_vertex3f);
861                                         if (r == 1 && g == 1 && b == 1)
862                                         {
863                                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
864                                                 {
865                                                         surface = texturesurfacelist[texturesurfaceindex];
866                                                         RSurf_DeformVertices(ent, texture, surface, modelorg);
867                                                         R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordlightmap2f);
868                                                         R_Mesh_TexCoordPointer(1, 2, surface->groupmesh->data_texcoordtexture2f);
869                                                         if (surface->lightmaptexture)
870                                                         {
871                                                                 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
872                                                                 R_Mesh_ColorPointer(NULL);
873                                                         }
874                                                         else //if (r == 1 && g == 1 && b == 1)
875                                                         {
876                                                                 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
877                                                                 R_Mesh_ColorPointer(surface->groupmesh->data_lightmapcolor4f);
878                                                         }
879                                                         GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
880                                                         R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
881                                                         GL_LockArrays(0, 0);
882                                                 }
883                                         }
884                                         else
885                                         {
886                                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
887                                                 {
888                                                         surface = texturesurfacelist[texturesurfaceindex];
889                                                         RSurf_DeformVertices(ent, texture, surface, modelorg);
890                                                         R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordlightmap2f);
891                                                         R_Mesh_TexCoordPointer(1, 2, surface->groupmesh->data_texcoordtexture2f);
892                                                         if (surface->lightmaptexture)
893                                                         {
894                                                                 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
895                                                                 R_Mesh_ColorPointer(NULL);
896                                                         }
897                                                         else
898                                                         {
899                                                                 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
900                                                                 R_Mesh_ColorPointer(varray_color4f);
901                                                                 for (i = 0, c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, c += 4)
902                                                                 {
903                                                                         c[0] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+0] * r;
904                                                                         c[1] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+1] * g;
905                                                                         c[2] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+2] * b;
906                                                                         c[3] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+3];
907                                                                 }
908                                                         }
909                                                         GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
910                                                         R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
911                                                         GL_LockArrays(0, 0);
912                                                 }
913                                         }
914                                 }
915                                 else
916                                 {
917                                         if (r == 1 && g == 1 && b == 1)
918                                         {
919 #if 0
920                                                 // experimental direct state calls for measuring
921                                                 // R_Mesh_ call overhead, do not use!
922                                                 R_Mesh_VertexPointer(varray_vertex3f);
923                                                 R_Mesh_TexCoordPointer(0, 2, varray_texcoord2f[0]);
924                                                 R_Mesh_TexCoordPointer(1, 2, varray_texcoord2f[1]);
925                                                 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
926                                                 R_Mesh_ColorPointer(varray_color4f);
927                                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
928                                                 {
929                                                         surface = texturesurfacelist[texturesurfaceindex];
930                                                         qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), surface->groupmesh->data_vertex3f);
931                                                         qglClientActiveTexture(GL_TEXTURE0_ARB);
932                                                         qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), surface->groupmesh->data_texcoordlightmap2f);
933                                                         qglClientActiveTexture(GL_TEXTURE1_ARB);
934                                                         qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), surface->groupmesh->data_texcoordtexture2f);
935                                                         if (surface->lightmaptexture)
936                                                         {
937                                                                 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
938                                                                 qglDisableClientState(GL_COLOR_ARRAY);
939                                                                 qglColor4f(r, g, b, 1);
940                                                         }
941                                                         else //if (r == 1 && g == 1 && b == 1)
942                                                         {
943                                                                 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
944                                                                 qglEnableClientState(GL_COLOR_ARRAY);
945                                                                 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), surface->groupmesh->data_lightmapcolor4f);
946                                                         }
947                                                         qglLockArraysEXT(0, surface->num_vertices);
948                                                         qglDrawRangeElements(GL_TRIANGLES, surface->num_firstvertex, surface->num_firstvertex + surface->num_vertices, surface->num_triangles * 3, GL_UNSIGNED_INT, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
949                                                         qglUnlockArraysEXT();
950                                                 }
951 #else
952                                                 groupmesh = NULL;
953                                                 lightmaptexture = NULL;
954                                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
955                                                 {
956                                                         surface = texturesurfacelist[texturesurfaceindex];
957                                                         if (groupmesh != surface->groupmesh)
958                                                         {
959                                                                 groupmesh = surface->groupmesh;
960                                                                 R_Mesh_VertexPointer(groupmesh->data_vertex3f);
961                                                                 R_Mesh_TexCoordPointer(0, 2, groupmesh->data_texcoordlightmap2f);
962                                                                 R_Mesh_TexCoordPointer(1, 2, groupmesh->data_texcoordtexture2f);
963                                                                 if (!lightmaptexture)
964                                                                         R_Mesh_ColorPointer(groupmesh->data_lightmapcolor4f);
965                                                         }
966                                                         if (lightmaptexture != surface->lightmaptexture)
967                                                         {
968                                                                 lightmaptexture = surface->lightmaptexture;
969                                                                 if (lightmaptexture)
970                                                                 {
971                                                                         R_Mesh_TexBind(0, R_GetTexture(lightmaptexture));
972                                                                         R_Mesh_ColorPointer(NULL);
973                                                                 }
974                                                                 else //if (r == 1 && g == 1 && b == 1)
975                                                                 {
976                                                                         R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
977                                                                         R_Mesh_ColorPointer(surface->groupmesh->data_lightmapcolor4f);
978                                                                 }
979                                                         }
980                                                         GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
981                                                         R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
982                                                         GL_LockArrays(0, 0);
983                                                 }
984 #endif
985                                         }
986                                         else
987                                         {
988                                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
989                                                 {
990                                                         surface = texturesurfacelist[texturesurfaceindex];
991                                                         R_Mesh_VertexPointer(surface->groupmesh->data_vertex3f);
992                                                         R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordlightmap2f);
993                                                         R_Mesh_TexCoordPointer(1, 2, surface->groupmesh->data_texcoordtexture2f);
994                                                         if (surface->lightmaptexture)
995                                                         {
996                                                                 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
997                                                                 R_Mesh_ColorPointer(NULL);
998                                                         }
999                                                         else
1000                                                         {
1001                                                                 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
1002                                                                 R_Mesh_ColorPointer(varray_color4f);
1003                                                                 for (i = 0, c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, c += 4)
1004                                                                 {
1005                                                                         c[0] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+0] * r;
1006                                                                         c[1] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+1] * g;
1007                                                                         c[2] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+2] * b;
1008                                                                         c[3] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+3];
1009                                                                 }
1010                                                         }
1011                                                         GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1012                                                         R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1013                                                         GL_LockArrays(0, 0);
1014                                                 }
1015                                         }
1016                                 }
1017                         }
1018                         // single texture
1019                         if (dolightmap)
1020                         {
1021                                 GL_BlendFunc(GL_ONE, GL_ZERO);
1022                                 GL_DepthMask(true);
1023                                 GL_Color(1, 1, 1, 1);
1024                                 memset(&m, 0, sizeof(m));
1025                                 R_Mesh_State(&m);
1026                                 if (texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
1027                                 {
1028                                         R_Mesh_VertexPointer(varray_vertex3f);
1029                                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1030                                         {
1031                                                 surface = texturesurfacelist[texturesurfaceindex];
1032                                                 RSurf_DeformVertices(ent, texture, surface, modelorg);
1033                                                 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordlightmap2f);
1034                                                 if (surface->lightmaptexture)
1035                                                 {
1036                                                         R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
1037                                                         R_Mesh_ColorPointer(NULL);
1038                                                 }
1039                                                 else //if (r == 1 && g == 1 && b == 1)
1040                                                 {
1041                                                         R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
1042                                                         R_Mesh_ColorPointer(surface->groupmesh->data_lightmapcolor4f);
1043                                                 }
1044                                                 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1045                                                 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1046                                                 GL_LockArrays(0, 0);
1047                                         }
1048                                 }
1049                                 else
1050                                 {
1051                                         groupmesh = NULL;
1052                                         lightmaptexture = NULL;
1053                                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1054                                         {
1055                                                 surface = texturesurfacelist[texturesurfaceindex];
1056                                                 if (groupmesh != surface->groupmesh)
1057                                                 {
1058                                                         groupmesh = surface->groupmesh;
1059                                                         R_Mesh_VertexPointer(groupmesh->data_vertex3f);
1060                                                         R_Mesh_TexCoordPointer(0, 2, groupmesh->data_texcoordlightmap2f);
1061                                                         if (!lightmaptexture)
1062                                                                 R_Mesh_ColorPointer(groupmesh->data_lightmapcolor4f);
1063                                                 }
1064                                                 if (lightmaptexture != surface->lightmaptexture)
1065                                                 {
1066                                                         lightmaptexture = surface->lightmaptexture;
1067                                                         if (lightmaptexture)
1068                                                         {
1069                                                                 R_Mesh_TexBind(0, R_GetTexture(lightmaptexture));
1070                                                                 R_Mesh_ColorPointer(NULL);
1071                                                         }
1072                                                         else //if (r == 1 && g == 1 && b == 1)
1073                                                         {
1074                                                                 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
1075                                                                 R_Mesh_ColorPointer(surface->groupmesh->data_lightmapcolor4f);
1076                                                         }
1077                                                 }
1078                                                 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1079                                                 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1080                                                 GL_LockArrays(0, 0);
1081                                         }
1082                                 }
1083                         }
1084                         if (dobase)
1085                         {
1086                                 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1087                                 GL_DepthMask(false);
1088                                 GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
1089                                 memset(&m, 0, sizeof(m));
1090                                 m.tex[0] = R_GetTexture(texture->skin.base);
1091                                 if (waterscrolling)
1092                                         m.texmatrix[0] = r_surf_waterscrollmatrix;
1093                                 R_Mesh_State(&m);
1094                                 if (texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
1095                                 {
1096                                         R_Mesh_VertexPointer(varray_vertex3f);
1097                                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1098                                         {
1099                                                 surface = texturesurfacelist[texturesurfaceindex];
1100                                                 RSurf_DeformVertices(ent, texture, surface, modelorg);
1101                                                 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
1102                                                 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1103                                                 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1104                                                 GL_LockArrays(0, 0);
1105                                         }
1106                                 }
1107                                 else
1108                                 {
1109                                         groupmesh = NULL;
1110                                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1111                                         {
1112                                                 surface = texturesurfacelist[texturesurfaceindex];
1113                                                 if (groupmesh != surface->groupmesh)
1114                                                 {
1115                                                         groupmesh = surface->groupmesh;
1116                                                         R_Mesh_VertexPointer(groupmesh->data_vertex3f);
1117                                                         R_Mesh_TexCoordPointer(0, 2, groupmesh->data_texcoordtexture2f);
1118                                                 }
1119                                                 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1120                                                 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1121                                                 GL_LockArrays(0, 0);
1122                                         }
1123                                 }
1124                         }
1125                 }
1126                 if (doambient)
1127                 {
1128                         doambient = false;
1129                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1130                         GL_DepthMask(false);
1131                         memset(&m, 0, sizeof(m));
1132                         m.tex[0] = R_GetTexture(texture->skin.base);
1133                         if (waterscrolling)
1134                                 m.texmatrix[0] = r_surf_waterscrollmatrix;
1135                         m.pointer_color = varray_color4f;
1136                         colorscale = 1;
1137                         if (gl_combine.integer)
1138                         {
1139                                 m.texrgbscale[0] = 4;
1140                                 colorscale *= 0.25f;
1141                         }
1142                         R_Mesh_State(&m);
1143                         base = r_ambient.value * (1.0f / 64.0f);
1144                         r = ent->colormod[0] * colorscale * base;
1145                         g = ent->colormod[1] * colorscale * base;
1146                         b = ent->colormod[2] * colorscale * base;
1147                         a = texture->currentalpha;
1148                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1149                         {
1150                                 surface = texturesurfacelist[texturesurfaceindex];
1151                                 vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
1152                                 R_Mesh_VertexPointer(vertex3f);
1153                                 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
1154                                 for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
1155                                 {
1156                                         c[0] = r;
1157                                         c[1] = g;
1158                                         c[2] = b;
1159                                         if (fogallpasses)
1160                                         {
1161                                                 VectorSubtract(v, modelorg, diff);
1162                                                 f = 1 - exp(fogdensity/DotProduct(diff, diff));
1163                                                 VectorScale(c, f, c);
1164                                         }
1165                                         if (!surface->lightmaptexture && surface->groupmesh->data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1166                                                 c[3] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+3] * a;
1167                                         else
1168                                                 c[3] = a;
1169                                 }
1170                                 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1171                                 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1172                                 GL_LockArrays(0, 0);
1173                         }
1174                 }
1175                 if (dodetail)
1176                 {
1177                         GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1178                         GL_DepthMask(false);
1179                         GL_Color(1, 1, 1, 1);
1180                         memset(&m, 0, sizeof(m));
1181                         m.tex[0] = R_GetTexture(texture->skin.detail);
1182                         R_Mesh_State(&m);
1183                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1184                         {
1185                                 surface = texturesurfacelist[texturesurfaceindex];
1186                                 R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, texture, surface, modelorg));
1187                                 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoorddetail2f);
1188                                 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1189                                 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1190                                 GL_LockArrays(0, 0);
1191                         }
1192                 }
1193                 if (doglow)
1194                 {
1195                         // if glow was not already done using multitexture, do it now.
1196                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1197                         GL_DepthMask(false);
1198                         memset(&m, 0, sizeof(m));
1199                         m.tex[0] = R_GetTexture(texture->skin.glow);
1200                         if (waterscrolling)
1201                                 m.texmatrix[0] = r_surf_waterscrollmatrix;
1202                         m.pointer_color = varray_color4f;
1203                         R_Mesh_State(&m);
1204                         colorscale = 1;
1205                         r = ent->colormod[0] * colorscale;
1206                         g = ent->colormod[1] * colorscale;
1207                         b = ent->colormod[2] * colorscale;
1208                         a = texture->currentalpha;
1209                         if (fogallpasses)
1210                         {
1211                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1212                                 {
1213                                         surface = texturesurfacelist[texturesurfaceindex];
1214                                         vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
1215                                         R_Mesh_VertexPointer(vertex3f);
1216                                         R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
1217                                         R_Mesh_ColorPointer(varray_color4f);
1218                                         if (!surface->lightmaptexture && surface->groupmesh->data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1219                                         {
1220                                                 for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
1221                                                 {
1222                                                         VectorSubtract(v, modelorg, diff);
1223                                                         f = 1 - exp(fogdensity/DotProduct(diff, diff));
1224                                                         c[0] = f * r;
1225                                                         c[1] = f * g;
1226                                                         c[2] = f * b;
1227                                                         c[3] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+3] * a;
1228                                                 }
1229                                         }
1230                                         else
1231                                         {
1232                                                 for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
1233                                                 {
1234                                                         VectorSubtract(v, modelorg, diff);
1235                                                         f = 1 - exp(fogdensity/DotProduct(diff, diff));
1236                                                         c[0] = f * r;
1237                                                         c[1] = f * g;
1238                                                         c[2] = f * b;
1239                                                         c[3] = a;
1240                                                 }
1241                                         }
1242                                         GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1243                                         R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1244                                         GL_LockArrays(0, 0);
1245                                 }
1246                         }
1247                         else
1248                         {
1249                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1250                                 {
1251                                         surface = texturesurfacelist[texturesurfaceindex];
1252                                         vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
1253                                         R_Mesh_VertexPointer(vertex3f);
1254                                         R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
1255                                         if (!surface->lightmaptexture && surface->groupmesh->data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1256                                         {
1257                                                 R_Mesh_ColorPointer(varray_color4f);
1258                                                 for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
1259                                                 {
1260                                                         c[0] = r;
1261                                                         c[1] = g;
1262                                                         c[2] = b;
1263                                                         c[3] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+3] * a;
1264                                                 }
1265                                         }
1266                                         else
1267                                         {
1268                                                 R_Mesh_ColorPointer(NULL);
1269                                                 GL_Color(r, g, b, a);
1270                                         }
1271                                         GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1272                                         R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1273                                         GL_LockArrays(0, 0);
1274                                 }
1275                         }
1276                 }
1277                 if (dofogpass)
1278                 {
1279                         // if this is opaque use alpha blend which will darken the earlier
1280                         // passes cheaply.
1281                         //
1282                         // if this is an alpha blended material, all the earlier passes
1283                         // were darkened by fog already, so we only need to add the fog
1284                         // color ontop through the fog mask texture
1285                         //
1286                         // if this is an additive blended material, all the earlier passes
1287                         // were darkened by fog already, and we should not add fog color
1288                         // (because the background was not darkened, there is no fog color
1289                         // that was lost behind it).
1290                         if (!fogallpasses)
1291                                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1292                         else
1293                                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1294                         GL_DepthMask(false);
1295                         memset(&m, 0, sizeof(m));
1296                         m.tex[0] = R_GetTexture(texture->skin.fog);
1297                         if (waterscrolling)
1298                                 m.texmatrix[0] = r_surf_waterscrollmatrix;
1299                         R_Mesh_State(&m);
1300                         r = fogcolor[0];
1301                         g = fogcolor[1];
1302                         b = fogcolor[2];
1303                         a = texture->currentalpha;
1304                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1305                         {
1306                                 surface = texturesurfacelist[texturesurfaceindex];
1307                                 vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
1308                                 R_Mesh_VertexPointer(vertex3f);
1309                                 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
1310                                 R_Mesh_ColorPointer(varray_color4f);
1311                                 //RSurf_FogPassColors_Vertex3f_Color4f((surface->groupmesh->data_vertex3f + 3 * surface->num_firstvertex), varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], texture->currentalpha, 1, surface->num_vertices, modelorg);
1312                                 if (!surface->lightmaptexture && surface->groupmesh->data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1313                                 {
1314                                         for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
1315                                         {
1316                                                 VectorSubtract(v, modelorg, diff);
1317                                                 f = exp(fogdensity/DotProduct(diff, diff));
1318                                                 c[0] = r;
1319                                                 c[1] = g;
1320                                                 c[2] = b;
1321                                                 c[3] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+3] * f * a;
1322                                         }
1323                                 }
1324                                 else
1325                                 {
1326                                         for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
1327                                         {
1328                                                 VectorSubtract(v, modelorg, diff);
1329                                                 f = exp(fogdensity/DotProduct(diff, diff));
1330                                                 c[0] = r;
1331                                                 c[1] = g;
1332                                                 c[2] = b;
1333                                                 c[3] = f * a;
1334                                         }
1335                                 }
1336                                 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1337                                 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1338                                 GL_LockArrays(0, 0);
1339                         }
1340                 }
1341         }
1342         if (texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1343                 qglEnable(GL_CULL_FACE);
1344 }
1345
1346 static void RSurfShader_Transparent_Callback(const void *calldata1, int calldata2)
1347 {
1348         const entity_render_t *ent = calldata1;
1349         const msurface_t *surface = ent->model->brush.data_surfaces + calldata2;
1350         vec3_t modelorg;
1351         texture_t *texture;
1352
1353         texture = surface->texture;
1354         if (texture->basematerialflags & MATERIALFLAG_SKY)
1355                 return; // transparent sky is too difficult
1356         R_UpdateTextureInfo(ent, texture);
1357
1358         R_Mesh_Matrix(&ent->matrix);
1359         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1360         R_DrawSurfaceList(ent, texture, 1, &surface, modelorg);
1361 }
1362
1363 void R_QueueSurfaceList(entity_render_t *ent, texture_t *texture, int texturenumsurfaces, const msurface_t **texturesurfacelist, const vec3_t modelorg)
1364 {
1365         int texturesurfaceindex;
1366         const msurface_t *surface;
1367         vec3_t tempcenter, center;
1368         if (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT)
1369         {
1370                 // drawing sky transparently would be too difficult
1371                 if (!(texture->currentmaterialflags & MATERIALFLAG_SKY))
1372                 {
1373                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1374                         {
1375                                 surface = texturesurfacelist[texturesurfaceindex];
1376                                 tempcenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
1377                                 tempcenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
1378                                 tempcenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
1379                                 Matrix4x4_Transform(&ent->matrix, tempcenter, center);
1380                                 R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_vieworigin : center, RSurfShader_Transparent_Callback, ent, surface - ent->model->brush.data_surfaces);
1381                         }
1382                 }
1383         }
1384         else
1385                 R_DrawSurfaceList(ent, texture, texturenumsurfaces, texturesurfacelist, modelorg);
1386 }
1387
1388 void R_DrawSurfaces(entity_render_t *ent, qboolean skysurfaces)
1389 {
1390         int i, j, f, flagsmask;
1391         msurface_t *surface, **surfacechain;
1392         texture_t *t, *texture;
1393         model_t *model = ent->model;
1394         vec3_t modelorg;
1395         const int maxsurfacelist = 1024;
1396         int numsurfacelist = 0;
1397         const msurface_t *surfacelist[1024];
1398         if (model == NULL)
1399                 return;
1400         R_Mesh_Matrix(&ent->matrix);
1401         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1402
1403         // update light styles
1404         if (!skysurfaces && model->brushq1.light_styleupdatechains)
1405         {
1406                 for (i = 0;i < model->brushq1.light_styles;i++)
1407                 {
1408                         if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1409                         {
1410                                 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1411                                 if ((surfacechain = model->brushq1.light_styleupdatechains[i]))
1412                                         for (;(surface = *surfacechain);surfacechain++)
1413                                                 surface->cached_dlight = true;
1414                         }
1415                 }
1416         }
1417
1418         R_UpdateAllTextureInfo(ent);
1419         flagsmask = skysurfaces ? MATERIALFLAG_SKY : (MATERIALFLAG_WATER | MATERIALFLAG_WALL);
1420         f = 0;
1421         t = NULL;
1422         texture = NULL;
1423         numsurfacelist = 0;
1424         if (ent == r_refdef.worldentity)
1425         {
1426                 for (i = 0, j = model->firstmodelsurface, surface = model->brush.data_surfaces + j;i < model->nummodelsurfaces;i++, j++, surface++)
1427                 {
1428                         if (!r_worldsurfacevisible[j])
1429                                 continue;
1430                         if (t != surface->texture)
1431                         {
1432                                 if (numsurfacelist)
1433                                 {
1434                                         R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1435                                         numsurfacelist = 0;
1436                                 }
1437                                 t = surface->texture;
1438                                 f = t->currentmaterialflags & flagsmask;
1439                                 texture = t->currentframe;
1440                         }
1441                         if (f && surface->num_triangles)
1442                         {
1443                                 // if lightmap parameters changed, rebuild lightmap texture
1444                                 if (surface->cached_dlight && surface->lightmapinfo->samples)
1445                                         R_BuildLightMap(ent, surface);
1446                                 // add face to draw list
1447                                 surfacelist[numsurfacelist++] = surface;
1448                                 if (numsurfacelist >= maxsurfacelist)
1449                                 {
1450                                         R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1451                                         numsurfacelist = 0;
1452                                 }
1453                         }
1454                 }
1455         }
1456         else
1457         {
1458                 for (i = 0, j = model->firstmodelsurface, surface = model->brush.data_surfaces + j;i < model->nummodelsurfaces;i++, j++, surface++)
1459                 {
1460                         if (t != surface->texture)
1461                         {
1462                                 if (numsurfacelist)
1463                                 {
1464                                         R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1465                                         numsurfacelist = 0;
1466                                 }
1467                                 t = surface->texture;
1468                                 f = t->currentmaterialflags & flagsmask;
1469                                 texture = t->currentframe;
1470                         }
1471                         if (f && surface->num_triangles)
1472                         {
1473                                 // if lightmap parameters changed, rebuild lightmap texture
1474                                 if (surface->cached_dlight && surface->lightmapinfo->samples)
1475                                         R_BuildLightMap(ent, surface);
1476                                 // add face to draw list
1477                                 surfacelist[numsurfacelist++] = surface;
1478                                 if (numsurfacelist >= maxsurfacelist)
1479                                 {
1480                                         R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1481                                         numsurfacelist = 0;
1482                                 }
1483                         }
1484                 }
1485         }
1486         if (numsurfacelist)
1487                 R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1488 }
1489
1490 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1491 {
1492         int i;
1493         float *v;
1494         rmeshstate_t m;
1495         const mportal_t *portal = calldata1;
1496         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1497         GL_DepthMask(false);
1498         GL_DepthTest(true);
1499         R_Mesh_Matrix(&r_identitymatrix);
1500
1501         memset(&m, 0, sizeof(m));
1502         m.pointer_vertex = varray_vertex3f;
1503         R_Mesh_State(&m);
1504
1505         i = calldata2;
1506         GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1507                          ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1508                          ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1509                          0.125f);
1510         if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1511         {
1512                 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1513                         VectorCopy(portal->points[i].position, v);
1514         }
1515         else
1516                 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1517                         VectorCopy(portal->points[i].position, v);
1518         GL_LockArrays(0, portal->numpoints);
1519         R_Mesh_Draw(0, portal->numpoints, portal->numpoints - 2, polygonelements);
1520         GL_LockArrays(0, 0);
1521 }
1522
1523 // LordHavoc: this is just a nice debugging tool, very slow
1524 static void R_DrawPortals(void)
1525 {
1526         int i, leafnum;//, portalnum;
1527         mportal_t *portal;
1528         float center[3], f;
1529         model_t *model = r_refdef.worldmodel;
1530         if (model == NULL)
1531                 return;
1532         for (leafnum = 0;leafnum < r_refdef.worldmodel->brush.num_leafs;leafnum++)
1533         {
1534                 if (r_worldleafvisible[leafnum])
1535                 {
1536                         //for (portalnum = 0, portal = model->brush.data_portals;portalnum < model->brush.num_portals;portalnum++, portal++)
1537                         for (portal = r_refdef.worldmodel->brush.data_leafs[leafnum].portals;portal;portal = portal->next)
1538                         {
1539                                 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1540                                 if (!R_CullBox(portal->mins, portal->maxs))
1541                                 {
1542                                         VectorClear(center);
1543                                         for (i = 0;i < portal->numpoints;i++)
1544                                                 VectorAdd(center, portal->points[i].position, center);
1545                                         f = ixtable[portal->numpoints];
1546                                         VectorScale(center, f, center);
1547                                         //R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, portal, portalnum);
1548                                         R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, portal, leafnum);
1549                                 }
1550                         }
1551                 }
1552         }
1553 }
1554
1555 static void R_DrawCollisionBrush(colbrushf_t *brush)
1556 {
1557         int i;
1558         rmeshstate_t m;
1559         memset(&m, 0, sizeof(m));
1560         m.pointer_vertex = brush->points->v;
1561         R_Mesh_State(&m);
1562         i = (int)(((size_t)brush) / sizeof(colbrushf_t));
1563         GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1564         GL_LockArrays(0, brush->numpoints);
1565         R_Mesh_Draw(0, brush->numpoints, brush->numtriangles, brush->elements);
1566         GL_LockArrays(0, 0);
1567 }
1568
1569 static void R_DrawCollisionSurface(entity_render_t *ent, msurface_t *surface)
1570 {
1571         int i;
1572         rmeshstate_t m;
1573         if (!surface->num_collisiontriangles)
1574                 return;
1575         memset(&m, 0, sizeof(m));
1576         m.pointer_vertex = surface->data_collisionvertex3f;
1577         R_Mesh_State(&m);
1578         i = (int)(((size_t)surface) / sizeof(msurface_t));
1579         GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1580         GL_LockArrays(0, surface->num_collisionvertices);
1581         R_Mesh_Draw(0, surface->num_collisionvertices, surface->num_collisiontriangles, surface->data_collisionelement3i);
1582         GL_LockArrays(0, 0);
1583 }
1584
1585 void R_WorldVisibility(void)
1586 {
1587         int i, j, *mark;
1588         mleaf_t *leaf;
1589         mleaf_t *viewleaf;
1590         model_t *model = r_refdef.worldmodel;
1591
1592         if (!model)
1593                 return;
1594
1595         // if possible find the leaf the view origin is in
1596         viewleaf = model->brush.PointInLeaf ? model->brush.PointInLeaf(model, r_vieworigin) : NULL;
1597         // if possible fetch the visible cluster bits
1598         if (model->brush.FatPVS)
1599                 model->brush.FatPVS(model, r_vieworigin, 2, r_pvsbits, sizeof(r_pvsbits));
1600
1601         // clear the visible surface and leaf flags arrays
1602         memset(r_worldsurfacevisible, 0, model->brush.num_surfaces);
1603         memset(r_worldleafvisible, 0, model->brush.num_leafs);
1604
1605         // if the user prefers surfaceworldnode (testing?) or the viewleaf could
1606         // not be found, or the viewleaf is not part of the visible world
1607         // (floating around in the void), use the pvs method
1608         if (r_surfaceworldnode.integer || !viewleaf || viewleaf->clusterindex < 0)
1609         {
1610                 // pvs method:
1611                 // similar to quake's RecursiveWorldNode but without cache misses
1612                 for (j = 0, leaf = model->brush.data_leafs;j < model->brush.num_leafs;j++, leaf++)
1613                 {
1614                         // if leaf is in current pvs and on the screen, mark its surfaces
1615                         if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
1616                         {
1617                                 c_leafs++;
1618                                 r_worldleafvisible[j] = true;
1619                                 if (leaf->numleafsurfaces)
1620                                         for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
1621                                                 r_worldsurfacevisible[*mark] = true;
1622                         }
1623                 }
1624         }
1625         else
1626         {
1627                 int leafstackpos;
1628                 mportal_t *p;
1629                 mleaf_t *leafstack[8192];
1630                 // portal method:
1631                 // follows portals leading outward from viewleaf, does not venture
1632                 // offscreen or into leafs that are not visible, faster than Quake's
1633                 // RecursiveWorldNode and vastly better in unvised maps, often culls a
1634                 // lot of surface that pvs alone would miss
1635                 leafstack[0] = viewleaf;
1636                 leafstackpos = 1;
1637                 while (leafstackpos)
1638                 {
1639                         c_leafs++;
1640                         leaf = leafstack[--leafstackpos];
1641                         r_worldleafvisible[leaf - model->brush.data_leafs] = true;
1642                         // mark any surfaces bounding this leaf
1643                         if (leaf->numleafsurfaces)
1644                                 for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
1645                                         r_worldsurfacevisible[*mark] = true;
1646                         // follow portals into other leafs
1647                         // the checks are:
1648                         // if viewer is behind portal (portal faces outward into the scene)
1649                         // and the portal polygon's bounding box is on the screen
1650                         // and the leaf has not been visited yet
1651                         // and the leaf is visible in the pvs
1652                         // (the first two checks won't cause as many cache misses as the leaf checks)
1653                         for (p = leaf->portals;p;p = p->next)
1654                                 if (DotProduct(r_vieworigin, p->plane.normal) < (p->plane.dist + 1) && !R_CullBox(p->mins, p->maxs) && !r_worldleafvisible[p->past - model->brush.data_leafs] && CHECKPVSBIT(r_pvsbits, p->past->clusterindex))
1655                                         leafstack[leafstackpos++] = p->past;
1656                 }
1657         }
1658
1659         if (r_drawportals.integer)
1660                 R_DrawPortals();
1661 }
1662
1663 void R_Q1BSP_DrawSky(entity_render_t *ent)
1664 {
1665         if (ent->model == NULL)
1666                 return;
1667         if (r_drawcollisionbrushes.integer < 2)
1668                 R_DrawSurfaces(ent, true);
1669 }
1670
1671 void R_Q1BSP_Draw(entity_render_t *ent)
1672 {
1673         if (ent->model == NULL)
1674                 return;
1675         c_bmodels++;
1676         if (r_drawcollisionbrushes.integer < 2)
1677                 R_DrawSurfaces(ent, false);
1678         if (r_drawcollisionbrushes.integer >= 1 && ent->model->brush.num_brushes)
1679         {
1680                 int i;
1681                 model_t *model = ent->model;
1682                 msurface_t *surface;
1683                 q3mbrush_t *brush;
1684                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1685                 GL_DepthMask(false);
1686                 GL_DepthTest(true);
1687                 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
1688                 for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
1689                         if (brush->colbrushf && brush->colbrushf->numtriangles)
1690                                 R_DrawCollisionBrush(brush->colbrushf);
1691                 for (i = 0, surface = model->brush.data_surfaces + model->firstmodelsurface;i < model->nummodelsurfaces;i++, surface++)
1692                         if (surface->num_collisiontriangles)
1693                                 R_DrawCollisionSurface(ent, surface);
1694                 qglPolygonOffset(0, 0);
1695         }
1696 }
1697
1698 typedef struct r_q1bsp_getlightinfo_s
1699 {
1700         model_t *model;
1701         vec3_t relativelightorigin;
1702         float lightradius;
1703         int *outleaflist;
1704         qbyte *outleafpvs;
1705         int outnumleafs;
1706         int *outsurfacelist;
1707         qbyte *outsurfacepvs;
1708         int outnumsurfaces;
1709         vec3_t outmins;
1710         vec3_t outmaxs;
1711         vec3_t lightmins;
1712         vec3_t lightmaxs;
1713         const qbyte *pvs;
1714 }
1715 r_q1bsp_getlightinfo_t;
1716
1717 void R_Q1BSP_RecursiveGetLightInfo(r_q1bsp_getlightinfo_t *info, mnode_t *node)
1718 {
1719         int sides;
1720         mleaf_t *leaf;
1721         for (;;)
1722         {
1723                 if (!BoxesOverlap(info->lightmins, info->lightmaxs, node->mins, node->maxs))
1724                         return;
1725                 if (!node->plane)
1726                         break;
1727                 sides = BoxOnPlaneSide(info->lightmins, info->lightmaxs, node->plane) - 1;
1728                 if (sides == 2)
1729                 {
1730                         R_Q1BSP_RecursiveGetLightInfo(info, node->children[0]);
1731                         node = node->children[1];
1732                 }
1733                 else
1734                         node = node->children[sides];
1735         }
1736         leaf = (mleaf_t *)node;
1737         if (info->pvs == NULL || CHECKPVSBIT(info->pvs, leaf->clusterindex))
1738         {
1739                 info->outmins[0] = min(info->outmins[0], leaf->mins[0]);
1740                 info->outmins[1] = min(info->outmins[1], leaf->mins[1]);
1741                 info->outmins[2] = min(info->outmins[2], leaf->mins[2]);
1742                 info->outmaxs[0] = max(info->outmaxs[0], leaf->maxs[0]);
1743                 info->outmaxs[1] = max(info->outmaxs[1], leaf->maxs[1]);
1744                 info->outmaxs[2] = max(info->outmaxs[2], leaf->maxs[2]);
1745                 if (info->outleafpvs)
1746                 {
1747                         int leafindex = leaf - info->model->brush.data_leafs;
1748                         if (!CHECKPVSBIT(info->outleafpvs, leafindex))
1749                         {
1750                                 SETPVSBIT(info->outleafpvs, leafindex);
1751                                 info->outleaflist[info->outnumleafs++] = leafindex;
1752                         }
1753                 }
1754                 if (info->outsurfacepvs)
1755                 {
1756                         int leafsurfaceindex;
1757                         for (leafsurfaceindex = 0;leafsurfaceindex < leaf->numleafsurfaces;leafsurfaceindex++)
1758                         {
1759                                 int surfaceindex = leaf->firstleafsurface[leafsurfaceindex];
1760                                 if (!CHECKPVSBIT(info->outsurfacepvs, surfaceindex))
1761                                 {
1762                                         msurface_t *surface = info->model->brush.data_surfaces + surfaceindex;
1763                                         if (BoxesOverlap(info->lightmins, info->lightmaxs, surface->mins, surface->maxs))
1764                                         if ((surface->texture->currentmaterialflags & (MATERIALFLAG_WALL | MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
1765                                         {
1766                                                 int triangleindex, t;
1767                                                 const int *e;
1768                                                 const vec_t *v[3];
1769                                                 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = info->model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->num_triangles;triangleindex++, t++, e += 3)
1770                                                 {
1771                                                         v[0] = info->model->brush.shadowmesh->vertex3f + e[0] * 3;
1772                                                         v[1] = info->model->brush.shadowmesh->vertex3f + e[1] * 3;
1773                                                         v[2] = info->model->brush.shadowmesh->vertex3f + e[2] * 3;
1774                                                         if (info->lightmaxs[0] > min(v[0][0], min(v[1][0], v[2][0])) && info->lightmins[0] < max(v[0][0], max(v[1][0], v[2][0])) && info->lightmaxs[1] > min(v[0][1], min(v[1][1], v[2][1])) && info->lightmins[1] < max(v[0][1], max(v[1][1], v[2][1])) && info->lightmaxs[2] > min(v[0][2], min(v[1][2], v[2][2])) && info->lightmins[2] < max(v[0][2], max(v[1][2], v[2][2])))
1775                                                         {
1776                                                                 SETPVSBIT(info->outsurfacepvs, surfaceindex);
1777                                                                 info->outsurfacelist[info->outnumsurfaces++] = surfaceindex;
1778                                                                 break;
1779                                                         }
1780                                                 }
1781                                         }
1782                                 }
1783                         }
1784                 }
1785         }
1786 }
1787
1788 void R_Q1BSP_GetLightInfo(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, vec3_t outmins, vec3_t outmaxs, int *outleaflist, qbyte *outleafpvs, int *outnumleafspointer, int *outsurfacelist, qbyte *outsurfacepvs, int *outnumsurfacespointer)
1789 {
1790         r_q1bsp_getlightinfo_t info;
1791         VectorCopy(relativelightorigin, info.relativelightorigin);
1792         info.lightradius = lightradius;
1793         info.lightmins[0] = info.relativelightorigin[0] - info.lightradius;
1794         info.lightmins[1] = info.relativelightorigin[1] - info.lightradius;
1795         info.lightmins[2] = info.relativelightorigin[2] - info.lightradius;
1796         info.lightmaxs[0] = info.relativelightorigin[0] + info.lightradius;
1797         info.lightmaxs[1] = info.relativelightorigin[1] + info.lightradius;
1798         info.lightmaxs[2] = info.relativelightorigin[2] + info.lightradius;
1799         if (ent->model == NULL)
1800         {
1801                 VectorCopy(info.lightmins, outmins);
1802                 VectorCopy(info.lightmaxs, outmaxs);
1803                 *outnumleafspointer = 0;
1804                 *outnumsurfacespointer = 0;
1805                 return;
1806         }
1807         info.model = ent->model;
1808         info.outleaflist = outleaflist;
1809         info.outleafpvs = outleafpvs;
1810         info.outnumleafs = 0;
1811         info.outsurfacelist = outsurfacelist;
1812         info.outsurfacepvs = outsurfacepvs;
1813         info.outnumsurfaces = 0;
1814         VectorCopy(info.relativelightorigin, info.outmins);
1815         VectorCopy(info.relativelightorigin, info.outmaxs);
1816         memset(outleafpvs, 0, (info.model->brush.num_leafs + 7) >> 3);
1817         memset(outsurfacepvs, 0, (info.model->nummodelsurfaces + 7) >> 3);
1818         if (info.model->brush.GetPVS)
1819                 info.pvs = info.model->brush.GetPVS(info.model, info.relativelightorigin);
1820         else
1821                 info.pvs = NULL;
1822         R_UpdateAllTextureInfo(ent);
1823         /*
1824         if (r_shadow_compilingrtlight)
1825         {
1826                 Portal_Visibility(info.model, info.relativelightorigin, leafmark, surfacemark, const mplane_t *frustumplanes, int numfrustumplanes, int exact, const float *boxmins, const float *boxmaxs, float *updateleafsmins, float *updateleafsmaxs)
1827         }
1828         else
1829         */
1830         {
1831                 // use BSP recursion as lights are often small
1832                 R_Q1BSP_RecursiveGetLightInfo(&info, info.model->brush.data_nodes);
1833         }
1834
1835         // limit combined leaf box to light boundaries
1836         outmins[0] = max(info.outmins[0], info.lightmins[0]);
1837         outmins[1] = max(info.outmins[1], info.lightmins[1]);
1838         outmins[2] = max(info.outmins[2], info.lightmins[2]);
1839         outmaxs[0] = min(info.outmaxs[0], info.lightmaxs[0]);
1840         outmaxs[1] = min(info.outmaxs[1], info.lightmaxs[1]);
1841         outmaxs[2] = min(info.outmaxs[2], info.lightmaxs[2]);
1842
1843         *outnumleafspointer = info.outnumleafs;
1844         *outnumsurfacespointer = info.outnumsurfaces;
1845 }
1846
1847 void R_Q1BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist, const vec3_t lightmins, const vec3_t lightmaxs)
1848 {
1849         model_t *model = ent->model;
1850         msurface_t *surface;
1851         int surfacelistindex;
1852         if (r_drawcollisionbrushes.integer < 2)
1853         {
1854                 R_Mesh_Matrix(&ent->matrix);
1855                 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
1856                 if (!r_shadow_compilingrtlight)
1857                         R_UpdateAllTextureInfo(ent);
1858                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1859                 {
1860                         surface = model->brush.data_surfaces + surfacelist[surfacelistindex];
1861                         if ((surface->texture->currentmaterialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT | MATERIALFLAG_WALL)) != MATERIALFLAG_WALL)
1862                                 continue;
1863                         if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1864                                 continue;
1865                         R_Shadow_MarkVolumeFromBox(surface->num_firstshadowmeshtriangle, surface->num_triangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, relativelightorigin, lightmins, lightmaxs, surface->mins, surface->maxs);
1866                 }
1867                 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);
1868         }
1869 }
1870
1871 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, int visiblelighting)
1872 {
1873         model_t *model = ent->model;
1874         msurface_t *surface;
1875         texture_t *t;
1876         int surfacelistindex;
1877         if (r_drawcollisionbrushes.integer < 2)
1878         {
1879                 R_Mesh_Matrix(&ent->matrix);
1880                 if (!r_shadow_compilingrtlight)
1881                         R_UpdateAllTextureInfo(ent);
1882                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1883                 {
1884                         surface = model->brush.data_surfaces + surfacelist[surfacelistindex];
1885                         if (surface->texture->basematerialflags & MATERIALFLAG_NODRAW || !surface->num_triangles)
1886                                 continue;
1887                         if (r_shadow_compilingrtlight)
1888                         {
1889                                 // if compiling an rtlight, capture the mesh
1890                                 t = surface->texture;
1891                                 if ((t->basematerialflags & (MATERIALFLAG_WALL | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
1892                                         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->groupmesh->data_vertex3f, surface->groupmesh->data_svector3f, surface->groupmesh->data_tvector3f, surface->groupmesh->data_normal3f, surface->groupmesh->data_texcoordtexture2f, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1893                         }
1894                         else if (ent != r_refdef.worldentity || r_worldsurfacevisible[surfacelist[surfacelistindex]])
1895                         {
1896                                 t = surface->texture->currentframe;
1897                                 // FIXME: transparent surfaces need to be lit later
1898                                 if ((t->currentmaterialflags & (MATERIALFLAG_WALL | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
1899                                 {
1900                                         if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1901                                                 qglDisable(GL_CULL_FACE);
1902                                         R_Shadow_RenderLighting(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle), surface->groupmesh->data_vertex3f, surface->groupmesh->data_svector3f, surface->groupmesh->data_tvector3f, surface->groupmesh->data_normal3f, surface->groupmesh->data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightcolor, NULL, NULL, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.base, NULL, NULL, t->skin.nmap, t->skin.gloss, lightcubemap, ambientscale, diffusescale, specularscale, visiblelighting);
1903                                         if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1904                                                 qglEnable(GL_CULL_FACE);
1905                                 }
1906                         }
1907                 }
1908         }
1909 }
1910
1911 #if 0
1912 static void gl_surf_start(void)
1913 {
1914 }
1915
1916 static void gl_surf_shutdown(void)
1917 {
1918 }
1919
1920 static void gl_surf_newmap(void)
1921 {
1922 }
1923 #endif
1924
1925 void GL_Surf_Init(void)
1926 {
1927
1928         Cvar_RegisterVariable(&r_ambient);
1929         Cvar_RegisterVariable(&r_drawportals);
1930         Cvar_RegisterVariable(&r_testvis);
1931         Cvar_RegisterVariable(&r_detailtextures);
1932         Cvar_RegisterVariable(&r_surfaceworldnode);
1933         Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
1934         Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
1935         Cvar_RegisterVariable(&r_q3bsp_renderskydepth);
1936         Cvar_RegisterVariable(&gl_lightmaps);
1937
1938         //R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
1939 }
1940