]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_rmain.c
renamed varray_ arrays to rsurface_array_, and they are no longer used outside the...
[xonotic/darkplaces.git] / gl_rmain.c
index 36cc3ae2658d025233996f75e8e9fdcb6feabb69..721b27ab18dd36caead8f2d81eebe11af9da6f70 100644 (file)
@@ -99,9 +99,9 @@ cvar_t gl_fogend = {0, "gl_fogend","0", "nehahra fog end distance (for Nehahra c
 cvar_t r_textureunits = {0, "r_textureunits", "32", "number of hardware texture units reported by driver (note: setting this to 1 turns off gl_combine)"};
 
 cvar_t r_glsl = {0, "r_glsl", "1", "enables use of OpenGL 2.0 pixel shaders for lighting"};
-cvar_t r_glsl_offsetmapping = {0, "r_glsl_offsetmapping", "0", "enables offset mapping effect (also known as parallax mapping or sometimes as virtual displacement mapping, not as good as relief mapping or silohuette mapping but much faster), can cause strange artifacts on many textures, requires bumpmaps for depth information (normalmaps can have depth information as alpha channel, but most do not)"};
-cvar_t r_glsl_offsetmapping_scale = {0, "r_glsl_offsetmapping_scale", "-0.04", "how deep the offset mapping effect is, and whether it is inward or outward"};
-cvar_t r_glsl_offsetmapping_bias = {0, "r_glsl_offsetmapping_bias", "0.04", "pushes the effect closer/further"};
+cvar_t r_glsl_offsetmapping = {0, "r_glsl_offsetmapping", "0", "offset mapping effect (also known as parallax mapping or virtual displacement mapping)"};
+cvar_t r_glsl_offsetmapping_reliefmapping = {0, "r_glsl_offsetmapping_reliefmapping", "0", "relief mapping effect (higher quality)"};
+cvar_t r_glsl_offsetmapping_scale = {0, "r_glsl_offsetmapping_scale", "0.04", "how deep the offset mapping effect is"};
 cvar_t r_glsl_usehalffloat = {0, "r_glsl_usehalffloat", "0", "use half and hvec variables in GLSL shader for a speed gain (NVIDIA only)"};
 cvar_t r_glsl_surfacenormalize = {0, "r_glsl_surfacenormalize", "1", "normalize bumpmap texels in GLSL shader, produces a more rounded look on small bumps and dents"};
 cvar_t r_glsl_deluxemapping = {0, "r_glsl_deluxemapping", "1", "use per pixel lighting on deluxemap-compiled q3bsp maps (or a value of 2 forces deluxemap shading even without deluxemaps)"};
@@ -608,15 +608,39 @@ static const char *builtinshaderstring =
 "{\n"
 "      // apply offsetmapping\n"
 "#ifdef USEOFFSETMAPPING\n"
+"      myhvec2 TexCoordOffset = TexCoord;\n"
+"#define TexCoord TexCoordOffset\n"
+"\n"
 "      myhvec3 eyedir = myhvec3(normalize(EyeVector));\n"
 "      myhalf depthbias = 1.0 - eyedir.z; // should this be a -?\n"
 "      depthbias = 1.0 - depthbias * depthbias;\n"
-"      // this is 3 sample because of ATI Radeon 9500-9800/X300 limits\n"
-"      myhvec2 OffsetVector = (EyeVector.xy * (1.0 / EyeVector.z) * depthbias) * vec2(-0.333, 0.333);\n"
-"      myhvec2 TexCoordOffset = TexCoord + OffsetVector * (OffsetMapping_Bias + OffsetMapping_Scale * texture2D(Texture_Normal, TexCoord).w);\n"
-"      TexCoordOffset += OffsetVector * (OffsetMapping_Bias + OffsetMapping_Scale * texture2D(Texture_Normal, TexCoordOffset).w);\n"
-"      TexCoordOffset += OffsetVector * (OffsetMapping_Bias + OffsetMapping_Scale * texture2D(Texture_Normal, TexCoordOffset).w);\n"
-"#define TexCoord TexCoordOffset\n"
+"\n"
+"#ifdef USEOFFSETMAPPING_RELIEFMAPPING\n"
+"      // 14 sample relief mapping: linear search and then binary search\n"
+"      myhvec3 OffsetVector = myhvec3(EyeVector.xy * (1.0 / EyeVector.z) * depthbias * OffsetMapping_Scale * myhvec2(-0.1, 0.1), -0.1);\n"
+"      vec3 RT = vec3(TexCoord - OffsetVector.xy * 10.0, 1.0) + OffsetVector;\n"
+"      if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;\n"
+"      if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;\n"
+"      if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;\n"
+"      if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;\n"
+"      if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;\n"
+"      if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;\n"
+"      if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;\n"
+"      if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;\n"
+"      if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;OffsetVector *= 0.5;RT -= OffsetVector;\n"
+"      if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;OffsetVector *= 0.5;RT -= OffsetVector;\n"
+"      if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;OffsetVector *= 0.5;RT -= OffsetVector;\n"
+"      if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;OffsetVector *= 0.5;RT -= OffsetVector;\n"
+"      if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;OffsetVector *= 0.5;RT -= OffsetVector;\n"
+"      if (RT.z > texture2D(Texture_Normal, RT.xy).a) RT += OffsetVector;OffsetVector *= 0.5;RT -= OffsetVector;\n"
+"      TexCoord = RT.xy;\n"
+"#else\n"
+"      // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits)\n"
+"      myhvec2 OffsetVector = myhvec2((EyeVector.xy * (1.0 / EyeVector.z) * depthbias) * OffsetMapping_Scale * myhvec2(-0.333, 0.333));\n"
+"      TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
+"      TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
+"      TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
+"#endif\n"
 "#endif\n"
 "\n"
 "      // combine the diffuse textures (base, pants, shirt)\n"
@@ -838,6 +862,12 @@ void R_GLSL_CompilePermutation(int permutation)
                fragstrings_list[fragstrings_count++] = "#define USEOFFSETMAPPING\n";
                strlcat(permutationname, " offsetmapping", sizeof(permutationname));
        }
+       if (permutation & SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING)
+       {
+               vertstrings_list[vertstrings_count++] = "#define USEOFFSETMAPPING_RELIEFMAPPING\n";
+               fragstrings_list[fragstrings_count++] = "#define USEOFFSETMAPPING_RELIEFMAPPING\n";
+               strlcat(permutationname, " OFFSETMAPPING_RELIEFMAPPING", sizeof(permutationname));
+       }
        if (permutation & SHADERPERMUTATION_SURFACENORMALIZE)
        {
                vertstrings_list[vertstrings_count++] = "#define SURFACENORMALIZE\n";
@@ -889,7 +919,6 @@ void R_GLSL_CompilePermutation(int permutation)
                p->loc_SpecularPower       = qglGetUniformLocationARB(p->program, "SpecularPower");
                p->loc_SpecularScale       = qglGetUniformLocationARB(p->program, "SpecularScale");
                p->loc_OffsetMapping_Scale = qglGetUniformLocationARB(p->program, "OffsetMapping_Scale");
-               p->loc_OffsetMapping_Bias  = qglGetUniformLocationARB(p->program, "OffsetMapping_Bias");
                p->loc_AmbientColor        = qglGetUniformLocationARB(p->program, "AmbientColor");
                p->loc_DiffuseColor        = qglGetUniformLocationARB(p->program, "DiffuseColor");
                p->loc_SpecularColor       = qglGetUniformLocationARB(p->program, "SpecularColor");
@@ -961,7 +990,11 @@ void R_SetupSurfaceShader(const entity_render_t *ent, const texture_t *texture,
        if (texture->colormapping)
                permutation |= SHADERPERMUTATION_COLORMAPPING;
        if (r_glsl_offsetmapping.integer)
+       {
                permutation |= SHADERPERMUTATION_OFFSETMAPPING;
+               if (r_glsl_offsetmapping_reliefmapping.integer)
+                       permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;
+       }
        if (r_glsl_surfacenormalize.integer)
                permutation |= SHADERPERMUTATION_SURFACENORMALIZE;
        if (r_glsl_usehalffloat.integer)
@@ -1067,7 +1100,6 @@ void R_SetupSurfaceShader(const entity_render_t *ent, const texture_t *texture,
        if (r_glsl_permutation->loc_FogRangeRecip >= 0) qglUniform1fARB(r_glsl_permutation->loc_FogRangeRecip, fograngerecip);
        if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularPower, texture->specularpower);
        if (r_glsl_permutation->loc_OffsetMapping_Scale >= 0) qglUniform1fARB(r_glsl_permutation->loc_OffsetMapping_Scale, r_glsl_offsetmapping_scale.value);
-       if (r_glsl_permutation->loc_OffsetMapping_Bias >= 0) qglUniform1fARB(r_glsl_permutation->loc_OffsetMapping_Bias, r_glsl_offsetmapping_bias.value);
        CHECKGLERROR
 }
 
@@ -1155,8 +1187,8 @@ void GL_Main_Init(void)
        Cvar_RegisterVariable(&r_textureunits);
        Cvar_RegisterVariable(&r_glsl);
        Cvar_RegisterVariable(&r_glsl_offsetmapping);
+       Cvar_RegisterVariable(&r_glsl_offsetmapping_reliefmapping);
        Cvar_RegisterVariable(&r_glsl_offsetmapping_scale);
-       Cvar_RegisterVariable(&r_glsl_offsetmapping_bias);
        Cvar_RegisterVariable(&r_glsl_usehalffloat);
        Cvar_RegisterVariable(&r_glsl_surfacenormalize);
        Cvar_RegisterVariable(&r_glsl_deluxemapping);
@@ -1555,6 +1587,8 @@ static void R_BlendView(void)
        qboolean dobloom;
        qboolean doblend;
        rmeshstate_t m;
+       float vertex3f[12];
+       float texcoord2f[3][8];
 
        // set the (poorly named) screenwidth and screenheight variables to
        // a power of 2 at least as large as the screen, these will define the
@@ -1573,10 +1607,10 @@ static void R_BlendView(void)
        GL_DepthTest(false);
        R_Mesh_Matrix(&identitymatrix);
        // vertex coordinates for a quad that covers the screen exactly
-       varray_vertex3f[0] = 0;varray_vertex3f[1] = 0;varray_vertex3f[2] = 0;
-       varray_vertex3f[3] = 1;varray_vertex3f[4] = 0;varray_vertex3f[5] = 0;
-       varray_vertex3f[6] = 1;varray_vertex3f[7] = 1;varray_vertex3f[8] = 0;
-       varray_vertex3f[9] = 0;varray_vertex3f[10] = 1;varray_vertex3f[11] = 0;
+       vertex3f[0] = 0;vertex3f[1] = 0;vertex3f[2] = 0;
+       vertex3f[3] = 1;vertex3f[4] = 0;vertex3f[5] = 0;
+       vertex3f[6] = 1;vertex3f[7] = 1;vertex3f[8] = 0;
+       vertex3f[9] = 0;vertex3f[10] = 1;vertex3f[11] = 0;
        if (dobloom)
        {
                int bloomwidth, bloomheight, x, dobloomblend, range;
@@ -1593,27 +1627,27 @@ static void R_BlendView(void)
                bloomheight = min(r_view_height, bloomwidth * r_view_height / r_view_width);
                // set up a texcoord array for the full resolution screen image
                // (we have to keep this around to copy back during final render)
-               varray_texcoord2f[0][0] = 0;
-               varray_texcoord2f[0][1] = (float)r_view_height / (float)screenheight;
-               varray_texcoord2f[0][2] = (float)r_view_width / (float)screenwidth;
-               varray_texcoord2f[0][3] = (float)r_view_height / (float)screenheight;
-               varray_texcoord2f[0][4] = (float)r_view_width / (float)screenwidth;
-               varray_texcoord2f[0][5] = 0;
-               varray_texcoord2f[0][6] = 0;
-               varray_texcoord2f[0][7] = 0;
+               texcoord2f[0][0] = 0;
+               texcoord2f[0][1] = (float)r_view_height / (float)screenheight;
+               texcoord2f[0][2] = (float)r_view_width / (float)screenwidth;
+               texcoord2f[0][3] = (float)r_view_height / (float)screenheight;
+               texcoord2f[0][4] = (float)r_view_width / (float)screenwidth;
+               texcoord2f[0][5] = 0;
+               texcoord2f[0][6] = 0;
+               texcoord2f[0][7] = 0;
                // set up a texcoord array for the reduced resolution bloom image
                // (which will be additive blended over the screen image)
-               varray_texcoord2f[1][0] = 0;
-               varray_texcoord2f[1][1] = (float)bloomheight / (float)screenheight;
-               varray_texcoord2f[1][2] = (float)bloomwidth / (float)screenwidth;
-               varray_texcoord2f[1][3] = (float)bloomheight / (float)screenheight;
-               varray_texcoord2f[1][4] = (float)bloomwidth / (float)screenwidth;
-               varray_texcoord2f[1][5] = 0;
-               varray_texcoord2f[1][6] = 0;
-               varray_texcoord2f[1][7] = 0;
+               texcoord2f[1][0] = 0;
+               texcoord2f[1][1] = (float)bloomheight / (float)screenheight;
+               texcoord2f[1][2] = (float)bloomwidth / (float)screenwidth;
+               texcoord2f[1][3] = (float)bloomheight / (float)screenheight;
+               texcoord2f[1][4] = (float)bloomwidth / (float)screenwidth;
+               texcoord2f[1][5] = 0;
+               texcoord2f[1][6] = 0;
+               texcoord2f[1][7] = 0;
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = varray_vertex3f;
-               m.pointer_texcoord[0] = varray_texcoord2f[0];
+               m.pointer_vertex = vertex3f;
+               m.pointer_texcoord[0] = texcoord2f[0];
                m.tex[0] = R_GetTexture(r_bloom_texture_screen);
                R_Mesh_State(&m);
                // copy view into the full resolution screen image texture
@@ -1639,9 +1673,9 @@ static void R_BlendView(void)
                // we now have a darkened bloom image in the framebuffer, copy it into
                // the bloom image texture for more processing
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = varray_vertex3f;
+               m.pointer_vertex = vertex3f;
                m.tex[0] = R_GetTexture(r_bloom_texture_bloom);
-               m.pointer_texcoord[0] = varray_texcoord2f[2];
+               m.pointer_texcoord[0] = texcoord2f[2];
                R_Mesh_State(&m);
                GL_ActiveTexture(0);
                qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view_x, vid.height - (r_view_y + bloomheight), bloomwidth, bloomheight);
@@ -1655,14 +1689,14 @@ static void R_BlendView(void)
                        xoffset = 0 / (float)bloomwidth * (float)bloomwidth / (float)screenwidth;
                        yoffset = x / (float)bloomheight * (float)bloomheight / (float)screenheight;
                        // compute a texcoord array with the specified x and y offset
-                       varray_texcoord2f[2][0] = xoffset+0;
-                       varray_texcoord2f[2][1] = yoffset+(float)bloomheight / (float)screenheight;
-                       varray_texcoord2f[2][2] = xoffset+(float)bloomwidth / (float)screenwidth;
-                       varray_texcoord2f[2][3] = yoffset+(float)bloomheight / (float)screenheight;
-                       varray_texcoord2f[2][4] = xoffset+(float)bloomwidth / (float)screenwidth;
-                       varray_texcoord2f[2][5] = yoffset+0;
-                       varray_texcoord2f[2][6] = xoffset+0;
-                       varray_texcoord2f[2][7] = yoffset+0;
+                       texcoord2f[2][0] = xoffset+0;
+                       texcoord2f[2][1] = yoffset+(float)bloomheight / (float)screenheight;
+                       texcoord2f[2][2] = xoffset+(float)bloomwidth / (float)screenwidth;
+                       texcoord2f[2][3] = yoffset+(float)bloomheight / (float)screenheight;
+                       texcoord2f[2][4] = xoffset+(float)bloomwidth / (float)screenwidth;
+                       texcoord2f[2][5] = yoffset+0;
+                       texcoord2f[2][6] = xoffset+0;
+                       texcoord2f[2][7] = yoffset+0;
                        // this r value looks like a 'dot' particle, fading sharply to
                        // black at the edges
                        // (probably not realistic but looks good enough)
@@ -1688,14 +1722,14 @@ static void R_BlendView(void)
                        xoffset = x / (float)bloomwidth * (float)bloomwidth / (float)screenwidth;
                        yoffset = 0 / (float)bloomheight * (float)bloomheight / (float)screenheight;
                        // compute a texcoord array with the specified x and y offset
-                       varray_texcoord2f[2][0] = xoffset+0;
-                       varray_texcoord2f[2][1] = yoffset+(float)bloomheight / (float)screenheight;
-                       varray_texcoord2f[2][2] = xoffset+(float)bloomwidth / (float)screenwidth;
-                       varray_texcoord2f[2][3] = yoffset+(float)bloomheight / (float)screenheight;
-                       varray_texcoord2f[2][4] = xoffset+(float)bloomwidth / (float)screenwidth;
-                       varray_texcoord2f[2][5] = yoffset+0;
-                       varray_texcoord2f[2][6] = xoffset+0;
-                       varray_texcoord2f[2][7] = yoffset+0;
+                       texcoord2f[2][0] = xoffset+0;
+                       texcoord2f[2][1] = yoffset+(float)bloomheight / (float)screenheight;
+                       texcoord2f[2][2] = xoffset+(float)bloomwidth / (float)screenwidth;
+                       texcoord2f[2][3] = yoffset+(float)bloomheight / (float)screenheight;
+                       texcoord2f[2][4] = xoffset+(float)bloomwidth / (float)screenwidth;
+                       texcoord2f[2][5] = yoffset+0;
+                       texcoord2f[2][6] = xoffset+0;
+                       texcoord2f[2][7] = yoffset+0;
                        // this r value looks like a 'dot' particle, fading sharply to
                        // black at the edges
                        // (probably not realistic but looks good enough)
@@ -1716,9 +1750,9 @@ static void R_BlendView(void)
                // put the original screen image back in place and blend the bloom
                // texture on it
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = varray_vertex3f;
+               m.pointer_vertex = vertex3f;
                m.tex[0] = R_GetTexture(r_bloom_texture_screen);
-               m.pointer_texcoord[0] = varray_texcoord2f[0];
+               m.pointer_texcoord[0] = texcoord2f[0];
 #if 0
                dobloomblend = false;
 #else
@@ -1728,7 +1762,7 @@ static void R_BlendView(void)
                        dobloomblend = false;
                        m.texcombinergb[1] = GL_ADD;
                        m.tex[1] = R_GetTexture(r_bloom_texture_bloom);
-                       m.pointer_texcoord[1] = varray_texcoord2f[1];
+                       m.pointer_texcoord[1] = texcoord2f[1];
                }
                else
                        dobloomblend = true;
@@ -1742,9 +1776,9 @@ static void R_BlendView(void)
                if (dobloomblend)
                {
                        memset(&m, 0, sizeof(m));
-                       m.pointer_vertex = varray_vertex3f;
+                       m.pointer_vertex = vertex3f;
                        m.tex[0] = R_GetTexture(r_bloom_texture_bloom);
-                       m.pointer_texcoord[0] = varray_texcoord2f[1];
+                       m.pointer_texcoord[0] = texcoord2f[1];
                        R_Mesh_State(&m);
                        GL_BlendFunc(GL_ONE, GL_ONE);
                        GL_Color(1,1,1,1);
@@ -1756,7 +1790,7 @@ static void R_BlendView(void)
        {
                // apply a color tint to the whole view
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = varray_vertex3f;
+               m.pointer_vertex = vertex3f;
                R_Mesh_State(&m);
                GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
                GL_Color(r_refdef.viewblend[0], r_refdef.viewblend[1], r_refdef.viewblend[2], r_refdef.viewblend[3]);
@@ -2212,6 +2246,7 @@ void R_DrawSprite(int blendfunc1, int blendfunc2, rtexture_t *texture, rtexture_
 {
        float fog = 0.0f, ifog;
        rmeshstate_t m;
+       float vertex3f[12];
 
        if (fogenabled)
                fog = VERTEXFOGTABLE(VectorDistance(origin, r_vieworigin));
@@ -2222,23 +2257,23 @@ void R_DrawSprite(int blendfunc1, int blendfunc2, rtexture_t *texture, rtexture_
        GL_DepthMask(false);
        GL_DepthTest(!depthdisable);
 
-       varray_vertex3f[ 0] = origin[0] + left[0] * scalex2 + up[0] * scaley1;
-       varray_vertex3f[ 1] = origin[1] + left[1] * scalex2 + up[1] * scaley1;
-       varray_vertex3f[ 2] = origin[2] + left[2] * scalex2 + up[2] * scaley1;
-       varray_vertex3f[ 3] = origin[0] + left[0] * scalex2 + up[0] * scaley2;
-       varray_vertex3f[ 4] = origin[1] + left[1] * scalex2 + up[1] * scaley2;
-       varray_vertex3f[ 5] = origin[2] + left[2] * scalex2 + up[2] * scaley2;
-       varray_vertex3f[ 6] = origin[0] + left[0] * scalex1 + up[0] * scaley2;
-       varray_vertex3f[ 7] = origin[1] + left[1] * scalex1 + up[1] * scaley2;
-       varray_vertex3f[ 8] = origin[2] + left[2] * scalex1 + up[2] * scaley2;
-       varray_vertex3f[ 9] = origin[0] + left[0] * scalex1 + up[0] * scaley1;
-       varray_vertex3f[10] = origin[1] + left[1] * scalex1 + up[1] * scaley1;
-       varray_vertex3f[11] = origin[2] + left[2] * scalex1 + up[2] * scaley1;
+       vertex3f[ 0] = origin[0] + left[0] * scalex2 + up[0] * scaley1;
+       vertex3f[ 1] = origin[1] + left[1] * scalex2 + up[1] * scaley1;
+       vertex3f[ 2] = origin[2] + left[2] * scalex2 + up[2] * scaley1;
+       vertex3f[ 3] = origin[0] + left[0] * scalex2 + up[0] * scaley2;
+       vertex3f[ 4] = origin[1] + left[1] * scalex2 + up[1] * scaley2;
+       vertex3f[ 5] = origin[2] + left[2] * scalex2 + up[2] * scaley2;
+       vertex3f[ 6] = origin[0] + left[0] * scalex1 + up[0] * scaley2;
+       vertex3f[ 7] = origin[1] + left[1] * scalex1 + up[1] * scaley2;
+       vertex3f[ 8] = origin[2] + left[2] * scalex1 + up[2] * scaley2;
+       vertex3f[ 9] = origin[0] + left[0] * scalex1 + up[0] * scaley1;
+       vertex3f[10] = origin[1] + left[1] * scalex1 + up[1] * scaley1;
+       vertex3f[11] = origin[2] + left[2] * scalex1 + up[2] * scaley1;
 
        memset(&m, 0, sizeof(m));
        m.tex[0] = R_GetTexture(texture);
        m.pointer_texcoord[0] = spritetexcoord2f;
-       m.pointer_vertex = varray_vertex3f;
+       m.pointer_vertex = vertex3f;
        R_Mesh_State(&m);
        GL_Color(cr * ifog, cg * ifog, cb * ifog, ca);
        R_Mesh_Draw(0, 4, 2, polygonelements);
@@ -2499,6 +2534,29 @@ void R_UpdateAllTextureInfo(entity_render_t *ent)
                        R_UpdateTextureInfo(ent, ent->model->data_textures + i);
 }
 
+int rsurface_array_size = 0;
+float *rsurface_array_vertex3f = NULL;
+float *rsurface_array_svector3f = NULL;
+float *rsurface_array_tvector3f = NULL;
+float *rsurface_array_normal3f = NULL;
+float *rsurface_array_color4f = NULL;
+float *rsurface_array_texcoord3f = NULL;
+
+void R_Mesh_ResizeArrays(int newvertices)
+{
+       if (rsurface_array_size >= newvertices)
+               return;
+       if (rsurface_array_vertex3f)
+               Mem_Free(rsurface_array_vertex3f);
+       rsurface_array_size = (newvertices + 1023) & ~1023;
+       rsurface_array_vertex3f = Mem_Alloc(r_main_mempool, rsurface_array_size * sizeof(float[19]));
+       rsurface_array_svector3f = rsurface_array_vertex3f + rsurface_array_size * 3;
+       rsurface_array_tvector3f = rsurface_array_vertex3f + rsurface_array_size * 6;
+       rsurface_array_normal3f = rsurface_array_vertex3f + rsurface_array_size * 9;
+       rsurface_array_color4f = rsurface_array_vertex3f + rsurface_array_size * 12;
+       rsurface_array_texcoord3f = rsurface_array_vertex3f + rsurface_array_size * 16;
+}
+
 float *rsurface_vertex3f;
 float *rsurface_svector3f;
 float *rsurface_tvector3f;
@@ -2507,15 +2565,17 @@ float *rsurface_lightmapcolor4f;
 
 void RSurf_SetVertexPointer(const entity_render_t *ent, const texture_t *texture, const msurface_t *surface, const vec3_t modelorg, qboolean generatenormals, qboolean generatetangents)
 {
+       if (rsurface_array_size < surface->groupmesh->num_vertices)
+               R_Mesh_ResizeArrays(surface->groupmesh->num_vertices);
        if ((ent->frameblend[0].lerp != 1 || ent->frameblend[0].frame != 0) && (surface->groupmesh->data_morphvertex3f || surface->groupmesh->data_vertexboneweights))
        {
-               rsurface_vertex3f = varray_vertex3f;
+               rsurface_vertex3f = rsurface_array_vertex3f;
                Mod_Alias_GetMesh_Vertex3f(ent->model, ent->frameblend, surface->groupmesh, rsurface_vertex3f);
                if (generatetangents || (texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2)))
                {
-                       rsurface_svector3f = varray_svector3f;
-                       rsurface_tvector3f = varray_tvector3f;
-                       rsurface_normal3f = varray_normal3f;
+                       rsurface_svector3f = rsurface_array_svector3f;
+                       rsurface_tvector3f = rsurface_array_tvector3f;
+                       rsurface_normal3f = rsurface_array_normal3f;
                        Mod_BuildTextureVectorsAndNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, rsurface_vertex3f, surface->groupmesh->data_texcoordtexture2f, surface->groupmesh->data_element3i + surface->num_firsttriangle * 3, rsurface_svector3f, rsurface_tvector3f, rsurface_normal3f, r_smoothnormals_areaweighting.integer);
                }
                else
@@ -2524,7 +2584,7 @@ void RSurf_SetVertexPointer(const entity_render_t *ent, const texture_t *texture
                        rsurface_tvector3f = NULL;
                        if (generatenormals)
                        {
-                               rsurface_normal3f = varray_normal3f;
+                               rsurface_normal3f = rsurface_array_normal3f;
                                Mod_BuildNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, rsurface_vertex3f, surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle, rsurface_normal3f, r_smoothnormals_areaweighting.integer);
                        }
                        else
@@ -2570,12 +2630,12 @@ void RSurf_SetVertexPointer(const entity_render_t *ent, const texture_t *texture
                                VectorSet(up, 0, 0, 1);
                        }
                        for (i = 0;i < 4;i++)
-                               VectorMAMAMAM(1, center, v[i][0], forward, v[i][1], right, v[i][2], up, varray_vertex3f + (surface->num_firstvertex+i+j) * 3);
+                               VectorMAMAMAM(1, center, v[i][0], forward, v[i][1], right, v[i][2], up, rsurface_array_vertex3f + (surface->num_firstvertex+i+j) * 3);
                }
-               rsurface_vertex3f = varray_vertex3f;
-               rsurface_svector3f = varray_svector3f;
-               rsurface_tvector3f = varray_tvector3f;
-               rsurface_normal3f = varray_normal3f;
+               rsurface_vertex3f = rsurface_array_vertex3f;
+               rsurface_svector3f = rsurface_array_svector3f;
+               rsurface_tvector3f = rsurface_array_tvector3f;
+               rsurface_normal3f = rsurface_array_normal3f;
                Mod_BuildTextureVectorsAndNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, rsurface_vertex3f, surface->groupmesh->data_texcoordtexture2f, surface->groupmesh->data_element3i + surface->num_firsttriangle * 3, rsurface_svector3f, rsurface_tvector3f, rsurface_normal3f, r_smoothnormals_areaweighting.integer);
        }
        R_Mesh_VertexPointer(rsurface_vertex3f);
@@ -2612,7 +2672,7 @@ static void RSurf_DrawLightmap(const entity_render_t *ent, const texture_t *text
                        int numverts = surface->num_vertices;
                        v = rsurface_vertex3f + 3 * surface->num_firstvertex;
                        c2 = rsurface_normal3f + 3 * surface->num_firstvertex;
-                       c = varray_color4f + 4 * 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)
                        {
@@ -2627,7 +2687,7 @@ static void RSurf_DrawLightmap(const entity_render_t *ent, const texture_t *text
                        b = 1;
                        a = 1;
                        applycolor = false;
-                       rsurface_lightmapcolor4f = varray_color4f;
+                       rsurface_lightmapcolor4f = rsurface_array_color4f;
                }
                else
                {
@@ -2641,7 +2701,7 @@ static void RSurf_DrawLightmap(const entity_render_t *ent, const texture_t *text
        {
                if (surface->lightmapinfo && surface->lightmapinfo->stainsamples)
                {
-                       for (i = 0, c = varray_color4f + 4 * surface->num_firstvertex;i < surface->num_vertices;i++, c += 4)
+                       for (i = 0, c = rsurface_array_color4f + 4 * surface->num_firstvertex;i < surface->num_vertices;i++, c += 4)
                        {
                                if (surface->lightmapinfo->samples)
                                {
@@ -2671,7 +2731,7 @@ static void RSurf_DrawLightmap(const entity_render_t *ent, const texture_t *text
                                else
                                        VectorClear(c);
                        }
-                       rsurface_lightmapcolor4f = varray_color4f;
+                       rsurface_lightmapcolor4f = rsurface_array_color4f;
                }
                else
                        rsurface_lightmapcolor4f = surface->groupmesh->data_lightmapcolor4f;
@@ -2682,7 +2742,7 @@ static void RSurf_DrawLightmap(const entity_render_t *ent, const texture_t *text
        {
                if (rsurface_lightmapcolor4f)
                {
-                       for (i = 0, v = (rsurface_vertex3f + 3 * surface->num_firstvertex), c = (rsurface_lightmapcolor4f + 4 * surface->num_firstvertex), c2 = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4, c2 += 4)
+                       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, modelorg));
                                c2[0] = c[0] * f;
@@ -2693,7 +2753,7 @@ static void RSurf_DrawLightmap(const entity_render_t *ent, const texture_t *text
                }
                else
                {
-                       for (i = 0, v = (rsurface_vertex3f + 3 * surface->num_firstvertex), c2 = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c2 += 4)
+                       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, modelorg));
                                c2[0] = f;
@@ -2702,18 +2762,18 @@ static void RSurf_DrawLightmap(const entity_render_t *ent, const texture_t *text
                                c2[3] = 1;
                        }
                }
-               rsurface_lightmapcolor4f = varray_color4f;
+               rsurface_lightmapcolor4f = rsurface_array_color4f;
        }
        if (applycolor && rsurface_lightmapcolor4f)
        {
-               for (i = 0, c = (rsurface_lightmapcolor4f + 4 * surface->num_firstvertex), c2 = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, c += 4, c2 += 4)
+               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 = varray_color4f;
+               rsurface_lightmapcolor4f = rsurface_array_color4f;
        }
        R_Mesh_ColorPointer(rsurface_lightmapcolor4f);
        GL_Color(r, g, b, a);
@@ -2887,7 +2947,7 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                                m.tex[1] = R_GetTexture(layer->texture);
                                m.texmatrix[1] = layer->texmatrix;
                                m.texrgbscale[1] = layertexrgbscale;
-                               m.pointer_color = varray_color4f;
+                               m.pointer_color = rsurface_array_color4f;
                                R_Mesh_State(&m);
                                if (lightmode == 2)
                                {
@@ -2924,7 +2984,7 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                                memset(&m, 0, sizeof(m));
                                m.tex[0] = R_GetTexture(layer->texture);
                                m.texmatrix[0] = layer->texmatrix;
-                               m.pointer_color = varray_color4f;
+                               m.pointer_color = rsurface_array_color4f;
                                m.texrgbscale[0] = layertexrgbscale;
                                R_Mesh_State(&m);
                                if (lightmode == 2)
@@ -2959,7 +3019,7 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                                memset(&m, 0, sizeof(m));
                                m.tex[0] = R_GetTexture(layer->texture);
                                m.texmatrix[0] = layer->texmatrix;
-                               m.pointer_color = varray_color4f;
+                               m.pointer_color = rsurface_array_color4f;
                                m.texrgbscale[0] = layertexrgbscale;
                                R_Mesh_State(&m);
                                for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
@@ -2974,7 +3034,7 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                                m.tex[0] = R_GetTexture(layer->texture);
                                m.texmatrix[0] = layer->texmatrix;
                                m.texrgbscale[0] = layertexrgbscale;
-                               m.pointer_color = varray_color4f;
+                               m.pointer_color = rsurface_array_color4f;
                                R_Mesh_State(&m);
                                if (lightmode == 2)
                                {
@@ -2999,7 +3059,7 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                                memset(&m, 0, sizeof(m));
                                m.tex[0] = R_GetTexture(layer->texture);
                                m.texmatrix[0] = layer->texmatrix;
-                               m.pointer_color = varray_color4f;
+                               m.pointer_color = rsurface_array_color4f;
                                m.texrgbscale[0] = layertexrgbscale;
                                R_Mesh_State(&m);
                                for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
@@ -3025,8 +3085,8 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                                        RSurf_SetVertexPointer(ent, texture, surface, modelorg, false, false);
                                        if (layer->texture)
                                                R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
-                                       R_Mesh_ColorPointer(varray_color4f);
-                                       for (i = 0, v = (rsurface_vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
+                                       R_Mesh_ColorPointer(rsurface_array_color4f);
+                                       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, modelorg));
                                                c[0] = layercolor[0];