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