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
26 #define MAX_LIGHTMAP_SIZE 256
28 cvar_t r_ambient = {0, "r_ambient", "0"};
29 cvar_t r_drawportals = {0, "r_drawportals", "0"};
30 cvar_t r_lockpvs = {0, "r_lockpvs", "0"};
31 cvar_t r_lockvisibility = {0, "r_lockvisibility", "0"};
32 cvar_t r_useportalculling = {0, "r_useportalculling", "0"};
33 cvar_t r_drawcollisionbrushes_polygonfactor = {0, "r_drawcollisionbrushes_polygonfactor", "-1"};
34 cvar_t r_drawcollisionbrushes_polygonoffset = {0, "r_drawcollisionbrushes_polygonoffset", "0"};
35 cvar_t r_q3bsp_renderskydepth = {0, "r_q3bsp_renderskydepth", "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 unsigned char r_pvsbits[(32768+7)>>3];
41 // TODO: dynamic resize according to r_refdef.worldmodel->brush.num_leafs
42 unsigned char r_worldleafvisible[32768];
43 // TODO: dynamic resize according to r_refdef.worldmodel->num_surfaces
44 unsigned char r_worldsurfacevisible[262144];
50 Combine and scale multiple lightmaps into the 8.8 format in blocklights
53 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 unsigned char *lightmap, *out, *stain;
58 static unsigned int intblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
59 static unsigned char templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
61 // update cached lighting info
62 surface->cached_dlight = 0;
64 smax = (surface->lightmapinfo->extents[0]>>4)+1;
65 tmax = (surface->lightmapinfo->extents[1]>>4)+1;
68 lightmap = surface->lightmapinfo->samples;
70 // set to full bright if no light data
72 if (!ent->model->brushq1.lightdata)
74 for (i = 0;i < size3;i++)
80 memset(bl, 0, size*3*sizeof(unsigned int));
82 // add all the lightmaps
86 for (maps = 0;maps < MAXLIGHTMAPS && surface->lightmapinfo->styles[maps] != 255;maps++, lightmap += size3)
87 for (scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[maps]], i = 0;i < size3;i++)
88 bl[i] += lightmap[i] * scale;
92 stain = surface->lightmapinfo->stainsamples;
95 // the >> 16 shift adjusts down 8 bits to account for the stainmap
96 // scaling, and remaps the 0-65536 (2x overbright) to 0-256, it will
97 // be doubled during rendering to achieve 2x overbright
98 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
99 if (ent->model->brushq1.lightmaprgba)
101 stride = (surface->lightmapinfo->lightmaptexturestride - smax) * 4;
102 for (i = 0;i < tmax;i++, out += stride)
104 for (j = 0;j < smax;j++)
106 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
107 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
108 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
115 stride = (surface->lightmapinfo->lightmaptexturestride - smax) * 3;
116 for (i = 0;i < tmax;i++, out += stride)
118 for (j = 0;j < smax;j++)
120 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
121 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
122 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
127 R_UpdateTexture(surface->lightmaptexture, templight);
130 void R_StainNode (mnode_t *node, model_t *model, const vec3_t origin, float radius, const float fcolor[8])
132 float ndist, a, ratio, maxdist, maxdist2, maxdist3, invradius, sdtable[256], td, dist2;
133 msurface_t *surface, *endsurface;
134 int i, s, t, smax, tmax, smax3, impacts, impactt, stained;
138 maxdist = radius * radius;
139 invradius = 1.0f / radius;
144 ndist = PlaneDiff(origin, node->plane);
147 node = node->children[0];
152 node = node->children[1];
156 dist2 = ndist * ndist;
157 maxdist3 = maxdist - dist2;
159 if (node->plane->type < 3)
161 VectorCopy(origin, impact);
162 impact[node->plane->type] -= ndist;
166 impact[0] = origin[0] - node->plane->normal[0] * ndist;
167 impact[1] = origin[1] - node->plane->normal[1] * ndist;
168 impact[2] = origin[2] - node->plane->normal[2] * ndist;
171 for (surface = model->data_surfaces + node->firstsurface, endsurface = surface + node->numsurfaces;surface < endsurface;surface++)
173 if (surface->lightmapinfo->stainsamples)
175 smax = (surface->lightmapinfo->extents[0] >> 4) + 1;
176 tmax = (surface->lightmapinfo->extents[1] >> 4) + 1;
178 impacts = DotProduct (impact, surface->lightmapinfo->texinfo->vecs[0]) + surface->lightmapinfo->texinfo->vecs[0][3] - surface->lightmapinfo->texturemins[0];
179 impactt = DotProduct (impact, surface->lightmapinfo->texinfo->vecs[1]) + surface->lightmapinfo->texinfo->vecs[1][3] - surface->lightmapinfo->texturemins[1];
181 s = bound(0, impacts, smax * 16) - impacts;
182 t = bound(0, impactt, tmax * 16) - impactt;
183 i = s * s + t * t + dist2;
187 // reduce calculations
188 for (s = 0, i = impacts; s < smax; s++, i -= 16)
189 sdtable[s] = i * i + dist2;
191 bl = surface->lightmapinfo->stainsamples;
196 for (t = 0;t < tmax;t++, i -= 16)
199 // make sure some part of it is visible on this line
202 maxdist2 = maxdist - td;
203 for (s = 0;s < smax;s++)
205 if (sdtable[s] < maxdist2)
207 ratio = lhrandom(0.0f, 1.0f);
208 a = (fcolor[3] + ratio * fcolor[7]) * (1.0f - sqrt(sdtable[s] + td) * invradius);
209 if (a >= (1.0f / 64.0f))
213 bl[0] = (unsigned char) ((float) bl[0] + a * ((fcolor[0] + ratio * fcolor[4]) - (float) bl[0]));
214 bl[1] = (unsigned char) ((float) bl[1] + a * ((fcolor[1] + ratio * fcolor[5]) - (float) bl[1]));
215 bl[2] = (unsigned char) ((float) bl[2] + a * ((fcolor[2] + ratio * fcolor[6]) - (float) bl[2]));
225 // force lightmap upload
227 surface->cached_dlight = true;
231 if (node->children[0]->plane)
233 if (node->children[1]->plane)
235 R_StainNode(node->children[0], model, origin, radius, fcolor);
236 node = node->children[1];
241 node = node->children[0];
245 else if (node->children[1]->plane)
247 node = node->children[1];
252 void R_Stain (const vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2)
256 entity_render_t *ent;
259 if (r_refdef.worldmodel == NULL || !r_refdef.worldmodel->brush.data_nodes || !r_refdef.worldmodel->brushq1.lightdata)
264 fcolor[3] = ca1 * (1.0f / 64.0f);
265 fcolor[4] = cr2 - cr1;
266 fcolor[5] = cg2 - cg1;
267 fcolor[6] = cb2 - cb1;
268 fcolor[7] = (ca2 - ca1) * (1.0f / 64.0f);
270 R_StainNode(r_refdef.worldmodel->brush.data_nodes + r_refdef.worldmodel->brushq1.hulls[0].firstclipnode, r_refdef.worldmodel, origin, radius, fcolor);
272 // look for embedded bmodels
273 for (n = 0;n < cl_num_brushmodel_entities;n++)
275 ent = &cl_entities[cl_brushmodel_entities[n]].render;
277 if (model && model->name[0] == '*')
279 if (model->brush.data_nodes)
281 Matrix4x4_Transform(&ent->inversematrix, origin, org);
282 R_StainNode(model->brush.data_nodes + model->brushq1.hulls[0].firstclipnode, model, org, radius, fcolor);
290 =============================================================
294 =============================================================
297 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
302 const mportal_t *portal = (mportal_t *)calldata1;
303 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
306 R_Mesh_Matrix(&r_identitymatrix);
308 memset(&m, 0, sizeof(m));
309 m.pointer_vertex = varray_vertex3f;
313 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
314 ((i & 0x0038) >> 3) * (1.0f / 7.0f),
315 ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
317 if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
319 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
320 VectorCopy(portal->points[i].position, v);
323 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
324 VectorCopy(portal->points[i].position, v);
325 GL_LockArrays(0, portal->numpoints);
326 R_Mesh_Draw(0, portal->numpoints, portal->numpoints - 2, polygonelements);
330 // LordHavoc: this is just a nice debugging tool, very slow
331 static void R_DrawPortals(void)
333 int i, leafnum;//, portalnum;
336 model_t *model = r_refdef.worldmodel;
339 for (leafnum = 0;leafnum < r_refdef.worldmodel->brush.num_leafs;leafnum++)
341 if (r_worldleafvisible[leafnum])
343 //for (portalnum = 0, portal = model->brush.data_portals;portalnum < model->brush.num_portals;portalnum++, portal++)
344 for (portal = r_refdef.worldmodel->brush.data_leafs[leafnum].portals;portal;portal = portal->next)
346 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
347 if (!R_CullBox(portal->mins, portal->maxs))
350 for (i = 0;i < portal->numpoints;i++)
351 VectorAdd(center, portal->points[i].position, center);
352 f = ixtable[portal->numpoints];
353 VectorScale(center, f, center);
354 //R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, portal, portalnum);
355 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, portal, leafnum);
362 static void R_DrawCollisionBrush(colbrushf_t *brush)
366 memset(&m, 0, sizeof(m));
367 m.pointer_vertex = brush->points->v;
369 i = (int)(((size_t)brush) / sizeof(colbrushf_t));
370 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
371 GL_LockArrays(0, brush->numpoints);
372 R_Mesh_Draw(0, brush->numpoints, brush->numtriangles, brush->elements);
376 static void R_DrawCollisionSurface(entity_render_t *ent, msurface_t *surface)
380 if (!surface->num_collisiontriangles)
382 memset(&m, 0, sizeof(m));
383 m.pointer_vertex = surface->data_collisionvertex3f;
385 i = (int)(((size_t)surface) / sizeof(msurface_t));
386 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
387 GL_LockArrays(0, surface->num_collisionvertices);
388 R_Mesh_Draw(0, surface->num_collisionvertices, surface->num_collisiontriangles, surface->data_collisionelement3i);
392 void R_WorldVisibility(void)
397 model_t *model = r_refdef.worldmodel;
402 // if possible find the leaf the view origin is in
403 viewleaf = model->brush.PointInLeaf ? model->brush.PointInLeaf(model, r_vieworigin) : NULL;
404 // if possible fetch the visible cluster bits
405 if (!r_lockpvs.integer && model->brush.FatPVS)
406 model->brush.FatPVS(model, r_vieworigin, 2, r_pvsbits, sizeof(r_pvsbits));
408 if (!r_lockvisibility.integer)
410 // clear the visible surface and leaf flags arrays
411 memset(r_worldsurfacevisible, 0, model->num_surfaces);
412 memset(r_worldleafvisible, 0, model->brush.num_leafs);
414 // if floating around in the void (no pvs data available, and no
415 // portals available), simply use all on-screen leafs.
416 if (!viewleaf || viewleaf->clusterindex < 0)
418 // no visibility method: (used when floating around in the void)
419 // simply cull each leaf to the frustum (view pyramid)
420 // similar to quake's RecursiveWorldNode but without cache misses
421 for (j = 0, leaf = model->brush.data_leafs;j < model->brush.num_leafs;j++, leaf++)
423 // if leaf is in current pvs and on the screen, mark its surfaces
424 if (!R_CullBox(leaf->mins, leaf->maxs))
426 renderstats.world_leafs++;
427 r_worldleafvisible[j] = true;
428 if (leaf->numleafsurfaces)
429 for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
430 r_worldsurfacevisible[*mark] = true;
434 // if the user prefers to disable portal culling (testing?), simply
435 // use all on-screen leafs that are in the pvs.
436 else if (!r_useportalculling.integer)
439 // simply check if each leaf is in the Potentially Visible Set,
440 // and cull to frustum (view pyramid)
441 // similar to quake's RecursiveWorldNode but without cache misses
442 for (j = 0, leaf = model->brush.data_leafs;j < model->brush.num_leafs;j++, leaf++)
444 // if leaf is in current pvs and on the screen, mark its surfaces
445 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
447 renderstats.world_leafs++;
448 r_worldleafvisible[j] = true;
449 if (leaf->numleafsurfaces)
450 for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
451 r_worldsurfacevisible[*mark] = true;
455 // otherwise use a recursive portal flow, culling each portal to
456 // frustum and checking if the leaf the portal leads to is in the pvs
461 mleaf_t *leafstack[8192];
462 // simple-frustum portal method:
463 // follows portals leading outward from viewleaf, does not venture
464 // offscreen or into leafs that are not visible, faster than
465 // Quake's RecursiveWorldNode and vastly better in unvised maps,
466 // often culls some surfaces that pvs alone would miss
467 // (such as a room in pvs that is hidden behind a wall, but the
468 // passage leading to the room is off-screen)
469 leafstack[0] = viewleaf;
473 renderstats.world_leafs++;
474 leaf = leafstack[--leafstackpos];
475 r_worldleafvisible[leaf - model->brush.data_leafs] = true;
476 // mark any surfaces bounding this leaf
477 if (leaf->numleafsurfaces)
478 for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
479 r_worldsurfacevisible[*mark] = true;
480 // follow portals into other leafs
482 // if viewer is behind portal (portal faces outward into the scene)
483 // and the portal polygon's bounding box is on the screen
484 // and the leaf has not been visited yet
485 // and the leaf is visible in the pvs
486 // (the first two checks won't cause as many cache misses as the leaf checks)
487 for (p = leaf->portals;p;p = p->next)
489 renderstats.world_portals++;
490 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))
491 leafstack[leafstackpos++] = p->past;
497 if (r_drawportals.integer)
501 void R_Q1BSP_DrawSky(entity_render_t *ent)
503 if (ent->model == NULL)
505 if (r_drawcollisionbrushes.integer < 2)
506 R_DrawSurfaces(ent, true);
509 void R_Q1BSP_Draw(entity_render_t *ent)
511 model_t *model = ent->model;
514 if (r_drawcollisionbrushes.integer < 2)
515 R_DrawSurfaces(ent, false);
516 if (r_drawcollisionbrushes.integer >= 1 && model->brush.num_brushes)
521 R_Mesh_Matrix(&ent->matrix);
522 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
525 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
526 for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
527 if (brush->colbrushf && brush->colbrushf->numtriangles)
528 R_DrawCollisionBrush(brush->colbrushf);
529 for (i = 0, surface = model->data_surfaces + model->firstmodelsurface;i < model->nummodelsurfaces;i++, surface++)
530 if (surface->num_collisiontriangles)
531 R_DrawCollisionSurface(ent, surface);
532 qglPolygonOffset(0, 0);
536 typedef struct r_q1bsp_getlightinfo_s
539 vec3_t relativelightorigin;
542 unsigned char *outleafpvs;
545 unsigned char *outsurfacepvs;
551 const unsigned char *pvs;
553 r_q1bsp_getlightinfo_t;
555 void R_Q1BSP_RecursiveGetLightInfo(r_q1bsp_getlightinfo_t *info, mnode_t *node)
561 mplane_t *plane = node->plane;
562 //if (!BoxesOverlap(info->lightmins, info->lightmaxs, node->mins, node->maxs))
567 sides = ((info->lightmaxs[plane->type] >= plane->dist) | ((info->lightmins[plane->type] < plane->dist) << 1));
569 sides = BoxOnPlaneSide(info->lightmins, info->lightmaxs, plane);
572 R_Q1BSP_RecursiveGetLightInfo(info, node->children[0]);
573 node = node->children[1];
576 node = node->children[sides - 1];
578 leaf = (mleaf_t *)node;
579 if (info->pvs == NULL || CHECKPVSBIT(info->pvs, leaf->clusterindex))
581 info->outmins[0] = min(info->outmins[0], leaf->mins[0]);
582 info->outmins[1] = min(info->outmins[1], leaf->mins[1]);
583 info->outmins[2] = min(info->outmins[2], leaf->mins[2]);
584 info->outmaxs[0] = max(info->outmaxs[0], leaf->maxs[0]);
585 info->outmaxs[1] = max(info->outmaxs[1], leaf->maxs[1]);
586 info->outmaxs[2] = max(info->outmaxs[2], leaf->maxs[2]);
587 if (info->outleafpvs)
589 int leafindex = leaf - info->model->brush.data_leafs;
590 if (!CHECKPVSBIT(info->outleafpvs, leafindex))
592 SETPVSBIT(info->outleafpvs, leafindex);
593 info->outleaflist[info->outnumleafs++] = leafindex;
596 if (info->outsurfacepvs)
598 int leafsurfaceindex;
599 for (leafsurfaceindex = 0;leafsurfaceindex < leaf->numleafsurfaces;leafsurfaceindex++)
601 int surfaceindex = leaf->firstleafsurface[leafsurfaceindex];
602 if (!CHECKPVSBIT(info->outsurfacepvs, surfaceindex))
604 msurface_t *surface = info->model->data_surfaces + surfaceindex;
605 if (BoxesOverlap(info->lightmins, info->lightmaxs, surface->mins, surface->maxs))
606 if ((surface->texture->currentmaterialflags & (MATERIALFLAG_WALL | MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
608 int triangleindex, t;
611 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = info->model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->num_triangles;triangleindex++, t++, e += 3)
613 v[0] = info->model->brush.shadowmesh->vertex3f + e[0] * 3;
614 v[1] = info->model->brush.shadowmesh->vertex3f + e[1] * 3;
615 v[2] = info->model->brush.shadowmesh->vertex3f + e[2] * 3;
616 if (PointInfrontOfTriangle(info->relativelightorigin, v[0], v[1], v[2]) && info->lightmaxs[0] > min(v[0][0], min(v[1][0], v[2][0])) && info->lightmins[0] < max(v[0][0], max(v[1][0], v[2][0])) && info->lightmaxs[1] > min(v[0][1], min(v[1][1], v[2][1])) && info->lightmins[1] < max(v[0][1], max(v[1][1], v[2][1])) && info->lightmaxs[2] > min(v[0][2], min(v[1][2], v[2][2])) && info->lightmins[2] < max(v[0][2], max(v[1][2], v[2][2])))
618 SETPVSBIT(info->outsurfacepvs, surfaceindex);
619 info->outsurfacelist[info->outnumsurfaces++] = surfaceindex;
630 void R_Q1BSP_GetLightInfo(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, vec3_t outmins, vec3_t outmaxs, int *outleaflist, unsigned char *outleafpvs, int *outnumleafspointer, int *outsurfacelist, unsigned char *outsurfacepvs, int *outnumsurfacespointer)
632 r_q1bsp_getlightinfo_t info;
633 VectorCopy(relativelightorigin, info.relativelightorigin);
634 info.lightradius = lightradius;
635 info.lightmins[0] = info.relativelightorigin[0] - info.lightradius;
636 info.lightmins[1] = info.relativelightorigin[1] - info.lightradius;
637 info.lightmins[2] = info.relativelightorigin[2] - info.lightradius;
638 info.lightmaxs[0] = info.relativelightorigin[0] + info.lightradius;
639 info.lightmaxs[1] = info.relativelightorigin[1] + info.lightradius;
640 info.lightmaxs[2] = info.relativelightorigin[2] + info.lightradius;
641 if (ent->model == NULL)
643 VectorCopy(info.lightmins, outmins);
644 VectorCopy(info.lightmaxs, outmaxs);
645 *outnumleafspointer = 0;
646 *outnumsurfacespointer = 0;
649 info.model = ent->model;
650 info.outleaflist = outleaflist;
651 info.outleafpvs = outleafpvs;
652 info.outnumleafs = 0;
653 info.outsurfacelist = outsurfacelist;
654 info.outsurfacepvs = outsurfacepvs;
655 info.outnumsurfaces = 0;
656 VectorCopy(info.relativelightorigin, info.outmins);
657 VectorCopy(info.relativelightorigin, info.outmaxs);
658 memset(outleafpvs, 0, (info.model->brush.num_leafs + 7) >> 3);
659 memset(outsurfacepvs, 0, (info.model->nummodelsurfaces + 7) >> 3);
660 if (info.model->brush.GetPVS)
661 info.pvs = info.model->brush.GetPVS(info.model, info.relativelightorigin);
664 R_UpdateAllTextureInfo(ent);
665 if (r_shadow_compilingrtlight)
667 // use portal recursion for exact light volume culling, and exact surface checking
668 Portal_Visibility(info.model, info.relativelightorigin, info.outleaflist, info.outleafpvs, &info.outnumleafs, info.outsurfacelist, info.outsurfacepvs, &info.outnumsurfaces, NULL, 0, true, info.lightmins, info.lightmaxs, info.outmins, info.outmaxs);
670 else if (r_shadow_realtime_dlight_portalculling.integer)
672 // use portal recursion for exact light volume culling, but not the expensive exact surface checking
673 Portal_Visibility(info.model, info.relativelightorigin, info.outleaflist, info.outleafpvs, &info.outnumleafs, info.outsurfacelist, info.outsurfacepvs, &info.outnumsurfaces, NULL, 0, r_shadow_realtime_dlight_portalculling.integer >= 2, info.lightmins, info.lightmaxs, info.outmins, info.outmaxs);
677 // use BSP recursion as lights are often small
678 R_Q1BSP_RecursiveGetLightInfo(&info, info.model->brush.data_nodes);
681 // limit combined leaf box to light boundaries
682 outmins[0] = max(info.outmins[0] - 1, info.lightmins[0]);
683 outmins[1] = max(info.outmins[1] - 1, info.lightmins[1]);
684 outmins[2] = max(info.outmins[2] - 1, info.lightmins[2]);
685 outmaxs[0] = min(info.outmaxs[0] + 1, info.lightmaxs[0]);
686 outmaxs[1] = min(info.outmaxs[1] + 1, info.lightmaxs[1]);
687 outmaxs[2] = min(info.outmaxs[2] + 1, info.lightmaxs[2]);
689 *outnumleafspointer = info.outnumleafs;
690 *outnumsurfacespointer = info.outnumsurfaces;
693 void R_Q1BSP_CompileShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
695 model_t *model = ent->model;
697 int surfacelistindex;
698 float projectdistance = lightradius + model->radius*2 + r_shadow_projectdistance.value;
700 r_shadow_compilingrtlight->static_meshchain_shadow = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768, 32768, NULL, NULL, NULL, false, false, true);
701 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
702 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
704 surface = model->data_surfaces + surfacelist[surfacelistindex];
705 texture = surface->texture;
706 if ((texture->basematerialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT | MATERIALFLAG_WALL)) != MATERIALFLAG_WALL)
708 if (texture->textureflags & (Q3TEXTUREFLAG_TWOSIDED | Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
710 R_Shadow_MarkVolumeFromBox(surface->num_firstshadowmeshtriangle, surface->num_triangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, relativelightorigin, r_shadow_compilingrtlight->cullmins, r_shadow_compilingrtlight->cullmaxs, surface->mins, surface->maxs);
712 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 + projectdistance, numshadowmark, shadowmarklist);
713 r_shadow_compilingrtlight->static_meshchain_shadow = Mod_ShadowMesh_Finish(r_shadow_mempool, r_shadow_compilingrtlight->static_meshchain_shadow, false, false);
716 extern float *rsurface_vertex3f;
717 extern float *rsurface_svector3f;
718 extern float *rsurface_tvector3f;
719 extern float *rsurface_normal3f;
720 extern void RSurf_SetVertexPointer(const entity_render_t *ent, const texture_t *texture, const msurface_t *surface, const vec3_t modelorg);
722 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)
724 model_t *model = ent->model;
726 int surfacelistindex;
727 float projectdistance = lightradius + model->radius*2 + r_shadow_projectdistance.value;
730 // check the box in modelspace, it was already checked in worldspace
731 if (!BoxesOverlap(ent->model->normalmins, ent->model->normalmaxs, lightmins, lightmaxs))
733 if (r_drawcollisionbrushes.integer >= 2)
735 R_UpdateAllTextureInfo(ent);
736 if (model->brush.shadowmesh)
738 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
739 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
741 surface = model->data_surfaces + surfacelist[surfacelistindex];
742 texture = surface->texture->currentframe;
743 if ((texture->currentmaterialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT | MATERIALFLAG_WALL)) != MATERIALFLAG_WALL)
745 if (texture->textureflags & (Q3TEXTUREFLAG_TWOSIDED | Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
747 R_Shadow_MarkVolumeFromBox(surface->num_firstshadowmeshtriangle, surface->num_triangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, relativelightorigin, lightmins, lightmaxs, surface->mins, surface->maxs);
749 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 + projectdistance, numshadowmark, shadowmarklist);
753 projectdistance = lightradius + ent->model->radius*2;
754 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
755 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
757 surface = model->data_surfaces + surfacelist[surfacelistindex];
758 // FIXME: get current skin
759 texture = surface->texture;//R_FetchAliasSkin(ent, surface->groupmesh);
760 if (texture->currentmaterialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT) || !surface->num_triangles)
762 RSurf_SetVertexPointer(ent, texture, surface, modelorg);
763 // identify lit faces within the bounding box
764 R_Shadow_PrepareShadowMark(surface->groupmesh->num_triangles);
765 R_Shadow_MarkVolumeFromBox(surface->num_firsttriangle, surface->num_triangles, rsurface_vertex3f, surface->groupmesh->data_element3i, relativelightorigin, lightmins, lightmaxs, surface->mins, surface->maxs);
766 R_Shadow_VolumeFromList(surface->groupmesh->num_vertices, surface->groupmesh->num_triangles, rsurface_vertex3f, surface->groupmesh->data_element3i, surface->groupmesh->data_neighbor3i, relativelightorigin, projectdistance, numshadowmark, shadowmarklist);
771 #define RSURF_MAX_BATCHSURFACES 1024
773 void R_Q1BSP_DrawLight(entity_render_t *ent, float *lightcolorbase, float *lightcolorpants, float *lightcolorshirt, int numsurfaces, const int *surfacelist)
775 model_t *model = ent->model;
778 int surfacelistindex, batchnumsurfaces;
779 msurface_t *batchsurfacelist[RSURF_MAX_BATCHSURFACES];
782 rtexture_t *basetexture = NULL;
783 rtexture_t *glosstexture = NULL;
784 float specularscale = 0;
786 if (r_drawcollisionbrushes.integer >= 2)
788 if (VectorLength2(lightcolorbase) + VectorLength2(lightcolorpants) + VectorLength2(lightcolorshirt) < 0.0001)
790 R_UpdateAllTextureInfo(ent);
791 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
795 batchnumsurfaces = 0;
796 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
798 if ((ent == r_refdef.worldentity && !r_worldsurfacevisible[surfacelist[surfacelistindex]]))
800 surface = model->data_surfaces + surfacelist[surfacelistindex];
801 renderstats.lights_lighttriangles += surface->num_triangles;
802 if (tex != surface->texture)
804 if (batchnumsurfaces > 0)
806 R_Shadow_RenderSurfacesLighting(ent, texture, batchnumsurfaces, batchsurfacelist, lightcolorbase, lightcolorpants, lightcolorshirt, basetexture, texture->skin.pants, texture->skin.shirt, texture->skin.nmap, glosstexture, specularscale, modelorg);
807 batchnumsurfaces = 0;
809 tex = surface->texture;
810 texture = surface->texture->currentframe;
811 // FIXME: transparent surfaces need to be lit later
812 skip = (texture->currentmaterialflags & (MATERIALFLAG_WALL | MATERIALFLAG_TRANSPARENT)) != MATERIALFLAG_WALL;
815 if (texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
816 qglDisable(GL_CULL_FACE);
818 qglEnable(GL_CULL_FACE);
819 glosstexture = r_texture_black;
821 if (texture->skin.gloss)
823 if (r_shadow_gloss.integer >= 1 && r_shadow_glossintensity.value > 0 && r_shadow_rtlight->specularscale > 0)
825 glosstexture = texture->skin.gloss;
826 specularscale = r_shadow_rtlight->specularscale * r_shadow_glossintensity.value;
831 if (r_shadow_gloss.integer >= 2 && r_shadow_gloss2intensity.value > 0 && r_shadow_glossintensity.value > 0 && r_shadow_rtlight->specularscale > 0)
833 glosstexture = r_texture_white;
834 specularscale = r_shadow_rtlight->specularscale * r_shadow_gloss2intensity.value;
837 basetexture = (ent->colormap < 0 && texture->skin.merged) ? texture->skin.merged : texture->skin.base;
838 if ((r_shadow_rtlight->ambientscale + r_shadow_rtlight->diffusescale) * (VectorLength2(lightcolorbase) + VectorLength2(lightcolorpants) + VectorLength2(lightcolorshirt)) + specularscale * VectorLength2(lightcolorbase) < (1.0f / 1048576.0f))
841 if (!skip && surface->num_triangles)
843 if (batchnumsurfaces == RSURF_MAX_BATCHSURFACES)
845 R_Shadow_RenderSurfacesLighting(ent, texture, batchnumsurfaces, batchsurfacelist, lightcolorbase, lightcolorpants, lightcolorshirt, basetexture, texture->skin.pants, texture->skin.shirt, texture->skin.nmap, glosstexture, specularscale, modelorg);
846 batchnumsurfaces = 0;
848 batchsurfacelist[batchnumsurfaces++] = surface;
851 if (batchnumsurfaces > 0)
853 R_Shadow_RenderSurfacesLighting(ent, texture, batchnumsurfaces, batchsurfacelist, lightcolorbase, lightcolorpants, lightcolorshirt, basetexture, texture->skin.pants, texture->skin.shirt, texture->skin.nmap, glosstexture, specularscale, modelorg);
854 batchnumsurfaces = 0;
856 qglEnable(GL_CULL_FACE);
860 static void gl_surf_start(void)
864 static void gl_surf_shutdown(void)
868 static void gl_surf_newmap(void)
873 void GL_Surf_Init(void)
876 Cvar_RegisterVariable(&r_ambient);
877 Cvar_RegisterVariable(&r_drawportals);
878 Cvar_RegisterVariable(&r_lockpvs);
879 Cvar_RegisterVariable(&r_lockvisibility);
880 Cvar_RegisterVariable(&r_useportalculling);
881 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
882 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
883 Cvar_RegisterVariable(&r_q3bsp_renderskydepth);
885 //R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);