]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_rmain.c
removed rsurface_entity field, and copied all fields that were accessed
[xonotic/darkplaces.git] / gl_rmain.c
index 72831d8775a9dfb1e43c10a4fdd11e9cbbd86767..6d3260206271d29128a5d634ea3b2e5c07fb6c1a 100644 (file)
@@ -104,6 +104,8 @@ cvar_t gl_lightmaps = {0, "gl_lightmaps", "0", "draws only lightmaps, no texture
 cvar_t r_test = {0, "r_test", "0", "internal development use only, leave it alone (usually does nothing anyway)"};
 cvar_t r_batchmode = {0, "r_batchmode", "1", "selects method of rendering multiple surfaces with one driver call (values are 0, 1, 2, etc...)"};
 
+extern qboolean v_flipped_state;
+
 typedef struct r_glsl_bloomshader_s
 {
        int program;
@@ -971,13 +973,13 @@ int R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, fl
        else if (permutation & SHADERPERMUTATION_MODE_LIGHTDIRECTION)
        {
                if (r_glsl_permutation->loc_AmbientColor >= 0)
-                       qglUniform3fARB(r_glsl_permutation->loc_AmbientColor, rsurface_entity->modellight_ambient[0] * ambientscale, rsurface_entity->modellight_ambient[1] * ambientscale, rsurface_entity->modellight_ambient[2] * ambientscale);
+                       qglUniform3fARB(r_glsl_permutation->loc_AmbientColor, rsurface_modellight_ambient[0] * ambientscale, rsurface_modellight_ambient[1] * ambientscale, rsurface_modellight_ambient[2] * ambientscale);
                if (r_glsl_permutation->loc_DiffuseColor >= 0)
-                       qglUniform3fARB(r_glsl_permutation->loc_DiffuseColor, rsurface_entity->modellight_diffuse[0] * diffusescale, rsurface_entity->modellight_diffuse[1] * diffusescale, rsurface_entity->modellight_diffuse[2] * diffusescale);
+                       qglUniform3fARB(r_glsl_permutation->loc_DiffuseColor, rsurface_modellight_diffuse[0] * diffusescale, rsurface_modellight_diffuse[1] * diffusescale, rsurface_modellight_diffuse[2] * diffusescale);
                if (r_glsl_permutation->loc_SpecularColor >= 0)
-                       qglUniform3fARB(r_glsl_permutation->loc_SpecularColor, rsurface_entity->modellight_diffuse[0] * specularscale, rsurface_entity->modellight_diffuse[1] * specularscale, rsurface_entity->modellight_diffuse[2] * specularscale);
+                       qglUniform3fARB(r_glsl_permutation->loc_SpecularColor, rsurface_modellight_diffuse[0] * specularscale, rsurface_modellight_diffuse[1] * specularscale, rsurface_modellight_diffuse[2] * specularscale);
                if (r_glsl_permutation->loc_LightDir >= 0)
-                       qglUniform3fARB(r_glsl_permutation->loc_LightDir, rsurface_entity->modellight_lightdir[0], rsurface_entity->modellight_lightdir[1], rsurface_entity->modellight_lightdir[2]);
+                       qglUniform3fARB(r_glsl_permutation->loc_LightDir, rsurface_modellight_lightdir[0], rsurface_modellight_lightdir[1], rsurface_modellight_lightdir[2]);
        }
        else
        {
@@ -1010,14 +1012,14 @@ int R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, fl
        if (r_glsl_permutation->loc_Color_Pants >= 0)
        {
                if (rsurface_texture->currentskinframe->pants)
-                       qglUniform3fARB(r_glsl_permutation->loc_Color_Pants, rsurface_entity->colormap_pantscolor[0], rsurface_entity->colormap_pantscolor[1], rsurface_entity->colormap_pantscolor[2]);
+                       qglUniform3fARB(r_glsl_permutation->loc_Color_Pants, rsurface_colormap_pantscolor[0], rsurface_colormap_pantscolor[1], rsurface_colormap_pantscolor[2]);
                else
                        qglUniform3fARB(r_glsl_permutation->loc_Color_Pants, 0, 0, 0);
        }
        if (r_glsl_permutation->loc_Color_Shirt >= 0)
        {
                if (rsurface_texture->currentskinframe->shirt)
-                       qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, rsurface_entity->colormap_shirtcolor[0], rsurface_entity->colormap_shirtcolor[1], rsurface_entity->colormap_shirtcolor[2]);
+                       qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, rsurface_colormap_shirtcolor[0], rsurface_colormap_shirtcolor[1], rsurface_colormap_shirtcolor[2]);
                else
                        qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, 0, 0, 0);
        }
@@ -2457,13 +2459,13 @@ void R_UpdateVariables(void)
                Cvar_SetValueQuick(&r_shadow_frontsidecasting, 1);
        r_refdef.polygonfactor = 0;
        r_refdef.polygonoffset = 0;
-       r_refdef.shadowpolygonfactor = r_refdef.polygonfactor + r_shadow_shadow_polygonfactor.value * (r_shadow_frontsidecasting.integer ? 1 : -1);
-       r_refdef.shadowpolygonoffset = r_refdef.polygonoffset + r_shadow_shadow_polygonoffset.value * (r_shadow_frontsidecasting.integer ? 1 : -1);
+       r_refdef.shadowpolygonfactor = r_refdef.polygonfactor + r_shadow_polygonfactor.value * (r_shadow_frontsidecasting.integer ? 1 : -1);
+       r_refdef.shadowpolygonoffset = r_refdef.polygonoffset + r_shadow_polygonoffset.value * (r_shadow_frontsidecasting.integer ? 1 : -1);
 
        r_refdef.rtworld = r_shadow_realtime_world.integer;
        r_refdef.rtworldshadows = r_shadow_realtime_world_shadows.integer && gl_stencil;
        r_refdef.rtdlight = (r_shadow_realtime_world.integer || r_shadow_realtime_dlight.integer) && !gl_flashblend.integer && r_dynamic.integer;
-       r_refdef.rtdlightshadows = r_refdef.rtdlight && (r_refdef.rtworld ? r_shadow_realtime_world_dlightshadows.integer : r_shadow_realtime_dlight_shadows.integer) && gl_stencil;
+       r_refdef.rtdlightshadows = r_refdef.rtdlight && r_shadow_realtime_dlight_shadows.integer && gl_stencil;
        r_refdef.lightmapintensity = r_refdef.rtworld ? r_shadow_realtime_world_lightmaps.value : 1;
        if (r_showsurfaces.integer)
        {
@@ -2952,6 +2954,16 @@ void R_DrawSprite(int blendfunc1, int blendfunc2, rtexture_t *texture, rtexture_
 
        R_Mesh_Matrix(&identitymatrix);
        GL_BlendFunc(blendfunc1, blendfunc2);
+
+       if(v_flipped_state)
+       {
+               scalex1 = -scalex1;
+               scalex2 = -scalex2;
+               GL_CullFace(GL_BACK);
+       }
+       else
+               GL_CullFace(GL_FRONT);
+
        GL_DepthMask(false);
        GL_DepthRange(0, depthshort ? 0.0625 : 1);
        GL_DepthTest(!depthdisable);
@@ -3381,9 +3393,16 @@ size_t rsurface_normal3f_bufferoffset;
 float *rsurface_lightmapcolor4f;
 int rsurface_lightmapcolor4f_bufferobject;
 size_t rsurface_lightmapcolor4f_bufferoffset;
+matrix4x4_t rsurface_matrix;
+matrix4x4_t rsurface_inversematrix;
+frameblend_t rsurface_frameblend[4];
+vec3_t rsurface_modellight_ambient;
+vec3_t rsurface_modellight_diffuse;
+vec3_t rsurface_modellight_lightdir;
+vec3_t rsurface_colormap_pantscolor;
+vec3_t rsurface_colormap_shirtcolor;
 vec3_t rsurface_modelorg;
 qboolean rsurface_generatedvertex;
-const entity_render_t *rsurface_entity;
 const model_t *rsurface_model;
 texture_t *rsurface_texture;
 qboolean rsurface_uselightmaptexture;
@@ -3406,12 +3425,26 @@ void RSurf_CleanUp(void)
 void RSurf_ActiveWorldEntity(void)
 {
        RSurf_CleanUp();
-       rsurface_entity = r_refdef.worldentity;
        rsurface_model = r_refdef.worldmodel;
        if (rsurface_array_size < rsurface_model->surfmesh.num_vertices)
                R_Mesh_ResizeArrays(rsurface_model->surfmesh.num_vertices);
+       rsurface_matrix = identitymatrix;
+       rsurface_inversematrix = identitymatrix;
        R_Mesh_Matrix(&identitymatrix);
        VectorCopy(r_view.origin, rsurface_modelorg);
+       VectorSet(rsurface_modellight_ambient, 0, 0, 0);
+       VectorSet(rsurface_modellight_diffuse, 0, 0, 0);
+       VectorSet(rsurface_modellight_lightdir, 0, 0, 1);
+       VectorSet(rsurface_colormap_pantscolor, 0, 0, 0);
+       VectorSet(rsurface_colormap_shirtcolor, 0, 0, 0);
+       rsurface_frameblend[0].frame = 0;
+       rsurface_frameblend[0].lerp = 1;
+       rsurface_frameblend[1].frame = 0;
+       rsurface_frameblend[1].lerp = 0;
+       rsurface_frameblend[2].frame = 0;
+       rsurface_frameblend[2].lerp = 0;
+       rsurface_frameblend[3].frame = 0;
+       rsurface_frameblend[3].lerp = 0;
        rsurface_modelvertex3f  = rsurface_model->surfmesh.data_vertex3f;
        rsurface_modelvertex3f_bufferobject = rsurface_model->surfmesh.vbo;
        rsurface_modelvertex3f_bufferoffset = rsurface_model->surfmesh.vbooffset_vertex3f;
@@ -3442,13 +3475,23 @@ void RSurf_ActiveWorldEntity(void)
 void RSurf_ActiveModelEntity(const entity_render_t *ent, qboolean wantnormals, qboolean wanttangents)
 {
        RSurf_CleanUp();
-       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_model->surfmesh.isanimated && (rsurface_entity->frameblend[0].lerp != 1 || rsurface_entity->frameblend[0].frame != 0))
+       rsurface_matrix = ent->matrix;
+       rsurface_inversematrix = ent->inversematrix;
+       R_Mesh_Matrix(&rsurface_matrix);
+       Matrix4x4_Transform(&rsurface_inversematrix, r_view.origin, rsurface_modelorg);
+       VectorCopy(ent->modellight_ambient, rsurface_modellight_ambient);
+       VectorCopy(ent->modellight_diffuse, rsurface_modellight_diffuse);
+       VectorCopy(ent->modellight_lightdir, rsurface_modellight_lightdir);
+       VectorCopy(ent->colormap_pantscolor, rsurface_colormap_pantscolor);
+       VectorCopy(ent->colormap_shirtcolor, rsurface_colormap_shirtcolor);
+       rsurface_frameblend[0] = ent->frameblend[0];
+       rsurface_frameblend[1] = ent->frameblend[1];
+       rsurface_frameblend[2] = ent->frameblend[2];
+       rsurface_frameblend[3] = ent->frameblend[3];
+       if (rsurface_model->surfmesh.isanimated && (rsurface_frameblend[0].lerp != 1 || rsurface_frameblend[0].frame != 0))
        {
                if (wanttangents)
                {
@@ -3456,7 +3499,7 @@ void RSurf_ActiveModelEntity(const entity_render_t *ent, qboolean wantnormals, q
                        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);
+                       Mod_Alias_GetMesh_Vertices(rsurface_model, rsurface_frameblend, rsurface_array_modelvertex3f, rsurface_array_modelnormal3f, rsurface_array_modelsvector3f, rsurface_array_modeltvector3f);
                }
                else if (wantnormals)
                {
@@ -3464,7 +3507,7 @@ void RSurf_ActiveModelEntity(const entity_render_t *ent, qboolean wantnormals, q
                        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);
+                       Mod_Alias_GetMesh_Vertices(rsurface_model, rsurface_frameblend, rsurface_array_modelvertex3f, rsurface_array_modelnormal3f, NULL, NULL);
                }
                else
                {
@@ -3472,7 +3515,7 @@ void RSurf_ActiveModelEntity(const entity_render_t *ent, qboolean wantnormals, q
                        rsurface_modelsvector3f = NULL;
                        rsurface_modeltvector3f = NULL;
                        rsurface_modelnormal3f = NULL;
-                       Mod_Alias_GetMesh_Vertices(rsurface_model, rsurface_entity->frameblend, rsurface_array_modelvertex3f, NULL, NULL, NULL);
+                       Mod_Alias_GetMesh_Vertices(rsurface_model, rsurface_frameblend, rsurface_array_modelvertex3f, NULL, NULL, NULL);
                }
                rsurface_modelvertex3f_bufferobject = 0;
                rsurface_modelvertex3f_bufferoffset = 0;
@@ -3514,6 +3557,7 @@ void RSurf_ActiveModelEntity(const entity_render_t *ent, qboolean wantnormals, q
        rsurface_normal3f_bufferoffset = rsurface_modelnormal3f_bufferoffset;
 }
 
+static const int quadedges[6][2] = {{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3}};
 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
@@ -3545,11 +3589,13 @@ void RSurf_PrepareVerticesForBatch(qboolean generatenormals, qboolean generateta
        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);
+               float center[3], forward[3], right[3], up[3], v[3], newforward[3], newright[3], newup[3];
+               Matrix4x4_Transform3x3(&rsurface_inversematrix, r_view.forward, newforward);
+               Matrix4x4_Transform3x3(&rsurface_inversematrix, r_view.right, newright);
+               Matrix4x4_Transform3x3(&rsurface_inversematrix, r_view.up, newup);
+               VectorNormalize(newforward);
+               VectorNormalize(newright);
+               VectorNormalize(newup);
                // make deformed versions of only the model vertices used by the specified surfaces
                for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
                {
@@ -3564,22 +3610,80 @@ void RSurf_PrepareVerticesForBatch(qboolean generatenormals, qboolean generateta
                                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);
+                                       const float *v1, *v2;
+                                       float f, l;
+                                       struct
+                                       {
+                                               float length2;
+                                               int quadedge;
+                                       }
+                                       shortest[2];
+                                       shortest[0].quadedge = shortest[1].quadedge = 0;
+                                       shortest[0].length2 = shortest[1].length2 = 0;
+                                       // find the two shortest edges, then use them to define the
+                                       // axis vectors for rotating around the central axis
+                                       for (i = 0;i < 6;i++)
+                                       {
+                                               v1 = rsurface_modelvertex3f + 3 * (surface->num_firstvertex + quadedges[i][0]);
+                                               v2 = rsurface_modelvertex3f + 3 * (surface->num_firstvertex + quadedges[i][1]);
+                                               l = VectorDistance2(v1, v2);
+                                               if (shortest[0].length2 > l || i == 0)
+                                               {
+                                                       shortest[1] = shortest[0];
+                                                       shortest[0].length2 = l;
+                                                       shortest[0].quadedge = i;
+                                               }
+                                               else if (shortest[1].length2 > l || i == 1)
+                                               {
+                                                       shortest[1].length2 = l;
+                                                       shortest[1].quadedge = i;
+                                               }
+                                       }
+                                       // this calculates the midpoints *2 (not bothering to average) of the two shortest edges, and subtracts one from the other to get the up vector
+                                       for (i = 0;i < 3;i++)
+                                       {
+                                               right[i] = rsurface_modelvertex3f[3 * (surface->num_firstvertex + quadedges[shortest[1].quadedge][1]) + i]
+                                                        + rsurface_modelvertex3f[3 * (surface->num_firstvertex + quadedges[shortest[1].quadedge][0]) + i];
+                                               up[i] = rsurface_modelvertex3f[3 * (surface->num_firstvertex + quadedges[shortest[1].quadedge][0]) + i]
+                                                     + rsurface_modelvertex3f[3 * (surface->num_firstvertex + quadedges[shortest[1].quadedge][1]) + i]
+                                                     - rsurface_modelvertex3f[3 * (surface->num_firstvertex + quadedges[shortest[0].quadedge][0]) + i]
+                                                     - rsurface_modelvertex3f[3 * (surface->num_firstvertex + quadedges[shortest[0].quadedge][1]) + i];
+                                       }
+                                       // calculate a forward vector to use instead of the original plane normal (this is how we get a new right vector)
+                                       VectorSubtract(rsurface_modelorg, center, forward);
+                                       CrossProduct(up, forward, newright);
+                                       // normalize the vectors involved
+                                       VectorNormalize(right);
+                                       VectorNormalize(newright);
+                                       // rotate the quad around the up axis vector, this is made
+                                       // especially easy by the fact we know the quad is flat,
+                                       // so we only have to subtract the center position and
+                                       // measure distance along the right vector, and then
+                                       // multiply that by the newright vector and add back the
+                                       // center position
+                                       // we also need to subtract the old position to undo the
+                                       // displacement from the center, which we do with a
+                                       // DotProduct, the subtraction/addition of center is also
+                                       // optimized into DotProducts here
+                                       l = DotProduct(newright, center) - DotProduct(right, center);
+                                       for (i = 0;i < 4;i++)
+                                       {
+                                               v1 = rsurface_modelvertex3f + 3 * (surface->num_firstvertex + j + i);
+                                               f = DotProduct(right, v1) - DotProduct(newright, v1) + l;
+                                               VectorMA(v1, f, newright, rsurface_array_deformedvertex3f + (surface->num_firstvertex+i+j) * 3);
+                                       }
+                               }
+                               else
+                               {
+                                       VectorCopy((rsurface_modelnormal3f  + 3 * surface->num_firstvertex) + j*3, forward);
+                                       VectorCopy((rsurface_modelsvector3f + 3 * surface->num_firstvertex) + j*3, right);
+                                       VectorCopy((rsurface_modeltvector3f + 3 * surface->num_firstvertex) + j*3, up);
+                                       for (i = 0;i < 4;i++)
+                                       {
+                                               VectorSubtract((rsurface_modelvertex3f + 3 * surface->num_firstvertex) + (j+i)*3, center, v);
+                                               VectorMAMAMAM(1, center, DotProduct(forward, v), newforward, DotProduct(right, v), newright, DotProduct(up, v), newup, rsurface_array_deformedvertex3f + (surface->num_firstvertex+i+j) * 3);
+                                       }
                                }
-                               // 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);
@@ -3989,13 +4093,13 @@ static void RSurf_DrawBatch_GL11_VertexShade(int texturenumsurfaces, msurface_t
        vec3_t lightdir;
        // TODO: optimize
        // model lighting
-       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;
+       VectorCopy(rsurface_modellight_lightdir, lightdir);
+       ambientcolor[0] = rsurface_modellight_ambient[0] * r * 0.5f;
+       ambientcolor[1] = rsurface_modellight_ambient[1] * g * 0.5f;
+       ambientcolor[2] = rsurface_modellight_ambient[2] * b * 0.5f;
+       diffusecolor[0] = rsurface_modellight_diffuse[0] * r * 0.5f;
+       diffusecolor[1] = rsurface_modellight_diffuse[1] * g * 0.5f;
+       diffusecolor[2] = rsurface_modellight_diffuse[2] * b * 0.5f;
        if (VectorLength2(diffusecolor) > 0)
        {
                // generate color arrays for the surfaces in this list
@@ -4076,7 +4180,7 @@ static void R_DrawTextureSurfaceList_Sky(int texturenumsurfaces, msurface_t **te
                skyrendernow = false;
                R_Sky();
                // restore entity matrix
-               R_Mesh_Matrix(&rsurface_entity->matrix);
+               R_Mesh_Matrix(&rsurface_matrix);
        }
        GL_DepthRange(0, (rsurface_texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
        GL_DepthTest(!(rsurface_texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
@@ -4135,6 +4239,7 @@ static void R_DrawTextureSurfaceList_GL20(int texturenumsurfaces, msurface_t **t
        R_Mesh_TexCoordPointer(3, 3, rsurface_normal3f, rsurface_normal3f_bufferobject, rsurface_normal3f_bufferoffset);
        R_Mesh_TexCoordPointer(4, 2, rsurface_model->surfmesh.data_texcoordlightmap2f, rsurface_model->surfmesh.vbo, rsurface_model->surfmesh.vbooffset_texcoordlightmap2f);
 
+       GL_Color(rsurface_texture->currentlayers[0].color[0], rsurface_texture->currentlayers[0].color[1], rsurface_texture->currentlayers[0].color[2], rsurface_texture->currentlayers[0].color[3]);
        if (rsurface_texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
        {
                R_Mesh_TexBind(7, R_GetTexture(r_texture_white));
@@ -4516,7 +4621,6 @@ static void R_DrawTextureSurfaceList(int texturenumsurfaces, msurface_t **textur
                GL_CullFace((rsurface_texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : GL_FRONT); // quake is backwards, this culls back faces
                GL_BlendFunc(rsurface_texture->currentlayers[0].blendfunc1, rsurface_texture->currentlayers[0].blendfunc2);
                GL_DepthMask(writedepth && !(rsurface_texture->currentmaterialflags & MATERIALFLAG_BLENDED));
-               GL_Color(rsurface_entity->colormod[0], rsurface_entity->colormod[1], rsurface_entity->colormod[2], rsurface_texture->currentalpha);
                GL_AlphaTest((rsurface_texture->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
                // FIXME: identify models using a better check than rsurface_model->brush.shadowmesh
                rsurface_lightmode = ((rsurface_texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT) || rsurface_model->brush.shadowmesh) ? 0 : 2;
@@ -4576,7 +4680,7 @@ static void R_DrawSurface_TransparentCallback(const entity_render_t *ent, const
        RSurf_CleanUp();
 }
 
-void R_QueueSurfaceList(int numsurfaces, msurface_t **surfacelist, int flagsmask, qboolean writedepth, qboolean depthonly)
+void R_QueueSurfaceList(entity_render_t *ent, int numsurfaces, msurface_t **surfacelist, int flagsmask, qboolean writedepth, qboolean depthonly)
 {
        int i, j;
        vec3_t tempcenter, center;
@@ -4608,8 +4712,8 @@ void R_QueueSurfaceList(int numsurfaces, msurface_t **surfacelist, int flagsmask
                        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);
+                       Matrix4x4_Transform(&rsurface_matrix, tempcenter, center);
+                       R_MeshQueue_AddTransparent(rsurface_texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST ? r_view.origin : center, R_DrawSurface_TransparentCallback, ent, surface - rsurface_model->data_surfaces, r_shadow_rtlight);
                }
                else
                {
@@ -4867,14 +4971,14 @@ void R_DrawWorldSurfaces(qboolean skysurfaces, qboolean writedepth, qboolean dep
                                counttriangles += surface->num_triangles;
                                if (numsurfacelist >= maxsurfacelist)
                                {
-                                       R_QueueSurfaceList(numsurfacelist, surfacelist, flagsmask, writedepth, depthonly);
+                                       R_QueueSurfaceList(r_refdef.worldentity, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly);
                                        numsurfacelist = 0;
                                }
                        }
                }
        }
        if (numsurfacelist)
-               R_QueueSurfaceList(numsurfacelist, surfacelist, flagsmask, writedepth, depthonly);
+               R_QueueSurfaceList(r_refdef.worldentity, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly);
        r_refdef.stats.entities_triangles += counttriangles;
        RSurf_CleanUp();
 
@@ -4945,13 +5049,13 @@ void R_DrawModelSurfaces(entity_render_t *ent, qboolean skysurfaces, qboolean wr
                        counttriangles += surface->num_triangles;
                        if (numsurfacelist >= maxsurfacelist)
                        {
-                               R_QueueSurfaceList(numsurfacelist, surfacelist, flagsmask, writedepth, depthonly);
+                               R_QueueSurfaceList(ent, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly);
                                numsurfacelist = 0;
                        }
                }
        }
        if (numsurfacelist)
-               R_QueueSurfaceList(numsurfacelist, surfacelist, flagsmask, writedepth, depthonly);
+               R_QueueSurfaceList(ent, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly);
        r_refdef.stats.entities_triangles += counttriangles;
        RSurf_CleanUp();