]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_backend.c
reduced memory usage by around 20MB (no longer allocates interleaved
[xonotic/darkplaces.git] / gl_backend.c
index 3596dc4f73d07215621a874efe92b60c93aea408..dc51f4251ec413eb185004cc4a021f70b7daf9aa 100644 (file)
@@ -12,7 +12,6 @@ extern D3DCAPS9 vid_d3d9caps;
 cvar_t gl_mesh_drawrangeelements = {0, "gl_mesh_drawrangeelements", "1", "use glDrawRangeElements function if available instead of glDrawElements (for performance comparisons or bug testing)"};
 cvar_t gl_mesh_testmanualfeeding = {0, "gl_mesh_testmanualfeeding", "0", "use glBegin(GL_TRIANGLES);glTexCoord2f();glVertex3f();glEnd(); primitives instead of glDrawElements (useful to test for driver bugs with glDrawElements)"};
 cvar_t gl_mesh_prefer_short_elements = {CVAR_SAVE, "gl_mesh_prefer_short_elements", "1", "use GL_UNSIGNED_SHORT element arrays instead of GL_UNSIGNED_INT"};
-cvar_t gl_mesh_separatearrays = {0, "gl_mesh_separatearrays", "1", "use several separate vertex arrays rather than one combined stream"};
 cvar_t gl_paranoid = {0, "gl_paranoid", "0", "enables OpenGL error checking and other tests"};
 cvar_t gl_printcheckerror = {0, "gl_printcheckerror", "0", "prints all OpenGL error checks, useful to identify location of driver crashes"};
 
@@ -166,7 +165,6 @@ typedef struct gl_state_s
        void *preparevertices_tempdata;
        size_t preparevertices_tempdatamaxsize;
        r_meshbuffer_t *preparevertices_dynamicvertexbuffer;
-       r_vertexposition_t *preparevertices_vertexposition;
        r_vertexgeneric_t *preparevertices_vertexgeneric;
        r_vertexmesh_t *preparevertices_vertexmesh;
        int preparevertices_numvertices;
@@ -499,7 +497,6 @@ void gl_backend_init(void)
        Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
        Cvar_RegisterVariable(&gl_mesh_testmanualfeeding);
        Cvar_RegisterVariable(&gl_mesh_prefer_short_elements);
-       Cvar_RegisterVariable(&gl_mesh_separatearrays);
 
        Cmd_AddCommand("gl_vbostats", GL_VBOStats_f, "prints a list of all buffer objects (vertex data and triangle elements) and total video memory used by them");
 
@@ -516,10 +513,164 @@ void R_Viewport_TransformToScreen(const r_viewport_t *v, const vec4_t in, vec4_t
        Matrix4x4_Transform4 (&v->projectmatrix, temp, out);
        iw = 1.0f / out[3];
        out[0] = v->x + (out[0] * iw + 1.0f) * v->width * 0.5f;
-       out[1] = v->y + v->height - (out[1] * iw + 1.0f) * v->height * 0.5f;
+
+       // for an odd reason, inverting this is wrong for R_Shadow_ScissorForBBox (we then get badly scissored lights)
+       //out[1] = v->y + v->height - (out[1] * iw + 1.0f) * v->height * 0.5f;
+       out[1] = v->y + (out[1] * iw + 1.0f) * v->height * 0.5f;
+
        out[2] = v->z + (out[2] * iw + 1.0f) * v->depth * 0.5f;
 }
 
+static int bboxedges[12][2] =
+{
+       // top
+       {0, 1}, // +X
+       {0, 2}, // +Y
+       {1, 3}, // Y, +X
+       {2, 3}, // X, +Y
+       // bottom
+       {4, 5}, // +X
+       {4, 6}, // +Y
+       {5, 7}, // Y, +X
+       {6, 7}, // X, +Y
+       // verticals
+       {0, 4}, // +Z
+       {1, 5}, // X, +Z
+       {2, 6}, // Y, +Z
+       {3, 7}, // XY, +Z
+};
+
+qboolean R_ScissorForBBox(const float *mins, const float *maxs, int *scissor)
+{
+       int i, ix1, iy1, ix2, iy2;
+       float x1, y1, x2, y2;
+       vec4_t v, v2;
+       float vertex[20][3];
+       int j, k;
+       vec4_t plane4f;
+       int numvertices;
+       float corner[8][4];
+       float dist[8];
+       int sign[8];
+       float f;
+
+       scissor[0] = r_refdef.view.viewport.x;
+       scissor[1] = r_refdef.view.viewport.y;
+       scissor[2] = r_refdef.view.viewport.width;
+       scissor[3] = r_refdef.view.viewport.height;
+
+       // if view is inside the box, just say yes it's visible
+       if (BoxesOverlap(r_refdef.view.origin, r_refdef.view.origin, mins, maxs))
+               return false;
+
+       x1 = y1 = x2 = y2 = 0;
+
+       // transform all corners that are infront of the nearclip plane
+       VectorNegate(r_refdef.view.frustum[4].normal, plane4f);
+       plane4f[3] = r_refdef.view.frustum[4].dist;
+       numvertices = 0;
+       for (i = 0;i < 8;i++)
+       {
+               Vector4Set(corner[i], (i & 1) ? maxs[0] : mins[0], (i & 2) ? maxs[1] : mins[1], (i & 4) ? maxs[2] : mins[2], 1);
+               dist[i] = DotProduct4(corner[i], plane4f);
+               sign[i] = dist[i] > 0;
+               if (!sign[i])
+               {
+                       VectorCopy(corner[i], vertex[numvertices]);
+                       numvertices++;
+               }
+       }
+       // if some points are behind the nearclip, add clipped edge points to make
+       // sure that the scissor boundary is complete
+       if (numvertices > 0 && numvertices < 8)
+       {
+               // add clipped edge points
+               for (i = 0;i < 12;i++)
+               {
+                       j = bboxedges[i][0];
+                       k = bboxedges[i][1];
+                       if (sign[j] != sign[k])
+                       {
+                               f = dist[j] / (dist[j] - dist[k]);
+                               VectorLerp(corner[j], f, corner[k], vertex[numvertices]);
+                               numvertices++;
+                       }
+               }
+       }
+
+       // if we have no points to check, it is behind the view plane
+       if (!numvertices)
+               return true;
+
+       // if we have some points to transform, check what screen area is covered
+       x1 = y1 = x2 = y2 = 0;
+       v[3] = 1.0f;
+       //Con_Printf("%i vertices to transform...\n", numvertices);
+       for (i = 0;i < numvertices;i++)
+       {
+               VectorCopy(vertex[i], v);
+               R_Viewport_TransformToScreen(&r_refdef.view.viewport, v, v2);
+               //Con_Printf("%.3f %.3f %.3f %.3f transformed to %.3f %.3f %.3f %.3f\n", v[0], v[1], v[2], v[3], v2[0], v2[1], v2[2], v2[3]);
+               if (i)
+               {
+                       if (x1 > v2[0]) x1 = v2[0];
+                       if (x2 < v2[0]) x2 = v2[0];
+                       if (y1 > v2[1]) y1 = v2[1];
+                       if (y2 < v2[1]) y2 = v2[1];
+               }
+               else
+               {
+                       x1 = x2 = v2[0];
+                       y1 = y2 = v2[1];
+               }
+       }
+
+       // now convert the scissor rectangle to integer screen coordinates
+       ix1 = (int)(x1 - 1.0f);
+       //iy1 = vid.height - (int)(y2 - 1.0f);
+       //iy1 = r_refdef.view.viewport.width + 2 * r_refdef.view.viewport.x - (int)(y2 - 1.0f);
+       iy1 = (int)(y1 - 1.0f);
+       ix2 = (int)(x2 + 1.0f);
+       //iy2 = vid.height - (int)(y1 + 1.0f);
+       //iy2 = r_refdef.view.viewport.height + 2 * r_refdef.view.viewport.y - (int)(y1 + 1.0f);
+       iy2 = (int)(y2 + 1.0f);
+       //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
+
+       // clamp it to the screen
+       if (ix1 < r_refdef.view.viewport.x) ix1 = r_refdef.view.viewport.x;
+       if (iy1 < r_refdef.view.viewport.y) iy1 = r_refdef.view.viewport.y;
+       if (ix2 > r_refdef.view.viewport.x + r_refdef.view.viewport.width) ix2 = r_refdef.view.viewport.x + r_refdef.view.viewport.width;
+       if (iy2 > r_refdef.view.viewport.y + r_refdef.view.viewport.height) iy2 = r_refdef.view.viewport.y + r_refdef.view.viewport.height;
+
+       // if it is inside out, it's not visible
+       if (ix2 <= ix1 || iy2 <= iy1)
+               return true;
+
+       // the light area is visible, set up the scissor rectangle
+       scissor[0] = ix1;
+       scissor[1] = iy1;
+       scissor[2] = ix2 - ix1;
+       scissor[3] = iy2 - iy1;
+
+       // D3D Y coordinate is top to bottom, OpenGL is bottom to top, fix the D3D one
+       switch(vid.renderpath)
+       {
+       case RENDERPATH_D3D9:
+       case RENDERPATH_D3D10:
+       case RENDERPATH_D3D11:
+               scissor[1] = vid.height - scissor[1] - scissor[3];
+               break;
+       case RENDERPATH_GL11:
+       case RENDERPATH_GL13:
+       case RENDERPATH_GL20:
+       case RENDERPATH_CGGL:
+               break;
+       }
+
+       return false;
+}
+
+
 static void R_Viewport_ApplyNearClipPlaneFloatGL(const r_viewport_t *v, float *m, float normalx, float normaly, float normalz, float dist)
 {
        float q[4];
@@ -992,11 +1143,11 @@ void R_Mesh_DestroyFramebufferObject(int fbo)
 void R_Mesh_SetRenderTargetsD3D9(IDirect3DSurface9 *depthsurface, IDirect3DSurface9 *colorsurface0, IDirect3DSurface9 *colorsurface1, IDirect3DSurface9 *colorsurface2, IDirect3DSurface9 *colorsurface3)
 {
 // LordHavoc: for some weird reason the redundant SetDepthStencilSurface calls are necessary (otherwise the lights fail depth test, as if they were using the shadowmap depth surface and render target still)
-//     if (gl_state.d3drt_depthsurface == depthsurface && gl_state.d3drt_colorsurfaces[0] == colorsurface0 && gl_state.d3drt_colorsurfaces[1] == colorsurface1 && gl_state.d3drt_colorsurfaces[2] == colorsurface2 && gl_state.d3drt_colorsurfaces[3] == colorsurface3)
-//             return;
+       if (gl_state.d3drt_depthsurface == depthsurface && gl_state.d3drt_colorsurfaces[0] == colorsurface0 && gl_state.d3drt_colorsurfaces[1] == colorsurface1 && gl_state.d3drt_colorsurfaces[2] == colorsurface2 && gl_state.d3drt_colorsurfaces[3] == colorsurface3)
+               return;
 
        gl_state.framebufferobject = depthsurface != gl_state.d3drt_backbufferdepthsurface || colorsurface0 != gl_state.d3drt_backbuffercolorsurface;
-//     if (gl_state.d3drt_depthsurface != depthsurface)
+       if (gl_state.d3drt_depthsurface != depthsurface)
        {
                gl_state.d3drt_depthsurface = depthsurface;
                IDirect3DDevice9_SetDepthStencilSurface(vid_d3d9dev, gl_state.d3drt_depthsurface);
@@ -2305,8 +2456,9 @@ void R_Mesh_Draw(int firstvertex, int numvertices, int firsttriangle, int numtri
        bufferoffset3i = element3i_bufferoffset;
        bufferobject3s = element3s_indexbuffer ? element3s_indexbuffer->bufferobject : 0;
        bufferoffset3s = element3s_bufferoffset;
-       r_refdef.stats.meshes++;
-       r_refdef.stats.meshes_elements += numelements;
+       r_refdef.stats.draws++;
+       r_refdef.stats.draws_vertices += numvertices;
+       r_refdef.stats.draws_elements += numelements;
        if (gl_paranoid.integer)
        {
                unsigned int i;
@@ -3344,13 +3496,13 @@ void R_Mesh_ResetTextureState(void)
 
 
 #ifdef SUPPORTD3D
-//#define r_vertexposition_d3d9fvf (D3DFVF_XYZ)
+//#define r_vertex3f_d3d9fvf (D3DFVF_XYZ)
 //#define r_vertexgeneric_d3d9fvf (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
 //#define r_vertexmesh_d3d9fvf (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX5 | D3DFVF_TEXCOORDSIZE1(3) | D3DFVF_TEXCOORDSIZE2(3) | D3DFVF_TEXCOORDSIZE3(3))
 
-D3DVERTEXELEMENT9 r_vertexposition_d3d9elements[] =
+D3DVERTEXELEMENT9 r_vertex3f_d3d9elements[] =
 {
-       {0, (int)((size_t)&((r_vertexposition_t *)0)->vertex3f), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
+       {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
        D3DDECL_END()
 };
 
@@ -3374,7 +3526,7 @@ D3DVERTEXELEMENT9 r_vertexmesh_d3d9elements[] =
        D3DDECL_END()
 };
 
-IDirect3DVertexDeclaration9 *r_vertexposition_d3d9decl;
+IDirect3DVertexDeclaration9 *r_vertex3f_d3d9decl;
 IDirect3DVertexDeclaration9 *r_vertexgeneric_d3d9decl;
 IDirect3DVertexDeclaration9 *r_vertexmesh_d3d9decl;
 #endif
@@ -3382,7 +3534,7 @@ IDirect3DVertexDeclaration9 *r_vertexmesh_d3d9decl;
 static void R_Mesh_InitVertexDeclarations(void)
 {
 #ifdef SUPPORTD3D
-       r_vertexposition_d3d9decl = NULL;
+       r_vertex3f_d3d9decl = NULL;
        r_vertexgeneric_d3d9decl = NULL;
        r_vertexmesh_d3d9decl = NULL;
        switch(vid.renderpath)
@@ -3393,7 +3545,7 @@ static void R_Mesh_InitVertexDeclarations(void)
        case RENDERPATH_GL11:
                break;
        case RENDERPATH_D3D9:
-               IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexposition_d3d9elements, &r_vertexposition_d3d9decl);
+               IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertex3f_d3d9elements, &r_vertex3f_d3d9decl);
                IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexgeneric_d3d9elements, &r_vertexgeneric_d3d9decl);
                IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexmesh_d3d9elements, &r_vertexmesh_d3d9decl);
                break;
@@ -3410,9 +3562,9 @@ static void R_Mesh_InitVertexDeclarations(void)
 static void R_Mesh_DestroyVertexDeclarations(void)
 {
 #ifdef SUPPORTD3D
-       if (r_vertexposition_d3d9decl)
-               IDirect3DVertexDeclaration9_Release(r_vertexposition_d3d9decl);
-       r_vertexposition_d3d9decl = NULL;
+       if (r_vertex3f_d3d9decl)
+               IDirect3DVertexDeclaration9_Release(r_vertex3f_d3d9decl);
+       r_vertex3f_d3d9decl = NULL;
        if (r_vertexgeneric_d3d9decl)
                IDirect3DVertexDeclaration9_Release(r_vertexgeneric_d3d9decl);
        r_vertexgeneric_d3d9decl = NULL;
@@ -3422,78 +3574,7 @@ static void R_Mesh_DestroyVertexDeclarations(void)
 #endif
 }
 
-r_vertexposition_t *R_Mesh_PrepareVertices_Position_Lock(int numvertices)
-{
-       size_t size;
-       size = sizeof(r_vertexposition_t) * numvertices;
-       if (gl_state.preparevertices_tempdatamaxsize < size)
-       {
-               gl_state.preparevertices_tempdatamaxsize = size;
-               gl_state.preparevertices_tempdata = Mem_Realloc(r_main_mempool, gl_state.preparevertices_tempdata, gl_state.preparevertices_tempdatamaxsize);
-       }
-       gl_state.preparevertices_vertexposition = (r_vertexposition_t *)gl_state.preparevertices_tempdata;
-       gl_state.preparevertices_numvertices = numvertices;
-       return gl_state.preparevertices_vertexposition;
-}
-
-qboolean R_Mesh_PrepareVertices_Position_Unlock(void)
-{
-       R_Mesh_PrepareVertices_Position(gl_state.preparevertices_numvertices, gl_state.preparevertices_vertexposition, NULL);
-       gl_state.preparevertices_vertexposition = NULL;
-       gl_state.preparevertices_numvertices = 0;
-       return true;
-}
-
-void R_Mesh_PrepareVertices_Position_Arrays(int numvertices, const float *vertex3f)
-{
-       int i;
-       r_vertexposition_t *vertex;
-       switch(vid.renderpath)
-       {
-       case RENDERPATH_GL20:
-       case RENDERPATH_CGGL:
-               R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
-               R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
-               R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
-               R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
-               R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
-               R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
-               R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
-               return;
-       case RENDERPATH_GL13:
-       case RENDERPATH_GL11:
-               R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
-               R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
-               R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
-               if (vid.texunits >= 2)
-                       R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
-               if (vid.texunits >= 3)
-                       R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
-               return;
-       case RENDERPATH_D3D9:
-#ifdef SUPPORTD3D
-               gl_state.d3dvertexbuffer = NULL;
-               gl_state.d3dvertexdata = (void *)vertex3f;
-               gl_state.d3dvertexsize = sizeof(float[3]);
-#endif
-               return;
-       case RENDERPATH_D3D10:
-               Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
-               break;
-       case RENDERPATH_D3D11:
-               Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
-               break;
-       }
-
-       // no quick path for this case, convert to vertex structs
-       vertex = R_Mesh_PrepareVertices_Position_Lock(numvertices);
-       for (i = 0;i < numvertices;i++)
-               VectorCopy(vertex3f + 3*i, vertex[i].vertex3f);
-       R_Mesh_PrepareVertices_Position_Unlock();
-       R_Mesh_PrepareVertices_Position(numvertices, vertex, NULL);
-}
-
-void R_Mesh_PrepareVertices_Position(int numvertices, const r_vertexposition_t *vertex, const r_meshbuffer_t *vertexbuffer)
+void R_Mesh_PrepareVertices_Vertex3f(int numvertices, const float *vertex3f, const r_meshbuffer_t *vertexbuffer)
 {
        // upload temporary vertexbuffer for this rendering
        if (!gl_state.usevbo_staticvertex)
@@ -3501,9 +3582,9 @@ void R_Mesh_PrepareVertices_Position(int numvertices, const r_vertexposition_t *
        if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
        {
                if (gl_state.preparevertices_dynamicvertexbuffer)
-                       R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex, numvertices * sizeof(*vertex));
+                       R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex3f, numvertices * sizeof(float[3]));
                else
-                       gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex, numvertices * sizeof(*vertex), "temporary", false, true, false);
+                       gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex3f, numvertices * sizeof(float[3]), "temporary", false, true, false);
                vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
        }
        switch(vid.renderpath)
@@ -3512,7 +3593,7 @@ void R_Mesh_PrepareVertices_Position(int numvertices, const r_vertexposition_t *
        case RENDERPATH_CGGL:
                if (vertexbuffer)
                {
-                       R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , vertexbuffer, (int)((unsigned char *)vertex->vertex3f           - (unsigned char *)vertex));
+                       R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
                        R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
                        R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
                        R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
@@ -3522,7 +3603,7 @@ void R_Mesh_PrepareVertices_Position(int numvertices, const r_vertexposition_t *
                }
                else
                {
-                       R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , NULL, 0);
+                       R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
                        R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
                        R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
                        R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
@@ -3534,14 +3615,14 @@ void R_Mesh_PrepareVertices_Position(int numvertices, const r_vertexposition_t *
        case RENDERPATH_GL13:
                if (vertexbuffer)
                {
-                       R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , vertexbuffer, (int)((unsigned char *)vertex->vertex3f           - (unsigned char *)vertex));
+                       R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
                        R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
                        R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
                        R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
                }
                else
                {
-                       R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , NULL, 0);
+                       R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
                        R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
                        R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
                        R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
@@ -3550,20 +3631,20 @@ void R_Mesh_PrepareVertices_Position(int numvertices, const r_vertexposition_t *
        case RENDERPATH_GL11:
                if (vertexbuffer)
                {
-                       R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , vertexbuffer, (int)((unsigned char *)vertex->vertex3f           - (unsigned char *)vertex));
+                       R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
                        R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
                        R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
                }
                else
                {
-                       R_Mesh_VertexPointer(     3, GL_FLOAT        , sizeof(*vertex), vertex->vertex3f          , NULL, 0);
+                       R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
                        R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
                        R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
                }
                break;
        case RENDERPATH_D3D9:
 #ifdef SUPPORTD3D
-               IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertexposition_d3d9decl);
+               IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertex3f_d3d9decl);
                if (vertexbuffer)
                        IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(*vertex));
                else
@@ -3614,7 +3695,7 @@ void R_Mesh_PrepareVertices_Generic_Arrays(int numvertices, const float *vertex3
        {
        case RENDERPATH_GL20:
        case RENDERPATH_CGGL:
-               if (gl_mesh_separatearrays.integer)
+               if (!vid.useinterleavedarrays)
                {
                        R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
                        R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
@@ -3628,7 +3709,7 @@ void R_Mesh_PrepareVertices_Generic_Arrays(int numvertices, const float *vertex3
                break;
        case RENDERPATH_GL13:
        case RENDERPATH_GL11:
-               if (gl_mesh_separatearrays.integer)
+               if (!vid.useinterleavedarrays)
                {
                        R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
                        R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
@@ -3795,7 +3876,7 @@ void R_Mesh_PrepareVertices_Mesh_Arrays(int numvertices, const float *vertex3f,
        {
        case RENDERPATH_GL20:
        case RENDERPATH_CGGL:
-               if (gl_mesh_separatearrays.integer)
+               if (!vid.useinterleavedarrays)
                {
                        R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
                        R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
@@ -3809,7 +3890,7 @@ void R_Mesh_PrepareVertices_Mesh_Arrays(int numvertices, const float *vertex3f,
                break;
        case RENDERPATH_GL13:
        case RENDERPATH_GL11:
-               if (gl_mesh_separatearrays.integer)
+               if (!vid.useinterleavedarrays)
                {
                        R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
                        R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);