+ // add surfaces to shadow casting mesh and light mesh
+ for (i = 0, face = cl.worldmodel->brushq3.data_thismodel->firstface;i < cl.worldmodel->brushq3.data_thismodel->numfaces;i++, face++)
+ {
+ if (face->lighttemp_castshadow)
+ {
+ face->lighttemp_castshadow = false;
+ if (!(face->texture->surfaceflags & (Q3SURFACEFLAG_NODRAW | Q3SURFACEFLAG_SKY)))
+ {
+ if (rtlight->shadow)
+ if (!(face->texture->nativecontents & CONTENTSQ3_TRANSLUCENT))
+ Mod_ShadowMesh_AddMesh(r_shadow_mempool, castmesh, NULL, NULL, NULL, face->data_vertex3f, NULL, NULL, NULL, NULL, face->num_triangles, face->data_element3i);
+ if (!(face->texture->surfaceflags & Q3SURFACEFLAG_SKY))
+ Mod_ShadowMesh_AddMesh(r_shadow_mempool, rtlight->static_meshchain_light, face->texture->skin.base, face->texture->skin.gloss, face->texture->skin.nmap, face->data_vertex3f, face->data_svector3f, face->data_tvector3f, face->data_normal3f, face->data_texcoordtexture2f, face->num_triangles, face->data_element3i);
+ }
+ }
+ }
+ }
+ else if (cl.worldmodel->brushq1.num_leafs)
+ {
+ mleaf_t *leaf;
+ msurface_t *surf;
+ VectorCopy(rtlight->shadoworigin, rtlight->cullmins);
+ VectorCopy(rtlight->shadoworigin, rtlight->cullmaxs);
+ i = CL_PointQ1Contents(rtlight->shadoworigin);
+
+ for (i = 0, surf = cl.worldmodel->brushq1.surfaces + cl.worldmodel->brushq1.firstmodelsurface;i < cl.worldmodel->brushq1.nummodelsurfaces;i++, surf++)
+ surf->lighttemp_castshadow = false;
+
+ if (r_shadow_portallight.integer && i != CONTENTS_SOLID && i != CONTENTS_SKY)
+ {
+ qbyte *byteleafpvs;
+ qbyte *bytesurfacepvs;
+
+ byteleafpvs = Mem_Alloc(tempmempool, cl.worldmodel->brushq1.num_leafs);
+ bytesurfacepvs = Mem_Alloc(tempmempool, cl.worldmodel->brushq1.numsurfaces);
+
+ Portal_Visibility(cl.worldmodel, rtlight->shadoworigin, byteleafpvs, bytesurfacepvs, NULL, 0, true, mins, maxs, rtlight->cullmins, rtlight->cullmaxs);
+
+ // make a pvs that only includes things within the box
+ for (i = 0, leaf = cl.worldmodel->brushq1.data_leafs;i < cl.worldmodel->brushq1.num_leafs;i++, leaf++)
+ {
+ if (byteleafpvs[i] && BoxesOverlap(leaf->mins, leaf->maxs, mins, maxs))
+ {
+ SETPVSBIT(lightpvs, leaf->clusterindex);
+ for (k = 0;k < 3;k++)
+ {
+ if (rtlight->cullmins[k] > leaf->mins[k]) rtlight->cullmins[k] = leaf->mins[k];
+ if (rtlight->cullmaxs[k] < leaf->maxs[k]) rtlight->cullmaxs[k] = leaf->maxs[k];
+ }
+ }
+ }
+
+ for (i = 0, surf = cl.worldmodel->brushq1.surfaces;i < cl.worldmodel->brushq1.numsurfaces;i++, surf++)
+ if (bytesurfacepvs[i] && BoxesOverlap(surf->poly_mins, surf->poly_maxs, mins, maxs))
+ surf->lighttemp_castshadow = true;
+
+ Mem_Free(byteleafpvs);
+ Mem_Free(bytesurfacepvs);
+
+ // make a cluster list for fast visibility checking during rendering
+ for (i = 0, rtlight->static_numclusters = 0;i < cl.worldmodel->brush.num_pvsclusters;i++)
+ if (CHECKPVSBIT(lightpvs, i))
+ rtlight->static_numclusters++;
+ rtlight->static_clusterindices = Mem_Alloc(r_shadow_mempool, rtlight->static_numclusters * sizeof(int));
+ for (i = 0, rtlight->static_numclusters = 0;i < cl.worldmodel->brush.num_pvsclusters;i++)
+ if (CHECKPVSBIT(lightpvs, i))
+ rtlight->static_clusterindices[rtlight->static_numclusters++] = i;
+ }
+ else
+ {
+ for (i = 0, leaf = cl.worldmodel->brushq1.data_leafs;i < cl.worldmodel->brushq1.num_leafs;i++, leaf++)
+ {
+ if (CHECKPVSBIT(lightfullpvs, leaf->clusterindex) && BoxesOverlap(leaf->mins, leaf->maxs, mins, maxs))
+ {
+ // make a pvs that only includes things within the box
+ SETPVSBIT(lightpvs, leaf->clusterindex);
+ for (k = 0;k < 3;k++)
+ {
+ if (rtlight->cullmins[k] > leaf->mins[k]) rtlight->cullmins[k] = leaf->mins[k];
+ if (rtlight->cullmaxs[k] < leaf->maxs[k]) rtlight->cullmaxs[k] = leaf->maxs[k];
+ }
+ for (j = 0;j < leaf->nummarksurfaces;j++)
+ {
+ surf = cl.worldmodel->brushq1.surfaces + leaf->firstmarksurface[j];
+ if (!surf->lighttemp_castshadow && BoxesOverlap(surf->poly_mins, surf->poly_maxs, mins, maxs))
+ surf->lighttemp_castshadow = true;
+ }
+ }
+ }
+
+ // make a pvs that only includes things within the box
+ for (i = 0, leaf = cl.worldmodel->brushq1.data_leafs;i < cl.worldmodel->brushq1.num_leafs;i++, leaf++)
+ if (CHECKPVSBIT(lightfullpvs, leaf->clusterindex) && BoxesOverlap(leaf->mins, leaf->maxs, mins, maxs))
+ SETPVSBIT(lightpvs, leaf->clusterindex);
+
+ // make a cluster list for fast visibility checking during rendering
+ for (i = 0, rtlight->static_numclusters = 0;i < cl.worldmodel->brush.num_pvsclusters;i++)
+ if (CHECKPVSBIT(lightpvs, i))
+ rtlight->static_numclusters++;
+ rtlight->static_clusterindices = Mem_Alloc(r_shadow_mempool, rtlight->static_numclusters * sizeof(int));
+ for (i = 0, rtlight->static_numclusters = 0;i < cl.worldmodel->brush.num_pvsclusters;i++)
+ if (CHECKPVSBIT(lightpvs, i))
+ rtlight->static_clusterindices[rtlight->static_numclusters++] = i;
+ }
+
+ // add surfaces to shadow casting mesh and light mesh
+ for (i = 0, surf = cl.worldmodel->brushq1.surfaces + cl.worldmodel->brushq1.firstmodelsurface;i < cl.worldmodel->brushq1.nummodelsurfaces;i++, surf++)
+ {
+ if (surf->lighttemp_castshadow)
+ {
+ surf->lighttemp_castshadow = false;
+ if (rtlight->shadow && (surf->flags & SURF_SHADOWCAST))
+ Mod_ShadowMesh_AddMesh(r_shadow_mempool, castmesh, NULL, NULL, NULL, surf->mesh.data_vertex3f, NULL, NULL, NULL, NULL, surf->mesh.num_triangles, surf->mesh.data_element3i);
+ if (!(surf->flags & SURF_DRAWSKY))
+ Mod_ShadowMesh_AddMesh(r_shadow_mempool, rtlight->static_meshchain_light, surf->texinfo->texture->skin.base, surf->texinfo->texture->skin.gloss, surf->texinfo->texture->skin.nmap, surf->mesh.data_vertex3f, surf->mesh.data_svector3f, surf->mesh.data_tvector3f, surf->mesh.data_normal3f, surf->mesh.data_texcoordtexture2f, surf->mesh.num_triangles, surf->mesh.data_element3i);
+ }
+ }
+ }