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);
652 GL_LockArrays(0, surface->mesh.num_vertices);
653 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
660 memset(&m, 0, sizeof(m));
661 m.tex[0] = R_GetTexture(texture->skin.base);
663 m.texmatrix[0] = r_surf_waterscrollmatrix;
664 m.texmatrix[0] = r_surf_waterscrollmatrix;
665 m.pointer_color = varray_color4f;
667 if (gl_combine.integer)
669 m.texrgbscale[0] = 4;
673 r = ent->colormod[0] * colorscale;
674 g = ent->colormod[1] * colorscale;
675 b = ent->colormod[2] * colorscale;
676 a = texture->currentalpha;
679 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
681 surface = texturesurfacelist[texturesurfaceindex];
682 vertex3f = RSurf_GetVertexPointer(ent, surface);
683 R_Mesh_VertexPointer(vertex3f);
684 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
685 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
690 if (surface->styles[0] != 255)
692 if (surface->mesh.data_lightmapcolor4f)
694 float scale = d_lightstylevalue[surface->styles[0]] * (1.0f / 128.0f);
695 VectorMA(c, scale, surface->mesh.data_lightmapcolor4f + i*4, c);
697 else if (surface->mesh.data_lightmapoffsets)
699 const qbyte *lm = surface->samples + surface->mesh.data_lightmapoffsets[i];
700 float scale = d_lightstylevalue[surface->styles[0]] * (1.0f / 32768.0f);
701 VectorMA(c, scale, lm, c);
702 if (surface->styles[1] != 255)
704 int size3 = ((surface->extents[0]>>4)+1)*((surface->extents[1]>>4)+1)*3;
706 scale = d_lightstylevalue[surface->styles[1]] * (1.0f / 32768.0f);
707 VectorMA(c, scale, lm, c);
708 if (surface->styles[2] != 255)
711 scale = d_lightstylevalue[surface->styles[2]] * (1.0f / 32768.0f);
712 VectorMA(c, scale, lm, c);
713 if (surface->styles[3] != 255)
716 scale = d_lightstylevalue[surface->styles[3]] * (1.0f / 32768.0f);
717 VectorMA(c, scale, lm, c);
728 VectorSubtract(v, modelorg, diff);
729 f = 1 - exp(fogdensity/DotProduct(diff, diff));
730 VectorScale(c, f, c);
732 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
733 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
737 GL_LockArrays(0, surface->mesh.num_vertices);
738 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
746 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
748 surface = texturesurfacelist[texturesurfaceindex];
749 vertex3f = RSurf_GetVertexPointer(ent, surface);
750 R_Mesh_VertexPointer(vertex3f);
751 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
752 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
754 R_Mesh_ColorPointer(varray_color4f);
755 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
757 VectorSubtract(v, modelorg, diff);
758 f = 1 - exp(fogdensity/DotProduct(diff, diff));
762 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
767 R_Mesh_ColorPointer(varray_color4f);
768 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
770 VectorSubtract(v, modelorg, diff);
771 f = 1 - exp(fogdensity/DotProduct(diff, diff));
779 GL_LockArrays(0, surface->mesh.num_vertices);
780 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
786 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
788 surface = texturesurfacelist[texturesurfaceindex];
789 vertex3f = RSurf_GetVertexPointer(ent, surface);
790 R_Mesh_VertexPointer(vertex3f);
791 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
792 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
794 R_Mesh_ColorPointer(varray_color4f);
795 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
800 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
805 R_Mesh_ColorPointer(NULL);
806 GL_Color(r, g, b, a);
809 GL_LockArrays(0, surface->mesh.num_vertices);
810 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
819 if (!dolightmap && dobase)
823 GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], 1);
824 memset(&m, 0, sizeof(m));
825 m.tex[0] = R_GetTexture(texture->skin.base);
827 m.texmatrix[0] = r_surf_waterscrollmatrix;
829 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
831 surface = texturesurfacelist[texturesurfaceindex];
832 R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, surface));
833 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
834 GL_LockArrays(0, surface->mesh.num_vertices);
835 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
839 if (r_lightmapintensity <= 0 && dolightmap && dobase)
843 GL_Color(0, 0, 0, 1);
844 memset(&m, 0, sizeof(m));
846 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
848 surface = texturesurfacelist[texturesurfaceindex];
849 R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, surface));
850 GL_LockArrays(0, surface->mesh.num_vertices);
851 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
855 if (r_textureunits.integer >= 2 && gl_combine.integer && dolightmap && dobase)
857 // dualtexture combine
858 GL_BlendFunc(GL_ONE, GL_ZERO);
862 memset(&m, 0, sizeof(m));
863 m.tex[1] = R_GetTexture(texture->skin.base);
865 m.texmatrix[1] = r_surf_waterscrollmatrix;
866 m.texrgbscale[1] = 2;
868 r = ent->colormod[0] * r_lightmapintensity;
869 g = ent->colormod[1] * r_lightmapintensity;
870 b = ent->colormod[2] * r_lightmapintensity;
871 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
873 surface = texturesurfacelist[texturesurfaceindex];
874 R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, surface));
875 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordlightmap2f);
876 R_Mesh_TexCoordPointer(1, 2, surface->mesh.data_texcoordtexture2f);
877 if (surface->lightmaptexture)
879 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
880 R_Mesh_ColorPointer(NULL);
881 GL_Color(r, g, b, 1);
883 else if (r == 1 && g == 1 && b == 1)
885 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
886 R_Mesh_ColorPointer(surface->mesh.data_lightmapcolor4f);
890 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
891 R_Mesh_ColorPointer(varray_color4f);
892 for (i = 0;i < surface->mesh.num_vertices;i++)
894 varray_color4f[i*4+0] = surface->mesh.data_lightmapcolor4f[i*4+0] * r;
895 varray_color4f[i*4+1] = surface->mesh.data_lightmapcolor4f[i*4+1] * g;
896 varray_color4f[i*4+2] = surface->mesh.data_lightmapcolor4f[i*4+2] * b;
897 varray_color4f[i*4+3] = surface->mesh.data_lightmapcolor4f[i*4+3];
900 GL_LockArrays(0, surface->mesh.num_vertices);
901 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
908 GL_BlendFunc(GL_ONE, GL_ZERO);
910 GL_Color(1, 1, 1, 1);
911 memset(&m, 0, sizeof(m));
913 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
915 surface = texturesurfacelist[texturesurfaceindex];
916 R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, surface));
917 R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
918 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordlightmap2f);
919 R_Mesh_ColorPointer(surface->lightmaptexture ? NULL : surface->mesh.data_lightmapcolor4f);
920 GL_LockArrays(0, surface->mesh.num_vertices);
921 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
927 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
929 GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
930 memset(&m, 0, sizeof(m));
931 m.tex[0] = R_GetTexture(texture->skin.base);
933 m.texmatrix[0] = r_surf_waterscrollmatrix;
935 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
937 surface = texturesurfacelist[texturesurfaceindex];
938 R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, surface));
939 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
940 GL_LockArrays(0, surface->mesh.num_vertices);
941 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
949 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
951 memset(&m, 0, sizeof(m));
952 m.tex[0] = R_GetTexture(texture->skin.base);
954 m.texmatrix[0] = r_surf_waterscrollmatrix;
955 m.pointer_color = varray_color4f;
957 if (gl_combine.integer)
959 m.texrgbscale[0] = 4;
963 base = r_ambient.value * (1.0f / 64.0f);
964 r = ent->colormod[0] * colorscale * base;
965 g = ent->colormod[1] * colorscale * base;
966 b = ent->colormod[2] * colorscale * base;
967 a = texture->currentalpha;
968 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
970 surface = texturesurfacelist[texturesurfaceindex];
971 vertex3f = RSurf_GetVertexPointer(ent, surface);
972 R_Mesh_VertexPointer(vertex3f);
973 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
974 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
981 VectorSubtract(v, modelorg, diff);
982 f = 1 - exp(fogdensity/DotProduct(diff, diff));
983 VectorScale(c, f, c);
985 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
986 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
990 GL_LockArrays(0, surface->mesh.num_vertices);
991 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
997 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
999 GL_Color(1, 1, 1, 1);
1000 memset(&m, 0, sizeof(m));
1001 m.tex[0] = R_GetTexture(texture->skin.detail);
1003 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1005 surface = texturesurfacelist[texturesurfaceindex];
1006 R_Mesh_VertexPointer(RSurf_GetVertexPointer(ent, surface));
1007 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoorddetail2f);
1008 GL_LockArrays(0, surface->mesh.num_vertices);
1009 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1010 GL_LockArrays(0, 0);
1015 // if glow was not already done using multitexture, do it now.
1016 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1017 GL_DepthMask(false);
1018 memset(&m, 0, sizeof(m));
1019 m.tex[0] = R_GetTexture(texture->skin.glow);
1021 m.texmatrix[0] = r_surf_waterscrollmatrix;
1022 m.pointer_color = varray_color4f;
1025 r = ent->colormod[0] * colorscale;
1026 g = ent->colormod[1] * colorscale;
1027 b = ent->colormod[2] * colorscale;
1028 a = texture->currentalpha;
1031 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1033 surface = texturesurfacelist[texturesurfaceindex];
1034 vertex3f = RSurf_GetVertexPointer(ent, surface);
1035 R_Mesh_VertexPointer(vertex3f);
1036 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
1037 R_Mesh_ColorPointer(varray_color4f);
1038 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1040 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1042 VectorSubtract(v, modelorg, diff);
1043 f = 1 - exp(fogdensity/DotProduct(diff, diff));
1047 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
1052 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1054 VectorSubtract(v, modelorg, diff);
1055 f = 1 - exp(fogdensity/DotProduct(diff, diff));
1062 GL_LockArrays(0, surface->mesh.num_vertices);
1063 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1064 GL_LockArrays(0, 0);
1069 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1071 surface = texturesurfacelist[texturesurfaceindex];
1072 vertex3f = RSurf_GetVertexPointer(ent, surface);
1073 R_Mesh_VertexPointer(vertex3f);
1074 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
1075 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1077 R_Mesh_ColorPointer(varray_color4f);
1078 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1083 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * a;
1088 R_Mesh_ColorPointer(NULL);
1089 GL_Color(r, g, b, a);
1091 GL_LockArrays(0, surface->mesh.num_vertices);
1092 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1093 GL_LockArrays(0, 0);
1099 // if this is opaque use alpha blend which will darken the earlier
1102 // if this is an alpha blended material, all the earlier passes
1103 // were darkened by fog already, so we only need to add the fog
1104 // color ontop through the fog mask texture
1106 // if this is an additive blended material, all the earlier passes
1107 // were darkened by fog already, and we should not add fog color
1108 // (because the background was not darkened, there is no fog color
1109 // that was lost behind it).
1111 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1113 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1114 GL_DepthMask(false);
1115 memset(&m, 0, sizeof(m));
1116 m.tex[0] = R_GetTexture(texture->skin.fog);
1118 m.texmatrix[0] = r_surf_waterscrollmatrix;
1123 a = texture->currentalpha;
1124 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1126 surface = texturesurfacelist[texturesurfaceindex];
1127 vertex3f = RSurf_GetVertexPointer(ent, surface);
1128 R_Mesh_VertexPointer(vertex3f);
1129 R_Mesh_TexCoordPointer(0, 2, surface->mesh.data_texcoordtexture2f);
1130 R_Mesh_ColorPointer(varray_color4f);
1131 //RSurf_FogPassColors_Vertex3f_Color4f(surface->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], texture->currentalpha, 1, surface->mesh.num_vertices, modelorg);
1132 if (surface->mesh.data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
1134 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1136 VectorSubtract(v, modelorg, diff);
1137 f = exp(fogdensity/DotProduct(diff, diff));
1141 c[3] = surface->mesh.data_lightmapcolor4f[i*4+3] * f * a;
1146 for (i = 0, v = vertex3f, c = varray_color4f;i < surface->mesh.num_vertices;i++, v += 3, c += 4)
1148 VectorSubtract(v, modelorg, diff);
1149 f = exp(fogdensity/DotProduct(diff, diff));
1156 GL_LockArrays(0, surface->mesh.num_vertices);
1157 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1158 GL_LockArrays(0, 0);
1162 if (texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1163 qglEnable(GL_CULL_FACE);
1166 static void RSurfShader_Transparent_Callback(const void *calldata1, int calldata2)
1168 const entity_render_t *ent = calldata1;
1169 const msurface_t *surface = ent->model->brush.data_surfaces + calldata2;
1173 texture = surface->texture;
1174 if (texture->basematerialflags & MATERIALFLAG_SKY)
1175 return; // transparent sky is too difficult
1176 R_UpdateTextureInfo(ent, texture);
1178 R_Mesh_Matrix(&ent->matrix);
1179 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1180 R_DrawSurfaceList(ent, texture, 1, &surface, modelorg);
1183 void R_QueueSurfaceList(entity_render_t *ent, texture_t *texture, int texturenumsurfaces, const msurface_t **texturesurfacelist, const vec3_t modelorg)
1185 int texturesurfaceindex;
1186 const msurface_t *surface;
1187 vec3_t tempcenter, center;
1188 if (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT)
1190 // drawing sky transparently would be too difficult
1191 if (!(texture->currentmaterialflags & MATERIALFLAG_SKY))
1193 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1195 surface = texturesurfacelist[texturesurfaceindex];
1196 tempcenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
1197 tempcenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
1198 tempcenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
1199 Matrix4x4_Transform(&ent->matrix, tempcenter, center);
1200 R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_vieworigin : center, RSurfShader_Transparent_Callback, ent, surface - ent->model->brush.data_surfaces);
1205 R_DrawSurfaceList(ent, texture, texturenumsurfaces, texturesurfacelist, modelorg);
1208 void R_DrawSurfaces(entity_render_t *ent, qboolean skysurfaces)
1210 int i, j, f, flagsmask;
1211 msurface_t *surface, **surfacechain;
1212 texture_t *t, *texture;
1213 model_t *model = ent->model;
1215 const int maxsurfacelist = 1024;
1216 int numsurfacelist = 0;
1217 const msurface_t *surfacelist[1024];
1220 R_Mesh_Matrix(&ent->matrix);
1221 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1223 // update light styles
1226 for (i = 0;i < model->brushq1.light_styles;i++)
1228 if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1230 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1231 if ((surfacechain = model->brushq1.light_styleupdatechains[i]))
1232 for (;(surface = *surfacechain);surfacechain++)
1233 surface->cached_dlight = true;
1238 R_UpdateAllTextureInfo(ent);
1239 flagsmask = skysurfaces ? MATERIALFLAG_SKY : (MATERIALFLAG_WATER | MATERIALFLAG_WALL);
1244 for (i = 0, j = model->firstmodelsurface;i < model->nummodelsurfaces;i++, j++)
1246 if (ent != r_refdef.worldentity || r_worldsurfacevisible[j])
1248 surface = model->brush.data_surfaces + j;
1249 if (t != surface->texture)
1253 R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1256 t = surface->texture;
1257 f = t->currentmaterialflags & flagsmask;
1258 texture = t->currentframe;
1260 if (f && surface->mesh.num_triangles)
1262 // if lightmap parameters changed, rebuild lightmap texture
1263 if (surface->cached_dlight && surface->samples)
1264 R_BuildLightMap(ent, surface);
1265 // add face to draw list
1266 surfacelist[numsurfacelist++] = surface;
1267 if (numsurfacelist >= maxsurfacelist)
1269 R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1276 R_QueueSurfaceList(ent, texture, numsurfacelist, surfacelist, modelorg);
1279 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1284 const mportal_t *portal = calldata1;
1285 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1286 GL_DepthMask(false);
1288 R_Mesh_Matrix(&r_identitymatrix);
1290 memset(&m, 0, sizeof(m));
1291 m.pointer_vertex = varray_vertex3f;
1295 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1296 ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1297 ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1299 if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1301 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1302 VectorCopy(portal->points[i].position, v);
1305 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1306 VectorCopy(portal->points[i].position, v);
1307 GL_LockArrays(0, portal->numpoints);
1308 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1309 GL_LockArrays(0, 0);
1312 // LordHavoc: this is just a nice debugging tool, very slow
1313 static void R_DrawPortals(void)
1318 model_t *model = r_refdef.worldmodel;
1321 for (portalnum = 0, portal = model->brush.data_portals;portalnum < model->brush.num_portals;portalnum++, portal++)
1323 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1324 if (!R_CullBox(portal->mins, portal->maxs))
1326 VectorClear(center);
1327 for (i = 0;i < portal->numpoints;i++)
1328 VectorAdd(center, portal->points[i].position, center);
1329 f = ixtable[portal->numpoints];
1330 VectorScale(center, f, center);
1331 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, portal, portalnum);
1336 static void R_DrawCollisionBrush(colbrushf_t *brush)
1340 memset(&m, 0, sizeof(m));
1341 m.pointer_vertex = brush->points->v;
1343 i = (int)(((size_t)brush) / sizeof(colbrushf_t));
1344 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1345 GL_LockArrays(0, brush->numpoints);
1346 R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1347 GL_LockArrays(0, 0);
1350 static void R_DrawCollisionSurface(entity_render_t *ent, msurface_t *surface)
1354 if (!surface->mesh.num_collisiontriangles)
1356 memset(&m, 0, sizeof(m));
1357 m.pointer_vertex = surface->mesh.data_collisionvertex3f;
1359 i = (int)(((size_t)surface) / sizeof(msurface_t));
1360 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1361 GL_LockArrays(0, surface->mesh.num_collisionvertices);
1362 R_Mesh_Draw(surface->mesh.num_collisionvertices, surface->mesh.num_collisiontriangles, surface->mesh.data_collisionelement3i);
1363 GL_LockArrays(0, 0);
1366 void R_WorldVisibility(void)
1371 model_t *model = r_refdef.worldmodel;
1376 // if possible find the leaf the view origin is in
1377 viewleaf = model->brushq1.PointInLeaf ? model->brushq1.PointInLeaf(model, r_vieworigin) : NULL;
1378 // if possible fetch the visible cluster bits
1379 if (model->brush.FatPVS)
1380 model->brush.FatPVS(model, r_vieworigin, 2, r_pvsbits, sizeof(r_pvsbits));
1382 // clear the visible surface and leaf flags arrays
1383 memset(r_worldsurfacevisible, 0, model->brush.num_surfaces);
1384 memset(r_worldleafvisible, 0, model->brush.num_leafs);
1386 // if the user prefers surfaceworldnode (testing?) or the viewleaf could
1387 // not be found, or the viewleaf is not part of the visible world
1388 // (floating around in the void), use the pvs method
1389 if (r_surfaceworldnode.integer || !viewleaf || viewleaf->clusterindex < 0)
1392 // similar to quake's RecursiveWorldNode but without cache misses
1393 for (j = 0, leaf = model->brush.data_leafs;j < model->brush.num_leafs;j++, leaf++)
1395 // if leaf is in current pvs and on the screen, mark its surfaces
1396 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
1399 r_worldleafvisible[j] = true;
1400 if (leaf->numleafsurfaces)
1401 for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
1402 r_worldsurfacevisible[*mark] = true;
1410 mleaf_t *leafstack[8192];
1412 // follows portals leading outward from viewleaf, does not venture
1413 // offscreen or into leafs that are not visible, faster than Quake's
1414 // RecursiveWorldNode and vastly better in unvised maps, often culls a
1415 // lot of surface that pvs alone would miss
1416 leafstack[0] = viewleaf;
1418 while (leafstackpos)
1421 leaf = leafstack[--leafstackpos];
1422 r_worldleafvisible[leaf - model->brush.data_leafs] = true;
1423 // mark any surfaces bounding this leaf
1424 if (leaf->numleafsurfaces)
1425 for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
1426 r_worldsurfacevisible[*mark] = true;
1427 // follow portals into other leafs
1429 // if viewer is behind portal (portal faces outward into the scene)
1430 // and the portal polygon's bounding box is on the screen
1431 // and the leaf has not been visited yet
1432 // and the leaf is visible in the pvs
1433 // (the first two checks won't cause as many cache misses as the leaf checks)
1434 for (p = leaf->portals;p;p = p->next)
1435 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))
1436 leafstack[leafstackpos++] = p->past;
1440 if (r_drawportals.integer)
1444 void R_Q1BSP_DrawSky(entity_render_t *ent)
1446 if (ent->model == NULL)
1448 if (r_drawcollisionbrushes.integer < 2)
1449 R_DrawSurfaces(ent, true);
1452 void R_Q1BSP_Draw(entity_render_t *ent)
1454 if (ent->model == NULL)
1457 if (r_drawcollisionbrushes.integer < 2)
1458 R_DrawSurfaces(ent, false);
1459 if (r_drawcollisionbrushes.integer >= 1 && ent->model->brush.num_brushes)
1462 model_t *model = ent->model;
1463 msurface_t *surface;
1465 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1466 GL_DepthMask(false);
1468 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
1469 for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
1470 if (brush->colbrushf && brush->colbrushf->numtriangles)
1471 R_DrawCollisionBrush(brush->colbrushf);
1472 for (i = 0, surface = model->brush.data_surfaces + model->firstmodelsurface;i < model->nummodelsurfaces;i++, surface++)
1473 if (surface->mesh.num_collisiontriangles)
1474 R_DrawCollisionSurface(ent, surface);
1475 qglPolygonOffset(0, 0);
1479 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)
1481 model_t *model = ent->model;
1482 vec3_t lightmins, lightmaxs;
1483 int t, leafindex, leafsurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
1486 msurface_t *surface;
1489 lightmins[0] = relativelightorigin[0] - lightradius;
1490 lightmins[1] = relativelightorigin[1] - lightradius;
1491 lightmins[2] = relativelightorigin[2] - lightradius;
1492 lightmaxs[0] = relativelightorigin[0] + lightradius;
1493 lightmaxs[1] = relativelightorigin[1] + lightradius;
1494 lightmaxs[2] = relativelightorigin[2] + lightradius;
1495 *outnumclusterspointer = 0;
1496 *outnumsurfacespointer = 0;
1497 memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
1498 memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
1501 VectorCopy(lightmins, outmins);
1502 VectorCopy(lightmaxs, outmaxs);
1505 VectorCopy(relativelightorigin, outmins);
1506 VectorCopy(relativelightorigin, outmaxs);
1507 if (model->brush.GetPVS)
1508 pvs = model->brush.GetPVS(model, relativelightorigin);
1511 R_UpdateAllTextureInfo(ent);
1512 // FIXME: use BSP recursion as lights are often small
1513 for (leafindex = 0, leaf = model->brush.data_leafs;leafindex < model->brush.num_leafs;leafindex++, leaf++)
1515 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
1517 outmins[0] = min(outmins[0], leaf->mins[0]);
1518 outmins[1] = min(outmins[1], leaf->mins[1]);
1519 outmins[2] = min(outmins[2], leaf->mins[2]);
1520 outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
1521 outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
1522 outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
1525 if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
1527 SETPVSBIT(outclusterpvs, leaf->clusterindex);
1528 outclusterlist[outnumclusters++] = leaf->clusterindex;
1533 for (leafsurfaceindex = 0;leafsurfaceindex < leaf->numleafsurfaces;leafsurfaceindex++)
1535 surfaceindex = leaf->firstleafsurface[leafsurfaceindex];
1536 if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
1538 surface = model->brush.data_surfaces + surfaceindex;
1539 if (BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs))
1540 if ((surface->texture->currentmaterialflags & (MATERIALFLAG_WALL | MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
1542 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
1544 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1545 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1546 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1547 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])))
1549 SETPVSBIT(outsurfacepvs, surfaceindex);
1550 outsurfacelist[outnumsurfaces++] = surfaceindex;
1561 // limit combined leaf box to light boundaries
1562 outmins[0] = max(outmins[0], lightmins[0]);
1563 outmins[1] = max(outmins[1], lightmins[1]);
1564 outmins[2] = max(outmins[2], lightmins[2]);
1565 outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
1566 outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
1567 outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
1569 *outnumclusterspointer = outnumclusters;
1570 *outnumsurfacespointer = outnumsurfaces;
1573 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)
1575 model_t *model = ent->model;
1576 msurface_t *surface;
1577 int surfacelistindex;
1578 if (r_drawcollisionbrushes.integer < 2)
1580 R_Mesh_Matrix(&ent->matrix);
1581 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
1582 if (!r_shadow_compilingrtlight)
1583 R_UpdateAllTextureInfo(ent);
1584 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1586 surface = model->brush.data_surfaces + surfacelist[surfacelistindex];
1587 if ((surface->texture->currentmaterialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT | MATERIALFLAG_WALL)) != MATERIALFLAG_WALL)
1589 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1591 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);
1593 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);
1597 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)
1599 model_t *model = ent->model;
1600 msurface_t *surface;
1602 int surfacelistindex;
1603 if (r_drawcollisionbrushes.integer < 2)
1605 R_Mesh_Matrix(&ent->matrix);
1606 if (!r_shadow_compilingrtlight)
1607 R_UpdateAllTextureInfo(ent);
1608 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1610 surface = model->brush.data_surfaces + surfacelist[surfacelistindex];
1611 if (surface->texture->basematerialflags & MATERIALFLAG_NODRAW || !surface->mesh.num_triangles)
1613 if (r_shadow_compilingrtlight)
1615 // if compiling an rtlight, capture the mesh
1616 t = surface->texture;
1617 if ((t->basematerialflags & (MATERIALFLAG_WALL | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
1618 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);
1620 else if (ent != r_refdef.worldentity || r_worldsurfacevisible[surfacelist[surfacelistindex]])
1622 t = surface->texture->currentframe;
1623 // FIXME: transparent surfaces need to be lit later
1624 if ((t->currentmaterialflags & (MATERIALFLAG_WALL | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
1626 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1627 qglDisable(GL_CULL_FACE);
1628 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);
1629 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1630 qglEnable(GL_CULL_FACE);
1638 static void gl_surf_start(void)
1642 static void gl_surf_shutdown(void)
1646 static void gl_surf_newmap(void)
1651 void GL_Surf_Init(void)
1654 Cvar_RegisterVariable(&r_ambient);
1655 Cvar_RegisterVariable(&r_drawportals);
1656 Cvar_RegisterVariable(&r_testvis);
1657 Cvar_RegisterVariable(&r_detailtextures);
1658 Cvar_RegisterVariable(&r_surfaceworldnode);
1659 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
1660 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
1661 Cvar_RegisterVariable(&r_q3bsp_renderskydepth);
1662 Cvar_RegisterVariable(&gl_lightmaps);
1664 //R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);