+void R_UpdateTextureInfo(const entity_render_t *ent, texture_t *t)
+{
+ model_t *model = ent->model;
+
+ // FIXME: identify models using a better check than ent->model->brush.shadowmesh
+ //int lightmode = ((ent->effects & EF_FULLBRIGHT) || ent->model->brush.shadowmesh) ? 0 : 2;
+
+ // switch to an alternate material if this is a q1bsp animated material
+ {
+ texture_t *texture = t;
+ int s = ent->skinnum;
+ if ((unsigned int)s >= (unsigned int)model->numskins)
+ s = 0;
+ if (model->skinscenes)
+ {
+ if (model->skinscenes[s].framecount > 1)
+ s = model->skinscenes[s].firstframe + (unsigned int) (r_refdef.time * model->skinscenes[s].framerate) % model->skinscenes[s].framecount;
+ else
+ s = model->skinscenes[s].firstframe;
+ }
+ if (s > 0)
+ t = t + s * model->num_surfaces;
+ if (t->animated)
+ {
+ // use an alternate animation if the entity's frame is not 0,
+ // and only if the texture has an alternate animation
+ if (ent->frame != 0 && t->anim_total[1])
+ t = t->anim_frames[1][(t->anim_total[1] >= 2) ? ((int)(r_refdef.time * 5.0f) % t->anim_total[1]) : 0];
+ else
+ t = t->anim_frames[0][(t->anim_total[0] >= 2) ? ((int)(r_refdef.time * 5.0f) % t->anim_total[0]) : 0];
+ }
+ texture->currentframe = t;
+ }
+
+ // pick a new currentskinframe if the material is animated
+ if (t->numskinframes >= 2)
+ t->currentskinframe = t->skinframes + ((int)(t->skinframerate * (cl.time - ent->frame2time)) % t->numskinframes);
+
+ t->currentmaterialflags = t->basematerialflags;
+ t->currentalpha = ent->alpha;
+ if (t->basematerialflags & MATERIALFLAG_WATERALPHA && (model->brush.supportwateralpha || r_novis.integer))
+ t->currentalpha *= r_wateralpha.value;
+ if (!(ent->flags & RENDER_LIGHT))
+ t->currentmaterialflags |= MATERIALFLAG_FULLBRIGHT;
+ if (ent->effects & EF_ADDITIVE)
+ t->currentmaterialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_TRANSPARENT;
+ else if (t->currentalpha < 1)
+ t->currentmaterialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_TRANSPARENT;
+ if (ent->effects & EF_NODEPTHTEST)
+ t->currentmaterialflags |= MATERIALFLAG_NODEPTHTEST;
+ if (t->currentmaterialflags & MATERIALFLAG_WATER && r_waterscroll.value != 0)
+ t->currenttexmatrix = r_waterscrollmatrix;
+ else
+ t->currenttexmatrix = identitymatrix;
+
+ t->colormapping = VectorLength2(ent->colormap_pantscolor) + VectorLength2(ent->colormap_shirtcolor) >= (1.0f / 1048576.0f);
+ t->basetexture = (!t->colormapping && t->currentskinframe->merged) ? t->currentskinframe->merged : t->currentskinframe->base;
+ t->glosstexture = r_texture_white;
+ t->specularpower = r_shadow_glossexponent.value;
+ t->specularscale = 0;
+ if (r_shadow_gloss.integer > 0)
+ {
+ if (t->currentskinframe->gloss)
+ {
+ if (r_shadow_glossintensity.value > 0)
+ {
+ t->glosstexture = t->currentskinframe->gloss;
+ t->specularscale = r_shadow_glossintensity.value;
+ }
+ }
+ else if (r_shadow_gloss.integer >= 2 && r_shadow_gloss2intensity.value > 0)
+ t->specularscale = r_shadow_gloss2intensity.value;
+ }
+
+ t->currentnumlayers = 0;
+ if (!(t->currentmaterialflags & MATERIALFLAG_NODRAW))
+ {
+ if (gl_lightmaps.integer)
+ R_Texture_AddLayer(t, true, GL_ONE, GL_ZERO, TEXTURELAYERTYPE_LITTEXTURE, r_texture_white, &identitymatrix, 1, 1, 1, 1);
+ else if (!(t->currentmaterialflags & MATERIALFLAG_SKY))
+ {
+ int blendfunc1, blendfunc2, depthmask;
+ if (t->currentmaterialflags & MATERIALFLAG_ADD)
+ {
+ blendfunc1 = GL_SRC_ALPHA;
+ blendfunc2 = GL_ONE;
+ }
+ else if (t->currentmaterialflags & MATERIALFLAG_ALPHA)
+ {
+ blendfunc1 = GL_SRC_ALPHA;
+ blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
+ }
+ else if (t->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND)
+ {
+ blendfunc1 = t->customblendfunc[0];
+ blendfunc2 = t->customblendfunc[1];
+ }
+ else
+ {
+ blendfunc1 = GL_ONE;
+ blendfunc2 = GL_ZERO;
+ }
+ depthmask = !(t->currentmaterialflags & MATERIALFLAG_BLENDED);
+ if (t->currentmaterialflags & (MATERIALFLAG_WATER | MATERIALFLAG_WALL))
+ {
+ rtexture_t *currentbasetexture;
+ int layerflags = 0;
+ if (r_refdef.fogenabled && (t->currentmaterialflags & MATERIALFLAG_BLENDED))
+ layerflags |= TEXTURELAYERFLAG_FOGDARKEN;
+ currentbasetexture = (VectorLength2(ent->colormap_pantscolor) + VectorLength2(ent->colormap_shirtcolor) < (1.0f / 1048576.0f) && t->currentskinframe->merged) ? t->currentskinframe->merged : t->currentskinframe->base;
+ if (t->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
+ {
+ // fullbright is not affected by r_refdef.lightmapintensity
+ R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, TEXTURELAYERTYPE_TEXTURE, currentbasetexture, &t->currenttexmatrix, ent->colormod[0], ent->colormod[1], ent->colormod[2], t->currentalpha);
+ if (VectorLength2(ent->colormap_pantscolor) >= (1.0f / 1048576.0f) && t->currentskinframe->pants)
+ R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->pants, &t->currenttexmatrix, ent->colormap_pantscolor[0] * ent->colormod[0], ent->colormap_pantscolor[1] * ent->colormod[1], ent->colormap_pantscolor[2] * ent->colormod[2], t->currentalpha);
+ if (VectorLength2(ent->colormap_shirtcolor) >= (1.0f / 1048576.0f) && t->currentskinframe->shirt)
+ R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->shirt, &t->currenttexmatrix, ent->colormap_shirtcolor[0] * ent->colormod[0], ent->colormap_shirtcolor[1] * ent->colormod[1], ent->colormap_shirtcolor[2] * ent->colormod[2], t->currentalpha);
+ }
+ else
+ {
+ float colorscale;
+ colorscale = 2;
+ // q3bsp has no lightmap updates, so the lightstylevalue that
+ // would normally be baked into the lightmap must be
+ // applied to the color
+ if (ent->model->type == mod_brushq3)
+ colorscale *= r_refdef.lightstylevalue[0] * (1.0f / 256.0f);
+ colorscale *= r_refdef.lightmapintensity;
+ R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, TEXTURELAYERTYPE_LITTEXTURE, currentbasetexture, &t->currenttexmatrix, ent->colormod[0] * colorscale, ent->colormod[1] * colorscale, ent->colormod[2] * colorscale, t->currentalpha);
+ if (r_ambient.value >= (1.0f/64.0f))
+ R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, currentbasetexture, &t->currenttexmatrix, ent->colormod[0] * r_ambient.value * (1.0f / 64.0f), ent->colormod[1] * r_ambient.value * (1.0f / 64.0f), ent->colormod[2] * r_ambient.value * (1.0f / 64.0f), t->currentalpha);
+ if (VectorLength2(ent->colormap_pantscolor) >= (1.0f / 1048576.0f) && t->currentskinframe->pants)
+ {
+ R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_LITTEXTURE, t->currentskinframe->pants, &t->currenttexmatrix, ent->colormap_pantscolor[0] * ent->colormod[0] * colorscale, ent->colormap_pantscolor[1] * ent->colormod[1] * colorscale, ent->colormap_pantscolor[2] * ent->colormod[2] * colorscale, t->currentalpha);
+ if (r_ambient.value >= (1.0f/64.0f))
+ R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->pants, &t->currenttexmatrix, ent->colormap_pantscolor[0] * ent->colormod[0] * r_ambient.value * (1.0f / 64.0f), ent->colormap_pantscolor[1] * ent->colormod[1] * r_ambient.value * (1.0f / 64.0f), ent->colormap_pantscolor[2] * ent->colormod[2] * r_ambient.value * (1.0f / 64.0f), t->currentalpha);
+ }
+ if (VectorLength2(ent->colormap_shirtcolor) >= (1.0f / 1048576.0f) && t->currentskinframe->shirt)
+ {
+ R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_LITTEXTURE, t->currentskinframe->shirt, &t->currenttexmatrix, ent->colormap_shirtcolor[0] * ent->colormod[0] * colorscale, ent->colormap_shirtcolor[1] * ent->colormod[1] * colorscale, ent->colormap_shirtcolor[2] * ent->colormod[2] * colorscale, t->currentalpha);
+ if (r_ambient.value >= (1.0f/64.0f))
+ R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->shirt, &t->currenttexmatrix, ent->colormap_shirtcolor[0] * ent->colormod[0] * r_ambient.value * (1.0f / 64.0f), ent->colormap_shirtcolor[1] * ent->colormod[1] * r_ambient.value * (1.0f / 64.0f), ent->colormap_shirtcolor[2] * ent->colormod[2] * r_ambient.value * (1.0f / 64.0f), t->currentalpha);
+ }
+ }
+ if (t->currentskinframe->glow != NULL)
+ R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->glow, &t->currenttexmatrix, r_hdr_glowintensity.value, r_hdr_glowintensity.value, r_hdr_glowintensity.value, t->currentalpha);
+ if (r_refdef.fogenabled && !(t->currentmaterialflags & MATERIALFLAG_ADD))
+ {
+ // if this is opaque use alpha blend which will darken the earlier
+ // passes cheaply.
+ //
+ // if this is an alpha blended material, all the earlier passes
+ // were darkened by fog already, so we only need to add the fog
+ // color ontop through the fog mask texture
+ //
+ // if this is an additive blended material, all the earlier passes
+ // were darkened by fog already, and we should not add fog color
+ // (because the background was not darkened, there is no fog color
+ // that was lost behind it).
+ R_Texture_AddLayer(t, false, GL_SRC_ALPHA, (t->currentmaterialflags & MATERIALFLAG_BLENDED) ? GL_ONE : GL_ONE_MINUS_SRC_ALPHA, TEXTURELAYERTYPE_FOG, t->currentskinframe->fog, &identitymatrix, r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2], t->currentalpha);
+ }
+ }
+ }
+ }
+}
+
+void R_UpdateAllTextureInfo(entity_render_t *ent)
+{
+ int i;
+ if (ent->model)
+ for (i = 0;i < ent->model->num_textures;i++)
+ R_UpdateTextureInfo(ent, ent->model->data_textures + i);
+}
+
+int rsurface_array_size = 0;
+float *rsurface_array_modelvertex3f = NULL;
+float *rsurface_array_modelsvector3f = NULL;
+float *rsurface_array_modeltvector3f = NULL;
+float *rsurface_array_modelnormal3f = NULL;
+float *rsurface_array_deformedvertex3f = NULL;
+float *rsurface_array_deformedsvector3f = NULL;
+float *rsurface_array_deformedtvector3f = NULL;
+float *rsurface_array_deformednormal3f = NULL;
+float *rsurface_array_color4f = NULL;
+float *rsurface_array_texcoord3f = NULL;
+
+void R_Mesh_ResizeArrays(int newvertices)
+{
+ float *base;
+ if (rsurface_array_size >= newvertices)
+ return;
+ if (rsurface_array_modelvertex3f)
+ Mem_Free(rsurface_array_modelvertex3f);
+ rsurface_array_size = (newvertices + 1023) & ~1023;
+ base = (float *)Mem_Alloc(r_main_mempool, rsurface_array_size * sizeof(float[31]));
+ rsurface_array_modelvertex3f = base + rsurface_array_size * 0;
+ rsurface_array_modelsvector3f = base + rsurface_array_size * 3;
+ rsurface_array_modeltvector3f = base + rsurface_array_size * 6;
+ rsurface_array_modelnormal3f = base + rsurface_array_size * 9;
+ rsurface_array_deformedvertex3f = base + rsurface_array_size * 12;
+ rsurface_array_deformedsvector3f = base + rsurface_array_size * 15;
+ rsurface_array_deformedtvector3f = base + rsurface_array_size * 18;
+ rsurface_array_deformednormal3f = base + rsurface_array_size * 21;
+ rsurface_array_texcoord3f = base + rsurface_array_size * 24;
+ rsurface_array_color4f = base + rsurface_array_size * 27;
+}
+
+float *rsurface_modelvertex3f;
+float *rsurface_modelsvector3f;
+float *rsurface_modeltvector3f;
+float *rsurface_modelnormal3f;
+float *rsurface_vertex3f;
+float *rsurface_svector3f;
+float *rsurface_tvector3f;
+float *rsurface_normal3f;
+float *rsurface_lightmapcolor4f;
+vec3_t rsurface_modelorg;
+qboolean rsurface_generatedvertex;
+const entity_render_t *rsurface_entity;
+const model_t *rsurface_model;
+texture_t *rsurface_texture;
+rtexture_t *rsurface_lightmaptexture;
+rsurfmode_t rsurface_mode;
+texture_t *rsurface_glsl_texture;
+qboolean rsurface_glsl_uselightmap;
+
+void RSurf_CleanUp(void)
+{
+ CHECKGLERROR
+ if (rsurface_mode == RSURFMODE_GLSL)
+ {
+ qglUseProgramObjectARB(0);CHECKGLERROR
+ }
+ GL_AlphaTest(false);
+ rsurface_mode = RSURFMODE_NONE;
+ rsurface_lightmaptexture = NULL;
+ rsurface_texture = NULL;
+ rsurface_glsl_texture = NULL;
+ rsurface_glsl_uselightmap = false;
+}
+
+void RSurf_ActiveEntity(const entity_render_t *ent, qboolean wantnormals, qboolean wanttangents)
+{
+ RSurf_CleanUp();
+ Matrix4x4_Transform(&ent->inversematrix, r_view.origin, rsurface_modelorg);
+ rsurface_entity = ent;
+ rsurface_model = ent->model;
+ if (rsurface_array_size < rsurface_model->surfmesh.num_vertices)
+ R_Mesh_ResizeArrays(rsurface_model->surfmesh.num_vertices);
+ R_Mesh_Matrix(&ent->matrix);
+ Matrix4x4_Transform(&ent->inversematrix, r_view.origin, rsurface_modelorg);
+ if ((rsurface_entity->frameblend[0].lerp != 1 || rsurface_entity->frameblend[0].frame != 0) && rsurface_model->surfmesh.isanimated)
+ {
+ if (wanttangents)
+ {
+ rsurface_modelvertex3f = rsurface_array_modelvertex3f;
+ rsurface_modelsvector3f = rsurface_array_modelsvector3f;
+ rsurface_modeltvector3f = rsurface_array_modeltvector3f;
+ rsurface_modelnormal3f = rsurface_array_modelnormal3f;
+ Mod_Alias_GetMesh_Vertices(rsurface_model, rsurface_entity->frameblend, rsurface_array_modelvertex3f, rsurface_array_modelnormal3f, rsurface_array_modelsvector3f, rsurface_array_modeltvector3f);
+ }
+ else if (wantnormals)
+ {
+ rsurface_modelvertex3f = rsurface_array_modelvertex3f;
+ rsurface_modelsvector3f = NULL;
+ rsurface_modeltvector3f = NULL;
+ rsurface_modelnormal3f = rsurface_array_modelnormal3f;
+ Mod_Alias_GetMesh_Vertices(rsurface_model, rsurface_entity->frameblend, rsurface_array_modelvertex3f, rsurface_array_modelnormal3f, NULL, NULL);
+ }
+ else
+ {
+ rsurface_modelvertex3f = rsurface_array_modelvertex3f;
+ rsurface_modelsvector3f = NULL;
+ rsurface_modeltvector3f = NULL;
+ rsurface_modelnormal3f = NULL;
+ Mod_Alias_GetMesh_Vertices(rsurface_model, rsurface_entity->frameblend, rsurface_array_modelvertex3f, NULL, NULL, NULL);
+ }
+ rsurface_generatedvertex = true;
+ }
+ else
+ {
+ rsurface_modelvertex3f = rsurface_model->surfmesh.data_vertex3f;
+ rsurface_modelsvector3f = rsurface_model->surfmesh.data_svector3f;
+ rsurface_modeltvector3f = rsurface_model->surfmesh.data_tvector3f;
+ rsurface_modelnormal3f = rsurface_model->surfmesh.data_normal3f;
+ rsurface_generatedvertex = false;
+ }
+ rsurface_vertex3f = rsurface_modelvertex3f;
+ rsurface_svector3f = rsurface_modelsvector3f;
+ rsurface_tvector3f = rsurface_modeltvector3f;
+ rsurface_normal3f = rsurface_modelnormal3f;
+}
+
+void RSurf_PrepareVerticesForBatch(qboolean generatenormals, qboolean generatetangents, int texturenumsurfaces, msurface_t **texturesurfacelist)
+{
+ // if vertices are dynamic (animated models), generate them into the temporary rsurface_array_model* arrays and point rsurface_model* at them instead of the static data from the model itself
+ if (rsurface_generatedvertex)
+ {
+ if (rsurface_texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
+ generatetangents = true;
+ if (generatetangents)
+ generatenormals = true;
+ if (generatenormals && !rsurface_modelnormal3f)
+ {
+ rsurface_normal3f = rsurface_modelnormal3f = rsurface_array_modelnormal3f;
+ Mod_BuildNormals(0, rsurface_model->surfmesh.num_vertices, rsurface_model->surfmesh.num_triangles, rsurface_modelvertex3f, rsurface_model->surfmesh.data_element3i, rsurface_array_modelnormal3f, r_smoothnormals_areaweighting.integer);
+ }
+ if (generatetangents && !rsurface_modelsvector3f)
+ {
+ rsurface_svector3f = rsurface_modelsvector3f = rsurface_array_modelsvector3f;
+ rsurface_tvector3f = rsurface_modeltvector3f = rsurface_array_modeltvector3f;
+ Mod_BuildTextureVectorsFromNormals(0, rsurface_model->surfmesh.num_vertices, rsurface_model->surfmesh.num_triangles, rsurface_modelvertex3f, rsurface_model->surfmesh.data_texcoordtexture2f, rsurface_modelnormal3f, rsurface_model->surfmesh.data_element3i, rsurface_array_modelsvector3f, rsurface_array_modeltvector3f, r_smoothnormals_areaweighting.integer);
+ }
+ }
+ // if vertices are deformed (sprite flares and things in maps, possibly water waves, bulges and other deformations), generate them into rsurface_deform* arrays from whatever the rsurface_model* array pointers point to (may be static model data or generated data for an animated model)
+ if (rsurface_texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
+ {
+ int texturesurfaceindex;
+ float center[3], forward[3], right[3], up[3], v[4][3];
+ matrix4x4_t matrix1, imatrix1;
+ Matrix4x4_Transform(&rsurface_entity->inversematrix, r_view.forward, forward);
+ Matrix4x4_Transform(&rsurface_entity->inversematrix, r_view.right, right);
+ Matrix4x4_Transform(&rsurface_entity->inversematrix, r_view.up, up);
+ // make deformed versions of only the model vertices used by the specified surfaces
+ for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
+ {
+ int i, j;
+ const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
+ // a single autosprite surface can contain multiple sprites...
+ for (j = 0;j < surface->num_vertices - 3;j += 4)
+ {
+ VectorClear(center);
+ for (i = 0;i < 4;i++)
+ VectorAdd(center, (rsurface_modelvertex3f + 3 * surface->num_firstvertex) + (j+i) * 3, center);
+ VectorScale(center, 0.25f, center);
+ if (rsurface_texture->textureflags & Q3TEXTUREFLAG_AUTOSPRITE2)
+ {
+ forward[0] = rsurface_modelorg[0] - center[0];
+ forward[1] = rsurface_modelorg[1] - center[1];
+ forward[2] = 0;
+ VectorNormalize(forward);
+ right[0] = forward[1];
+ right[1] = -forward[0];
+ right[2] = 0;
+ VectorSet(up, 0, 0, 1);
+ }
+ // FIXME: calculate vectors from triangle edges instead of using texture vectors as an easy way out?
+ Matrix4x4_FromVectors(&matrix1, (rsurface_modelnormal3f + 3 * surface->num_firstvertex) + j*3, (rsurface_modelsvector3f + 3 * surface->num_firstvertex) + j*3, (rsurface_modeltvector3f + 3 * surface->num_firstvertex) + j*3, center);
+ Matrix4x4_Invert_Simple(&imatrix1, &matrix1);
+ for (i = 0;i < 4;i++)
+ Matrix4x4_Transform(&imatrix1, (rsurface_modelvertex3f + 3 * surface->num_firstvertex) + (j+i)*3, v[i]);
+ for (i = 0;i < 4;i++)
+ VectorMAMAMAM(1, center, v[i][0], forward, v[i][1], right, v[i][2], up, rsurface_array_deformedvertex3f + (surface->num_firstvertex+i+j) * 3);
+ }
+ Mod_BuildNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, rsurface_modelvertex3f, rsurface_model->surfmesh.data_element3i + surface->num_firsttriangle * 3, rsurface_array_deformednormal3f, r_smoothnormals_areaweighting.integer);
+ Mod_BuildTextureVectorsFromNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, rsurface_modelvertex3f, rsurface_model->surfmesh.data_texcoordtexture2f, rsurface_array_deformednormal3f, rsurface_model->surfmesh.data_element3i + surface->num_firsttriangle * 3, rsurface_array_deformedsvector3f, rsurface_array_deformedtvector3f, r_smoothnormals_areaweighting.integer);
+ }
+ rsurface_vertex3f = rsurface_array_deformedvertex3f;
+ rsurface_svector3f = rsurface_array_deformedsvector3f;
+ rsurface_tvector3f = rsurface_array_deformedtvector3f;
+ rsurface_normal3f = rsurface_array_deformednormal3f;
+ }
+ else
+ {
+ rsurface_vertex3f = rsurface_modelvertex3f;
+ rsurface_svector3f = rsurface_modelsvector3f;
+ rsurface_tvector3f = rsurface_modeltvector3f;
+ rsurface_normal3f = rsurface_modelnormal3f;
+ }
+ R_Mesh_VertexPointer(rsurface_vertex3f);
+}
+
+void RSurf_DrawBatch_Simple(int texturenumsurfaces, msurface_t **texturesurfacelist)
+{
+ int texturesurfaceindex;
+ const msurface_t *surface = texturesurfacelist[0];
+ int firstvertex = surface->num_firstvertex;
+ int endvertex = surface->num_firstvertex + surface->num_vertices;
+ if (texturenumsurfaces == 1)
+ {
+ GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
+ R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface_model->surfmesh.data_element3i + 3 * surface->num_firsttriangle));
+ }
+ else if (r_batchmode.integer == 2)
+ {
+ #define MAXBATCHTRIANGLES 4096
+ int batchtriangles = 0;
+ int batchelements[MAXBATCHTRIANGLES*3];
+ for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
+ {
+ surface = texturesurfacelist[texturesurfaceindex];
+ if (surface->num_triangles >= 256 || (batchtriangles == 0 && texturesurfaceindex + 1 >= texturenumsurfaces))
+ {
+ R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface_model->surfmesh.data_element3i + 3 * surface->num_firsttriangle));
+ continue;
+ }
+ if (batchtriangles + surface->num_triangles > MAXBATCHTRIANGLES)
+ {
+ R_Mesh_Draw(firstvertex, endvertex - firstvertex, batchtriangles, batchelements);
+ batchtriangles = 0;
+ firstvertex = surface->num_firstvertex;
+ endvertex = surface->num_firstvertex + surface->num_vertices;
+ }
+ else
+ {
+ firstvertex = min(firstvertex, surface->num_firstvertex);
+ endvertex = max(endvertex, surface->num_firstvertex + surface->num_vertices);
+ }
+ memcpy(batchelements + batchtriangles * 3, rsurface_model->surfmesh.data_element3i + 3 * surface->num_firsttriangle, surface->num_triangles * sizeof(int[3]));
+ batchtriangles += surface->num_triangles;
+ }
+ if (batchtriangles)
+ R_Mesh_Draw(firstvertex, endvertex - firstvertex, batchtriangles, batchelements);
+ }
+ else if (r_batchmode.integer == 1)
+ {
+ int firsttriangle = 0;
+ int endtriangle = -1;
+ for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
+ {
+ surface = texturesurfacelist[texturesurfaceindex];
+ if (surface->num_firsttriangle != endtriangle)
+ {
+ if (endtriangle > firsttriangle)
+ {
+ GL_LockArrays(firstvertex, endvertex - firstvertex);
+ R_Mesh_Draw(firstvertex, endvertex - firstvertex, endtriangle - firsttriangle, (rsurface_model->surfmesh.data_element3i + 3 * firsttriangle));
+ }
+ firstvertex = surface->num_firstvertex;
+ endvertex = surface->num_firstvertex + surface->num_vertices;
+ firsttriangle = surface->num_firsttriangle;
+ }
+ else
+ {
+ firstvertex = min(firstvertex, surface->num_firstvertex);
+ endvertex = max(endvertex, surface->num_firstvertex + surface->num_vertices);
+ }
+ endtriangle = surface->num_firsttriangle + surface->num_triangles;
+ }
+ if (endtriangle > firsttriangle)
+ {
+ GL_LockArrays(firstvertex, endvertex - firstvertex);
+ R_Mesh_Draw(firstvertex, endvertex - firstvertex, endtriangle - firsttriangle, (rsurface_model->surfmesh.data_element3i + 3 * firsttriangle));
+ }
+ }
+ else
+ {
+ for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
+ {
+ surface = texturesurfacelist[texturesurfaceindex];
+ GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
+ R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface_model->surfmesh.data_element3i + 3 * surface->num_firsttriangle));
+ }
+ }
+}
+
+static void RSurf_DrawBatch_ShowSurfaces(int texturenumsurfaces, msurface_t **texturesurfacelist)
+{
+ int texturesurfaceindex;
+ for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
+ {
+ const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
+ int k = (int)(((size_t)surface) / sizeof(msurface_t));
+ GL_Color((k & 15) * (1.0f / 16.0f) * r_view.colorscale, ((k >> 4) & 15) * (1.0f / 16.0f) * r_view.colorscale, ((k >> 8) & 15) * (1.0f / 16.0f) * r_view.colorscale, 0.2f);
+ GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
+ R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface_model->surfmesh.data_element3i + 3 * surface->num_firsttriangle));
+ }
+}
+
+static void RSurf_DrawBatch_Lightmap(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a, int lightmode, qboolean applycolor, qboolean applyfog)
+{
+ int texturesurfaceindex;
+ int i;
+ float f;
+ float *v, *c, *c2;
+ // TODO: optimize
+ if (lightmode >= 2)
+ {
+ // model lighting
+ vec3_t ambientcolor;
+ vec3_t diffusecolor;
+ vec3_t lightdir;
+ VectorCopy(rsurface_entity->modellight_lightdir, lightdir);
+ ambientcolor[0] = rsurface_entity->modellight_ambient[0] * r * 0.5f;
+ ambientcolor[1] = rsurface_entity->modellight_ambient[1] * g * 0.5f;
+ ambientcolor[2] = rsurface_entity->modellight_ambient[2] * b * 0.5f;
+ diffusecolor[0] = rsurface_entity->modellight_diffuse[0] * r * 0.5f;
+ diffusecolor[1] = rsurface_entity->modellight_diffuse[1] * g * 0.5f;
+ diffusecolor[2] = rsurface_entity->modellight_diffuse[2] * b * 0.5f;
+ if (VectorLength2(diffusecolor) > 0)
+ {
+ // generate color arrays for the surfaces in this list
+ for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
+ {
+ const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
+ int numverts = surface->num_vertices;
+ v = rsurface_vertex3f + 3 * surface->num_firstvertex;
+ c2 = rsurface_normal3f + 3 * surface->num_firstvertex;
+ c = rsurface_array_color4f + 4 * surface->num_firstvertex;
+ // q3-style directional shading
+ for (i = 0;i < numverts;i++, v += 3, c2 += 3, c += 4)
+ {
+ if ((f = DotProduct(c2, lightdir)) > 0)
+ VectorMA(ambientcolor, f, diffusecolor, c);
+ else
+ VectorCopy(ambientcolor, c);
+ c[3] = a;
+ }
+ }
+ r = 1;
+ g = 1;
+ b = 1;
+ a = 1;
+ applycolor = false;
+ rsurface_lightmapcolor4f = rsurface_array_color4f;
+ }
+ else
+ {
+ r = ambientcolor[0];
+ g = ambientcolor[1];
+ b = ambientcolor[2];
+ rsurface_lightmapcolor4f = NULL;
+ }
+ }
+ else if (lightmode >= 1 || !rsurface_lightmaptexture)
+ {
+ if (texturesurfacelist[0]->lightmapinfo && texturesurfacelist[0]->lightmapinfo->stainsamples)
+ {
+ // generate color arrays for the surfaces in this list
+ for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
+ {
+ const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
+ for (i = 0, c = rsurface_array_color4f + 4 * surface->num_firstvertex;i < surface->num_vertices;i++, c += 4)
+ {
+ if (surface->lightmapinfo->samples)
+ {
+ const unsigned char *lm = surface->lightmapinfo->samples + (rsurface_model->surfmesh.data_lightmapoffsets + surface->num_firstvertex)[i];
+ float scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[0]] * (1.0f / 32768.0f);
+ VectorScale(lm, scale, c);
+ if (surface->lightmapinfo->styles[1] != 255)
+ {
+ int size3 = ((surface->lightmapinfo->extents[0]>>4)+1)*((surface->lightmapinfo->extents[1]>>4)+1)*3;
+ lm += size3;
+ scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[1]] * (1.0f / 32768.0f);
+ VectorMA(c, scale, lm, c);
+ if (surface->lightmapinfo->styles[2] != 255)
+ {
+ lm += size3;
+ scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[2]] * (1.0f / 32768.0f);
+ VectorMA(c, scale, lm, c);
+ if (surface->lightmapinfo->styles[3] != 255)
+ {
+ lm += size3;
+ scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[3]] * (1.0f / 32768.0f);
+ VectorMA(c, scale, lm, c);
+ }
+ }
+ }
+ }
+ else
+ VectorClear(c);
+ c[3] = 1;
+ }
+ }
+ rsurface_lightmapcolor4f = rsurface_array_color4f;
+ }
+ else
+ rsurface_lightmapcolor4f = rsurface_model->surfmesh.data_lightmapcolor4f;
+ }
+ else
+ {
+ // just lightmap it
+ rsurface_lightmapcolor4f = NULL;
+ }
+ if (applyfog)
+ {
+ if (rsurface_lightmapcolor4f)
+ {
+ // generate color arrays for the surfaces in this list
+ for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
+ {
+ const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
+ for (i = 0, v = (rsurface_vertex3f + 3 * surface->num_firstvertex), c = (rsurface_lightmapcolor4f + 4 * surface->num_firstvertex), c2 = (rsurface_array_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4, c2 += 4)
+ {
+ f = 1 - VERTEXFOGTABLE(VectorDistance(v, rsurface_modelorg));
+ c2[0] = c[0] * f;
+ c2[1] = c[1] * f;
+ c2[2] = c[2] * f;
+ c2[3] = c[3];
+ }
+ }
+ }
+ else
+ {
+ for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
+ {
+ const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
+ for (i = 0, v = (rsurface_vertex3f + 3 * surface->num_firstvertex), c2 = (rsurface_array_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c2 += 4)
+ {
+ f = 1 - VERTEXFOGTABLE(VectorDistance(v, rsurface_modelorg));
+ c2[0] = f;
+ c2[1] = f;
+ c2[2] = f;
+ c2[3] = 1;
+ }
+ }
+ }
+ rsurface_lightmapcolor4f = rsurface_array_color4f;
+ }
+ if (applycolor && rsurface_lightmapcolor4f)
+ {
+ for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
+ {
+ const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
+ for (i = 0, c = (rsurface_lightmapcolor4f + 4 * surface->num_firstvertex), c2 = (rsurface_array_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, c += 4, c2 += 4)
+ {
+ c2[0] = c[0] * r;
+ c2[1] = c[1] * g;
+ c2[2] = c[2] * b;
+ c2[3] = c[3] * a;
+ }
+ }
+ rsurface_lightmapcolor4f = rsurface_array_color4f;
+ }
+ R_Mesh_ColorPointer(rsurface_lightmapcolor4f);
+ GL_Color(r, g, b, a);
+ RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
+}
+
+static void R_DrawTextureSurfaceList_ShowSurfaces(int texturenumsurfaces, msurface_t **texturesurfacelist)
+{
+ if (rsurface_mode != RSURFMODE_SHOWSURFACES)
+ {
+ rsurface_mode = RSURFMODE_SHOWSURFACES;
+ GL_DepthMask(true);
+ GL_BlendFunc(GL_ONE, GL_ZERO);
+ R_Mesh_ColorPointer(NULL);
+ R_Mesh_ResetTextureState();
+ }
+ RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
+ RSurf_DrawBatch_ShowSurfaces(texturenumsurfaces, texturesurfacelist);
+}
+
+static void R_DrawTextureSurfaceList_Sky(int texturenumsurfaces, msurface_t **texturesurfacelist)
+{
+ // transparent sky would be ridiculous
+ if ((rsurface_texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
+ return;
+ if (rsurface_mode != RSURFMODE_SKY)
+ {
+ if (rsurface_mode == RSURFMODE_GLSL)
+ {
+ qglUseProgramObjectARB(0);CHECKGLERROR
+ }
+ rsurface_mode = RSURFMODE_SKY;
+ }
+ if (skyrendernow)
+ {
+ skyrendernow = false;
+ R_Sky();
+ // restore entity matrix
+ R_Mesh_Matrix(&rsurface_entity->matrix);
+ }
+ GL_DepthMask(true);
+ // LordHavoc: HalfLife maps have freaky skypolys so don't use
+ // skymasking on them, and Quake3 never did sky masking (unlike
+ // software Quake and software Quake2), so disable the sky masking
+ // in Quake3 maps as it causes problems with q3map2 sky tricks,
+ // and skymasking also looks very bad when noclipping outside the
+ // level, so don't use it then either.
+ if (rsurface_model->type == mod_brushq1 && r_q1bsp_skymasking.integer && !r_viewcache.world_novis)
+ {
+ GL_Color(r_refdef.fogcolor[0] * r_view.colorscale, r_refdef.fogcolor[1] * r_view.colorscale, r_refdef.fogcolor[2] * r_view.colorscale, 1);
+ R_Mesh_ColorPointer(NULL);
+ R_Mesh_ResetTextureState();
+ 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);
+ }
+ RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
+ RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
+ if (skyrendermasked)
+ GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
+ }
+}
+
+static void R_DrawTextureSurfaceList_GL20(int texturenumsurfaces, msurface_t **texturesurfacelist)
+{
+ int lightmode;
+ // FIXME: identify models using a better check than rsurface_model->brush.shadowmesh
+ lightmode = ((rsurface_entity->effects & EF_FULLBRIGHT) || rsurface_model->brush.shadowmesh) ? 0 : 2;
+ if (rsurface_mode != RSURFMODE_GLSL)
+ {
+ rsurface_mode = RSURFMODE_GLSL;
+ rsurface_glsl_texture = NULL;
+ rsurface_glsl_uselightmap = false;
+ R_Mesh_ResetTextureState();
+ }
+ if (rsurface_glsl_texture != rsurface_texture || rsurface_glsl_uselightmap != (rsurface_lightmaptexture != NULL))
+ {
+ rsurface_glsl_texture = rsurface_texture;
+ rsurface_glsl_uselightmap = rsurface_lightmaptexture != NULL;
+ GL_BlendFunc(rsurface_texture->currentlayers[0].blendfunc1, rsurface_texture->currentlayers[0].blendfunc2);
+ GL_DepthMask(!(rsurface_texture->currentmaterialflags & MATERIALFLAG_BLENDED));
+ GL_Color(rsurface_entity->colormod[0], rsurface_entity->colormod[1], rsurface_entity->colormod[2], rsurface_texture->currentalpha);
+ R_SetupSurfaceShader(vec3_origin, lightmode == 2);
+ //permutation_deluxemapping = permutation_lightmapping = R_SetupSurfaceShader(vec3_origin, lightmode == 2, false);
+ //if (r_glsl_deluxemapping.integer)
+ // permutation_deluxemapping = R_SetupSurfaceShader(vec3_origin, lightmode == 2, true);
+ R_Mesh_TexCoordPointer(0, 2, rsurface_model->surfmesh.data_texcoordtexture2f);
+ R_Mesh_TexCoordPointer(4, 2, rsurface_model->surfmesh.data_texcoordlightmap2f);
+ GL_AlphaTest((rsurface_texture->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
+ }
+ if (!r_glsl_permutation)
+ return;
+ RSurf_PrepareVerticesForBatch(true, true, texturenumsurfaces, texturesurfacelist);
+ R_Mesh_TexCoordPointer(1, 3, rsurface_svector3f);
+ R_Mesh_TexCoordPointer(2, 3, rsurface_tvector3f);
+ R_Mesh_TexCoordPointer(3, 3, rsurface_normal3f);
+ if (rsurface_texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
+ {
+ R_Mesh_TexBind(7, R_GetTexture(r_texture_white));
+ if (r_glsl_permutation->loc_Texture_Deluxemap >= 0)
+ R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
+ R_Mesh_ColorPointer(NULL);
+ }
+ else if (rsurface_lightmaptexture)
+ {
+ R_Mesh_TexBind(7, R_GetTexture(rsurface_lightmaptexture));
+ if (r_glsl_permutation->loc_Texture_Deluxemap >= 0)
+ R_Mesh_TexBind(8, R_GetTexture(texturesurfacelist[0]->deluxemaptexture));
+ R_Mesh_ColorPointer(NULL);
+ }
+ else
+ {
+ R_Mesh_TexBind(7, R_GetTexture(r_texture_white));
+ if (r_glsl_permutation->loc_Texture_Deluxemap >= 0)
+ R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
+ R_Mesh_ColorPointer(rsurface_model->surfmesh.data_lightmapcolor4f);
+ }
+ RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
+}
+
+static void R_DrawTextureSurfaceList_GL13(int texturenumsurfaces, msurface_t **texturesurfacelist)
+{
+ // OpenGL 1.3 path - anything not completely ancient
+ int texturesurfaceindex;
+ int lightmode;
+ qboolean applycolor;
+ qboolean applyfog;
+ rmeshstate_t m;
+ int layerindex;
+ const texturelayer_t *layer;
+ CHECKGLERROR
+ // FIXME: identify models using a better check than rsurface_model->brush.shadowmesh
+ lightmode = ((rsurface_entity->effects & EF_FULLBRIGHT) || rsurface_model->brush.shadowmesh) ? 0 : 2;
+ if (rsurface_mode != RSURFMODE_MULTIPASS)
+ rsurface_mode = RSURFMODE_MULTIPASS;
+ RSurf_PrepareVerticesForBatch(true, false, texturenumsurfaces, texturesurfacelist);
+ for (layerindex = 0, layer = rsurface_texture->currentlayers;layerindex < rsurface_texture->currentnumlayers;layerindex++, layer++)
+ {
+ vec4_t layercolor;
+ int layertexrgbscale;
+ if (rsurface_texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
+ {
+ if (layerindex == 0)
+ GL_AlphaTest(true);
+ else
+ {
+ GL_AlphaTest(false);
+ qglDepthFunc(GL_EQUAL);CHECKGLERROR
+ }
+ }
+ GL_DepthMask(layer->depthmask);
+ GL_BlendFunc(layer->blendfunc1, layer->blendfunc2);
+ if ((layer->color[0] > 2 || layer->color[1] > 2 || layer->color[2] > 2) && (gl_combine.integer || layer->depthmask))
+ {
+ layertexrgbscale = 4;
+ VectorScale(layer->color, 0.25f, layercolor);
+ }
+ else if ((layer->color[0] > 1 || layer->color[1] > 1 || layer->color[2] > 1) && (gl_combine.integer || layer->depthmask))
+ {
+ layertexrgbscale = 2;
+ VectorScale(layer->color, 0.5f, layercolor);
+ }
+ else
+ {
+ layertexrgbscale = 1;
+ VectorScale(layer->color, 1.0f, layercolor);
+ }
+ layercolor[3] = layer->color[3];
+ applycolor = layercolor[0] != 1 || layercolor[1] != 1 || layercolor[2] != 1 || layercolor[3] != 1;
+ R_Mesh_ColorPointer(NULL);
+ applyfog = (layer->flags & TEXTURELAYERFLAG_FOGDARKEN) != 0;
+ switch (layer->type)
+ {
+ case TEXTURELAYERTYPE_LITTEXTURE:
+ memset(&m, 0, sizeof(m));
+ if (lightmode >= 1 || !rsurface_lightmaptexture)
+ m.tex[0] = R_GetTexture(r_texture_white);
+ else
+ m.tex[0] = R_GetTexture(rsurface_lightmaptexture);
+ m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordlightmap2f;
+ m.tex[1] = R_GetTexture(layer->texture);
+ m.texmatrix[1] = layer->texmatrix;
+ m.texrgbscale[1] = layertexrgbscale;
+ m.pointer_texcoord[1] = rsurface_model->surfmesh.data_texcoordtexture2f;
+ R_Mesh_TextureState(&m);
+ RSurf_DrawBatch_Lightmap(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], lightmode, applycolor, applyfog);
+ break;
+ case TEXTURELAYERTYPE_TEXTURE:
+ memset(&m, 0, sizeof(m));
+ m.tex[0] = R_GetTexture(layer->texture);
+ m.texmatrix[0] = layer->texmatrix;
+ m.texrgbscale[0] = layertexrgbscale;
+ m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+ R_Mesh_TextureState(&m);
+ RSurf_DrawBatch_Lightmap(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], 0, applycolor, applyfog);
+ break;
+ case TEXTURELAYERTYPE_FOG:
+ memset(&m, 0, sizeof(m));
+ m.texrgbscale[0] = layertexrgbscale;
+ if (layer->texture)
+ {
+ m.tex[0] = R_GetTexture(layer->texture);
+ m.texmatrix[0] = layer->texmatrix;
+ m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+ }
+ R_Mesh_TextureState(&m);
+ // generate a color array for the fog pass
+ R_Mesh_ColorPointer(rsurface_array_color4f);
+ for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
+ {
+ int i;
+ float f, *v, *c;
+ const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
+ for (i = 0, v = (rsurface_vertex3f + 3 * surface->num_firstvertex), c = (rsurface_array_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
+ {
+ f = VERTEXFOGTABLE(VectorDistance(v, rsurface_modelorg));
+ c[0] = layercolor[0];
+ c[1] = layercolor[1];
+ c[2] = layercolor[2];
+ c[3] = f * layercolor[3];
+ }
+ }
+ RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
+ break;
+ default:
+ Con_Printf("R_DrawTextureSurfaceList: unknown layer type %i\n", layer->type);
+ }
+ GL_LockArrays(0, 0);
+ }
+ CHECKGLERROR
+ if (rsurface_texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
+ {
+ qglDepthFunc(GL_LEQUAL);CHECKGLERROR
+ GL_AlphaTest(false);
+ }
+}
+
+static void R_DrawTextureSurfaceList_GL11(int texturenumsurfaces, msurface_t **texturesurfacelist)
+{
+ // OpenGL 1.1 - crusty old voodoo path
+ int texturesurfaceindex;
+ int lightmode;
+ qboolean applyfog;
+ rmeshstate_t m;
+ int layerindex;
+ const texturelayer_t *layer;
+ CHECKGLERROR
+ // FIXME: identify models using a better check than rsurface_model->brush.shadowmesh
+ lightmode = ((rsurface_entity->effects & EF_FULLBRIGHT) || rsurface_model->brush.shadowmesh) ? 0 : 2;
+ if (rsurface_mode != RSURFMODE_MULTIPASS)
+ rsurface_mode = RSURFMODE_MULTIPASS;
+ RSurf_PrepareVerticesForBatch(true, false, texturenumsurfaces, texturesurfacelist);
+ for (layerindex = 0, layer = rsurface_texture->currentlayers;layerindex < rsurface_texture->currentnumlayers;layerindex++, layer++)
+ {
+ if (rsurface_texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
+ {
+ if (layerindex == 0)
+ GL_AlphaTest(true);
+ else
+ {
+ GL_AlphaTest(false);
+ qglDepthFunc(GL_EQUAL);CHECKGLERROR
+ }
+ }
+ GL_DepthMask(layer->depthmask);
+ GL_BlendFunc(layer->blendfunc1, layer->blendfunc2);
+ R_Mesh_ColorPointer(NULL);
+ applyfog = (layer->flags & TEXTURELAYERFLAG_FOGDARKEN) != 0;
+ switch (layer->type)
+ {
+ case TEXTURELAYERTYPE_LITTEXTURE:
+ if (layer->blendfunc1 == GL_ONE && layer->blendfunc2 == GL_ZERO)
+ {
+ // two-pass lit texture with 2x rgbscale
+ // first the lightmap pass
+ memset(&m, 0, sizeof(m));
+ if (lightmode >= 1 || !rsurface_lightmaptexture)
+ m.tex[0] = R_GetTexture(r_texture_white);
+ else
+ m.tex[0] = R_GetTexture(rsurface_lightmaptexture);
+ m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordlightmap2f;
+ R_Mesh_TextureState(&m);
+ RSurf_DrawBatch_Lightmap(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, lightmode, false, false);
+ GL_LockArrays(0, 0);
+ // then apply the texture to it
+ GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
+ memset(&m, 0, sizeof(m));
+ m.tex[0] = R_GetTexture(layer->texture);
+ m.texmatrix[0] = layer->texmatrix;
+ m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+ R_Mesh_TextureState(&m);
+ RSurf_DrawBatch_Lightmap(texturenumsurfaces, texturesurfacelist, layer->color[0] * 0.5f, layer->color[1] * 0.5f, layer->color[2] * 0.5f, layer->color[3], 0, layer->color[0] != 2 || layer->color[1] != 2 || layer->color[2] != 2 || layer->color[3] != 1, false);
+ }
+ else
+ {
+ // single pass vertex-lighting-only texture with 1x rgbscale and transparency support
+ memset(&m, 0, sizeof(m));
+ m.tex[0] = R_GetTexture(layer->texture);
+ m.texmatrix[0] = layer->texmatrix;
+ m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+ R_Mesh_TextureState(&m);
+ RSurf_DrawBatch_Lightmap(texturenumsurfaces, texturesurfacelist, layer->color[0], layer->color[1], layer->color[2], layer->color[3], lightmode == 2 ? 2 : 1, layer->color[0] != 1 || layer->color[1] != 1 || layer->color[2] != 1 || layer->color[3] != 1, applyfog);
+ }
+ break;
+ case TEXTURELAYERTYPE_TEXTURE:
+ // singletexture unlit texture with transparency support
+ memset(&m, 0, sizeof(m));
+ m.tex[0] = R_GetTexture(layer->texture);
+ m.texmatrix[0] = layer->texmatrix;
+ m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+ R_Mesh_TextureState(&m);
+ RSurf_DrawBatch_Lightmap(texturenumsurfaces, texturesurfacelist, layer->color[0], layer->color[1], layer->color[2], layer->color[3], 0, layer->color[0] != 1 || layer->color[1] != 1 || layer->color[2] != 1 || layer->color[3] != 1, applyfog);
+ break;
+ case TEXTURELAYERTYPE_FOG:
+ // singletexture fogging
+ R_Mesh_ColorPointer(rsurface_array_color4f);
+ if (layer->texture)
+ {
+ memset(&m, 0, sizeof(m));
+ m.tex[0] = R_GetTexture(layer->texture);
+ m.texmatrix[0] = layer->texmatrix;
+ m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+ R_Mesh_TextureState(&m);
+ }
+ else
+ R_Mesh_ResetTextureState();
+ // generate a color array for the fog pass
+ for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
+ {
+ int i;
+ float f, *v, *c;
+ const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
+ for (i = 0, v = (rsurface_vertex3f + 3 * surface->num_firstvertex), c = (rsurface_array_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
+ {
+ f = VERTEXFOGTABLE(VectorDistance(v, rsurface_modelorg));
+ c[0] = layer->color[0];
+ c[1] = layer->color[1];
+ c[2] = layer->color[2];
+ c[3] = f * layer->color[3];
+ }
+ }
+ RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
+ break;
+ default:
+ Con_Printf("R_DrawTextureSurfaceList: unknown layer type %i\n", layer->type);
+ }
+ GL_LockArrays(0, 0);
+ }
+ CHECKGLERROR
+ if (rsurface_texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
+ {
+ qglDepthFunc(GL_LEQUAL);CHECKGLERROR
+ GL_AlphaTest(false);
+ }
+}
+
+static void R_DrawTextureSurfaceList(int texturenumsurfaces, msurface_t **texturesurfacelist)
+{
+ if (rsurface_texture->currentmaterialflags & MATERIALFLAG_NODRAW)
+ return;
+ r_shadow_rtlight = NULL;
+ r_refdef.stats.entities_surfaces += texturenumsurfaces;
+ CHECKGLERROR
+ GL_DepthTest(!(rsurface_texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
+ GL_CullFace(((rsurface_texture->textureflags & Q3TEXTUREFLAG_TWOSIDED) || (rsurface_entity->flags & RENDER_NOCULLFACE)) ? GL_NONE : GL_FRONT); // quake is backwards, this culls back faces
+ if (r_showsurfaces.integer)
+ R_DrawTextureSurfaceList_ShowSurfaces(texturenumsurfaces, texturesurfacelist);
+ else if (rsurface_texture->currentmaterialflags & MATERIALFLAG_SKY)
+ R_DrawTextureSurfaceList_Sky(texturenumsurfaces, texturesurfacelist);
+ else if (rsurface_texture->currentnumlayers)
+ {
+ if (r_glsl.integer && gl_support_fragment_shader)
+ R_DrawTextureSurfaceList_GL20(texturenumsurfaces, texturesurfacelist);
+ else if (gl_combine.integer && r_textureunits.integer >= 2)
+ R_DrawTextureSurfaceList_GL13(texturenumsurfaces, texturesurfacelist);
+ else
+ R_DrawTextureSurfaceList_GL11(texturenumsurfaces, texturesurfacelist);
+ }
+ CHECKGLERROR
+ GL_LockArrays(0, 0);
+}
+
+#define BATCHSIZE 256
+static void R_DrawSurface_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
+{
+ int surfacelistindex;
+ int batchcount;
+ texture_t *t;
+ msurface_t *texturesurfacelist[BATCHSIZE];
+ // if the model is static it doesn't matter what value we give for
+ // wantnormals and wanttangents, so this logic uses only rules applicable
+ // to a model, knowing that they are meaningless otherwise
+ if ((ent->effects & EF_FULLBRIGHT) || r_showsurfaces.integer || VectorLength2(ent->modellight_diffuse) < (1.0f / 256.0f))
+ RSurf_ActiveEntity(ent, false, false);
+ else
+ RSurf_ActiveEntity(ent, true, r_glsl.integer && gl_support_fragment_shader);
+ batchcount = 0;
+ t = NULL;
+ for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
+ {
+ msurface_t *surface = ent->model->data_surfaces + surfacelist[surfacelistindex];
+
+ if (t != surface->texture || rsurface_lightmaptexture != surface->lightmaptexture)
+ {
+ if (batchcount > 0)
+ if (!(rsurface_texture->currentmaterialflags & MATERIALFLAG_SKY)) // transparent sky is too difficult
+ R_DrawTextureSurfaceList(batchcount, texturesurfacelist);
+ batchcount = 0;
+ t = surface->texture;
+ rsurface_lightmaptexture = surface->lightmaptexture;
+ R_UpdateTextureInfo(ent, t);
+ rsurface_texture = t->currentframe;
+ }
+
+ texturesurfacelist[batchcount++] = surface;
+ }
+ if (batchcount > 0)
+ if (!(rsurface_texture->currentmaterialflags & MATERIALFLAG_SKY)) // transparent sky is too difficult
+ R_DrawTextureSurfaceList(batchcount, texturesurfacelist);
+ RSurf_CleanUp();
+}
+
+void R_QueueTextureSurfaceList(int texturenumsurfaces, msurface_t **texturesurfacelist)
+{
+ int texturesurfaceindex;
+ vec3_t tempcenter, center;
+ if (rsurface_texture->currentmaterialflags & MATERIALFLAG_BLENDED)
+ {
+ // drawing sky transparently would be too difficult
+ if (!(rsurface_texture->currentmaterialflags & MATERIALFLAG_SKY))
+ {
+ for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
+ {
+ const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
+ tempcenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
+ tempcenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
+ tempcenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
+ Matrix4x4_Transform(&rsurface_entity->matrix, tempcenter, center);
+ R_MeshQueue_AddTransparent(rsurface_texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST ? r_view.origin : center, R_DrawSurface_TransparentCallback, rsurface_entity, surface - rsurface_model->data_surfaces, r_shadow_rtlight);
+ }
+ }
+ }
+ else
+ R_DrawTextureSurfaceList(texturenumsurfaces, texturesurfacelist);
+}
+
+extern void R_BuildLightMap(const entity_render_t *ent, msurface_t *surface);
+void R_DrawSurfaces(entity_render_t *ent, qboolean skysurfaces)
+{
+ int i, j, f, flagsmask;
+ int counttriangles = 0;
+ texture_t *t;
+ model_t *model = ent->model;
+ const int maxsurfacelist = 1024;
+ int numsurfacelist = 0;
+ msurface_t *surfacelist[1024];
+ if (model == NULL)
+ return;
+
+ // if the model is static it doesn't matter what value we give for
+ // wantnormals and wanttangents, so this logic uses only rules applicable
+ // to a model, knowing that they are meaningless otherwise
+ if ((ent->effects & EF_FULLBRIGHT) || r_showsurfaces.integer || VectorLength2(ent->modellight_diffuse) < (1.0f / 256.0f))
+ RSurf_ActiveEntity(ent, false, false);
+ else
+ RSurf_ActiveEntity(ent, true, r_glsl.integer && gl_support_fragment_shader);
+
+ // update light styles
+ if (!skysurfaces && model->brushq1.light_styleupdatechains)
+ {
+ msurface_t *surface, **surfacechain;
+ for (i = 0;i < model->brushq1.light_styles;i++)
+ {
+ if (model->brushq1.light_stylevalue[i] != r_refdef.lightstylevalue[model->brushq1.light_style[i]])
+ {
+ model->brushq1.light_stylevalue[i] = r_refdef.lightstylevalue[model->brushq1.light_style[i]];
+ if ((surfacechain = model->brushq1.light_styleupdatechains[i]))
+ for (;(surface = *surfacechain);surfacechain++)
+ surface->cached_dlight = true;
+ }
+ }
+ }
+
+ R_UpdateAllTextureInfo(ent);
+ flagsmask = skysurfaces ? MATERIALFLAG_SKY : (MATERIALFLAG_WATER | MATERIALFLAG_WALL);
+ f = 0;
+ t = NULL;
+ rsurface_lightmaptexture = NULL;
+ rsurface_texture = NULL;
+ numsurfacelist = 0;
+ if (ent == r_refdef.worldentity)
+ {
+ msurface_t *surface;
+ for (i = 0, j = model->firstmodelsurface, surface = model->data_surfaces + j;i < model->nummodelsurfaces;i++, j++, surface++)
+ {
+ if (!r_viewcache.world_surfacevisible[j])
+ continue;
+ if (t != surface->texture || rsurface_lightmaptexture != surface->lightmaptexture)
+ {
+ if (numsurfacelist)
+ {
+ R_QueueTextureSurfaceList(numsurfacelist, surfacelist);
+ numsurfacelist = 0;
+ }
+ t = surface->texture;
+ rsurface_lightmaptexture = surface->lightmaptexture;
+ rsurface_texture = t->currentframe;
+ f = rsurface_texture->currentmaterialflags & flagsmask;
+ }
+ if (f && surface->num_triangles)
+ {
+ // if lightmap parameters changed, rebuild lightmap texture
+ if (surface->cached_dlight)
+ R_BuildLightMap(ent, surface);
+ // add face to draw list
+ surfacelist[numsurfacelist++] = surface;
+ counttriangles += surface->num_triangles;
+ if (numsurfacelist >= maxsurfacelist)
+ {
+ R_QueueTextureSurfaceList(numsurfacelist, surfacelist);
+ numsurfacelist = 0;
+ }
+ }
+ }
+ }
+ else
+ {
+ msurface_t *surface;
+ for (i = 0, j = model->firstmodelsurface, surface = model->data_surfaces + j;i < model->nummodelsurfaces;i++, j++, surface++)
+ {
+ if (t != surface->texture || rsurface_lightmaptexture != surface->lightmaptexture)
+ {
+ if (numsurfacelist)
+ {
+ R_QueueTextureSurfaceList(numsurfacelist, surfacelist);
+ numsurfacelist = 0;
+ }
+ t = surface->texture;
+ rsurface_lightmaptexture = surface->lightmaptexture;
+ rsurface_texture = t->currentframe;
+ f = rsurface_texture->currentmaterialflags & flagsmask;
+ }
+ if (f && surface->num_triangles)
+ {
+ // if lightmap parameters changed, rebuild lightmap texture
+ if (surface->cached_dlight)
+ R_BuildLightMap(ent, surface);
+ // add face to draw list
+ surfacelist[numsurfacelist++] = surface;
+ counttriangles += surface->num_triangles;
+ if (numsurfacelist >= maxsurfacelist)
+ {
+ R_QueueTextureSurfaceList(numsurfacelist, surfacelist);
+ numsurfacelist = 0;
+ }
+ }
+ }
+ }
+ if (numsurfacelist)
+ R_QueueTextureSurfaceList(numsurfacelist, surfacelist);
+ r_refdef.stats.entities_triangles += counttriangles;
+ RSurf_CleanUp();
+
+ if (r_showcollisionbrushes.integer && model->brush.num_brushes && !skysurfaces)
+ {
+ int i;
+ const msurface_t *surface;
+ q3mbrush_t *brush;
+ CHECKGLERROR
+ R_Mesh_Matrix(&ent->matrix);
+ R_Mesh_ColorPointer(NULL);
+ R_Mesh_ResetTextureState();
+ GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
+ GL_DepthMask(false);
+ GL_DepthTest(!r_showdisabledepthtest.integer);
+ qglPolygonOffset(r_refdef.polygonfactor + r_showcollisionbrushes_polygonfactor.value, r_refdef.polygonoffset + r_showcollisionbrushes_polygonoffset.value);CHECKGLERROR
+ for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
+ if (brush->colbrushf && brush->colbrushf->numtriangles)
+ R_DrawCollisionBrush(brush->colbrushf);
+ for (i = 0, surface = model->data_surfaces + model->firstmodelsurface;i < model->nummodelsurfaces;i++, surface++)
+ if (surface->num_collisiontriangles)
+ R_DrawCollisionSurface(ent, surface);
+ qglPolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);CHECKGLERROR
+ }
+
+ if (r_showtris.integer || r_shownormals.integer)
+ {
+ int k, l;
+ msurface_t *surface;
+ const int *elements;
+ vec3_t v;
+ CHECKGLERROR
+ GL_DepthTest(!r_showdisabledepthtest.integer);
+ GL_DepthMask(true);
+ GL_BlendFunc(GL_ONE, GL_ZERO);
+ R_Mesh_ColorPointer(NULL);
+ R_Mesh_ResetTextureState();
+ for (i = 0, j = model->firstmodelsurface, surface = model->data_surfaces + j;i < model->nummodelsurfaces;i++, j++, surface++)
+ {
+ if (ent == r_refdef.worldentity && !r_viewcache.world_surfacevisible[j])
+ continue;
+ rsurface_texture = surface->texture->currentframe;
+ if ((rsurface_texture->currentmaterialflags & flagsmask) && surface->num_triangles)
+ {
+ RSurf_PrepareVerticesForBatch(true, true, 1, &surface);
+ if (r_showtris.integer)
+ {
+ if (!rsurface_texture->currentlayers->depthmask)
+ GL_Color(r_showtris.value * r_view.colorscale, 0, 0, 1);
+ else if (ent == r_refdef.worldentity)
+ GL_Color(r_showtris.value * r_view.colorscale, r_showtris.value * r_view.colorscale, r_showtris.value * r_view.colorscale, 1);
+ else
+ GL_Color(0, r_showtris.value * r_view.colorscale, 0, 1);
+ elements = (ent->model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);
+ CHECKGLERROR
+ qglBegin(GL_LINES);
+ for (k = 0;k < surface->num_triangles;k++, elements += 3)
+ {
+ qglArrayElement(elements[0]);qglArrayElement(elements[1]);
+ qglArrayElement(elements[1]);qglArrayElement(elements[2]);
+ qglArrayElement(elements[2]);qglArrayElement(elements[0]);
+ }
+ qglEnd();
+ CHECKGLERROR
+ }
+ if (r_shownormals.integer)
+ {
+ GL_Color(r_shownormals.value * r_view.colorscale, 0, 0, 1);
+ qglBegin(GL_LINES);
+ for (k = 0, l = surface->num_firstvertex;k < surface->num_vertices;k++, l++)
+ {
+ VectorCopy(rsurface_vertex3f + l * 3, v);
+ qglVertex3f(v[0], v[1], v[2]);
+ VectorMA(v, 8, rsurface_svector3f + l * 3, v);
+ qglVertex3f(v[0], v[1], v[2]);
+ }
+ qglEnd();
+ CHECKGLERROR
+ GL_Color(0, 0, r_shownormals.value * r_view.colorscale, 1);
+ qglBegin(GL_LINES);
+ for (k = 0, l = surface->num_firstvertex;k < surface->num_vertices;k++, l++)
+ {
+ VectorCopy(rsurface_vertex3f + l * 3, v);
+ qglVertex3f(v[0], v[1], v[2]);
+ VectorMA(v, 8, rsurface_tvector3f + l * 3, v);
+ qglVertex3f(v[0], v[1], v[2]);
+ }
+ qglEnd();
+ CHECKGLERROR
+ GL_Color(0, r_shownormals.value * r_view.colorscale, 0, 1);
+ qglBegin(GL_LINES);
+ for (k = 0, l = surface->num_firstvertex;k < surface->num_vertices;k++, l++)
+ {
+ VectorCopy(rsurface_vertex3f + l * 3, v);
+ qglVertex3f(v[0], v[1], v[2]);
+ VectorMA(v, 8, rsurface_normal3f + l * 3, v);
+ qglVertex3f(v[0], v[1], v[2]);
+ }
+ qglEnd();
+ CHECKGLERROR
+ }
+ }
+ }
+ rsurface_texture = NULL;
+ }
+}