]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_rsurf.c
disabled dlight from scrag and hellknight shots
[divverent/darkplaces.git] / gl_rsurf.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
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.
8
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.
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20 // r_surf.c: surface-related refresh code
21
22 #include "quakedef.h"
23 #include "r_shadow.h"
24 #include "portals.h"
25
26 #define MAX_LIGHTMAP_SIZE 256
27
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"};
36
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];
45
46 /*
47 ===============
48 R_BuildLightMap
49
50 Combine and scale multiple lightmaps into the 8.8 format in blocklights
51 ===============
52 */
53 void R_BuildLightMap (const entity_render_t *ent, msurface_t *surface)
54 {
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];
60
61         // update cached lighting info
62         surface->cached_dlight = 0;
63
64         smax = (surface->lightmapinfo->extents[0]>>4)+1;
65         tmax = (surface->lightmapinfo->extents[1]>>4)+1;
66         size = smax*tmax;
67         size3 = size*3;
68         lightmap = surface->lightmapinfo->samples;
69
70 // set to full bright if no light data
71         bl = intblocklights;
72         if (!ent->model->brushq1.lightdata)
73         {
74                 for (i = 0;i < size3;i++)
75                         bl[i] = 255*256;
76         }
77         else
78         {
79 // clear to no light
80                 memset(bl, 0, size*3*sizeof(unsigned int));
81
82 // add all the lightmaps
83                 if (lightmap)
84                 {
85                         bl = intblocklights;
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;
89                 }
90         }
91
92         stain = surface->lightmapinfo->stainsamples;
93         bl = intblocklights;
94         out = templight;
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)
100         {
101                 stride = (surface->lightmapinfo->lightmaptexturestride - smax) * 4;
102                 for (i = 0;i < tmax;i++, out += stride)
103                 {
104                         for (j = 0;j < smax;j++)
105                         {
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);
109                                 *out++ = 255;
110                         }
111                 }
112         }
113         else
114         {
115                 stride = (surface->lightmapinfo->lightmaptexturestride - smax) * 3;
116                 for (i = 0;i < tmax;i++, out += stride)
117                 {
118                         for (j = 0;j < smax;j++)
119                         {
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);
123                         }
124                 }
125         }
126
127         R_UpdateTexture(surface->lightmaptexture, templight);
128 }
129
130 void R_StainNode (mnode_t *node, model_t *model, const vec3_t origin, float radius, const float fcolor[8])
131 {
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;
135         unsigned char *bl;
136         vec3_t impact;
137
138         maxdist = radius * radius;
139         invradius = 1.0f / radius;
140
141 loc0:
142         if (!node->plane)
143                 return;
144         ndist = PlaneDiff(origin, node->plane);
145         if (ndist > radius)
146         {
147                 node = node->children[0];
148                 goto loc0;
149         }
150         if (ndist < -radius)
151         {
152                 node = node->children[1];
153                 goto loc0;
154         }
155
156         dist2 = ndist * ndist;
157         maxdist3 = maxdist - dist2;
158
159         if (node->plane->type < 3)
160         {
161                 VectorCopy(origin, impact);
162                 impact[node->plane->type] -= ndist;
163         }
164         else
165         {
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;
169         }
170
171         for (surface = model->data_surfaces + node->firstsurface, endsurface = surface + node->numsurfaces;surface < endsurface;surface++)
172         {
173                 if (surface->lightmapinfo->stainsamples)
174                 {
175                         smax = (surface->lightmapinfo->extents[0] >> 4) + 1;
176                         tmax = (surface->lightmapinfo->extents[1] >> 4) + 1;
177
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];
180
181                         s = bound(0, impacts, smax * 16) - impacts;
182                         t = bound(0, impactt, tmax * 16) - impactt;
183                         i = s * s + t * t + dist2;
184                         if (i > maxdist)
185                                 continue;
186
187                         // reduce calculations
188                         for (s = 0, i = impacts; s < smax; s++, i -= 16)
189                                 sdtable[s] = i * i + dist2;
190
191                         bl = surface->lightmapinfo->stainsamples;
192                         smax3 = smax * 3;
193                         stained = false;
194
195                         i = impactt;
196                         for (t = 0;t < tmax;t++, i -= 16)
197                         {
198                                 td = i * i;
199                                 // make sure some part of it is visible on this line
200                                 if (td < maxdist3)
201                                 {
202                                         maxdist2 = maxdist - td;
203                                         for (s = 0;s < smax;s++)
204                                         {
205                                                 if (sdtable[s] < maxdist2)
206                                                 {
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))
210                                                         {
211                                                                 if (a > 1)
212                                                                         a = 1;
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]));
216                                                                 stained = true;
217                                                         }
218                                                 }
219                                                 bl += 3;
220                                         }
221                                 }
222                                 else // skip line
223                                         bl += smax3;
224                         }
225                         // force lightmap upload
226                         if (stained)
227                                 surface->cached_dlight = true;
228                 }
229         }
230
231         if (node->children[0]->plane)
232         {
233                 if (node->children[1]->plane)
234                 {
235                         R_StainNode(node->children[0], model, origin, radius, fcolor);
236                         node = node->children[1];
237                         goto loc0;
238                 }
239                 else
240                 {
241                         node = node->children[0];
242                         goto loc0;
243                 }
244         }
245         else if (node->children[1]->plane)
246         {
247                 node = node->children[1];
248                 goto loc0;
249         }
250 }
251
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)
253 {
254         int n;
255         float fcolor[8];
256         entity_render_t *ent;
257         model_t *model;
258         vec3_t org;
259         if (r_refdef.worldmodel == NULL || !r_refdef.worldmodel->brush.data_nodes || !r_refdef.worldmodel->brushq1.lightdata)
260                 return;
261         fcolor[0] = cr1;
262         fcolor[1] = cg1;
263         fcolor[2] = cb1;
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);
269
270         R_StainNode(r_refdef.worldmodel->brush.data_nodes + r_refdef.worldmodel->brushq1.hulls[0].firstclipnode, r_refdef.worldmodel, origin, radius, fcolor);
271
272         // look for embedded bmodels
273         for (n = 0;n < cl_num_brushmodel_entities;n++)
274         {
275                 ent = &cl_entities[cl_brushmodel_entities[n]].render;
276                 model = ent->model;
277                 if (model && model->name[0] == '*')
278                 {
279                         if (model->brush.data_nodes)
280                         {
281                                 Matrix4x4_Transform(&ent->inversematrix, origin, org);
282                                 R_StainNode(model->brush.data_nodes + model->brushq1.hulls[0].firstclipnode, model, org, radius, fcolor);
283                         }
284                 }
285         }
286 }
287
288
289 /*
290 =============================================================
291
292         BRUSH MODELS
293
294 =============================================================
295 */
296
297 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
298 {
299         int i;
300         float *v;
301         rmeshstate_t m;
302         const mportal_t *portal = (mportal_t *)calldata1;
303         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
304         GL_DepthMask(false);
305         GL_DepthTest(true);
306         R_Mesh_Matrix(&r_identitymatrix);
307
308         memset(&m, 0, sizeof(m));
309         m.pointer_vertex = varray_vertex3f;
310         R_Mesh_State(&m);
311
312         i = calldata2;
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),
316                          0.125f);
317         if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
318         {
319                 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
320                         VectorCopy(portal->points[i].position, v);
321         }
322         else
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);
327         GL_LockArrays(0, 0);
328 }
329
330 // LordHavoc: this is just a nice debugging tool, very slow
331 static void R_DrawPortals(void)
332 {
333         int i, leafnum;//, portalnum;
334         mportal_t *portal;
335         float center[3], f;
336         model_t *model = r_refdef.worldmodel;
337         if (model == NULL)
338                 return;
339         for (leafnum = 0;leafnum < r_refdef.worldmodel->brush.num_leafs;leafnum++)
340         {
341                 if (r_worldleafvisible[leafnum])
342                 {
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)
345                         {
346                                 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
347                                 if (!R_CullBox(portal->mins, portal->maxs))
348                                 {
349                                         VectorClear(center);
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);
356                                 }
357                         }
358                 }
359         }
360 }
361
362 static void R_DrawCollisionBrush(colbrushf_t *brush)
363 {
364         int i;
365         rmeshstate_t m;
366         memset(&m, 0, sizeof(m));
367         m.pointer_vertex = brush->points->v;
368         R_Mesh_State(&m);
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);
373         GL_LockArrays(0, 0);
374 }
375
376 static void R_DrawCollisionSurface(entity_render_t *ent, msurface_t *surface)
377 {
378         int i;
379         rmeshstate_t m;
380         if (!surface->num_collisiontriangles)
381                 return;
382         memset(&m, 0, sizeof(m));
383         m.pointer_vertex = surface->data_collisionvertex3f;
384         R_Mesh_State(&m);
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);
389         GL_LockArrays(0, 0);
390 }
391
392 void R_WorldVisibility(void)
393 {
394         int i, j, *mark;
395         mleaf_t *leaf;
396         mleaf_t *viewleaf;
397         model_t *model = r_refdef.worldmodel;
398
399         if (!model)
400                 return;
401
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));
407
408         if (!r_lockvisibility.integer)
409         {
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);
413
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)
417                 {
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++)
422                         {
423                                 // if leaf is in current pvs and on the screen, mark its surfaces
424                                 if (!R_CullBox(leaf->mins, leaf->maxs))
425                                 {
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;
431                                 }
432                         }
433                 }
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)
437                 {
438                         // pvs method:
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++)
443                         {
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))
446                                 {
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;
452                                 }
453                         }
454                 }
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
457                 else
458                 {
459                         int leafstackpos;
460                         mportal_t *p;
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;
470                         leafstackpos = 1;
471                         while (leafstackpos)
472                         {
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
481                                 // the checks are:
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)
488                                 {
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;
492                                 }
493                         }
494                 }
495         }
496
497         if (r_drawportals.integer)
498                 R_DrawPortals();
499 }
500
501 void R_Q1BSP_DrawSky(entity_render_t *ent)
502 {
503         if (ent->model == NULL)
504                 return;
505         if (r_drawcollisionbrushes.integer < 2)
506                 R_DrawSurfaces(ent, true);
507 }
508
509 void R_Q1BSP_Draw(entity_render_t *ent)
510 {
511         model_t *model = ent->model;
512         if (model == NULL)
513                 return;
514         if (r_drawcollisionbrushes.integer < 2)
515                 R_DrawSurfaces(ent, false);
516         if (r_drawcollisionbrushes.integer >= 1 && model->brush.num_brushes)
517         {
518                 int i;
519                 msurface_t *surface;
520                 q3mbrush_t *brush;
521                 R_Mesh_Matrix(&ent->matrix);
522                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
523                 GL_DepthMask(false);
524                 GL_DepthTest(true);
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);
533         }
534 }
535
536 typedef struct r_q1bsp_getlightinfo_s
537 {
538         model_t *model;
539         vec3_t relativelightorigin;
540         float lightradius;
541         int *outleaflist;
542         unsigned char *outleafpvs;
543         int outnumleafs;
544         int *outsurfacelist;
545         unsigned char *outsurfacepvs;
546         int outnumsurfaces;
547         vec3_t outmins;
548         vec3_t outmaxs;
549         vec3_t lightmins;
550         vec3_t lightmaxs;
551         const unsigned char *pvs;
552 }
553 r_q1bsp_getlightinfo_t;
554
555 void R_Q1BSP_RecursiveGetLightInfo(r_q1bsp_getlightinfo_t *info, mnode_t *node)
556 {
557         int sides;
558         mleaf_t *leaf;
559         for (;;)
560         {
561                 mplane_t *plane = node->plane;
562                 //if (!BoxesOverlap(info->lightmins, info->lightmaxs, node->mins, node->maxs))
563                 //      return;
564                 if (!plane)
565                         break;
566                 if (plane->type < 3)
567                         sides = ((info->lightmaxs[plane->type] >= plane->dist) | ((info->lightmins[plane->type] < plane->dist) << 1));
568                 else
569                         sides = BoxOnPlaneSide(info->lightmins, info->lightmaxs, plane);
570                 if (sides == 3)
571                 {
572                         R_Q1BSP_RecursiveGetLightInfo(info, node->children[0]);
573                         node = node->children[1];
574                 }
575                 else
576                         node = node->children[sides - 1];
577         }
578         leaf = (mleaf_t *)node;
579         if (info->pvs == NULL || CHECKPVSBIT(info->pvs, leaf->clusterindex))
580         {
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)
588                 {
589                         int leafindex = leaf - info->model->brush.data_leafs;
590                         if (!CHECKPVSBIT(info->outleafpvs, leafindex))
591                         {
592                                 SETPVSBIT(info->outleafpvs, leafindex);
593                                 info->outleaflist[info->outnumleafs++] = leafindex;
594                         }
595                 }
596                 if (info->outsurfacepvs)
597                 {
598                         int leafsurfaceindex;
599                         for (leafsurfaceindex = 0;leafsurfaceindex < leaf->numleafsurfaces;leafsurfaceindex++)
600                         {
601                                 int surfaceindex = leaf->firstleafsurface[leafsurfaceindex];
602                                 if (!CHECKPVSBIT(info->outsurfacepvs, surfaceindex))
603                                 {
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)
607                                         {
608                                                 int triangleindex, t;
609                                                 const int *e;
610                                                 const vec_t *v[3];
611                                                 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = info->model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->num_triangles;triangleindex++, t++, e += 3)
612                                                 {
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])))
617                                                         {
618                                                                 SETPVSBIT(info->outsurfacepvs, surfaceindex);
619                                                                 info->outsurfacelist[info->outnumsurfaces++] = surfaceindex;
620                                                                 break;
621                                                         }
622                                                 }
623                                         }
624                                 }
625                         }
626                 }
627         }
628 }
629
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)
631 {
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)
642         {
643                 VectorCopy(info.lightmins, outmins);
644                 VectorCopy(info.lightmaxs, outmaxs);
645                 *outnumleafspointer = 0;
646                 *outnumsurfacespointer = 0;
647                 return;
648         }
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);
662         else
663                 info.pvs = NULL;
664         R_UpdateAllTextureInfo(ent);
665         if (r_shadow_compilingrtlight)
666         {
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);
669         }
670         else if (r_shadow_realtime_dlight_portalculling.integer)
671         {
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);
674         }
675         else
676         {
677                 // use BSP recursion as lights are often small
678                 R_Q1BSP_RecursiveGetLightInfo(&info, info.model->brush.data_nodes);
679         }
680
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]);
688
689         *outnumleafspointer = info.outnumleafs;
690         *outnumsurfacespointer = info.outnumsurfaces;
691 }
692
693 void R_Q1BSP_CompileShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
694 {
695         model_t *model = ent->model;
696         msurface_t *surface;
697         int surfacelistindex;
698         float projectdistance = lightradius + model->radius*2 + r_shadow_projectdistance.value;
699         texture_t *texture;
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++)
703         {
704                 surface = model->data_surfaces + surfacelist[surfacelistindex];
705                 texture = surface->texture;
706                 if ((texture->basematerialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT | MATERIALFLAG_WALL)) != MATERIALFLAG_WALL)
707                         continue;
708                 if (texture->textureflags & (Q3TEXTUREFLAG_TWOSIDED | Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
709                         continue;
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);
711         }
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);
714 }
715
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);
721
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)
723 {
724         model_t *model = ent->model;
725         msurface_t *surface;
726         int surfacelistindex;
727         float projectdistance = lightradius + model->radius*2 + r_shadow_projectdistance.value;
728         vec3_t modelorg;
729         texture_t *texture;
730         // check the box in modelspace, it was already checked in worldspace
731         if (!BoxesOverlap(ent->model->normalmins, ent->model->normalmaxs, lightmins, lightmaxs))
732                 return;
733         if (r_drawcollisionbrushes.integer >= 2)
734                 return;
735         R_UpdateAllTextureInfo(ent);
736         if (model->brush.shadowmesh)
737         {
738                 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
739                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
740                 {
741                         surface = model->data_surfaces + surfacelist[surfacelistindex];
742                         texture = surface->texture->currentframe;
743                         if ((texture->currentmaterialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT | MATERIALFLAG_WALL)) != MATERIALFLAG_WALL)
744                                 continue;
745                         if (texture->textureflags & (Q3TEXTUREFLAG_TWOSIDED | Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
746                                 continue;
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);
748                 }
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);
750         }
751         else
752         {
753                 projectdistance = lightradius + ent->model->radius*2;
754                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
755                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
756                 {
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)
761                                 continue;
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);
767                 }
768         }
769 }
770
771 #define RSURF_MAX_BATCHSURFACES 1024
772
773 void R_Q1BSP_DrawLight(entity_render_t *ent, float *lightcolorbase, float *lightcolorpants, float *lightcolorshirt, int numsurfaces, const int *surfacelist)
774 {
775         model_t *model = ent->model;
776         msurface_t *surface;
777         texture_t *texture;
778         int surfacelistindex, batchnumsurfaces;
779         msurface_t *batchsurfacelist[RSURF_MAX_BATCHSURFACES];
780         vec3_t modelorg;
781         texture_t *tex;
782         rtexture_t *basetexture = NULL;
783         rtexture_t *glosstexture = NULL;
784         float specularscale = 0;
785         qboolean skip;
786         if (r_drawcollisionbrushes.integer >= 2)
787                 return;
788         if (VectorLength2(lightcolorbase) + VectorLength2(lightcolorpants) + VectorLength2(lightcolorshirt) < 0.0001)
789                 return;
790         R_UpdateAllTextureInfo(ent);
791         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
792         tex = NULL;
793         texture = NULL;
794         skip = false;
795         batchnumsurfaces = 0;
796         for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
797         {
798                 if ((ent == r_refdef.worldentity && !r_worldsurfacevisible[surfacelist[surfacelistindex]]))
799                         continue;
800                 surface = model->data_surfaces + surfacelist[surfacelistindex];
801                 renderstats.lights_lighttriangles += surface->num_triangles;
802                 if (tex != surface->texture)
803                 {
804                         if (batchnumsurfaces > 0)
805                         {
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;
808                         }
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;
813                         if (skip)
814                                 continue;
815                         if (texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
816                                 qglDisable(GL_CULL_FACE);
817                         else
818                                 qglEnable(GL_CULL_FACE);
819                         glosstexture = r_texture_black;
820                         specularscale = 0;
821                         if (texture->skin.gloss)
822                         {
823                                 if (r_shadow_gloss.integer >= 1 && r_shadow_glossintensity.value > 0 && r_shadow_rtlight->specularscale > 0)
824                                 {
825                                         glosstexture = texture->skin.gloss;
826                                         specularscale = r_shadow_rtlight->specularscale * r_shadow_glossintensity.value;
827                                 }
828                         }
829                         else
830                         {
831                                 if (r_shadow_gloss.integer >= 2 && r_shadow_gloss2intensity.value > 0 && r_shadow_glossintensity.value > 0 && r_shadow_rtlight->specularscale > 0)
832                                 {
833                                         glosstexture = r_texture_white;
834                                         specularscale = r_shadow_rtlight->specularscale * r_shadow_gloss2intensity.value;
835                                 }
836                         }
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))
839                                 skip = true;
840                 }
841                 if (!skip && surface->num_triangles)
842                 {
843                         if (batchnumsurfaces == RSURF_MAX_BATCHSURFACES)
844                         {
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;
847                         }
848                         batchsurfacelist[batchnumsurfaces++] = surface;
849                 }
850         }
851         if (batchnumsurfaces > 0)
852         {
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;
855         }
856         qglEnable(GL_CULL_FACE);
857 }
858
859 #if 0
860 static void gl_surf_start(void)
861 {
862 }
863
864 static void gl_surf_shutdown(void)
865 {
866 }
867
868 static void gl_surf_newmap(void)
869 {
870 }
871 #endif
872
873 void GL_Surf_Init(void)
874 {
875
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);
884
885         //R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
886 }
887