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", "brighter world cheat (not allowed in multiplayer), value is 0-128"};
29 cvar_t r_drawportals = {0, "r_drawportals", "0", "shows portals (separating polygons) in world interior in quake1 maps"};
30 cvar_t r_lockpvs = {0, "r_lockpvs", "0", "disables pvs switching, allows you to walk around and inspect what is visible from a given location in the map (anything not visible from your current location will not be drawn)"};
31 cvar_t r_lockvisibility = {0, "r_lockvisibility", "0", "disables visibility updates, allows you to walk around and inspect what is visible from a given viewpoint in the map (anything offscreen at the moment this is enabled will not be drawn)"};
32 cvar_t r_useportalculling = {0, "r_useportalculling", "1", "use advanced portal culling visibility method to improve performance over just Potentially Visible Set, provides an even more significant speed improvement in unvised maps"};
33 cvar_t r_drawcollisionbrushes_polygonfactor = {0, "r_drawcollisionbrushes_polygonfactor", "-1", "expands outward the brush polygons a little bit, used to make collision brushes appear infront of walls"};
34 cvar_t r_drawcollisionbrushes_polygonoffset = {0, "r_drawcollisionbrushes_polygonoffset", "0", "nudges brush polygon depth in hardware depth units, used to make collision brushes appear infront of walls"};
35 cvar_t r_q3bsp_renderskydepth = {0, "r_q3bsp_renderskydepth", "0", "draws sky depth masking in q3 maps (as in q1 maps), this means for example that sky polygons can hide other things"};
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 entity_render_t *ent, int surfacenumber, const rtlight_t *rtlight)
299 const mportal_t *portal = (mportal_t *)ent;
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)
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, (entity_render_t *)portal, leafnum, r_shadow_rtlight);
361 static void R_DrawCollisionBrush(colbrushf_t *brush)
365 memset(&m, 0, sizeof(m));
366 m.pointer_vertex = brush->points->v;
368 i = (int)(((size_t)brush) / sizeof(colbrushf_t));
369 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
370 GL_LockArrays(0, brush->numpoints);
371 R_Mesh_Draw(0, brush->numpoints, brush->numtriangles, brush->elements);
375 static void R_DrawCollisionSurface(entity_render_t *ent, msurface_t *surface)
379 if (!surface->num_collisiontriangles)
381 memset(&m, 0, sizeof(m));
382 m.pointer_vertex = surface->data_collisionvertex3f;
384 i = (int)(((size_t)surface) / sizeof(msurface_t));
385 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
386 GL_LockArrays(0, surface->num_collisionvertices);
387 R_Mesh_Draw(0, surface->num_collisionvertices, surface->num_collisiontriangles, surface->data_collisionelement3i);
391 void R_WorldVisibility(void)
396 model_t *model = r_refdef.worldmodel;
401 // if possible find the leaf the view origin is in
402 viewleaf = model->brush.PointInLeaf ? model->brush.PointInLeaf(model, r_vieworigin) : NULL;
403 // if possible fetch the visible cluster bits
404 if (!r_lockpvs.integer && model->brush.FatPVS)
405 model->brush.FatPVS(model, r_vieworigin, 2, r_pvsbits, sizeof(r_pvsbits));
407 if (!r_lockvisibility.integer)
409 // clear the visible surface and leaf flags arrays
410 memset(r_worldsurfacevisible, 0, model->num_surfaces);
411 memset(r_worldleafvisible, 0, model->brush.num_leafs);
413 // if floating around in the void (no pvs data available, and no
414 // portals available), simply use all on-screen leafs.
415 if (!viewleaf || viewleaf->clusterindex < 0)
417 // no visibility method: (used when floating around in the void)
418 // simply cull each leaf to the frustum (view pyramid)
419 // similar to quake's RecursiveWorldNode but without cache misses
420 for (j = 0, leaf = model->brush.data_leafs;j < model->brush.num_leafs;j++, leaf++)
422 // if leaf is in current pvs and on the screen, mark its surfaces
423 if (!R_CullBox(leaf->mins, leaf->maxs))
425 renderstats.world_leafs++;
426 r_worldleafvisible[j] = true;
427 if (leaf->numleafsurfaces)
428 for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
429 r_worldsurfacevisible[*mark] = true;
433 // if the user prefers to disable portal culling (testing?), simply
434 // use all on-screen leafs that are in the pvs.
435 else if (!r_useportalculling.integer)
438 // simply check if each leaf is in the Potentially Visible Set,
439 // and cull to frustum (view pyramid)
440 // similar to quake's RecursiveWorldNode but without cache misses
441 for (j = 0, leaf = model->brush.data_leafs;j < model->brush.num_leafs;j++, leaf++)
443 // if leaf is in current pvs and on the screen, mark its surfaces
444 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
446 renderstats.world_leafs++;
447 r_worldleafvisible[j] = true;
448 if (leaf->numleafsurfaces)
449 for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
450 r_worldsurfacevisible[*mark] = true;
454 // otherwise use a recursive portal flow, culling each portal to
455 // frustum and checking if the leaf the portal leads to is in the pvs
460 mleaf_t *leafstack[8192];
461 // simple-frustum portal method:
462 // follows portals leading outward from viewleaf, does not venture
463 // offscreen or into leafs that are not visible, faster than
464 // Quake's RecursiveWorldNode and vastly better in unvised maps,
465 // often culls some surfaces that pvs alone would miss
466 // (such as a room in pvs that is hidden behind a wall, but the
467 // passage leading to the room is off-screen)
468 leafstack[0] = viewleaf;
472 renderstats.world_leafs++;
473 leaf = leafstack[--leafstackpos];
474 r_worldleafvisible[leaf - model->brush.data_leafs] = true;
475 // mark any surfaces bounding this leaf
476 if (leaf->numleafsurfaces)
477 for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
478 r_worldsurfacevisible[*mark] = true;
479 // follow portals into other leafs
481 // if viewer is behind portal (portal faces outward into the scene)
482 // and the portal polygon's bounding box is on the screen
483 // and the leaf has not been visited yet
484 // and the leaf is visible in the pvs
485 // (the first two checks won't cause as many cache misses as the leaf checks)
486 for (p = leaf->portals;p;p = p->next)
488 renderstats.world_portals++;
489 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))
490 leafstack[leafstackpos++] = p->past;
496 if (r_drawportals.integer)
500 void R_Q1BSP_DrawSky(entity_render_t *ent)
502 if (ent->model == NULL)
504 if (r_drawcollisionbrushes.integer < 2)
505 R_DrawSurfaces(ent, true);
508 void R_Q1BSP_Draw(entity_render_t *ent)
510 model_t *model = ent->model;
513 if (r_drawcollisionbrushes.integer < 2)
514 R_DrawSurfaces(ent, false);
515 if (r_drawcollisionbrushes.integer >= 1 && model->brush.num_brushes)
520 R_Mesh_Matrix(&ent->matrix);
521 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
524 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
525 for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
526 if (brush->colbrushf && brush->colbrushf->numtriangles)
527 R_DrawCollisionBrush(brush->colbrushf);
528 for (i = 0, surface = model->data_surfaces + model->firstmodelsurface;i < model->nummodelsurfaces;i++, surface++)
529 if (surface->num_collisiontriangles)
530 R_DrawCollisionSurface(ent, surface);
531 qglPolygonOffset(0, 0);
535 typedef struct r_q1bsp_getlightinfo_s
538 vec3_t relativelightorigin;
541 unsigned char *outleafpvs;
544 unsigned char *outsurfacepvs;
550 const unsigned char *pvs;
552 r_q1bsp_getlightinfo_t;
554 void R_Q1BSP_RecursiveGetLightInfo(r_q1bsp_getlightinfo_t *info, mnode_t *node)
560 mplane_t *plane = node->plane;
561 //if (!BoxesOverlap(info->lightmins, info->lightmaxs, node->mins, node->maxs))
566 sides = ((info->lightmaxs[plane->type] >= plane->dist) | ((info->lightmins[plane->type] < plane->dist) << 1));
568 sides = BoxOnPlaneSide(info->lightmins, info->lightmaxs, plane);
571 R_Q1BSP_RecursiveGetLightInfo(info, node->children[0]);
572 node = node->children[1];
575 return; // ERROR: NAN bounding box!
577 node = node->children[sides - 1];
579 leaf = (mleaf_t *)node;
580 if (info->pvs == NULL || CHECKPVSBIT(info->pvs, leaf->clusterindex))
582 info->outmins[0] = min(info->outmins[0], leaf->mins[0]);
583 info->outmins[1] = min(info->outmins[1], leaf->mins[1]);
584 info->outmins[2] = min(info->outmins[2], leaf->mins[2]);
585 info->outmaxs[0] = max(info->outmaxs[0], leaf->maxs[0]);
586 info->outmaxs[1] = max(info->outmaxs[1], leaf->maxs[1]);
587 info->outmaxs[2] = max(info->outmaxs[2], leaf->maxs[2]);
588 if (info->outleafpvs)
590 int leafindex = leaf - info->model->brush.data_leafs;
591 if (!CHECKPVSBIT(info->outleafpvs, leafindex))
593 SETPVSBIT(info->outleafpvs, leafindex);
594 info->outleaflist[info->outnumleafs++] = leafindex;
597 if (info->outsurfacepvs)
599 int leafsurfaceindex;
600 for (leafsurfaceindex = 0;leafsurfaceindex < leaf->numleafsurfaces;leafsurfaceindex++)
602 int surfaceindex = leaf->firstleafsurface[leafsurfaceindex];
603 if (!CHECKPVSBIT(info->outsurfacepvs, surfaceindex))
605 msurface_t *surface = info->model->data_surfaces + surfaceindex;
606 if (BoxesOverlap(info->lightmins, info->lightmaxs, surface->mins, surface->maxs))
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)) || (ent->flags & RENDER_NOCULLFACE))
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)) || (ent->flags & RENDER_NOCULLFACE))
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 static void R_Q1BSP_DrawLight_TransparentCallback(const entity_render_t *ent, int surfacenumber, const rtlight_t *rtlight)
773 msurface_t *surface = ent->model->data_surfaces + surfacenumber;
774 texture_t *texture = surface->texture;
775 R_UpdateTextureInfo(ent, texture);
776 texture = texture->currentframe;
777 R_Shadow_RenderMode_Begin();
778 R_Shadow_RenderMode_ActiveLight((rtlight_t *)rtlight);
779 R_Shadow_RenderMode_Lighting(false, true);
780 R_Shadow_SetupEntityLight(ent);
781 R_Shadow_RenderSurfacesLighting(ent, texture, 1, &surface);
782 R_Shadow_RenderMode_End();
785 static void R_Q1BSP_DrawLight_TransparentBatch(const entity_render_t *ent, texture_t *texture, int batchnumsurfaces, msurface_t **batchsurfacelist)
787 int batchsurfaceindex;
788 msurface_t *batchsurface;
789 vec3_t tempcenter, center;
790 for (batchsurfaceindex = 0;batchsurfaceindex < batchnumsurfaces;batchsurfaceindex++)
792 batchsurface = batchsurfacelist[batchsurfaceindex];
793 tempcenter[0] = (batchsurface->mins[0] + batchsurface->maxs[0]) * 0.5f;
794 tempcenter[1] = (batchsurface->mins[1] + batchsurface->maxs[1]) * 0.5f;
795 tempcenter[2] = (batchsurface->mins[2] + batchsurface->maxs[2]) * 0.5f;
796 Matrix4x4_Transform(&ent->matrix, tempcenter, center);
797 R_MeshQueue_AddTransparent(texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST ? r_vieworigin : center, R_Q1BSP_DrawLight_TransparentCallback, ent, batchsurface - ent->model->data_surfaces, r_shadow_rtlight);
801 #define RSURF_MAX_BATCHSURFACES 1024
803 void R_Q1BSP_DrawLight(entity_render_t *ent, int numsurfaces, const int *surfacelist)
805 model_t *model = ent->model;
808 int surfacelistindex, batchnumsurfaces;
809 msurface_t *batchsurfacelist[RSURF_MAX_BATCHSURFACES];
813 if (r_drawcollisionbrushes.integer >= 2)
815 R_UpdateAllTextureInfo(ent);
816 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
820 batchnumsurfaces = 0;
821 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
823 if ((ent == r_refdef.worldentity && !r_worldsurfacevisible[surfacelist[surfacelistindex]]))
825 surface = model->data_surfaces + surfacelist[surfacelistindex];
826 renderstats.lights_lighttriangles += surface->num_triangles;
827 if (tex != surface->texture)
829 if (batchnumsurfaces > 0)
831 if (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT)
832 R_Q1BSP_DrawLight_TransparentBatch(ent, texture, batchnumsurfaces, batchsurfacelist);
834 R_Shadow_RenderSurfacesLighting(ent, texture, batchnumsurfaces, batchsurfacelist);
835 batchnumsurfaces = 0;
837 tex = surface->texture;
838 texture = surface->texture->currentframe;
839 skip = (texture->currentmaterialflags & MATERIALFLAG_SKY) != 0;
843 if (!skip && surface->num_triangles)
845 if (batchnumsurfaces == RSURF_MAX_BATCHSURFACES)
847 if (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT)
848 R_Q1BSP_DrawLight_TransparentBatch(ent, texture, batchnumsurfaces, batchsurfacelist);
850 R_Shadow_RenderSurfacesLighting(ent, texture, batchnumsurfaces, batchsurfacelist);
851 batchnumsurfaces = 0;
853 batchsurfacelist[batchnumsurfaces++] = surface;
856 if (batchnumsurfaces > 0)
858 if (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT)
859 R_Q1BSP_DrawLight_TransparentBatch(ent, texture, batchnumsurfaces, batchsurfacelist);
861 R_Shadow_RenderSurfacesLighting(ent, texture, batchnumsurfaces, batchsurfacelist);
862 batchnumsurfaces = 0;
864 qglEnable(GL_CULL_FACE);
868 void R_ReplaceWorldTexture (void)
873 const char *r, *newt;
874 m = r_refdef.worldmodel;
878 Con_Print("r_replacemaptexture <texname> <newtexname> - replaces texture\n");
879 Con_Print("r_replacemaptexture <texname> - switch back to default texture\n");
882 if(!cl.islocalgame || !cl.worldmodel)
884 Con_Print("This command works only in singleplayer\n");
891 for(i=0,t=m->data_textures;i<m->num_textures;i++,t++)
893 if(t->width && !strcasecmp(t->name, r))
895 if(Mod_LoadSkinFrame(&t->skin, (char*)newt, TEXF_MIPMAP | TEXF_ALPHA | TEXF_PRECACHE | TEXF_PICMIP, false, r_fullbrights.integer))
897 Con_Printf("%s replaced with %s\n", r, newt);
902 Con_Printf("%s was not found\n", newt);
903 Mod_LoadSkinFrame(&t->skin, (char*)r, TEXF_MIPMAP | TEXF_ALPHA | TEXF_PRECACHE | TEXF_PICMIP, false, r_fullbrights.integer);//back to default
911 void R_ListWorldTextures (void)
916 m = r_refdef.worldmodel;
918 Con_Print("Worldmodel textures :\n");
919 for(i=0,t=m->data_textures;i<m->num_textures;i++,t++)
920 if(t->skin.base != r_texture_notexture)
921 Con_Printf("%s\n", t->name);
925 static void gl_surf_start(void)
929 static void gl_surf_shutdown(void)
933 static void gl_surf_newmap(void)
938 void GL_Surf_Init(void)
941 Cvar_RegisterVariable(&r_ambient);
942 Cvar_RegisterVariable(&r_drawportals);
943 Cvar_RegisterVariable(&r_lockpvs);
944 Cvar_RegisterVariable(&r_lockvisibility);
945 Cvar_RegisterVariable(&r_useportalculling);
946 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
947 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
948 Cvar_RegisterVariable(&r_q3bsp_renderskydepth);
950 Cmd_AddCommand ("r_replacemaptexture", R_ReplaceWorldTexture, "override a map texture for testing purposes"); // By [515]
951 Cmd_AddCommand ("r_listmaptextures", R_ListWorldTextures, "list all textures used by the current map"); // By [515]
953 //R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);