fix 3 NULL vertex pointer crashes (caused by R_Mesh_State lingering where it shouldn't)
[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, *vertex3f;
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                 R_Mesh_State(&m);
412                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
413                 {
414                         surface = texturesurfacelist[texturesurfaceindex];
415                         R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
416                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordlightmap2f);
417                         R_Mesh_ColorPointer(surface->lightmaptexture ? NULL : surface->mesh.data_lightmapcolor4f);
418                         R_Mesh_VertexPointer(surface->mesh.data_vertex3f);
419                         GL_LockArrays(0, surface->mesh.num_vertices);
420                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
421                         GL_LockArrays(0, 0);
422                 }
423                 qglEnable(GL_CULL_FACE);
424                 return;
425         }
426         GL_DepthTest(!(texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
427         GL_DepthMask(!(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT));
428         if (texture->currentmaterialflags & MATERIALFLAG_ADD)
429                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
430         else if (texture->currentmaterialflags & MATERIALFLAG_ALPHA)
431                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
432         else
433                 GL_BlendFunc(GL_ONE, GL_ZERO);
434         // water waterscrolling in texture matrix
435         waterscrolling = (texture->currentmaterialflags & MATERIALFLAG_WATER) && r_waterscroll.value != 0;
436         if (texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
437                 qglDisable(GL_CULL_FACE);
438         if (texture->currentmaterialflags & MATERIALFLAG_SKY)
439         {
440                 if (skyrendernow)
441                 {
442                         skyrendernow = false;
443                         if (skyrendermasked)
444                                 R_Sky();
445                 }
446                 // LordHavoc: HalfLife maps have freaky skypolys...
447                 if (!ent->model->brush.ishlbsp)
448                 {
449                         R_Mesh_Matrix(&ent->matrix);
450                         GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
451                         if (skyrendermasked)
452                         {
453                                 // depth-only (masking)
454                                 GL_ColorMask(0,0,0,0);
455                                 // just to make sure that braindead drivers don't draw anything
456                                 // despite that colormask...
457                                 GL_BlendFunc(GL_ZERO, GL_ONE);
458                         }
459                         else
460                         {
461                                 // fog sky
462                                 GL_BlendFunc(GL_ONE, GL_ZERO);
463                         }
464                         GL_DepthMask(true);
465                         GL_DepthTest(true);
466                         memset(&m, 0, sizeof(m));
467                         R_Mesh_State(&m);
468                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
469                         {
470                                 surface = texturesurfacelist[texturesurfaceindex];
471                                 R_Mesh_VertexPointer(surface->mesh.data_vertex3f);
472                                 GL_LockArrays(0, surface->mesh.num_vertices);
473                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
474                                 GL_LockArrays(0, 0);
475                         }
476                         GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
477                 }
478         }
479         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)
480         {
481                 // NVIDIA Geforce3 distortion texture shader on water
482                 float args[4] = {0.05f,0,0,0.04f};
483                 memset(&m, 0, sizeof(m));
484                 m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(r_refdef.time * 16)&63]);
485                 m.tex[1] = R_GetTexture(texture->skin.base);
486                 m.texcombinergb[0] = GL_REPLACE;
487                 m.texcombinergb[1] = GL_REPLACE;
488                 Matrix4x4_CreateFromQuakeEntity(&m.texmatrix[0], 0, 0, 0, 0, 0, 0, r_watershader.value);
489                 m.texmatrix[1] = r_surf_waterscrollmatrix;
490                 R_Mesh_State(&m);
491
492                 GL_Color(1, 1, 1, texture->currentalpha);
493                 GL_ActiveTexture(0);
494                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
495                 GL_ActiveTexture(1);
496                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
497                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
498                 qglTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
499                 qglEnable(GL_TEXTURE_SHADER_NV);
500
501                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
502                 {
503                         surface = texturesurfacelist[texturesurfaceindex];
504                         R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, surface));
505                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
506                         R_Mesh_TexCoordPointer(1, 2, surface->mesh.data_texcoordtexture2f);
507                         GL_LockArrays(0, surface->mesh.num_vertices);
508                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
509                         GL_LockArrays(0, 0);
510                 }
511
512                 qglDisable(GL_TEXTURE_SHADER_NV);
513                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
514                 GL_ActiveTexture(0);
515         }
516         else if (texture->currentmaterialflags & (MATERIALFLAG_WATER | MATERIALFLAG_WALL))
517         {
518                 // normal surface (wall or water)
519                 dobase = true;
520                 dolightmap = !(texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT);
521                 doambient = r_ambient.value >= (1/64.0f);
522                 dodetail = r_detailtextures.integer && texture->skin.detail != NULL && !(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT);
523                 doglow = texture->skin.glow != NULL;
524                 dofogpass = fogenabled && !(texture->currentmaterialflags & MATERIALFLAG_ADD);
525                 fogallpasses = fogenabled && !(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT);
526                 if (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT)
527                 {
528                         if (dobase && dolightmap && gl_combine.integer)
529                         {
530                                 dobase = false;
531                                 memset(&m, 0, sizeof(m));
532                                 m.tex[1] = R_GetTexture(texture->skin.base);
533                                 if (waterscrolling)
534                                         m.texmatrix[1] = r_surf_waterscrollmatrix;
535                                 m.texrgbscale[1] = 2;
536                                 m.pointer_color = varray_color4f;
537                                 R_Mesh_State(&m);
538                                 colorscale = 1;
539                                 r = ent->colormod[0] * colorscale;
540                                 g = ent->colormod[1] * colorscale;
541                                 b = ent->colormod[2] * colorscale;
542                                 a = texture->currentalpha;
543                                 base = r_ambient.value * (1.0f / 64.0f);
544                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
545                                 {
546                                         surface = texturesurfacelist[texturesurfaceindex];
547                                         vertex3f = RSurf_GetVertexPointer(ent, surface);
548                                         R_Mesh_VertexPointer(vertex3f);
549                                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordlightmap2f);
550                                         R_Mesh_TexCoordPointer(1, 2, surface->mesh.data_texcoordtexture2f);
551                                         if (surface->lightmaptexture)
552                                         {
553                                                 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
554                                                 if (fogallpasses)
555                                                 {
556                                                         R_Mesh_ColorPointer(varray_color4f);
557                                                         for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
558                                                         {
559                                                                 VectorSubtract(v, modelorg, diff);
560                                                                 f = 1 - exp(fogdensity/DotProduct(diff, diff));
561                                                                 c[0] = f * r;
562                                                                 c[1] = f * g;
563                                                                 c[2] = f * b;
564                                                                 c[3] = a;
565                                                         }
566                                                 }
567                                                 else
568                                                 {
569                                                         R_Mesh_ColorPointer(NULL);
570                                                         GL_Color(r, g, b, a);
571                                                 }
572                                         }
573                                         else
574                                         {
575                                                 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
576                                                 R_Mesh_ColorPointer(varray_color4f);
577                                                 if (surface->styles[0] != 255)
578                                                 {
579                                                         for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
580                                                         {
581                                                                 c[0] = 0;
582                                                                 c[1] = 0;
583                                                                 c[2] = 0;
584                                                                 if (surface->styles[0] != 255)
585                                                                 {
586                                                                         if (surface->mesh.data_lightmapcolor4f)
587                                                                         {
588                                                                                 float scale = d_lightstylevalue[surface->styles[0]] * (1.0f / 128.0f);
589                                                                                 VectorMA(c, scale, surface->mesh.data_lightmapcolor4f + i*4, c);
590                                                                         }
591                                                                         else if (surface->mesh.data_lightmapoffsets)
592                                                                         {
593                                                                                 const qbyte *lm = surface->samples + surface->mesh.data_lightmapoffsets[i];
594                                                                                 float scale = d_lightstylevalue[surface->styles[0]] * (1.0f / 32768.0f);
595                                                                                 VectorMA(c, scale, lm, c);
596                                                                                 if (surface->styles[1] != 255)
597                                                                                 {
598                                                                                         int size3 = ((surface->extents[0]>>4)+1)*((surface->extents[1]>>4)+1)*3;
599                                                                                         lm += size3;
600                                                                                         scale = d_lightstylevalue[surface->styles[1]] * (1.0f / 32768.0f);
601                                                                                         VectorMA(c, scale, lm, c);
602                                                                                         if (surface->styles[2] != 255)
603                                                                                         {
604                                                                                                 lm += size3;
605                                                                                                 scale = d_lightstylevalue[surface->styles[2]] * (1.0f / 32768.0f);
606                                                                                                 VectorMA(c, scale, lm, c);
607                                                                                                 if (surface->styles[3] != 255)
608                                                                                                 {
609                                                                                                         lm += size3;
610                                                                                                         scale = d_lightstylevalue[surface->styles[3]] * (1.0f / 32768.0f);
611                                                                                                         VectorMA(c, scale, lm, c);
612                                                                                                 }
613                                                                                         }
614                                                                                 }
615                                                                         }
616                                                                 }
617                                                                 c[0] *= r;
618                                                                 c[1] *= g;
619                                                                 c[2] *= b;
620                                                                 if (fogallpasses)
621                                                                 {
622                                                                         VectorSubtract(v, modelorg, diff);
623                                                                         f = 1 - exp(fogdensity/DotProduct(diff, diff));
624                                                                         VectorScale(c, f, c);
625                                                                 }
626                                                                 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
627                                                                         c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
628                                                                 else
629                                                                         c[3] = a;
630                                                         }
631                                                 }
632                                                 else
633                                                 {
634                                                         if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
635                                                         {
636                                                                 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
637                                                                 {
638                                                                         c[0] = 0;
639                                                                         c[1] = 0;
640                                                                         c[2] = 0;
641                                                                         c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
642                                                                 }
643                                                         }
644                                                         else
645                                                         {
646                                                                 R_Mesh_ColorPointer(NULL);
647                                                                 GL_Color(0, 0, 0, a);
648                                                         }
649                                                 }
650                                         }
651                                         GL_LockArrays(0, surface->mesh.num_vertices);
652                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
653                                         GL_LockArrays(0, 0);
654                                 }
655                         }
656                         if (dobase)
657                         {
658                                 dobase = false;
659                                 memset(&m, 0, sizeof(m));
660                                 m.tex[0] = R_GetTexture(texture->skin.base);
661                                 if (waterscrolling)
662                                         m.texmatrix[0] = r_surf_waterscrollmatrix;
663                                 m.texmatrix[0] = r_surf_waterscrollmatrix;
664                                 m.pointer_color = varray_color4f;
665                                 colorscale = 1;
666                                 if (gl_combine.integer)
667                                 {
668                                         m.texrgbscale[0] = 4;
669                                         colorscale *= 0.25f;
670                                 }
671                                 R_Mesh_State(&m);
672                                 r = ent->colormod[0] * colorscale;
673                                 g = ent->colormod[1] * colorscale;
674                                 b = ent->colormod[2] * colorscale;
675                                 a = texture->currentalpha;
676                                 if (dolightmap)
677                                 {
678                                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
679                                         {
680                                                 surface = texturesurfacelist[texturesurfaceindex];
681                                                 vertex3f = RSurf_GetVertexPointer(ent, surface);
682                                                 R_Mesh_VertexPointer(vertex3f);
683                                                 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
684                                                 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
685                                                 {
686                                                         c[0] = 0;
687                                                         c[1] = 0;
688                                                         c[2] = 0;
689                                                         if (surface->styles[0] != 255)
690                                                         {
691                                                                 if (surface->mesh.data_lightmapcolor4f)
692                                                                 {
693                                                                         float scale = d_lightstylevalue[surface->styles[0]] * (1.0f / 128.0f);
694                                                                         VectorMA(c, scale, surface->mesh.data_lightmapcolor4f + i*4, c);
695                                                                 }
696                                                                 else if (surface->mesh.data_lightmapoffsets)
697                                                                 {
698                                                                         const qbyte *lm = surface->samples + surface->mesh.data_lightmapoffsets[i];
699                                                                         float scale = d_lightstylevalue[surface->styles[0]] * (1.0f / 32768.0f);
700                                                                         VectorMA(c, scale, lm, c);
701                                                                         if (surface->styles[1] != 255)
702                                                                         {
703                                                                                 int size3 = ((surface->extents[0]>>4)+1)*((surface->extents[1]>>4)+1)*3;
704                                                                                 lm += size3;
705                                                                                 scale = d_lightstylevalue[surface->styles[1]] * (1.0f / 32768.0f);
706                                                                                 VectorMA(c, scale, lm, c);
707                                                                                 if (surface->styles[2] != 255)
708                                                                                 {
709                                                                                         lm += size3;
710                                                                                         scale = d_lightstylevalue[surface->styles[2]] * (1.0f / 32768.0f);
711                                                                                         VectorMA(c, scale, lm, c);
712                                                                                         if (surface->styles[3] != 255)
713                                                                                         {
714                                                                                                 lm += size3;
715                                                                                                 scale = d_lightstylevalue[surface->styles[3]] * (1.0f / 32768.0f);
716                                                                                                 VectorMA(c, scale, lm, c);
717                                                                                         }
718                                                                                 }
719                                                                         }
720                                                                 }
721                                                         }
722                                                         c[0] *= r;
723                                                         c[1] *= g;
724                                                         c[2] *= b;
725                                                         if (fogallpasses)
726                                                         {
727                                                                 VectorSubtract(v, modelorg, diff);
728                                                                 f = 1 - exp(fogdensity/DotProduct(diff, diff));
729                                                                 VectorScale(c, f, c);
730                                                         }
731                                                         if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
732                                                                 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
733                                                         else
734                                                                 c[3] = a;
735                                                 }
736                                                 GL_LockArrays(0, surface->mesh.num_vertices);
737                                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
738                                                 GL_LockArrays(0, 0);
739                                         }
740                                 }
741                                 else
742                                 {
743                                         if (fogallpasses)
744                                         {
745                                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
746                                                 {
747                                                         surface = texturesurfacelist[texturesurfaceindex];
748                                                         vertex3f = RSurf_GetVertexPointer(ent, surface);
749                                                         R_Mesh_VertexPointer(vertex3f);
750                                                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
751                                                         if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
752                                                         {
753                                                                 R_Mesh_ColorPointer(varray_color4f);
754                                                                 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
755                                                                 {
756                                                                         VectorSubtract(v, modelorg, diff);
757                                                                         f = 1 - exp(fogdensity/DotProduct(diff, diff));
758                                                                         c[0] = r * f;
759                                                                         c[1] = g * f;
760                                                                         c[2] = b * f;
761                                                                         c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
762                                                                 }
763                                                         }
764                                                         else
765                                                         {
766                                                                 R_Mesh_ColorPointer(varray_color4f);
767                                                                 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
768                                                                 {
769                                                                         VectorSubtract(v, modelorg, diff);
770                                                                         f = 1 - exp(fogdensity/DotProduct(diff, diff));
771                                                                         c[0] = r * f;
772                                                                         c[1] = g * f;
773                                                                         c[2] = b * f;
774                                                                         c[3] = a;
775                                                                 }
776                                                         }
777                                                         GL_LockArrays(0, surface->mesh.num_vertices);
778                                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
779                                                         GL_LockArrays(0, 0);
780                                                 }
781                                         }
782                                         else
783                                         {
784                                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
785                                                 {
786                                                         surface = texturesurfacelist[texturesurfaceindex];
787                                                         vertex3f = RSurf_GetVertexPointer(ent, surface);
788                                                         R_Mesh_VertexPointer(vertex3f);
789                                                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
790                                                         if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
791                                                         {
792                                                                 R_Mesh_ColorPointer(varray_color4f);
793                                                                 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
794                                                                 {
795                                                                         c[0] = r;
796                                                                         c[1] = g;
797                                                                         c[2] = b;
798                                                                         c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
799                                                                 }
800                                                         }
801                                                         else
802                                                         {
803                                                                 R_Mesh_ColorPointer(NULL);
804                                                                 GL_Color(r, g, b, a);
805                                                         }
806                                                         GL_LockArrays(0, surface->mesh.num_vertices);
807                                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
808                                                         GL_LockArrays(0, 0);
809                                                 }
810                                         }
811                                 }
812                         }
813                 }
814                 else
815                 {
816                         if (!dolightmap && dobase)
817                         {
818                                 dolightmap = false;
819                                 dobase = false;
820                                 GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], 1);
821                                 memset(&m, 0, sizeof(m));
822                                 m.tex[0] = R_GetTexture(texture->skin.base);
823                                 if (waterscrolling)
824                                         m.texmatrix[0] = r_surf_waterscrollmatrix;
825                                 R_Mesh_State(&m);
826                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
827                                 {
828                                         surface = texturesurfacelist[texturesurfaceindex];
829                                         R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, surface));
830                                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
831                                         GL_LockArrays(0, surface->mesh.num_vertices);
832                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
833                                         GL_LockArrays(0, 0);
834                                 }
835                         }
836                         if (r_lightmapintensity <= 0 && dolightmap && dobase)
837                         {
838                                 dolightmap = false;
839                                 dobase = false;
840                                 GL_Color(0, 0, 0, 1);
841                                 memset(&m, 0, sizeof(m));
842                                 R_Mesh_State(&m);
843                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
844                                 {
845                                         surface = texturesurfacelist[texturesurfaceindex];
846                                         R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, surface));
847                                         GL_LockArrays(0, surface->mesh.num_vertices);
848                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
849                                         GL_LockArrays(0, 0);
850                                 }
851                         }
852                         if (r_textureunits.integer >= 2 && gl_combine.integer && dolightmap && dobase)
853                         {
854                                 // dualtexture combine
855                                 GL_BlendFunc(GL_ONE, GL_ZERO);
856                                 GL_DepthMask(true);
857                                 dolightmap = false;
858                                 dobase = false;
859                                 memset(&m, 0, sizeof(m));
860                                 m.tex[1] = R_GetTexture(texture->skin.base);
861                                 if (waterscrolling)
862                                         m.texmatrix[1] = r_surf_waterscrollmatrix;
863                                 m.texrgbscale[1] = 2;
864                                 R_Mesh_State(&m);
865                                 r = ent->colormod[0] * r_lightmapintensity;
866                                 g = ent->colormod[1] * r_lightmapintensity;
867                                 b = ent->colormod[2] * r_lightmapintensity;
868                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
869                                 {
870                                         surface = texturesurfacelist[texturesurfaceindex];
871                                         R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, surface));
872                                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordlightmap2f);
873                                         R_Mesh_TexCoordPointer(1, 2, surface->mesh.data_texcoordtexture2f);
874                                         if (surface->lightmaptexture)
875                                         {
876                                                 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
877                                                 R_Mesh_ColorPointer(NULL);
878                                                 GL_Color(r, g, b, 1);
879                                         }
880                                         else if (r == 1 && g == 1 && b == 1)
881                                         {
882                                                 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
883                                                 R_Mesh_ColorPointer(surface->mesh.data_lightmapcolor4f);
884                                         }
885                                         else
886                                         {
887                                                 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
888                                                 R_Mesh_ColorPointer(varray_color4f);
889                                                 for (i = 0;i < surface->mesh.num_vertices;i++)
890                                                 {
891                                                         varray_color4f[i*4+0] = surface->mesh.data_lightmapcolor4f[i*4+0] * r;
892                                                         varray_color4f[i*4+1] = surface->mesh.data_lightmapcolor4f[i*4+1] * g;
893                                                         varray_color4f[i*4+2] = surface->mesh.data_lightmapcolor4f[i*4+2] * b;
894                                                         varray_color4f[i*4+3] = surface->mesh.data_lightmapcolor4f[i*4+3];
895                                                 }
896                                         }
897                                         GL_LockArrays(0, surface->mesh.num_vertices);
898                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
899                                         GL_LockArrays(0, 0);
900                                 }
901                         }
902                         // single texture
903                         if (dolightmap)
904                         {
905                                 GL_BlendFunc(GL_ONE, GL_ZERO);
906                                 GL_DepthMask(true);
907                                 GL_Color(1, 1, 1, 1);
908                                 memset(&m, 0, sizeof(m));
909                                 R_Mesh_State(&m);
910                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
911                                 {
912                                         surface = texturesurfacelist[texturesurfaceindex];
913                                         R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, surface));
914                                         R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
915                                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordlightmap2f);
916                                         R_Mesh_ColorPointer(surface->lightmaptexture ? NULL : surface->mesh.data_lightmapcolor4f);
917                                         GL_LockArrays(0, surface->mesh.num_vertices);
918                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
919                                         GL_LockArrays(0, 0);
920                                 }
921                         }
922                         if (dobase)
923                         {
924                                 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
925                                 GL_DepthMask(false);
926                                 GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
927                                 memset(&m, 0, sizeof(m));
928                                 m.tex[0] = R_GetTexture(texture->skin.base);
929                                 if (waterscrolling)
930                                         m.texmatrix[0] = r_surf_waterscrollmatrix;
931                                 R_Mesh_State(&m);
932                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
933                                 {
934                                         surface = texturesurfacelist[texturesurfaceindex];
935                                         R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, surface));
936                                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
937                                         GL_LockArrays(0, surface->mesh.num_vertices);
938                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
939                                         GL_LockArrays(0, 0);
940                                 }
941                         }
942                 }
943                 if (doambient)
944                 {
945                         doambient = false;
946                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
947                         GL_DepthMask(false);
948                         memset(&m, 0, sizeof(m));
949                         m.tex[0] = R_GetTexture(texture->skin.base);
950                         if (waterscrolling)
951                                 m.texmatrix[0] = r_surf_waterscrollmatrix;
952                         m.pointer_color = varray_color4f;
953                         colorscale = 1;
954                         if (gl_combine.integer)
955                         {
956                                 m.texrgbscale[0] = 4;
957                                 colorscale *= 0.25f;
958                         }
959                         R_Mesh_State(&m);
960                         base = r_ambient.value * (1.0f / 64.0f);
961                         r = ent->colormod[0] * colorscale * base;
962                         g = ent->colormod[1] * colorscale * base;
963                         b = ent->colormod[2] * colorscale * base;
964                         a = texture->currentalpha;
965                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
966                         {
967                                 surface = texturesurfacelist[texturesurfaceindex];
968                                 vertex3f = RSurf_GetVertexPointer(ent, surface);
969                                 R_Mesh_VertexPointer(vertex3f);
970                                 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
971                                 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
972                                 {
973                                         c[0] = r;
974                                         c[1] = g;
975                                         c[2] = b;
976                                         if (fogallpasses)
977                                         {
978                                                 VectorSubtract(v, modelorg, diff);
979                                                 f = 1 - exp(fogdensity/DotProduct(diff, diff));
980                                                 VectorScale(c, f, c);
981                                         }
982                                         if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
983                                                 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
984                                         else
985                                                 c[3] = a;
986                                 }
987                                 GL_LockArrays(0, surface->mesh.num_vertices);
988                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
989                                 GL_LockArrays(0, 0);
990                         }
991                 }
992                 if (dodetail)
993                 {
994                         GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
995                         GL_DepthMask(false);
996                         GL_Color(1, 1, 1, 1);
997                         memset(&m, 0, sizeof(m));
998                         m.tex[0] = R_GetTexture(texture->skin.detail);
999                         R_Mesh_State(&m);
1000                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1001                         {
1002                                 surface = texturesurfacelist[texturesurfaceindex];
1003                                 R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, surface));
1004                                 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoorddetail2f);
1005                                 GL_LockArrays(0, surface->mesh.num_vertices);
1006                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1007                                 GL_LockArrays(0, 0);
1008                         }
1009                 }
1010                 if (doglow)
1011                 {
1012                         // if glow was not already done using multitexture, do it now.
1013                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1014                         GL_DepthMask(false);
1015                         memset(&m, 0, sizeof(m));
1016                         m.tex[0] = R_GetTexture(texture->skin.glow);
1017                         if (waterscrolling)
1018                                 m.texmatrix[0] = r_surf_waterscrollmatrix;
1019                         m.pointer_color = varray_color4f;
1020                         R_Mesh_State(&m);
1021                         colorscale = 1;
1022                         r = ent->colormod[0] * colorscale;
1023                         g = ent->colormod[1] * colorscale;
1024                         b = ent->colormod[2] * colorscale;
1025                         a = texture->currentalpha;
1026                         if (fogallpasses)
1027                         {
1028                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1029                                 {
1030                                         surface = texturesurfacelist[texturesurfaceindex];
1031                                         vertex3f = RSurf_GetVertexPointer(ent, surface);
1032                                         R_Mesh_VertexPointer(vertex3f);
1033                                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
1034                                         R_Mesh_ColorPointer(varray_color4f);
1035                                         if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1036                                         {
1037                                                 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1038                                                 {
1039                                                         VectorSubtract(v, modelorg, diff);
1040                                                         f = 1 - exp(fogdensity/DotProduct(diff, diff));
1041                                                         c[0] = f * r;
1042                                                         c[1] = f * g;
1043                                                         c[2] = f * b;
1044                                                         c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
1045                                                 }
1046                                         }
1047                                         else
1048                                         {
1049                                                 for (i = 0, v = vertex3f, 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] = a;
1057                                                 }
1058                                         }
1059                                         GL_LockArrays(0, surface->mesh.num_vertices);
1060                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1061                                         GL_LockArrays(0, 0);
1062                                 }
1063                         }
1064                         else
1065                         {
1066                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1067                                 {
1068                                         surface = texturesurfacelist[texturesurfaceindex];
1069                                         vertex3f = RSurf_GetVertexPointer(ent, surface);
1070                                         R_Mesh_VertexPointer(vertex3f);
1071                                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
1072                                         if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1073                                         {
1074                                                 R_Mesh_ColorPointer(varray_color4f);
1075                                                 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1076                                                 {
1077                                                         c[0] = r;
1078                                                         c[1] = g;
1079                                                         c[2] = b;
1080                                                         c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
1081                                                 }
1082                                         }
1083                                         else
1084                                         {
1085                                                 R_Mesh_ColorPointer(NULL);
1086                                                 GL_Color(r, g, b, a);
1087                                         }
1088                                         GL_LockArrays(0, surface->mesh.num_vertices);
1089                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1090                                         GL_LockArrays(0, 0);
1091                                 }
1092                         }
1093                 }
1094                 if (dofogpass)
1095                 {
1096                         // if this is opaque use alpha blend which will darken the earlier
1097                         // passes cheaply.
1098                         //
1099                         // if this is an alpha blended material, all the earlier passes
1100                         // were darkened by fog already, so we only need to add the fog
1101                         // color ontop through the fog mask texture
1102                         //
1103                         // if this is an additive blended material, all the earlier passes
1104                         // were darkened by fog already, and we should not add fog color
1105                         // (because the background was not darkened, there is no fog color
1106                         // that was lost behind it).
1107                         if (!fogallpasses)
1108                                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1109                         else
1110                                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1111                         GL_DepthMask(false);
1112                         memset(&m, 0, sizeof(m));
1113                         m.tex[0] = R_GetTexture(texture->skin.fog);
1114                         if (waterscrolling)
1115                                 m.texmatrix[0] = r_surf_waterscrollmatrix;
1116                         R_Mesh_State(&m);
1117                         r = fogcolor[0];
1118                         g = fogcolor[1];
1119                         b = fogcolor[2];
1120                         a = texture->currentalpha;
1121                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1122                         {
1123                                 surface = texturesurfacelist[texturesurfaceindex];
1124                                 vertex3f = RSurf_GetVertexPointer(ent, surface);
1125                                 R_Mesh_VertexPointer(vertex3f);
1126                                 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
1127                                 R_Mesh_ColorPointer(varray_color4f);
1128                                 //RSurf_FogPassColors_Vertex3f_Color4f(surface->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], texture->currentalpha, 1, surface->mesh.num_vertices, modelorg);
1129                                 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1130                                 {
1131                                         for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1132                                         {
1133                                                 VectorSubtract(v, modelorg, diff);
1134                                                 f = exp(fogdensity/DotProduct(diff, diff));
1135                                                 c[0] = r;
1136                                                 c[1] = g;
1137                                                 c[2] = b;
1138                                                 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * f * a;
1139                                         }
1140                                 }
1141                                 else
1142                                 {
1143                                         for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1144                                         {
1145                                                 VectorSubtract(v, modelorg, diff);
1146                                                 f = exp(fogdensity/DotProduct(diff, diff));
1147                                                 c[0] = r;
1148                                                 c[1] = g;
1149                                                 c[2] = b;
1150                                                 c[3] = f * a;
1151                                         }
1152                                 }
1153                                 GL_LockArrays(0, surface->mesh.num_vertices);
1154                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1155                                 GL_LockArrays(0, 0);
1156                         }
1157                 }
1158         }
1159         if (texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1160                 qglEnable(GL_CULL_FACE);
1161 }
1162
1163 static void RSurfShader_Transparent_Callback(const void *calldata1, int calldata2)
1164 {
1165         const entity_render_t *ent = calldata1;
1166         const msurface_t *surface = ent->model->brush.data_surfaces + calldata2;
1167         vec3_t modelorg;
1168         texture_t *texture;
1169
1170         texture = surface->texture;
1171         if (texture->basematerialflags & MATERIALFLAG_SKY)
1172                 return; // transparent sky is too difficult
1173         R_UpdateTextureInfo(ent, texture);
1174
1175         R_Mesh_Matrix(&ent->matrix);
1176         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1177         R_DrawSurfaceList(ent, texture, 1, &surface, modelorg);
1178 }
1179
1180 void R_QueueSurfaceList(entity_render_t *ent, texture_t *texture, int texturenumsurfaces, const msurface_t **texturesurfacelist, const vec3_t modelorg)
1181 {
1182         int texturesurfaceindex;
1183         const msurface_t *surface;
1184         vec3_t tempcenter, center;
1185         if (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT)
1186         {
1187                 // drawing sky transparently would be too difficult
1188                 if (!(texture->currentmaterialflags & MATERIALFLAG_SKY))
1189                 {
1190                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1191                         {
1192                                 surface = texturesurfacelist[texturesurfaceindex];
1193                                 tempcenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
1194                                 tempcenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
1195                                 tempcenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
1196                                 Matrix4x4_Transform(&ent->matrix, tempcenter, center);
1197                                 R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_vieworigin : center, RSurfShader_Transparent_Callback, ent, surface - ent->model->brush.data_surfaces);
1198                         }
1199                 }
1200         }
1201         else
1202                 R_DrawSurfaceList(ent, texture, texturenumsurfaces, texturesurfacelist, modelorg);
1203 }
1204
1205 void R_DrawSurfaces(entity_render_t *ent, qboolean skysurfaces)
1206 {
1207         int i, j, f, flagsmask;
1208         msurface_t *surface, **surfacechain;
1209         texture_t *t, *texture;
1210         model_t *model = ent->model;
1211         vec3_t modelorg;
1212         const int maxsurfacelist = 1024;
1213         int numsurfacelist = 0;
1214         const msurface_t *surfacelist[1024];
1215         if (model == NULL)
1216                 return;
1217         R_Mesh_Matrix(&ent->matrix);
1218         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1219
1220         // update light styles
1221         if (!skysurfaces)
1222         {
1223                 for (i = 0;i < model->brushq1.light_styles;i++)
1224                 {
1225                         if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1226                         {
1227                                 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1228                                 if ((surfacechain = model->brushq1.light_styleupdatechains[i]))
1229                                         for (;(surface = *surfacechain);surfacechain++)
1230                                                 surface->cached_dlight = true;
1231                         }
1232                 }
1233         }
1234
1235         R_UpdateAllTextureInfo(ent);
1236         flagsmask = skysurfaces ? MATERIALFLAG_SKY : (MATERIALFLAG_WATER | MATERIALFLAG_WALL);
1237         f = 0;
1238         t = NULL;
1239         texture = NULL;
1240         numsurfacelist = 0;
1241         for (i = 0, j = model->firstmodelsurface;i < model->nummodelsurfaces;i++, j++)
1242         {
1243                 if (ent != r_refdef.worldentity || r_worldsurfacevisible[j])
1244                 {
1245                         surface = model->brush.data_surfaces + j;
1246                         if (t != surface->texture)
1247                         {
1248                                 if (numsurfacelist)
1249                                 {
1250                                         R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1251                                         numsurfacelist = 0;
1252                                 }
1253                                 t = surface->texture;
1254                                 f = t->currentmaterialflags & flagsmask;
1255                                 texture = t->currentframe;
1256                         }
1257                         if (f && surface->mesh.num_triangles)
1258                         {
1259                                 // if lightmap parameters changed, rebuild lightmap texture
1260                                 if (surface->cached_dlight && surface->samples)
1261                                         R_BuildLightMap(ent, surface);
1262                                 // add face to draw list
1263                                 surfacelist[numsurfacelist++] = surface;
1264                                 if (numsurfacelist >= maxsurfacelist)
1265                                 {
1266                                         R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1267                                         numsurfacelist = 0;
1268                                 }
1269                         }
1270                 }
1271         }
1272         if (numsurfacelist)
1273                 R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1274 }
1275
1276 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1277 {
1278         int i;
1279         float *v;
1280         rmeshstate_t m;
1281         const mportal_t *portal = calldata1;
1282         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1283         GL_DepthMask(false);
1284         GL_DepthTest(true);
1285         R_Mesh_Matrix(&r_identitymatrix);
1286
1287         memset(&m, 0, sizeof(m));
1288         m.pointer_vertex = varray_vertex3f;
1289         R_Mesh_State(&m);
1290
1291         i = calldata2;
1292         GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1293                          ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1294                          ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1295                          0.125f);
1296         if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1297         {
1298                 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1299                         VectorCopy(portal->points[i].position, v);
1300         }
1301         else
1302                 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1303                         VectorCopy(portal->points[i].position, v);
1304         GL_LockArrays(0, portal->numpoints);
1305         R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1306         GL_LockArrays(0, 0);
1307 }
1308
1309 // LordHavoc: this is just a nice debugging tool, very slow
1310 static void R_DrawPortals(void)
1311 {
1312         int i, portalnum;
1313         mportal_t *portal;
1314         float center[3], f;
1315         model_t *model = r_refdef.worldmodel;
1316         if (model == NULL)
1317                 return;
1318         for (portalnum = 0, portal = model->brush.data_portals;portalnum < model->brush.num_portals;portalnum++, portal++)
1319         {
1320                 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1321                 if (!R_CullBox(portal->mins, portal->maxs))
1322                 {
1323                         VectorClear(center);
1324                         for (i = 0;i < portal->numpoints;i++)
1325                                 VectorAdd(center, portal->points[i].position, center);
1326                         f = ixtable[portal->numpoints];
1327                         VectorScale(center, f, center);
1328                         R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, portal, portalnum);
1329                 }
1330         }
1331 }
1332
1333 static void R_DrawCollisionBrush(colbrushf_t *brush)
1334 {
1335         int i;
1336         rmeshstate_t m;
1337         memset(&m, 0, sizeof(m));
1338         m.pointer_vertex = brush->points->v;
1339         R_Mesh_State(&m);
1340         i = (int)(((size_t)brush) / sizeof(colbrushf_t));
1341         GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1342         GL_LockArrays(0, brush->numpoints);
1343         R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1344         GL_LockArrays(0, 0);
1345 }
1346
1347 static void R_DrawCollisionSurface(entity_render_t *ent, msurface_t *surface)
1348 {
1349         int i;
1350         rmeshstate_t m;
1351         if (!surface->mesh.num_collisiontriangles)
1352                 return;
1353         memset(&m, 0, sizeof(m));
1354         m.pointer_vertex = surface->mesh.data_collisionvertex3f;
1355         R_Mesh_State(&m);
1356         i = (int)(((size_t)surface) / sizeof(msurface_t));
1357         GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1358         GL_LockArrays(0, surface->mesh.num_collisionvertices);
1359         R_Mesh_Draw(surface->mesh.num_collisionvertices, surface->mesh.num_collisiontriangles, surface->mesh.data_collisionelement3i);
1360         GL_LockArrays(0, 0);
1361 }
1362
1363 void R_WorldVisibility(void)
1364 {
1365         int i, j, *mark;
1366         mleaf_t *leaf;
1367         mleaf_t *viewleaf;
1368         model_t *model = r_refdef.worldmodel;
1369
1370         if (!model)
1371                 return;
1372
1373         // if possible find the leaf the view origin is in
1374         viewleaf = model->brushq1.PointInLeaf ? model->brushq1.PointInLeaf(model, r_vieworigin) : NULL;
1375         // if possible fetch the visible cluster bits
1376         if (model->brush.FatPVS)
1377                 model->brush.FatPVS(model, r_vieworigin, 2, r_pvsbits, sizeof(r_pvsbits));
1378
1379         // clear the visible surface and leaf flags arrays
1380         memset(r_worldsurfacevisible, 0, model->brush.num_surfaces);
1381         memset(r_worldleafvisible, 0, model->brush.num_leafs);
1382
1383         // if the user prefers surfaceworldnode (testing?) or the viewleaf could
1384         // not be found, or the viewleaf is not part of the visible world
1385         // (floating around in the void), use the pvs method
1386         if (r_surfaceworldnode.integer || !viewleaf || viewleaf->clusterindex < 0)
1387         {
1388                 // pvs method:
1389                 // similar to quake's RecursiveWorldNode but without cache misses
1390                 for (j = 0, leaf = model->brush.data_leafs;j < model->brush.num_leafs;j++, leaf++)
1391                 {
1392                         // if leaf is in current pvs and on the screen, mark its surfaces
1393                         if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
1394                         {
1395                                 c_leafs++;
1396                                 r_worldleafvisible[j] = true;
1397                                 if (leaf->numleafsurfaces)
1398                                         for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
1399                                                 r_worldsurfacevisible[*mark] = true;
1400                         }
1401                 }
1402         }
1403         else
1404         {
1405                 int leafstackpos;
1406                 mportal_t *p;
1407                 mleaf_t *leafstack[8192];
1408                 // portal method:
1409                 // follows portals leading outward from viewleaf, does not venture
1410                 // offscreen or into leafs that are not visible, faster than Quake's
1411                 // RecursiveWorldNode and vastly better in unvised maps, often culls a
1412                 // lot of surface that pvs alone would miss
1413                 leafstack[0] = viewleaf;
1414                 leafstackpos = 1;
1415                 while (leafstackpos)
1416                 {
1417                         c_leafs++;
1418                         leaf = leafstack[--leafstackpos];
1419                         r_worldleafvisible[leaf - model->brush.data_leafs] = true;
1420                         // mark any surfaces bounding this leaf
1421                         if (leaf->numleafsurfaces)
1422                                 for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
1423                                         r_worldsurfacevisible[*mark] = true;
1424                         // follow portals into other leafs
1425                         // the checks are:
1426                         // if viewer is behind portal (portal faces outward into the scene)
1427                         // and the portal polygon's bounding box is on the screen
1428                         // and the leaf has not been visited yet
1429                         // and the leaf is visible in the pvs
1430                         // (the first two checks won't cause as many cache misses as the leaf checks)
1431                         for (p = leaf->portals;p;p = p->next)
1432                                 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))
1433                                         leafstack[leafstackpos++] = p->past;
1434                 }
1435         }
1436
1437         if (r_drawportals.integer)
1438                 R_DrawPortals();
1439 }
1440
1441 void R_Q1BSP_DrawSky(entity_render_t *ent)
1442 {
1443         if (ent->model == NULL)
1444                 return;
1445         if (r_drawcollisionbrushes.integer < 2)
1446                 R_DrawSurfaces(ent, true);
1447 }
1448
1449 void R_Q1BSP_Draw(entity_render_t *ent)
1450 {
1451         if (ent->model == NULL)
1452                 return;
1453         c_bmodels++;
1454         if (r_drawcollisionbrushes.integer < 2)
1455                 R_DrawSurfaces(ent, false);
1456         if (r_drawcollisionbrushes.integer >= 1 && ent->model->brush.num_brushes)
1457         {
1458                 int i;
1459                 model_t *model = ent->model;
1460                 msurface_t *surface;
1461                 q3mbrush_t *brush;
1462                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1463                 GL_DepthMask(false);
1464                 GL_DepthTest(true);
1465                 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
1466                 for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
1467                         if (brush->colbrushf && brush->colbrushf->numtriangles)
1468                                 R_DrawCollisionBrush(brush->colbrushf);
1469                 for (i = 0, surface = model->brush.data_surfaces + model->firstmodelsurface;i < model->nummodelsurfaces;i++, surface++)
1470                         if (surface->mesh.num_collisiontriangles)
1471                                 R_DrawCollisionSurface(ent, surface);
1472                 qglPolygonOffset(0, 0);
1473         }
1474 }
1475
1476 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)
1477 {
1478         model_t *model = ent->model;
1479         vec3_t lightmins, lightmaxs;
1480         int t, leafindex, leafsurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
1481         const int *e;
1482         const float *v[3];
1483         msurface_t *surface;
1484         mleaf_t *leaf;
1485         const qbyte *pvs;
1486         lightmins[0] = relativelightorigin[0] - lightradius;
1487         lightmins[1] = relativelightorigin[1] - lightradius;
1488         lightmins[2] = relativelightorigin[2] - lightradius;
1489         lightmaxs[0] = relativelightorigin[0] + lightradius;
1490         lightmaxs[1] = relativelightorigin[1] + lightradius;
1491         lightmaxs[2] = relativelightorigin[2] + lightradius;
1492         *outnumclusterspointer = 0;
1493         *outnumsurfacespointer = 0;
1494         memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
1495         memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
1496         if (model == NULL)
1497         {
1498                 VectorCopy(lightmins, outmins);
1499                 VectorCopy(lightmaxs, outmaxs);
1500                 return;
1501         }
1502         VectorCopy(relativelightorigin, outmins);
1503         VectorCopy(relativelightorigin, outmaxs);
1504         if (model->brush.GetPVS)
1505                 pvs = model->brush.GetPVS(model, relativelightorigin);
1506         else
1507                 pvs = NULL;
1508         R_UpdateAllTextureInfo(ent);
1509         // FIXME: use BSP recursion as lights are often small
1510         for (leafindex = 0, leaf = model->brush.data_leafs;leafindex < model->brush.num_leafs;leafindex++, leaf++)
1511         {
1512                 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
1513                 {
1514                         outmins[0] = min(outmins[0], leaf->mins[0]);
1515                         outmins[1] = min(outmins[1], leaf->mins[1]);
1516                         outmins[2] = min(outmins[2], leaf->mins[2]);
1517                         outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
1518                         outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
1519                         outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
1520                         if (outclusterpvs)
1521                         {
1522                                 if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
1523                                 {
1524                                         SETPVSBIT(outclusterpvs, leaf->clusterindex);
1525                                         outclusterlist[outnumclusters++] = leaf->clusterindex;
1526                                 }
1527                         }
1528                         if (outsurfacepvs)
1529                         {
1530                                 for (leafsurfaceindex = 0;leafsurfaceindex < leaf->numleafsurfaces;leafsurfaceindex++)
1531                                 {
1532                                         surfaceindex = leaf->firstleafsurface[leafsurfaceindex];
1533                                         if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
1534                                         {
1535                                                 surface = model->brush.data_surfaces + surfaceindex;
1536                                                 if (BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs))
1537                                                 if ((surface->texture->currentmaterialflags & (MATERIALFLAG_WALL | MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
1538                                                 {
1539                                                         for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
1540                                                         {
1541                                                                 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1542                                                                 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1543                                                                 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1544                                                                 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])))
1545                                                                 {
1546                                                                         SETPVSBIT(outsurfacepvs, surfaceindex);
1547                                                                         outsurfacelist[outnumsurfaces++] = surfaceindex;
1548                                                                         break;
1549                                                                 }
1550                                                         }
1551                                                 }
1552                                         }
1553                                 }
1554                         }
1555                 }
1556         }
1557
1558         // limit combined leaf box to light boundaries
1559         outmins[0] = max(outmins[0], lightmins[0]);
1560         outmins[1] = max(outmins[1], lightmins[1]);
1561         outmins[2] = max(outmins[2], lightmins[2]);
1562         outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
1563         outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
1564         outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
1565
1566         *outnumclusterspointer = outnumclusters;
1567         *outnumsurfacespointer = outnumsurfaces;
1568 }
1569
1570 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)
1571 {
1572         model_t *model = ent->model;
1573         msurface_t *surface;
1574         int surfacelistindex;
1575         if (r_drawcollisionbrushes.integer < 2)
1576         {
1577                 R_Mesh_Matrix(&ent->matrix);
1578                 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
1579                 if (!r_shadow_compilingrtlight)
1580                         R_UpdateAllTextureInfo(ent);
1581                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1582                 {
1583                         surface = model->brush.data_surfaces + surfacelist[surfacelistindex];
1584                         if ((surface->texture->currentmaterialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT | MATERIALFLAG_WALL)) != MATERIALFLAG_WALL)
1585                                 continue;
1586                         if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1587                                 continue;
1588                         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);
1589                 }
1590                 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);
1591         }
1592 }
1593
1594 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)
1595 {
1596         model_t *model = ent->model;
1597         msurface_t *surface;
1598         texture_t *t;
1599         int surfacelistindex;
1600         if (r_drawcollisionbrushes.integer < 2)
1601         {
1602                 R_Mesh_Matrix(&ent->matrix);
1603                 if (!r_shadow_compilingrtlight)
1604                         R_UpdateAllTextureInfo(ent);
1605                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1606                 {
1607                         surface = model->brush.data_surfaces + surfacelist[surfacelistindex];
1608                         if (surface->texture->basematerialflags & MATERIALFLAG_NODRAW || !surface->mesh.num_triangles)
1609                                 continue;
1610                         if (r_shadow_compilingrtlight)
1611                         {
1612                                 // if compiling an rtlight, capture the mesh
1613                                 t = surface->texture;
1614                                 if ((t->basematerialflags & (MATERIALFLAG_WALL | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
1615                                         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);
1616                         }
1617                         else if (ent != r_refdef.worldentity || r_worldsurfacevisible[surfacelist[surfacelistindex]])
1618                         {
1619                                 t = surface->texture->currentframe;
1620                                 // FIXME: transparent surfaces need to be lit later
1621                                 if ((t->currentmaterialflags & (MATERIALFLAG_WALL | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
1622                                 {
1623                                         if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1624                                                 qglDisable(GL_CULL_FACE);
1625                                         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);
1626                                         if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1627                                                 qglEnable(GL_CULL_FACE);
1628                                 }
1629                         }
1630                 }
1631         }
1632 }
1633
1634 #if 0
1635 static void gl_surf_start(void)
1636 {
1637 }
1638
1639 static void gl_surf_shutdown(void)
1640 {
1641 }
1642
1643 static void gl_surf_newmap(void)
1644 {
1645 }
1646 #endif
1647
1648 void GL_Surf_Init(void)
1649 {
1650
1651         Cvar_RegisterVariable(&r_ambient);
1652         Cvar_RegisterVariable(&r_drawportals);
1653         Cvar_RegisterVariable(&r_testvis);
1654         Cvar_RegisterVariable(&r_detailtextures);
1655         Cvar_RegisterVariable(&r_surfaceworldnode);
1656         Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
1657         Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
1658         Cvar_RegisterVariable(&r_q3bsp_renderskydepth);
1659         Cvar_RegisterVariable(&gl_lightmaps);
1660
1661         //R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
1662 }
1663