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;
384 const float *v, *vertex3f;
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));
412 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
414 surface = texturesurfacelist[texturesurfaceindex];
415 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
416 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordlightmap2f);
417 R_Mesh_ColorPointer(surface->lightmaptexture ? NULL : surface->mesh.data_lightmapcolor4f);
418 R_Mesh_VertexPointer(surface->mesh.data_vertex3f);
419 GL_LockArrays(0, surface->mesh.num_vertices);
420 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
423 qglEnable(GL_CULL_FACE);
426 GL_DepthTest(!(texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
427 GL_DepthMask(!(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT));
428 if (texture->currentmaterialflags & MATERIALFLAG_ADD)
429 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
430 else if (texture->currentmaterialflags & MATERIALFLAG_ALPHA)
431 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
433 GL_BlendFunc(GL_ONE, GL_ZERO);
434 // water waterscrolling in texture matrix
435 waterscrolling = (texture->currentmaterialflags & MATERIALFLAG_WATER) && r_waterscroll.value != 0;
436 if (texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
437 qglDisable(GL_CULL_FACE);
438 if (texture->currentmaterialflags & MATERIALFLAG_SKY)
442 skyrendernow = false;
446 // LordHavoc: HalfLife maps have freaky skypolys...
447 if (!ent->model->brush.ishlbsp)
449 R_Mesh_Matrix(&ent->matrix);
450 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
453 // depth-only (masking)
454 GL_ColorMask(0,0,0,0);
455 // just to make sure that braindead drivers don't draw anything
456 // despite that colormask...
457 GL_BlendFunc(GL_ZERO, GL_ONE);
462 GL_BlendFunc(GL_ONE, GL_ZERO);
466 memset(&m, 0, sizeof(m));
468 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
470 surface = texturesurfacelist[texturesurfaceindex];
471 R_Mesh_VertexPointer(surface->mesh.data_vertex3f);
472 GL_LockArrays(0, surface->mesh.num_vertices);
473 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
476 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
479 else if ((texture->currentmaterialflags & MATERIALFLAG_WATER) && r_watershader.value && gl_textureshader && !texture->skin.glow && !fogenabled && ent->colormod[0] == 1 && ent->colormod[1] == 1 && ent->colormod[2] == 1)
481 // NVIDIA Geforce3 distortion texture shader on water
482 float args[4] = {0.05f,0,0,0.04f};
483 memset(&m, 0, sizeof(m));
484 m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(r_refdef.time * 16)&63]);
485 m.tex[1] = R_GetTexture(texture->skin.base);
486 m.texcombinergb[0] = GL_REPLACE;
487 m.texcombinergb[1] = GL_REPLACE;
488 Matrix4x4_CreateFromQuakeEntity(&m.texmatrix[0], 0, 0, 0, 0, 0, 0, r_watershader.value);
489 m.texmatrix[1] = r_surf_waterscrollmatrix;
492 GL_Color(1, 1, 1, texture->currentalpha);
494 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
496 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
497 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
498 qglTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
499 qglEnable(GL_TEXTURE_SHADER_NV);
501 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
503 surface = texturesurfacelist[texturesurfaceindex];
504 R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, surface));
505 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
506 R_Mesh_TexCoordPointer(1, 2, surface->mesh.data_texcoordtexture2f);
507 GL_LockArrays(0, surface->mesh.num_vertices);
508 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
512 qglDisable(GL_TEXTURE_SHADER_NV);
513 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
516 else if (texture->currentmaterialflags & (MATERIALFLAG_WATER | MATERIALFLAG_WALL))
518 // normal surface (wall or water)
520 dolightmap = !(texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT);
521 doambient = r_ambient.value >= (1/64.0f);
522 dodetail = r_detailtextures.integer && texture->skin.detail != NULL && !(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT);
523 doglow = texture->skin.glow != NULL;
524 dofogpass = fogenabled && !(texture->currentmaterialflags & MATERIALFLAG_ADD);
525 fogallpasses = fogenabled && !(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT);
526 if (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT)
528 if (dobase && dolightmap && gl_combine.integer)
531 memset(&m, 0, sizeof(m));
532 m.tex[1] = R_GetTexture(texture->skin.base);
534 m.texmatrix[1] = r_surf_waterscrollmatrix;
535 m.texrgbscale[1] = 2;
536 m.pointer_color = varray_color4f;
539 r = ent->colormod[0] * colorscale;
540 g = ent->colormod[1] * colorscale;
541 b = ent->colormod[2] * colorscale;
542 a = texture->currentalpha;
543 base = r_ambient.value * (1.0f / 64.0f);
544 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
546 surface = texturesurfacelist[texturesurfaceindex];
547 vertex3f = RSurf_GetVertexPointer(ent, surface);
548 R_Mesh_VertexPointer(vertex3f);
549 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordlightmap2f);
550 R_Mesh_TexCoordPointer(1, 2, surface->mesh.data_texcoordtexture2f);
551 if (surface->lightmaptexture)
553 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
556 R_Mesh_ColorPointer(varray_color4f);
557 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
559 VectorSubtract(v, modelorg, diff);
560 f = 1 - exp(fogdensity/DotProduct(diff, diff));
569 R_Mesh_ColorPointer(NULL);
570 GL_Color(r, g, b, a);
575 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
576 R_Mesh_ColorPointer(varray_color4f);
577 if (surface->styles[0] != 255)
579 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
584 if (surface->styles[0] != 255)
586 if (surface->mesh.data_lightmapcolor4f)
588 float scale = d_lightstylevalue[surface->styles[0]] * (1.0f / 128.0f);
589 VectorMA(c, scale, surface->mesh.data_lightmapcolor4f + i*4, c);
591 else if (surface->mesh.data_lightmapoffsets)
593 const qbyte *lm = surface->samples + surface->mesh.data_lightmapoffsets[i];
594 float scale = d_lightstylevalue[surface->styles[0]] * (1.0f / 32768.0f);
595 VectorMA(c, scale, lm, c);
596 if (surface->styles[1] != 255)
598 int size3 = ((surface->extents[0]>>4)+1)*((surface->extents[1]>>4)+1)*3;
600 scale = d_lightstylevalue[surface->styles[1]] * (1.0f / 32768.0f);
601 VectorMA(c, scale, lm, c);
602 if (surface->styles[2] != 255)
605 scale = d_lightstylevalue[surface->styles[2]] * (1.0f / 32768.0f);
606 VectorMA(c, scale, lm, c);
607 if (surface->styles[3] != 255)
610 scale = d_lightstylevalue[surface->styles[3]] * (1.0f / 32768.0f);
611 VectorMA(c, scale, lm, c);
622 VectorSubtract(v, modelorg, diff);
623 f = 1 - exp(fogdensity/DotProduct(diff, diff));
624 VectorScale(c, f, c);
626 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
627 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
634 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
636 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
641 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
646 R_Mesh_ColorPointer(NULL);
647 GL_Color(0, 0, 0, a);
651 GL_LockArrays(0, surface->mesh.num_vertices);
652 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
659 memset(&m, 0, sizeof(m));
660 m.tex[0] = R_GetTexture(texture->skin.base);
662 m.texmatrix[0] = r_surf_waterscrollmatrix;
663 m.texmatrix[0] = r_surf_waterscrollmatrix;
664 m.pointer_color = varray_color4f;
666 if (gl_combine.integer)
668 m.texrgbscale[0] = 4;
672 r = ent->colormod[0] * colorscale;
673 g = ent->colormod[1] * colorscale;
674 b = ent->colormod[2] * colorscale;
675 a = texture->currentalpha;
678 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
680 surface = texturesurfacelist[texturesurfaceindex];
681 vertex3f = RSurf_GetVertexPointer(ent, surface);
682 R_Mesh_VertexPointer(vertex3f);
683 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
684 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
689 if (surface->styles[0] != 255)
691 if (surface->mesh.data_lightmapcolor4f)
693 float scale = d_lightstylevalue[surface->styles[0]] * (1.0f / 128.0f);
694 VectorMA(c, scale, surface->mesh.data_lightmapcolor4f + i*4, c);
696 else if (surface->mesh.data_lightmapoffsets)
698 const qbyte *lm = surface->samples + surface->mesh.data_lightmapoffsets[i];
699 float scale = d_lightstylevalue[surface->styles[0]] * (1.0f / 32768.0f);
700 VectorMA(c, scale, lm, c);
701 if (surface->styles[1] != 255)
703 int size3 = ((surface->extents[0]>>4)+1)*((surface->extents[1]>>4)+1)*3;
705 scale = d_lightstylevalue[surface->styles[1]] * (1.0f / 32768.0f);
706 VectorMA(c, scale, lm, c);
707 if (surface->styles[2] != 255)
710 scale = d_lightstylevalue[surface->styles[2]] * (1.0f / 32768.0f);
711 VectorMA(c, scale, lm, c);
712 if (surface->styles[3] != 255)
715 scale = d_lightstylevalue[surface->styles[3]] * (1.0f / 32768.0f);
716 VectorMA(c, scale, lm, c);
727 VectorSubtract(v, modelorg, diff);
728 f = 1 - exp(fogdensity/DotProduct(diff, diff));
729 VectorScale(c, f, c);
731 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
732 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
736 GL_LockArrays(0, surface->mesh.num_vertices);
737 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
745 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
747 surface = texturesurfacelist[texturesurfaceindex];
748 vertex3f = RSurf_GetVertexPointer(ent, surface);
749 R_Mesh_VertexPointer(vertex3f);
750 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
751 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
753 R_Mesh_ColorPointer(varray_color4f);
754 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
756 VectorSubtract(v, modelorg, diff);
757 f = 1 - exp(fogdensity/DotProduct(diff, diff));
761 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
766 R_Mesh_ColorPointer(varray_color4f);
767 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
769 VectorSubtract(v, modelorg, diff);
770 f = 1 - exp(fogdensity/DotProduct(diff, diff));
777 GL_LockArrays(0, surface->mesh.num_vertices);
778 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
784 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
786 surface = texturesurfacelist[texturesurfaceindex];
787 vertex3f = RSurf_GetVertexPointer(ent, surface);
788 R_Mesh_VertexPointer(vertex3f);
789 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
790 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
792 R_Mesh_ColorPointer(varray_color4f);
793 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
798 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
803 R_Mesh_ColorPointer(NULL);
804 GL_Color(r, g, b, a);
806 GL_LockArrays(0, surface->mesh.num_vertices);
807 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
816 if (!dolightmap && dobase)
820 GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], 1);
821 memset(&m, 0, sizeof(m));
822 m.tex[0] = R_GetTexture(texture->skin.base);
824 m.texmatrix[0] = r_surf_waterscrollmatrix;
826 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
828 surface = texturesurfacelist[texturesurfaceindex];
829 R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, surface));
830 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
831 GL_LockArrays(0, surface->mesh.num_vertices);
832 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
836 if (r_lightmapintensity <= 0 && dolightmap && dobase)
840 GL_Color(0, 0, 0, 1);
841 memset(&m, 0, sizeof(m));
843 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
845 surface = texturesurfacelist[texturesurfaceindex];
846 R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, surface));
847 GL_LockArrays(0, surface->mesh.num_vertices);
848 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
852 if (r_textureunits.integer >= 2 && gl_combine.integer && dolightmap && dobase)
854 // dualtexture combine
855 GL_BlendFunc(GL_ONE, GL_ZERO);
859 memset(&m, 0, sizeof(m));
860 m.tex[1] = R_GetTexture(texture->skin.base);
862 m.texmatrix[1] = r_surf_waterscrollmatrix;
863 m.texrgbscale[1] = 2;
865 r = ent->colormod[0] * r_lightmapintensity;
866 g = ent->colormod[1] * r_lightmapintensity;
867 b = ent->colormod[2] * r_lightmapintensity;
868 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
870 surface = texturesurfacelist[texturesurfaceindex];
871 R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, surface));
872 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordlightmap2f);
873 R_Mesh_TexCoordPointer(1, 2, surface->mesh.data_texcoordtexture2f);
874 if (surface->lightmaptexture)
876 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
877 R_Mesh_ColorPointer(NULL);
878 GL_Color(r, g, b, 1);
880 else if (r == 1 && g == 1 && b == 1)
882 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
883 R_Mesh_ColorPointer(surface->mesh.data_lightmapcolor4f);
887 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
888 R_Mesh_ColorPointer(varray_color4f);
889 for (i = 0;i < surface->mesh.num_vertices;i++)
891 varray_color4f[i*4+0] = surface->mesh.data_lightmapcolor4f[i*4+0] * r;
892 varray_color4f[i*4+1] = surface->mesh.data_lightmapcolor4f[i*4+1] * g;
893 varray_color4f[i*4+2] = surface->mesh.data_lightmapcolor4f[i*4+2] * b;
894 varray_color4f[i*4+3] = surface->mesh.data_lightmapcolor4f[i*4+3];
897 GL_LockArrays(0, surface->mesh.num_vertices);
898 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
905 GL_BlendFunc(GL_ONE, GL_ZERO);
907 GL_Color(1, 1, 1, 1);
908 memset(&m, 0, sizeof(m));
910 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
912 surface = texturesurfacelist[texturesurfaceindex];
913 R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, surface));
914 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
915 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordlightmap2f);
916 R_Mesh_ColorPointer(surface->lightmaptexture ? NULL : surface->mesh.data_lightmapcolor4f);
917 GL_LockArrays(0, surface->mesh.num_vertices);
918 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
924 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
926 GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
927 memset(&m, 0, sizeof(m));
928 m.tex[0] = R_GetTexture(texture->skin.base);
930 m.texmatrix[0] = r_surf_waterscrollmatrix;
932 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
934 surface = texturesurfacelist[texturesurfaceindex];
935 R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, surface));
936 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
937 GL_LockArrays(0, surface->mesh.num_vertices);
938 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
946 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
948 memset(&m, 0, sizeof(m));
949 m.tex[0] = R_GetTexture(texture->skin.base);
951 m.texmatrix[0] = r_surf_waterscrollmatrix;
952 m.pointer_color = varray_color4f;
954 if (gl_combine.integer)
956 m.texrgbscale[0] = 4;
960 base = r_ambient.value * (1.0f / 64.0f);
961 r = ent->colormod[0] * colorscale * base;
962 g = ent->colormod[1] * colorscale * base;
963 b = ent->colormod[2] * colorscale * base;
964 a = texture->currentalpha;
965 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
967 surface = texturesurfacelist[texturesurfaceindex];
968 vertex3f = RSurf_GetVertexPointer(ent, surface);
969 R_Mesh_VertexPointer(vertex3f);
970 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
971 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
978 VectorSubtract(v, modelorg, diff);
979 f = 1 - exp(fogdensity/DotProduct(diff, diff));
980 VectorScale(c, f, c);
982 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
983 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
987 GL_LockArrays(0, surface->mesh.num_vertices);
988 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
994 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
996 GL_Color(1, 1, 1, 1);
997 memset(&m, 0, sizeof(m));
998 m.tex[0] = R_GetTexture(texture->skin.detail);
1000 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1002 surface = texturesurfacelist[texturesurfaceindex];
1003 R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, surface));
1004 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoorddetail2f);
1005 GL_LockArrays(0, surface->mesh.num_vertices);
1006 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1007 GL_LockArrays(0, 0);
1012 // if glow was not already done using multitexture, do it now.
1013 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1014 GL_DepthMask(false);
1015 memset(&m, 0, sizeof(m));
1016 m.tex[0] = R_GetTexture(texture->skin.glow);
1018 m.texmatrix[0] = r_surf_waterscrollmatrix;
1019 m.pointer_color = varray_color4f;
1022 r = ent->colormod[0] * colorscale;
1023 g = ent->colormod[1] * colorscale;
1024 b = ent->colormod[2] * colorscale;
1025 a = texture->currentalpha;
1028 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1030 surface = texturesurfacelist[texturesurfaceindex];
1031 vertex3f = RSurf_GetVertexPointer(ent, surface);
1032 R_Mesh_VertexPointer(vertex3f);
1033 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
1034 R_Mesh_ColorPointer(varray_color4f);
1035 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1037 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1039 VectorSubtract(v, modelorg, diff);
1040 f = 1 - exp(fogdensity/DotProduct(diff, diff));
1044 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
1049 for (i = 0, v = vertex3f, 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));
1059 GL_LockArrays(0, surface->mesh.num_vertices);
1060 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1061 GL_LockArrays(0, 0);
1066 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1068 surface = texturesurfacelist[texturesurfaceindex];
1069 vertex3f = RSurf_GetVertexPointer(ent, surface);
1070 R_Mesh_VertexPointer(vertex3f);
1071 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
1072 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1074 R_Mesh_ColorPointer(varray_color4f);
1075 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1080 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
1085 R_Mesh_ColorPointer(NULL);
1086 GL_Color(r, g, b, a);
1088 GL_LockArrays(0, surface->mesh.num_vertices);
1089 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1090 GL_LockArrays(0, 0);
1096 // if this is opaque use alpha blend which will darken the earlier
1099 // if this is an alpha blended material, all the earlier passes
1100 // were darkened by fog already, so we only need to add the fog
1101 // color ontop through the fog mask texture
1103 // if this is an additive blended material, all the earlier passes
1104 // were darkened by fog already, and we should not add fog color
1105 // (because the background was not darkened, there is no fog color
1106 // that was lost behind it).
1108 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1110 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1111 GL_DepthMask(false);
1112 memset(&m, 0, sizeof(m));
1113 m.tex[0] = R_GetTexture(texture->skin.fog);
1115 m.texmatrix[0] = r_surf_waterscrollmatrix;
1120 a = texture->currentalpha;
1121 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1123 surface = texturesurfacelist[texturesurfaceindex];
1124 vertex3f = RSurf_GetVertexPointer(ent, surface);
1125 R_Mesh_VertexPointer(vertex3f);
1126 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
1127 R_Mesh_ColorPointer(varray_color4f);
1128 //RSurf_FogPassColors_Vertex3f_Color4f(surface->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], texture->currentalpha, 1, surface->mesh.num_vertices, modelorg);
1129 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1131 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1133 VectorSubtract(v, modelorg, diff);
1134 f = exp(fogdensity/DotProduct(diff, diff));
1138 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * f * a;
1143 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1145 VectorSubtract(v, modelorg, diff);
1146 f = exp(fogdensity/DotProduct(diff, diff));
1153 GL_LockArrays(0, surface->mesh.num_vertices);
1154 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1155 GL_LockArrays(0, 0);
1159 if (texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1160 qglEnable(GL_CULL_FACE);
1163 static void RSurfShader_Transparent_Callback(const void *calldata1, int calldata2)
1165 const entity_render_t *ent = calldata1;
1166 const msurface_t *surface = ent->model->brush.data_surfaces + calldata2;
1170 texture = surface->texture;
1171 if (texture->basematerialflags & MATERIALFLAG_SKY)
1172 return; // transparent sky is too difficult
1173 R_UpdateTextureInfo(ent, texture);
1175 R_Mesh_Matrix(&ent->matrix);
1176 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1177 R_DrawSurfaceList(ent, texture, 1, &surface, modelorg);
1180 void R_QueueSurfaceList(entity_render_t *ent, texture_t *texture, int texturenumsurfaces, const msurface_t **texturesurfacelist, const vec3_t modelorg)
1182 int texturesurfaceindex;
1183 const msurface_t *surface;
1184 vec3_t tempcenter, center;
1185 if (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT)
1187 // drawing sky transparently would be too difficult
1188 if (!(texture->currentmaterialflags & MATERIALFLAG_SKY))
1190 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1192 surface = texturesurfacelist[texturesurfaceindex];
1193 tempcenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
1194 tempcenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
1195 tempcenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
1196 Matrix4x4_Transform(&ent->matrix, tempcenter, center);
1197 R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_vieworigin : center, RSurfShader_Transparent_Callback, ent, surface - ent->model->brush.data_surfaces);
1202 R_DrawSurfaceList(ent, texture, texturenumsurfaces, texturesurfacelist, modelorg);
1205 void R_DrawSurfaces(entity_render_t *ent, qboolean skysurfaces)
1207 int i, j, f, flagsmask;
1208 msurface_t *surface, **surfacechain;
1209 texture_t *t, *texture;
1210 model_t *model = ent->model;
1212 const int maxsurfacelist = 1024;
1213 int numsurfacelist = 0;
1214 const msurface_t *surfacelist[1024];
1217 R_Mesh_Matrix(&ent->matrix);
1218 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1220 // update light styles
1223 for (i = 0;i < model->brushq1.light_styles;i++)
1225 if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1227 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1228 if ((surfacechain = model->brushq1.light_styleupdatechains[i]))
1229 for (;(surface = *surfacechain);surfacechain++)
1230 surface->cached_dlight = true;
1235 R_UpdateAllTextureInfo(ent);
1236 flagsmask = skysurfaces ? MATERIALFLAG_SKY : (MATERIALFLAG_WATER | MATERIALFLAG_WALL);
1241 for (i = 0, j = model->firstmodelsurface;i < model->nummodelsurfaces;i++, j++)
1243 if (ent != r_refdef.worldentity || r_worldsurfacevisible[j])
1245 surface = model->brush.data_surfaces + j;
1246 if (t != surface->texture)
1250 R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1253 t = surface->texture;
1254 f = t->currentmaterialflags & flagsmask;
1255 texture = t->currentframe;
1257 if (f && surface->mesh.num_triangles)
1259 // if lightmap parameters changed, rebuild lightmap texture
1260 if (surface->cached_dlight && surface->samples)
1261 R_BuildLightMap(ent, surface);
1262 // add face to draw list
1263 surfacelist[numsurfacelist++] = surface;
1264 if (numsurfacelist >= maxsurfacelist)
1266 R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1273 R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1276 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1281 const mportal_t *portal = calldata1;
1282 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1283 GL_DepthMask(false);
1285 R_Mesh_Matrix(&r_identitymatrix);
1287 memset(&m, 0, sizeof(m));
1288 m.pointer_vertex = varray_vertex3f;
1292 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1293 ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1294 ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1296 if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1298 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1299 VectorCopy(portal->points[i].position, v);
1302 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1303 VectorCopy(portal->points[i].position, v);
1304 GL_LockArrays(0, portal->numpoints);
1305 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1306 GL_LockArrays(0, 0);
1309 // LordHavoc: this is just a nice debugging tool, very slow
1310 static void R_DrawPortals(void)
1315 model_t *model = r_refdef.worldmodel;
1318 for (portalnum = 0, portal = model->brush.data_portals;portalnum < model->brush.num_portals;portalnum++, portal++)
1320 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1321 if (!R_CullBox(portal->mins, portal->maxs))
1323 VectorClear(center);
1324 for (i = 0;i < portal->numpoints;i++)
1325 VectorAdd(center, portal->points[i].position, center);
1326 f = ixtable[portal->numpoints];
1327 VectorScale(center, f, center);
1328 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, portal, portalnum);
1333 static void R_DrawCollisionBrush(colbrushf_t *brush)
1337 memset(&m, 0, sizeof(m));
1338 m.pointer_vertex = brush->points->v;
1340 i = (int)(((size_t)brush) / sizeof(colbrushf_t));
1341 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1342 GL_LockArrays(0, brush->numpoints);
1343 R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1344 GL_LockArrays(0, 0);
1347 static void R_DrawCollisionSurface(entity_render_t *ent, msurface_t *surface)
1351 if (!surface->mesh.num_collisiontriangles)
1353 memset(&m, 0, sizeof(m));
1354 m.pointer_vertex = surface->mesh.data_collisionvertex3f;
1356 i = (int)(((size_t)surface) / sizeof(msurface_t));
1357 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1358 GL_LockArrays(0, surface->mesh.num_collisionvertices);
1359 R_Mesh_Draw(surface->mesh.num_collisionvertices, surface->mesh.num_collisiontriangles, surface->mesh.data_collisionelement3i);
1360 GL_LockArrays(0, 0);
1363 void R_WorldVisibility(void)
1368 model_t *model = r_refdef.worldmodel;
1373 // if possible find the leaf the view origin is in
1374 viewleaf = model->brushq1.PointInLeaf ? model->brushq1.PointInLeaf(model, r_vieworigin) : NULL;
1375 // if possible fetch the visible cluster bits
1376 if (model->brush.FatPVS)
1377 model->brush.FatPVS(model, r_vieworigin, 2, r_pvsbits, sizeof(r_pvsbits));
1379 // clear the visible surface and leaf flags arrays
1380 memset(r_worldsurfacevisible, 0, model->brush.num_surfaces);
1381 memset(r_worldleafvisible, 0, model->brush.num_leafs);
1383 // if the user prefers surfaceworldnode (testing?) or the viewleaf could
1384 // not be found, or the viewleaf is not part of the visible world
1385 // (floating around in the void), use the pvs method
1386 if (r_surfaceworldnode.integer || !viewleaf || viewleaf->clusterindex < 0)
1389 // similar to quake's RecursiveWorldNode but without cache misses
1390 for (j = 0, leaf = model->brush.data_leafs;j < model->brush.num_leafs;j++, leaf++)
1392 // if leaf is in current pvs and on the screen, mark its surfaces
1393 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
1396 r_worldleafvisible[j] = true;
1397 if (leaf->numleafsurfaces)
1398 for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
1399 r_worldsurfacevisible[*mark] = true;
1407 mleaf_t *leafstack[8192];
1409 // follows portals leading outward from viewleaf, does not venture
1410 // offscreen or into leafs that are not visible, faster than Quake's
1411 // RecursiveWorldNode and vastly better in unvised maps, often culls a
1412 // lot of surface that pvs alone would miss
1413 leafstack[0] = viewleaf;
1415 while (leafstackpos)
1418 leaf = leafstack[--leafstackpos];
1419 r_worldleafvisible[leaf - model->brush.data_leafs] = true;
1420 // mark any surfaces bounding this leaf
1421 if (leaf->numleafsurfaces)
1422 for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
1423 r_worldsurfacevisible[*mark] = true;
1424 // follow portals into other leafs
1426 // if viewer is behind portal (portal faces outward into the scene)
1427 // and the portal polygon's bounding box is on the screen
1428 // and the leaf has not been visited yet
1429 // and the leaf is visible in the pvs
1430 // (the first two checks won't cause as many cache misses as the leaf checks)
1431 for (p = leaf->portals;p;p = p->next)
1432 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))
1433 leafstack[leafstackpos++] = p->past;
1437 if (r_drawportals.integer)
1441 void R_Q1BSP_DrawSky(entity_render_t *ent)
1443 if (ent->model == NULL)
1445 if (r_drawcollisionbrushes.integer < 2)
1446 R_DrawSurfaces(ent, true);
1449 void R_Q1BSP_Draw(entity_render_t *ent)
1451 if (ent->model == NULL)
1454 if (r_drawcollisionbrushes.integer < 2)
1455 R_DrawSurfaces(ent, false);
1456 if (r_drawcollisionbrushes.integer >= 1 && ent->model->brush.num_brushes)
1459 model_t *model = ent->model;
1460 msurface_t *surface;
1462 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1463 GL_DepthMask(false);
1465 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
1466 for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
1467 if (brush->colbrushf && brush->colbrushf->numtriangles)
1468 R_DrawCollisionBrush(brush->colbrushf);
1469 for (i = 0, surface = model->brush.data_surfaces + model->firstmodelsurface;i < model->nummodelsurfaces;i++, surface++)
1470 if (surface->mesh.num_collisiontriangles)
1471 R_DrawCollisionSurface(ent, surface);
1472 qglPolygonOffset(0, 0);
1476 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)
1478 model_t *model = ent->model;
1479 vec3_t lightmins, lightmaxs;
1480 int t, leafindex, leafsurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
1483 msurface_t *surface;
1486 lightmins[0] = relativelightorigin[0] - lightradius;
1487 lightmins[1] = relativelightorigin[1] - lightradius;
1488 lightmins[2] = relativelightorigin[2] - lightradius;
1489 lightmaxs[0] = relativelightorigin[0] + lightradius;
1490 lightmaxs[1] = relativelightorigin[1] + lightradius;
1491 lightmaxs[2] = relativelightorigin[2] + lightradius;
1492 *outnumclusterspointer = 0;
1493 *outnumsurfacespointer = 0;
1494 memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
1495 memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
1498 VectorCopy(lightmins, outmins);
1499 VectorCopy(lightmaxs, outmaxs);
1502 VectorCopy(relativelightorigin, outmins);
1503 VectorCopy(relativelightorigin, outmaxs);
1504 if (model->brush.GetPVS)
1505 pvs = model->brush.GetPVS(model, relativelightorigin);
1508 R_UpdateAllTextureInfo(ent);
1509 // FIXME: use BSP recursion as lights are often small
1510 for (leafindex = 0, leaf = model->brush.data_leafs;leafindex < model->brush.num_leafs;leafindex++, leaf++)
1512 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
1514 outmins[0] = min(outmins[0], leaf->mins[0]);
1515 outmins[1] = min(outmins[1], leaf->mins[1]);
1516 outmins[2] = min(outmins[2], leaf->mins[2]);
1517 outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
1518 outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
1519 outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
1522 if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
1524 SETPVSBIT(outclusterpvs, leaf->clusterindex);
1525 outclusterlist[outnumclusters++] = leaf->clusterindex;
1530 for (leafsurfaceindex = 0;leafsurfaceindex < leaf->numleafsurfaces;leafsurfaceindex++)
1532 surfaceindex = leaf->firstleafsurface[leafsurfaceindex];
1533 if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
1535 surface = model->brush.data_surfaces + surfaceindex;
1536 if (BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs))
1537 if ((surface->texture->currentmaterialflags & (MATERIALFLAG_WALL | MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
1539 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
1541 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1542 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1543 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1544 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])))
1546 SETPVSBIT(outsurfacepvs, surfaceindex);
1547 outsurfacelist[outnumsurfaces++] = surfaceindex;
1558 // limit combined leaf box to light boundaries
1559 outmins[0] = max(outmins[0], lightmins[0]);
1560 outmins[1] = max(outmins[1], lightmins[1]);
1561 outmins[2] = max(outmins[2], lightmins[2]);
1562 outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
1563 outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
1564 outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
1566 *outnumclusterspointer = outnumclusters;
1567 *outnumsurfacespointer = outnumsurfaces;
1570 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)
1572 model_t *model = ent->model;
1573 msurface_t *surface;
1574 int surfacelistindex;
1575 if (r_drawcollisionbrushes.integer < 2)
1577 R_Mesh_Matrix(&ent->matrix);
1578 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
1579 if (!r_shadow_compilingrtlight)
1580 R_UpdateAllTextureInfo(ent);
1581 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1583 surface = model->brush.data_surfaces + surfacelist[surfacelistindex];
1584 if ((surface->texture->currentmaterialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT | MATERIALFLAG_WALL)) != MATERIALFLAG_WALL)
1586 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1588 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);
1590 R_Shadow_VolumeFromList(model->brush.shadowmesh->numverts, model->brush.shadowmesh->numtriangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, model->brush.shadowmesh->neighbor3i, relativelightorigin, lightradius + model->radius + r_shadow_projectdistance.value, numshadowmark, shadowmarklist);
1594 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)
1596 model_t *model = ent->model;
1597 msurface_t *surface;
1599 int surfacelistindex;
1600 if (r_drawcollisionbrushes.integer < 2)
1602 R_Mesh_Matrix(&ent->matrix);
1603 if (!r_shadow_compilingrtlight)
1604 R_UpdateAllTextureInfo(ent);
1605 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1607 surface = model->brush.data_surfaces + surfacelist[surfacelistindex];
1608 if (surface->texture->basematerialflags & MATERIALFLAG_NODRAW || !surface->mesh.num_triangles)
1610 if (r_shadow_compilingrtlight)
1612 // if compiling an rtlight, capture the mesh
1613 t = surface->texture;
1614 if ((t->basematerialflags & (MATERIALFLAG_WALL | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
1615 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);
1617 else if (ent != r_refdef.worldentity || r_worldsurfacevisible[surfacelist[surfacelistindex]])
1619 t = surface->texture->currentframe;
1620 // FIXME: transparent surfaces need to be lit later
1621 if ((t->currentmaterialflags & (MATERIALFLAG_WALL | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
1623 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1624 qglDisable(GL_CULL_FACE);
1625 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);
1626 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1627 qglEnable(GL_CULL_FACE);
1635 static void gl_surf_start(void)
1639 static void gl_surf_shutdown(void)
1643 static void gl_surf_newmap(void)
1648 void GL_Surf_Init(void)
1651 Cvar_RegisterVariable(&r_ambient);
1652 Cvar_RegisterVariable(&r_drawportals);
1653 Cvar_RegisterVariable(&r_testvis);
1654 Cvar_RegisterVariable(&r_detailtextures);
1655 Cvar_RegisterVariable(&r_surfaceworldnode);
1656 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
1657 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
1658 Cvar_RegisterVariable(&r_q3bsp_renderskydepth);
1659 Cvar_RegisterVariable(&gl_lightmaps);
1661 //R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);