+ if (ent != &cl_entities[0].render)
+ R_PrepareBrushModel(ent);
+ R_PrepareSurfaces(ent);
+ R_UpdateTextureInfo(ent);
+ R_UpdateLightmapInfo(ent);
+ R_DrawSurfaces(ent, SURF_DRAWTURB | SURF_LIGHTMAP);
+}
+
+void R_Q1BSP_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];
+ msurface_t *surface;
+ mleaf_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->brushq1.data_leafs;leafindex < model->brushq1.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->nummarksurfaces;marksurfaceindex++)
+ {
+ surfaceindex = leaf->firstmarksurface[marksurfaceindex];
+ if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
+ {
+ surface = model->brushq1.surfaces + surfaceindex;
+ if (BoxesOverlap(lightmins, lightmaxs, surface->poly_mins, surface->poly_maxs) && (surface->flags & SURF_LIGHTMAP) && !surface->texinfo->texture->skin.fog)
+ {
+ for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.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_Q1BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
+{
+ model_t *model = ent->model;
+ vec3_t lightmins, lightmaxs;
+ msurface_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->brushq1.surfaces + surfacelist[surfacelistindex];
+ for (j = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;j < surface->mesh.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_Q1BSP_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;
+ msurface_t *surface;
+ texture_t *t;
+ 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);
+ R_UpdateTextureInfo(ent);
+ for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
+ {
+ surface = model->brushq1.surfaces + surfacelist[surfacelistindex];
+ if (r_shadow_compilingrtlight)
+ {
+ // if compiling an rtlight, capture the mesh
+ t = surface->texinfo->texture;
+ if (t->flags & SURF_LIGHTMAP && t->skin.fog == NULL)
+ Mod_ShadowMesh_AddMesh(r_shadow_mempool, r_shadow_compilingrtlight->static_meshchain_light, surface->texinfo->texture->skin.base, surface->texinfo->texture->skin.gloss, surface->texinfo->texture->skin.nmap, surface->mesh.data_vertex3f, surface->mesh.data_svector3f, surface->mesh.data_tvector3f, surface->mesh.data_normal3f, surface->mesh.data_texcoordtexture2f, surface->mesh.num_triangles, surface->mesh.data_element3i);
+ }
+ else if (ent != &cl_entities[0].render || surface->visframe == r_framecount)
+ {
+ t = surface->texinfo->texture->currentframe;
+ if (t->flags & SURF_LIGHTMAP)
+ R_Shadow_RenderLighting(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i, surface->mesh.data_vertex3f, surface->mesh.data_svector3f, surface->mesh.data_tvector3f, surface->mesh.data_normal3f, surface->mesh.data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.base, t->skin.nmap, t->skin.gloss, lightcubemap, LIGHTING_DIFFUSE | LIGHTING_SPECULAR);
+ }
+ }
+ }
+}
+
+void R_DrawCollisionBrush(colbrushf_t *brush)
+{
+ int i;
+ rmeshstate_t m;
+ memset(&m, 0, sizeof(m));
+ m.pointer_vertex = brush->points->v;
+ R_Mesh_State(&m);
+ i = ((int)brush) / sizeof(colbrushf_t);
+ GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
+ GL_LockArrays(0, brush->numpoints);
+ R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
+ GL_LockArrays(0, 0);
+}
+
+void R_Q3BSP_DrawCollisionFace(entity_render_t *ent, q3msurface_t *face)
+{
+ int i;
+ rmeshstate_t m;
+ if (!face->num_collisiontriangles)
+ return;
+ memset(&m, 0, sizeof(m));
+ m.pointer_vertex = face->data_collisionvertex3f;
+ R_Mesh_State(&m);
+ i = ((int)face) / sizeof(q3msurface_t);
+ GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
+ GL_LockArrays(0, face->num_collisionvertices);
+ R_Mesh_Draw(face->num_collisionvertices, face->num_collisiontriangles, face->data_collisionelement3i);
+ GL_LockArrays(0, 0);
+}
+
+void R_Q3BSP_DrawSkyFace(entity_render_t *ent, q3msurface_t *face)
+{
+ rmeshstate_t m;
+ if (!face->num_triangles)
+ return;
+ c_faces++;
+ if (skyrendernow)
+ {
+ skyrendernow = false;
+ if (skyrendermasked)
+ R_Sky();
+ }
+ if (!r_q3bsp_renderskydepth.integer)
+ return;
+
+ R_Mesh_Matrix(&ent->matrix);
+
+ GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
+ if (skyrendermasked)
+ {
+ // depth-only (masking)
+ GL_ColorMask(0,0,0,0);
+ // just to make sure that braindead drivers don't draw anything
+ // despite that colormask...
+ GL_BlendFunc(GL_ZERO, GL_ONE);
+ }
+ else
+ {
+ // fog sky
+ GL_BlendFunc(GL_ONE, GL_ZERO);
+ }
+ GL_DepthMask(true);
+ GL_DepthTest(true);
+
+ memset(&m, 0, sizeof(m));
+ m.pointer_vertex = face->data_vertex3f;
+ R_Mesh_State(&m);
+
+ GL_LockArrays(0, face->num_vertices);
+ R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
+ GL_LockArrays(0, 0);
+ GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
+}
+
+void R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(entity_render_t *ent, q3msurface_t *face)
+{
+ rmeshstate_t m;
+ memset(&m, 0, sizeof(m));
+ GL_BlendFunc(GL_ONE, GL_ZERO);
+ GL_DepthMask(true);
+ GL_DepthTest(true);
+ if (face->texture->skin.glow)
+ {
+ m.tex[0] = R_GetTexture(face->texture->skin.glow);
+ m.pointer_texcoord[0] = face->data_texcoordtexture2f;
+ GL_Color(1, 1, 1, 1);
+ }
+ else
+ GL_Color(0, 0, 0, 1);
+ m.pointer_vertex = face->data_vertex3f;
+ R_Mesh_State(&m);
+ GL_LockArrays(0, face->num_vertices);
+ R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
+ GL_LockArrays(0, 0);
+}
+
+void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(entity_render_t *ent, q3msurface_t *face)
+{
+ rmeshstate_t m;
+ memset(&m, 0, sizeof(m));
+ GL_BlendFunc(GL_ONE, GL_ZERO);
+ GL_DepthMask(true);
+ GL_DepthTest(true);
+ m.tex[0] = R_GetTexture(face->texture->skin.base);
+ m.pointer_texcoord[0] = face->data_texcoordtexture2f;
+ m.tex[1] = R_GetTexture(face->lightmaptexture);
+ m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
+ m.texrgbscale[1] = 2;
+ GL_Color(1, 1, 1, 1);
+ m.pointer_vertex = face->data_vertex3f;
+ R_Mesh_State(&m);
+ GL_LockArrays(0, face->num_vertices);
+ R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
+ GL_LockArrays(0, 0);
+}
+
+void R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(entity_render_t *ent, q3msurface_t *face)
+{
+ rmeshstate_t m;
+ memset(&m, 0, sizeof(m));
+ GL_BlendFunc(GL_ONE, GL_ZERO);
+ GL_DepthMask(true);
+ GL_DepthTest(true);
+ m.tex[0] = R_GetTexture(face->texture->skin.base);
+ m.pointer_texcoord[0] = face->data_texcoordtexture2f;
+ GL_Color(1, 1, 1, 1);
+ m.pointer_vertex = face->data_vertex3f;
+ R_Mesh_State(&m);
+ GL_LockArrays(0, face->num_vertices);
+ R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
+ GL_LockArrays(0, 0);
+}
+
+void R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(entity_render_t *ent, q3msurface_t *face)
+{
+ rmeshstate_t m;
+ memset(&m, 0, sizeof(m));
+ GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
+ GL_DepthMask(false);
+ GL_DepthTest(true);
+ m.tex[0] = R_GetTexture(face->lightmaptexture);
+ m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
+ GL_Color(1, 1, 1, 1);
+ m.pointer_vertex = face->data_vertex3f;
+ R_Mesh_State(&m);
+ GL_LockArrays(0, face->num_vertices);
+ R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
+ GL_LockArrays(0, 0);
+}
+
+void R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(entity_render_t *ent, q3msurface_t *face)
+{
+ rmeshstate_t m;
+ memset(&m, 0, sizeof(m));
+ GL_BlendFunc(GL_ONE, GL_ZERO);
+ GL_DepthMask(true);
+ GL_DepthTest(true);
+ m.tex[0] = R_GetTexture(face->lightmaptexture);
+ m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
+ GL_Color(r_lightmapintensity, r_lightmapintensity, r_lightmapintensity, 1);
+ m.pointer_vertex = face->data_vertex3f;
+ R_Mesh_State(&m);
+ GL_LockArrays(0, face->num_vertices);
+ R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
+ GL_LockArrays(0, 0);
+}
+
+void R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(entity_render_t *ent, q3msurface_t *face)
+{
+ rmeshstate_t m;
+ memset(&m, 0, sizeof(m));
+ GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
+ GL_DepthMask(false);
+ GL_DepthTest(true);
+ if (face->texture->skin.glow)
+ {
+ m.tex[0] = R_GetTexture(face->texture->skin.glow);
+ m.pointer_texcoord[0] = face->data_texcoordtexture2f;
+ GL_Color(1, 1, 1, 1);
+ }
+ else
+ GL_Color(0, 0, 0, 1);
+ m.pointer_vertex = face->data_vertex3f;
+ R_Mesh_State(&m);
+ GL_LockArrays(0, face->num_vertices);
+ R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
+ GL_LockArrays(0, 0);
+}
+
+void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(entity_render_t *ent, q3msurface_t *face)
+{
+ int i;
+ float mul;
+ rmeshstate_t m;
+ memset(&m, 0, sizeof(m));
+ GL_BlendFunc(GL_ONE, GL_ZERO);
+ GL_DepthMask(true);
+ GL_DepthTest(true);
+ m.tex[0] = R_GetTexture(face->texture->skin.base);
+ m.pointer_texcoord[0] = face->data_texcoordtexture2f;
+ mul = 2.0f * r_lightmapintensity;
+ if (mul == 2 && gl_combine.integer)
+ {
+ m.texrgbscale[0] = 2;
+ m.pointer_color = face->data_color4f;
+ }
+ else if (mul == 1)
+ m.pointer_color = face->data_color4f;
+ else
+ {
+ for (i = 0;i < face->num_vertices;i++)
+ {
+ varray_color4f[i*4+0] = face->data_color4f[i*4+0] * mul;
+ varray_color4f[i*4+1] = face->data_color4f[i*4+1] * mul;
+ varray_color4f[i*4+2] = face->data_color4f[i*4+2] * mul;
+ varray_color4f[i*4+3] = face->data_color4f[i*4+3];
+ }
+ m.pointer_color = varray_color4f;
+ }
+ m.pointer_vertex = face->data_vertex3f;
+ R_Mesh_State(&m);
+ GL_LockArrays(0, face->num_vertices);
+ R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
+ GL_LockArrays(0, 0);
+}
+
+void R_Q3BSP_DrawFace_OpaqueWall_Pass_VertexOnly(entity_render_t *ent, q3msurface_t *face)
+{
+ int i;
+ float mul;
+ rmeshstate_t m;
+ memset(&m, 0, sizeof(m));
+ GL_BlendFunc(GL_ONE, GL_ZERO);
+ GL_DepthMask(true);
+ GL_DepthTest(true);
+ mul = 2.0f * r_lightmapintensity;
+ if (mul == 1)
+ m.pointer_color = face->data_color4f;
+ else
+ {
+ for (i = 0;i < face->num_vertices;i++)
+ {
+ varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
+ varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
+ varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
+ varray_color4f[i*4+3] = face->data_color4f[i*4+3];
+ }
+ m.pointer_color = varray_color4f;
+ }
+ m.pointer_vertex = face->data_vertex3f;
+ R_Mesh_State(&m);
+ GL_LockArrays(0, face->num_vertices);
+ R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
+ GL_LockArrays(0, 0);
+}
+
+void R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(entity_render_t *ent, q3msurface_t *face)
+{
+ rmeshstate_t m;
+ memset(&m, 0, sizeof(m));
+ GL_BlendFunc(GL_ONE, GL_ONE);
+ GL_DepthMask(true);
+ GL_DepthTest(true);
+ m.tex[0] = R_GetTexture(face->texture->skin.base);
+ m.pointer_texcoord[0] = face->data_texcoordtexture2f;
+ GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
+ m.pointer_vertex = face->data_vertex3f;
+ R_Mesh_State(&m);
+ GL_LockArrays(0, face->num_vertices);
+ R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
+ GL_LockArrays(0, 0);
+}
+
+void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int facenumber)
+{
+ int i;
+ float colorscale;
+ const entity_render_t *ent = voident;
+ q3msurface_t *face = ent->model->brushq3.data_faces + facenumber;
+ rmeshstate_t m;
+ R_Mesh_Matrix(&ent->matrix);
+ memset(&m, 0, sizeof(m));
+ if (ent->effects & EF_ADDITIVE)
+ GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
+ else
+ GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ GL_DepthMask(false);
+ GL_DepthTest(true);
+ m.tex[0] = R_GetTexture(face->texture->skin.base);
+ m.pointer_texcoord[0] = face->data_texcoordtexture2f;
+ colorscale = r_lightmapintensity;
+ if (gl_combine.integer)
+ m.texrgbscale[0] = 2;
+ else
+ colorscale *= 2.0f;
+ for (i = 0;i < face->num_vertices;i++)
+ {
+ varray_color4f[i*4+0] = face->data_color4f[i*4+0] * colorscale;
+ varray_color4f[i*4+1] = face->data_color4f[i*4+1] * colorscale;
+ varray_color4f[i*4+2] = face->data_color4f[i*4+2] * colorscale;
+ varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
+ }
+ m.pointer_color = varray_color4f;
+ m.pointer_vertex = face->data_vertex3f;
+ R_Mesh_State(&m);
+ qglDisable(GL_CULL_FACE);
+ GL_LockArrays(0, face->num_vertices);
+ R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
+ GL_LockArrays(0, 0);
+ qglEnable(GL_CULL_FACE);
+}
+
+void R_Q3BSP_DrawFace(entity_render_t *ent, q3msurface_t *face)
+{
+ if (!face->num_triangles)
+ return;
+ if (face->texture->surfaceflags && (face->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW)))
+ return;
+ c_faces++;
+ if ((face->texture->surfaceparms & Q3SURFACEPARM_TRANS) || ent->alpha < 1 || (ent->effects & EF_ADDITIVE))
+ {
+ vec3_t facecenter, center;
+ facecenter[0] = (face->mins[0] + face->maxs[0]) * 0.5f;
+ facecenter[1] = (face->mins[1] + face->maxs[1]) * 0.5f;
+ facecenter[2] = (face->mins[2] + face->maxs[2]) * 0.5f;
+ Matrix4x4_Transform(&ent->matrix, facecenter, center);
+ R_MeshQueue_AddTransparent(center, R_Q3BSP_DrawFace_TransparentCallback, ent, face - ent->model->brushq3.data_faces);
+ return;
+ }
+ R_Mesh_Matrix(&ent->matrix);
+ if (r_lightmapintensity <= 0)
+ R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(ent, face);
+ else if (!(ent->flags & RENDER_LIGHT))
+ {
+ R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
+ if (face->texture->skin.glow)
+ R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
+ }
+ else if (face->lightmaptexture)
+ {
+ if (gl_lightmaps.integer)
+ R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(ent, face);
+ else
+ {
+ if (r_textureunits.integer >= 2 && gl_combine.integer)
+ R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(ent, face);
+ else
+ {
+ R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
+ R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(ent, face);
+ }
+ if (face->texture->skin.glow)
+ R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
+ }
+ }
+ else
+ {
+ if (gl_lightmaps.integer)
+ R_Q3BSP_DrawFace_OpaqueWall_Pass_VertexOnly(ent, face);
+ else
+ {
+ 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);
+ }
+ }