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