slight optimizations to bsp renderer
[xonotic/darkplaces.git] / gl_rsurf.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // r_surf.c: surface-related refresh code
21
22 #include "quakedef.h"
23 #include "r_shadow.h"
24
25 #define MAX_LIGHTMAP_SIZE 256
26
27 cvar_t r_ambient = {0, "r_ambient", "0"};
28 cvar_t r_drawportals = {0, "r_drawportals", "0"};
29 cvar_t r_testvis = {0, "r_testvis", "0"};
30 cvar_t r_detailtextures = {CVAR_SAVE, "r_detailtextures", "1"};
31 cvar_t r_surfaceworldnode = {0, "r_surfaceworldnode", "0"};
32 cvar_t r_drawcollisionbrushes_polygonfactor = {0, "r_drawcollisionbrushes_polygonfactor", "-1"};
33 cvar_t r_drawcollisionbrushes_polygonoffset = {0, "r_drawcollisionbrushes_polygonoffset", "0"};
34 cvar_t r_q3bsp_renderskydepth = {0, "r_q3bsp_renderskydepth", "0"};
35 cvar_t gl_lightmaps = {0, "gl_lightmaps", "0"};
36
37 // flag arrays used for visibility checking on world model
38 // (all other entities have no per-surface/per-leaf visibility checks)
39 // TODO: dynamic resize according to r_refdef.worldmodel->brush.num_clusters
40 qbyte r_pvsbits[(32768+7)>>3];
41 // TODO: dynamic resize according to r_refdef.worldmodel->brush.num_leafs
42 qbyte r_worldleafvisible[32768];
43 // TODO: dynamic resize according to r_refdef.worldmodel->brush.num_surfaces
44 qbyte r_worldsurfacevisible[262144];
45
46 /*
47 ===============
48 R_BuildLightMap
49
50 Combine and scale multiple lightmaps into the 8.8 format in blocklights
51 ===============
52 */
53 static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surface)
54 {
55         int smax, tmax, i, j, size, size3, maps, stride, l;
56         unsigned int *bl, scale;
57         qbyte *lightmap, *out, *stain;
58         static unsigned int intblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
59         static qbyte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
60
61         // update cached lighting info
62         surface->cached_dlight = 0;
63
64         smax = (surface->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 void RSurf_DeformVertices(const entity_render_t *ent, const texture_t *texture, const msurface_t *surface, const vec3_t modelorg)
299 {
300         int i, j;
301         float center[3], forward[3], right[3], up[3], v[4][3];
302         matrix4x4_t matrix1, imatrix1;
303         if (texture->textureflags & Q3TEXTUREFLAG_AUTOSPRITE2)
304         {
305                 // a single autosprite surface can contain multiple sprites...
306                 VectorClear(forward);
307                 VectorClear(right);
308                 VectorSet(up, 0, 0, 1);
309                 for (j = 0;j < surface->mesh.num_vertices - 3;j += 4)
310                 {
311                         VectorClear(center);
312                         for (i = 0;i < 4;i++)
313                                 VectorAdd(center, surface->mesh.data_vertex3f + (j+i) * 3, center);
314                         VectorScale(center, 0.25f, center);
315                         // FIXME: calculate vectors from triangle edges instead of using texture vectors as an easy way out?
316                         Matrix4x4_FromVectors(&matrix1, surface->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                         forward[0] = modelorg[0] - center[0];
321                         forward[1] = modelorg[1] - center[1];
322                         VectorNormalize(forward);
323                         right[0] = forward[1];
324                         right[1] = -forward[0];
325                         for (i = 0;i < 4;i++)
326                                 VectorMAMAMAM(1, center, v[i][0], forward, v[i][1], right, v[i][2], up, varray_vertex3f + (i+j) * 3);
327                 }
328         }
329         else if (texture->textureflags & Q3TEXTUREFLAG_AUTOSPRITE)
330         {
331                 Matrix4x4_Transform(&ent->inversematrix, r_viewforward, forward);
332                 Matrix4x4_Transform(&ent->inversematrix, r_viewright, right);
333                 Matrix4x4_Transform(&ent->inversematrix, r_viewup, up);
334                 // a single autosprite surface can contain multiple sprites...
335                 for (j = 0;j < surface->mesh.num_vertices - 3;j += 4)
336                 {
337                         VectorClear(center);
338                         for (i = 0;i < 4;i++)
339                                 VectorAdd(center, surface->mesh.data_vertex3f + (j+i) * 3, center);
340                         VectorScale(center, 0.25f, center);
341                         // FIXME: calculate vectors from triangle edges instead of using texture vectors as an easy way out?
342                         Matrix4x4_FromVectors(&matrix1, surface->mesh.data_normal3f + j*3, surface->mesh.data_svector3f + j*3, surface->mesh.data_tvector3f + j*3, center);
343                         Matrix4x4_Invert_Simple(&imatrix1, &matrix1);
344                         for (i = 0;i < 4;i++)
345                                 Matrix4x4_Transform(&imatrix1, surface->mesh.data_vertex3f + (j+i)*3, v[i]);
346                         for (i = 0;i < 4;i++)
347                                 VectorMAMAMAM(1, center, v[i][0], forward, v[i][1], right, v[i][2], up, varray_vertex3f + (i+j) * 3);
348                 }
349         }
350         else
351                 memcpy(varray_vertex3f, surface->mesh.data_vertex3f, sizeof(float[3]) * surface->mesh.num_vertices);
352 }
353
354 // any sort of deformvertices call is *VERY* rare, so this must be optimized
355 // to skip deformvertices quickly!
356 #if 1
357 #define RSurf_GetVertexPointer(ent, texture, surface, modelorg) ((texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2)) ? (RSurf_DeformVertices(ent, texture, surface, modelorg), varray_vertex3f) : surface->mesh.data_vertex3f)
358 #else
359 static float *RSurf_GetVertexPointer(const entity_render_t *ent, const texture_t *texture, const msurface_t *surface, const vec3_t modelorg)
360 {
361         if (texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
362         {
363                 RSurf_DeformVertices(ent, texture, surface, modelorg);
364                 return varray_vertex3f;
365         }
366         else
367                 return surface->mesh.data_vertex3f;
368 }
369 #endif
370
371 void R_UpdateTextureInfo(const entity_render_t *ent, texture_t *t)
372 {
373         // we don't need to set currentframe if t->animated is false because
374         // it was already set up by the texture loader for non-animating
375         if (t->animated)
376         {
377                 t->currentframe = t->anim_frames[ent->frame != 0][(t->anim_total[ent->frame != 0] >= 2) ? ((int)(r_refdef.time * 5.0f) % t->anim_total[ent->frame != 0]) : 0];
378                 t = t->currentframe;
379         }
380         t->currentmaterialflags = t->basematerialflags;
381         t->currentalpha = ent->alpha;
382         if (t->basematerialflags & MATERIALFLAG_WATERALPHA)
383                 t->currentalpha *= r_wateralpha.value;
384         if (!(ent->flags & RENDER_LIGHT))
385                 t->currentmaterialflags |= MATERIALFLAG_FULLBRIGHT;
386         if (ent->effects & EF_ADDITIVE)
387                 t->currentmaterialflags |= MATERIALFLAG_ADD | MATERIALFLAG_TRANSPARENT;
388         else if (t->currentalpha < 1)
389                 t->currentmaterialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_TRANSPARENT;
390 }
391
392 matrix4x4_t r_surf_waterscrollmatrix;
393
394 void R_UpdateAllTextureInfo(entity_render_t *ent)
395 {
396         int i;
397         Matrix4x4_CreateTranslate(&r_surf_waterscrollmatrix, sin(r_refdef.time) * 0.025 * r_waterscroll.value, sin(r_refdef.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
398         if (ent->model)
399                 for (i = 0;i < ent->model->brush.num_textures;i++)
400                         R_UpdateTextureInfo(ent, ent->model->brush.data_textures + i);
401 }
402
403 static void R_DrawSurfaceList(const entity_render_t *ent, texture_t *texture, int texturenumsurfaces, const msurface_t **texturesurfacelist, const vec3_t modelorg)
404 {
405         int i;
406         int texturesurfaceindex;
407         const float *v, *vertex3f;
408         float *c;
409         float diff[3];
410         float f, r, g, b, a, base, colorscale;
411         const msurface_t *surface;
412         qboolean dolightmap;
413         qboolean dobase;
414         qboolean doambient;
415         qboolean dodetail;
416         qboolean doglow;
417         qboolean dofogpass;
418         qboolean fogallpasses;
419         qboolean waterscrolling;
420         rmeshstate_t m;
421         texture = texture->currentframe;
422         if (texture->currentmaterialflags & MATERIALFLAG_NODRAW)
423                 return;
424         c_faces += texturenumsurfaces;
425         // gl_lightmaps debugging mode skips normal texturing
426         if (gl_lightmaps.integer)
427         {
428                 GL_BlendFunc(GL_ONE, GL_ZERO);
429                 GL_DepthMask(true);
430                 GL_DepthTest(true);
431                 qglDisable(GL_CULL_FACE);
432                 GL_Color(1, 1, 1, 1);
433                 memset(&m, 0, sizeof(m));
434                 R_Mesh_State(&m);
435                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
436                 {
437                         surface = texturesurfacelist[texturesurfaceindex];
438                         R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
439                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordlightmap2f);
440                         R_Mesh_ColorPointer(surface->lightmaptexture ? NULL : surface->mesh.data_lightmapcolor4f);
441                         R_Mesh_VertexPointer(surface->mesh.data_vertex3f);
442                         GL_LockArrays(0, surface->mesh.num_vertices);
443                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
444                         GL_LockArrays(0, 0);
445                 }
446                 qglEnable(GL_CULL_FACE);
447                 return;
448         }
449         GL_DepthTest(!(texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
450         GL_DepthMask(!(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT));
451         if (texture->currentmaterialflags & MATERIALFLAG_ADD)
452                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
453         else if (texture->currentmaterialflags & MATERIALFLAG_ALPHA)
454                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
455         else
456                 GL_BlendFunc(GL_ONE, GL_ZERO);
457         // water waterscrolling in texture matrix
458         waterscrolling = (texture->currentmaterialflags & MATERIALFLAG_WATER) && r_waterscroll.value != 0;
459         if (texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
460                 qglDisable(GL_CULL_FACE);
461         if (texture->currentmaterialflags & MATERIALFLAG_SKY)
462         {
463                 if (skyrendernow)
464                 {
465                         skyrendernow = false;
466                         if (skyrendermasked)
467                                 R_Sky();
468                 }
469                 // LordHavoc: HalfLife maps have freaky skypolys...
470                 if (!ent->model->brush.ishlbsp)
471                 {
472                         R_Mesh_Matrix(&ent->matrix);
473                         GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
474                         if (skyrendermasked)
475                         {
476                                 // depth-only (masking)
477                                 GL_ColorMask(0,0,0,0);
478                                 // just to make sure that braindead drivers don't draw anything
479                                 // despite that colormask...
480                                 GL_BlendFunc(GL_ZERO, GL_ONE);
481                         }
482                         else
483                         {
484                                 // fog sky
485                                 GL_BlendFunc(GL_ONE, GL_ZERO);
486                         }
487                         GL_DepthMask(true);
488                         GL_DepthTest(true);
489                         memset(&m, 0, sizeof(m));
490                         R_Mesh_State(&m);
491                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
492                         {
493                                 surface = texturesurfacelist[texturesurfaceindex];
494                                 R_Mesh_VertexPointer(surface->mesh.data_vertex3f);
495                                 GL_LockArrays(0, surface->mesh.num_vertices);
496                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
497                                 GL_LockArrays(0, 0);
498                         }
499                         GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
500                 }
501         }
502         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)
503         {
504                 // NVIDIA Geforce3 distortion texture shader on water
505                 float args[4] = {0.05f,0,0,0.04f};
506                 memset(&m, 0, sizeof(m));
507                 m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(r_refdef.time * 16)&63]);
508                 m.tex[1] = R_GetTexture(texture->skin.base);
509                 m.texcombinergb[0] = GL_REPLACE;
510                 m.texcombinergb[1] = GL_REPLACE;
511                 Matrix4x4_CreateFromQuakeEntity(&m.texmatrix[0], 0, 0, 0, 0, 0, 0, r_watershader.value);
512                 m.texmatrix[1] = r_surf_waterscrollmatrix;
513                 R_Mesh_State(&m);
514
515                 GL_Color(1, 1, 1, texture->currentalpha);
516                 GL_ActiveTexture(0);
517                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
518                 GL_ActiveTexture(1);
519                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
520                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
521                 qglTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
522                 qglEnable(GL_TEXTURE_SHADER_NV);
523
524                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
525                 {
526                         surface = texturesurfacelist[texturesurfaceindex];
527                         R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, texture, surface, modelorg));
528                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
529                         R_Mesh_TexCoordPointer(1, 2, surface->mesh.data_texcoordtexture2f);
530                         GL_LockArrays(0, surface->mesh.num_vertices);
531                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
532                         GL_LockArrays(0, 0);
533                 }
534
535                 qglDisable(GL_TEXTURE_SHADER_NV);
536                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
537                 GL_ActiveTexture(0);
538         }
539         else if (texture->currentmaterialflags & (MATERIALFLAG_WATER | MATERIALFLAG_WALL))
540         {
541                 // normal surface (wall or water)
542                 dobase = true;
543                 dolightmap = !(texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT);
544                 doambient = r_ambient.value >= (1/64.0f);
545                 dodetail = r_detailtextures.integer && texture->skin.detail != NULL && !(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT);
546                 doglow = texture->skin.glow != NULL;
547                 dofogpass = fogenabled && !(texture->currentmaterialflags & MATERIALFLAG_ADD);
548                 fogallpasses = fogenabled && !(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT);
549                 if (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT)
550                 {
551                         if (dobase && dolightmap && gl_combine.integer)
552                         {
553                                 dobase = false;
554                                 memset(&m, 0, sizeof(m));
555                                 m.tex[1] = R_GetTexture(texture->skin.base);
556                                 if (waterscrolling)
557                                         m.texmatrix[1] = r_surf_waterscrollmatrix;
558                                 m.texrgbscale[1] = 2;
559                                 m.pointer_color = varray_color4f;
560                                 R_Mesh_State(&m);
561                                 colorscale = 1;
562                                 r = ent->colormod[0] * colorscale;
563                                 g = ent->colormod[1] * colorscale;
564                                 b = ent->colormod[2] * colorscale;
565                                 a = texture->currentalpha;
566                                 base = r_ambient.value * (1.0f / 64.0f);
567                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
568                                 {
569                                         surface = texturesurfacelist[texturesurfaceindex];
570                                         vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
571                                         R_Mesh_VertexPointer(vertex3f);
572                                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordlightmap2f);
573                                         R_Mesh_TexCoordPointer(1, 2, surface->mesh.data_texcoordtexture2f);
574                                         if (surface->lightmaptexture)
575                                         {
576                                                 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
577                                                 if (fogallpasses)
578                                                 {
579                                                         R_Mesh_ColorPointer(varray_color4f);
580                                                         for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
581                                                         {
582                                                                 VectorSubtract(v, modelorg, diff);
583                                                                 f = 1 - exp(fogdensity/DotProduct(diff, diff));
584                                                                 c[0] = f * r;
585                                                                 c[1] = f * g;
586                                                                 c[2] = f * b;
587                                                                 c[3] = a;
588                                                         }
589                                                 }
590                                                 else
591                                                 {
592                                                         R_Mesh_ColorPointer(NULL);
593                                                         GL_Color(r, g, b, a);
594                                                 }
595                                         }
596                                         else
597                                         {
598                                                 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
599                                                 R_Mesh_ColorPointer(varray_color4f);
600                                                 if (surface->styles[0] != 255)
601                                                 {
602                                                         for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
603                                                         {
604                                                                 c[0] = 0;
605                                                                 c[1] = 0;
606                                                                 c[2] = 0;
607                                                                 if (surface->styles[0] != 255)
608                                                                 {
609                                                                         if (surface->mesh.data_lightmapcolor4f)
610                                                                         {
611                                                                                 float scale = d_lightstylevalue[surface->styles[0]] * (1.0f / 128.0f);
612                                                                                 VectorMA(c, scale, surface->mesh.data_lightmapcolor4f + i*4, c);
613                                                                         }
614                                                                         else if (surface->mesh.data_lightmapoffsets)
615                                                                         {
616                                                                                 const qbyte *lm = surface->samples + surface->mesh.data_lightmapoffsets[i];
617                                                                                 float scale = d_lightstylevalue[surface->styles[0]] * (1.0f / 32768.0f);
618                                                                                 VectorMA(c, scale, lm, c);
619                                                                                 if (surface->styles[1] != 255)
620                                                                                 {
621                                                                                         int size3 = ((surface->extents[0]>>4)+1)*((surface->extents[1]>>4)+1)*3;
622                                                                                         lm += size3;
623                                                                                         scale = d_lightstylevalue[surface->styles[1]] * (1.0f / 32768.0f);
624                                                                                         VectorMA(c, scale, lm, c);
625                                                                                         if (surface->styles[2] != 255)
626                                                                                         {
627                                                                                                 lm += size3;
628                                                                                                 scale = d_lightstylevalue[surface->styles[2]] * (1.0f / 32768.0f);
629                                                                                                 VectorMA(c, scale, lm, c);
630                                                                                                 if (surface->styles[3] != 255)
631                                                                                                 {
632                                                                                                         lm += size3;
633                                                                                                         scale = d_lightstylevalue[surface->styles[3]] * (1.0f / 32768.0f);
634                                                                                                         VectorMA(c, scale, lm, c);
635                                                                                                 }
636                                                                                         }
637                                                                                 }
638                                                                         }
639                                                                 }
640                                                                 c[0] *= r;
641                                                                 c[1] *= g;
642                                                                 c[2] *= b;
643                                                                 if (fogallpasses)
644                                                                 {
645                                                                         VectorSubtract(v, modelorg, diff);
646                                                                         f = 1 - exp(fogdensity/DotProduct(diff, diff));
647                                                                         VectorScale(c, f, c);
648                                                                 }
649                                                                 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
650                                                                         c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
651                                                                 else
652                                                                         c[3] = a;
653                                                         }
654                                                 }
655                                                 else
656                                                 {
657                                                         if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
658                                                         {
659                                                                 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
660                                                                 {
661                                                                         c[0] = 0;
662                                                                         c[1] = 0;
663                                                                         c[2] = 0;
664                                                                         c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
665                                                                 }
666                                                         }
667                                                         else
668                                                         {
669                                                                 R_Mesh_ColorPointer(NULL);
670                                                                 GL_Color(0, 0, 0, a);
671                                                         }
672                                                 }
673                                         }
674                                         GL_LockArrays(0, surface->mesh.num_vertices);
675                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
676                                         GL_LockArrays(0, 0);
677                                 }
678                         }
679                         if (dobase)
680                         {
681                                 dobase = false;
682                                 memset(&m, 0, sizeof(m));
683                                 m.tex[0] = R_GetTexture(texture->skin.base);
684                                 if (waterscrolling)
685                                         m.texmatrix[0] = r_surf_waterscrollmatrix;
686                                 m.texmatrix[0] = r_surf_waterscrollmatrix;
687                                 m.pointer_color = varray_color4f;
688                                 colorscale = 1;
689                                 if (gl_combine.integer)
690                                 {
691                                         m.texrgbscale[0] = 4;
692                                         colorscale *= 0.25f;
693                                 }
694                                 R_Mesh_State(&m);
695                                 r = ent->colormod[0] * colorscale;
696                                 g = ent->colormod[1] * colorscale;
697                                 b = ent->colormod[2] * colorscale;
698                                 a = texture->currentalpha;
699                                 if (dolightmap)
700                                 {
701                                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
702                                         {
703                                                 surface = texturesurfacelist[texturesurfaceindex];
704                                                 vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
705                                                 R_Mesh_VertexPointer(vertex3f);
706                                                 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
707                                                 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
708                                                 {
709                                                         c[0] = 0;
710                                                         c[1] = 0;
711                                                         c[2] = 0;
712                                                         if (surface->styles[0] != 255)
713                                                         {
714                                                                 if (surface->mesh.data_lightmapcolor4f)
715                                                                 {
716                                                                         float scale = d_lightstylevalue[surface->styles[0]] * (1.0f / 128.0f);
717                                                                         VectorMA(c, scale, surface->mesh.data_lightmapcolor4f + i*4, c);
718                                                                 }
719                                                                 else if (surface->mesh.data_lightmapoffsets)
720                                                                 {
721                                                                         const qbyte *lm = surface->samples + surface->mesh.data_lightmapoffsets[i];
722                                                                         float scale = d_lightstylevalue[surface->styles[0]] * (1.0f / 32768.0f);
723                                                                         VectorMA(c, scale, lm, c);
724                                                                         if (surface->styles[1] != 255)
725                                                                         {
726                                                                                 int size3 = ((surface->extents[0]>>4)+1)*((surface->extents[1]>>4)+1)*3;
727                                                                                 lm += size3;
728                                                                                 scale = d_lightstylevalue[surface->styles[1]] * (1.0f / 32768.0f);
729                                                                                 VectorMA(c, scale, lm, c);
730                                                                                 if (surface->styles[2] != 255)
731                                                                                 {
732                                                                                         lm += size3;
733                                                                                         scale = d_lightstylevalue[surface->styles[2]] * (1.0f / 32768.0f);
734                                                                                         VectorMA(c, scale, lm, c);
735                                                                                         if (surface->styles[3] != 255)
736                                                                                         {
737                                                                                                 lm += size3;
738                                                                                                 scale = d_lightstylevalue[surface->styles[3]] * (1.0f / 32768.0f);
739                                                                                                 VectorMA(c, scale, lm, c);
740                                                                                         }
741                                                                                 }
742                                                                         }
743                                                                 }
744                                                         }
745                                                         c[0] *= r;
746                                                         c[1] *= g;
747                                                         c[2] *= b;
748                                                         if (fogallpasses)
749                                                         {
750                                                                 VectorSubtract(v, modelorg, diff);
751                                                                 f = 1 - exp(fogdensity/DotProduct(diff, diff));
752                                                                 VectorScale(c, f, c);
753                                                         }
754                                                         if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
755                                                                 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
756                                                         else
757                                                                 c[3] = a;
758                                                 }
759                                                 GL_LockArrays(0, surface->mesh.num_vertices);
760                                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
761                                                 GL_LockArrays(0, 0);
762                                         }
763                                 }
764                                 else
765                                 {
766                                         if (fogallpasses)
767                                         {
768                                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
769                                                 {
770                                                         surface = texturesurfacelist[texturesurfaceindex];
771                                                         vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
772                                                         R_Mesh_VertexPointer(vertex3f);
773                                                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
774                                                         if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
775                                                         {
776                                                                 R_Mesh_ColorPointer(varray_color4f);
777                                                                 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
778                                                                 {
779                                                                         VectorSubtract(v, modelorg, diff);
780                                                                         f = 1 - exp(fogdensity/DotProduct(diff, diff));
781                                                                         c[0] = r * f;
782                                                                         c[1] = g * f;
783                                                                         c[2] = b * f;
784                                                                         c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
785                                                                 }
786                                                         }
787                                                         else
788                                                         {
789                                                                 R_Mesh_ColorPointer(varray_color4f);
790                                                                 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
791                                                                 {
792                                                                         VectorSubtract(v, modelorg, diff);
793                                                                         f = 1 - exp(fogdensity/DotProduct(diff, diff));
794                                                                         c[0] = r * f;
795                                                                         c[1] = g * f;
796                                                                         c[2] = b * f;
797                                                                         c[3] = a;
798                                                                 }
799                                                         }
800                                                         GL_LockArrays(0, surface->mesh.num_vertices);
801                                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
802                                                         GL_LockArrays(0, 0);
803                                                 }
804                                         }
805                                         else
806                                         {
807                                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
808                                                 {
809                                                         surface = texturesurfacelist[texturesurfaceindex];
810                                                         vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
811                                                         R_Mesh_VertexPointer(vertex3f);
812                                                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
813                                                         if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
814                                                         {
815                                                                 R_Mesh_ColorPointer(varray_color4f);
816                                                                 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
817                                                                 {
818                                                                         c[0] = r;
819                                                                         c[1] = g;
820                                                                         c[2] = b;
821                                                                         c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
822                                                                 }
823                                                         }
824                                                         else
825                                                         {
826                                                                 R_Mesh_ColorPointer(NULL);
827                                                                 GL_Color(r, g, b, a);
828                                                         }
829                                                         GL_LockArrays(0, surface->mesh.num_vertices);
830                                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
831                                                         GL_LockArrays(0, 0);
832                                                 }
833                                         }
834                                 }
835                         }
836                 }
837                 else
838                 {
839                         if (!dolightmap && dobase)
840                         {
841                                 dolightmap = false;
842                                 dobase = false;
843                                 GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], 1);
844                                 memset(&m, 0, sizeof(m));
845                                 m.tex[0] = R_GetTexture(texture->skin.base);
846                                 if (waterscrolling)
847                                         m.texmatrix[0] = r_surf_waterscrollmatrix;
848                                 R_Mesh_State(&m);
849                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
850                                 {
851                                         surface = texturesurfacelist[texturesurfaceindex];
852                                         R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, texture, surface, modelorg));
853                                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
854                                         GL_LockArrays(0, surface->mesh.num_vertices);
855                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
856                                         GL_LockArrays(0, 0);
857                                 }
858                         }
859                         if (r_lightmapintensity <= 0 && dolightmap && dobase)
860                         {
861                                 dolightmap = false;
862                                 dobase = false;
863                                 GL_Color(0, 0, 0, 1);
864                                 memset(&m, 0, sizeof(m));
865                                 R_Mesh_State(&m);
866                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
867                                 {
868                                         surface = texturesurfacelist[texturesurfaceindex];
869                                         R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, texture, surface, modelorg));
870                                         GL_LockArrays(0, surface->mesh.num_vertices);
871                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
872                                         GL_LockArrays(0, 0);
873                                 }
874                         }
875                         if (r_textureunits.integer >= 2 && gl_combine.integer && dolightmap && dobase)
876                         {
877                                 // dualtexture combine
878                                 GL_BlendFunc(GL_ONE, GL_ZERO);
879                                 GL_DepthMask(true);
880                                 dolightmap = false;
881                                 dobase = false;
882                                 memset(&m, 0, sizeof(m));
883                                 m.tex[1] = R_GetTexture(texture->skin.base);
884                                 if (waterscrolling)
885                                         m.texmatrix[1] = r_surf_waterscrollmatrix;
886                                 m.texrgbscale[1] = 2;
887                                 R_Mesh_State(&m);
888                                 r = ent->colormod[0] * r_lightmapintensity;
889                                 g = ent->colormod[1] * r_lightmapintensity;
890                                 b = ent->colormod[2] * r_lightmapintensity;
891                                 GL_Color(r, g, b, 1);
892                                 if (texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
893                                 {
894                                         R_Mesh_VertexPointer(varray_vertex3f);
895                                         if (r == 1 && g == 1 && b == 1)
896                                         {
897                                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
898                                                 {
899                                                         surface = texturesurfacelist[texturesurfaceindex];
900                                                         RSurf_DeformVertices(ent, texture, surface, modelorg);
901                                                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordlightmap2f);
902                                                         R_Mesh_TexCoordPointer(1, 2, surface->mesh.data_texcoordtexture2f);
903                                                         if (surface->lightmaptexture)
904                                                         {
905                                                                 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
906                                                                 R_Mesh_ColorPointer(NULL);
907                                                         }
908                                                         else //if (r == 1 && g == 1 && b == 1)
909                                                         {
910                                                                 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
911                                                                 R_Mesh_ColorPointer(surface->mesh.data_lightmapcolor4f);
912                                                         }
913                                                         GL_LockArrays(0, surface->mesh.num_vertices);
914                                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
915                                                         GL_LockArrays(0, 0);
916                                                 }
917                                         }
918                                         else
919                                         {
920                                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
921                                                 {
922                                                         surface = texturesurfacelist[texturesurfaceindex];
923                                                         RSurf_DeformVertices(ent, texture, surface, modelorg);
924                                                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordlightmap2f);
925                                                         R_Mesh_TexCoordPointer(1, 2, surface->mesh.data_texcoordtexture2f);
926                                                         if (surface->lightmaptexture)
927                                                         {
928                                                                 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
929                                                                 R_Mesh_ColorPointer(NULL);
930                                                         }
931                                                         else
932                                                         {
933                                                                 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
934                                                                 R_Mesh_ColorPointer(varray_color4f);
935                                                                 for (i = 0;i < surface->mesh.num_vertices;i++)
936                                                                 {
937                                                                         varray_color4f[i*4+0] = surface->mesh.data_lightmapcolor4f[i*4+0] * r;
938                                                                         varray_color4f[i*4+1] = surface->mesh.data_lightmapcolor4f[i*4+1] * g;
939                                                                         varray_color4f[i*4+2] = surface->mesh.data_lightmapcolor4f[i*4+2] * b;
940                                                                         varray_color4f[i*4+3] = surface->mesh.data_lightmapcolor4f[i*4+3];
941                                                                 }
942                                                         }
943                                                         GL_LockArrays(0, surface->mesh.num_vertices);
944                                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
945                                                         GL_LockArrays(0, 0);
946                                                 }
947                                         }
948                                 }
949                                 else
950                                 {
951                                         if (r == 1 && g == 1 && b == 1)
952                                         {
953 #if 0
954                                                 // experimental direct state calls for measuring
955                                                 // R_Mesh_ call overhead, do not use!
956                                                 R_Mesh_VertexPointer(varray_vertex3f);
957                                                 R_Mesh_TexCoordPointer(0, 2, varray_texcoord2f[0]);
958                                                 R_Mesh_TexCoordPointer(1, 2, varray_texcoord2f[1]);
959                                                 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
960                                                 R_Mesh_ColorPointer(varray_color4f);
961                                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
962                                                 {
963                                                         surface = texturesurfacelist[texturesurfaceindex];
964                                                         qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), surface->mesh.data_vertex3f);
965                                                         qglClientActiveTexture(GL_TEXTURE0_ARB);
966                                                         qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), surface->mesh.data_texcoordlightmap2f);
967                                                         qglClientActiveTexture(GL_TEXTURE1_ARB);
968                                                         qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), surface->mesh.data_texcoordtexture2f);
969                                                         if (surface->lightmaptexture)
970                                                         {
971                                                                 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
972                                                                 qglDisableClientState(GL_COLOR_ARRAY);
973                                                                 qglColor4f(r, g, b, 1);
974                                                         }
975                                                         else //if (r == 1 && g == 1 && b == 1)
976                                                         {
977                                                                 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
978                                                                 qglEnableClientState(GL_COLOR_ARRAY);
979                                                                 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), surface->mesh.data_lightmapcolor4f);
980                                                         }
981                                                         qglLockArraysEXT(0, surface->mesh.num_vertices);
982                                                         qglDrawRangeElements(GL_TRIANGLES, 0, surface->mesh.num_vertices, surface->mesh.num_triangles * 3, GL_UNSIGNED_INT, surface->mesh.data_element3i);
983                                                         qglUnlockArraysEXT();
984                                                 }
985 #else
986                                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
987                                                 {
988                                                         surface = texturesurfacelist[texturesurfaceindex];
989                                                         R_Mesh_VertexPointer(surface->mesh.data_vertex3f);
990                                                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordlightmap2f);
991                                                         R_Mesh_TexCoordPointer(1, 2, surface->mesh.data_texcoordtexture2f);
992                                                         if (surface->lightmaptexture)
993                                                         {
994                                                                 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
995                                                                 R_Mesh_ColorPointer(NULL);
996                                                         }
997                                                         else //if (r == 1 && g == 1 && b == 1)
998                                                         {
999                                                                 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
1000                                                                 R_Mesh_ColorPointer(surface->mesh.data_lightmapcolor4f);
1001                                                         }
1002                                                         GL_LockArrays(0, surface->mesh.num_vertices);
1003                                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1004                                                         GL_LockArrays(0, 0);
1005                                                 }
1006 #endif
1007                                         }
1008                                         else
1009                                         {
1010                                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1011                                                 {
1012                                                         surface = texturesurfacelist[texturesurfaceindex];
1013                                                         R_Mesh_VertexPointer(surface->mesh.data_vertex3f);
1014                                                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordlightmap2f);
1015                                                         R_Mesh_TexCoordPointer(1, 2, surface->mesh.data_texcoordtexture2f);
1016                                                         if (surface->lightmaptexture)
1017                                                         {
1018                                                                 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
1019                                                                 R_Mesh_ColorPointer(NULL);
1020                                                         }
1021                                                         else
1022                                                         {
1023                                                                 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
1024                                                                 R_Mesh_ColorPointer(varray_color4f);
1025                                                                 for (i = 0;i < surface->mesh.num_vertices;i++)
1026                                                                 {
1027                                                                         varray_color4f[i*4+0] = surface->mesh.data_lightmapcolor4f[i*4+0] * r;
1028                                                                         varray_color4f[i*4+1] = surface->mesh.data_lightmapcolor4f[i*4+1] * g;
1029                                                                         varray_color4f[i*4+2] = surface->mesh.data_lightmapcolor4f[i*4+2] * b;
1030                                                                         varray_color4f[i*4+3] = surface->mesh.data_lightmapcolor4f[i*4+3];
1031                                                                 }
1032                                                         }
1033                                                         GL_LockArrays(0, surface->mesh.num_vertices);
1034                                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1035                                                         GL_LockArrays(0, 0);
1036                                                 }
1037                                         }
1038                                 }
1039                         }
1040                         // single texture
1041                         if (dolightmap)
1042                         {
1043                                 GL_BlendFunc(GL_ONE, GL_ZERO);
1044                                 GL_DepthMask(true);
1045                                 GL_Color(1, 1, 1, 1);
1046                                 memset(&m, 0, sizeof(m));
1047                                 R_Mesh_State(&m);
1048                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1049                                 {
1050                                         surface = texturesurfacelist[texturesurfaceindex];
1051                                         R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, texture, surface, modelorg));
1052                                         R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
1053                                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordlightmap2f);
1054                                         R_Mesh_ColorPointer(surface->lightmaptexture ? NULL : surface->mesh.data_lightmapcolor4f);
1055                                         GL_LockArrays(0, surface->mesh.num_vertices);
1056                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1057                                         GL_LockArrays(0, 0);
1058                                 }
1059                         }
1060                         if (dobase)
1061                         {
1062                                 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1063                                 GL_DepthMask(false);
1064                                 GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
1065                                 memset(&m, 0, sizeof(m));
1066                                 m.tex[0] = R_GetTexture(texture->skin.base);
1067                                 if (waterscrolling)
1068                                         m.texmatrix[0] = r_surf_waterscrollmatrix;
1069                                 R_Mesh_State(&m);
1070                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1071                                 {
1072                                         surface = texturesurfacelist[texturesurfaceindex];
1073                                         R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, texture, surface, modelorg));
1074                                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
1075                                         GL_LockArrays(0, surface->mesh.num_vertices);
1076                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1077                                         GL_LockArrays(0, 0);
1078                                 }
1079                         }
1080                 }
1081                 if (doambient)
1082                 {
1083                         doambient = false;
1084                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1085                         GL_DepthMask(false);
1086                         memset(&m, 0, sizeof(m));
1087                         m.tex[0] = R_GetTexture(texture->skin.base);
1088                         if (waterscrolling)
1089                                 m.texmatrix[0] = r_surf_waterscrollmatrix;
1090                         m.pointer_color = varray_color4f;
1091                         colorscale = 1;
1092                         if (gl_combine.integer)
1093                         {
1094                                 m.texrgbscale[0] = 4;
1095                                 colorscale *= 0.25f;
1096                         }
1097                         R_Mesh_State(&m);
1098                         base = r_ambient.value * (1.0f / 64.0f);
1099                         r = ent->colormod[0] * colorscale * base;
1100                         g = ent->colormod[1] * colorscale * base;
1101                         b = ent->colormod[2] * colorscale * base;
1102                         a = texture->currentalpha;
1103                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1104                         {
1105                                 surface = texturesurfacelist[texturesurfaceindex];
1106                                 vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
1107                                 R_Mesh_VertexPointer(vertex3f);
1108                                 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
1109                                 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1110                                 {
1111                                         c[0] = r;
1112                                         c[1] = g;
1113                                         c[2] = b;
1114                                         if (fogallpasses)
1115                                         {
1116                                                 VectorSubtract(v, modelorg, diff);
1117                                                 f = 1 - exp(fogdensity/DotProduct(diff, diff));
1118                                                 VectorScale(c, f, c);
1119                                         }
1120                                         if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1121                                                 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
1122                                         else
1123                                                 c[3] = a;
1124                                 }
1125                                 GL_LockArrays(0, surface->mesh.num_vertices);
1126                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1127                                 GL_LockArrays(0, 0);
1128                         }
1129                 }
1130                 if (dodetail)
1131                 {
1132                         GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1133                         GL_DepthMask(false);
1134                         GL_Color(1, 1, 1, 1);
1135                         memset(&m, 0, sizeof(m));
1136                         m.tex[0] = R_GetTexture(texture->skin.detail);
1137                         R_Mesh_State(&m);
1138                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1139                         {
1140                                 surface = texturesurfacelist[texturesurfaceindex];
1141                                 R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, texture, surface, modelorg));
1142                                 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoorddetail2f);
1143                                 GL_LockArrays(0, surface->mesh.num_vertices);
1144                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1145                                 GL_LockArrays(0, 0);
1146                         }
1147                 }
1148                 if (doglow)
1149                 {
1150                         // if glow was not already done using multitexture, do it now.
1151                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1152                         GL_DepthMask(false);
1153                         memset(&m, 0, sizeof(m));
1154                         m.tex[0] = R_GetTexture(texture->skin.glow);
1155                         if (waterscrolling)
1156                                 m.texmatrix[0] = r_surf_waterscrollmatrix;
1157                         m.pointer_color = varray_color4f;
1158                         R_Mesh_State(&m);
1159                         colorscale = 1;
1160                         r = ent->colormod[0] * colorscale;
1161                         g = ent->colormod[1] * colorscale;
1162                         b = ent->colormod[2] * colorscale;
1163                         a = texture->currentalpha;
1164                         if (fogallpasses)
1165                         {
1166                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1167                                 {
1168                                         surface = texturesurfacelist[texturesurfaceindex];
1169                                         vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
1170                                         R_Mesh_VertexPointer(vertex3f);
1171                                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
1172                                         R_Mesh_ColorPointer(varray_color4f);
1173                                         if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1174                                         {
1175                                                 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1176                                                 {
1177                                                         VectorSubtract(v, modelorg, diff);
1178                                                         f = 1 - exp(fogdensity/DotProduct(diff, diff));
1179                                                         c[0] = f * r;
1180                                                         c[1] = f * g;
1181                                                         c[2] = f * b;
1182                                                         c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
1183                                                 }
1184                                         }
1185                                         else
1186                                         {
1187                                                 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1188                                                 {
1189                                                         VectorSubtract(v, modelorg, diff);
1190                                                         f = 1 - exp(fogdensity/DotProduct(diff, diff));
1191                                                         c[0] = f * r;
1192                                                         c[1] = f * g;
1193                                                         c[2] = f * b;
1194                                                         c[3] = a;
1195                                                 }
1196                                         }
1197                                         GL_LockArrays(0, surface->mesh.num_vertices);
1198                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1199                                         GL_LockArrays(0, 0);
1200                                 }
1201                         }
1202                         else
1203                         {
1204                                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1205                                 {
1206                                         surface = texturesurfacelist[texturesurfaceindex];
1207                                         vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
1208                                         R_Mesh_VertexPointer(vertex3f);
1209                                         R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
1210                                         if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1211                                         {
1212                                                 R_Mesh_ColorPointer(varray_color4f);
1213                                                 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1214                                                 {
1215                                                         c[0] = r;
1216                                                         c[1] = g;
1217                                                         c[2] = b;
1218                                                         c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
1219                                                 }
1220                                         }
1221                                         else
1222                                         {
1223                                                 R_Mesh_ColorPointer(NULL);
1224                                                 GL_Color(r, g, b, a);
1225                                         }
1226                                         GL_LockArrays(0, surface->mesh.num_vertices);
1227                                         R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1228                                         GL_LockArrays(0, 0);
1229                                 }
1230                         }
1231                 }
1232                 if (dofogpass)
1233                 {
1234                         // if this is opaque use alpha blend which will darken the earlier
1235                         // passes cheaply.
1236                         //
1237                         // if this is an alpha blended material, all the earlier passes
1238                         // were darkened by fog already, so we only need to add the fog
1239                         // color ontop through the fog mask texture
1240                         //
1241                         // if this is an additive blended material, all the earlier passes
1242                         // were darkened by fog already, and we should not add fog color
1243                         // (because the background was not darkened, there is no fog color
1244                         // that was lost behind it).
1245                         if (!fogallpasses)
1246                                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1247                         else
1248                                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1249                         GL_DepthMask(false);
1250                         memset(&m, 0, sizeof(m));
1251                         m.tex[0] = R_GetTexture(texture->skin.fog);
1252                         if (waterscrolling)
1253                                 m.texmatrix[0] = r_surf_waterscrollmatrix;
1254                         R_Mesh_State(&m);
1255                         r = fogcolor[0];
1256                         g = fogcolor[1];
1257                         b = fogcolor[2];
1258                         a = texture->currentalpha;
1259                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1260                         {
1261                                 surface = texturesurfacelist[texturesurfaceindex];
1262                                 vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
1263                                 R_Mesh_VertexPointer(vertex3f);
1264                                 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
1265                                 R_Mesh_ColorPointer(varray_color4f);
1266                                 //RSurf_FogPassColors_Vertex3f_Color4f(surface->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], texture->currentalpha, 1, surface->mesh.num_vertices, modelorg);
1267                                 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1268                                 {
1269                                         for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1270                                         {
1271                                                 VectorSubtract(v, modelorg, diff);
1272                                                 f = exp(fogdensity/DotProduct(diff, diff));
1273                                                 c[0] = r;
1274                                                 c[1] = g;
1275                                                 c[2] = b;
1276                                                 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * f * a;
1277                                         }
1278                                 }
1279                                 else
1280                                 {
1281                                         for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1282                                         {
1283                                                 VectorSubtract(v, modelorg, diff);
1284                                                 f = exp(fogdensity/DotProduct(diff, diff));
1285                                                 c[0] = r;
1286                                                 c[1] = g;
1287                                                 c[2] = b;
1288                                                 c[3] = f * a;
1289                                         }
1290                                 }
1291                                 GL_LockArrays(0, surface->mesh.num_vertices);
1292                                 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1293                                 GL_LockArrays(0, 0);
1294                         }
1295                 }
1296         }
1297         if (texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1298                 qglEnable(GL_CULL_FACE);
1299 }
1300
1301 static void RSurfShader_Transparent_Callback(const void *calldata1, int calldata2)
1302 {
1303         const entity_render_t *ent = calldata1;
1304         const msurface_t *surface = ent->model->brush.data_surfaces + calldata2;
1305         vec3_t modelorg;
1306         texture_t *texture;
1307
1308         texture = surface->texture;
1309         if (texture->basematerialflags & MATERIALFLAG_SKY)
1310                 return; // transparent sky is too difficult
1311         R_UpdateTextureInfo(ent, texture);
1312
1313         R_Mesh_Matrix(&ent->matrix);
1314         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1315         R_DrawSurfaceList(ent, texture, 1, &surface, modelorg);
1316 }
1317
1318 void R_QueueSurfaceList(entity_render_t *ent, texture_t *texture, int texturenumsurfaces, const msurface_t **texturesurfacelist, const vec3_t modelorg)
1319 {
1320         int texturesurfaceindex;
1321         const msurface_t *surface;
1322         vec3_t tempcenter, center;
1323         if (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT)
1324         {
1325                 // drawing sky transparently would be too difficult
1326                 if (!(texture->currentmaterialflags & MATERIALFLAG_SKY))
1327                 {
1328                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1329                         {
1330                                 surface = texturesurfacelist[texturesurfaceindex];
1331                                 tempcenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
1332                                 tempcenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
1333                                 tempcenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
1334                                 Matrix4x4_Transform(&ent->matrix, tempcenter, center);
1335                                 R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_vieworigin : center, RSurfShader_Transparent_Callback, ent, surface - ent->model->brush.data_surfaces);
1336                         }
1337                 }
1338         }
1339         else
1340                 R_DrawSurfaceList(ent, texture, texturenumsurfaces, texturesurfacelist, modelorg);
1341 }
1342
1343 void R_DrawSurfaces(entity_render_t *ent, qboolean skysurfaces)
1344 {
1345         int i, j, f, flagsmask;
1346         msurface_t *surface, **surfacechain;
1347         texture_t *t, *texture;
1348         model_t *model = ent->model;
1349         vec3_t modelorg;
1350         const int maxsurfacelist = 1024;
1351         int numsurfacelist = 0;
1352         const msurface_t *surfacelist[1024];
1353         if (model == NULL)
1354                 return;
1355         R_Mesh_Matrix(&ent->matrix);
1356         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1357
1358         // update light styles
1359         if (!skysurfaces && model->brushq1.light_styleupdatechains)
1360         {
1361                 for (i = 0;i < model->brushq1.light_styles;i++)
1362                 {
1363                         if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1364                         {
1365                                 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1366                                 if ((surfacechain = model->brushq1.light_styleupdatechains[i]))
1367                                         for (;(surface = *surfacechain);surfacechain++)
1368                                                 surface->cached_dlight = true;
1369                         }
1370                 }
1371         }
1372
1373         R_UpdateAllTextureInfo(ent);
1374         flagsmask = skysurfaces ? MATERIALFLAG_SKY : (MATERIALFLAG_WATER | MATERIALFLAG_WALL);
1375         f = 0;
1376         t = NULL;
1377         texture = NULL;
1378         numsurfacelist = 0;
1379         if (ent == r_refdef.worldentity)
1380         {
1381                 for (i = 0, j = model->firstmodelsurface, surface = model->brush.data_surfaces + j;i < model->nummodelsurfaces;i++, j++, surface++)
1382                 {
1383                         if (!r_worldsurfacevisible[j])
1384                                 continue;
1385                         if (t != surface->texture)
1386                         {
1387                                 if (numsurfacelist)
1388                                 {
1389                                         R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1390                                         numsurfacelist = 0;
1391                                 }
1392                                 t = surface->texture;
1393                                 f = t->currentmaterialflags & flagsmask;
1394                                 texture = t->currentframe;
1395                         }
1396                         if (f && surface->mesh.num_triangles)
1397                         {
1398                                 // if lightmap parameters changed, rebuild lightmap texture
1399                                 if (surface->cached_dlight && surface->samples)
1400                                         R_BuildLightMap(ent, surface);
1401                                 // add face to draw list
1402                                 surfacelist[numsurfacelist++] = surface;
1403                                 if (numsurfacelist >= maxsurfacelist)
1404                                 {
1405                                         R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1406                                         numsurfacelist = 0;
1407                                 }
1408                         }
1409                 }
1410         }
1411         else
1412         {
1413                 for (i = 0, j = model->firstmodelsurface, surface = model->brush.data_surfaces + j;i < model->nummodelsurfaces;i++, j++, surface++)
1414                 {
1415                         if (t != surface->texture)
1416                         {
1417                                 if (numsurfacelist)
1418                                 {
1419                                         R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1420                                         numsurfacelist = 0;
1421                                 }
1422                                 t = surface->texture;
1423                                 f = t->currentmaterialflags & flagsmask;
1424                                 texture = t->currentframe;
1425                         }
1426                         if (f && surface->mesh.num_triangles)
1427                         {
1428                                 // if lightmap parameters changed, rebuild lightmap texture
1429                                 if (surface->cached_dlight && surface->samples)
1430                                         R_BuildLightMap(ent, surface);
1431                                 // add face to draw list
1432                                 surfacelist[numsurfacelist++] = surface;
1433                                 if (numsurfacelist >= maxsurfacelist)
1434                                 {
1435                                         R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1436                                         numsurfacelist = 0;
1437                                 }
1438                         }
1439                 }
1440         }
1441         if (numsurfacelist)
1442                 R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1443 }
1444
1445 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1446 {
1447         int i;
1448         float *v;
1449         rmeshstate_t m;
1450         const mportal_t *portal = calldata1;
1451         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1452         GL_DepthMask(false);
1453         GL_DepthTest(true);
1454         R_Mesh_Matrix(&r_identitymatrix);
1455
1456         memset(&m, 0, sizeof(m));
1457         m.pointer_vertex = varray_vertex3f;
1458         R_Mesh_State(&m);
1459
1460         i = calldata2;
1461         GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1462                          ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1463                          ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1464                          0.125f);
1465         if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1466         {
1467                 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1468                         VectorCopy(portal->points[i].position, v);
1469         }
1470         else
1471                 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1472                         VectorCopy(portal->points[i].position, v);
1473         GL_LockArrays(0, portal->numpoints);
1474         R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1475         GL_LockArrays(0, 0);
1476 }
1477
1478 // LordHavoc: this is just a nice debugging tool, very slow
1479 static void R_DrawPortals(void)
1480 {
1481         int i, portalnum;
1482         mportal_t *portal;
1483         float center[3], f;
1484         model_t *model = r_refdef.worldmodel;
1485         if (model == NULL)
1486                 return;
1487         for (portalnum = 0, portal = model->brush.data_portals;portalnum < model->brush.num_portals;portalnum++, portal++)
1488         {
1489                 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1490                 if (!R_CullBox(portal->mins, portal->maxs))
1491                 {
1492                         VectorClear(center);
1493                         for (i = 0;i < portal->numpoints;i++)
1494                                 VectorAdd(center, portal->points[i].position, center);
1495                         f = ixtable[portal->numpoints];
1496                         VectorScale(center, f, center);
1497                         R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, portal, portalnum);
1498                 }
1499         }
1500 }
1501
1502 static void R_DrawCollisionBrush(colbrushf_t *brush)
1503 {
1504         int i;
1505         rmeshstate_t m;
1506         memset(&m, 0, sizeof(m));
1507         m.pointer_vertex = brush->points->v;
1508         R_Mesh_State(&m);
1509         i = (int)(((size_t)brush) / sizeof(colbrushf_t));
1510         GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1511         GL_LockArrays(0, brush->numpoints);
1512         R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1513         GL_LockArrays(0, 0);
1514 }
1515
1516 static void R_DrawCollisionSurface(entity_render_t *ent, msurface_t *surface)
1517 {
1518         int i;
1519         rmeshstate_t m;
1520         if (!surface->mesh.num_collisiontriangles)
1521                 return;
1522         memset(&m, 0, sizeof(m));
1523         m.pointer_vertex = surface->mesh.data_collisionvertex3f;
1524         R_Mesh_State(&m);
1525         i = (int)(((size_t)surface) / sizeof(msurface_t));
1526         GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1527         GL_LockArrays(0, surface->mesh.num_collisionvertices);
1528         R_Mesh_Draw(surface->mesh.num_collisionvertices, surface->mesh.num_collisiontriangles, surface->mesh.data_collisionelement3i);
1529         GL_LockArrays(0, 0);
1530 }
1531
1532 void R_WorldVisibility(void)
1533 {
1534         int i, j, *mark;
1535         mleaf_t *leaf;
1536         mleaf_t *viewleaf;
1537         model_t *model = r_refdef.worldmodel;
1538
1539         if (!model)
1540                 return;
1541
1542         // if possible find the leaf the view origin is in
1543         viewleaf = model->brushq1.PointInLeaf ? model->brushq1.PointInLeaf(model, r_vieworigin) : NULL;
1544         // if possible fetch the visible cluster bits
1545         if (model->brush.FatPVS)
1546                 model->brush.FatPVS(model, r_vieworigin, 2, r_pvsbits, sizeof(r_pvsbits));
1547
1548         // clear the visible surface and leaf flags arrays
1549         memset(r_worldsurfacevisible, 0, model->brush.num_surfaces);
1550         memset(r_worldleafvisible, 0, model->brush.num_leafs);
1551
1552         // if the user prefers surfaceworldnode (testing?) or the viewleaf could
1553         // not be found, or the viewleaf is not part of the visible world
1554         // (floating around in the void), use the pvs method
1555         if (r_surfaceworldnode.integer || !viewleaf || viewleaf->clusterindex < 0)
1556         {
1557                 // pvs method:
1558                 // similar to quake's RecursiveWorldNode but without cache misses
1559                 for (j = 0, leaf = model->brush.data_leafs;j < model->brush.num_leafs;j++, leaf++)
1560                 {
1561                         // if leaf is in current pvs and on the screen, mark its surfaces
1562                         if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
1563                         {
1564                                 c_leafs++;
1565                                 r_worldleafvisible[j] = true;
1566                                 if (leaf->numleafsurfaces)
1567                                         for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
1568                                                 r_worldsurfacevisible[*mark] = true;
1569                         }
1570                 }
1571         }
1572         else
1573         {
1574                 int leafstackpos;
1575                 mportal_t *p;
1576                 mleaf_t *leafstack[8192];
1577                 // portal method:
1578                 // follows portals leading outward from viewleaf, does not venture
1579                 // offscreen or into leafs that are not visible, faster than Quake's
1580                 // RecursiveWorldNode and vastly better in unvised maps, often culls a
1581                 // lot of surface that pvs alone would miss
1582                 leafstack[0] = viewleaf;
1583                 leafstackpos = 1;
1584                 while (leafstackpos)
1585                 {
1586                         c_leafs++;
1587                         leaf = leafstack[--leafstackpos];
1588                         r_worldleafvisible[leaf - model->brush.data_leafs] = true;
1589                         // mark any surfaces bounding this leaf
1590                         if (leaf->numleafsurfaces)
1591                                 for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
1592                                         r_worldsurfacevisible[*mark] = true;
1593                         // follow portals into other leafs
1594                         // the checks are:
1595                         // if viewer is behind portal (portal faces outward into the scene)
1596                         // and the portal polygon's bounding box is on the screen
1597                         // and the leaf has not been visited yet
1598                         // and the leaf is visible in the pvs
1599                         // (the first two checks won't cause as many cache misses as the leaf checks)
1600                         for (p = leaf->portals;p;p = p->next)
1601                                 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))
1602                                         leafstack[leafstackpos++] = p->past;
1603                 }
1604         }
1605
1606         if (r_drawportals.integer)
1607                 R_DrawPortals();
1608 }
1609
1610 void R_Q1BSP_DrawSky(entity_render_t *ent)
1611 {
1612         if (ent->model == NULL)
1613                 return;
1614         if (r_drawcollisionbrushes.integer < 2)
1615                 R_DrawSurfaces(ent, true);
1616 }
1617
1618 void R_Q1BSP_Draw(entity_render_t *ent)
1619 {
1620         if (ent->model == NULL)
1621                 return;
1622         c_bmodels++;
1623         if (r_drawcollisionbrushes.integer < 2)
1624                 R_DrawSurfaces(ent, false);
1625         if (r_drawcollisionbrushes.integer >= 1 && ent->model->brush.num_brushes)
1626         {
1627                 int i;
1628                 model_t *model = ent->model;
1629                 msurface_t *surface;
1630                 q3mbrush_t *brush;
1631                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1632                 GL_DepthMask(false);
1633                 GL_DepthTest(true);
1634                 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
1635                 for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
1636                         if (brush->colbrushf && brush->colbrushf->numtriangles)
1637                                 R_DrawCollisionBrush(brush->colbrushf);
1638                 for (i = 0, surface = model->brush.data_surfaces + model->firstmodelsurface;i < model->nummodelsurfaces;i++, surface++)
1639                         if (surface->mesh.num_collisiontriangles)
1640                                 R_DrawCollisionSurface(ent, surface);
1641                 qglPolygonOffset(0, 0);
1642         }
1643 }
1644
1645 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)
1646 {
1647         model_t *model = ent->model;
1648         vec3_t lightmins, lightmaxs;
1649         int t, leafindex, leafsurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
1650         const int *e;
1651         const float *v[3];
1652         msurface_t *surface;
1653         mleaf_t *leaf;
1654         const qbyte *pvs;
1655         lightmins[0] = relativelightorigin[0] - lightradius;
1656         lightmins[1] = relativelightorigin[1] - lightradius;
1657         lightmins[2] = relativelightorigin[2] - lightradius;
1658         lightmaxs[0] = relativelightorigin[0] + lightradius;
1659         lightmaxs[1] = relativelightorigin[1] + lightradius;
1660         lightmaxs[2] = relativelightorigin[2] + lightradius;
1661         *outnumclusterspointer = 0;
1662         *outnumsurfacespointer = 0;
1663         memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
1664         memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
1665         if (model == NULL)
1666         {
1667                 VectorCopy(lightmins, outmins);
1668                 VectorCopy(lightmaxs, outmaxs);
1669                 return;
1670         }
1671         VectorCopy(relativelightorigin, outmins);
1672         VectorCopy(relativelightorigin, outmaxs);
1673         if (model->brush.GetPVS)
1674                 pvs = model->brush.GetPVS(model, relativelightorigin);
1675         else
1676                 pvs = NULL;
1677         R_UpdateAllTextureInfo(ent);
1678         // FIXME: use BSP recursion as lights are often small
1679         for (leafindex = 0, leaf = model->brush.data_leafs;leafindex < model->brush.num_leafs;leafindex++, leaf++)
1680         {
1681                 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
1682                 {
1683                         outmins[0] = min(outmins[0], leaf->mins[0]);
1684                         outmins[1] = min(outmins[1], leaf->mins[1]);
1685                         outmins[2] = min(outmins[2], leaf->mins[2]);
1686                         outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
1687                         outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
1688                         outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
1689                         if (outclusterpvs)
1690                         {
1691                                 if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
1692                                 {
1693                                         SETPVSBIT(outclusterpvs, leaf->clusterindex);
1694                                         outclusterlist[outnumclusters++] = leaf->clusterindex;
1695                                 }
1696                         }
1697                         if (outsurfacepvs)
1698                         {
1699                                 for (leafsurfaceindex = 0;leafsurfaceindex < leaf->numleafsurfaces;leafsurfaceindex++)
1700                                 {
1701                                         surfaceindex = leaf->firstleafsurface[leafsurfaceindex];
1702                                         if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
1703                                         {
1704                                                 surface = model->brush.data_surfaces + surfaceindex;
1705                                                 if (BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs))
1706                                                 if ((surface->texture->currentmaterialflags & (MATERIALFLAG_WALL | MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
1707                                                 {
1708                                                         for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
1709                                                         {
1710                                                                 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1711                                                                 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1712                                                                 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1713                                                                 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])))
1714                                                                 {
1715                                                                         SETPVSBIT(outsurfacepvs, surfaceindex);
1716                                                                         outsurfacelist[outnumsurfaces++] = surfaceindex;
1717                                                                         break;
1718                                                                 }
1719                                                         }
1720                                                 }
1721                                         }
1722                                 }
1723                         }
1724                 }
1725         }
1726
1727         // limit combined leaf box to light boundaries
1728         outmins[0] = max(outmins[0], lightmins[0]);
1729         outmins[1] = max(outmins[1], lightmins[1]);
1730         outmins[2] = max(outmins[2], lightmins[2]);
1731         outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
1732         outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
1733         outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
1734
1735         *outnumclusterspointer = outnumclusters;
1736         *outnumsurfacespointer = outnumsurfaces;
1737 }
1738
1739 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)
1740 {
1741         model_t *model = ent->model;
1742         msurface_t *surface;
1743         int surfacelistindex;
1744         if (r_drawcollisionbrushes.integer < 2)
1745         {
1746                 R_Mesh_Matrix(&ent->matrix);
1747                 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
1748                 if (!r_shadow_compilingrtlight)
1749                         R_UpdateAllTextureInfo(ent);
1750                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1751                 {
1752                         surface = model->brush.data_surfaces + surfacelist[surfacelistindex];
1753                         if ((surface->texture->currentmaterialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT | MATERIALFLAG_WALL)) != MATERIALFLAG_WALL)
1754                                 continue;
1755                         if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1756                                 continue;
1757                         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);
1758                 }
1759                 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);
1760         }
1761 }
1762
1763 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)
1764 {
1765         model_t *model = ent->model;
1766         msurface_t *surface;
1767         texture_t *t;
1768         int surfacelistindex;
1769         if (r_drawcollisionbrushes.integer < 2)
1770         {
1771                 R_Mesh_Matrix(&ent->matrix);
1772                 if (!r_shadow_compilingrtlight)
1773                         R_UpdateAllTextureInfo(ent);
1774                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1775                 {
1776                         surface = model->brush.data_surfaces + surfacelist[surfacelistindex];
1777                         if (surface->texture->basematerialflags & MATERIALFLAG_NODRAW || !surface->mesh.num_triangles)
1778                                 continue;
1779                         if (r_shadow_compilingrtlight)
1780                         {
1781                                 // if compiling an rtlight, capture the mesh
1782                                 t = surface->texture;
1783                                 if ((t->basematerialflags & (MATERIALFLAG_WALL | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
1784                                         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);
1785                         }
1786                         else if (ent != r_refdef.worldentity || r_worldsurfacevisible[surfacelist[surfacelistindex]])
1787                         {
1788                                 t = surface->texture->currentframe;
1789                                 // FIXME: transparent surfaces need to be lit later
1790                                 if ((t->currentmaterialflags & (MATERIALFLAG_WALL | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
1791                                 {
1792                                         if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1793                                                 qglDisable(GL_CULL_FACE);
1794                                         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);
1795                                         if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1796                                                 qglEnable(GL_CULL_FACE);
1797                                 }
1798                         }
1799                 }
1800         }
1801 }
1802
1803 #if 0
1804 static void gl_surf_start(void)
1805 {
1806 }
1807
1808 static void gl_surf_shutdown(void)
1809 {
1810 }
1811
1812 static void gl_surf_newmap(void)
1813 {
1814 }
1815 #endif
1816
1817 void GL_Surf_Init(void)
1818 {
1819
1820         Cvar_RegisterVariable(&r_ambient);
1821         Cvar_RegisterVariable(&r_drawportals);
1822         Cvar_RegisterVariable(&r_testvis);
1823         Cvar_RegisterVariable(&r_detailtextures);
1824         Cvar_RegisterVariable(&r_surfaceworldnode);
1825         Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
1826         Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
1827         Cvar_RegisterVariable(&r_q3bsp_renderskydepth);
1828         Cvar_RegisterVariable(&gl_lightmaps);
1829
1830         //R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
1831 }
1832