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