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->extents[0]>>4)+1;
65 tmax = (surface->extents[1]>>4)+1;
68 lightmap = surface->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->styles[maps] != 255;maps++, lightmap += size3)
87 for (scale = d_lightstylevalue[surface->styles[maps]], i = 0;i < size3;i++)
88 bl[i] += lightmap[i] * scale;
92 stain = surface->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->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->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->stainsamples)
175 smax = (surface->extents[0] >> 4) + 1;
176 tmax = (surface->extents[1] >> 4) + 1;
178 impacts = DotProduct (impact, surface->texinfo->vecs[0]) + surface->texinfo->vecs[0][3] - surface->texturemins[0];
179 impactt = DotProduct (impact, surface->texinfo->vecs[1]) + surface->texinfo->vecs[1][3] - surface->texturemins[1];
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->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)
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_brushmodel_entities[n];
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 float *RSurf_GetVertexPointer(const entity_render_t *ent, const msurface_t *surface)
300 if (surface->texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
302 texture_t *texture = surface->texture;
304 float center[3], center2[3], forward[3], right[3], up[3], v[4][3];
305 matrix4x4_t matrix1, imatrix1;
306 R_Mesh_Matrix(&r_identitymatrix);
307 // a single autosprite surface can contain multiple sprites...
308 for (j = 0;j < surface->mesh.num_vertices - 3;j += 4)
311 for (i = 0;i < 4;i++)
312 VectorAdd(center, surface->mesh.data_vertex3f + (j+i) * 3, center);
313 VectorScale(center, 0.25f, center);
314 Matrix4x4_Transform(&ent->matrix, center, center2);
315 // FIXME: calculate vectors from triangle edges instead of using texture vectors as an easy way out?
316 Matrix4x4_FromVectors(&matrix1, surface->mesh.data_normal3f + j*3, surface->mesh.data_svector3f + j*3, surface->mesh.data_tvector3f + j*3, center);
317 Matrix4x4_Invert_Simple(&imatrix1, &matrix1);
318 for (i = 0;i < 4;i++)
319 Matrix4x4_Transform(&imatrix1, surface->mesh.data_vertex3f + (j+i)*3, v[i]);
320 if (texture->textureflags & Q3TEXTUREFLAG_AUTOSPRITE2)
322 forward[0] = r_vieworigin[0] - center2[0];
323 forward[1] = r_vieworigin[1] - center2[1];
325 VectorNormalize(forward);
326 right[0] = forward[1];
327 right[1] = -forward[0];
335 VectorCopy(r_viewforward, forward);
336 VectorCopy(r_viewright, right);
337 VectorCopy(r_viewup, up);
339 for (i = 0;i < 4;i++)
340 VectorMAMAMAM(1, center2, v[i][0], forward, v[i][1], right, v[i][2], up, varray_vertex3f + (i+j) * 3);
342 return varray_vertex3f;
345 return surface->mesh.data_vertex3f;
348 void R_UpdateTextureInfo(const entity_render_t *ent, texture_t *t)
350 // we don't need to set currentframe if t->animated is false because
351 // it was already set up by the texture loader for non-animating
354 t->currentframe = t->anim_frames[ent->frame != 0][(t->anim_total[ent->frame != 0] >= 2) ? ((int)(r_refdef.time * 5.0f) % t->anim_total[ent->frame != 0]) : 0];
357 t->currentmaterialflags = t->basematerialflags;
358 t->currentalpha = ent->alpha;
359 if (t->basematerialflags & MATERIALFLAG_WATERALPHA)
360 t->currentalpha *= r_wateralpha.value;
361 if (!(ent->flags & RENDER_LIGHT))
362 t->currentmaterialflags |= MATERIALFLAG_FULLBRIGHT;
363 if (ent->effects & EF_ADDITIVE)
364 t->currentmaterialflags |= MATERIALFLAG_ADD | MATERIALFLAG_TRANSPARENT;
365 else if (t->currentalpha < 1)
366 t->currentmaterialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_TRANSPARENT;
369 matrix4x4_t r_surf_waterscrollmatrix;
371 void R_UpdateAllTextureInfo(entity_render_t *ent)
374 Matrix4x4_CreateTranslate(&r_surf_waterscrollmatrix, sin(r_refdef.time) * 0.025 * r_waterscroll.value, sin(r_refdef.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
376 for (i = 0;i < ent->model->brush.num_textures;i++)
377 R_UpdateTextureInfo(ent, ent->model->brush.data_textures + i);
380 static void R_DrawSurfaceList(const entity_render_t *ent, texture_t *texture, int texturenumsurfaces, const msurface_t **texturesurfacelist, const vec3_t modelorg)
383 int texturesurfaceindex;
387 float f, r, g, b, a, base, colorscale;
388 const msurface_t *surface;
395 qboolean fogallpasses;
396 qboolean waterscrolling;
398 texture = texture->currentframe;
399 if (texture->currentmaterialflags & MATERIALFLAG_NODRAW)
401 c_faces += texturenumsurfaces;
402 // gl_lightmaps debugging mode skips normal texturing
403 if (gl_lightmaps.integer)
405 GL_BlendFunc(GL_ONE, GL_ZERO);
408 qglDisable(GL_CULL_FACE);
409 GL_Color(1, 1, 1, 1);
410 memset(&m, 0, sizeof(m));
411 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
413 surface = texturesurfacelist[texturesurfaceindex];
414 m.tex[0] = R_GetTexture(surface->lightmaptexture);
415 m.pointer_texcoord[0] = surface->mesh.data_texcoordlightmap2f;
416 if (surface->lightmaptexture)
418 GL_Color(1, 1, 1, 1);
419 m.pointer_color = NULL;
422 m.pointer_color = surface->mesh.data_lightmapcolor4f;
423 m.pointer_vertex = surface->mesh.data_vertex3f;
425 GL_LockArrays(0, surface->mesh.num_vertices);
426 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
429 qglEnable(GL_CULL_FACE);
432 GL_DepthTest(!(texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
433 GL_DepthMask(!(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT));
434 if (texture->currentmaterialflags & MATERIALFLAG_ADD)
435 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
436 else if (texture->currentmaterialflags & MATERIALFLAG_ALPHA)
437 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
439 GL_BlendFunc(GL_ONE, GL_ZERO);
440 // water waterscrolling in texture matrix
441 waterscrolling = (texture->currentmaterialflags & MATERIALFLAG_WATER) && r_waterscroll.value != 0;
442 if (texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
443 qglDisable(GL_CULL_FACE);
444 if (texture->currentmaterialflags & MATERIALFLAG_SKY)
448 skyrendernow = false;
452 // LordHavoc: HalfLife maps have freaky skypolys...
453 if (!ent->model->brush.ishlbsp)
455 R_Mesh_Matrix(&ent->matrix);
456 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
459 // depth-only (masking)
460 GL_ColorMask(0,0,0,0);
461 // just to make sure that braindead drivers don't draw anything
462 // despite that colormask...
463 GL_BlendFunc(GL_ZERO, GL_ONE);
468 GL_BlendFunc(GL_ONE, GL_ZERO);
472 memset(&m, 0, sizeof(m));
473 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
475 surface = texturesurfacelist[texturesurfaceindex];
476 m.pointer_vertex = surface->mesh.data_vertex3f;
478 GL_LockArrays(0, surface->mesh.num_vertices);
479 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
482 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
485 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)
487 // NVIDIA Geforce3 distortion texture shader on water
488 float args[4] = {0.05f,0,0,0.04f};
489 memset(&m, 0, sizeof(m));
490 m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(r_refdef.time * 16)&63]);
491 m.tex[1] = R_GetTexture(texture->skin.base);
492 m.texcombinergb[0] = GL_REPLACE;
493 m.texcombinergb[1] = GL_REPLACE;
494 Matrix4x4_CreateFromQuakeEntity(&m.texmatrix[0], 0, 0, 0, 0, 0, 0, r_watershader.value);
495 m.texmatrix[1] = r_surf_waterscrollmatrix;
498 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
500 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
501 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
502 qglTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
503 qglEnable(GL_TEXTURE_SHADER_NV);
505 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
507 surface = texturesurfacelist[texturesurfaceindex];
508 m.pointer_vertex = RSurf_GetVertexPointer(ent, surface);
509 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
510 m.pointer_texcoord[1] = surface->mesh.data_texcoordtexture2f;
512 GL_LockArrays(0, surface->mesh.num_vertices);
513 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
517 qglDisable(GL_TEXTURE_SHADER_NV);
518 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
521 else if (texture->currentmaterialflags & (MATERIALFLAG_WATER | MATERIALFLAG_WALL))
523 // normal surface (wall or water)
525 dolightmap = !(texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT);
526 doambient = r_ambient.value >= (1/64.0f);
527 dodetail = texture->skin.detail != NULL && !(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT);
528 doglow = texture->skin.glow != NULL;
529 dofogpass = fogenabled && !(texture->currentmaterialflags & MATERIALFLAG_ADD);
530 fogallpasses = fogenabled && !(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT);
531 if (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT)
533 if (dobase && dolightmap && gl_combine.integer)
536 memset(&m, 0, sizeof(m));
537 m.tex[1] = R_GetTexture(texture->skin.base);
539 m.texmatrix[1] = r_surf_waterscrollmatrix;
540 m.texrgbscale[1] = 2;
541 m.pointer_color = varray_color4f;
543 r = ent->colormod[0] * colorscale;
544 g = ent->colormod[1] * colorscale;
545 b = ent->colormod[2] * colorscale;
546 a = texture->currentalpha;
547 base = r_ambient.value * (1.0f / 64.0f);
548 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
550 surface = texturesurfacelist[texturesurfaceindex];
551 m.pointer_vertex = RSurf_GetVertexPointer(ent, surface);
552 m.pointer_texcoord[0] = surface->mesh.data_texcoordlightmap2f;
553 m.pointer_texcoord[1] = surface->mesh.data_texcoordtexture2f;
554 if (surface->lightmaptexture)
556 m.tex[0] = R_GetTexture(surface->lightmaptexture);
559 m.pointer_color = varray_color4f;
560 for (i = 0, v = m.pointer_vertex, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
562 VectorSubtract(v, modelorg, diff);
563 f = 1 - exp(fogdensity/DotProduct(diff, diff));
572 m.pointer_color = NULL;
573 GL_Color(r, g, b, a);
578 m.tex[0] = R_GetTexture(r_texture_white);
579 m.pointer_color = varray_color4f;
580 if (surface->styles[0] != 255)
582 for (i = 0, v = m.pointer_vertex, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
587 if (surface->styles[0] != 255)
589 if (surface->mesh.data_lightmapcolor4f)
591 float scale = d_lightstylevalue[surface->styles[0]] * (1.0f / 128.0f);
592 VectorMA(c, scale, surface->mesh.data_lightmapcolor4f + i*4, c);
594 else if (surface->mesh.data_lightmapoffsets)
596 const qbyte *lm = surface->samples + surface->mesh.data_lightmapoffsets[i];
597 float scale = d_lightstylevalue[surface->styles[0]] * (1.0f / 32768.0f);
598 VectorMA(c, scale, lm, c);
599 if (surface->styles[1] != 255)
601 int size3 = ((surface->extents[0]>>4)+1)*((surface->extents[1]>>4)+1)*3;
603 scale = d_lightstylevalue[surface->styles[1]] * (1.0f / 32768.0f);
604 VectorMA(c, scale, lm, c);
605 if (surface->styles[2] != 255)
608 scale = d_lightstylevalue[surface->styles[2]] * (1.0f / 32768.0f);
609 VectorMA(c, scale, lm, c);
610 if (surface->styles[3] != 255)
613 scale = d_lightstylevalue[surface->styles[3]] * (1.0f / 32768.0f);
614 VectorMA(c, scale, lm, c);
625 VectorSubtract(v, modelorg, diff);
626 f = 1 - exp(fogdensity/DotProduct(diff, diff));
627 VectorScale(c, f, c);
629 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
630 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
637 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
639 for (i = 0, v = m.pointer_vertex, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
644 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
649 m.pointer_color = NULL;
650 GL_Color(0, 0, 0, a);
655 GL_LockArrays(0, surface->mesh.num_vertices);
656 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
663 memset(&m, 0, sizeof(m));
664 m.tex[0] = R_GetTexture(texture->skin.base);
666 m.texmatrix[0] = r_surf_waterscrollmatrix;
667 m.texmatrix[0] = r_surf_waterscrollmatrix;
668 m.pointer_color = varray_color4f;
670 if (gl_combine.integer)
672 m.texrgbscale[0] = 4;
675 r = ent->colormod[0] * colorscale;
676 g = ent->colormod[1] * colorscale;
677 b = ent->colormod[2] * colorscale;
678 a = texture->currentalpha;
681 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
683 surface = texturesurfacelist[texturesurfaceindex];
684 m.pointer_vertex = RSurf_GetVertexPointer(ent, surface);
685 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
686 for (i = 0, v = m.pointer_vertex, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
691 if (surface->styles[0] != 255)
693 if (surface->mesh.data_lightmapcolor4f)
695 float scale = d_lightstylevalue[surface->styles[0]] * (1.0f / 128.0f);
696 VectorMA(c, scale, surface->mesh.data_lightmapcolor4f + i*4, c);
698 else if (surface->mesh.data_lightmapoffsets)
700 const qbyte *lm = surface->samples + surface->mesh.data_lightmapoffsets[i];
701 float scale = d_lightstylevalue[surface->styles[0]] * (1.0f / 32768.0f);
702 VectorMA(c, scale, lm, c);
703 if (surface->styles[1] != 255)
705 int size3 = ((surface->extents[0]>>4)+1)*((surface->extents[1]>>4)+1)*3;
707 scale = d_lightstylevalue[surface->styles[1]] * (1.0f / 32768.0f);
708 VectorMA(c, scale, lm, c);
709 if (surface->styles[2] != 255)
712 scale = d_lightstylevalue[surface->styles[2]] * (1.0f / 32768.0f);
713 VectorMA(c, scale, lm, c);
714 if (surface->styles[3] != 255)
717 scale = d_lightstylevalue[surface->styles[3]] * (1.0f / 32768.0f);
718 VectorMA(c, scale, lm, c);
729 VectorSubtract(v, modelorg, diff);
730 f = 1 - exp(fogdensity/DotProduct(diff, diff));
731 VectorScale(c, f, c);
733 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
734 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
739 GL_LockArrays(0, surface->mesh.num_vertices);
740 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
748 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
750 surface = texturesurfacelist[texturesurfaceindex];
751 m.pointer_vertex = RSurf_GetVertexPointer(ent, surface);
752 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
754 m.pointer_texcoord[1] = surface->mesh.data_texcoordtexture2f;
755 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
757 m.pointer_color = varray_color4f;
758 for (i = 0, v = m.pointer_vertex, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
760 VectorSubtract(v, modelorg, diff);
761 f = 1 - exp(fogdensity/DotProduct(diff, diff));
765 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
770 m.pointer_color = varray_color4f;
771 for (i = 0, v = m.pointer_vertex, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
773 VectorSubtract(v, modelorg, diff);
774 f = 1 - exp(fogdensity/DotProduct(diff, diff));
782 GL_LockArrays(0, surface->mesh.num_vertices);
783 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
789 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
791 surface = texturesurfacelist[texturesurfaceindex];
792 m.pointer_vertex = RSurf_GetVertexPointer(ent, surface);
793 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
795 m.pointer_texcoord[1] = surface->mesh.data_texcoordtexture2f;
796 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
798 m.pointer_color = varray_color4f;
799 for (i = 0, v = m.pointer_vertex, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
804 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
809 m.pointer_color = NULL;
810 GL_Color(r, g, b, a);
813 GL_LockArrays(0, surface->mesh.num_vertices);
814 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
823 if (!dolightmap && dobase)
827 GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], 1);
828 memset(&m, 0, sizeof(m));
829 m.tex[0] = R_GetTexture(texture->skin.base);
831 m.texmatrix[0] = r_surf_waterscrollmatrix;
832 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
834 surface = texturesurfacelist[texturesurfaceindex];
835 m.pointer_vertex = RSurf_GetVertexPointer(ent, surface);
836 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
838 GL_LockArrays(0, surface->mesh.num_vertices);
839 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
843 if (r_lightmapintensity <= 0 && dolightmap && dobase)
847 GL_Color(0, 0, 0, 1);
848 memset(&m, 0, sizeof(m));
849 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
851 surface = texturesurfacelist[texturesurfaceindex];
852 m.pointer_vertex = RSurf_GetVertexPointer(ent, surface);
854 GL_LockArrays(0, surface->mesh.num_vertices);
855 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
859 if (r_textureunits.integer >= 2 && gl_combine.integer && dolightmap && dobase)
861 // dualtexture combine
862 GL_BlendFunc(GL_ONE, GL_ZERO);
866 memset(&m, 0, sizeof(m));
867 m.tex[1] = R_GetTexture(texture->skin.base);
869 m.texmatrix[1] = r_surf_waterscrollmatrix;
870 m.texrgbscale[1] = 2;
871 r = ent->colormod[0] * r_lightmapintensity;
872 g = ent->colormod[1] * r_lightmapintensity;
873 b = ent->colormod[2] * r_lightmapintensity;
874 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
876 surface = texturesurfacelist[texturesurfaceindex];
877 memset(&m, 0, sizeof(m));
878 m.tex[1] = R_GetTexture(texture->skin.base);
880 m.texmatrix[1] = r_surf_waterscrollmatrix;
881 m.texrgbscale[1] = 2;
882 m.pointer_vertex = RSurf_GetVertexPointer(ent, surface);
883 m.pointer_texcoord[0] = surface->mesh.data_texcoordlightmap2f;
884 m.pointer_texcoord[1] = surface->mesh.data_texcoordtexture2f;
885 if (surface->lightmaptexture)
887 m.tex[0] = R_GetTexture(surface->lightmaptexture);
888 m.pointer_color = NULL;
889 GL_Color(r, g, b, 1);
891 else if (r == 1 && g == 1 && b == 1)
893 m.tex[0] = R_GetTexture(r_texture_white);
894 m.pointer_color = surface->mesh.data_lightmapcolor4f;
898 m.tex[0] = R_GetTexture(r_texture_white);
899 m.pointer_color = varray_color4f;
900 for (i = 0;i < surface->mesh.num_vertices;i++)
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];
909 GL_LockArrays(0, surface->mesh.num_vertices);
910 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
917 GL_BlendFunc(GL_ONE, GL_ZERO);
919 GL_Color(1, 1, 1, 1);
920 memset(&m, 0, sizeof(m));
921 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
923 surface = texturesurfacelist[texturesurfaceindex];
924 m.pointer_vertex = RSurf_GetVertexPointer(ent, surface);
925 m.tex[0] = R_GetTexture(surface->lightmaptexture);
926 m.pointer_texcoord[0] = surface->mesh.data_texcoordlightmap2f;
927 if (surface->lightmaptexture)
928 m.pointer_color = NULL;
930 m.pointer_color = surface->mesh.data_lightmapcolor4f;
932 GL_LockArrays(0, surface->mesh.num_vertices);
933 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
939 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
941 GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
942 memset(&m, 0, sizeof(m));
943 m.tex[0] = R_GetTexture(texture->skin.base);
945 m.texmatrix[0] = r_surf_waterscrollmatrix;
946 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
948 surface = texturesurfacelist[texturesurfaceindex];
949 m.pointer_vertex = RSurf_GetVertexPointer(ent, surface);
950 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
952 GL_LockArrays(0, surface->mesh.num_vertices);
953 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
961 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
963 memset(&m, 0, sizeof(m));
964 m.tex[0] = R_GetTexture(texture->skin.base);
966 m.texmatrix[0] = r_surf_waterscrollmatrix;
967 m.pointer_color = varray_color4f;
969 if (gl_combine.integer)
971 m.texrgbscale[0] = 4;
974 base = r_ambient.value * (1.0f / 64.0f);
975 r = ent->colormod[0] * colorscale * base;
976 g = ent->colormod[1] * colorscale * base;
977 b = ent->colormod[2] * colorscale * base;
978 a = texture->currentalpha;
979 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
981 surface = texturesurfacelist[texturesurfaceindex];
982 m.pointer_vertex = RSurf_GetVertexPointer(ent, surface);
983 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
984 for (i = 0, v = m.pointer_vertex, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
991 VectorSubtract(v, modelorg, diff);
992 f = 1 - exp(fogdensity/DotProduct(diff, diff));
993 VectorScale(c, f, c);
995 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
996 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
1001 GL_LockArrays(0, surface->mesh.num_vertices);
1002 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1003 GL_LockArrays(0, 0);
1008 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1009 GL_DepthMask(false);
1010 GL_Color(1, 1, 1, 1);
1011 memset(&m, 0, sizeof(m));
1012 m.tex[0] = R_GetTexture(texture->skin.detail);
1013 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1015 surface = texturesurfacelist[texturesurfaceindex];
1016 m.pointer_vertex = RSurf_GetVertexPointer(ent, surface);
1017 m.pointer_texcoord[0] = surface->mesh.data_texcoorddetail2f;
1019 GL_LockArrays(0, surface->mesh.num_vertices);
1020 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1021 GL_LockArrays(0, 0);
1026 // if glow was not already done using multitexture, do it now.
1027 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1028 GL_DepthMask(false);
1029 memset(&m, 0, sizeof(m));
1030 m.tex[0] = R_GetTexture(texture->skin.glow);
1032 m.texmatrix[0] = r_surf_waterscrollmatrix;
1033 m.pointer_color = varray_color4f;
1035 r = ent->colormod[0] * colorscale;
1036 g = ent->colormod[1] * colorscale;
1037 b = ent->colormod[2] * colorscale;
1038 a = texture->currentalpha;
1041 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1043 surface = texturesurfacelist[texturesurfaceindex];
1044 m.pointer_vertex = RSurf_GetVertexPointer(ent, surface);
1045 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1046 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1048 m.pointer_color = varray_color4f;
1049 for (i = 0, v = m.pointer_vertex, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1051 VectorSubtract(v, modelorg, diff);
1052 f = 1 - exp(fogdensity/DotProduct(diff, diff));
1056 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
1061 m.pointer_color = varray_color4f;
1062 for (i = 0, v = m.pointer_vertex, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1064 VectorSubtract(v, modelorg, diff);
1065 f = 1 - exp(fogdensity/DotProduct(diff, diff));
1073 GL_LockArrays(0, surface->mesh.num_vertices);
1074 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1075 GL_LockArrays(0, 0);
1080 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1082 surface = texturesurfacelist[texturesurfaceindex];
1083 m.pointer_vertex = RSurf_GetVertexPointer(ent, surface);
1084 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1085 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1087 m.pointer_color = varray_color4f;
1088 for (i = 0, v = m.pointer_vertex, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1093 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
1098 m.pointer_color = NULL;
1099 GL_Color(r, g, b, a);
1102 GL_LockArrays(0, surface->mesh.num_vertices);
1103 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1104 GL_LockArrays(0, 0);
1110 // if this is opaque use alpha blend which will darken the earlier
1113 // if this is an alpha blended material, all the earlier passes
1114 // were darkened by fog already, so we only need to add the fog
1115 // color ontop through the fog mask texture
1117 // if this is an additive blended material, all the earlier passes
1118 // were darkened by fog already, and we should not add fog color
1119 // (because the background was not darkened, there is no fog color
1120 // that was lost behind it).
1122 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1124 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1125 GL_DepthMask(false);
1126 memset(&m, 0, sizeof(m));
1127 m.tex[0] = R_GetTexture(texture->skin.fog);
1129 m.texmatrix[0] = r_surf_waterscrollmatrix;
1133 a = texture->currentalpha;
1134 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1136 surface = texturesurfacelist[texturesurfaceindex];
1137 m.pointer_vertex = RSurf_GetVertexPointer(ent, surface);
1138 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1139 m.pointer_color = varray_color4f;
1140 //RSurf_FogPassColors_Vertex3f_Color4f(surface->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], texture->currentalpha, 1, surface->mesh.num_vertices, modelorg);
1141 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1143 m.pointer_color = varray_color4f;
1144 for (i = 0, v = m.pointer_vertex, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1146 VectorSubtract(v, modelorg, diff);
1147 f = exp(fogdensity/DotProduct(diff, diff));
1151 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * f * a;
1156 m.pointer_color = varray_color4f;
1157 for (i = 0, v = m.pointer_vertex, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1159 VectorSubtract(v, modelorg, diff);
1160 f = exp(fogdensity/DotProduct(diff, diff));
1168 GL_LockArrays(0, surface->mesh.num_vertices);
1169 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1170 GL_LockArrays(0, 0);
1174 if (texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1175 qglEnable(GL_CULL_FACE);
1178 static void RSurfShader_Transparent_Callback(const void *calldata1, int calldata2)
1180 const entity_render_t *ent = calldata1;
1181 const msurface_t *surface = ent->model->brush.data_surfaces + calldata2;
1185 texture = surface->texture;
1186 if (texture->basematerialflags & MATERIALFLAG_SKY)
1187 return; // transparent sky is too difficult
1188 R_UpdateTextureInfo(ent, texture);
1190 R_Mesh_Matrix(&ent->matrix);
1191 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1192 R_DrawSurfaceList(ent, texture, 1, &surface, modelorg);
1195 void R_QueueSurfaceList(entity_render_t *ent, texture_t *texture, int texturenumsurfaces, const msurface_t **texturesurfacelist, const vec3_t modelorg)
1197 int texturesurfaceindex;
1198 const msurface_t *surface;
1199 vec3_t tempcenter, center;
1200 if (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT)
1202 // drawing sky transparently would be too difficult
1203 if (!(texture->currentmaterialflags & MATERIALFLAG_SKY))
1205 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1207 surface = texturesurfacelist[texturesurfaceindex];
1208 tempcenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
1209 tempcenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
1210 tempcenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
1211 Matrix4x4_Transform(&ent->matrix, tempcenter, center);
1212 R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_vieworigin : center, RSurfShader_Transparent_Callback, ent, surface - ent->model->brush.data_surfaces);
1217 R_DrawSurfaceList(ent, texture, texturenumsurfaces, texturesurfacelist, modelorg);
1220 void R_DrawSurfaces(entity_render_t *ent, qboolean skysurfaces)
1222 int i, j, f, flagsmask;
1223 msurface_t *surface, **surfacechain;
1224 texture_t *t, *texture;
1225 model_t *model = ent->model;
1227 const int maxsurfacelist = 1024;
1228 int numsurfacelist = 0;
1229 const msurface_t *surfacelist[1024];
1232 R_Mesh_Matrix(&ent->matrix);
1233 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1235 // update light styles
1238 for (i = 0;i < model->brushq1.light_styles;i++)
1240 if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1242 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1243 if ((surfacechain = model->brushq1.light_styleupdatechains[i]))
1244 for (;(surface = *surfacechain);surfacechain++)
1245 surface->cached_dlight = true;
1250 R_UpdateAllTextureInfo(ent);
1251 flagsmask = skysurfaces ? MATERIALFLAG_SKY : (MATERIALFLAG_WATER | MATERIALFLAG_WALL);
1256 for (i = 0, j = model->firstmodelsurface;i < model->nummodelsurfaces;i++, j++)
1258 if (ent != r_refdef.worldentity || r_worldsurfacevisible[j])
1260 surface = model->brush.data_surfaces + j;
1261 if (t != surface->texture)
1265 R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1268 t = surface->texture;
1269 f = t->currentmaterialflags & flagsmask;
1270 texture = t->currentframe;
1274 // if lightmap parameters changed, rebuild lightmap texture
1275 if (surface->cached_dlight && surface->samples)
1276 R_BuildLightMap(ent, surface);
1277 // add face to draw list
1278 surfacelist[numsurfacelist++] = surface;
1279 if (numsurfacelist >= maxsurfacelist)
1281 R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1288 R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1291 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1296 const mportal_t *portal = calldata1;
1297 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1298 GL_DepthMask(false);
1300 R_Mesh_Matrix(&r_identitymatrix);
1302 memset(&m, 0, sizeof(m));
1303 m.pointer_vertex = varray_vertex3f;
1307 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1308 ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1309 ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1311 if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1313 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1314 VectorCopy(portal->points[i].position, v);
1317 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1318 VectorCopy(portal->points[i].position, v);
1319 GL_LockArrays(0, portal->numpoints);
1320 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1321 GL_LockArrays(0, 0);
1324 // LordHavoc: this is just a nice debugging tool, very slow
1325 static void R_DrawPortals(void)
1330 model_t *model = r_refdef.worldmodel;
1333 for (portalnum = 0, portal = model->brush.data_portals;portalnum < model->brush.num_portals;portalnum++, portal++)
1335 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1336 if (!R_CullBox(portal->mins, portal->maxs))
1338 VectorClear(center);
1339 for (i = 0;i < portal->numpoints;i++)
1340 VectorAdd(center, portal->points[i].position, center);
1341 f = ixtable[portal->numpoints];
1342 VectorScale(center, f, center);
1343 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, portal, portalnum);
1348 static void R_DrawCollisionBrush(colbrushf_t *brush)
1352 memset(&m, 0, sizeof(m));
1353 m.pointer_vertex = brush->points->v;
1355 i = (int)(((size_t)brush) / sizeof(colbrushf_t));
1356 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1357 GL_LockArrays(0, brush->numpoints);
1358 R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1359 GL_LockArrays(0, 0);
1362 static void R_DrawCollisionSurface(entity_render_t *ent, msurface_t *surface)
1366 if (!surface->mesh.num_collisiontriangles)
1368 memset(&m, 0, sizeof(m));
1369 m.pointer_vertex = surface->mesh.data_collisionvertex3f;
1371 i = (int)(((size_t)surface) / sizeof(msurface_t));
1372 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1373 GL_LockArrays(0, surface->mesh.num_collisionvertices);
1374 R_Mesh_Draw(surface->mesh.num_collisionvertices, surface->mesh.num_collisiontriangles, surface->mesh.data_collisionelement3i);
1375 GL_LockArrays(0, 0);
1378 void R_WorldVisibility(void)
1383 model_t *model = r_refdef.worldmodel;
1388 // if possible find the leaf the view origin is in
1389 viewleaf = model->brushq1.PointInLeaf ? model->brushq1.PointInLeaf(model, r_vieworigin) : NULL;
1390 // if possible fetch the visible cluster bits
1391 if (model->brush.FatPVS)
1392 model->brush.FatPVS(model, r_vieworigin, 2, r_pvsbits, sizeof(r_pvsbits));
1394 // clear the visible surface and leaf flags arrays
1395 memset(r_worldsurfacevisible, 0, model->brush.num_surfaces);
1396 memset(r_worldleafvisible, 0, model->brush.num_leafs);
1398 // if the user prefers surfaceworldnode (testing?) or the viewleaf could
1399 // not be found, or the viewleaf is not part of the visible world
1400 // (floating around in the void), use the pvs method
1401 if (r_surfaceworldnode.integer || !viewleaf || viewleaf->clusterindex < 0)
1404 // similar to quake's RecursiveWorldNode but without cache misses
1405 for (j = 0, leaf = model->brush.data_leafs;j < model->brush.num_leafs;j++, leaf++)
1407 // if leaf is in current pvs and on the screen, mark its surfaces
1408 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
1411 r_worldleafvisible[j] = true;
1412 if (leaf->numleafsurfaces)
1413 for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
1414 r_worldsurfacevisible[*mark] = true;
1422 mleaf_t *leafstack[8192];
1424 // follows portals leading outward from viewleaf, does not venture
1425 // offscreen or into leafs that are not visible, faster than Quake's
1426 // RecursiveWorldNode and vastly better in unvised maps, often culls a
1427 // lot of surface that pvs alone would miss
1428 leafstack[0] = viewleaf;
1430 while (leafstackpos)
1433 leaf = leafstack[--leafstackpos];
1434 r_worldleafvisible[leaf - model->brush.data_leafs] = true;
1435 // mark any surfaces bounding this leaf
1436 if (leaf->numleafsurfaces)
1437 for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
1438 r_worldsurfacevisible[*mark] = true;
1439 // follow portals into other leafs
1441 // if viewer is behind portal (portal faces outward into the scene)
1442 // and the portal polygon's bounding box is on the screen
1443 // and the leaf has not been visited yet
1444 // and the leaf is visible in the pvs
1445 // (the first two checks won't cause as many cache misses as the leaf checks)
1446 for (p = leaf->portals;p;p = p->next)
1447 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))
1448 leafstack[leafstackpos++] = p->past;
1452 if (r_drawportals.integer)
1456 void R_Q1BSP_DrawSky(entity_render_t *ent)
1458 if (ent->model == NULL)
1460 if (r_drawcollisionbrushes.integer < 2)
1461 R_DrawSurfaces(ent, true);
1464 void R_Q1BSP_Draw(entity_render_t *ent)
1466 if (ent->model == NULL)
1469 if (r_drawcollisionbrushes.integer < 2)
1470 R_DrawSurfaces(ent, false);
1471 if (r_drawcollisionbrushes.integer >= 1 && ent->model->brush.num_brushes)
1474 model_t *model = ent->model;
1475 msurface_t *surface;
1477 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1478 GL_DepthMask(false);
1480 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
1481 for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
1482 if (brush->colbrushf && brush->colbrushf->numtriangles)
1483 R_DrawCollisionBrush(brush->colbrushf);
1484 for (i = 0, surface = model->brush.data_surfaces + model->firstmodelsurface;i < model->nummodelsurfaces;i++, surface++)
1485 if (surface->mesh.num_collisiontriangles)
1486 R_DrawCollisionSurface(ent, surface);
1487 qglPolygonOffset(0, 0);
1491 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)
1493 model_t *model = ent->model;
1494 vec3_t lightmins, lightmaxs;
1495 int t, leafindex, leafsurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
1498 msurface_t *surface;
1501 lightmins[0] = relativelightorigin[0] - lightradius;
1502 lightmins[1] = relativelightorigin[1] - lightradius;
1503 lightmins[2] = relativelightorigin[2] - lightradius;
1504 lightmaxs[0] = relativelightorigin[0] + lightradius;
1505 lightmaxs[1] = relativelightorigin[1] + lightradius;
1506 lightmaxs[2] = relativelightorigin[2] + lightradius;
1507 *outnumclusterspointer = 0;
1508 *outnumsurfacespointer = 0;
1509 memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
1510 memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
1513 VectorCopy(lightmins, outmins);
1514 VectorCopy(lightmaxs, outmaxs);
1517 VectorCopy(relativelightorigin, outmins);
1518 VectorCopy(relativelightorigin, outmaxs);
1519 if (model->brush.GetPVS)
1520 pvs = model->brush.GetPVS(model, relativelightorigin);
1523 R_UpdateAllTextureInfo(ent);
1524 // FIXME: use BSP recursion as lights are often small
1525 for (leafindex = 0, leaf = model->brush.data_leafs;leafindex < model->brush.num_leafs;leafindex++, leaf++)
1527 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
1529 outmins[0] = min(outmins[0], leaf->mins[0]);
1530 outmins[1] = min(outmins[1], leaf->mins[1]);
1531 outmins[2] = min(outmins[2], leaf->mins[2]);
1532 outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
1533 outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
1534 outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
1537 if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
1539 SETPVSBIT(outclusterpvs, leaf->clusterindex);
1540 outclusterlist[outnumclusters++] = leaf->clusterindex;
1545 for (leafsurfaceindex = 0;leafsurfaceindex < leaf->numleafsurfaces;leafsurfaceindex++)
1547 surfaceindex = leaf->firstleafsurface[leafsurfaceindex];
1548 if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
1550 surface = model->brush.data_surfaces + surfaceindex;
1551 if (BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs))
1552 if ((surface->texture->currentmaterialflags & (MATERIALFLAG_WALL | MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
1554 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
1556 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1557 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1558 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1559 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])))
1561 SETPVSBIT(outsurfacepvs, surfaceindex);
1562 outsurfacelist[outnumsurfaces++] = surfaceindex;
1573 // limit combined leaf box to light boundaries
1574 outmins[0] = max(outmins[0], lightmins[0]);
1575 outmins[1] = max(outmins[1], lightmins[1]);
1576 outmins[2] = max(outmins[2], lightmins[2]);
1577 outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
1578 outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
1579 outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
1581 *outnumclusterspointer = outnumclusters;
1582 *outnumsurfacespointer = outnumsurfaces;
1585 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)
1587 model_t *model = ent->model;
1588 msurface_t *surface;
1589 int surfacelistindex;
1590 if (r_drawcollisionbrushes.integer < 2)
1592 R_Mesh_Matrix(&ent->matrix);
1593 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
1594 if (!r_shadow_compilingrtlight)
1595 R_UpdateAllTextureInfo(ent);
1596 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1598 surface = model->brush.data_surfaces + surfacelist[surfacelistindex];
1599 if ((surface->texture->currentmaterialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT | MATERIALFLAG_WALL)) != MATERIALFLAG_WALL)
1601 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1603 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);
1605 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);
1609 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)
1611 model_t *model = ent->model;
1612 msurface_t *surface;
1614 int surfacelistindex;
1615 if (r_drawcollisionbrushes.integer < 2)
1617 R_Mesh_Matrix(&ent->matrix);
1618 if (!r_shadow_compilingrtlight)
1619 R_UpdateAllTextureInfo(ent);
1620 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1622 surface = model->brush.data_surfaces + surfacelist[surfacelistindex];
1623 if (surface->texture->basematerialflags & MATERIALFLAG_NODRAW || !surface->mesh.num_triangles)
1625 if (r_shadow_compilingrtlight)
1627 // if compiling an rtlight, capture the mesh
1628 t = surface->texture;
1629 if ((t->basematerialflags & (MATERIALFLAG_WALL | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
1630 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);
1632 else if (ent != r_refdef.worldentity || r_worldsurfacevisible[surfacelist[surfacelistindex]])
1634 t = surface->texture->currentframe;
1635 // FIXME: transparent surfaces need to be lit later
1636 if ((t->currentmaterialflags & (MATERIALFLAG_WALL | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
1638 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1639 qglDisable(GL_CULL_FACE);
1640 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);
1641 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1642 qglEnable(GL_CULL_FACE);
1650 static void gl_surf_start(void)
1654 static void gl_surf_shutdown(void)
1658 static void gl_surf_newmap(void)
1663 void GL_Surf_Init(void)
1666 Cvar_RegisterVariable(&r_ambient);
1667 Cvar_RegisterVariable(&r_drawportals);
1668 Cvar_RegisterVariable(&r_testvis);
1669 Cvar_RegisterVariable(&r_detailtextures);
1670 Cvar_RegisterVariable(&r_surfaceworldnode);
1671 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
1672 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
1673 Cvar_RegisterVariable(&r_q3bsp_renderskydepth);
1674 Cvar_RegisterVariable(&gl_lightmaps);
1676 //R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);