]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - model_shared.c
Don't set MATERIALFLAG_NOSHADOW on SKY materials. They need to cast shadows to preve...
[xonotic/darkplaces.git] / model_shared.c
index 523461cde870cea69d71aca95495bd9cab16f8ca..8688b13e3fceb37ec4c25653638e15bb189532e7 100644 (file)
@@ -63,7 +63,7 @@ static q3shader_data_t* q3shader_data;
 static void mod_start(void)
 {
        int i, count;
-       int nummodels = Mem_ExpandableArray_IndexRange(&models);
+       int nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
        dp_model_t *mod;
 
        SCR_PushLoadingScreen(false, "Loading models", 1.0);
@@ -86,7 +86,7 @@ static void mod_start(void)
 static void mod_shutdown(void)
 {
        int i;
-       int nummodels = Mem_ExpandableArray_IndexRange(&models);
+       int nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
        dp_model_t *mod;
 
        for (i = 0;i < nummodels;i++)
@@ -100,8 +100,8 @@ static void mod_shutdown(void)
 static void mod_newmap(void)
 {
        msurface_t *surface;
-       int i, j, k, surfacenum, ssize, tsize;
-       int nummodels = Mem_ExpandableArray_IndexRange(&models);
+       int i, j, k, l, surfacenum, ssize, tsize;
+       int nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
        dp_model_t *mod;
 
        for (i = 0;i < nummodels;i++)
@@ -110,10 +110,11 @@ static void mod_newmap(void)
                {
                        for (j = 0;j < mod->num_textures && mod->data_textures;j++)
                        {
-                               for (k = 0;k < mod->data_textures[j].numskinframes;k++)
-                                       R_SkinFrame_MarkUsed(mod->data_textures[j].skinframes[k]);
-                               for (k = 0;k < mod->data_textures[j].backgroundnumskinframes;k++)
-                                       R_SkinFrame_MarkUsed(mod->data_textures[j].backgroundskinframes[k]);
+                               // note that materialshaderpass and backgroundshaderpass point to shaderpasses[] and so do the pre/post shader ranges, so this catches all of them...
+                               for (l = 0; l < Q3SHADER_MAXLAYERS; l++)
+                                       if (mod->data_textures[j].shaderpasses[l])
+                                               for (k = 0; k < mod->data_textures[j].shaderpasses[l]->numframes; k++)
+                                                       R_SkinFrame_MarkUsed(mod->data_textures[j].shaderpasses[l]->skinframes[k]);
                        }
                        if (mod->brush.solidskyskinframe)
                                R_SkinFrame_MarkUsed(mod->brush.solidskyskinframe);
@@ -200,12 +201,6 @@ void Mod_UnloadModel (dp_model_t *mod)
        used = mod->used;
        if (mod->mempool)
        {
-               if (mod->surfmesh.vertex3fbuffer)
-                       R_Mesh_DestroyMeshBuffer(mod->surfmesh.vertex3fbuffer);
-               mod->surfmesh.vertex3fbuffer = NULL;
-               if (mod->surfmesh.vertexmeshbuffer)
-                       R_Mesh_DestroyMeshBuffer(mod->surfmesh.vertexmeshbuffer);
-               mod->surfmesh.vertexmeshbuffer = NULL;
                if (mod->surfmesh.data_element3i_indexbuffer)
                        R_Mesh_DestroyMeshBuffer(mod->surfmesh.data_element3i_indexbuffer);
                mod->surfmesh.data_element3i_indexbuffer = NULL;
@@ -228,78 +223,95 @@ void Mod_UnloadModel (dp_model_t *mod)
        mod->loaded = false;
 }
 
-void R_Model_Null_Draw(entity_render_t *ent)
+static void R_Model_Null_Draw(entity_render_t *ent)
 {
        return;
 }
 
 
-typedef void (*mod_framegroupify_parsegroups_t) (unsigned int i, int start, int len, float fps, qboolean loop, void *pass);
+typedef void (*mod_framegroupify_parsegroups_t) (unsigned int i, int start, int len, float fps, qboolean loop, const char *name, void *pass);
 
-int Mod_FrameGroupify_ParseGroups(const char *buf, mod_framegroupify_parsegroups_t cb, void *pass)
+static int Mod_FrameGroupify_ParseGroups(const char *buf, mod_framegroupify_parsegroups_t cb, void *pass)
 {
        const char *bufptr;
        int start, len;
        float fps;
        unsigned int i;
        qboolean loop;
+       char name[64];
 
        bufptr = buf;
        i = 0;
-       for(;;)
+       while(bufptr)
        {
                // an anim scene!
-               if (!COM_ParseToken_Simple(&bufptr, true, false))
-                       break;
+
+               // REQUIRED: fetch start
+               COM_ParseToken_Simple(&bufptr, true, false, true);
+               if (!bufptr)
+                       break; // end of file
                if (!strcmp(com_token, "\n"))
                        continue; // empty line
                start = atoi(com_token);
-               if (!COM_ParseToken_Simple(&bufptr, true, false))
-                       break;
-               if (!strcmp(com_token, "\n"))
+
+               // REQUIRED: fetch length
+               COM_ParseToken_Simple(&bufptr, true, false, true);
+               if (!bufptr || !strcmp(com_token, "\n"))
                {
                        Con_Printf("framegroups file: missing number of frames\n");
                        continue;
                }
                len = atoi(com_token);
-               if (!COM_ParseToken_Simple(&bufptr, true, false))
-                       break;
-               // we default to looping as it's usually wanted, so to NOT loop you append a 0
-               if (strcmp(com_token, "\n"))
+
+               // OPTIONAL args start
+               COM_ParseToken_Simple(&bufptr, true, false, true);
+
+               // OPTIONAL: fetch fps
+               fps = 20;
+               if (bufptr && strcmp(com_token, "\n"))
                {
                        fps = atof(com_token);
-                       if (!COM_ParseToken_Simple(&bufptr, true, false))
-                               break;
-                       if (strcmp(com_token, "\n"))
-                               loop = atoi(com_token) != 0;
-                       else
-                               loop = true;
+                       COM_ParseToken_Simple(&bufptr, true, false, true);
                }
-               else
+
+               // OPTIONAL: fetch loopflag
+               loop = true;
+               if (bufptr && strcmp(com_token, "\n"))
+               {
+                       loop = (atoi(com_token) != 0);
+                       COM_ParseToken_Simple(&bufptr, true, false, true);
+               }
+
+               // OPTIONAL: fetch name
+               name[0] = 0;
+               if (bufptr && strcmp(com_token, "\n"))
                {
-                       fps = 20;
-                       loop = true;
+                       strlcpy(name, com_token, sizeof(name));
+                       COM_ParseToken_Simple(&bufptr, true, false, true);
                }
 
+               // OPTIONAL: remaining unsupported tokens (eat them)
+               while (bufptr && strcmp(com_token, "\n"))
+                       COM_ParseToken_Simple(&bufptr, true, false, true);
+
+               //Con_Printf("data: %d %d %d %f %d (%s)\n", i, start, len, fps, loop, name);
+
                if(cb)
-                       cb(i, start, len, fps, loop, pass);
+                       cb(i, start, len, fps, loop, (name[0] ? name : NULL), pass);
                ++i;
        }
 
        return i;
 }
 
-void Mod_FrameGroupify_ParseGroups_Count (unsigned int i, int start, int len, float fps, qboolean loop, void *pass)
-{
-       unsigned int *cnt = (unsigned int *) pass;
-       ++*cnt;
-}
-
-void Mod_FrameGroupify_ParseGroups_Store (unsigned int i, int start, int len, float fps, qboolean loop, void *pass)
+static void Mod_FrameGroupify_ParseGroups_Store (unsigned int i, int start, int len, float fps, qboolean loop, const char *name, void *pass)
 {
        dp_model_t *mod = (dp_model_t *) pass;
        animscene_t *anim = &mod->animscenes[i];
-       dpsnprintf(anim->name, sizeof(anim[i].name), "groupified_%d_anim", i);
+       if(name)
+               strlcpy(anim->name, name, sizeof(anim[i].name));
+       else
+               dpsnprintf(anim->name, sizeof(anim[i].name), "groupified_%d_anim", i);
        anim->firstframe = bound(0, start, mod->num_poses - 1);
        anim->framecount = bound(1, len, mod->num_poses - anim->firstframe);
        anim->framerate = max(1, fps);
@@ -307,7 +319,7 @@ void Mod_FrameGroupify_ParseGroups_Store (unsigned int i, int start, int len, fl
        //Con_Printf("frame group %d is %d %d %f %d\n", i, start, len, fps, loop);
 }
 
-void Mod_FrameGroupify(dp_model_t *mod, const char *buf)
+static void Mod_FrameGroupify(dp_model_t *mod, const char *buf)
 {
        unsigned int cnt;
 
@@ -328,7 +340,7 @@ void Mod_FrameGroupify(dp_model_t *mod, const char *buf)
        Mod_FrameGroupify_ParseGroups(buf, Mod_FrameGroupify_ParseGroups_Store, mod);
 }
 
-void Mod_FindPotentialDeforms(dp_model_t *mod)
+static void Mod_FindPotentialDeforms(dp_model_t *mod)
 {
        int i, j;
        texture_t *texture;
@@ -337,7 +349,9 @@ void Mod_FindPotentialDeforms(dp_model_t *mod)
        for (i = 0;i < mod->num_textures;i++)
        {
                texture = mod->data_textures + i;
-               if (texture->tcgen.tcgen == Q3TCGEN_ENVIRONMENT)
+               if (texture->materialshaderpass && texture->materialshaderpass->tcgen.tcgen == Q3TCGEN_ENVIRONMENT)
+                       mod->wantnormals = true;
+               if (texture->materialshaderpass && texture->materialshaderpass->tcgen.tcgen == Q3TCGEN_ENVIRONMENT)
                        mod->wantnormals = true;
                for (j = 0;j < Q3MAXDEFORMS;j++)
                {
@@ -366,6 +380,7 @@ dp_model_t *Mod_LoadModel(dp_model_t *mod, qboolean crash, qboolean checkdisk)
        unsigned int crc;
        void *buf;
        fs_offset_t filesize = 0;
+       char vabuf[1024];
 
        mod->used = true;
 
@@ -449,6 +464,9 @@ dp_model_t *Mod_LoadModel(dp_model_t *mod, qboolean crash, qboolean checkdisk)
        // errors can prevent the corresponding mod->loaded = true;
        mod->loaded = false;
 
+       // default lightmap scale
+       mod->lightmapscale = 1;
+
        // default model radius and bounding box (mainly for missing models)
        mod->radius = 16;
        VectorSet(mod->normalmins, -mod->radius, -mod->radius, -mod->radius);
@@ -486,13 +504,13 @@ dp_model_t *Mod_LoadModel(dp_model_t *mod, qboolean crash, qboolean checkdisk)
                else if (!memcmp(buf, "ACTRHEAD", 8)) Mod_PSKMODEL_Load(mod, buf, bufend);
                else if (!memcmp(buf, "INTERQUAKEMODEL", 16)) Mod_INTERQUAKEMODEL_Load(mod, buf, bufend);
                else if (strlen(mod->name) >= 4 && !strcmp(mod->name + strlen(mod->name) - 4, ".map")) Mod_MAP_Load(mod, buf, bufend);
-               else if (num == BSPVERSION || num == 30) Mod_Q1BSP_Load(mod, buf, bufend);
+               else if (num == BSPVERSION || num == 30 || !memcmp(buf, "BSP2", 4) || !memcmp(buf, "2PSB", 4)) Mod_Q1BSP_Load(mod, buf, bufend);
                else Con_Printf("Mod_LoadModel: model \"%s\" is of unknown/unsupported type\n", mod->name);
                Mem_Free(buf);
 
                Mod_FindPotentialDeforms(mod);
-                                       
-               buf = FS_LoadFile (va("%s.framegroups", mod->name), tempmempool, false, &filesize);
+
+               buf = FS_LoadFile(va(vabuf, sizeof(vabuf), "%s.framegroups", mod->name), tempmempool, false, &filesize);
                if(buf)
                {
                        Mod_FrameGroupify(mod, (const char *)buf);
@@ -518,7 +536,7 @@ dp_model_t *Mod_LoadModel(dp_model_t *mod, qboolean crash, qboolean checkdisk)
 void Mod_ClearUsed(void)
 {
        int i;
-       int nummodels = Mem_ExpandableArray_IndexRange(&models);
+       int nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
        dp_model_t *mod;
        for (i = 0;i < nummodels;i++)
                if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0])
@@ -528,7 +546,7 @@ void Mod_ClearUsed(void)
 void Mod_PurgeUnused(void)
 {
        int i;
-       int nummodels = Mem_ExpandableArray_IndexRange(&models);
+       int nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
        dp_model_t *mod;
        for (i = 0;i < nummodels;i++)
        {
@@ -558,10 +576,10 @@ dp_model_t *Mod_FindName(const char *name, const char *parentname)
        // if we're not dedicatd, the renderer calls will crash without video
        Host_StartVideo();
 
-       nummodels = Mem_ExpandableArray_IndexRange(&models);
+       nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
 
        if (!name[0])
-               Host_Error ("Mod_ForName: NULL name");
+               Host_Error ("Mod_ForName: empty name");
 
        // search the currently loaded models
        for (i = 0;i < nummodels;i++)
@@ -611,7 +629,7 @@ Reloads all models if they have changed
 void Mod_Reload(void)
 {
        int i, count;
-       int nummodels = Mem_ExpandableArray_IndexRange(&models);
+       int nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
        dp_model_t *mod;
 
        SCR_PushLoadingScreen(false, "Reloading models", 1.0);
@@ -642,7 +660,7 @@ Mod_Print
 static void Mod_Print(void)
 {
        int i;
-       int nummodels = Mem_ExpandableArray_IndexRange(&models);
+       int nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
        dp_model_t *mod;
 
        Con_Print("Loaded models:\n");
@@ -856,7 +874,8 @@ void Mod_BuildNormals(int firstvertex, int numvertices, int numtriangles, const
                VectorNormalize(vectorNormal);
 }
 
-void Mod_BuildBumpVectors(const float *v0, const float *v1, const float *v2, const float *tc0, const float *tc1, const float *tc2, float *svector3f, float *tvector3f, float *normal3f)
+#if 0
+static void Mod_BuildBumpVectors(const float *v0, const float *v1, const float *v2, const float *tc0, const float *tc1, const float *tc2, float *svector3f, float *tvector3f, float *normal3f)
 {
        float f, tangentcross[3], v10[3], v20[3], tc10[2], tc20[2];
        // 79 add/sub/negate/multiply (1 cycle), 1 compare (3 cycle?), total cycles not counting load/store/exchange roughly 82 cycles
@@ -899,12 +918,13 @@ void Mod_BuildBumpVectors(const float *v0, const float *v1, const float *v2, con
                VectorNegate(tvector3f, tvector3f);
        }
 }
+#endif
 
 // warning: this is a very expensive function!
 void Mod_BuildTextureVectorsFromNormals(int firstvertex, int numvertices, int numtriangles, const float *vertex3f, const float *texcoord2f, const float *normal3f, const int *elements, float *svector3f, float *tvector3f, qboolean areaweighting)
 {
        int i, tnum;
-       float sdir[3], tdir[3], normal[3], *sv, *tv;
+       float sdir[3], tdir[3], normal[3], *svec, *tvec;
        const float *v0, *v1, *v2, *tc0, *tc1, *tc2, *n;
        float f, tangentcross[3], v10[3], v20[3], tc10[2], tc20[2];
        const int *e;
@@ -970,14 +990,14 @@ void Mod_BuildTextureVectorsFromNormals(int firstvertex, int numvertices, int nu
        // make the tangents completely perpendicular to the surface normal, and
        // then normalize them
        // 16 assignments, 2 divide, 2 sqrt, 2 negates, 14 adds, 24 multiplies
-       for (i = 0, sv = svector3f + 3 * firstvertex, tv = tvector3f + 3 * firstvertex, n = normal3f + 3 * firstvertex;i < numvertices;i++, sv += 3, tv += 3, n += 3)
+       for (i = 0, svec = svector3f + 3 * firstvertex, tvec = tvector3f + 3 * firstvertex, n = normal3f + 3 * firstvertex;i < numvertices;i++, svec += 3, tvec += 3, n += 3)
        {
-               f = -DotProduct(sv, n);
-               VectorMA(sv, f, n, sv);
-               VectorNormalize(sv);
-               f = -DotProduct(tv, n);
-               VectorMA(tv, f, n, tv);
-               VectorNormalize(tv);
+               f = -DotProduct(svec, n);
+               VectorMA(svec, f, n, svec);
+               VectorNormalize(svec);
+               f = -DotProduct(tvec, n);
+               VectorMA(tvec, f, n, tvec);
+               VectorNormalize(tvec);
        }
 }
 
@@ -1212,21 +1232,13 @@ static void Mod_ShadowMesh_CreateVBOs(shadowmesh_t *mesh, mempool_t *mempool)
                }
        }
 
-       // upload r_vertexmesh_t array as a buffer
-       if (mesh->vertexmesh && !mesh->vertexmeshbuffer)
-               mesh->vertexmeshbuffer = R_Mesh_CreateMeshBuffer(mesh->vertexmesh, mesh->numverts * sizeof(*mesh->vertexmesh), loadmodel->name, false, false, false);
-
-       // upload vertex3f array as a buffer
-       if (mesh->vertex3f && !mesh->vertex3fbuffer)
-               mesh->vertex3fbuffer = R_Mesh_CreateMeshBuffer(mesh->vertex3f, mesh->numverts * sizeof(float[3]), loadmodel->name, false, false, false);
-
        // upload short indices as a buffer
        if (mesh->element3s && !mesh->element3s_indexbuffer)
-               mesh->element3s_indexbuffer = R_Mesh_CreateMeshBuffer(mesh->element3s, mesh->numtriangles * sizeof(short[3]), loadmodel->name, true, false, true);
+               mesh->element3s_indexbuffer = R_Mesh_CreateMeshBuffer(mesh->element3s, mesh->numtriangles * sizeof(short[3]), loadmodel->name, true, false, false, true);
 
        // upload int indices as a buffer
        if (mesh->element3i && !mesh->element3i_indexbuffer && !mesh->element3s)
-               mesh->element3i_indexbuffer = R_Mesh_CreateMeshBuffer(mesh->element3i, mesh->numtriangles * sizeof(int[3]), loadmodel->name, true, false, false);
+               mesh->element3i_indexbuffer = R_Mesh_CreateMeshBuffer(mesh->element3i, mesh->numtriangles * sizeof(int[3]), loadmodel->name, true, false, false, false);
 
        // vertex buffer is several arrays and we put them in the same buffer
        //
@@ -1235,21 +1247,23 @@ static void Mod_ShadowMesh_CreateVBOs(shadowmesh_t *mesh, mempool_t *mempool)
        // other hand animated models don't use a lot of vertices anyway...
        if (!mesh->vbo_vertexbuffer && !vid.useinterleavedarrays)
        {
-               size_t size;
+               int size;
                unsigned char *mem;
                size = 0;
+               mesh->vbooffset_vertexmesh         = size;if (mesh->vertexmesh        ) size += mesh->numverts * sizeof(r_vertexmesh_t);
                mesh->vbooffset_vertex3f           = size;if (mesh->vertex3f          ) size += mesh->numverts * sizeof(float[3]);
                mesh->vbooffset_svector3f          = size;if (mesh->svector3f         ) size += mesh->numverts * sizeof(float[3]);
                mesh->vbooffset_tvector3f          = size;if (mesh->tvector3f         ) size += mesh->numverts * sizeof(float[3]);
                mesh->vbooffset_normal3f           = size;if (mesh->normal3f          ) size += mesh->numverts * sizeof(float[3]);
                mesh->vbooffset_texcoord2f         = size;if (mesh->texcoord2f        ) size += mesh->numverts * sizeof(float[2]);
                mem = (unsigned char *)Mem_Alloc(tempmempool, size);
+               if (mesh->vertexmesh        ) memcpy(mem + mesh->vbooffset_vertexmesh        , mesh->vertexmesh        , mesh->numverts * sizeof(r_vertexmesh_t));
                if (mesh->vertex3f          ) memcpy(mem + mesh->vbooffset_vertex3f          , mesh->vertex3f          , mesh->numverts * sizeof(float[3]));
                if (mesh->svector3f         ) memcpy(mem + mesh->vbooffset_svector3f         , mesh->svector3f         , mesh->numverts * sizeof(float[3]));
                if (mesh->tvector3f         ) memcpy(mem + mesh->vbooffset_tvector3f         , mesh->tvector3f         , mesh->numverts * sizeof(float[3]));
                if (mesh->normal3f          ) memcpy(mem + mesh->vbooffset_normal3f          , mesh->normal3f          , mesh->numverts * sizeof(float[3]));
                if (mesh->texcoord2f        ) memcpy(mem + mesh->vbooffset_texcoord2f        , mesh->texcoord2f        , mesh->numverts * sizeof(float[2]));
-               mesh->vbo_vertexbuffer = R_Mesh_CreateMeshBuffer(mem, size, "shadowmesh", false, false, false);
+               mesh->vbo_vertexbuffer = R_Mesh_CreateMeshBuffer(mem, size, "shadowmesh", false, false, false, false);
                Mem_Free(mem);
        }
 }
@@ -1338,10 +1352,6 @@ void Mod_ShadowMesh_Free(shadowmesh_t *mesh)
        shadowmesh_t *nextmesh;
        for (;mesh;mesh = nextmesh)
        {
-               if (mesh->vertex3fbuffer)
-                       R_Mesh_DestroyMeshBuffer(mesh->vertex3fbuffer);
-               if (mesh->vertexmeshbuffer)
-                       R_Mesh_DestroyMeshBuffer(mesh->vertexmeshbuffer);
                if (mesh->element3i_indexbuffer)
                        R_Mesh_DestroyMeshBuffer(mesh->element3i_indexbuffer);
                if (mesh->element3s_indexbuffer)
@@ -1368,7 +1378,7 @@ void Mod_CreateCollisionMesh(dp_model_t *mod)
        for (k = 0;k < mod->nummodelsurfaces;k++)
        {
                surface = mod->data_surfaces + mod->firstmodelsurface + k;
-               if (!strcmp(surface->texture->name, "collision")) // found collision mesh
+               if (!strcmp(surface->texture->name, "collision") || !strcmp(surface->texture->name, "collisionconvex")) // found collision mesh
                {
                        usesinglecollisionmesh = true;
                        numcollisionmeshtriangles = surface->num_triangles;
@@ -1394,7 +1404,8 @@ void Mod_CreateCollisionMesh(dp_model_t *mod)
        mod->brush.collisionmesh = Mod_ShadowMesh_Finish(mempool, mod->brush.collisionmesh, false, false, false);
 }
 
-void Mod_GetTerrainVertex3fTexCoord2fFromBGRA(const unsigned char *imagepixels, int imagewidth, int imageheight, int ix, int iy, float *vertex3f, float *texcoord2f, matrix4x4_t *pixelstepmatrix, matrix4x4_t *pixeltexturestepmatrix)
+#if 0
+static void Mod_GetTerrainVertex3fTexCoord2fFromBGRA(const unsigned char *imagepixels, int imagewidth, int imageheight, int ix, int iy, float *vertex3f, float *texcoord2f, matrix4x4_t *pixelstepmatrix, matrix4x4_t *pixeltexturestepmatrix)
 {
        float v[3], tc[3];
        v[0] = ix;
@@ -1409,7 +1420,7 @@ void Mod_GetTerrainVertex3fTexCoord2fFromBGRA(const unsigned char *imagepixels,
        texcoord2f[1] = tc[1];
 }
 
-void Mod_GetTerrainVertexFromBGRA(const unsigned char *imagepixels, int imagewidth, int imageheight, int ix, int iy, float *vertex3f, float *svector3f, float *tvector3f, float *normal3f, float *texcoord2f, matrix4x4_t *pixelstepmatrix, matrix4x4_t *pixeltexturestepmatrix)
+static void Mod_GetTerrainVertexFromBGRA(const unsigned char *imagepixels, int imagewidth, int imageheight, int ix, int iy, float *vertex3f, float *svector3f, float *tvector3f, float *normal3f, float *texcoord2f, matrix4x4_t *pixelstepmatrix, matrix4x4_t *pixeltexturestepmatrix)
 {
        float vup[3], vdown[3], vleft[3], vright[3];
        float tcup[3], tcdown[3], tcleft[3], tcright[3];
@@ -1434,7 +1445,7 @@ void Mod_GetTerrainVertexFromBGRA(const unsigned char *imagepixels, int imagewid
        VectorAdd(normal3f, nl, normal3f);
 }
 
-void Mod_ConstructTerrainPatchFromBGRA(const unsigned char *imagepixels, int imagewidth, int imageheight, int x1, int y1, int width, int height, int *element3i, int *neighbor3i, float *vertex3f, float *svector3f, float *tvector3f, float *normal3f, float *texcoord2f, matrix4x4_t *pixelstepmatrix, matrix4x4_t *pixeltexturestepmatrix)
+static void Mod_ConstructTerrainPatchFromBGRA(const unsigned char *imagepixels, int imagewidth, int imageheight, int x1, int y1, int width, int height, int *element3i, int *neighbor3i, float *vertex3f, float *svector3f, float *tvector3f, float *normal3f, float *texcoord2f, matrix4x4_t *pixelstepmatrix, matrix4x4_t *pixeltexturestepmatrix)
 {
        int x, y, ix, iy, *e;
        e = element3i;
@@ -1456,6 +1467,7 @@ void Mod_ConstructTerrainPatchFromBGRA(const unsigned char *imagepixels, int ima
                for (x = 0, ix = x1;x < width + 1;x++, ix++, vertex3f += 3, texcoord2f += 2, svector3f += 3, tvector3f += 3, normal3f += 3)
                        Mod_GetTerrainVertexFromBGRA(imagepixels, imagewidth, imageheight, ix, iy, vertex3f, texcoord2f, svector3f, tvector3f, normal3f, pixelstepmatrix, pixeltexturestepmatrix);
 }
+#endif
 
 #if 0
 void Mod_Terrain_SurfaceRecurseChunk(dp_model_t *model, int stepsize, int x, int y)
@@ -1537,7 +1549,7 @@ void Mod_Terrain_UpdateSurfacesForViewOrigin(dp_model_t *model)
 }
 #endif
 
-int Mod_LoadQ3Shaders_EnumerateWaveFunc(const char *s)
+static int Mod_LoadQ3Shaders_EnumerateWaveFunc(const char *s)
 {
        int offset = 0;
        if (!strncasecmp(s, "user", 4)) // parse stuff like "user1sin", always user<n>func
@@ -1569,7 +1581,7 @@ static void Q3Shader_AddToHash (q3shaderinfo_t* shader)
        unsigned short hash = CRC_Block_CaseInsensitive ((const unsigned char *)shader->name, strlen (shader->name));
        q3shader_hash_entry_t* entry = q3shader_data->hash + (hash % Q3SHADER_HASH_SIZE);
        q3shader_hash_entry_t* lastEntry = NULL;
-       while (entry != NULL)
+       do
        {
                if (strcasecmp (entry->shader.name, shader->name) == 0)
                {
@@ -1602,6 +1614,7 @@ static void Q3Shader_AddToHash (q3shaderinfo_t* shader)
                lastEntry = entry;
                entry = entry->chain;
        }
+       while (entry != NULL);
        if (entry == NULL)
        {
                if (lastEntry->shader.name[0] != 0)
@@ -1627,6 +1640,8 @@ extern cvar_t mod_q3shader_default_offsetmapping_scale;
 extern cvar_t mod_q3shader_default_offsetmapping_bias;
 extern cvar_t mod_q3shader_default_polygonoffset;
 extern cvar_t mod_q3shader_default_polygonfactor;
+extern cvar_t mod_q3shader_force_addalpha;
+extern cvar_t mod_q3shader_force_terrain_alphaflag;
 void Mod_LoadQ3Shaders(void)
 {
        int j;
@@ -1639,8 +1654,8 @@ void Mod_LoadQ3Shaders(void)
        int numparameters;
        char parameter[TEXTURE_MAXFRAMES + 4][Q3PATHLENGTH];
        char *custsurfaceparmnames[256]; // VorteX: q3map2 has 64 but well, someone will need more
-       unsigned long custsurfaceparms[256]; 
-       int numcustsurfaceparms;
+       unsigned long custsurfaceflags[256]; 
+       int numcustsurfaceflags;
        qboolean dpshaderkill;
 
        Mod_FreeQ3Shaders();
@@ -1654,7 +1669,7 @@ void Mod_LoadQ3Shaders(void)
                q3shaders_mem, sizeof (char**), 256);
 
        // parse custinfoparms.txt
-       numcustsurfaceparms = 0;
+       numcustsurfaceflags = 0;
        if ((text = f = (char *)FS_LoadFile("scripts/custinfoparms.txt", tempmempool, false, NULL)) != NULL)
        {
                if (!COM_ParseToken_QuakeC(&text, false) || strcasecmp(com_token, "{"))
@@ -1674,21 +1689,21 @@ void Mod_LoadQ3Shaders(void)
                                        if (!strcasecmp(com_token, "}"))
                                                break;  
                                        // register surfaceflag
-                                       if (numcustsurfaceparms >= 256)
+                                       if (numcustsurfaceflags >= 256)
                                        {
                                                Con_Printf("scripts/custinfoparms.txt: surfaceflags section parsing error - max 256 surfaceflags exceeded\n");
                                                break;
                                        }
                                        // name
-                                       j = strlen(com_token)+1;
-                                       custsurfaceparmnames[numcustsurfaceparms] = (char *)Mem_Alloc(tempmempool, j);
-                                       strlcpy(custsurfaceparmnames[numcustsurfaceparms], com_token, j+1);
+                                       j = (int)strlen(com_token)+1;
+                                       custsurfaceparmnames[numcustsurfaceflags] = (char *)Mem_Alloc(tempmempool, j);
+                                       strlcpy(custsurfaceparmnames[numcustsurfaceflags], com_token, j+1);
                                        // value
                                        if (COM_ParseToken_QuakeC(&text, false))
-                                               custsurfaceparms[numcustsurfaceparms] = strtol(com_token, NULL, 0);
+                                               custsurfaceflags[numcustsurfaceflags] = strtol(com_token, NULL, 0);
                                        else
-                                               custsurfaceparms[numcustsurfaceparms] = 0;
-                                       numcustsurfaceparms++;
+                                               custsurfaceflags[numcustsurfaceflags] = 0;
+                                       numcustsurfaceflags++;
                                }
                        }
                }
@@ -1707,6 +1722,22 @@ void Mod_LoadQ3Shaders(void)
                while (COM_ParseToken_QuakeC(&text, false))
                {
                        memset (&shader, 0, sizeof(shader));
+                       shader.name[0] = 0;
+                       shader.surfaceparms = 0;
+                       shader.surfaceflags = 0;
+                       shader.textureflags = 0;
+                       shader.numlayers = 0;
+                       shader.lighting = false;
+                       shader.vertexalpha = false;
+                       shader.textureblendalpha = false;
+                       shader.skyboxname[0] = 0;
+                       shader.deforms[0].deform = Q3DEFORM_NONE;
+                       shader.dpnortlight = false;
+                       shader.dpshadow = false;
+                       shader.dpnoshadow = false;
+                       shader.dpmeshcollisions = false;
+                       shader.dpshaderkill = false;
+                       shader.dpreflectcube[0] = 0;
                        shader.reflectmin = 0;
                        shader.reflectmax = 1;
                        shader.refractfactor = 1;
@@ -1714,13 +1745,19 @@ void Mod_LoadQ3Shaders(void)
                        shader.reflectfactor = 1;
                        Vector4Set(shader.reflectcolor4f, 1, 1, 1, 1);
                        shader.r_water_wateralpha = 1;
+                       shader.r_water_waterscroll[0] = 0;
+                       shader.r_water_waterscroll[1] = 0;
                        shader.offsetmapping = (mod_q3shader_default_offsetmapping.value) ? OFFSETMAPPING_DEFAULT : OFFSETMAPPING_OFF;
                        shader.offsetscale = mod_q3shader_default_offsetmapping_scale.value;
                        shader.offsetbias = mod_q3shader_default_offsetmapping_bias.value;
-                       shader.specularscalemod = 1;
-                       shader.specularpowermod = 1;
                        shader.biaspolygonoffset = mod_q3shader_default_polygonoffset.value;
                        shader.biaspolygonfactor = mod_q3shader_default_polygonfactor.value;
+                       shader.transparentsort = TRANSPARENTSORT_DISTANCE;
+                       shader.specularscalemod = 1;
+                       shader.specularpowermod = 1;
+                       shader.rtlightambient = 0;
+                       // WHEN ADDING DEFAULTS HERE, REMEMBER TO PUT DEFAULTS IN ALL LOADERS
+                       // JUST GREP FOR "specularscalemod = 1".
 
                        strlcpy(shader.name, com_token, sizeof(shader.name));
                        if (!COM_ParseToken_QuakeC(&text, false) || strcasecmp(com_token, "{"))
@@ -1789,6 +1826,11 @@ void Mod_LoadQ3Shaders(void)
                                                                        layer->blendfunc[0] = GL_ONE;
                                                                        layer->blendfunc[1] = GL_ONE;
                                                                }
+                                                               else if (!strcasecmp(parameter[1], "addalpha"))
+                                                               {
+                                                                       layer->blendfunc[0] = GL_SRC_ALPHA;
+                                                                       layer->blendfunc[1] = GL_ONE;
+                                                               }
                                                                else if (!strcasecmp(parameter[1], "filter"))
                                                                {
                                                                        layer->blendfunc[0] = GL_DST_COLOR;
@@ -1816,7 +1858,7 @@ void Mod_LoadQ3Shaders(void)
                                                                        else if (!strcasecmp(parameter[k+1], "GL_DST_COLOR"))
                                                                                layer->blendfunc[k] = GL_DST_COLOR;
                                                                        else if (!strcasecmp(parameter[k+1], "GL_DST_ALPHA"))
-                                                                               layer->blendfunc[k] = GL_ONE_MINUS_DST_ALPHA;
+                                                                               layer->blendfunc[k] = GL_DST_ALPHA;
                                                                        else if (!strcasecmp(parameter[k+1], "GL_ONE_MINUS_SRC_COLOR"))
                                                                                layer->blendfunc[k] = GL_ONE_MINUS_SRC_COLOR;
                                                                        else if (!strcasecmp(parameter[k+1], "GL_ONE_MINUS_SRC_ALPHA"))
@@ -1968,8 +2010,19 @@ void Mod_LoadQ3Shaders(void)
                                                {
                                                        // multilayer terrain shader or similar
                                                        shader.textureblendalpha = true;
+                                                       if (mod_q3shader_force_terrain_alphaflag.integer)
+                                                               shader.layers[0].texflags |= TEXF_ALPHA;
                                                }
                                        }
+
+                                       if(mod_q3shader_force_addalpha.integer)
+                                       {
+                                               // for a long while, DP treated GL_ONE GL_ONE as GL_SRC_ALPHA GL_ONE
+                                               // this cvar brings back this behaviour
+                                               if(layer->blendfunc[0] == GL_ONE && layer->blendfunc[1] == GL_ONE)
+                                                       layer->blendfunc[0] = GL_SRC_ALPHA;
+                                       }
+                                       
                                        layer->texflags = 0;
                                        if (layer->alphatest)
                                                layer->texflags |= TEXF_ALPHA;
@@ -2089,19 +2142,21 @@ void Mod_LoadQ3Shaders(void)
                                                shader.surfaceparms |= Q3SURFACEPARM_POINTLIGHT;
                                        else if (!strcasecmp(parameter[1], "antiportal"))
                                                shader.surfaceparms |= Q3SURFACEPARM_ANTIPORTAL;
+                                       else if (!strcasecmp(parameter[1], "skip"))
+                                               ; // shader.surfaceparms |= Q3SURFACEPARM_SKIP; FIXME we don't have enough #defines for this any more, and the engine doesn't need this one anyway
                                        else
                                        {
                                                // try custom surfaceparms
-                                               for (j = 0; j < numcustsurfaceparms; j++)
+                                               for (j = 0; j < numcustsurfaceflags; j++)
                                                {
                                                        if (!strcasecmp(custsurfaceparmnames[j], parameter[1]))
                                                        {
-                                                               shader.surfaceparms |= custsurfaceparms[j];
+                                                               shader.surfaceflags |= custsurfaceflags[j];
                                                                break;
                                                        }
                                                }
                                                // failed all
-                                               if (j == numcustsurfaceparms)
+                                               if (j == numcustsurfaceflags)
                                                        Con_DPrintf("%s parsing warning: unknown surfaceparm \"%s\"\n", search->filenames[fileindex], parameter[1]);
                                        }
                                }
@@ -2203,6 +2258,18 @@ void Mod_LoadQ3Shaders(void)
                                                        shader.biaspolygonoffset = 0;
                                        }
                                }
+                               else if (!strcasecmp(parameter[0], "dptransparentsort") && numparameters >= 2)
+                               {
+                                       shader.textureflags |= Q3TEXTUREFLAG_TRANSPARENTSORT;
+                                       if (!strcasecmp(parameter[1], "sky"))
+                                               shader.transparentsort = TRANSPARENTSORT_SKY;
+                                       else if (!strcasecmp(parameter[1], "distance"))
+                                               shader.transparentsort = TRANSPARENTSORT_DISTANCE;
+                                       else if (!strcasecmp(parameter[1], "hud"))
+                                               shader.transparentsort = TRANSPARENTSORT_HUD;
+                                       else
+                                               Con_DPrintf("%s parsing warning: unknown dptransparentsort category \"%s\", or not enough arguments\n", search->filenames[fileindex], parameter[1]);
+                               }
                                else if (!strcasecmp(parameter[0], "dprefract") && numparameters >= 5)
                                {
                                        shader.textureflags |= Q3TEXTUREFLAG_REFRACTION;
@@ -2259,8 +2326,17 @@ void Mod_LoadQ3Shaders(void)
                                                shader.offsetmapping = OFFSETMAPPING_RELIEF;
                                        if (numparameters >= 3)
                                                shader.offsetscale = atof(parameter[2]);
-                                       if (numparameters >= 4)
-                                               shader.offsetbias = atof(parameter[3]);
+                                       if (numparameters >= 5)
+                                       {
+                                               if(!strcasecmp(parameter[3], "bias"))
+                                                       shader.offsetbias = atof(parameter[4]);
+                                               else if(!strcasecmp(parameter[3], "match"))
+                                                       shader.offsetbias = 1.0f - atof(parameter[4]);
+                                               else if(!strcasecmp(parameter[3], "match8"))
+                                                       shader.offsetbias = 1.0f - atof(parameter[4]) / 255.0f;
+                                               else if(!strcasecmp(parameter[3], "match16"))
+                                                       shader.offsetbias = 1.0f - atof(parameter[4]) / 65535.0f;
+                                       }
                                }
                                else if (!strcasecmp(parameter[0], "deformvertexes") && numparameters >= 2)
                                {
@@ -2305,30 +2381,6 @@ void Mod_LoadQ3Shaders(void)
                        // hide this shader if a cvar said it should be killed
                        if (shader.dpshaderkill)
                                shader.numlayers = 0;
-                       // pick the primary layer to render with
-                       if (shader.numlayers)
-                       {
-                               shader.backgroundlayer = -1;
-                               shader.primarylayer = 0;
-                               // if lightmap comes first this is definitely an ordinary texture
-                               // if the first two layers have the correct blendfuncs and use vertex alpha, it is a blended terrain shader
-                               if ((shader.layers[shader.primarylayer].texturename != NULL)
-                                 && !strcasecmp(shader.layers[shader.primarylayer].texturename[0], "$lightmap"))
-                               {
-                                       shader.backgroundlayer = -1;
-                                       shader.primarylayer = 1;
-                               }
-                               else if (shader.numlayers >= 2
-                               &&   shader.layers[1].alphagen.alphagen == Q3ALPHAGEN_VERTEX
-                               &&  (shader.layers[0].blendfunc[0] == GL_ONE       && shader.layers[0].blendfunc[1] == GL_ZERO                && !shader.layers[0].alphatest)
-                               && ((shader.layers[1].blendfunc[0] == GL_SRC_ALPHA && shader.layers[1].blendfunc[1] == GL_ONE_MINUS_SRC_ALPHA)
-                               ||  (shader.layers[1].blendfunc[0] == GL_ONE       && shader.layers[1].blendfunc[1] == GL_ZERO                &&  shader.layers[1].alphatest)))
-                               {
-                                       // terrain blending or other effects
-                                       shader.backgroundlayer = 0;
-                                       shader.primarylayer = 1;
-                               }
-                       }
                        // fix up multiple reflection types
                        if(shader.textureflags & Q3TEXTUREFLAG_WATERSHADER)
                                shader.textureflags &= ~(Q3TEXTUREFLAG_REFRACTION | Q3TEXTUREFLAG_REFLECTION | Q3TEXTUREFLAG_CAMERA);
@@ -2339,7 +2391,7 @@ void Mod_LoadQ3Shaders(void)
        }
        FS_FreeSearch(search);
        // free custinfoparm values
-       for (j = 0; j < numcustsurfaceparms; j++)
+       for (j = 0; j < numcustsurfaceflags; j++)
                Mem_Free(custsurfaceparmnames[j]);
 }
 
@@ -2360,15 +2412,59 @@ q3shaderinfo_t *Mod_LookupQ3Shader(const char *name)
        return NULL;
 }
 
-qboolean Mod_LoadTextureFromQ3Shader(texture_t *texture, const char *name, qboolean warnmissing, qboolean fallback, int defaulttexflags)
+texture_shaderpass_t *Mod_CreateShaderPass(skinframe_t *skinframe)
+{
+       texture_shaderpass_t *shaderpass = (texture_shaderpass_t *)Mem_Alloc(loadmodel->mempool, sizeof(*shaderpass));
+       shaderpass->framerate = 0.0f;
+       shaderpass->numframes = 1;
+       shaderpass->blendfunc[0] = GL_ONE;
+       shaderpass->blendfunc[1] = GL_ZERO;
+       shaderpass->rgbgen.rgbgen = Q3RGBGEN_IDENTITY;
+       shaderpass->alphagen.alphagen = Q3ALPHAGEN_IDENTITY;
+       shaderpass->alphatest = false;
+       shaderpass->tcgen.tcgen = Q3TCGEN_TEXTURE;
+       shaderpass->skinframes[0] = skinframe;
+       return shaderpass;
+}
+
+texture_shaderpass_t *Mod_CreateShaderPassFromQ3ShaderLayer(q3shaderinfo_layer_t *layer, int layerindex, int texflags, const char *texturename)
 {
        int j;
+       texture_shaderpass_t *shaderpass = (texture_shaderpass_t *)Mem_Alloc(loadmodel->mempool, sizeof(*shaderpass));
+       shaderpass->alphatest = layer->alphatest != 0;
+       shaderpass->framerate = layer->framerate;
+       shaderpass->numframes = layer->numframes;
+       shaderpass->blendfunc[0] = layer->blendfunc[0];
+       shaderpass->blendfunc[1] = layer->blendfunc[1];
+       shaderpass->rgbgen = layer->rgbgen;
+       shaderpass->alphagen = layer->alphagen;
+       shaderpass->tcgen = layer->tcgen;
+       for (j = 0; j < Q3MAXTCMODS && layer->tcmods[j].tcmod != Q3TCMOD_NONE; j++)
+               shaderpass->tcmods[j] = layer->tcmods[j];
+       for (j = 0; j < layer->numframes; j++)
+       {
+               if (cls.state == ca_dedicated)
+               {
+                       shaderpass->skinframes[j] = NULL;
+               }
+               else if (!(shaderpass->skinframes[j] = R_SkinFrame_LoadExternal(layer->texturename[j], texflags, false)))
+               {
+                       Con_Printf("^1%s:^7 could not load texture ^3\"%s\"^7 (frame %i) for layer %i of shader ^2\"%s\"\n", loadmodel->name, layer->texturename[j], j, layerindex, texturename);
+                       shaderpass->skinframes[j] = R_SkinFrame_LoadMissing();
+               }
+       }
+       return shaderpass;
+}
+
+qboolean Mod_LoadTextureFromQ3Shader(texture_t *texture, const char *name, qboolean warnmissing, qboolean fallback, int defaulttexflags)
+{
        int texflagsmask, texflagsor;
        qboolean success = true;
        q3shaderinfo_t *shader;
        if (!name)
                name = "";
        strlcpy(texture->name, name, sizeof(texture->name));
+       texture->basealpha = 1.0f;
        shader = name[0] ? Mod_LookupQ3Shader(name) : NULL;
 
        texflagsmask = ~0;
@@ -2388,22 +2484,21 @@ qboolean Mod_LoadTextureFromQ3Shader(texture_t *texture, const char *name, qbool
        texture->specularscalemod = 1;
        texture->specularpowermod = 1; 
        texture->rtlightambient = 0;
-       // WHEN ADDING DEFAULTS HERE, REMEMBER TO SYNC TO SHADER LOADING ABOVE
-       // HERE, AND Q1BSP LOADING
+       texture->transparentsort = TRANSPARENTSORT_DISTANCE;
+       // WHEN ADDING DEFAULTS HERE, REMEMBER TO PUT DEFAULTS IN ALL LOADERS
        // JUST GREP FOR "specularscalemod = 1".
 
        if (shader)
        {
                if (developer_loading.integer)
                        Con_Printf("%s: loaded shader for %s\n", loadmodel->name, name);
-               texture->surfaceparms = shader->surfaceparms;
 
                // allow disabling of picmip or compression by defaulttexflags
                texture->textureflags = (shader->textureflags & texflagsmask) | texflagsor;
 
                if (shader->surfaceparms & Q3SURFACEPARM_SKY)
                {
-                       texture->basematerialflags = MATERIALFLAG_SKY | MATERIALFLAG_NOSHADOW;
+                       texture->basematerialflags = MATERIALFLAG_SKY;
                        if (shader->skyboxname[0])
                        {
                                // quake3 seems to append a _ to the skybox name, so this must do so as well
@@ -2434,6 +2529,7 @@ qboolean Mod_LoadTextureFromQ3Shader(texture_t *texture, const char *name, qbool
                        texture->basematerialflags |= MATERIALFLAG_CAMERA;
                texture->customblendfunc[0] = GL_ONE;
                texture->customblendfunc[1] = GL_ZERO;
+               texture->transparentsort = shader->transparentsort;
                if (shader->numlayers > 0)
                {
                        texture->customblendfunc[0] = shader->layers[0].blendfunc[0];
@@ -2473,57 +2569,107 @@ nothing                GL_ZERO GL_ONE
                }
                if (!shader->lighting)
                        texture->basematerialflags |= MATERIALFLAG_FULLBRIGHT;
-               if (shader->primarylayer >= 0)
+
+               // here be dragons: convert quake3 shaders to material
+               if (shader->numlayers > 0)
                {
-                       q3shaderinfo_layer_t* primarylayer = shader->layers + shader->primarylayer;
-                       // copy over many primarylayer parameters
-                       texture->rgbgen = primarylayer->rgbgen;
-                       texture->alphagen = primarylayer->alphagen;
-                       texture->tcgen = primarylayer->tcgen;
-                       memcpy(texture->tcmods, primarylayer->tcmods, sizeof(texture->tcmods));
-                       // load the textures
-                       texture->numskinframes = primarylayer->numframes;
-                       texture->skinframerate = primarylayer->framerate;
-                       for (j = 0;j < primarylayer->numframes;j++)
+                       int i;
+                       int terrainbackgroundlayer = -1;
+                       int lightmaplayer = -1;
+                       int alphagenspecularlayer = -1;
+                       int rgbgenvertexlayer = -1;
+                       int rgbgendiffuselayer = -1;
+                       int materiallayer = -1;
+                       int endofprelayers = 0;
+                       int firstpostlayer = 0;
+                       int shaderpassindex = 0;
+                       for (i = 0; i < shader->numlayers; i++)
                        {
-                               if(cls.state == ca_dedicated)
-                               {
-                                       texture->skinframes[j] = NULL;
-                               }
-                               else if (!(texture->skinframes[j] = R_SkinFrame_LoadExternal(primarylayer->texturename[j], (primarylayer->texflags & texflagsmask) | texflagsor, false)))
-                               {
-                                       Con_Printf("^1%s:^7 could not load texture ^3\"%s\"^7 (frame %i) for shader ^2\"%s\"\n", loadmodel->name, primarylayer->texturename[j], j, texture->name);
-                                       texture->skinframes[j] = R_SkinFrame_LoadMissing();
-                               }
+                               if (shader->layers[i].texturename != NULL && !strcasecmp(shader->layers[i].texturename[0], "$lightmap"))
+                                       lightmaplayer = i;
+                               if (shader->layers[i].rgbgen.rgbgen == Q3RGBGEN_VERTEX)
+                                       rgbgenvertexlayer = i;
+                               if (shader->layers[i].rgbgen.rgbgen == Q3RGBGEN_LIGHTINGDIFFUSE)
+                                       rgbgendiffuselayer = i;
+                               if (shader->layers[i].alphagen.alphagen == Q3ALPHAGEN_LIGHTINGSPECULAR)
+                                       alphagenspecularlayer = i;
                        }
-               }
-               if (shader->backgroundlayer >= 0)
-               {
-                       q3shaderinfo_layer_t* backgroundlayer = shader->layers + shader->backgroundlayer;
-                       // copy over one secondarylayer parameter
-                       memcpy(texture->backgroundtcmods, backgroundlayer->tcmods, sizeof(texture->backgroundtcmods));
-                       // load the textures
-                       texture->backgroundnumskinframes = backgroundlayer->numframes;
-                       texture->backgroundskinframerate = backgroundlayer->framerate;
-                       for (j = 0;j < backgroundlayer->numframes;j++)
+                       if (shader->numlayers >= 2
+                        && shader->layers[1].alphagen.alphagen == Q3ALPHAGEN_VERTEX
+                        && (shader->layers[0].blendfunc[0] == GL_ONE && shader->layers[0].blendfunc[1] == GL_ZERO && !shader->layers[0].alphatest)
+                        && ((shader->layers[1].blendfunc[0] == GL_SRC_ALPHA && shader->layers[1].blendfunc[1] == GL_ONE_MINUS_SRC_ALPHA)
+                                || (shader->layers[1].blendfunc[0] == GL_ONE && shader->layers[1].blendfunc[1] == GL_ZERO && shader->layers[1].alphatest)))
                        {
-                               if(cls.state == ca_dedicated)
-                               {
-                                       texture->skinframes[j] = NULL;
-                               }
-                               else if (!(texture->backgroundskinframes[j] = R_SkinFrame_LoadExternal(backgroundlayer->texturename[j], (backgroundlayer->texflags & texflagsmask) | texflagsor, false)))
-                               {
-                                       Con_Printf("^1%s:^7 could not load texture ^3\"%s\"^7 (background frame %i) for shader ^2\"%s\"\n", loadmodel->name, backgroundlayer->texturename[j], j, texture->name);
-                                       texture->backgroundskinframes[j] = R_SkinFrame_LoadMissing();
-                               }
+                               // terrain blend or certain other effects involving alphatest over a regular layer
+                               terrainbackgroundlayer = 0;
+                               materiallayer = 1;
+                               // terrain may be vertex lit (in which case both layers are rgbGen vertex) or lightmapped (in which ase the third layer is lightmap)
+                               firstpostlayer = lightmaplayer >= 0 ? lightmaplayer + 1 : materiallayer + 1;
                        }
+                       else if (lightmaplayer == 0)
+                       {
+                               // ordinary texture but with $lightmap before diffuse
+                               materiallayer = 1;
+                               firstpostlayer = lightmaplayer + 2;
+                       }
+                       else if (lightmaplayer >= 1)
+                       {
+                               // ordinary texture - we don't properly apply lighting to the prelayers, but oh well...
+                               endofprelayers = lightmaplayer - 1;
+                               materiallayer = lightmaplayer - 1;
+                               firstpostlayer = lightmaplayer + 1;
+                       }
+                       else if (rgbgenvertexlayer >= 0)
+                       {
+                               // map models with baked lighting
+                               materiallayer = rgbgenvertexlayer;
+                               endofprelayers = rgbgenvertexlayer;
+                               firstpostlayer = rgbgenvertexlayer + 1;
+                       }
+                       else if (rgbgendiffuselayer >= 0)
+                       {
+                               // entity models with dynamic lighting
+                               materiallayer = rgbgendiffuselayer;
+                               endofprelayers = rgbgendiffuselayer;
+                               firstpostlayer = rgbgendiffuselayer + 1;
+                               // player models often have specular as a pass after diffuse - we don't currently make use of that specular texture (would need to meld it into the skinframe)...
+                               if (alphagenspecularlayer >= 0)
+                                       firstpostlayer = alphagenspecularlayer + 1;
+                       }
+                       else
+                       {
+                               // special effects shaders - treat first as primary layer and do everything else as post
+                               endofprelayers = 0;
+                               materiallayer = 0;
+                               firstpostlayer = 1;
+                       }
+                       // convert the main material layer
+                       // FIXME: if alphagenspecularlayer is used, we should pass a specular texture name to R_SkinFrame_LoadExternal and have it load that texture instead of the assumed name for _gloss texture
+                       if (materiallayer >= 0)
+                               texture->materialshaderpass = texture->shaderpasses[shaderpassindex++] = Mod_CreateShaderPassFromQ3ShaderLayer(&shader->layers[materiallayer], materiallayer, (shader->layers[materiallayer].texflags & texflagsmask) | texflagsor, texture->name);
+                       // convert the terrain background blend layer (if any)
+                       if (terrainbackgroundlayer >= 0)
+                               texture->backgroundshaderpass = texture->shaderpasses[shaderpassindex++] = Mod_CreateShaderPassFromQ3ShaderLayer(&shader->layers[terrainbackgroundlayer], terrainbackgroundlayer, (shader->layers[terrainbackgroundlayer].texflags & texflagsmask) | texflagsor, texture->name);
+                       // convert the prepass layers (if any)
+                       texture->startpreshaderpass = shaderpassindex;
+                       for (i = 0; i < endofprelayers; i++)
+                               texture->shaderpasses[shaderpassindex++] = Mod_CreateShaderPassFromQ3ShaderLayer(&shader->layers[i], i, (shader->layers[i].texflags & texflagsmask) | texflagsor, texture->name);
+                       texture->endpreshaderpass = shaderpassindex;
+                       texture->startpostshaderpass = shaderpassindex;
+                       // convert the postpass layers (if any)
+                       for (i = firstpostlayer; i < shader->numlayers; i++)
+                               texture->shaderpasses[shaderpassindex++] = Mod_CreateShaderPassFromQ3ShaderLayer(&shader->layers[i], i, (shader->layers[i].texflags & texflagsmask) | texflagsor, texture->name);
+                       texture->startpostshaderpass = shaderpassindex;
                }
+
                if (shader->dpshadow)
                        texture->basematerialflags &= ~MATERIALFLAG_NOSHADOW;
                if (shader->dpnoshadow)
                        texture->basematerialflags |= MATERIALFLAG_NOSHADOW;
                if (shader->dpnortlight)
                        texture->basematerialflags |= MATERIALFLAG_NORTLIGHT;
+               if (shader->vertexalpha)
+                       texture->basematerialflags |= MATERIALFLAG_ALPHAGEN_VERTEX;
                memcpy(texture->deforms, shader->deforms, sizeof(texture->deforms));
                texture->reflectmin = shader->reflectmin;
                texture->reflectmax = shader->reflectmax;
@@ -2585,6 +2731,40 @@ nothing                GL_ZERO GL_ONE
        //      if (shader->surfaceparms & Q3SURFACEPARM_LIGHTGRID    ) texture->supercontents |= SUPERCONTENTS_LIGHTGRID    ;
        //      if (shader->surfaceparms & Q3SURFACEPARM_ANTIPORTAL   ) texture->supercontents |= SUPERCONTENTS_ANTIPORTAL   ;
 
+               texture->surfaceflags = shader->surfaceflags;
+               if (shader->surfaceparms & Q3SURFACEPARM_ALPHASHADOW  ) texture->surfaceflags |= Q3SURFACEFLAG_ALPHASHADOW  ;
+       //      if (shader->surfaceparms & Q3SURFACEPARM_AREAPORTAL   ) texture->surfaceflags |= Q3SURFACEFLAG_AREAPORTAL   ;
+       //      if (shader->surfaceparms & Q3SURFACEPARM_CLUSTERPORTAL) texture->surfaceflags |= Q3SURFACEFLAG_CLUSTERPORTAL;
+       //      if (shader->surfaceparms & Q3SURFACEPARM_DETAIL       ) texture->surfaceflags |= Q3SURFACEFLAG_DETAIL       ;
+       //      if (shader->surfaceparms & Q3SURFACEPARM_DONOTENTER   ) texture->surfaceflags |= Q3SURFACEFLAG_DONOTENTER   ;
+       //      if (shader->surfaceparms & Q3SURFACEPARM_FOG          ) texture->surfaceflags |= Q3SURFACEFLAG_FOG          ;
+       //      if (shader->surfaceparms & Q3SURFACEPARM_LAVA         ) texture->surfaceflags |= Q3SURFACEFLAG_LAVA         ;
+               if (shader->surfaceparms & Q3SURFACEPARM_LIGHTFILTER  ) texture->surfaceflags |= Q3SURFACEFLAG_LIGHTFILTER  ;
+               if (shader->surfaceparms & Q3SURFACEPARM_METALSTEPS   ) texture->surfaceflags |= Q3SURFACEFLAG_METALSTEPS   ;
+               if (shader->surfaceparms & Q3SURFACEPARM_NODAMAGE     ) texture->surfaceflags |= Q3SURFACEFLAG_NODAMAGE     ;
+               if (shader->surfaceparms & Q3SURFACEPARM_NODLIGHT     ) texture->surfaceflags |= Q3SURFACEFLAG_NODLIGHT     ;
+               if (shader->surfaceparms & Q3SURFACEPARM_NODRAW       ) texture->surfaceflags |= Q3SURFACEFLAG_NODRAW       ;
+       //      if (shader->surfaceparms & Q3SURFACEPARM_NODROP       ) texture->surfaceflags |= Q3SURFACEFLAG_NODROP       ;
+               if (shader->surfaceparms & Q3SURFACEPARM_NOIMPACT     ) texture->surfaceflags |= Q3SURFACEFLAG_NOIMPACT     ;
+               if (shader->surfaceparms & Q3SURFACEPARM_NOLIGHTMAP   ) texture->surfaceflags |= Q3SURFACEFLAG_NOLIGHTMAP   ;
+               if (shader->surfaceparms & Q3SURFACEPARM_NOMARKS      ) texture->surfaceflags |= Q3SURFACEFLAG_NOMARKS      ;
+       //      if (shader->surfaceparms & Q3SURFACEPARM_NOMIPMAPS    ) texture->surfaceflags |= Q3SURFACEFLAG_NOMIPMAPS    ;
+               if (shader->surfaceparms & Q3SURFACEPARM_NONSOLID     ) texture->surfaceflags |= Q3SURFACEFLAG_NONSOLID     ;
+       //      if (shader->surfaceparms & Q3SURFACEPARM_ORIGIN       ) texture->surfaceflags |= Q3SURFACEFLAG_ORIGIN       ;
+       //      if (shader->surfaceparms & Q3SURFACEPARM_PLAYERCLIP   ) texture->surfaceflags |= Q3SURFACEFLAG_PLAYERCLIP   ;
+               if (shader->surfaceparms & Q3SURFACEPARM_SKY          ) texture->surfaceflags |= Q3SURFACEFLAG_SKY          ;
+               if (shader->surfaceparms & Q3SURFACEPARM_SLICK        ) texture->surfaceflags |= Q3SURFACEFLAG_SLICK        ;
+       //      if (shader->surfaceparms & Q3SURFACEPARM_SLIME        ) texture->surfaceflags |= Q3SURFACEFLAG_SLIME        ;
+       //      if (shader->surfaceparms & Q3SURFACEPARM_STRUCTURAL   ) texture->surfaceflags |= Q3SURFACEFLAG_STRUCTURAL   ;
+       //      if (shader->surfaceparms & Q3SURFACEPARM_TRANS        ) texture->surfaceflags |= Q3SURFACEFLAG_TRANS        ;
+       //      if (shader->surfaceparms & Q3SURFACEPARM_WATER        ) texture->surfaceflags |= Q3SURFACEFLAG_WATER        ;
+               if (shader->surfaceparms & Q3SURFACEPARM_POINTLIGHT   ) texture->surfaceflags |= Q3SURFACEFLAG_POINTLIGHT   ;
+               if (shader->surfaceparms & Q3SURFACEPARM_HINT         ) texture->surfaceflags |= Q3SURFACEFLAG_HINT         ;
+               if (shader->surfaceparms & Q3SURFACEPARM_DUST         ) texture->surfaceflags |= Q3SURFACEFLAG_DUST         ;
+       //      if (shader->surfaceparms & Q3SURFACEPARM_BOTCLIP      ) texture->surfaceflags |= Q3SURFACEFLAG_BOTCLIP      ;
+       //      if (shader->surfaceparms & Q3SURFACEPARM_LIGHTGRID    ) texture->surfaceflags |= Q3SURFACEFLAG_LIGHTGRID    ;
+       //      if (shader->surfaceparms & Q3SURFACEPARM_ANTIPORTAL   ) texture->surfaceflags |= Q3SURFACEFLAG_ANTIPORTAL   ;
+
                if (shader->dpmeshcollisions)
                        texture->basematerialflags |= MATERIALFLAG_MESHCOLLISIONS;
                if (shader->dpshaderkill && developer_extra.integer)
@@ -2594,14 +2774,12 @@ nothing                GL_ZERO GL_ONE
        {
                if (developer_extra.integer)
                        Con_DPrintf("^1%s:^7 using fallback noshader material for ^3\"%s\"\n", loadmodel->name, name);
-               texture->surfaceparms = 0;
                texture->supercontents = SUPERCONTENTS_SOLID | SUPERCONTENTS_OPAQUE;
        }
        else if (!strcmp(texture->name, "common/nodraw") || !strcmp(texture->name, "textures/common/nodraw"))
        {
                if (developer_extra.integer)
                        Con_DPrintf("^1%s:^7 using fallback nodraw material for ^3\"%s\"\n", loadmodel->name, name);
-               texture->surfaceparms = 0;
                texture->basematerialflags = MATERIALFLAG_NODRAW | MATERIALFLAG_NOSHADOW;
                texture->supercontents = SUPERCONTENTS_SOLID;
        }
@@ -2609,7 +2787,6 @@ nothing                GL_ZERO GL_ONE
        {
                if (developer_extra.integer)
                        Con_DPrintf("^1%s:^7 No shader found for texture ^3\"%s\"\n", loadmodel->name, texture->name);
-               texture->surfaceparms = 0;
                if (texture->surfaceflags & Q3SURFACEFLAG_NODRAW)
                {
                        texture->basematerialflags |= MATERIALFLAG_NODRAW | MATERIALFLAG_NOSHADOW;
@@ -2617,7 +2794,7 @@ nothing                GL_ZERO GL_ONE
                }
                else if (texture->surfaceflags & Q3SURFACEFLAG_SKY)
                {
-                       texture->basematerialflags |= MATERIALFLAG_SKY | MATERIALFLAG_NOSHADOW;
+                       texture->basematerialflags |= MATERIALFLAG_SKY;
                        texture->supercontents = SUPERCONTENTS_SKY;
                }
                else
@@ -2625,20 +2802,22 @@ nothing                GL_ZERO GL_ONE
                        texture->basematerialflags |= MATERIALFLAG_WALL;
                        texture->supercontents = SUPERCONTENTS_SOLID | SUPERCONTENTS_OPAQUE;
                }
-               texture->numskinframes = 1;
                if(cls.state == ca_dedicated)
                {
-                       texture->skinframes[0] = NULL;
+                       texture->materialshaderpass = NULL;
                        success = false;
                }
                else
                {
                        if (fallback)
                        {
-                               if ((texture->skinframes[0] = R_SkinFrame_LoadExternal(texture->name, defaulttexflags, false)))
+                               texture->materialshaderpass = texture->shaderpasses[0] = Mod_CreateShaderPass(R_SkinFrame_LoadExternal(texture->name, defaulttexflags, false));
+                               if (texture->materialshaderpass->skinframes[0])
                                {
-                                       if(texture->skinframes[0]->hasalpha)
+                                       if (texture->materialshaderpass->skinframes[0]->hasalpha)
                                                texture->basematerialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
+                                       if (texture->q2contents)
+                                               texture->supercontents = Mod_Q2BSP_SuperContentsFromNativeContents(loadmodel, texture->q2contents);
                                }
                                else
                                        success = false;
@@ -2651,12 +2830,12 @@ nothing                GL_ZERO GL_ONE
        }
        // init the animation variables
        texture->currentframe = texture;
-       if (texture->numskinframes < 1)
-               texture->numskinframes = 1;
-       if (!texture->skinframes[0])
-               texture->skinframes[0] = R_SkinFrame_LoadMissing();
-       texture->currentskinframe = texture->skinframes[0];
-       texture->backgroundcurrentskinframe = texture->backgroundskinframes[0];
+       if (!texture->materialshaderpass)
+               texture->materialshaderpass = texture->shaderpasses[0] = Mod_CreateShaderPass(R_SkinFrame_LoadMissing());
+       if (!texture->materialshaderpass->skinframes[0])
+               texture->materialshaderpass->skinframes[0] = R_SkinFrame_LoadMissing();
+       texture->currentskinframe = texture->materialshaderpass ? texture->materialshaderpass->skinframes[0] : NULL;
+       texture->backgroundcurrentskinframe = texture->backgroundshaderpass ? texture->backgroundshaderpass->skinframes[0] : NULL;
        return success;
 }
 
@@ -2668,6 +2847,7 @@ skinfile_t *Mod_LoadSkinFiles(void)
        skinfile_t *skinfile = NULL, *first = NULL;
        skinfileitem_t *skinfileitem;
        char word[10][MAX_QPATH];
+       char vabuf[1024];
 
 /*
 sample file:
@@ -2685,7 +2865,7 @@ tag_weapon,
 tag_torso,
 */
        memset(word, 0, sizeof(word));
-       for (i = 0;i < 256 && (data = text = (char *)FS_LoadFile(va("%s_%i.skin", loadmodel->name, i), tempmempool, true, NULL));i++)
+       for (i = 0;i < 256 && (data = text = (char *)FS_LoadFile(va(vabuf, sizeof(vabuf), "%s_%i.skin", loadmodel->name, i), tempmempool, true, NULL));i++)
        {
                // If it's the first file we parse
                if (skinfile == NULL)
@@ -2855,7 +3035,7 @@ void Mod_MakeSortedSurfaces(dp_model_t *mod)
        for (j = 0;j < mod->nummodelsurfaces;j++)
        {
                const msurface_t *surface = mod->data_surfaces + j + mod->firstmodelsurface;
-               int t = (int)(surface->texture - mod->data_textures);
+               t = (int)(surface->texture - mod->data_textures);
                numsurfacesfortexture[t]++;
        }
        j = 0;
@@ -2867,7 +3047,7 @@ void Mod_MakeSortedSurfaces(dp_model_t *mod)
        for (j = 0;j < mod->nummodelsurfaces;j++)
        {
                const msurface_t *surface = mod->data_surfaces + j + mod->firstmodelsurface;
-               int t = (int)(surface->texture - mod->data_textures);
+               t = (int)(surface->texture - mod->data_textures);
                mod->sortedmodelsurfaces[firstsurfacefortexture[t]++] = j + mod->firstmodelsurface;
        }
        Mem_Free(firstsurfacefortexture);
@@ -2894,12 +3074,12 @@ void Mod_BuildVBOs(void)
 
        // build r_vertexmesh_t array
        // (compressed interleaved array for D3D)
-       if (!loadmodel->surfmesh.vertexmesh && vid.useinterleavedarrays)
+       if (!loadmodel->surfmesh.data_vertexmesh && vid.useinterleavedarrays)
        {
                int vertexindex;
                int numvertices = loadmodel->surfmesh.num_vertices;
                r_vertexmesh_t *vertexmesh;
-               loadmodel->surfmesh.vertexmesh = vertexmesh = (r_vertexmesh_t*)Mem_Alloc(loadmodel->mempool, numvertices * sizeof(*loadmodel->surfmesh.vertexmesh));
+               loadmodel->surfmesh.data_vertexmesh = vertexmesh = (r_vertexmesh_t*)Mem_Alloc(loadmodel->mempool, numvertices * sizeof(r_vertexmesh_t));
                for (vertexindex = 0;vertexindex < numvertices;vertexindex++, vertexmesh++)
                {
                        VectorCopy(loadmodel->surfmesh.data_vertex3f + 3*vertexindex, vertexmesh->vertex3f);
@@ -2911,24 +3091,20 @@ void Mod_BuildVBOs(void)
                        Vector2Copy(loadmodel->surfmesh.data_texcoordtexture2f + 2*vertexindex, vertexmesh->texcoordtexture2f);
                        if (loadmodel->surfmesh.data_texcoordlightmap2f)
                                Vector2Scale(loadmodel->surfmesh.data_texcoordlightmap2f + 2*vertexindex, 1.0f, vertexmesh->texcoordlightmap2f);
+                       if (loadmodel->surfmesh.data_skeletalindex4ub)
+                               Vector4Copy(loadmodel->surfmesh.data_skeletalindex4ub + 4*vertexindex, vertexmesh->skeletalindex4ub);
+                       if (loadmodel->surfmesh.data_skeletalweight4ub)
+                               Vector4Copy(loadmodel->surfmesh.data_skeletalweight4ub + 4*vertexindex, vertexmesh->skeletalweight4ub);
                }
        }
 
-       // upload r_vertexmesh_t array as a buffer
-       if (loadmodel->surfmesh.vertexmesh && !loadmodel->surfmesh.vertexmeshbuffer)
-               loadmodel->surfmesh.vertexmeshbuffer = R_Mesh_CreateMeshBuffer(loadmodel->surfmesh.vertexmesh, loadmodel->surfmesh.num_vertices * sizeof(*loadmodel->surfmesh.vertexmesh), loadmodel->name, false, false, false);
-
-       // upload vertex3f array as a buffer
-       if (loadmodel->surfmesh.data_vertex3f && !loadmodel->surfmesh.vertex3fbuffer)
-               loadmodel->surfmesh.vertex3fbuffer = R_Mesh_CreateMeshBuffer(loadmodel->surfmesh.data_vertex3f, loadmodel->surfmesh.num_vertices * sizeof(float[3]), loadmodel->name, false, false, false);
-
        // upload short indices as a buffer
        if (loadmodel->surfmesh.data_element3s && !loadmodel->surfmesh.data_element3s_indexbuffer)
-               loadmodel->surfmesh.data_element3s_indexbuffer = R_Mesh_CreateMeshBuffer(loadmodel->surfmesh.data_element3s, loadmodel->surfmesh.num_triangles * sizeof(short[3]), loadmodel->name, true, false, true);
+               loadmodel->surfmesh.data_element3s_indexbuffer = R_Mesh_CreateMeshBuffer(loadmodel->surfmesh.data_element3s, loadmodel->surfmesh.num_triangles * sizeof(short[3]), loadmodel->name, true, false, false, true);
 
        // upload int indices as a buffer
        if (loadmodel->surfmesh.data_element3i && !loadmodel->surfmesh.data_element3i_indexbuffer && !loadmodel->surfmesh.data_element3s)
-               loadmodel->surfmesh.data_element3i_indexbuffer = R_Mesh_CreateMeshBuffer(loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles * sizeof(int[3]), loadmodel->name, true, false, false);
+               loadmodel->surfmesh.data_element3i_indexbuffer = R_Mesh_CreateMeshBuffer(loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles * sizeof(int[3]), loadmodel->name, true, false, false, false);
 
        // only build a vbo if one has not already been created (this is important for brush models which load specially)
        // vertex buffer is several arrays and we put them in the same buffer
@@ -2938,9 +3114,10 @@ void Mod_BuildVBOs(void)
        // other hand animated models don't use a lot of vertices anyway...
        if (!loadmodel->surfmesh.vbo_vertexbuffer && !vid.useinterleavedarrays)
        {
-               size_t size;
+               int size;
                unsigned char *mem;
                size = 0;
+               loadmodel->surfmesh.vbooffset_vertexmesh         = size;if (loadmodel->surfmesh.data_vertexmesh        ) size += loadmodel->surfmesh.num_vertices * sizeof(r_vertexmesh_t);
                loadmodel->surfmesh.vbooffset_vertex3f           = size;if (loadmodel->surfmesh.data_vertex3f          ) size += loadmodel->surfmesh.num_vertices * sizeof(float[3]);
                loadmodel->surfmesh.vbooffset_svector3f          = size;if (loadmodel->surfmesh.data_svector3f         ) size += loadmodel->surfmesh.num_vertices * sizeof(float[3]);
                loadmodel->surfmesh.vbooffset_tvector3f          = size;if (loadmodel->surfmesh.data_tvector3f         ) size += loadmodel->surfmesh.num_vertices * sizeof(float[3]);
@@ -2948,7 +3125,10 @@ void Mod_BuildVBOs(void)
                loadmodel->surfmesh.vbooffset_texcoordtexture2f  = size;if (loadmodel->surfmesh.data_texcoordtexture2f ) size += loadmodel->surfmesh.num_vertices * sizeof(float[2]);
                loadmodel->surfmesh.vbooffset_texcoordlightmap2f = size;if (loadmodel->surfmesh.data_texcoordlightmap2f) size += loadmodel->surfmesh.num_vertices * sizeof(float[2]);
                loadmodel->surfmesh.vbooffset_lightmapcolor4f    = size;if (loadmodel->surfmesh.data_lightmapcolor4f   ) size += loadmodel->surfmesh.num_vertices * sizeof(float[4]);
+               loadmodel->surfmesh.vbooffset_skeletalindex4ub   = size;if (loadmodel->surfmesh.data_skeletalindex4ub  ) size += loadmodel->surfmesh.num_vertices * sizeof(unsigned char[4]);
+               loadmodel->surfmesh.vbooffset_skeletalweight4ub  = size;if (loadmodel->surfmesh.data_skeletalweight4ub ) size += loadmodel->surfmesh.num_vertices * sizeof(unsigned char[4]);
                mem = (unsigned char *)Mem_Alloc(tempmempool, size);
+               if (loadmodel->surfmesh.data_vertexmesh        ) memcpy(mem + loadmodel->surfmesh.vbooffset_vertexmesh        , loadmodel->surfmesh.data_vertexmesh        , loadmodel->surfmesh.num_vertices * sizeof(r_vertexmesh_t));
                if (loadmodel->surfmesh.data_vertex3f          ) memcpy(mem + loadmodel->surfmesh.vbooffset_vertex3f          , loadmodel->surfmesh.data_vertex3f          , loadmodel->surfmesh.num_vertices * sizeof(float[3]));
                if (loadmodel->surfmesh.data_svector3f         ) memcpy(mem + loadmodel->surfmesh.vbooffset_svector3f         , loadmodel->surfmesh.data_svector3f         , loadmodel->surfmesh.num_vertices * sizeof(float[3]));
                if (loadmodel->surfmesh.data_tvector3f         ) memcpy(mem + loadmodel->surfmesh.vbooffset_tvector3f         , loadmodel->surfmesh.data_tvector3f         , loadmodel->surfmesh.num_vertices * sizeof(float[3]));
@@ -2956,11 +3136,14 @@ void Mod_BuildVBOs(void)
                if (loadmodel->surfmesh.data_texcoordtexture2f ) memcpy(mem + loadmodel->surfmesh.vbooffset_texcoordtexture2f , loadmodel->surfmesh.data_texcoordtexture2f , loadmodel->surfmesh.num_vertices * sizeof(float[2]));
                if (loadmodel->surfmesh.data_texcoordlightmap2f) memcpy(mem + loadmodel->surfmesh.vbooffset_texcoordlightmap2f, loadmodel->surfmesh.data_texcoordlightmap2f, loadmodel->surfmesh.num_vertices * sizeof(float[2]));
                if (loadmodel->surfmesh.data_lightmapcolor4f   ) memcpy(mem + loadmodel->surfmesh.vbooffset_lightmapcolor4f   , loadmodel->surfmesh.data_lightmapcolor4f   , loadmodel->surfmesh.num_vertices * sizeof(float[4]));
-               loadmodel->surfmesh.vbo_vertexbuffer = R_Mesh_CreateMeshBuffer(mem, size, loadmodel->name, false, false, false);
+               if (loadmodel->surfmesh.data_skeletalindex4ub  ) memcpy(mem + loadmodel->surfmesh.vbooffset_skeletalindex4ub  , loadmodel->surfmesh.data_skeletalindex4ub  , loadmodel->surfmesh.num_vertices * sizeof(unsigned char[4]));
+               if (loadmodel->surfmesh.data_skeletalweight4ub ) memcpy(mem + loadmodel->surfmesh.vbooffset_skeletalweight4ub , loadmodel->surfmesh.data_skeletalweight4ub , loadmodel->surfmesh.num_vertices * sizeof(unsigned char[4]));
+               loadmodel->surfmesh.vbo_vertexbuffer = R_Mesh_CreateMeshBuffer(mem, size, loadmodel->name, false, false, false, false);
                Mem_Free(mem);
        }
 }
 
+extern cvar_t mod_obj_orientation;
 static void Mod_Decompile_OBJ(dp_model_t *model, const char *filename, const char *mtlfilename, const char *originalfilename)
 {
        int submodelindex, vertexindex, surfaceindex, triangleindex, textureindex, countvertices = 0, countsurfaces = 0, countfaces = 0, counttextures = 0;
@@ -3028,7 +3211,10 @@ static void Mod_Decompile_OBJ(dp_model_t *model, const char *filename, const cha
                        memcpy(outbuffer, oldbuffer, outbufferpos);
                        Z_Free(oldbuffer);
                }
-               l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "v %f %f %f\nvn %f %f %f\nvt %f %f\n", v[0], v[2], v[1], vn[0], vn[2], vn[1], vt[0], 1-vt[1]);
+               if(mod_obj_orientation.integer)
+                       l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "v %f %f %f\nvn %f %f %f\nvt %f %f\n", v[0], v[2], v[1], vn[0], vn[2], vn[1], vt[0], 1-vt[1]);
+               else
+                       l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "v %f %f %f\nvn %f %f %f\nvt %f %f\n", v[0], v[1], v[2], vn[0], vn[1], vn[2], vt[0], 1-vt[1]);
                if (l > 0)
                        outbufferpos += l;
        }
@@ -3058,7 +3244,10 @@ static void Mod_Decompile_OBJ(dp_model_t *model, const char *filename, const cha
                                a = e[0]+1;
                                b = e[1]+1;
                                c = e[2]+1;
-                               l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "f %i/%i/%i %i/%i/%i %i/%i/%i\n", a,a,a,b,b,b,c,c,c);
+                               if(mod_obj_orientation.integer)
+                                       l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "f %i/%i/%i %i/%i/%i %i/%i/%i\n", a,a,a,b,b,b,c,c,c);
+                               else
+                                       l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "f %i/%i/%i %i/%i/%i %i/%i/%i\n", a,a,a,c,c,c,b,b,b);
                                if (l > 0)
                                        outbufferpos += l;
                        }
@@ -3133,7 +3322,7 @@ static void Mod_Decompile_SMD(dp_model_t *model, const char *filename, int first
 
                        // strangely the smd angles are for a transposed matrix, so we
                        // have to generate a transposed matrix, then convert that...
-                       Matrix4x4_FromBonePose6s(&posematrix, model->num_posescale, model->data_poses6s + 6*(model->num_bones * poseindex + transformindex));
+                       Matrix4x4_FromBonePose7s(&posematrix, model->num_posescale, model->data_poses7s + 7*(model->num_bones * poseindex + transformindex));
                        Matrix4x4_ToArray12FloatGL(&posematrix, mtest[0]);
                        AnglesFromVectors(angles, mtest[0], mtest[2], false);
                        if (angles[0] >= 180) angles[0] -= 360;
@@ -3256,6 +3445,7 @@ static void Mod_Decompile_f(void)
        int zymtextsize = 0;
        int dpmtextsize = 0;
        int framegroupstextsize = 0;
+       char vabuf[1024];
 
        if (Cmd_Argc() != 2)
        {
@@ -3267,6 +3457,11 @@ static void Mod_Decompile_f(void)
        FS_StripExtension(inname, basename, sizeof(basename));
 
        mod = Mod_ForName(inname, false, true, inname[0] == '*' ? cl.model_name[1] : NULL);
+       if (!mod)
+       {
+               Con_Print("No such model\n");
+               return;
+       }
        if (mod->brush.submodel)
        {
                // if we're decompiling a submodel, be sure to give it a proper name based on its parent
@@ -3274,11 +3469,6 @@ static void Mod_Decompile_f(void)
                dpsnprintf(basename, sizeof(basename), "%s/%s", outname, mod->name);
                outname[0] = 0;
        }
-       if (!mod)
-       {
-               Con_Print("No such model\n");
-               return;
-       }
        if (!mod->surfmesh.num_triangles)
        {
                Con_Print("Empty model (or sprite)\n");
@@ -3316,7 +3506,7 @@ static void Mod_Decompile_f(void)
                        {
                                // individual frame
                                // check for additional frames with same name
-                               for (l = 0, k = strlen(animname);animname[l];l++)
+                               for (l = 0, k = (int)strlen(animname);animname[l];l++)
                                        if(animname[l] < '0' || animname[l] > '9')
                                                k = l + 1;
                                if(k > 0 && animname[k-1] == '_')
@@ -3326,7 +3516,7 @@ static void Mod_Decompile_f(void)
                                for (j = i + 1;j < mod->numframes;j++)
                                {
                                        strlcpy(animname2, mod->animscenes[j].name, sizeof(animname2));
-                                       for (l = 0, k = strlen(animname2);animname2[l];l++)
+                                       for (l = 0, k = (int)strlen(animname2);animname2[l];l++)
                                                if(animname2[l] < '0' || animname2[l] > '9')
                                                        k = l + 1;
                                        if(k > 0 && animname[k-1] == '_')
@@ -3362,22 +3552,22 @@ static void Mod_Decompile_f(void)
                        }
                }
                if (zymtextsize)
-                       FS_WriteFile(va("%s_decompiled/out_zym.txt", basename), zymtextbuffer, (fs_offset_t)zymtextsize);
+                       FS_WriteFile(va(vabuf, sizeof(vabuf), "%s_decompiled/out_zym.txt", basename), zymtextbuffer, (fs_offset_t)zymtextsize);
                if (dpmtextsize)
-                       FS_WriteFile(va("%s_decompiled/out_dpm.txt", basename), dpmtextbuffer, (fs_offset_t)dpmtextsize);
+                       FS_WriteFile(va(vabuf, sizeof(vabuf), "%s_decompiled/out_dpm.txt", basename), dpmtextbuffer, (fs_offset_t)dpmtextsize);
                if (framegroupstextsize)
-                       FS_WriteFile(va("%s_decompiled.framegroups", basename), framegroupstextbuffer, (fs_offset_t)framegroupstextsize);
+                       FS_WriteFile(va(vabuf, sizeof(vabuf), "%s_decompiled.framegroups", basename), framegroupstextbuffer, (fs_offset_t)framegroupstextsize);
        }
 }
 
-void Mod_AllocLightmap_Init(mod_alloclightmap_state_t *state, int width, int height)
+void Mod_AllocLightmap_Init(mod_alloclightmap_state_t *state, mempool_t *mempool, int width, int height)
 {
        int y;
        memset(state, 0, sizeof(*state));
        state->width = width;
        state->height = height;
        state->currentY = 0;
-       state->rows = (mod_alloclightmap_row_t *)Mem_Alloc(loadmodel->mempool, state->height * sizeof(*state->rows));
+       state->rows = (mod_alloclightmap_row_t *)Mem_Alloc(mempool, state->height * sizeof(*state->rows));
        for (y = 0;y < state->height;y++)
        {
                state->rows[y].currentX = 0;
@@ -3500,7 +3690,6 @@ static float mod_generatelightmaps_offsets[3][MAX_LIGHTMAPSAMPLES][3];
 static int mod_generatelightmaps_numlights;
 static lightmaplight_t *mod_generatelightmaps_lightinfo;
 
-extern int R_Shadow_GetRTLightInfo(unsigned int lightindex, float *origin, float *radius, float *color);
 extern cvar_t r_shadow_lightattenuationdividebias;
 extern cvar_t r_shadow_lightattenuationlinearscale;
 
@@ -3543,7 +3732,7 @@ static void Mod_GenerateLightmaps_LightPoint(dp_model_t *model, const vec3_t pos
                        continue;
                if (model && model->TraceLine)
                {
-                       model->TraceLine(model, NULL, NULL, &trace, pos, lightorigin, SUPERCONTENTS_VISBLOCKERMASK);
+                       model->TraceLine(model, NULL, NULL, &trace, pos, lightorigin, SUPERCONTENTS_VISBLOCKERMASK, SUPERCONTENTS_SKY);
                        if (trace.fraction < 1)
                                continue;
                }
@@ -3745,7 +3934,7 @@ static void Mod_GenerateLightmaps_SamplePoint(const float *pos, const float *nor
                                if (!normal)
                                {
                                        // for light grid we'd better check visibility of the offset point
-                                       cl.worldmodel->TraceLine(cl.worldmodel, NULL, NULL, &trace, pos, offsetpos, SUPERCONTENTS_VISBLOCKERMASK);
+                                       cl.worldmodel->TraceLine(cl.worldmodel, NULL, NULL, &trace, pos, offsetpos, SUPERCONTENTS_VISBLOCKERMASK, SUPERCONTENTS_SKY);
                                        if (trace.fraction < 1)
                                                VectorLerp(pos, trace.fraction, offsetpos, offsetpos);
                                }
@@ -3923,15 +4112,6 @@ static void Mod_GenerateLightmaps_UnweldTriangles(dp_model_t *model)
        if (model->surfmesh.num_vertices > 65536)
                model->surfmesh.data_element3s = NULL;
 
-       if (model->surfmesh.vertexmesh)
-               Mem_Free(model->surfmesh.vertexmesh);
-       model->surfmesh.vertexmesh = NULL;
-       if (model->surfmesh.vertex3fbuffer)
-               R_Mesh_DestroyMeshBuffer(model->surfmesh.vertex3fbuffer);
-       model->surfmesh.vertex3fbuffer = NULL;
-       if (model->surfmesh.vertexmeshbuffer)
-               R_Mesh_DestroyMeshBuffer(model->surfmesh.vertexmeshbuffer);
-       model->surfmesh.vertexmeshbuffer = NULL;
        if (model->surfmesh.data_element3i_indexbuffer)
                R_Mesh_DestroyMeshBuffer(model->surfmesh.data_element3i_indexbuffer);
        model->surfmesh.data_element3i_indexbuffer = NULL;
@@ -4081,6 +4261,7 @@ static void Mod_GenerateLightmaps_CreateLightmaps(dp_model_t *model)
        unsigned char *lightmappixels;
        unsigned char *deluxemappixels;
        mod_alloclightmap_state_t lmstate;
+       char vabuf[1024];
 
        // generate lightmap projection information for all triangles
        if (model->texturepool == NULL)
@@ -4089,7 +4270,7 @@ static void Mod_GenerateLightmaps_CreateLightmaps(dp_model_t *model)
        lm_borderpixels = mod_generatelightmaps_borderpixels.integer;
        lm_texturesize = bound(lm_borderpixels*2+1, 64, (int)vid.maxtexturesize_2d);
        //lm_maxpixels = lm_texturesize-(lm_borderpixels*2+1);
-       Mod_AllocLightmap_Init(&lmstate, lm_texturesize, lm_texturesize);
+       Mod_AllocLightmap_Init(&lmstate, loadmodel->mempool, lm_texturesize, lm_texturesize);
        lightmapnumber = 0;
        for (surfaceindex = 0;surfaceindex < model->num_surfaces;surfaceindex++)
        {
@@ -4137,7 +4318,7 @@ static void Mod_GenerateLightmaps_CreateLightmaps(dp_model_t *model)
                                surfaceindex = -1;
                                lightmapnumber = 0;
                                Mod_AllocLightmap_Free(&lmstate);
-                               Mod_AllocLightmap_Init(&lmstate, lm_texturesize, lm_texturesize);
+                               Mod_AllocLightmap_Init(&lmstate, loadmodel->mempool, lm_texturesize, lm_texturesize);
                                break;
                        }
                        // if we have maxed out the lightmap size, and this triangle does
@@ -4263,8 +4444,8 @@ static void Mod_GenerateLightmaps_CreateLightmaps(dp_model_t *model)
 
        for (lightmapindex = 0;lightmapindex < model->brushq3.num_mergedlightmaps;lightmapindex++)
        {
-               model->brushq3.data_lightmaps[lightmapindex] = R_LoadTexture2D(model->texturepool, va("lightmap%i", lightmapindex), lm_texturesize, lm_texturesize, lightmappixels + lightmapindex * lm_texturesize * lm_texturesize * 4, TEXTYPE_BGRA, TEXF_FORCELINEAR, -1, NULL);
-               model->brushq3.data_deluxemaps[lightmapindex] = R_LoadTexture2D(model->texturepool, va("deluxemap%i", lightmapindex), lm_texturesize, lm_texturesize, deluxemappixels + lightmapindex * lm_texturesize * lm_texturesize * 4, TEXTYPE_BGRA, TEXF_FORCELINEAR, -1, NULL);
+               model->brushq3.data_lightmaps[lightmapindex] = R_LoadTexture2D(model->texturepool, va(vabuf, sizeof(vabuf), "lightmap%i", lightmapindex), lm_texturesize, lm_texturesize, lightmappixels + lightmapindex * lm_texturesize * lm_texturesize * 4, TEXTYPE_BGRA, TEXF_FORCELINEAR, -1, NULL);
+               model->brushq3.data_deluxemaps[lightmapindex] = R_LoadTexture2D(model->texturepool, va(vabuf, sizeof(vabuf), "deluxemap%i", lightmapindex), lm_texturesize, lm_texturesize, deluxemappixels + lightmapindex * lm_texturesize * lm_texturesize * 4, TEXTYPE_BGRA, TEXF_FORCELINEAR, -1, NULL);
        }
 
        if (lightmappixels)
@@ -4275,7 +4456,6 @@ static void Mod_GenerateLightmaps_CreateLightmaps(dp_model_t *model)
        for (surfaceindex = 0;surfaceindex < model->num_surfaces;surfaceindex++)
        {
                surface = model->data_surfaces + surfaceindex;
-               e = model->surfmesh.data_element3i + surface->num_firsttriangle*3;
                if (!surface->num_triangles)
                        continue;
                lightmapindex = mod_generatelightmaps_lightmaptriangles[surface->num_firsttriangle].lightmapindex;