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