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_testvis = {0, "r_testvis", "0"};
31 cvar_t r_detailtextures = {CVAR_SAVE, "r_detailtextures", "1"};
32 cvar_t r_surfaceworldnode = {0, "r_surfaceworldnode", "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 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->num_surfaces
44 qbyte 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 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->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 = d_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] = (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 || !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 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 void R_DrawPortal_Callback(const void *calldata1, int calldata2)
303 const mportal_t *portal = calldata1;
304 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
307 R_Mesh_Matrix(&r_identitymatrix);
309 memset(&m, 0, sizeof(m));
310 m.pointer_vertex = varray_vertex3f;
314 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
315 ((i & 0x0038) >> 3) * (1.0f / 7.0f),
316 ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
318 if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
320 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
321 VectorCopy(portal->points[i].position, v);
324 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
325 VectorCopy(portal->points[i].position, v);
326 GL_LockArrays(0, portal->numpoints);
327 R_Mesh_Draw(0, portal->numpoints, portal->numpoints - 2, polygonelements);
331 // LordHavoc: this is just a nice debugging tool, very slow
332 static void R_DrawPortals(void)
334 int i, leafnum;//, portalnum;
337 model_t *model = r_refdef.worldmodel;
340 for (leafnum = 0;leafnum < r_refdef.worldmodel->brush.num_leafs;leafnum++)
342 if (r_worldleafvisible[leafnum])
344 //for (portalnum = 0, portal = model->brush.data_portals;portalnum < model->brush.num_portals;portalnum++, portal++)
345 for (portal = r_refdef.worldmodel->brush.data_leafs[leafnum].portals;portal;portal = portal->next)
347 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
348 if (!R_CullBox(portal->mins, portal->maxs))
351 for (i = 0;i < portal->numpoints;i++)
352 VectorAdd(center, portal->points[i].position, center);
353 f = ixtable[portal->numpoints];
354 VectorScale(center, f, center);
355 //R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, portal, portalnum);
356 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, portal, leafnum);
363 static void R_DrawCollisionBrush(colbrushf_t *brush)
367 memset(&m, 0, sizeof(m));
368 m.pointer_vertex = brush->points->v;
370 i = (int)(((size_t)brush) / sizeof(colbrushf_t));
371 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
372 GL_LockArrays(0, brush->numpoints);
373 R_Mesh_Draw(0, brush->numpoints, brush->numtriangles, brush->elements);
377 static void R_DrawCollisionSurface(entity_render_t *ent, msurface_t *surface)
381 if (!surface->num_collisiontriangles)
383 memset(&m, 0, sizeof(m));
384 m.pointer_vertex = surface->data_collisionvertex3f;
386 i = (int)(((size_t)surface) / sizeof(msurface_t));
387 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
388 GL_LockArrays(0, surface->num_collisionvertices);
389 R_Mesh_Draw(0, surface->num_collisionvertices, surface->num_collisiontriangles, surface->data_collisionelement3i);
393 void R_WorldVisibility(void)
398 model_t *model = r_refdef.worldmodel;
403 // if possible find the leaf the view origin is in
404 viewleaf = model->brush.PointInLeaf ? model->brush.PointInLeaf(model, r_vieworigin) : NULL;
405 // if possible fetch the visible cluster bits
406 if (model->brush.FatPVS)
407 model->brush.FatPVS(model, r_vieworigin, 2, r_pvsbits, sizeof(r_pvsbits));
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 the user prefers surfaceworldnode (testing?) or the viewleaf could
414 // not be found, or the viewleaf is not part of the visible world
415 // (floating around in the void), use the pvs method
416 if (r_surfaceworldnode.integer || !viewleaf || viewleaf->clusterindex < 0)
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 (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
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;
437 mleaf_t *leafstack[8192];
439 // follows portals leading outward from viewleaf, does not venture
440 // offscreen or into leafs that are not visible, faster than Quake's
441 // RecursiveWorldNode and vastly better in unvised maps, often culls a
442 // lot of surface that pvs alone would miss
443 leafstack[0] = viewleaf;
448 leaf = leafstack[--leafstackpos];
449 r_worldleafvisible[leaf - model->brush.data_leafs] = true;
450 // mark any surfaces bounding this leaf
451 if (leaf->numleafsurfaces)
452 for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
453 r_worldsurfacevisible[*mark] = true;
454 // follow portals into other leafs
456 // if viewer is behind portal (portal faces outward into the scene)
457 // and the portal polygon's bounding box is on the screen
458 // and the leaf has not been visited yet
459 // and the leaf is visible in the pvs
460 // (the first two checks won't cause as many cache misses as the leaf checks)
461 for (p = leaf->portals;p;p = p->next)
462 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))
463 leafstack[leafstackpos++] = p->past;
467 if (r_drawportals.integer)
471 void R_Q1BSP_DrawSky(entity_render_t *ent)
473 if (ent->model == NULL)
475 if (r_drawcollisionbrushes.integer < 2)
476 R_DrawSurfaces(ent, true);
479 void R_Q1BSP_Draw(entity_render_t *ent)
481 if (ent->model == NULL)
484 if (r_drawcollisionbrushes.integer < 2)
485 R_DrawSurfaces(ent, false);
486 if (r_drawcollisionbrushes.integer >= 1 && ent->model->brush.num_brushes)
489 model_t *model = ent->model;
492 R_Mesh_Matrix(&ent->matrix);
493 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
496 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
497 for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
498 if (brush->colbrushf && brush->colbrushf->numtriangles)
499 R_DrawCollisionBrush(brush->colbrushf);
500 for (i = 0, surface = model->data_surfaces + model->firstmodelsurface;i < model->nummodelsurfaces;i++, surface++)
501 if (surface->num_collisiontriangles)
502 R_DrawCollisionSurface(ent, surface);
503 qglPolygonOffset(0, 0);
507 typedef struct r_q1bsp_getlightinfo_s
510 vec3_t relativelightorigin;
516 qbyte *outsurfacepvs;
524 r_q1bsp_getlightinfo_t;
526 void R_Q1BSP_RecursiveGetLightInfo(r_q1bsp_getlightinfo_t *info, mnode_t *node)
532 if (!BoxesOverlap(info->lightmins, info->lightmaxs, node->mins, node->maxs))
536 sides = BoxOnPlaneSide(info->lightmins, info->lightmaxs, node->plane) - 1;
539 R_Q1BSP_RecursiveGetLightInfo(info, node->children[0]);
540 node = node->children[1];
543 node = node->children[sides];
545 leaf = (mleaf_t *)node;
546 if (info->pvs == NULL || CHECKPVSBIT(info->pvs, leaf->clusterindex))
548 info->outmins[0] = min(info->outmins[0], leaf->mins[0]);
549 info->outmins[1] = min(info->outmins[1], leaf->mins[1]);
550 info->outmins[2] = min(info->outmins[2], leaf->mins[2]);
551 info->outmaxs[0] = max(info->outmaxs[0], leaf->maxs[0]);
552 info->outmaxs[1] = max(info->outmaxs[1], leaf->maxs[1]);
553 info->outmaxs[2] = max(info->outmaxs[2], leaf->maxs[2]);
554 if (info->outleafpvs)
556 int leafindex = leaf - info->model->brush.data_leafs;
557 if (!CHECKPVSBIT(info->outleafpvs, leafindex))
559 SETPVSBIT(info->outleafpvs, leafindex);
560 info->outleaflist[info->outnumleafs++] = leafindex;
563 if (info->outsurfacepvs)
565 int leafsurfaceindex;
566 for (leafsurfaceindex = 0;leafsurfaceindex < leaf->numleafsurfaces;leafsurfaceindex++)
568 int surfaceindex = leaf->firstleafsurface[leafsurfaceindex];
569 if (!CHECKPVSBIT(info->outsurfacepvs, surfaceindex))
571 msurface_t *surface = info->model->data_surfaces + surfaceindex;
572 if (BoxesOverlap(info->lightmins, info->lightmaxs, surface->mins, surface->maxs))
573 if ((surface->texture->currentmaterialflags & (MATERIALFLAG_WALL | MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
575 int triangleindex, t;
578 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = info->model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->num_triangles;triangleindex++, t++, e += 3)
580 v[0] = info->model->brush.shadowmesh->vertex3f + e[0] * 3;
581 v[1] = info->model->brush.shadowmesh->vertex3f + e[1] * 3;
582 v[2] = info->model->brush.shadowmesh->vertex3f + e[2] * 3;
583 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])))
585 SETPVSBIT(info->outsurfacepvs, surfaceindex);
586 info->outsurfacelist[info->outnumsurfaces++] = surfaceindex;
597 void R_Q1BSP_GetLightInfo(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, vec3_t outmins, vec3_t outmaxs, int *outleaflist, qbyte *outleafpvs, int *outnumleafspointer, int *outsurfacelist, qbyte *outsurfacepvs, int *outnumsurfacespointer)
599 r_q1bsp_getlightinfo_t info;
600 VectorCopy(relativelightorigin, info.relativelightorigin);
601 info.lightradius = lightradius;
602 info.lightmins[0] = info.relativelightorigin[0] - info.lightradius;
603 info.lightmins[1] = info.relativelightorigin[1] - info.lightradius;
604 info.lightmins[2] = info.relativelightorigin[2] - info.lightradius;
605 info.lightmaxs[0] = info.relativelightorigin[0] + info.lightradius;
606 info.lightmaxs[1] = info.relativelightorigin[1] + info.lightradius;
607 info.lightmaxs[2] = info.relativelightorigin[2] + info.lightradius;
608 if (ent->model == NULL)
610 VectorCopy(info.lightmins, outmins);
611 VectorCopy(info.lightmaxs, outmaxs);
612 *outnumleafspointer = 0;
613 *outnumsurfacespointer = 0;
616 info.model = ent->model;
617 info.outleaflist = outleaflist;
618 info.outleafpvs = outleafpvs;
619 info.outnumleafs = 0;
620 info.outsurfacelist = outsurfacelist;
621 info.outsurfacepvs = outsurfacepvs;
622 info.outnumsurfaces = 0;
623 VectorCopy(info.relativelightorigin, info.outmins);
624 VectorCopy(info.relativelightorigin, info.outmaxs);
625 memset(outleafpvs, 0, (info.model->brush.num_leafs + 7) >> 3);
626 memset(outsurfacepvs, 0, (info.model->nummodelsurfaces + 7) >> 3);
627 if (info.model->brush.GetPVS)
628 info.pvs = info.model->brush.GetPVS(info.model, info.relativelightorigin);
631 R_UpdateAllTextureInfo(ent);
632 if (r_shadow_compilingrtlight)
634 // use portal recursion for exact light volume culling, and exact surface checking
635 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);
637 else if (r_shadow_realtime_dlight_portalculling.integer)
639 // use portal recursion for exact light volume culling, but not the expensive exact surface checking
640 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);
644 // use BSP recursion as lights are often small
645 R_Q1BSP_RecursiveGetLightInfo(&info, info.model->brush.data_nodes);
648 // limit combined leaf box to light boundaries
649 outmins[0] = max(info.outmins[0] - 1, info.lightmins[0]);
650 outmins[1] = max(info.outmins[1] - 1, info.lightmins[1]);
651 outmins[2] = max(info.outmins[2] - 1, info.lightmins[2]);
652 outmaxs[0] = min(info.outmaxs[0] + 1, info.lightmaxs[0]);
653 outmaxs[1] = min(info.outmaxs[1] + 1, info.lightmaxs[1]);
654 outmaxs[2] = min(info.outmaxs[2] + 1, info.lightmaxs[2]);
656 *outnumleafspointer = info.outnumleafs;
657 *outnumsurfacespointer = info.outnumsurfaces;
660 void R_Q1BSP_CompileShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
662 model_t *model = ent->model;
664 int surfacelistindex;
665 float projectdistance = lightradius + model->radius*2 + r_shadow_projectdistance.value;
667 r_shadow_compilingrtlight->static_meshchain_shadow = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768, 32768, NULL, NULL, NULL, false, false, true);
668 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
669 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
671 surface = model->data_surfaces + surfacelist[surfacelistindex];
672 texture = surface->texture;
673 if ((texture->basematerialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT | MATERIALFLAG_WALL)) != MATERIALFLAG_WALL)
675 if (texture->textureflags & (Q3TEXTUREFLAG_TWOSIDED | Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
677 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);
679 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);
680 r_shadow_compilingrtlight->static_meshchain_shadow = Mod_ShadowMesh_Finish(r_shadow_mempool, r_shadow_compilingrtlight->static_meshchain_shadow, false, false);
683 extern float *rsurface_vertex3f;
684 extern float *rsurface_svector3f;
685 extern float *rsurface_tvector3f;
686 extern float *rsurface_normal3f;
687 extern void RSurf_SetVertexPointer(const entity_render_t *ent, const texture_t *texture, const msurface_t *surface, const vec3_t modelorg);
689 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)
691 model_t *model = ent->model;
693 int surfacelistindex;
694 float projectdistance = lightradius + model->radius*2 + r_shadow_projectdistance.value;
697 // check the box in modelspace, it was already checked in worldspace
698 if (!BoxesOverlap(ent->model->normalmins, ent->model->normalmaxs, lightmins, lightmaxs))
700 if (r_drawcollisionbrushes.integer >= 2)
702 R_UpdateAllTextureInfo(ent);
703 if (model->brush.shadowmesh)
705 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
706 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
708 surface = model->data_surfaces + surfacelist[surfacelistindex];
709 texture = surface->texture->currentframe;
710 if ((texture->currentmaterialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT | MATERIALFLAG_WALL)) != MATERIALFLAG_WALL)
712 if (texture->textureflags & (Q3TEXTUREFLAG_TWOSIDED | Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
714 R_Shadow_MarkVolumeFromBox(surface->num_firstshadowmeshtriangle, surface->num_triangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, relativelightorigin, lightmins, lightmaxs, surface->mins, surface->maxs);
716 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);
720 projectdistance = lightradius + ent->model->radius*2;
721 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
722 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
724 surface = model->data_surfaces + surfacelist[surfacelistindex];
725 // FIXME: get current skin
726 texture = surface->texture;//R_FetchAliasSkin(ent, surface->groupmesh);
727 if (texture->currentmaterialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT) || !surface->num_triangles)
729 RSurf_SetVertexPointer(ent, texture, surface, modelorg);
730 // identify lit faces within the bounding box
731 R_Shadow_PrepareShadowMark(surface->groupmesh->num_triangles);
732 R_Shadow_MarkVolumeFromBox(surface->num_firsttriangle, surface->num_triangles, rsurface_vertex3f, surface->groupmesh->data_element3i, relativelightorigin, lightmins, lightmaxs, surface->mins, surface->maxs);
733 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);
738 #define RSURF_MAX_BATCHSURFACES 1024
740 void R_Q1BSP_DrawLight(entity_render_t *ent, float *lightcolorbase, int numsurfaces, const int *surfacelist)
742 model_t *model = ent->model;
745 int surfacelistindex, batchnumsurfaces;
746 msurface_t *batchsurfacelist[RSURF_MAX_BATCHSURFACES];
749 vec3_t lightcolorpants, lightcolorshirt;
750 rtexture_t *basetexture = NULL;
751 rtexture_t *glosstexture = NULL;
752 float specularscale = 0;
754 if (r_drawcollisionbrushes.integer >= 2)
756 R_UpdateAllTextureInfo(ent);
757 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
761 batchnumsurfaces = 0;
762 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
764 if ((ent == r_refdef.worldentity && !r_worldsurfacevisible[surfacelist[surfacelistindex]]))
766 surface = model->data_surfaces + surfacelist[surfacelistindex];
767 if (tex != surface->texture)
769 if (batchnumsurfaces > 0)
771 R_Shadow_RenderSurfacesLighting(ent, texture, batchnumsurfaces, batchsurfacelist, lightcolorbase, lightcolorpants, lightcolorshirt, basetexture, texture->skin.pants, texture->skin.shirt, texture->skin.nmap, glosstexture, specularscale, modelorg);
772 batchnumsurfaces = 0;
774 tex = surface->texture;
775 texture = surface->texture->currentframe;
776 // FIXME: transparent surfaces need to be lit later
777 skip = (texture->currentmaterialflags & (MATERIALFLAG_WALL | MATERIALFLAG_TRANSPARENT)) != MATERIALFLAG_WALL;
780 if (texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
781 qglDisable(GL_CULL_FACE);
783 qglEnable(GL_CULL_FACE);
784 glosstexture = r_texture_black;
786 if (texture->skin.gloss)
788 if (r_shadow_gloss.integer >= 1 && r_shadow_glossintensity.value > 0 && r_shadow_rtlight->specularscale > 0)
790 glosstexture = texture->skin.gloss;
791 specularscale = r_shadow_rtlight->specularscale * r_shadow_glossintensity.value;
796 if (r_shadow_gloss.integer >= 2 && r_shadow_gloss2intensity.value > 0 && r_shadow_glossintensity.value > 0 && r_shadow_rtlight->specularscale > 0)
798 glosstexture = r_texture_white;
799 specularscale = r_shadow_rtlight->specularscale * r_shadow_gloss2intensity.value;
802 VectorClear(lightcolorpants);
803 VectorClear(lightcolorshirt);
804 if (ent->colormap >= 0)
806 // 128-224 are backwards ranges
807 int b = (ent->colormap & 0xF) << 4;b += (b >= 128 && b < 224) ? 4 : 12;
808 if (texture->skin.pants && b < 224)
810 qbyte *bcolor = (qbyte *) (&palette_complete[b]);
811 lightcolorpants[0] = lightcolorbase[0] * bcolor[0] * (1.0f / 255.0f);
812 lightcolorpants[1] = lightcolorbase[1] * bcolor[1] * (1.0f / 255.0f);
813 lightcolorpants[2] = lightcolorbase[2] * bcolor[2] * (1.0f / 255.0f);
815 // 128-224 are backwards ranges
816 b = (ent->colormap & 0xF0);b += (b >= 128 && b < 224) ? 4 : 12;
817 if (texture->skin.shirt && b < 224)
819 qbyte *bcolor = (qbyte *) (&palette_complete[b]);
820 lightcolorshirt[0] = lightcolorbase[0] * bcolor[0] * (1.0f / 255.0f);
821 lightcolorshirt[1] = lightcolorbase[1] * bcolor[1] * (1.0f / 255.0f);
822 lightcolorshirt[2] = lightcolorbase[2] * bcolor[2] * (1.0f / 255.0f);
824 basetexture = texture->skin.base;
827 basetexture = texture->skin.merged ? texture->skin.merged : texture->skin.base;
828 if ((r_shadow_rtlight->ambientscale + r_shadow_rtlight->diffusescale) * (VectorLength2(lightcolorbase) + VectorLength2(lightcolorpants) + VectorLength2(lightcolorshirt)) + specularscale * VectorLength2(lightcolorbase) < (1.0f / 1048576.0f))
831 if (!skip && surface->num_triangles)
833 if (batchnumsurfaces == RSURF_MAX_BATCHSURFACES)
835 R_Shadow_RenderSurfacesLighting(ent, texture, batchnumsurfaces, batchsurfacelist, lightcolorbase, lightcolorpants, lightcolorshirt, basetexture, texture->skin.pants, texture->skin.shirt, texture->skin.nmap, glosstexture, specularscale, modelorg);
836 batchnumsurfaces = 0;
838 batchsurfacelist[batchnumsurfaces++] = surface;
841 if (batchnumsurfaces > 0)
843 R_Shadow_RenderSurfacesLighting(ent, texture, batchnumsurfaces, batchsurfacelist, lightcolorbase, lightcolorpants, lightcolorshirt, basetexture, texture->skin.pants, texture->skin.shirt, texture->skin.nmap, glosstexture, specularscale, modelorg);
844 batchnumsurfaces = 0;
846 qglEnable(GL_CULL_FACE);
850 static void gl_surf_start(void)
854 static void gl_surf_shutdown(void)
858 static void gl_surf_newmap(void)
863 void GL_Surf_Init(void)
866 Cvar_RegisterVariable(&r_ambient);
867 Cvar_RegisterVariable(&r_drawportals);
868 Cvar_RegisterVariable(&r_testvis);
869 Cvar_RegisterVariable(&r_detailtextures);
870 Cvar_RegisterVariable(&r_surfaceworldnode);
871 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
872 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
873 Cvar_RegisterVariable(&r_q3bsp_renderskydepth);
875 //R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);