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_surfaceworldnode = {0, "r_surfaceworldnode", "0"};
32 cvar_t r_drawcollisionbrushes_polygonfactor = {0, "r_drawcollisionbrushes_polygonfactor", "-1"};
33 cvar_t r_drawcollisionbrushes_polygonoffset = {0, "r_drawcollisionbrushes_polygonoffset", "0"};
34 cvar_t r_q3bsp_renderskydepth = {0, "r_q3bsp_renderskydepth", "0"};
36 // flag arrays used for visibility checking on world model
37 // (all other entities have no per-surface/per-leaf visibility checks)
38 // TODO: dynamic resize according to r_refdef.worldmodel->brush.num_clusters
39 qbyte r_pvsbits[(32768+7)>>3];
40 // TODO: dynamic resize according to r_refdef.worldmodel->brush.num_leafs
41 qbyte r_worldleafvisible[32768];
42 // TODO: dynamic resize according to r_refdef.worldmodel->num_surfaces
43 qbyte r_worldsurfacevisible[262144];
49 Combine and scale multiple lightmaps into the 8.8 format in blocklights
52 void R_BuildLightMap (const entity_render_t *ent, msurface_t *surface)
54 int smax, tmax, i, j, size, size3, maps, stride, l;
55 unsigned int *bl, scale;
56 qbyte *lightmap, *out, *stain;
57 static unsigned int intblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
58 static qbyte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
60 // update cached lighting info
61 surface->cached_dlight = 0;
63 smax = (surface->lightmapinfo->extents[0]>>4)+1;
64 tmax = (surface->lightmapinfo->extents[1]>>4)+1;
67 lightmap = surface->lightmapinfo->samples;
69 // set to full bright if no light data
71 if (!ent->model->brushq1.lightdata)
73 for (i = 0;i < size3;i++)
79 memset(bl, 0, size*3*sizeof(unsigned int));
81 // add all the lightmaps
85 for (maps = 0;maps < MAXLIGHTMAPS && surface->lightmapinfo->styles[maps] != 255;maps++, lightmap += size3)
86 for (scale = d_lightstylevalue[surface->lightmapinfo->styles[maps]], i = 0;i < size3;i++)
87 bl[i] += lightmap[i] * scale;
91 stain = surface->lightmapinfo->stainsamples;
94 // the >> 16 shift adjusts down 8 bits to account for the stainmap
95 // scaling, and remaps the 0-65536 (2x overbright) to 0-256, it will
96 // be doubled during rendering to achieve 2x overbright
97 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
98 if (ent->model->brushq1.lightmaprgba)
100 stride = (surface->lightmapinfo->lightmaptexturestride - smax) * 4;
101 for (i = 0;i < tmax;i++, out += stride)
103 for (j = 0;j < smax;j++)
105 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
106 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
107 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
114 stride = (surface->lightmapinfo->lightmaptexturestride - smax) * 3;
115 for (i = 0;i < tmax;i++, out += stride)
117 for (j = 0;j < smax;j++)
119 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
120 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
121 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
126 R_UpdateTexture(surface->lightmaptexture, templight);
129 void R_StainNode (mnode_t *node, model_t *model, const vec3_t origin, float radius, const float fcolor[8])
131 float ndist, a, ratio, maxdist, maxdist2, maxdist3, invradius, sdtable[256], td, dist2;
132 msurface_t *surface, *endsurface;
133 int i, s, t, smax, tmax, smax3, impacts, impactt, stained;
137 maxdist = radius * radius;
138 invradius = 1.0f / radius;
143 ndist = PlaneDiff(origin, node->plane);
146 node = node->children[0];
151 node = node->children[1];
155 dist2 = ndist * ndist;
156 maxdist3 = maxdist - dist2;
158 if (node->plane->type < 3)
160 VectorCopy(origin, impact);
161 impact[node->plane->type] -= ndist;
165 impact[0] = origin[0] - node->plane->normal[0] * ndist;
166 impact[1] = origin[1] - node->plane->normal[1] * ndist;
167 impact[2] = origin[2] - node->plane->normal[2] * ndist;
170 for (surface = model->data_surfaces + node->firstsurface, endsurface = surface + node->numsurfaces;surface < endsurface;surface++)
172 if (surface->lightmapinfo->stainsamples)
174 smax = (surface->lightmapinfo->extents[0] >> 4) + 1;
175 tmax = (surface->lightmapinfo->extents[1] >> 4) + 1;
177 impacts = DotProduct (impact, surface->lightmapinfo->texinfo->vecs[0]) + surface->lightmapinfo->texinfo->vecs[0][3] - surface->lightmapinfo->texturemins[0];
178 impactt = DotProduct (impact, surface->lightmapinfo->texinfo->vecs[1]) + surface->lightmapinfo->texinfo->vecs[1][3] - surface->lightmapinfo->texturemins[1];
180 s = bound(0, impacts, smax * 16) - impacts;
181 t = bound(0, impactt, tmax * 16) - impactt;
182 i = s * s + t * t + dist2;
186 // reduce calculations
187 for (s = 0, i = impacts; s < smax; s++, i -= 16)
188 sdtable[s] = i * i + dist2;
190 bl = surface->lightmapinfo->stainsamples;
195 for (t = 0;t < tmax;t++, i -= 16)
198 // make sure some part of it is visible on this line
201 maxdist2 = maxdist - td;
202 for (s = 0;s < smax;s++)
204 if (sdtable[s] < maxdist2)
206 ratio = lhrandom(0.0f, 1.0f);
207 a = (fcolor[3] + ratio * fcolor[7]) * (1.0f - sqrt(sdtable[s] + td) * invradius);
208 if (a >= (1.0f / 64.0f))
212 bl[0] = (qbyte) ((float) bl[0] + a * ((fcolor[0] + ratio * fcolor[4]) - (float) bl[0]));
213 bl[1] = (qbyte) ((float) bl[1] + a * ((fcolor[1] + ratio * fcolor[5]) - (float) bl[1]));
214 bl[2] = (qbyte) ((float) bl[2] + a * ((fcolor[2] + ratio * fcolor[6]) - (float) bl[2]));
224 // force lightmap upload
226 surface->cached_dlight = true;
230 if (node->children[0]->plane)
232 if (node->children[1]->plane)
234 R_StainNode(node->children[0], model, origin, radius, fcolor);
235 node = node->children[1];
240 node = node->children[0];
244 else if (node->children[1]->plane)
246 node = node->children[1];
251 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)
255 entity_render_t *ent;
258 if (r_refdef.worldmodel == NULL || !r_refdef.worldmodel->brush.data_nodes || !r_refdef.worldmodel->brushq1.lightdata)
263 fcolor[3] = ca1 * (1.0f / 64.0f);
264 fcolor[4] = cr2 - cr1;
265 fcolor[5] = cg2 - cg1;
266 fcolor[6] = cb2 - cb1;
267 fcolor[7] = (ca2 - ca1) * (1.0f / 64.0f);
269 R_StainNode(r_refdef.worldmodel->brush.data_nodes + r_refdef.worldmodel->brushq1.hulls[0].firstclipnode, r_refdef.worldmodel, origin, radius, fcolor);
271 // look for embedded bmodels
272 for (n = 0;n < cl_num_brushmodel_entities;n++)
274 ent = &cl_entities[cl_brushmodel_entities[n]].render;
276 if (model && model->name[0] == '*')
278 Mod_CheckLoaded(model);
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 = 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 (model->brush.FatPVS)
406 model->brush.FatPVS(model, r_vieworigin, 2, r_pvsbits, sizeof(r_pvsbits));
408 // clear the visible surface and leaf flags arrays
409 memset(r_worldsurfacevisible, 0, model->num_surfaces);
410 memset(r_worldleafvisible, 0, model->brush.num_leafs);
412 // if the user prefers surfaceworldnode (testing?) or the viewleaf could
413 // not be found, or the viewleaf is not part of the visible world
414 // (floating around in the void), use the pvs method
415 if (r_surfaceworldnode.integer || !viewleaf || viewleaf->clusterindex < 0)
418 // similar to quake's RecursiveWorldNode but without cache misses
419 for (j = 0, leaf = model->brush.data_leafs;j < model->brush.num_leafs;j++, leaf++)
421 // if leaf is in current pvs and on the screen, mark its surfaces
422 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
425 r_worldleafvisible[j] = true;
426 if (leaf->numleafsurfaces)
427 for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
428 r_worldsurfacevisible[*mark] = true;
436 mleaf_t *leafstack[8192];
438 // follows portals leading outward from viewleaf, does not venture
439 // offscreen or into leafs that are not visible, faster than Quake's
440 // RecursiveWorldNode and vastly better in unvised maps, often culls a
441 // lot of surface that pvs alone would miss
442 leafstack[0] = viewleaf;
447 leaf = leafstack[--leafstackpos];
448 r_worldleafvisible[leaf - model->brush.data_leafs] = true;
449 // mark any surfaces bounding this leaf
450 if (leaf->numleafsurfaces)
451 for (i = 0, mark = leaf->firstleafsurface;i < leaf->numleafsurfaces;i++, mark++)
452 r_worldsurfacevisible[*mark] = true;
453 // follow portals into other leafs
455 // if viewer is behind portal (portal faces outward into the scene)
456 // and the portal polygon's bounding box is on the screen
457 // and the leaf has not been visited yet
458 // and the leaf is visible in the pvs
459 // (the first two checks won't cause as many cache misses as the leaf checks)
460 for (p = leaf->portals;p;p = p->next)
461 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))
462 leafstack[leafstackpos++] = p->past;
466 if (r_drawportals.integer)
470 void R_Q1BSP_DrawSky(entity_render_t *ent)
472 if (ent->model == NULL)
474 if (r_drawcollisionbrushes.integer < 2)
475 R_DrawSurfaces(ent, true);
478 void R_Q1BSP_Draw(entity_render_t *ent)
480 if (ent->model == NULL)
483 if (r_drawcollisionbrushes.integer < 2)
484 R_DrawSurfaces(ent, false);
485 if (r_drawcollisionbrushes.integer >= 1 && ent->model->brush.num_brushes)
488 model_t *model = ent->model;
491 R_Mesh_Matrix(&ent->matrix);
492 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
495 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
496 for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
497 if (brush->colbrushf && brush->colbrushf->numtriangles)
498 R_DrawCollisionBrush(brush->colbrushf);
499 for (i = 0, surface = model->data_surfaces + model->firstmodelsurface;i < model->nummodelsurfaces;i++, surface++)
500 if (surface->num_collisiontriangles)
501 R_DrawCollisionSurface(ent, surface);
502 qglPolygonOffset(0, 0);
506 typedef struct r_q1bsp_getlightinfo_s
509 vec3_t relativelightorigin;
515 qbyte *outsurfacepvs;
523 r_q1bsp_getlightinfo_t;
525 void R_Q1BSP_RecursiveGetLightInfo(r_q1bsp_getlightinfo_t *info, mnode_t *node)
531 if (!BoxesOverlap(info->lightmins, info->lightmaxs, node->mins, node->maxs))
535 sides = BoxOnPlaneSide(info->lightmins, info->lightmaxs, node->plane) - 1;
538 R_Q1BSP_RecursiveGetLightInfo(info, node->children[0]);
539 node = node->children[1];
542 node = node->children[sides];
544 leaf = (mleaf_t *)node;
545 if (info->pvs == NULL || CHECKPVSBIT(info->pvs, leaf->clusterindex))
547 info->outmins[0] = min(info->outmins[0], leaf->mins[0]);
548 info->outmins[1] = min(info->outmins[1], leaf->mins[1]);
549 info->outmins[2] = min(info->outmins[2], leaf->mins[2]);
550 info->outmaxs[0] = max(info->outmaxs[0], leaf->maxs[0]);
551 info->outmaxs[1] = max(info->outmaxs[1], leaf->maxs[1]);
552 info->outmaxs[2] = max(info->outmaxs[2], leaf->maxs[2]);
553 if (info->outleafpvs)
555 int leafindex = leaf - info->model->brush.data_leafs;
556 if (!CHECKPVSBIT(info->outleafpvs, leafindex))
558 SETPVSBIT(info->outleafpvs, leafindex);
559 info->outleaflist[info->outnumleafs++] = leafindex;
562 if (info->outsurfacepvs)
564 int leafsurfaceindex;
565 for (leafsurfaceindex = 0;leafsurfaceindex < leaf->numleafsurfaces;leafsurfaceindex++)
567 int surfaceindex = leaf->firstleafsurface[leafsurfaceindex];
568 if (!CHECKPVSBIT(info->outsurfacepvs, surfaceindex))
570 msurface_t *surface = info->model->data_surfaces + surfaceindex;
571 if (BoxesOverlap(info->lightmins, info->lightmaxs, surface->mins, surface->maxs))
572 if ((surface->texture->currentmaterialflags & (MATERIALFLAG_WALL | MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT)) == MATERIALFLAG_WALL)
574 int triangleindex, t;
577 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = info->model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->num_triangles;triangleindex++, t++, e += 3)
579 v[0] = info->model->brush.shadowmesh->vertex3f + e[0] * 3;
580 v[1] = info->model->brush.shadowmesh->vertex3f + e[1] * 3;
581 v[2] = info->model->brush.shadowmesh->vertex3f + e[2] * 3;
582 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])))
584 SETPVSBIT(info->outsurfacepvs, surfaceindex);
585 info->outsurfacelist[info->outnumsurfaces++] = surfaceindex;
596 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)
598 r_q1bsp_getlightinfo_t info;
599 VectorCopy(relativelightorigin, info.relativelightorigin);
600 info.lightradius = lightradius;
601 info.lightmins[0] = info.relativelightorigin[0] - info.lightradius;
602 info.lightmins[1] = info.relativelightorigin[1] - info.lightradius;
603 info.lightmins[2] = info.relativelightorigin[2] - info.lightradius;
604 info.lightmaxs[0] = info.relativelightorigin[0] + info.lightradius;
605 info.lightmaxs[1] = info.relativelightorigin[1] + info.lightradius;
606 info.lightmaxs[2] = info.relativelightorigin[2] + info.lightradius;
607 if (ent->model == NULL)
609 VectorCopy(info.lightmins, outmins);
610 VectorCopy(info.lightmaxs, outmaxs);
611 *outnumleafspointer = 0;
612 *outnumsurfacespointer = 0;
615 info.model = ent->model;
616 info.outleaflist = outleaflist;
617 info.outleafpvs = outleafpvs;
618 info.outnumleafs = 0;
619 info.outsurfacelist = outsurfacelist;
620 info.outsurfacepvs = outsurfacepvs;
621 info.outnumsurfaces = 0;
622 VectorCopy(info.relativelightorigin, info.outmins);
623 VectorCopy(info.relativelightorigin, info.outmaxs);
624 memset(outleafpvs, 0, (info.model->brush.num_leafs + 7) >> 3);
625 memset(outsurfacepvs, 0, (info.model->nummodelsurfaces + 7) >> 3);
626 if (info.model->brush.GetPVS)
627 info.pvs = info.model->brush.GetPVS(info.model, info.relativelightorigin);
630 R_UpdateAllTextureInfo(ent);
631 if (r_shadow_compilingrtlight)
633 // use portal recursion for exact light volume culling, and exact surface checking
634 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);
636 else if (r_shadow_realtime_dlight_portalculling.integer)
638 // use portal recursion for exact light volume culling, but not the expensive exact surface checking
639 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);
643 // use BSP recursion as lights are often small
644 R_Q1BSP_RecursiveGetLightInfo(&info, info.model->brush.data_nodes);
647 // limit combined leaf box to light boundaries
648 outmins[0] = max(info.outmins[0] - 1, info.lightmins[0]);
649 outmins[1] = max(info.outmins[1] - 1, info.lightmins[1]);
650 outmins[2] = max(info.outmins[2] - 1, info.lightmins[2]);
651 outmaxs[0] = min(info.outmaxs[0] + 1, info.lightmaxs[0]);
652 outmaxs[1] = min(info.outmaxs[1] + 1, info.lightmaxs[1]);
653 outmaxs[2] = min(info.outmaxs[2] + 1, info.lightmaxs[2]);
655 *outnumleafspointer = info.outnumleafs;
656 *outnumsurfacespointer = info.outnumsurfaces;
659 void R_Q1BSP_CompileShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
661 model_t *model = ent->model;
663 int surfacelistindex;
664 float projectdistance = lightradius + model->radius*2 + r_shadow_projectdistance.value;
666 r_shadow_compilingrtlight->static_meshchain_shadow = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768, 32768, NULL, NULL, NULL, false, false, true);
667 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
668 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
670 surface = model->data_surfaces + surfacelist[surfacelistindex];
671 texture = surface->texture;
672 if ((texture->basematerialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT | MATERIALFLAG_WALL)) != MATERIALFLAG_WALL)
674 if (texture->textureflags & (Q3TEXTUREFLAG_TWOSIDED | Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
676 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);
678 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);
679 r_shadow_compilingrtlight->static_meshchain_shadow = Mod_ShadowMesh_Finish(r_shadow_mempool, r_shadow_compilingrtlight->static_meshchain_shadow, false, false);
682 extern float *rsurface_vertex3f;
683 extern float *rsurface_svector3f;
684 extern float *rsurface_tvector3f;
685 extern float *rsurface_normal3f;
686 extern void RSurf_SetVertexPointer(const entity_render_t *ent, const texture_t *texture, const msurface_t *surface, const vec3_t modelorg);
688 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)
690 model_t *model = ent->model;
692 int surfacelistindex;
693 float projectdistance = lightradius + model->radius*2 + r_shadow_projectdistance.value;
696 // check the box in modelspace, it was already checked in worldspace
697 if (!BoxesOverlap(ent->model->normalmins, ent->model->normalmaxs, lightmins, lightmaxs))
699 if (r_drawcollisionbrushes.integer >= 2)
701 R_UpdateAllTextureInfo(ent);
702 if (model->brush.shadowmesh)
704 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
705 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
707 surface = model->data_surfaces + surfacelist[surfacelistindex];
708 texture = surface->texture->currentframe;
709 if ((texture->currentmaterialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT | MATERIALFLAG_WALL)) != MATERIALFLAG_WALL)
711 if (texture->textureflags & (Q3TEXTUREFLAG_TWOSIDED | Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
713 R_Shadow_MarkVolumeFromBox(surface->num_firstshadowmeshtriangle, surface->num_triangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, relativelightorigin, lightmins, lightmaxs, surface->mins, surface->maxs);
715 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);
719 projectdistance = lightradius + ent->model->radius*2;
720 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
721 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
723 surface = model->data_surfaces + surfacelist[surfacelistindex];
724 // FIXME: get current skin
725 texture = surface->texture;//R_FetchAliasSkin(ent, surface->groupmesh);
726 if (texture->currentmaterialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT) || !surface->num_triangles)
728 RSurf_SetVertexPointer(ent, texture, surface, modelorg);
729 // identify lit faces within the bounding box
730 R_Shadow_PrepareShadowMark(surface->groupmesh->num_triangles);
731 R_Shadow_MarkVolumeFromBox(surface->num_firsttriangle, surface->num_triangles, rsurface_vertex3f, surface->groupmesh->data_element3i, relativelightorigin, lightmins, lightmaxs, surface->mins, surface->maxs);
732 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);
737 #define RSURF_MAX_BATCHSURFACES 1024
739 void R_Q1BSP_DrawLight(entity_render_t *ent, float *lightcolorbase, float *lightcolorpants, float *lightcolorshirt, int numsurfaces, const int *surfacelist)
741 model_t *model = ent->model;
744 int surfacelistindex, batchnumsurfaces;
745 msurface_t *batchsurfacelist[RSURF_MAX_BATCHSURFACES];
748 rtexture_t *basetexture = NULL;
749 rtexture_t *glosstexture = NULL;
750 float specularscale = 0;
752 if (r_drawcollisionbrushes.integer >= 2)
754 if (VectorLength2(lightcolorbase) + VectorLength2(lightcolorpants) + VectorLength2(lightcolorshirt) < 0.0001)
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 basetexture = (ent->colormap < 0 && texture->skin.merged) ? texture->skin.merged : texture->skin.base;
803 if ((r_shadow_rtlight->ambientscale + r_shadow_rtlight->diffusescale) * (VectorLength2(lightcolorbase) + VectorLength2(lightcolorpants) + VectorLength2(lightcolorshirt)) + specularscale * VectorLength2(lightcolorbase) < (1.0f / 1048576.0f))
806 if (!skip && surface->num_triangles)
808 if (batchnumsurfaces == RSURF_MAX_BATCHSURFACES)
810 R_Shadow_RenderSurfacesLighting(ent, texture, batchnumsurfaces, batchsurfacelist, lightcolorbase, lightcolorpants, lightcolorshirt, basetexture, texture->skin.pants, texture->skin.shirt, texture->skin.nmap, glosstexture, specularscale, modelorg);
811 batchnumsurfaces = 0;
813 batchsurfacelist[batchnumsurfaces++] = surface;
816 if (batchnumsurfaces > 0)
818 R_Shadow_RenderSurfacesLighting(ent, texture, batchnumsurfaces, batchsurfacelist, lightcolorbase, lightcolorpants, lightcolorshirt, basetexture, texture->skin.pants, texture->skin.shirt, texture->skin.nmap, glosstexture, specularscale, modelorg);
819 batchnumsurfaces = 0;
821 qglEnable(GL_CULL_FACE);
825 static void gl_surf_start(void)
829 static void gl_surf_shutdown(void)
833 static void gl_surf_newmap(void)
838 void GL_Surf_Init(void)
841 Cvar_RegisterVariable(&r_ambient);
842 Cvar_RegisterVariable(&r_drawportals);
843 Cvar_RegisterVariable(&r_testvis);
844 Cvar_RegisterVariable(&r_surfaceworldnode);
845 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
846 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
847 Cvar_RegisterVariable(&r_q3bsp_renderskydepth);
849 //R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);