2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
20 // r_surf.c: surface-related refresh code
25 #define MAX_LIGHTMAP_SIZE 256
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"};
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];
50 Combine and scale multiple lightmaps into the 8.8 format in blocklights
53 static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surface)
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];
61 // update cached lighting info
62 surface->cached_dlight = 0;
64 smax = (surface->lightmapinfo->extents[0]>>4)+1;
65 tmax = (surface->lightmapinfo->extents[1]>>4)+1;
68 lightmap = surface->lightmapinfo->samples;
70 // set to full bright if no light data
72 if (!ent->model->brushq1.lightdata)
74 for (i = 0;i < size3;i++)
80 memset(bl, 0, size*3*sizeof(unsigned int));
82 // add all the lightmaps
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;
92 stain = surface->lightmapinfo->stainsamples;
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)
101 stride = (surface->lightmapinfo->lightmaptexturestride - smax) * 4;
102 for (i = 0;i < tmax;i++, out += stride)
104 for (j = 0;j < smax;j++)
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);
115 stride = (surface->lightmapinfo->lightmaptexturestride - smax) * 3;
116 for (i = 0;i < tmax;i++, out += stride)
118 for (j = 0;j < smax;j++)
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);
127 R_UpdateTexture(surface->lightmaptexture, templight);
130 void R_StainNode (mnode_t *node, model_t *model, const vec3_t origin, float radius, const float fcolor[8])
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;
138 maxdist = radius * radius;
139 invradius = 1.0f / radius;
144 ndist = PlaneDiff(origin, node->plane);
147 node = node->children[0];
152 node = node->children[1];
156 dist2 = ndist * ndist;
157 maxdist3 = maxdist - dist2;
159 if (node->plane->type < 3)
161 VectorCopy(origin, impact);
162 impact[node->plane->type] -= ndist;
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;
171 for (surface = model->brush.data_surfaces + node->firstsurface, endsurface = surface + node->numsurfaces;surface < endsurface;surface++)
173 if (surface->lightmapinfo->stainsamples)
175 smax = (surface->lightmapinfo->extents[0] >> 4) + 1;
176 tmax = (surface->lightmapinfo->extents[1] >> 4) + 1;
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];
181 s = bound(0, impacts, smax * 16) - impacts;
182 t = bound(0, impactt, tmax * 16) - impactt;
183 i = s * s + t * t + dist2;
187 // reduce calculations
188 for (s = 0, i = impacts; s < smax; s++, i -= 16)
189 sdtable[s] = i * i + dist2;
191 bl = surface->lightmapinfo->stainsamples;
196 for (t = 0;t < tmax;t++, i -= 16)
199 // make sure some part of it is visible on this line
202 maxdist2 = maxdist - td;
203 for (s = 0;s < smax;s++)
205 if (sdtable[s] < maxdist2)
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))
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]));
225 // force lightmap upload
227 surface->cached_dlight = true;
231 if (node->children[0]->plane)
233 if (node->children[1]->plane)
235 R_StainNode(node->children[0], model, origin, radius, fcolor);
236 node = node->children[1];
241 node = node->children[0];
245 else if (node->children[1]->plane)
247 node = node->children[1];
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)
256 entity_render_t *ent;
259 if (r_refdef.worldmodel == NULL || !r_refdef.worldmodel->brush.data_nodes || !r_refdef.worldmodel->brushq1.lightdata)
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);
270 R_StainNode(r_refdef.worldmodel->brush.data_nodes + r_refdef.worldmodel->brushq1.hulls[0].firstclipnode, r_refdef.worldmodel, origin, radius, fcolor);
272 // look for embedded bmodels
273 for (n = 0;n < cl_num_brushmodel_entities;n++)
275 ent = &cl_entities[cl_brushmodel_entities[n]].render;
277 if (model && model->name[0] == '*')
279 Mod_CheckLoaded(model);
280 if (model->brush.data_nodes)
282 Matrix4x4_Transform(&ent->inversematrix, origin, org);
283 R_StainNode(model->brush.data_nodes + model->brushq1.hulls[0].firstclipnode, model, org, radius, fcolor);
291 =============================================================
295 =============================================================
298 static void RSurf_DeformVertices(const entity_render_t *ent, const texture_t *texture, const msurface_t *surface, const vec3_t modelorg)
301 float center[3], forward[3], right[3], up[3], v[4][3];
302 matrix4x4_t matrix1, imatrix1;
303 if (texture->textureflags & Q3TEXTUREFLAG_AUTOSPRITE2)
305 // a single autosprite surface can contain multiple sprites...
306 VectorClear(forward);
308 VectorSet(up, 0, 0, 1);
309 for (j = 0;j < surface->num_vertices - 3;j += 4)
312 for (i = 0;i < 4;i++)
313 VectorAdd(center, (surface->groupmesh->data_vertex3f + 3 * surface->num_firstvertex) + (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->groupmesh->data_normal3f + 3 * surface->num_firstvertex) + j*3, (surface->groupmesh->data_svector3f + 3 * surface->num_firstvertex) + j*3, (surface->groupmesh->data_tvector3f + 3 * surface->num_firstvertex) + j*3, center);
317 Matrix4x4_Invert_Simple(&imatrix1, &matrix1);
318 for (i = 0;i < 4;i++)
319 Matrix4x4_Transform(&imatrix1, (surface->groupmesh->data_vertex3f + 3 * surface->num_firstvertex) + (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 + (surface->num_firstvertex+i+j) * 3);
329 else if (texture->textureflags & Q3TEXTUREFLAG_AUTOSPRITE)
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->num_vertices - 3;j += 4)
338 for (i = 0;i < 4;i++)
339 VectorAdd(center, (surface->groupmesh->data_vertex3f + 3 * surface->num_firstvertex) + (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->groupmesh->data_normal3f + 3 * surface->num_firstvertex) + j*3, (surface->groupmesh->data_svector3f + 3 * surface->num_firstvertex) + j*3, (surface->groupmesh->data_tvector3f + 3 * surface->num_firstvertex) + j*3, center);
343 Matrix4x4_Invert_Simple(&imatrix1, &matrix1);
344 for (i = 0;i < 4;i++)
345 Matrix4x4_Transform(&imatrix1, (surface->groupmesh->data_vertex3f + 3 * surface->num_firstvertex) + (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 + (surface->num_firstvertex+i+j) * 3);
351 memcpy((varray_vertex3f + 3 * surface->num_firstvertex), (surface->groupmesh->data_vertex3f + 3 * surface->num_firstvertex), sizeof(float[3]) * surface->num_vertices);
354 // any sort of deformvertices call is *VERY* rare, so this must be optimized
355 // to skip deformvertices quickly!
357 #define RSurf_GetVertexPointer(ent, texture, surface, modelorg) ((texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2)) ? (RSurf_DeformVertices(ent, texture, surface, modelorg), varray_vertex3f) : surface->groupmesh->data_vertex3f)
359 static float *RSurf_GetVertexPointer(const entity_render_t *ent, const texture_t *texture, const msurface_t *surface, const vec3_t modelorg)
361 if (texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
363 RSurf_DeformVertices(ent, texture, surface, modelorg);
364 return varray_vertex3f;
367 return surface->groupmesh->data_vertex3f;
371 void R_UpdateTextureInfo(const entity_render_t *ent, texture_t *t)
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
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];
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;
392 matrix4x4_t r_surf_waterscrollmatrix;
394 void R_UpdateAllTextureInfo(entity_render_t *ent)
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);
399 for (i = 0;i < ent->model->brush.num_textures;i++)
400 R_UpdateTextureInfo(ent, ent->model->brush.data_textures + i);
403 static void R_DrawSurfaceList(const entity_render_t *ent, texture_t *texture, int texturenumsurfaces, const msurface_t **texturesurfacelist, const vec3_t modelorg)
406 int texturesurfaceindex;
407 const float *v, *vertex3f;
410 float f, r, g, b, a, base, colorscale;
411 const msurface_t *surface;
418 qboolean fogallpasses;
419 qboolean waterscrolling;
420 surfmesh_t *groupmesh;
421 rtexture_t *lightmaptexture;
423 texture = texture->currentframe;
424 if (texture->currentmaterialflags & MATERIALFLAG_NODRAW)
426 c_faces += texturenumsurfaces;
427 // gl_lightmaps debugging mode skips normal texturing
428 if (gl_lightmaps.integer)
430 GL_BlendFunc(GL_ONE, GL_ZERO);
433 qglDisable(GL_CULL_FACE);
434 GL_Color(1, 1, 1, 1);
435 memset(&m, 0, sizeof(m));
437 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
439 surface = texturesurfacelist[texturesurfaceindex];
440 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
441 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordlightmap2f);
442 R_Mesh_ColorPointer(surface->lightmaptexture ? NULL : surface->groupmesh->data_lightmapcolor4f);
443 R_Mesh_VertexPointer(surface->groupmesh->data_vertex3f);
444 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
445 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
448 qglEnable(GL_CULL_FACE);
451 GL_DepthTest(!(texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
452 GL_DepthMask(!(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT));
453 if (texture->currentmaterialflags & MATERIALFLAG_ADD)
454 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
455 else if (texture->currentmaterialflags & MATERIALFLAG_ALPHA)
456 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
458 GL_BlendFunc(GL_ONE, GL_ZERO);
459 // water waterscrolling in texture matrix
460 waterscrolling = (texture->currentmaterialflags & MATERIALFLAG_WATER) && r_waterscroll.value != 0;
461 if (texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
462 qglDisable(GL_CULL_FACE);
463 if (texture->currentmaterialflags & MATERIALFLAG_SKY)
467 skyrendernow = false;
471 // LordHavoc: HalfLife maps have freaky skypolys...
472 if (!ent->model->brush.ishlbsp)
474 R_Mesh_Matrix(&ent->matrix);
475 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
478 // depth-only (masking)
479 GL_ColorMask(0,0,0,0);
480 // just to make sure that braindead drivers don't draw anything
481 // despite that colormask...
482 GL_BlendFunc(GL_ZERO, GL_ONE);
487 GL_BlendFunc(GL_ONE, GL_ZERO);
491 memset(&m, 0, sizeof(m));
493 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
495 surface = texturesurfacelist[texturesurfaceindex];
496 R_Mesh_VertexPointer(surface->groupmesh->data_vertex3f);
497 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
498 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
501 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
504 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)
506 // NVIDIA Geforce3 distortion texture shader on water
507 float args[4] = {0.05f,0,0,0.04f};
508 memset(&m, 0, sizeof(m));
509 m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(r_refdef.time * 16)&63]);
510 m.tex[1] = R_GetTexture(texture->skin.base);
511 m.texcombinergb[0] = GL_REPLACE;
512 m.texcombinergb[1] = GL_REPLACE;
513 Matrix4x4_CreateFromQuakeEntity(&m.texmatrix[0], 0, 0, 0, 0, 0, 0, r_watershader.value);
514 m.texmatrix[1] = r_surf_waterscrollmatrix;
517 GL_Color(1, 1, 1, texture->currentalpha);
519 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
521 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
522 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
523 qglTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
524 qglEnable(GL_TEXTURE_SHADER_NV);
526 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
528 surface = texturesurfacelist[texturesurfaceindex];
529 R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, texture, surface, modelorg));
530 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
531 R_Mesh_TexCoordPointer(1, 2, surface->groupmesh->data_texcoordtexture2f);
532 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
533 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
537 qglDisable(GL_TEXTURE_SHADER_NV);
538 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
541 else if (texture->currentmaterialflags & (MATERIALFLAG_WATER | MATERIALFLAG_WALL))
543 // normal surface (wall or water)
545 dolightmap = !(texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT);
546 doambient = r_ambient.value >= (1/64.0f);
547 dodetail = r_detailtextures.integer && texture->skin.detail != NULL && !(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT);
548 doglow = texture->skin.glow != NULL;
549 dofogpass = fogenabled && !(texture->currentmaterialflags & MATERIALFLAG_ADD);
550 fogallpasses = fogenabled && !(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT);
551 if (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT)
553 if (dobase && dolightmap && gl_combine.integer)
556 memset(&m, 0, sizeof(m));
557 m.tex[1] = R_GetTexture(texture->skin.base);
559 m.texmatrix[1] = r_surf_waterscrollmatrix;
560 m.texrgbscale[1] = 2;
561 m.pointer_color = varray_color4f;
564 r = ent->colormod[0] * colorscale;
565 g = ent->colormod[1] * colorscale;
566 b = ent->colormod[2] * colorscale;
567 a = texture->currentalpha;
568 base = r_ambient.value * (1.0f / 64.0f);
569 // q3bsp has no lightmap updates, so the lightstylevalue that
570 // would normally be baked into the lightmaptexture must be
571 // applied to the color
572 if (ent->model->brushq1.lightdata)
574 float scale = d_lightstylevalue[0] * (1.0f / 128.0f);
579 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
581 surface = texturesurfacelist[texturesurfaceindex];
582 vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
583 R_Mesh_VertexPointer(vertex3f);
584 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordlightmap2f);
585 R_Mesh_TexCoordPointer(1, 2, surface->groupmesh->data_texcoordtexture2f);
586 if (surface->lightmaptexture)
588 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
591 R_Mesh_ColorPointer(varray_color4f);
592 for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
594 VectorSubtract(v, modelorg, diff);
595 f = 1 - exp(fogdensity/DotProduct(diff, diff));
604 R_Mesh_ColorPointer(NULL);
605 GL_Color(r, g, b, a);
610 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
611 R_Mesh_ColorPointer(varray_color4f);
612 if (!surface->lightmaptexture)
614 for (i = 0, c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, c += 4)
616 c[0] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+0] * r;
617 c[1] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+1] * g;
618 c[2] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+2] * b;
619 c[3] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+3] * a;
623 for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
625 VectorSubtract(v, modelorg, diff);
626 f = 1 - exp(fogdensity/DotProduct(diff, diff));
627 VectorScale(c, f, c);
633 R_Mesh_ColorPointer(NULL);
634 GL_Color(0, 0, 0, a);
637 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
638 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
645 memset(&m, 0, sizeof(m));
646 m.tex[0] = R_GetTexture(texture->skin.base);
648 m.texmatrix[0] = r_surf_waterscrollmatrix;
649 m.pointer_color = varray_color4f;
651 if (gl_combine.integer)
653 m.texrgbscale[0] = 4;
657 r = ent->colormod[0] * colorscale;
658 g = ent->colormod[1] * colorscale;
659 b = ent->colormod[2] * colorscale;
660 a = texture->currentalpha;
663 // q3bsp has no lightmap updates, so the lightstylevalue that
664 // would normally be baked into the lightmaptexture must be
665 // applied to the color
666 if (!ent->model->brushq1.lightdata)
668 float scale = d_lightstylevalue[0] * (1.0f / 128.0f);
673 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
675 surface = texturesurfacelist[texturesurfaceindex];
676 vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
677 R_Mesh_VertexPointer(vertex3f);
678 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
679 for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
684 if (!surface->lightmapinfo)
685 VectorCopy((surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex) + i*4, c);
686 else //if (surface->lightmapinfo)
688 const qbyte *lm = surface->lightmapinfo->samples + (surface->groupmesh->data_lightmapoffsets + surface->num_firstvertex)[i];
689 float scale = d_lightstylevalue[surface->lightmapinfo->styles[0]] * (1.0f / 32768.0f);
690 VectorMA(c, scale, lm, c);
691 if (surface->lightmapinfo->styles[1] != 255)
693 int size3 = ((surface->lightmapinfo->extents[0]>>4)+1)*((surface->lightmapinfo->extents[1]>>4)+1)*3;
695 scale = d_lightstylevalue[surface->lightmapinfo->styles[1]] * (1.0f / 32768.0f);
696 VectorMA(c, scale, lm, c);
697 if (surface->lightmapinfo->styles[2] != 255)
700 scale = d_lightstylevalue[surface->lightmapinfo->styles[2]] * (1.0f / 32768.0f);
701 VectorMA(c, scale, lm, c);
702 if (surface->lightmapinfo->styles[3] != 255)
705 scale = d_lightstylevalue[surface->lightmapinfo->styles[3]] * (1.0f / 32768.0f);
706 VectorMA(c, scale, lm, c);
716 VectorSubtract(v, modelorg, diff);
717 f = 1 - exp(fogdensity/DotProduct(diff, diff));
718 VectorScale(c, f, c);
720 if (!surface->lightmapinfo && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
721 c[3] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+3] * a;
725 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
726 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
734 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
736 surface = texturesurfacelist[texturesurfaceindex];
737 vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
738 R_Mesh_VertexPointer(vertex3f);
739 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
740 if (!surface->lightmapinfo && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
742 R_Mesh_ColorPointer(varray_color4f);
743 for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
745 VectorSubtract(v, modelorg, diff);
746 f = 1 - exp(fogdensity/DotProduct(diff, diff));
750 c[3] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+3] * a;
755 R_Mesh_ColorPointer(varray_color4f);
756 for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
758 VectorSubtract(v, modelorg, diff);
759 f = 1 - exp(fogdensity/DotProduct(diff, diff));
766 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
767 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
773 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
775 surface = texturesurfacelist[texturesurfaceindex];
776 vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
777 R_Mesh_VertexPointer(vertex3f);
778 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
779 if (!surface->lightmaptexture && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
781 R_Mesh_ColorPointer(varray_color4f);
782 for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
787 c[3] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+3] * a;
792 R_Mesh_ColorPointer(NULL);
793 GL_Color(r, g, b, a);
795 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
796 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
805 if (!dolightmap && dobase)
809 GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], 1);
810 memset(&m, 0, sizeof(m));
811 m.tex[0] = R_GetTexture(texture->skin.base);
813 m.texmatrix[0] = r_surf_waterscrollmatrix;
815 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
817 surface = texturesurfacelist[texturesurfaceindex];
818 R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, texture, surface, modelorg));
819 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
820 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
821 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
825 if (r_lightmapintensity <= 0 && dolightmap && dobase)
829 GL_Color(0, 0, 0, 1);
830 memset(&m, 0, sizeof(m));
832 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
834 surface = texturesurfacelist[texturesurfaceindex];
835 R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, texture, surface, modelorg));
836 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
837 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
841 if (r_textureunits.integer >= 2 && gl_combine.integer && dolightmap && dobase)
843 // dualtexture combine
844 GL_BlendFunc(GL_ONE, GL_ZERO);
848 memset(&m, 0, sizeof(m));
849 m.tex[1] = R_GetTexture(texture->skin.base);
851 m.texmatrix[1] = r_surf_waterscrollmatrix;
852 m.texrgbscale[1] = 2;
854 r = ent->colormod[0] * r_lightmapintensity;
855 g = ent->colormod[1] * r_lightmapintensity;
856 b = ent->colormod[2] * r_lightmapintensity;
857 GL_Color(r, g, b, 1);
858 if (texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
860 R_Mesh_VertexPointer(varray_vertex3f);
861 if (r == 1 && g == 1 && b == 1)
863 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
865 surface = texturesurfacelist[texturesurfaceindex];
866 RSurf_DeformVertices(ent, texture, surface, modelorg);
867 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordlightmap2f);
868 R_Mesh_TexCoordPointer(1, 2, surface->groupmesh->data_texcoordtexture2f);
869 if (surface->lightmaptexture)
871 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
872 R_Mesh_ColorPointer(NULL);
874 else //if (r == 1 && g == 1 && b == 1)
876 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
877 R_Mesh_ColorPointer(surface->groupmesh->data_lightmapcolor4f);
879 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
880 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
886 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
888 surface = texturesurfacelist[texturesurfaceindex];
889 RSurf_DeformVertices(ent, texture, surface, modelorg);
890 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordlightmap2f);
891 R_Mesh_TexCoordPointer(1, 2, surface->groupmesh->data_texcoordtexture2f);
892 if (surface->lightmaptexture)
894 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
895 R_Mesh_ColorPointer(NULL);
899 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
900 R_Mesh_ColorPointer(varray_color4f);
901 for (i = 0, c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, c += 4)
903 c[0] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+0] * r;
904 c[1] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+1] * g;
905 c[2] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+2] * b;
906 c[3] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+3];
909 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
910 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
917 if (r == 1 && g == 1 && b == 1)
920 // experimental direct state calls for measuring
921 // R_Mesh_ call overhead, do not use!
922 R_Mesh_VertexPointer(varray_vertex3f);
923 R_Mesh_TexCoordPointer(0, 2, varray_texcoord2f[0]);
924 R_Mesh_TexCoordPointer(1, 2, varray_texcoord2f[1]);
925 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
926 R_Mesh_ColorPointer(varray_color4f);
927 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
929 surface = texturesurfacelist[texturesurfaceindex];
930 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), surface->groupmesh->data_vertex3f);
931 qglClientActiveTexture(GL_TEXTURE0_ARB);
932 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), surface->groupmesh->data_texcoordlightmap2f);
933 qglClientActiveTexture(GL_TEXTURE1_ARB);
934 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), surface->groupmesh->data_texcoordtexture2f);
935 if (surface->lightmaptexture)
937 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
938 qglDisableClientState(GL_COLOR_ARRAY);
939 qglColor4f(r, g, b, 1);
941 else //if (r == 1 && g == 1 && b == 1)
943 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
944 qglEnableClientState(GL_COLOR_ARRAY);
945 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), surface->groupmesh->data_lightmapcolor4f);
947 qglLockArraysEXT(0, surface->num_vertices);
948 qglDrawRangeElements(GL_TRIANGLES, surface->num_firstvertex, surface->num_firstvertex + surface->num_vertices, surface->num_triangles * 3, GL_UNSIGNED_INT, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
949 qglUnlockArraysEXT();
953 lightmaptexture = NULL;
954 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
956 surface = texturesurfacelist[texturesurfaceindex];
957 if (groupmesh != surface->groupmesh)
959 groupmesh = surface->groupmesh;
960 R_Mesh_VertexPointer(groupmesh->data_vertex3f);
961 R_Mesh_TexCoordPointer(0, 2, groupmesh->data_texcoordlightmap2f);
962 R_Mesh_TexCoordPointer(1, 2, groupmesh->data_texcoordtexture2f);
963 if (!lightmaptexture)
964 R_Mesh_ColorPointer(groupmesh->data_lightmapcolor4f);
966 if (lightmaptexture != surface->lightmaptexture)
968 lightmaptexture = surface->lightmaptexture;
971 R_Mesh_TexBind(0, R_GetTexture(lightmaptexture));
972 R_Mesh_ColorPointer(NULL);
974 else //if (r == 1 && g == 1 && b == 1)
976 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
977 R_Mesh_ColorPointer(surface->groupmesh->data_lightmapcolor4f);
980 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
981 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
988 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
990 surface = texturesurfacelist[texturesurfaceindex];
991 R_Mesh_VertexPointer(surface->groupmesh->data_vertex3f);
992 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordlightmap2f);
993 R_Mesh_TexCoordPointer(1, 2, surface->groupmesh->data_texcoordtexture2f);
994 if (surface->lightmaptexture)
996 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
997 R_Mesh_ColorPointer(NULL);
1001 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
1002 R_Mesh_ColorPointer(varray_color4f);
1003 for (i = 0, c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, c += 4)
1005 c[0] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+0] * r;
1006 c[1] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+1] * g;
1007 c[2] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+2] * b;
1008 c[3] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+3];
1011 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1012 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1013 GL_LockArrays(0, 0);
1021 GL_BlendFunc(GL_ONE, GL_ZERO);
1023 GL_Color(1, 1, 1, 1);
1024 memset(&m, 0, sizeof(m));
1026 if (texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
1028 R_Mesh_VertexPointer(varray_vertex3f);
1029 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1031 surface = texturesurfacelist[texturesurfaceindex];
1032 RSurf_DeformVertices(ent, texture, surface, modelorg);
1033 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordlightmap2f);
1034 if (surface->lightmaptexture)
1036 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
1037 R_Mesh_ColorPointer(NULL);
1039 else //if (r == 1 && g == 1 && b == 1)
1041 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
1042 R_Mesh_ColorPointer(surface->groupmesh->data_lightmapcolor4f);
1044 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1045 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1046 GL_LockArrays(0, 0);
1052 lightmaptexture = NULL;
1053 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1055 surface = texturesurfacelist[texturesurfaceindex];
1056 if (groupmesh != surface->groupmesh)
1058 groupmesh = surface->groupmesh;
1059 R_Mesh_VertexPointer(groupmesh->data_vertex3f);
1060 R_Mesh_TexCoordPointer(0, 2, groupmesh->data_texcoordlightmap2f);
1061 if (!lightmaptexture)
1062 R_Mesh_ColorPointer(groupmesh->data_lightmapcolor4f);
1064 if (lightmaptexture != surface->lightmaptexture)
1066 lightmaptexture = surface->lightmaptexture;
1067 if (lightmaptexture)
1069 R_Mesh_TexBind(0, R_GetTexture(lightmaptexture));
1070 R_Mesh_ColorPointer(NULL);
1072 else //if (r == 1 && g == 1 && b == 1)
1074 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
1075 R_Mesh_ColorPointer(surface->groupmesh->data_lightmapcolor4f);
1078 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1079 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1080 GL_LockArrays(0, 0);
1086 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1087 GL_DepthMask(false);
1088 GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
1089 memset(&m, 0, sizeof(m));
1090 m.tex[0] = R_GetTexture(texture->skin.base);
1092 m.texmatrix[0] = r_surf_waterscrollmatrix;
1094 if (texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
1096 R_Mesh_VertexPointer(varray_vertex3f);
1097 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1099 surface = texturesurfacelist[texturesurfaceindex];
1100 RSurf_DeformVertices(ent, texture, surface, modelorg);
1101 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
1102 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1103 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1104 GL_LockArrays(0, 0);
1110 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1112 surface = texturesurfacelist[texturesurfaceindex];
1113 if (groupmesh != surface->groupmesh)
1115 groupmesh = surface->groupmesh;
1116 R_Mesh_VertexPointer(groupmesh->data_vertex3f);
1117 R_Mesh_TexCoordPointer(0, 2, groupmesh->data_texcoordtexture2f);
1119 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1120 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1121 GL_LockArrays(0, 0);
1129 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1130 GL_DepthMask(false);
1131 memset(&m, 0, sizeof(m));
1132 m.tex[0] = R_GetTexture(texture->skin.base);
1134 m.texmatrix[0] = r_surf_waterscrollmatrix;
1135 m.pointer_color = varray_color4f;
1137 if (gl_combine.integer)
1139 m.texrgbscale[0] = 4;
1140 colorscale *= 0.25f;
1143 base = r_ambient.value * (1.0f / 64.0f);
1144 r = ent->colormod[0] * colorscale * base;
1145 g = ent->colormod[1] * colorscale * base;
1146 b = ent->colormod[2] * colorscale * base;
1147 a = texture->currentalpha;
1148 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1150 surface = texturesurfacelist[texturesurfaceindex];
1151 vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
1152 R_Mesh_VertexPointer(vertex3f);
1153 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
1154 for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
1161 VectorSubtract(v, modelorg, diff);
1162 f = 1 - exp(fogdensity/DotProduct(diff, diff));
1163 VectorScale(c, f, c);
1165 if (!surface->lightmaptexture && surface->groupmesh->data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1166 c[3] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+3] * a;
1170 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1171 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1172 GL_LockArrays(0, 0);
1177 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1178 GL_DepthMask(false);
1179 GL_Color(1, 1, 1, 1);
1180 memset(&m, 0, sizeof(m));
1181 m.tex[0] = R_GetTexture(texture->skin.detail);
1183 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1185 surface = texturesurfacelist[texturesurfaceindex];
1186 R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, texture, surface, modelorg));
1187 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoorddetail2f);
1188 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1189 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1190 GL_LockArrays(0, 0);
1195 // if glow was not already done using multitexture, do it now.
1196 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1197 GL_DepthMask(false);
1198 memset(&m, 0, sizeof(m));
1199 m.tex[0] = R_GetTexture(texture->skin.glow);
1201 m.texmatrix[0] = r_surf_waterscrollmatrix;
1202 m.pointer_color = varray_color4f;
1205 r = ent->colormod[0] * colorscale;
1206 g = ent->colormod[1] * colorscale;
1207 b = ent->colormod[2] * colorscale;
1208 a = texture->currentalpha;
1211 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1213 surface = texturesurfacelist[texturesurfaceindex];
1214 vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
1215 R_Mesh_VertexPointer(vertex3f);
1216 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
1217 R_Mesh_ColorPointer(varray_color4f);
1218 if (!surface->lightmaptexture && surface->groupmesh->data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1220 for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
1222 VectorSubtract(v, modelorg, diff);
1223 f = 1 - exp(fogdensity/DotProduct(diff, diff));
1227 c[3] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+3] * a;
1232 for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
1234 VectorSubtract(v, modelorg, diff);
1235 f = 1 - exp(fogdensity/DotProduct(diff, diff));
1242 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1243 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1244 GL_LockArrays(0, 0);
1249 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1251 surface = texturesurfacelist[texturesurfaceindex];
1252 vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
1253 R_Mesh_VertexPointer(vertex3f);
1254 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
1255 if (!surface->lightmaptexture && surface->groupmesh->data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1257 R_Mesh_ColorPointer(varray_color4f);
1258 for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
1263 c[3] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+3] * a;
1268 R_Mesh_ColorPointer(NULL);
1269 GL_Color(r, g, b, a);
1271 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1272 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1273 GL_LockArrays(0, 0);
1279 // if this is opaque use alpha blend which will darken the earlier
1282 // if this is an alpha blended material, all the earlier passes
1283 // were darkened by fog already, so we only need to add the fog
1284 // color ontop through the fog mask texture
1286 // if this is an additive blended material, all the earlier passes
1287 // were darkened by fog already, and we should not add fog color
1288 // (because the background was not darkened, there is no fog color
1289 // that was lost behind it).
1291 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1293 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1294 GL_DepthMask(false);
1295 memset(&m, 0, sizeof(m));
1296 m.tex[0] = R_GetTexture(texture->skin.fog);
1298 m.texmatrix[0] = r_surf_waterscrollmatrix;
1303 a = texture->currentalpha;
1304 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1306 surface = texturesurfacelist[texturesurfaceindex];
1307 vertex3f = RSurf_GetVertexPointer(ent, texture, surface, modelorg);
1308 R_Mesh_VertexPointer(vertex3f);
1309 R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
1310 R_Mesh_ColorPointer(varray_color4f);
1311 //RSurf_FogPassColors_Vertex3f_Color4f((surface->groupmesh->data_vertex3f + 3 * surface->num_firstvertex), varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], texture->currentalpha, 1, surface->num_vertices, modelorg);
1312 if (!surface->lightmaptexture && surface->groupmesh->data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1314 for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
1316 VectorSubtract(v, modelorg, diff);
1317 f = exp(fogdensity/DotProduct(diff, diff));
1321 c[3] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+3] * f * a;
1326 for (i = 0, v = (vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
1328 VectorSubtract(v, modelorg, diff);
1329 f = exp(fogdensity/DotProduct(diff, diff));
1336 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
1337 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1338 GL_LockArrays(0, 0);
1342 if (texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1343 qglEnable(GL_CULL_FACE);
1346 static void RSurfShader_Transparent_Callback(const void *calldata1, int calldata2)
1348 const entity_render_t *ent = calldata1;
1349 const msurface_t *surface = ent->model->brush.data_surfaces + calldata2;
1353 texture = surface->texture;
1354 if (texture->basematerialflags & MATERIALFLAG_SKY)
1355 return; // transparent sky is too difficult
1356 R_UpdateTextureInfo(ent, texture);
1358 R_Mesh_Matrix(&ent->matrix);
1359 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1360 R_DrawSurfaceList(ent, texture, 1, &surface, modelorg);
1363 void R_QueueSurfaceList(entity_render_t *ent, texture_t *texture, int texturenumsurfaces, const msurface_t **texturesurfacelist, const vec3_t modelorg)
1365 int texturesurfaceindex;
1366 const msurface_t *surface;
1367 vec3_t tempcenter, center;
1368 if (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT)
1370 // drawing sky transparently would be too difficult
1371 if (!(texture->currentmaterialflags & MATERIALFLAG_SKY))
1373 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1375 surface = texturesurfacelist[texturesurfaceindex];
1376 tempcenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
1377 tempcenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
1378 tempcenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
1379 Matrix4x4_Transform(&ent->matrix, tempcenter, center);
1380 R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_vieworigin : center, RSurfShader_Transparent_Callback, ent, surface - ent->model->brush.data_surfaces);
1385 R_DrawSurfaceList(ent, texture, texturenumsurfaces, texturesurfacelist, modelorg);
1388 void R_DrawSurfaces(entity_render_t *ent, qboolean skysurfaces)
1390 int i, j, f, flagsmask;
1391 msurface_t *surface, **surfacechain;
1392 texture_t *t, *texture;
1393 model_t *model = ent->model;
1395 const int maxsurfacelist = 1024;
1396 int numsurfacelist = 0;
1397 const msurface_t *surfacelist[1024];
1400 R_Mesh_Matrix(&ent->matrix);
1401 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1403 // update light styles
1404 if (!skysurfaces && model->brushq1.light_styleupdatechains)
1406 for (i = 0;i < model->brushq1.light_styles;i++)
1408 if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1410 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1411 if ((surfacechain = model->brushq1.light_styleupdatechains[i]))
1412 for (;(surface = *surfacechain);surfacechain++)
1413 surface->cached_dlight = true;
1418 R_UpdateAllTextureInfo(ent);
1419 flagsmask = skysurfaces ? MATERIALFLAG_SKY : (MATERIALFLAG_WATER | MATERIALFLAG_WALL);
1424 if (ent == r_refdef.worldentity)
1426 for (i = 0, j = model->firstmodelsurface, surface = model->brush.data_surfaces + j;i < model->nummodelsurfaces;i++, j++, surface++)
1428 if (!r_worldsurfacevisible[j])
1430 if (t != surface->texture)
1434 R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1437 t = surface->texture;
1438 f = t->currentmaterialflags & flagsmask;
1439 texture = t->currentframe;
1441 if (f && surface->num_triangles)
1443 // if lightmap parameters changed, rebuild lightmap texture
1444 if (surface->cached_dlight && surface->lightmapinfo->samples)
1445 R_BuildLightMap(ent, surface);
1446 // add face to draw list
1447 surfacelist[numsurfacelist++] = surface;
1448 if (numsurfacelist >= maxsurfacelist)
1450 R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1458 for (i = 0, j = model->firstmodelsurface, surface = model->brush.data_surfaces + j;i < model->nummodelsurfaces;i++, j++, surface++)
1460 if (t != surface->texture)
1464 R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1467 t = surface->texture;
1468 f = t->currentmaterialflags & flagsmask;
1469 texture = t->currentframe;
1471 if (f && surface->num_triangles)
1473 // if lightmap parameters changed, rebuild lightmap texture
1474 if (surface->cached_dlight && surface->lightmapinfo->samples)
1475 R_BuildLightMap(ent, surface);
1476 // add face to draw list
1477 surfacelist[numsurfacelist++] = surface;
1478 if (numsurfacelist >= maxsurfacelist)
1480 R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1487 R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1490 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1495 const mportal_t *portal = calldata1;
1496 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1497 GL_DepthMask(false);
1499 R_Mesh_Matrix(&r_identitymatrix);
1501 memset(&m, 0, sizeof(m));
1502 m.pointer_vertex = varray_vertex3f;
1506 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1507 ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1508 ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1510 if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1512 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1513 VectorCopy(portal->points[i].position, v);
1516 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1517 VectorCopy(portal->points[i].position, v);
1518 GL_LockArrays(0, portal->numpoints);
1519 R_Mesh_Draw(0, portal->numpoints, portal->numpoints - 2, polygonelements);
1520 GL_LockArrays(0, 0);
1523 // LordHavoc: this is just a nice debugging tool, very slow
1524 static void R_DrawPortals(void)
1526 int i, leafnum;//, portalnum;
1529 model_t *model = r_refdef.worldmodel;
1532 for (leafnum = 0;leafnum < r_refdef.worldmodel->brush.num_leafs;leafnum++)
1534 if (r_worldleafvisible[leafnum])
1536 //for (portalnum = 0, portal = model->brush.data_portals;portalnum < model->brush.num_portals;portalnum++, portal++)
1537 for (portal = r_refdef.worldmodel->brush.data_leafs[leafnum].portals;portal;portal = portal->next)
1539 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1540 if (!R_CullBox(portal->mins, portal->maxs))
1542 VectorClear(center);
1543 for (i = 0;i < portal->numpoints;i++)
1544 VectorAdd(center, portal->points[i].position, center);
1545 f = ixtable[portal->numpoints];
1546 VectorScale(center, f, center);
1547 //R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, portal, portalnum);
1548 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, portal, leafnum);
1555 static void R_DrawCollisionBrush(colbrushf_t *brush)
1559 memset(&m, 0, sizeof(m));
1560 m.pointer_vertex = brush->points->v;
1562 i = (int)(((size_t)brush) / sizeof(colbrushf_t));
1563 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1564 GL_LockArrays(0, brush->numpoints);
1565 R_Mesh_Draw(0, brush->numpoints, brush->numtriangles, brush->elements);
1566 GL_LockArrays(0, 0);
1569 static void R_DrawCollisionSurface(entity_render_t *ent, msurface_t *surface)
1573 if (!surface->num_collisiontriangles)
1575 memset(&m, 0, sizeof(m));
1576 m.pointer_vertex = surface->data_collisionvertex3f;
1578 i = (int)(((size_t)surface) / sizeof(msurface_t));
1579 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1580 GL_LockArrays(0, surface->num_collisionvertices);
1581 R_Mesh_Draw(0, surface->num_collisionvertices, surface->num_collisiontriangles, surface->data_collisionelement3i);
1582 GL_LockArrays(0, 0);
1585 void R_WorldVisibility(void)
1590 model_t *model = r_refdef.worldmodel;
1595 // if possible find the leaf the view origin is in
1596 viewleaf = model->brushq1.PointInLeaf ? model->brushq1.PointInLeaf(model, r_vieworigin) : NULL;
1597 // if possible fetch the visible cluster bits
1598 if (model->brush.FatPVS)
1599 model->brush.FatPVS(model, r_vieworigin, 2, r_pvsbits, sizeof(r_pvsbits));
1601 // clear the visible surface and leaf flags arrays
1602 memset(r_worldsurfacevisible, 0, model->brush.num_surfaces);
1603 memset(r_worldleafvisible, 0, model->brush.num_leafs);
1605 // if the user prefers surfaceworldnode (testing?) or the viewleaf could
1606 // not be found, or the viewleaf is not part of the visible world
1607 // (floating around in the void), use the pvs method
1608 if (r_surfaceworldnode.integer || !viewleaf || viewleaf->clusterindex < 0)
1611 // similar to quake's RecursiveWorldNode but without cache misses
1612 for (j = 0, leaf = model->brush.data_leafs;j < model->brush.num_leafs;j++, leaf++)
1614 // if leaf is in current pvs and on the screen, mark its surfaces
1615 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
1618 r_worldleafvisible[j] = true;
1619 if (leaf->numleafsurfaces)
1620 for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
1621 r_worldsurfacevisible[*mark] = true;
1629 mleaf_t *leafstack[8192];
1631 // follows portals leading outward from viewleaf, does not venture
1632 // offscreen or into leafs that are not visible, faster than Quake's
1633 // RecursiveWorldNode and vastly better in unvised maps, often culls a
1634 // lot of surface that pvs alone would miss
1635 leafstack[0] = viewleaf;
1637 while (leafstackpos)
1640 leaf = leafstack[--leafstackpos];
1641 r_worldleafvisible[leaf - model->brush.data_leafs] = true;
1642 // mark any surfaces bounding this leaf
1643 if (leaf->numleafsurfaces)
1644 for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
1645 r_worldsurfacevisible[*mark] = true;
1646 // follow portals into other leafs
1648 // if viewer is behind portal (portal faces outward into the scene)
1649 // and the portal polygon's bounding box is on the screen
1650 // and the leaf has not been visited yet
1651 // and the leaf is visible in the pvs
1652 // (the first two checks won't cause as many cache misses as the leaf checks)
1653 for (p = leaf->portals;p;p = p->next)
1654 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))
1655 leafstack[leafstackpos++] = p->past;
1659 if (r_drawportals.integer)
1663 void R_Q1BSP_DrawSky(entity_render_t *ent)
1665 if (ent->model == NULL)
1667 if (r_drawcollisionbrushes.integer < 2)
1668 R_DrawSurfaces(ent, true);
1671 void R_Q1BSP_Draw(entity_render_t *ent)
1673 if (ent->model == NULL)
1676 if (r_drawcollisionbrushes.integer < 2)
1677 R_DrawSurfaces(ent, false);
1678 if (r_drawcollisionbrushes.integer >= 1 && ent->model->brush.num_brushes)
1681 model_t *model = ent->model;
1682 msurface_t *surface;
1684 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1685 GL_DepthMask(false);
1687 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
1688 for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
1689 if (brush->colbrushf && brush->colbrushf->numtriangles)
1690 R_DrawCollisionBrush(brush->colbrushf);
1691 for (i = 0, surface = model->brush.data_surfaces + model->firstmodelsurface;i < model->nummodelsurfaces;i++, surface++)
1692 if (surface->num_collisiontriangles)
1693 R_DrawCollisionSurface(ent, surface);
1694 qglPolygonOffset(0, 0);
1698 typedef struct r_q1bsp_getlightinfo_s
1701 vec3_t relativelightorigin;
1703 int *outclusterlist;
1704 qbyte *outclusterpvs;
1706 int *outsurfacelist;
1707 qbyte *outsurfacepvs;
1715 r_q1bsp_getlightinfo_t;
1717 void R_Q1BSP_RecursiveGetLightInfo(r_q1bsp_getlightinfo_t *info, mnode_t *node)
1723 if (!BoxesOverlap(info->lightmins, info->lightmaxs, node->mins, node->maxs))
1727 sides = BoxOnPlaneSide(info->lightmins, info->lightmaxs, node->plane) - 1;
1730 R_Q1BSP_RecursiveGetLightInfo(info, node->children[0]);
1731 node = node->children[1];
1734 node = node->children[sides];
1736 leaf = (mleaf_t *)node;
1737 if (info->pvs == NULL || CHECKPVSBIT(info->pvs, leaf->clusterindex))
1739 info->outmins[0] = min(info->outmins[0], leaf->mins[0]);
1740 info->outmins[1] = min(info->outmins[1], leaf->mins[1]);
1741 info->outmins[2] = min(info->outmins[2], leaf->mins[2]);
1742 info->outmaxs[0] = max(info->outmaxs[0], leaf->maxs[0]);
1743 info->outmaxs[1] = max(info->outmaxs[1], leaf->maxs[1]);
1744 info->outmaxs[2] = max(info->outmaxs[2], leaf->maxs[2]);
1745 if (info->outclusterpvs)
1747 if (!CHECKPVSBIT(info->outclusterpvs, leaf->clusterindex))
1749 SETPVSBIT(info->outclusterpvs, leaf->clusterindex);
1750 info->outclusterlist[info->outnumclusters++] = leaf->clusterindex;
1753 if (info->outsurfacepvs)
1755 int leafsurfaceindex;
1756 for (leafsurfaceindex = 0;leafsurfaceindex < leaf->numleafsurfaces;leafsurfaceindex++)
1758 int surfaceindex = leaf->firstleafsurface[leafsurfaceindex];
1759 if (!CHECKPVSBIT(info->outsurfacepvs, surfaceindex))
1761 msurface_t *surface = info->model->brush.data_surfaces + surfaceindex;
1762 if (BoxesOverlap(info->lightmins, info->lightmaxs, surface->mins, surface->maxs))
1763 if ((surface->texture->currentmaterialflags & (MATERIALFLAG_WALL | MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
1765 int triangleindex, t;
1768 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = info->model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->num_triangles;triangleindex++, t++, e += 3)
1770 v[0] = info->model->brush.shadowmesh->vertex3f + e[0] * 3;
1771 v[1] = info->model->brush.shadowmesh->vertex3f + e[1] * 3;
1772 v[2] = info->model->brush.shadowmesh->vertex3f + e[2] * 3;
1773 if (info->lightmaxs[0] > min(v[0][0], min(v[1][0], v[2][0])) && info->lightmins[0] < max(v[0][0], max(v[1][0], v[2][0])) && info->lightmaxs[1] > min(v[0][1], min(v[1][1], v[2][1])) && info->lightmins[1] < max(v[0][1], max(v[1][1], v[2][1])) && info->lightmaxs[2] > min(v[0][2], min(v[1][2], v[2][2])) && info->lightmins[2] < max(v[0][2], max(v[1][2], v[2][2])))
1775 SETPVSBIT(info->outsurfacepvs, surfaceindex);
1776 info->outsurfacelist[info->outnumsurfaces++] = surfaceindex;
1787 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)
1789 r_q1bsp_getlightinfo_t info;
1790 VectorCopy(relativelightorigin, info.relativelightorigin);
1791 info.lightradius = lightradius;
1792 info.lightmins[0] = info.relativelightorigin[0] - info.lightradius;
1793 info.lightmins[1] = info.relativelightorigin[1] - info.lightradius;
1794 info.lightmins[2] = info.relativelightorigin[2] - info.lightradius;
1795 info.lightmaxs[0] = info.relativelightorigin[0] + info.lightradius;
1796 info.lightmaxs[1] = info.relativelightorigin[1] + info.lightradius;
1797 info.lightmaxs[2] = info.relativelightorigin[2] + info.lightradius;
1798 if (ent->model == NULL)
1800 VectorCopy(info.lightmins, outmins);
1801 VectorCopy(info.lightmaxs, outmaxs);
1802 *outnumclusterspointer = 0;
1803 *outnumsurfacespointer = 0;
1806 info.model = ent->model;
1807 info.outclusterlist = outclusterlist;
1808 info.outclusterpvs = outclusterpvs;
1809 info.outnumclusters = 0;
1810 info.outsurfacelist = outsurfacelist;
1811 info.outsurfacepvs = outsurfacepvs;
1812 info.outnumsurfaces = 0;
1813 VectorCopy(info.relativelightorigin, info.outmins);
1814 VectorCopy(info.relativelightorigin, info.outmaxs);
1815 memset(outclusterpvs, 0, info.model->brush.num_pvsclusterbytes);
1816 memset(outsurfacepvs, 0, (info.model->nummodelsurfaces + 7) >> 3);
1817 if (info.model->brush.GetPVS)
1818 info.pvs = info.model->brush.GetPVS(info.model, info.relativelightorigin);
1821 R_UpdateAllTextureInfo(ent);
1822 // use BSP recursion as lights are often small
1823 R_Q1BSP_RecursiveGetLightInfo(&info, info.model->brush.data_nodes);
1825 // limit combined leaf box to light boundaries
1826 outmins[0] = max(info.outmins[0], info.lightmins[0]);
1827 outmins[1] = max(info.outmins[1], info.lightmins[1]);
1828 outmins[2] = max(info.outmins[2], info.lightmins[2]);
1829 outmaxs[0] = min(info.outmaxs[0], info.lightmaxs[0]);
1830 outmaxs[1] = min(info.outmaxs[1], info.lightmaxs[1]);
1831 outmaxs[2] = min(info.outmaxs[2], info.lightmaxs[2]);
1833 *outnumclusterspointer = info.outnumclusters;
1834 *outnumsurfacespointer = info.outnumsurfaces;
1837 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)
1839 model_t *model = ent->model;
1840 msurface_t *surface;
1841 int surfacelistindex;
1842 if (r_drawcollisionbrushes.integer < 2)
1844 R_Mesh_Matrix(&ent->matrix);
1845 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
1846 if (!r_shadow_compilingrtlight)
1847 R_UpdateAllTextureInfo(ent);
1848 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1850 surface = model->brush.data_surfaces + surfacelist[surfacelistindex];
1851 if ((surface->texture->currentmaterialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT | MATERIALFLAG_WALL)) != MATERIALFLAG_WALL)
1853 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1855 R_Shadow_MarkVolumeFromBox(surface->num_firstshadowmeshtriangle, surface->num_triangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, relativelightorigin, lightmins, lightmaxs, surface->mins, surface->maxs);
1857 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);
1861 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)
1863 model_t *model = ent->model;
1864 msurface_t *surface;
1866 int surfacelistindex;
1867 if (r_drawcollisionbrushes.integer < 2)
1869 R_Mesh_Matrix(&ent->matrix);
1870 if (!r_shadow_compilingrtlight)
1871 R_UpdateAllTextureInfo(ent);
1872 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1874 surface = model->brush.data_surfaces + surfacelist[surfacelistindex];
1875 if (surface->texture->basematerialflags & MATERIALFLAG_NODRAW || !surface->num_triangles)
1877 if (r_shadow_compilingrtlight)
1879 // if compiling an rtlight, capture the mesh
1880 t = surface->texture;
1881 if ((t->basematerialflags & (MATERIALFLAG_WALL | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
1882 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->groupmesh->data_vertex3f, surface->groupmesh->data_svector3f, surface->groupmesh->data_tvector3f, surface->groupmesh->data_normal3f, surface->groupmesh->data_texcoordtexture2f, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
1884 else if (ent != r_refdef.worldentity || r_worldsurfacevisible[surfacelist[surfacelistindex]])
1886 t = surface->texture->currentframe;
1887 // FIXME: transparent surfaces need to be lit later
1888 if ((t->currentmaterialflags & (MATERIALFLAG_WALL | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
1890 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1891 qglDisable(GL_CULL_FACE);
1892 R_Shadow_RenderLighting(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle), surface->groupmesh->data_vertex3f, surface->groupmesh->data_svector3f, surface->groupmesh->data_tvector3f, surface->groupmesh->data_normal3f, surface->groupmesh->data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightcolor, NULL, NULL, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.base, NULL, NULL, t->skin.nmap, t->skin.gloss, lightcubemap, ambientscale, diffusescale, specularscale);
1893 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1894 qglEnable(GL_CULL_FACE);
1902 static void gl_surf_start(void)
1906 static void gl_surf_shutdown(void)
1910 static void gl_surf_newmap(void)
1915 void GL_Surf_Init(void)
1918 Cvar_RegisterVariable(&r_ambient);
1919 Cvar_RegisterVariable(&r_drawportals);
1920 Cvar_RegisterVariable(&r_testvis);
1921 Cvar_RegisterVariable(&r_detailtextures);
1922 Cvar_RegisterVariable(&r_surfaceworldnode);
1923 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
1924 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
1925 Cvar_RegisterVariable(&r_q3bsp_renderskydepth);
1926 Cvar_RegisterVariable(&gl_lightmaps);
1928 //R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);