+ R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(ent, face);
+ if (face->texture->skin.glow)
+ R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
+ }
+ }
+ if (r_ambient.value)
+ R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(ent, face);
+}
+
+void R_Q3BSP_RecursiveWorldNode(q3mnode_t *node)
+{
+ int i;
+ q3mleaf_t *leaf;
+ for (;;)
+ {
+ if (R_CullBox(node->mins, node->maxs))
+ return;
+ if (!node->plane)
+ break;
+ c_nodes++;
+ R_Q3BSP_RecursiveWorldNode(node->children[0]);
+ node = node->children[1];
+ }
+ leaf = (q3mleaf_t *)node;
+ if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
+ {
+ c_leafs++;
+ for (i = 0;i < leaf->numleaffaces;i++)
+ leaf->firstleafface[i]->visframe = r_framecount;
+ }
+}
+
+// FIXME: num_leafs needs to be recalculated at load time to include only
+// node-referenced leafs, as some maps are incorrectly compiled with leafs for
+// the submodels (which would render the submodels occasionally, as part of
+// the world - not good)
+void R_Q3BSP_MarkLeafPVS(void)
+{
+ int i, j;
+ q3mleaf_t *leaf;
+ for (j = 0, leaf = cl.worldmodel->brushq3.data_leafs;j < cl.worldmodel->brushq3.num_leafs;j++, leaf++)
+ {
+ if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
+ {
+ c_leafs++;
+ for (i = 0;i < leaf->numleaffaces;i++)
+ leaf->firstleafface[i]->visframe = r_framecount;
+ }
+ }
+}
+
+static int r_q3bsp_framecount = -1;
+
+void R_Q3BSP_DrawSky(entity_render_t *ent)
+{
+ int i;
+ q3msurface_t *face;
+ vec3_t modelorg;
+ model_t *model;
+ R_Mesh_Matrix(&ent->matrix);
+ model = ent->model;
+ if (r_drawcollisionbrushes.integer < 2)
+ {
+ Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
+ if (ent == &cl_entities[0].render)
+ {
+ if (r_q3bsp_framecount != r_framecount)
+ {
+ r_q3bsp_framecount = r_framecount;
+ R_Q3BSP_RecursiveWorldNode(model->brushq3.data_nodes);
+ //R_Q3BSP_MarkLeafPVS();
+ }
+ for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
+ if (face->visframe == r_framecount && (face->texture->surfaceflags & Q3SURFACEFLAG_SKY) && !R_CullBox(face->mins, face->maxs))
+ R_Q3BSP_DrawSkyFace(ent, face);
+ }
+ else
+ for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
+ if ((face->texture->surfaceflags & Q3SURFACEFLAG_SKY))
+ R_Q3BSP_DrawSkyFace(ent, face);
+ }
+}
+
+void R_Q3BSP_Draw(entity_render_t *ent)
+{
+ int i;
+ q3msurface_t *face;
+ vec3_t modelorg;
+ model_t *model;
+ qbyte *pvs;
+ R_Mesh_Matrix(&ent->matrix);
+ model = ent->model;
+ if (r_drawcollisionbrushes.integer < 2)
+ {
+ Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
+ if (ent == &cl_entities[0].render)
+ {
+ if (model->brush.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
+ if (r_q3bsp_framecount != r_framecount)
+ {
+ r_q3bsp_framecount = r_framecount;
+ R_Q3BSP_RecursiveWorldNode(model->brushq3.data_nodes);
+ //R_Q3BSP_MarkLeafPVS();
+ }
+ for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
+ if (face->visframe == r_framecount && !R_CullBox(face->mins, face->maxs))
+ R_Q3BSP_DrawFace(ent, face);
+ }
+ else
+ for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
+ R_Q3BSP_DrawFace(ent, face);
+ }
+ if (r_drawcollisionbrushes.integer >= 1)
+ {
+ GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
+ GL_DepthMask(false);
+ GL_DepthTest(true);
+ qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
+ for (i = 0;i < model->brushq3.data_thismodel->numbrushes;i++)
+ if (model->brushq3.data_thismodel->firstbrush[i].colbrushf && model->brushq3.data_thismodel->firstbrush[i].colbrushf->numtriangles)
+ R_DrawCollisionBrush(model->brushq3.data_thismodel->firstbrush[i].colbrushf);
+ for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
+ if (face->num_collisiontriangles)
+ R_Q3BSP_DrawCollisionFace(ent, face);
+ qglPolygonOffset(0, 0);
+ }
+}
+
+void R_Q3BSP_GetLightInfo(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, vec3_t outmins, vec3_t outmaxs, int *outclusterlist, qbyte *outclusterpvs, int *outnumclusterspointer, int *outsurfacelist, qbyte *outsurfacepvs, int *outnumsurfacespointer)
+{
+ model_t *model = ent->model;
+ vec3_t lightmins, lightmaxs;
+ int t, leafindex, marksurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
+ const int *e;
+ const float *v[3];
+ q3msurface_t *surface;
+ q3mleaf_t *leaf;
+ const qbyte *pvs;
+ lightmins[0] = relativelightorigin[0] - lightradius;
+ lightmins[1] = relativelightorigin[1] - lightradius;
+ lightmins[2] = relativelightorigin[2] - lightradius;
+ lightmaxs[0] = relativelightorigin[0] + lightradius;
+ lightmaxs[1] = relativelightorigin[1] + lightradius;
+ lightmaxs[2] = relativelightorigin[2] + lightradius;
+ *outnumclusterspointer = 0;
+ *outnumsurfacespointer = 0;
+ memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
+ memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
+ if (model == NULL)
+ {
+ VectorCopy(lightmins, outmins);
+ VectorCopy(lightmaxs, outmaxs);
+ return;
+ }
+ VectorCopy(relativelightorigin, outmins);
+ VectorCopy(relativelightorigin, outmaxs);
+ if (model->brush.GetPVS)
+ pvs = model->brush.GetPVS(model, relativelightorigin);
+ else
+ pvs = NULL;
+ // FIXME: use BSP recursion as lights are often small
+ for (leafindex = 0, leaf = model->brushq3.data_leafs;leafindex < model->brushq3.num_leafs;leafindex++, leaf++)
+ {
+ if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
+ {
+ outmins[0] = min(outmins[0], leaf->mins[0]);
+ outmins[1] = min(outmins[1], leaf->mins[1]);
+ outmins[2] = min(outmins[2], leaf->mins[2]);
+ outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
+ outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
+ outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
+ if (outclusterpvs)
+ {
+ if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
+ {
+ SETPVSBIT(outclusterpvs, leaf->clusterindex);
+ outclusterlist[outnumclusters++] = leaf->clusterindex;
+ }
+ }
+ if (outsurfacepvs)
+ {
+ for (marksurfaceindex = 0;marksurfaceindex < leaf->numleaffaces;marksurfaceindex++)
+ {
+ surface = leaf->firstleafface[marksurfaceindex];
+ surfaceindex = surface - model->brushq3.data_faces;
+ if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
+ {
+ if (BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs) && !(surface->texture->surfaceparms & Q3SURFACEPARM_TRANS) && !(surface->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW)))
+ {
+ for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->num_triangles;triangleindex++, t++, e += 3)
+ {
+ v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
+ v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
+ v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
+ if (PointInfrontOfTriangle(relativelightorigin, v[0], v[1], v[2]) && lightmaxs[0] > min(v[0][0], min(v[1][0], v[2][0])) && lightmins[0] < max(v[0][0], max(v[1][0], v[2][0])) && lightmaxs[1] > min(v[0][1], min(v[1][1], v[2][1])) && lightmins[1] < max(v[0][1], max(v[1][1], v[2][1])) && lightmaxs[2] > min(v[0][2], min(v[1][2], v[2][2])) && lightmins[2] < max(v[0][2], max(v[1][2], v[2][2])))
+ {
+ SETPVSBIT(outsurfacepvs, surfaceindex);
+ outsurfacelist[outnumsurfaces++] = surfaceindex;
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ // limit combined leaf box to light boundaries
+ outmins[0] = max(outmins[0], lightmins[0]);
+ outmins[1] = max(outmins[1], lightmins[1]);
+ outmins[2] = max(outmins[2], lightmins[2]);
+ outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
+ outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
+ outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
+
+ *outnumclusterspointer = outnumclusters;
+ *outnumsurfacespointer = outnumsurfaces;
+}
+
+void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
+{
+ model_t *model = ent->model;
+ vec3_t lightmins, lightmaxs;
+ q3msurface_t *surface;
+ int surfacelistindex, j, t;
+ const int *e;
+ const float *v[3];
+ if (r_drawcollisionbrushes.integer < 2)
+ {
+ lightmins[0] = relativelightorigin[0] - lightradius;
+ lightmins[1] = relativelightorigin[1] - lightradius;
+ lightmins[2] = relativelightorigin[2] - lightradius;
+ lightmaxs[0] = relativelightorigin[0] + lightradius;
+ lightmaxs[1] = relativelightorigin[1] + lightradius;
+ lightmaxs[2] = relativelightorigin[2] + lightradius;
+ R_Mesh_Matrix(&ent->matrix);
+ R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
+ for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
+ {
+ surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
+ // FIXME: check some manner of face->rendermode here?
+ if (!(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && surface->num_triangles && !surface->texture->skin.fog)
+ {
+ for (j = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;j < surface->num_triangles;j++, t++, e += 3)
+ {
+ v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
+ v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
+ v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
+ if (PointInfrontOfTriangle(relativelightorigin, v[0], v[1], v[2]) && lightmaxs[0] > min(v[0][0], min(v[1][0], v[2][0])) && lightmins[0] < max(v[0][0], max(v[1][0], v[2][0])) && lightmaxs[1] > min(v[0][1], min(v[1][1], v[2][1])) && lightmins[1] < max(v[0][1], max(v[1][1], v[2][1])) && lightmaxs[2] > min(v[0][2], min(v[1][2], v[2][2])) && lightmins[2] < max(v[0][2], max(v[1][2], v[2][2])))
+ shadowmarklist[numshadowmark++] = t;
+ }
+ }
+ }
+ 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 + r_shadow_projectdistance.value, numshadowmark, shadowmarklist);
+ }
+}
+
+void R_Q3BSP_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap, int numsurfaces, const int *surfacelist)
+{
+ model_t *model = ent->model;
+ vec3_t lightmins, lightmaxs, modelorg;
+ q3msurface_t *surface;
+ int surfacelistindex;
+ if (r_drawcollisionbrushes.integer < 2)
+ {
+ lightmins[0] = relativelightorigin[0] - lightradius;
+ lightmins[1] = relativelightorigin[1] - lightradius;
+ lightmins[2] = relativelightorigin[2] - lightradius;
+ lightmaxs[0] = relativelightorigin[0] + lightradius;
+ lightmaxs[1] = relativelightorigin[1] + lightradius;
+ lightmaxs[2] = relativelightorigin[2] + lightradius;
+ R_Mesh_Matrix(&ent->matrix);
+ Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
+ for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
+ {
+ surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
+ if (r_shadow_compilingrtlight)
+ {
+ // if compiling an rtlight, capture the mesh
+ Mod_ShadowMesh_AddMesh(r_shadow_mempool, r_shadow_compilingrtlight->static_meshchain_light, surface->texture->skin.base, surface->texture->skin.gloss, surface->texture->skin.nmap, surface->data_vertex3f, surface->data_svector3f, surface->data_tvector3f, surface->data_normal3f, surface->data_texcoordtexture2f, surface->num_triangles, surface->data_element3i);
+ }
+ else if ((ent != &cl_entities[0].render || surface->visframe == r_framecount) && !(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && surface->num_triangles)
+ R_Shadow_RenderLighting(surface->num_vertices, surface->num_triangles, surface->data_element3i, surface->data_vertex3f, surface->data_svector3f, surface->data_tvector3f, surface->data_normal3f, surface->data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, surface->texture->skin.base, surface->texture->skin.nmap, surface->texture->skin.gloss, lightcubemap, LIGHTING_DIFFUSE | LIGHTING_SPECULAR);