]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - model_shared.c
physics: fix and refactor unsticking
[xonotic/darkplaces.git] / model_shared.c
index 3121250f1cafb1dab8347f65bb4dcdfc20e896b1..d6cea915396455ffce1b181e5ed6a0f99018af31 100644 (file)
@@ -27,19 +27,42 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include "r_shadow.h"
 #include "polygon.h"
 
-cvar_t r_mipskins = {CVAR_CLIENT | CVAR_SAVE, "r_mipskins", "0", "mipmaps model skins so they render faster in the distance and do not display noise artifacts, can cause discoloration of skins if they contain undesirable border colors"};
-cvar_t r_mipnormalmaps = {CVAR_CLIENT | CVAR_SAVE, "r_mipnormalmaps", "1", "mipmaps normalmaps (turning it off looks sharper but may have aliasing)"};
-cvar_t mod_generatelightmaps_unitspersample = {CVAR_CLIENT | CVAR_SAVE, "mod_generatelightmaps_unitspersample", "8", "lightmap resolution"};
-cvar_t mod_generatelightmaps_borderpixels = {CVAR_CLIENT | CVAR_SAVE, "mod_generatelightmaps_borderpixels", "2", "extra space around polygons to prevent sampling artifacts"};
-cvar_t mod_generatelightmaps_texturesize = {CVAR_CLIENT | CVAR_SAVE, "mod_generatelightmaps_texturesize", "1024", "size of lightmap textures"};
-cvar_t mod_generatelightmaps_lightmapsamples = {CVAR_CLIENT | CVAR_SAVE, "mod_generatelightmaps_lightmapsamples", "16", "number of shadow tests done per lightmap pixel"};
-cvar_t mod_generatelightmaps_vertexsamples = {CVAR_CLIENT | CVAR_SAVE, "mod_generatelightmaps_vertexsamples", "16", "number of shadow tests done per vertex"};
-cvar_t mod_generatelightmaps_gridsamples = {CVAR_CLIENT | CVAR_SAVE, "mod_generatelightmaps_gridsamples", "64", "number of shadow tests done per lightgrid cell"};
-cvar_t mod_generatelightmaps_lightmapradius = {CVAR_CLIENT | CVAR_SAVE, "mod_generatelightmaps_lightmapradius", "16", "sampling area around each lightmap pixel"};
-cvar_t mod_generatelightmaps_vertexradius = {CVAR_CLIENT | CVAR_SAVE, "mod_generatelightmaps_vertexradius", "16", "sampling area around each vertex"};
-cvar_t mod_generatelightmaps_gridradius = {CVAR_CLIENT | CVAR_SAVE, "mod_generatelightmaps_gridradius", "64", "sampling area around each lightgrid cell center"};
-
-dp_model_t *loadmodel;
+cvar_t r_mipskins = {CF_CLIENT | CF_ARCHIVE, "r_mipskins", "0", "mipmaps model skins so they render faster in the distance and do not display noise artifacts, can cause discoloration of skins if they contain undesirable border colors"};
+cvar_t r_mipnormalmaps = {CF_CLIENT | CF_ARCHIVE, "r_mipnormalmaps", "1", "mipmaps normalmaps (turning it off looks sharper but may have aliasing)"};
+cvar_t mod_generatelightmaps_unitspersample = {CF_CLIENT | CF_ARCHIVE, "mod_generatelightmaps_unitspersample", "8", "lightmap resolution"};
+cvar_t mod_generatelightmaps_borderpixels = {CF_CLIENT | CF_ARCHIVE, "mod_generatelightmaps_borderpixels", "2", "extra space around polygons to prevent sampling artifacts"};
+cvar_t mod_generatelightmaps_texturesize = {CF_CLIENT | CF_ARCHIVE, "mod_generatelightmaps_texturesize", "1024", "size of lightmap textures"};
+cvar_t mod_generatelightmaps_lightmapsamples = {CF_CLIENT | CF_ARCHIVE, "mod_generatelightmaps_lightmapsamples", "16", "number of shadow tests done per lightmap pixel"};
+cvar_t mod_generatelightmaps_vertexsamples = {CF_CLIENT | CF_ARCHIVE, "mod_generatelightmaps_vertexsamples", "16", "number of shadow tests done per vertex"};
+cvar_t mod_generatelightmaps_gridsamples = {CF_CLIENT | CF_ARCHIVE, "mod_generatelightmaps_gridsamples", "64", "number of shadow tests done per lightgrid cell"};
+cvar_t mod_generatelightmaps_lightmapradius = {CF_CLIENT | CF_ARCHIVE, "mod_generatelightmaps_lightmapradius", "16", "sampling area around each lightmap pixel"};
+cvar_t mod_generatelightmaps_vertexradius = {CF_CLIENT | CF_ARCHIVE, "mod_generatelightmaps_vertexradius", "16", "sampling area around each vertex"};
+cvar_t mod_generatelightmaps_gridradius = {CF_CLIENT | CF_ARCHIVE, "mod_generatelightmaps_gridradius", "64", "sampling area around each lightgrid cell center"};
+
+model_t *loadmodel;
+
+// Supported model formats
+static modloader_t loader[] =
+{
+       {"obj", NULL, 0, Mod_OBJ_Load},
+       {NULL, "IDPO", 4, Mod_IDP0_Load},
+       {NULL, "IDP2", 4, Mod_IDP2_Load},
+       {NULL, "IDP3", 4, Mod_IDP3_Load},
+       {NULL, "IDSP", 4, Mod_IDSP_Load},
+       {NULL, "IDS2", 4, Mod_IDS2_Load},
+       {NULL, "\035", 1, Mod_Q1BSP_Load},
+       {NULL, "\036", 1, Mod_HLBSP_Load},
+       {NULL, "BSP2", 4, Mod_BSP2_Load},
+       {NULL, "2PSB", 4, Mod_2PSB_Load},
+       {NULL, "IBSP", 4, Mod_IBSP_Load},
+       {NULL, "VBSP", 4, Mod_VBSP_Load},
+       {NULL, "ZYMOTICMODEL", 13, Mod_ZYMOTICMODEL_Load},
+       {NULL, "DARKPLACESMODEL", 16, Mod_DARKPLACESMODEL_Load},
+       {NULL, "PSKMODEL", 9, Mod_PSKMODEL_Load},
+       {NULL, "INTERQUAKEMODEL", 16, Mod_INTERQUAKEMODEL_Load},
+       {"map", NULL, 0, Mod_MAP_Load},
+       {NULL, NULL, 0, NULL}
+};
 
 static mempool_t *mod_mempool;
 static memexpandablearray_t models;
@@ -47,7 +70,7 @@ static memexpandablearray_t models;
 static mempool_t* q3shaders_mem;
 typedef struct q3shader_hash_entry_s
 {
-  q3shaderinfo_t shader;
+  shader_t shader;
   struct q3shader_hash_entry_s* chain;
 } q3shader_hash_entry_t;
 #define Q3SHADER_HASH_SIZE  1021
@@ -63,16 +86,16 @@ static void mod_start(void)
 {
        int i, count;
        int nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
-       dp_model_t *mod;
+       model_t *mod;
 
        SCR_PushLoadingScreen("Loading models", 1.0);
        count = 0;
        for (i = 0;i < nummodels;i++)
-               if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && mod->name[0] != '*')
+               if ((mod = (model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && mod->name[0] != '*')
                        if (mod->used)
                                ++count;
        for (i = 0;i < nummodels;i++)
-               if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && mod->name[0] != '*')
+               if ((mod = (model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && mod->name[0] != '*')
                        if (mod->used)
                        {
                                SCR_PushLoadingScreen(mod->name, 1.0 / count);
@@ -86,10 +109,10 @@ static void mod_shutdown(void)
 {
        int i;
        int nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
-       dp_model_t *mod;
+       model_t *mod;
 
        for (i = 0;i < nummodels;i++)
-               if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && (mod->loaded || mod->mempool))
+               if ((mod = (model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && (mod->loaded || mod->mempool))
                        Mod_UnloadModel(mod);
 
        Mod_FreeQ3Shaders();
@@ -101,11 +124,11 @@ static void mod_newmap(void)
        msurface_t *surface;
        int i, j, k, l, surfacenum, ssize, tsize;
        int nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
-       dp_model_t *mod;
+       model_t *mod;
 
        for (i = 0;i < nummodels;i++)
        {
-               if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->mempool)
+               if ((mod = (model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->mempool)
                {
                        for (j = 0;j < mod->num_textures && mod->data_textures;j++)
                        {
@@ -127,7 +150,7 @@ static void mod_newmap(void)
 
        for (i = 0;i < nummodels;i++)
        {
-               if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->mempool && mod->data_surfaces)
+               if ((mod = (model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->mempool && mod->data_surfaces)
                {
                        for (surfacenum = 0, surface = mod->data_surfaces;surfacenum < mod->num_surfaces;surfacenum++, surface++)
                        {
@@ -155,7 +178,7 @@ static void Mod_GenerateLightmaps_f(cmd_state_t *cmd);
 void Mod_Init (void)
 {
        mod_mempool = Mem_AllocPool("modelinfo", 0, NULL);
-       Mem_ExpandableArray_NewArray(&models, mod_mempool, sizeof(dp_model_t), 16);
+       Mem_ExpandableArray_NewArray(&models, mod_mempool, sizeof(model_t), 16);
 
        Mod_BrushInit();
        Mod_AliasInit();
@@ -174,10 +197,10 @@ void Mod_Init (void)
        Cvar_RegisterVariable(&mod_generatelightmaps_vertexradius);
        Cvar_RegisterVariable(&mod_generatelightmaps_gridradius);
 
-       Cmd_AddCommand(CMD_CLIENT, "modellist", Mod_Print_f, "prints a list of loaded models");
-       Cmd_AddCommand(CMD_CLIENT, "modelprecache", Mod_Precache_f, "load a model");
-       Cmd_AddCommand(CMD_CLIENT, "modeldecompile", Mod_Decompile_f, "exports a model in several formats for editing purposes");
-       Cmd_AddCommand(CMD_CLIENT, "mod_generatelightmaps", Mod_GenerateLightmaps_f, "rebuilds lighting on current worldmodel");
+       Cmd_AddCommand(CF_CLIENT, "modellist", Mod_Print_f, "prints a list of loaded models");
+       Cmd_AddCommand(CF_CLIENT, "modelprecache", Mod_Precache_f, "load a model");
+       Cmd_AddCommand(CF_CLIENT, "modeldecompile", Mod_Decompile_f, "exports a model in several formats for editing purposes");
+       Cmd_AddCommand(CF_CLIENT, "mod_generatelightmaps", Mod_GenerateLightmaps_f, "rebuilds lighting on current worldmodel");
 }
 
 void Mod_RenderInit(void)
@@ -185,16 +208,16 @@ void Mod_RenderInit(void)
        R_RegisterModule("Models", mod_start, mod_shutdown, mod_newmap, NULL, NULL);
 }
 
-void Mod_UnloadModel (dp_model_t *mod)
+void Mod_UnloadModel (model_t *mod)
 {
        char name[MAX_QPATH];
-       qboolean used;
-       dp_model_t *parentmodel;
+       qbool used;
+       model_t *parentmodel;
 
        if (developer_loading.integer)
                Con_Printf("unloading model %s\n", mod->name);
 
-       strlcpy(name, mod->name, sizeof(name));
+       dp_strlcpy(name, mod->name, sizeof(name));
        parentmodel = mod->brush.parentmodel;
        used = mod->used;
        if (mod->mempool)
@@ -221,9 +244,9 @@ void Mod_UnloadModel (dp_model_t *mod)
        R_FreeTexturePool(&mod->texturepool);
        Mem_FreePool(&mod->mempool);
        // clear the struct to make it available
-       memset(mod, 0, sizeof(dp_model_t));
+       memset(mod, 0, sizeof(model_t));
        // restore the fields we want to preserve
-       strlcpy(mod->name, name, sizeof(mod->name));
+       dp_strlcpy(mod->name, name, sizeof(mod->name));
        mod->brush.parentmodel = parentmodel;
        mod->used = used;
        mod->loaded = false;
@@ -235,7 +258,7 @@ static void R_Model_Null_Draw(entity_render_t *ent)
 }
 
 
-typedef void (*mod_framegroupify_parsegroups_t) (unsigned int i, int start, int len, float fps, qboolean loop, const char *name, void *pass);
+typedef void (*mod_framegroupify_parsegroups_t) (unsigned int i, int start, int len, float fps, qbool loop, const char *name, void *pass);
 
 static int Mod_FrameGroupify_ParseGroups(const char *buf, mod_framegroupify_parsegroups_t cb, void *pass)
 {
@@ -243,7 +266,7 @@ static int Mod_FrameGroupify_ParseGroups(const char *buf, mod_framegroupify_pars
        int start, len;
        float fps;
        unsigned int i;
-       qboolean loop;
+       qbool loop;
        char name[64];
 
        bufptr = buf;
@@ -292,7 +315,7 @@ static int Mod_FrameGroupify_ParseGroups(const char *buf, mod_framegroupify_pars
                name[0] = 0;
                if (bufptr && strcmp(com_token, "\n"))
                {
-                       strlcpy(name, com_token, sizeof(name));
+                       dp_strlcpy(name, com_token, sizeof(name));
                        COM_ParseToken_Simple(&bufptr, true, false, true);
                }
 
@@ -310,12 +333,12 @@ static int Mod_FrameGroupify_ParseGroups(const char *buf, mod_framegroupify_pars
        return i;
 }
 
-static void Mod_FrameGroupify_ParseGroups_Store (unsigned int i, int start, int len, float fps, qboolean loop, const char *name, void *pass)
+static void Mod_FrameGroupify_ParseGroups_Store (unsigned int i, int start, int len, float fps, qbool loop, const char *name, void *pass)
 {
-       dp_model_t *mod = (dp_model_t *) pass;
+       model_t *mod = (model_t *) pass;
        animscene_t *anim = &mod->animscenes[i];
        if(name)
-               strlcpy(anim->name, name, sizeof(anim[i].name));
+               dp_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);
@@ -325,7 +348,7 @@ static void Mod_FrameGroupify_ParseGroups_Store (unsigned int i, int start, int
        //Con_Printf("frame group %d is %d %d %f %d\n", i, start, len, fps, loop);
 }
 
-static void Mod_FrameGroupify(dp_model_t *mod, const char *buf)
+static void Mod_FrameGroupify(model_t *mod, const char *buf)
 {
        unsigned int cnt;
 
@@ -346,7 +369,7 @@ static void Mod_FrameGroupify(dp_model_t *mod, const char *buf)
        Mod_FrameGroupify_ParseGroups(buf, Mod_FrameGroupify_ParseGroups_Store, mod);
 }
 
-static void Mod_FindPotentialDeforms(dp_model_t *mod)
+static void Mod_FindPotentialDeforms(model_t *mod)
 {
        int i, j;
        texture_t *texture;
@@ -380,9 +403,8 @@ Mod_LoadModel
 Loads a model
 ==================
 */
-dp_model_t *Mod_LoadModel(dp_model_t *mod, qboolean crash, qboolean checkdisk)
+model_t *Mod_LoadModel(model_t *mod, qbool crash, qbool checkdisk)
 {
-       int num;
        unsigned int crc;
        void *buf;
        fs_offset_t filesize = 0;
@@ -490,46 +512,46 @@ dp_model_t *Mod_LoadModel(dp_model_t *mod, qboolean crash, qboolean checkdisk)
 
        if (buf)
        {
+               int i;
+               const char *ext = FS_FileExtension(mod->name);  
                char *bufend = (char *)buf + filesize;
-
                // all models use memory, so allocate a memory pool
                mod->mempool = Mem_AllocPool(mod->name, 0, NULL);
 
-               num = LittleLong(*((int *)buf));
-               // call the apropriate loader
+               // We need to have a reference to the base model in case we're parsing submodels
                loadmodel = mod;
-               if (!strcasecmp(FS_FileExtension(mod->name), "obj")) Mod_OBJ_Load(mod, buf, bufend);
-               else if (!memcmp(buf, "IDPO", 4)) Mod_IDP0_Load(mod, buf, bufend);
-               else if (!memcmp(buf, "IDP2", 4)) Mod_IDP2_Load(mod, buf, bufend);
-               else if (!memcmp(buf, "IDP3", 4)) Mod_IDP3_Load(mod, buf, bufend);
-               else if (!memcmp(buf, "IDSP", 4)) Mod_IDSP_Load(mod, buf, bufend);
-               else if (!memcmp(buf, "IDS2", 4)) Mod_IDS2_Load(mod, buf, bufend);
-               else if (!memcmp(buf, "IBSP", 4)) Mod_IBSP_Load(mod, buf, bufend);
-               else if (!memcmp(buf, "ZYMOTICMODEL", 12)) Mod_ZYMOTICMODEL_Load(mod, buf, bufend);
-               else if (!memcmp(buf, "DARKPLACESMODEL", 16)) Mod_DARKPLACESMODEL_Load(mod, buf, bufend);
-               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 || !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(vabuf, sizeof(vabuf), "%s.framegroups", mod->name), tempmempool, false, &filesize);
-               if(buf)
+
+               // Call the appropriate loader. Try matching magic bytes.
+               for (i = 0; loader[i].Load; i++)
                {
-                       Mod_FrameGroupify(mod, (const char *)buf);
-                       Mem_Free(buf);
-               }
+                       // Headerless formats can just load based on extension. Otherwise match the magic string.
+                       if((loader[i].extension && !strcasecmp(ext, loader[i].extension) && !loader[i].header) ||
+                          (loader[i].header && !memcmp(buf, loader[i].header, loader[i].headersize)))
+                       {
+                               // Matched. Load it.
+                               loader[i].Load(mod, buf, bufend);
+                               Mem_Free(buf);
+
+                               Mod_FindPotentialDeforms(mod);
+
+                               buf = FS_LoadFile(va(vabuf, sizeof(vabuf), "%s.framegroups", mod->name), tempmempool, false, &filesize);
+                               if(buf)
+                               {
+                                       Mod_FrameGroupify(mod, (const char *)buf);
+                                       Mem_Free(buf);
+                               }
 
-               Mod_BuildVBOs();
+                               Mod_SetDrawSkyAndWater(mod);
+                               Mod_BuildVBOs();
+                               break;
+                       }
+               }
+               if(!loader[i].Load)
+                       Con_Printf(CON_ERROR "Mod_LoadModel: model \"%s\" is of unknown/unsupported type\n", mod->name);
        }
        else if (crash)
-       {
                // LadyHavoc: Sys_Error was *ANNOYING*
-               Con_Printf ("Mod_LoadModel: %s not found\n", mod->name);
-       }
+               Con_Printf (CON_ERROR "Mod_LoadModel: %s not found\n", mod->name);
 
        // no fatal errors occurred, so this model is ready to use.
        mod->loaded = true;
@@ -543,9 +565,9 @@ void Mod_ClearUsed(void)
 {
        int i;
        int nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
-       dp_model_t *mod;
+       model_t *mod;
        for (i = 0;i < nummodels;i++)
-               if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0])
+               if ((mod = (model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0])
                        mod->used = false;
 }
 
@@ -553,10 +575,10 @@ void Mod_PurgeUnused(void)
 {
        int i;
        int nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
-       dp_model_t *mod;
+       model_t *mod;
        for (i = 0;i < nummodels;i++)
        {
-               if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && !mod->used)
+               if ((mod = (model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && !mod->used)
                {
                        Mod_UnloadModel(mod);
                        Mem_ExpandableArray_FreeRecord(&models, mod);
@@ -570,11 +592,11 @@ Mod_FindName
 
 ==================
 */
-dp_model_t *Mod_FindName(const char *name, const char *parentname)
+model_t *Mod_FindName(const char *name, const char *parentname)
 {
        int i;
        int nummodels;
-       dp_model_t *mod;
+       model_t *mod;
 
        if (!parentname)
                parentname = "";
@@ -587,7 +609,7 @@ dp_model_t *Mod_FindName(const char *name, const char *parentname)
        // search the currently loaded models
        for (i = 0;i < nummodels;i++)
        {
-               if ((mod = (dp_model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && !strcmp(mod->name, name) && ((!mod->brush.parentmodel && !parentname[0]) || (mod->brush.parentmodel && parentname[0] && !strcmp(mod->brush.parentmodel->name, parentname))))
+               if ((mod = (model_t*) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && !strcmp(mod->name, name) && ((!mod->brush.parentmodel && !parentname[0]) || (mod->brush.parentmodel && parentname[0] && !strcmp(mod->brush.parentmodel->name, parentname))))
                {
                        mod->used = true;
                        return mod;
@@ -595,8 +617,8 @@ dp_model_t *Mod_FindName(const char *name, const char *parentname)
        }
 
        // no match found, create a new one
-       mod = (dp_model_t *) Mem_ExpandableArray_AllocRecord(&models);
-       strlcpy(mod->name, name, sizeof(mod->name));
+       mod = (model_t *) Mem_ExpandableArray_AllocRecord(&models);
+       dp_strlcpy(mod->name, name, sizeof(mod->name));
        if (parentname[0])
                mod->brush.parentmodel = Mod_FindName(parentname, NULL);
        else
@@ -606,6 +628,8 @@ dp_model_t *Mod_FindName(const char *name, const char *parentname)
        return mod;
 }
 
+extern qbool vid_opened;
+
 /*
 ==================
 Mod_ForName
@@ -613,9 +637,14 @@ Mod_ForName
 Loads in a model for the given name
 ==================
 */
-dp_model_t *Mod_ForName(const char *name, qboolean crash, qboolean checkdisk, const char *parentname)
+model_t *Mod_ForName(const char *name, qbool crash, qbool checkdisk, const char *parentname)
 {
-       dp_model_t *model;
+       model_t *model;
+
+       // FIXME: So we don't crash if a server is started early.
+       if(!vid_opened)
+               CL_StartVideo();
+
        model = Mod_FindName(name, parentname);
        if (!model->loaded || checkdisk)
                Mod_LoadModel(model, crash, checkdisk);
@@ -633,15 +662,15 @@ void Mod_Reload(void)
 {
        int i, count;
        int nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
-       dp_model_t *mod;
+       model_t *mod;
 
        SCR_PushLoadingScreen("Reloading models", 1.0);
        count = 0;
        for (i = 0;i < nummodels;i++)
-               if ((mod = (dp_model_t *) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && mod->name[0] != '*' && mod->used)
+               if ((mod = (model_t *) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && mod->name[0] != '*' && mod->used)
                        ++count;
        for (i = 0;i < nummodels;i++)
-               if ((mod = (dp_model_t *) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && mod->name[0] != '*' && mod->used)
+               if ((mod = (model_t *) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && mod->name[0] != '*' && mod->used)
                {
                        SCR_PushLoadingScreen(mod->name, 1.0 / count);
                        Mod_LoadModel(mod, true, true);
@@ -664,12 +693,12 @@ static void Mod_Print_f(cmd_state_t *cmd)
 {
        int i;
        int nummodels = (int)Mem_ExpandableArray_IndexRange(&models);
-       dp_model_t *mod;
+       model_t *mod;
 
        Con_Print("Loaded models:\n");
        for (i = 0;i < nummodels;i++)
        {
-               if ((mod = (dp_model_t *) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && mod->name[0] != '*')
+               if ((mod = (model_t *) Mem_ExpandableArray_RecordAtIndex(&models, i)) && mod->name[0] && mod->name[0] != '*')
                {
                        if (mod->brush.numsubmodels)
                                Con_Printf("%4iK %s (%i submodels)\n", mod->mempool ? (int)((mod->mempool->totalsize + 1023) / 1024) : 0, mod->name, mod->brush.numsubmodels);
@@ -706,7 +735,7 @@ int Mod_BuildVertexRemapTableFromElements(int numelements, const int *elements,
        return count;
 }
 
-qboolean Mod_ValidateElements(int *element3i, unsigned short *element3s, int numtriangles, int firstvertex, int numvertices, const char *filename, int fileline)
+qbool Mod_ValidateElements(int *element3i, unsigned short *element3s, int numtriangles, int firstvertex, int numvertices, const char *filename, int fileline)
 {
        int first = firstvertex, last = first + numvertices - 1, numelements = numtriangles * 3;
        int i;
@@ -774,7 +803,7 @@ qboolean Mod_ValidateElements(int *element3i, unsigned short *element3s, int num
 }
 
 // warning: this is an expensive function!
-void Mod_BuildNormals(int firstvertex, int numvertices, int numtriangles, const float *vertex3f, const int *elements, float *normal3f, qboolean areaweighting)
+void Mod_BuildNormals(int firstvertex, int numvertices, int numtriangles, const float *vertex3f, const int *elements, float *normal3f, qbool areaweighting)
 {
        int i, j;
        const int *element;
@@ -861,7 +890,7 @@ static void Mod_BuildBumpVectors(const float *v0, const float *v1, const float *
 #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)
+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, qbool areaweighting)
 {
        int i, tnum;
        float sdir[3], tdir[3], normal[3], *svec, *tvec;
@@ -941,7 +970,7 @@ void Mod_BuildTextureVectorsFromNormals(int firstvertex, int numvertices, int nu
        }
 }
 
-void Mod_AllocSurfMesh(mempool_t *mempool, int numvertices, int numtriangles, qboolean lightmapoffsets, qboolean vertexcolors)
+void Mod_AllocSurfMesh(mempool_t *mempool, int numvertices, int numtriangles, qbool lightmapoffsets, qbool vertexcolors)
 {
        unsigned char *data;
        data = (unsigned char *)Mem_Alloc(mempool, numvertices * (3 + 3 + 3 + 3 + 2 + 2 + (vertexcolors ? 4 : 0)) * sizeof(float) + numvertices * (lightmapoffsets ? 1 : 0) * sizeof(int) + numtriangles * sizeof(int[3]) + (numvertices <= 65536 ? numtriangles * sizeof(unsigned short[3]) : 0));
@@ -1015,6 +1044,12 @@ void Mod_ShadowMesh_AddMesh(shadowmesh_t *mesh, const float *vertex3f, int numtr
 
        for (i = 0;i < numtris;i++)
        {
+               if ((mesh->numtriangles * 3 + 2) * sizeof(int) + 1 >= ((memheader_t *)((unsigned char *)mesh->element3i - sizeof(memheader_t)))->size)
+               {
+                       // FIXME: we didn't allocate enough space for all the tris, see R_Mod_CompileShadowMap
+                       Con_Print(CON_WARN "Mod_ShadowMesh_AddMesh: insufficient memory allocated!\n");
+                       return;
+               }
                mesh->element3i[mesh->numtriangles * 3 + 0] = Mod_ShadowMesh_AddVertex(mesh, vertex3f + 3 * element3i[i * 3 + 0]);
                mesh->element3i[mesh->numtriangles * 3 + 1] = Mod_ShadowMesh_AddVertex(mesh, vertex3f + 3 * element3i[i * 3 + 1]);
                mesh->element3i[mesh->numtriangles * 3 + 2] = Mod_ShadowMesh_AddVertex(mesh, vertex3f + 3 * element3i[i * 3 + 2]);
@@ -1061,7 +1096,7 @@ static void Mod_ShadowMesh_CreateVBOs(shadowmesh_t *mesh)
        }
 }
 
-shadowmesh_t *Mod_ShadowMesh_Finish(shadowmesh_t *mesh, qboolean createvbo)
+shadowmesh_t *Mod_ShadowMesh_Finish(shadowmesh_t *mesh, qbool createvbo)
 {
        if (mesh->numverts >= 3 && mesh->numtriangles >= 1)
        {
@@ -1158,10 +1193,10 @@ void Mod_ShadowMesh_Free(shadowmesh_t *mesh)
        Mem_Free(mesh);
 }
 
-void Mod_CreateCollisionMesh(dp_model_t *mod)
+void Mod_CreateCollisionMesh(model_t *mod)
 {
        int k, numcollisionmeshtriangles;
-       qboolean usesinglecollisionmesh = false;
+       qbool usesinglecollisionmesh = false;
        const msurface_t *surface = NULL;
 
        mempool_t *mempool = mod->mempool;
@@ -1170,9 +1205,9 @@ void Mod_CreateCollisionMesh(dp_model_t *mod)
        // make a single combined collision mesh for physics engine use
        // TODO rewrite this to use the collision brushes as source, to fix issues with e.g. common/caulk which creates no drawsurface
        numcollisionmeshtriangles = 0;
-       for (k = 0;k < mod->nummodelsurfaces;k++)
+       for (k = mod->submodelsurfaces_start;k < mod->submodelsurfaces_end;k++)
        {
-               surface = mod->data_surfaces + mod->firstmodelsurface + k;
+               surface = mod->data_surfaces + k;
                if (!strcmp(surface->texture->name, "collision") || !strcmp(surface->texture->name, "collisionconvex")) // found collision mesh
                {
                        usesinglecollisionmesh = true;
@@ -1188,9 +1223,9 @@ void Mod_CreateCollisionMesh(dp_model_t *mod)
                Mod_ShadowMesh_AddMesh(mod->brush.collisionmesh, mod->surfmesh.data_vertex3f, surface->num_triangles, (mod->surfmesh.data_element3i + 3 * surface->num_firsttriangle));
        else
        {
-               for (k = 0;k < mod->nummodelsurfaces;k++)
+               for (k = mod->submodelsurfaces_start; k < mod->submodelsurfaces_end; k++)
                {
-                       surface = mod->data_surfaces + mod->firstmodelsurface + k;
+                       surface = mod->data_surfaces + k;
                        if (!(surface->texture->supercontents & SUPERCONTENTS_SOLID))
                                continue;
                        Mod_ShadowMesh_AddMesh(mod->brush.collisionmesh, mod->surfmesh.data_vertex3f, surface->num_triangles, (mod->surfmesh.data_element3i + 3 * surface->num_firsttriangle));
@@ -1264,7 +1299,7 @@ static void Mod_ConstructTerrainPatchFromBGRA(const unsigned char *imagepixels,
 #endif
 
 #if 0
-void Mod_Terrain_SurfaceRecurseChunk(dp_model_t *model, int stepsize, int x, int y)
+void Mod_Terrain_SurfaceRecurseChunk(model_t *model, int stepsize, int x, int y)
 {
        float mins[3];
        float maxs[3];
@@ -1335,7 +1370,7 @@ void Mod_Terrain_SurfaceRecurseChunk(dp_model_t *model, int stepsize, int x, int
        // TODO: emit skirt vertices
 }
 
-void Mod_Terrain_UpdateSurfacesForViewOrigin(dp_model_t *model)
+void Mod_Terrain_UpdateSurfacesForViewOrigin(model_t *model)
 {
        for (y = 0;y < model->terrain.size[1];y += model->terrain.
        Mod_Terrain_SurfaceRecurseChunk(model, model->terrain.maxstepsize, x, y);
@@ -1370,7 +1405,7 @@ void Mod_FreeQ3Shaders(void)
        Mem_FreePool(&q3shaders_mem);
 }
 
-static void Q3Shader_AddToHash (q3shaderinfo_t* shader)
+static void Q3Shader_AddToHash (shader_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);
@@ -1425,7 +1460,7 @@ static void Q3Shader_AddToHash (q3shaderinfo_t* shader)
                /* else: head of chain, in hash entry array */
                entry = lastEntry;
        }
-       memcpy (&entry->shader, shader, sizeof (q3shaderinfo_t));
+       memcpy (&entry->shader, shader, sizeof (shader_t));
 }
 
 void Mod_LoadQ3Shaders(void)
@@ -1435,14 +1470,14 @@ void Mod_LoadQ3Shaders(void)
        fssearch_t *search;
        char *f;
        const char *text;
-       q3shaderinfo_t shader;
+       shader_t shader;
        q3shaderinfo_layer_t *layer;
        int numparameters;
        char parameter[TEXTURE_MAXFRAMES + 4][Q3PATHLENGTH];
        char *custsurfaceparmnames[256]; // VorteX: q3map2 has 64 but well, someone will need more
        unsigned long custsurfaceflags[256]; 
        int numcustsurfaceflags;
-       qboolean dpshaderkill;
+       qbool dpshaderkill;
 
        Mod_FreeQ3Shaders();
 
@@ -1483,7 +1518,7 @@ void Mod_LoadQ3Shaders(void)
                                        // name
                                        j = (int)strlen(com_token)+1;
                                        custsurfaceparmnames[numcustsurfaceflags] = (char *)Mem_Alloc(tempmempool, j);
-                                       strlcpy(custsurfaceparmnames[numcustsurfaceflags], com_token, j+1);
+                                       dp_strlcpy(custsurfaceparmnames[numcustsurfaceflags], com_token, j+1);
                                        // value
                                        if (COM_ParseToken_QuakeC(&text, false))
                                                custsurfaceflags[numcustsurfaceflags] = strtol(com_token, NULL, 0);
@@ -1497,7 +1532,7 @@ void Mod_LoadQ3Shaders(void)
        }
 
        // parse shaders
-       search = FS_Search("scripts/*.shader", true, false);
+       search = FS_Search("scripts/*.shader", true, false, NULL);
        if (!search)
                return;
        for (fileindex = 0;fileindex < search->numfilenames;fileindex++)
@@ -1545,7 +1580,7 @@ void Mod_LoadQ3Shaders(void)
                        // WHEN ADDING DEFAULTS HERE, REMEMBER TO PUT DEFAULTS IN ALL LOADERS
                        // JUST GREP FOR "specularscalemod = 1".
 
-                       strlcpy(shader.name, com_token, sizeof(shader.name));
+                       dp_strlcpy(shader.name, com_token, sizeof(shader.name));
                        if (!COM_ParseToken_QuakeC(&text, false) || strcasecmp(com_token, "{"))
                        {
                                Con_DPrintf("%s parsing error - expected \"{\", found \"%s\"\n", search->filenames[fileindex], com_token);
@@ -1588,7 +1623,7 @@ void Mod_LoadQ3Shaders(void)
                                                                if(j == 0 && !strncasecmp(com_token, "dp_", 3))
                                                                        dpsnprintf(parameter[j], sizeof(parameter[j]), "dp%s", &com_token[3]);
                                                                else
-                                                                       strlcpy(parameter[j], com_token, sizeof(parameter[j]));
+                                                                       dp_strlcpy(parameter[j], com_token, sizeof(parameter[j]));
                                                                numparameters = j + 1;
                                                        }
                                                        if (!COM_ParseToken_QuakeC(&text, true))
@@ -1843,7 +1878,7 @@ void Mod_LoadQ3Shaders(void)
                                                if(j == 0 && !strncasecmp(com_token, "dp_", 3))
                                                        dpsnprintf(parameter[j], sizeof(parameter[j]), "dp%s", &com_token[3]);
                                                else
-                                                       strlcpy(parameter[j], com_token, sizeof(parameter[j]));
+                                                       dp_strlcpy(parameter[j], com_token, sizeof(parameter[j]));
                                                numparameters = j + 1;
                                        }
                                        if (!COM_ParseToken_QuakeC(&text, true))
@@ -1953,7 +1988,7 @@ void Mod_LoadQ3Shaders(void)
                                else if (!strcasecmp(parameter[0], "dpnortlight"))
                                        shader.dpnortlight = true;
                                else if (!strcasecmp(parameter[0], "dpreflectcube"))
-                                       strlcpy(shader.dpreflectcube, parameter[1], sizeof(shader.dpreflectcube));
+                                       dp_strlcpy(shader.dpreflectcube, parameter[1], sizeof(shader.dpreflectcube));
                                else if (!strcasecmp(parameter[0], "dpmeshcollisions"))
                                        shader.dpmeshcollisions = true;
                                // this sets dpshaderkill to true if dpshaderkillifcvarzero was used, and to false if dpnoshaderkillifcvarzero was used
@@ -2012,14 +2047,14 @@ void Mod_LoadQ3Shaders(void)
                                {
                                        // some q3 skies don't have the sky parm set
                                        shader.surfaceparms |= Q3SURFACEPARM_SKY;
-                                       strlcpy(shader.skyboxname, parameter[1], sizeof(shader.skyboxname));
+                                       dp_strlcpy(shader.skyboxname, parameter[1], sizeof(shader.skyboxname));
                                }
                                else if (!strcasecmp(parameter[0], "skyparms") && numparameters >= 2)
                                {
                                        // some q3 skies don't have the sky parm set
                                        shader.surfaceparms |= Q3SURFACEPARM_SKY;
                                        if (!atoi(parameter[1]) && strcasecmp(parameter[1], "-"))
-                                               strlcpy(shader.skyboxname, parameter[1], sizeof(shader.skyboxname));
+                                               dp_strlcpy(shader.skyboxname, parameter[1], sizeof(shader.skyboxname));
                                }
                                else if (!strcasecmp(parameter[0], "cull") && numparameters >= 2)
                                {
@@ -2181,7 +2216,7 @@ void Mod_LoadQ3Shaders(void)
                Mem_Free(custsurfaceparmnames[j]);
 }
 
-q3shaderinfo_t *Mod_LookupQ3Shader(const char *name)
+shader_t *Mod_LookupQ3Shader(const char *name)
 {
        unsigned short hash;
        q3shader_hash_entry_t* entry;
@@ -2232,14 +2267,14 @@ texture_shaderpass_t *Mod_CreateShaderPassFromQ3ShaderLayer(mempool_t *mempool,
        return shaderpass;
 }
 
-qboolean Mod_LoadTextureFromQ3Shader(mempool_t *mempool, const char *modelname, texture_t *texture, const char *name, qboolean warnmissing, qboolean fallback, int defaulttexflags, int defaultmaterialflags)
+qbool Mod_LoadTextureFromQ3Shader(mempool_t *mempool, const char *modelname, texture_t *texture, const char *name, qbool warnmissing, qbool fallback, int defaulttexflags, int defaultmaterialflags)
 {
        int texflagsmask, texflagsor;
-       qboolean success = true;
-       q3shaderinfo_t *shader;
+       qbool success = true;
+       shader_t *shader;
        if (!name)
                name = "";
-       strlcpy(texture->name, name, sizeof(texture->name));
+       dp_strlcpy(texture->name, name, sizeof(texture->name));
        texture->basealpha = 1.0f;
        shader = name[0] ? Mod_LookupQ3Shader(name) : NULL;
 
@@ -2599,7 +2634,7 @@ nothing                GL_ZERO GL_ONE
                        }
                        else
                                success = false;
-                       if (!success)
+                       if (!success && warnmissing)
                                Con_Printf("^1%s:^7 could not load texture ^3\"%s\"\n", modelname, texture->name);
                }
        }
@@ -2620,7 +2655,7 @@ void Mod_LoadCustomMaterial(mempool_t *mempool, texture_t *texture, const char *
        if (!(materialflags & (MATERIALFLAG_WALL | MATERIALFLAG_SKY)))
                Con_DPrintf("^1Custom texture ^3\"%s\" does not have MATERIALFLAG_WALL set\n", texture->name);
 
-       strlcpy(texture->name, name, sizeof(texture->name));
+       dp_strlcpy(texture->name, name, sizeof(texture->name));
        texture->basealpha = 1.0f;
        texture->basematerialflags = materialflags;
        texture->supercontents = supercontents;
@@ -2647,7 +2682,7 @@ void Mod_LoadCustomMaterial(mempool_t *mempool, texture_t *texture, const char *
        texture->backgroundcurrentskinframe = NULL;
 }
 
-void Mod_UnloadCustomMaterial(texture_t *texture, qboolean purgeskins)
+void Mod_UnloadCustomMaterial(texture_t *texture, qbool purgeskins)
 {
        long unsigned int i, j;
        for (i = 0; i < sizeof(texture->shaderpasses) / sizeof(texture->shaderpasses[0]); i++)
@@ -2720,7 +2755,7 @@ tag_torso,
                        do
                        {
                                if (words < 10)
-                                       strlcpy(word[words++], com_token, sizeof (word[0]));
+                                       dp_strlcpy(word[words++], com_token, sizeof (word[0]));
                                else
                                        wordsoverflow = true;
                        }
@@ -2740,8 +2775,8 @@ tag_torso,
                                        skinfileitem = (skinfileitem_t *)Mem_Alloc(loadmodel->mempool, sizeof(skinfileitem_t));
                                        skinfileitem->next = skinfile->items;
                                        skinfile->items = skinfileitem;
-                                       strlcpy (skinfileitem->name, word[1], sizeof (skinfileitem->name));
-                                       strlcpy (skinfileitem->replacement, word[2], sizeof (skinfileitem->replacement));
+                                       dp_strlcpy (skinfileitem->name, word[1], sizeof (skinfileitem->name));
+                                       dp_strlcpy (skinfileitem->replacement, word[2], sizeof (skinfileitem->replacement));
                                }
                                else
                                        Con_Printf("Mod_LoadSkinFiles: parsing error in file \"%s_%i.skin\" on line #%i: wrong number of parameters to command \"%s\", see documentation in DP_GFX_SKINFILES extension in dpextensions.qc\n", loadmodel->name, i, line, word[0]);
@@ -2759,8 +2794,8 @@ tag_torso,
                                skinfileitem = (skinfileitem_t *)Mem_Alloc(loadmodel->mempool, sizeof(skinfileitem_t));
                                skinfileitem->next = skinfile->items;
                                skinfile->items = skinfileitem;
-                               strlcpy (skinfileitem->name, word[0], sizeof (skinfileitem->name));
-                               strlcpy (skinfileitem->replacement, word[2], sizeof (skinfileitem->replacement));
+                               dp_strlcpy (skinfileitem->name, word[0], sizeof (skinfileitem->name));
+                               dp_strlcpy (skinfileitem->replacement, word[2], sizeof (skinfileitem->replacement));
                        }
                        else
                                Con_Printf("Mod_LoadSkinFiles: parsing error in file \"%s_%i.skin\" on line #%i: does not look like tag or mesh specification, or replace command, see documentation in DP_GFX_SKINFILES extension in dpextensions.qc\n", loadmodel->name, i, line);
@@ -2849,41 +2884,81 @@ void Mod_VertexRangeFromElements(int numelements, const int *elements, int *firs
                *lastvertexpointer = lastvertex;
 }
 
-void Mod_MakeSortedSurfaces(dp_model_t *mod)
+void Mod_SetDrawSkyAndWater(model_t* mod)
+{
+       int j;
+       uint64_t basematerialflags = 0;
+       // by default assume there is no sky or water used in this model
+       mod->DrawSky = NULL;
+       mod->DrawAddWaterPlanes = NULL;
+       // combine all basematerialflags observed in the submodelsurfaces range, then check for special flags
+       for (j = mod->submodelsurfaces_start; j < mod->submodelsurfaces_end; j++)
+               if (mod->data_surfaces[j].texture)
+                       basematerialflags |= mod->data_surfaces[j].texture->basematerialflags;
+       if (basematerialflags & MATERIALFLAG_SKY)
+               mod->DrawSky = R_Mod_DrawSky;
+       if (basematerialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA))
+               mod->DrawAddWaterPlanes = R_Mod_DrawAddWaterPlanes;
+}
+
+typedef struct Mod_MakeSortedSurfaces_qsortsurface_s
+{
+       int surfaceindex;
+       q3deffect_t* effect;
+       texture_t* texture;
+       rtexture_t* lightmaptexture;
+}
+Mod_MakeSortedSurfaces_qsortsurface_t;
+
+static int Mod_MakeSortedSurfaces_qsortfunc(const void *a, const void *b)
+{
+       const Mod_MakeSortedSurfaces_qsortsurface_t* l = (Mod_MakeSortedSurfaces_qsortsurface_t*)a;
+       const Mod_MakeSortedSurfaces_qsortsurface_t* r = (Mod_MakeSortedSurfaces_qsortsurface_t*)b;
+       if (l->effect < r->effect)
+               return -1;
+       if (l->effect > r->effect)
+               return 1;
+       if (l->texture < r->texture)
+               return -1;
+       if (l->texture > r->texture)
+               return 1;
+       if (l->lightmaptexture < r->lightmaptexture)
+               return -1;
+       if (l->lightmaptexture > r->lightmaptexture)
+               return 1;
+       if (l->surfaceindex < r->surfaceindex)
+               return -1;
+       if (l->surfaceindex > r->surfaceindex)
+               return 1;
+       return 0;
+}
+
+void Mod_MakeSortedSurfaces(model_t *mod)
 {
        // make an optimal set of texture-sorted batches to draw...
-       int j, t;
-       int *firstsurfacefortexture;
-       int *numsurfacesfortexture;
-       if (!mod->sortedmodelsurfaces)
-               mod->sortedmodelsurfaces = (int *) Mem_Alloc(loadmodel->mempool, mod->nummodelsurfaces * sizeof(*mod->sortedmodelsurfaces));
-       firstsurfacefortexture = (int *) Mem_Alloc(tempmempool, mod->num_textures * sizeof(*firstsurfacefortexture));
-       numsurfacesfortexture = (int *) Mem_Alloc(tempmempool, mod->num_textures * sizeof(*numsurfacesfortexture));
-       memset(numsurfacesfortexture, 0, mod->num_textures * sizeof(*numsurfacesfortexture));
-       for (j = 0;j < mod->nummodelsurfaces;j++)
-       {
-               const msurface_t *surface = mod->data_surfaces + j + mod->firstmodelsurface;
-               if(!surface->texture)
-                       continue;
-               t = (int)(surface->texture - mod->data_textures);
-               numsurfacesfortexture[t]++;
-       }
-       j = 0;
-       for (t = 0;t < mod->num_textures;t++)
-       {
-               firstsurfacefortexture[t] = j;
-               j += numsurfacesfortexture[t];
-       }
-       for (j = 0;j < mod->nummodelsurfaces;j++)
+       int j, k;
+       Mod_MakeSortedSurfaces_qsortsurface_t *info;
+
+       if(cls.state == ca_dedicated)
+               return;
+
+       info = (Mod_MakeSortedSurfaces_qsortsurface_t*)Mem_Alloc(loadmodel->mempool, mod->num_surfaces * sizeof(*info));
+       if (!mod->modelsurfaces_sorted)
+               mod->modelsurfaces_sorted = (int *) Mem_Alloc(loadmodel->mempool, mod->num_surfaces * sizeof(*mod->modelsurfaces_sorted));
+       // the goal is to sort by submodel (can't change which submodel a surface belongs to), and then by effects and textures
+       for (j = 0; j < mod->num_surfaces; j++)
        {
-               const msurface_t *surface = mod->data_surfaces + j + mod->firstmodelsurface;
-               if (!surface->texture)
-                       continue;
-               t = (int)(surface->texture - mod->data_textures);
-               mod->sortedmodelsurfaces[firstsurfacefortexture[t]++] = j + mod->firstmodelsurface;
+               info[j].surfaceindex = j;
+               info[j].effect = mod->data_surfaces[j].effect;
+               info[j].texture = mod->data_surfaces[j].texture;
+               info[j].lightmaptexture = mod->data_surfaces[j].lightmaptexture;
        }
-       Mem_Free(firstsurfacefortexture);
-       Mem_Free(numsurfacesfortexture);
+       for (k = 0; k < mod->brush.numsubmodels; k++)
+               if (mod->brush.submodels[k]->submodelsurfaces_end > mod->brush.submodels[k]->submodelsurfaces_start + 1)
+                       qsort(info + mod->brush.submodels[k]->submodelsurfaces_start, (size_t)mod->brush.submodels[k]->submodelsurfaces_end - mod->brush.submodels[k]->submodelsurfaces_start, sizeof(*info), Mod_MakeSortedSurfaces_qsortfunc);
+       for (j = 0; j < mod->num_surfaces; j++)
+               mod->modelsurfaces_sorted[j] = info[j].surfaceindex;
+       Mem_Free(info);
 }
 
 void Mod_BuildVBOs(void)
@@ -2955,7 +3030,7 @@ void Mod_BuildVBOs(void)
 }
 
 extern cvar_t mod_obj_orientation;
-static void Mod_Decompile_OBJ(dp_model_t *model, const char *filename, const char *mtlfilename, const char *originalfilename)
+static void Mod_Decompile_OBJ(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;
        int a, b, c;
@@ -2969,7 +3044,7 @@ static void Mod_Decompile_OBJ(dp_model_t *model, const char *filename, const cha
        const msurface_t *surface;
        const int maxtextures = 256;
        char *texturenames = (char *) Z_Malloc(maxtextures * MAX_QPATH);
-       dp_model_t *submodel;
+       model_t *submodel;
 
        // construct the mtllib file
        l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "# mtllib for %s exported by darkplaces engine\n", originalfilename);
@@ -2989,7 +3064,7 @@ static void Mod_Decompile_OBJ(dp_model_t *model, const char *filename, const cha
                if (textureindex >= maxtextures)
                        continue; // just a precaution
                textureindex = counttextures++;
-               strlcpy(texturenames + textureindex * MAX_QPATH, texname, MAX_QPATH);
+               dp_strlcpy(texturenames + textureindex * MAX_QPATH, texname, MAX_QPATH);
                if (outbufferpos >= outbuffermax >> 1)
                {
                        outbuffermax *= 2;
@@ -3036,9 +3111,9 @@ static void Mod_Decompile_OBJ(dp_model_t *model, const char *filename, const cha
                if (l > 0)
                        outbufferpos += l;
                submodel = model->brush.numsubmodels ? model->brush.submodels[submodelindex] : model;
-               for (surfaceindex = 0;surfaceindex < submodel->nummodelsurfaces;surfaceindex++)
+               for (surfaceindex = submodel->submodelsurfaces_start;surfaceindex < submodel->submodelsurfaces_end;surfaceindex++)
                {
-                       surface = model->data_surfaces + submodel->sortedmodelsurfaces[surfaceindex];
+                       surface = model->data_surfaces + surfaceindex;
                        l = dpsnprintf(outbuffer + outbufferpos, outbuffermax - outbufferpos, "usemtl %s\n", (surface->texture && surface->texture->name[0]) ? surface->texture->name : "default");
                        if (l > 0)
                                outbufferpos += l;
@@ -3076,7 +3151,7 @@ static void Mod_Decompile_OBJ(dp_model_t *model, const char *filename, const cha
        Con_Printf("Wrote %s (%i bytes, %i vertices, %i faces, %i surfaces with %i distinct textures)\n", filename, (int)outbufferpos, countvertices, countfaces, countsurfaces, counttextures);
 }
 
-static void Mod_Decompile_SMD(dp_model_t *model, const char *filename, int firstpose, int numposes, qboolean writetriangles)
+static void Mod_Decompile_SMD(model_t *model, const char *filename, int firstpose, int numposes, qbool writetriangles)
 {
        int countnodes = 0, counttriangles = 0, countframes = 0;
        int surfaceindex;
@@ -3134,7 +3209,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_FromBonePose7s(&posematrix, model->num_posescale, model->data_poses7s + 7*(model->num_bones * poseindex + transformindex));
-                       Matrix4x4_ToArray12FloatGL(&posematrix, mtest[0]);
+                       Matrix4x4_ToArray12FloatGL(&posematrix, mtest);
                        AnglesFromVectors(angles, mtest[0], mtest[2], false);
                        if (angles[0] >= 180) angles[0] -= 360;
                        if (angles[1] >= 180) angles[1] -= 360;
@@ -3243,7 +3318,7 @@ decompiles a model to editable files
 static void Mod_Decompile_f(cmd_state_t *cmd)
 {
        int i, j, k, l, first, count;
-       dp_model_t *mod;
+       model_t *mod;
        char inname[MAX_QPATH];
        char outname[MAX_QPATH];
        char mtlname[MAX_QPATH];
@@ -3264,7 +3339,7 @@ static void Mod_Decompile_f(cmd_state_t *cmd)
                return;
        }
 
-       strlcpy(inname, Cmd_Argv(cmd, 1), sizeof(inname));
+       dp_strlcpy(inname, Cmd_Argv(cmd, 1), sizeof(inname));
        FS_StripExtension(inname, basename, sizeof(basename));
 
        mod = Mod_ForName(inname, false, true, inname[0] == '*' ? cl.model_name[1] : NULL);
@@ -3305,7 +3380,7 @@ static void Mod_Decompile_f(cmd_state_t *cmd)
                if (l > 0) dpmtextsize += l;
                for (i = 0;i < mod->numframes;i = j)
                {
-                       strlcpy(animname, mod->animscenes[i].name, sizeof(animname));
+                       dp_strlcpy(animname, mod->animscenes[i].name, sizeof(animname));
                        first = mod->animscenes[i].firstframe;
                        if (mod->animscenes[i].framecount > 1)
                        {
@@ -3326,7 +3401,7 @@ static void Mod_Decompile_f(cmd_state_t *cmd)
                                count = mod->num_poses - first;
                                for (j = i + 1;j < mod->numframes;j++)
                                {
-                                       strlcpy(animname2, mod->animscenes[j].name, sizeof(animname2));
+                                       dp_strlcpy(animname2, mod->animscenes[j].name, sizeof(animname2));
                                        for (l = 0, k = (int)strlen(animname2);animname2[l];l++)
                                                if(animname2[l] < '0' || animname2[l] > '9')
                                                        k = l + 1;
@@ -3341,7 +3416,7 @@ static void Mod_Decompile_f(cmd_state_t *cmd)
                                }
                                // if it's only one frame, use the original frame name
                                if (j == i + 1)
-                                       strlcpy(animname, mod->animscenes[i].name, sizeof(animname));
+                                       dp_strlcpy(animname, mod->animscenes[i].name, sizeof(animname));
                                
                        }
                        dpsnprintf(outname, sizeof(outname), "%s_decompiled/%s.smd", basename, animname);
@@ -3404,7 +3479,7 @@ void Mod_AllocLightmap_Free(mod_alloclightmap_state_t *state)
        memset(state, 0, sizeof(*state));
 }
 
-qboolean Mod_AllocLightmap_Block(mod_alloclightmap_state_t *state, int blockwidth, int blockheight, int *outx, int *outy)
+qbool Mod_AllocLightmap_Block(mod_alloclightmap_state_t *state, int blockwidth, int blockheight, int *outx, int *outy)
 {
        mod_alloclightmap_row_t *row;
        int y;
@@ -3504,7 +3579,7 @@ static lightmaplight_t *mod_generatelightmaps_lightinfo;
 extern cvar_t r_shadow_lightattenuationdividebias;
 extern cvar_t r_shadow_lightattenuationlinearscale;
 
-static void Mod_GenerateLightmaps_LightPoint(dp_model_t *model, const vec3_t pos, vec3_t ambient, vec3_t diffuse, vec3_t lightdir)
+static void Mod_GenerateLightmaps_LightPoint(model_t *model, const vec3_t pos, vec3_t ambient, vec3_t diffuse, vec3_t lightdir)
 {
        int i;
        int index;
@@ -3573,7 +3648,7 @@ static void Mod_GenerateLightmaps_LightPoint(dp_model_t *model, const vec3_t pos
        VectorCopy(dir, lightdir);
 }
 
-static void Mod_GenerateLightmaps_CreateLights_ComputeSVBSP_InsertSurfaces(const dp_model_t *model, svbsp_t *svbsp, const float *mins, const float *maxs)
+static void Mod_GenerateLightmaps_CreateLights_ComputeSVBSP_InsertSurfaces(const model_t *model, svbsp_t *svbsp, const float *mins, const float *maxs)
 {
        int surfaceindex;
        int triangleindex;
@@ -3582,8 +3657,9 @@ static void Mod_GenerateLightmaps_CreateLights_ComputeSVBSP_InsertSurfaces(const
        const int *element3i = model->surfmesh.data_element3i;
        const int *e;
        float v2[3][3];
-       for (surfaceindex = 0, surface = model->data_surfaces;surfaceindex < model->nummodelsurfaces;surfaceindex++, surface++)
+       for (surfaceindex = model->submodelsurfaces_start;surfaceindex < model->submodelsurfaces_end;surfaceindex++)
        {
+               surface = model->data_surfaces + surfaceindex;
                if (!BoxesOverlap(surface->mins, surface->maxs, mins, maxs))
                        continue;
                if (surface->texture->basematerialflags & MATERIALFLAG_NOSHADOW)
@@ -3598,7 +3674,7 @@ static void Mod_GenerateLightmaps_CreateLights_ComputeSVBSP_InsertSurfaces(const
        }
 }
 
-static void Mod_GenerateLightmaps_CreateLights_ComputeSVBSP(dp_model_t *model, lightmaplight_t *lightinfo)
+static void Mod_GenerateLightmaps_CreateLights_ComputeSVBSP(model_t *model, lightmaplight_t *lightinfo)
 {
        int maxnodes = 1<<14;
        svbsp_node_t *nodes;
@@ -3637,7 +3713,7 @@ static void Mod_GenerateLightmaps_CreateLights_ComputeSVBSP(dp_model_t *model, l
        Mem_Free(nodes);
 }
 
-static void Mod_GenerateLightmaps_CreateLights(dp_model_t *model)
+static void Mod_GenerateLightmaps_CreateLights(model_t *model)
 {
        int index;
        int result;
@@ -3676,7 +3752,7 @@ static void Mod_GenerateLightmaps_CreateLights(dp_model_t *model)
        }
 }
 
-static void Mod_GenerateLightmaps_DestroyLights(dp_model_t *model)
+static void Mod_GenerateLightmaps_DestroyLights(model_t *model)
 {
        int i;
        if (mod_generatelightmaps_lightinfo)
@@ -3690,7 +3766,7 @@ static void Mod_GenerateLightmaps_DestroyLights(dp_model_t *model)
        mod_generatelightmaps_numlights = 0;
 }
 
-static qboolean Mod_GenerateLightmaps_SamplePoint_SVBSP(const svbsp_t *svbsp, const float *pos)
+static qbool Mod_GenerateLightmaps_SamplePoint_SVBSP(const svbsp_t *svbsp, const float *pos)
 {
        const svbsp_node_t *node;
        const svbsp_node_t *nodes = svbsp->nodes;
@@ -3839,7 +3915,7 @@ static void Mod_GenerateLightmaps_GridSample(const float *pos, q3dlightgrid_t *s
        else {s->diffusepitch = (unsigned char)(acos(dir[2]) * (127.5f/M_PI));s->diffuseyaw = (unsigned char)(atan2(dir[1], dir[0]) * (127.5f/M_PI));}
 }
 
-static void Mod_GenerateLightmaps_InitSampleOffsets(dp_model_t *model)
+static void Mod_GenerateLightmaps_InitSampleOffsets(model_t *model)
 {
        float radius[3];
        float temp[3];
@@ -3861,7 +3937,7 @@ static void Mod_GenerateLightmaps_InitSampleOffsets(dp_model_t *model)
        }
 }
 
-static void Mod_GenerateLightmaps_DestroyLightmaps(dp_model_t *model)
+static void Mod_GenerateLightmaps_DestroyLightmaps(model_t *model)
 {
        msurface_t *surface;
        int surfaceindex;
@@ -3890,7 +3966,7 @@ static void Mod_GenerateLightmaps_DestroyLightmaps(dp_model_t *model)
        }
 }
 
-static void Mod_GenerateLightmaps_UnweldTriangles(dp_model_t *model)
+static void Mod_GenerateLightmaps_UnweldTriangles(model_t *model)
 {
        msurface_t *surface;
        int surfaceindex;
@@ -3993,7 +4069,7 @@ static void Mod_GenerateLightmaps_UnweldTriangles(dp_model_t *model)
                model->brush.submodels[i]->surfmesh = model->surfmesh;
 }
 
-static void Mod_GenerateLightmaps_CreateTriangleInformation(dp_model_t *model)
+static void Mod_GenerateLightmaps_CreateTriangleInformation(model_t *model)
 {
        msurface_t *surface;
        int surfaceindex;
@@ -4035,7 +4111,7 @@ static void Mod_GenerateLightmaps_CreateTriangleInformation(dp_model_t *model)
        }
 }
 
-static void Mod_GenerateLightmaps_DestroyTriangleInformation(dp_model_t *model)
+static void Mod_GenerateLightmaps_DestroyTriangleInformation(model_t *model)
 {
        if (mod_generatelightmaps_lightmaptriangles)
                Mem_Free(mod_generatelightmaps_lightmaptriangles);
@@ -4044,7 +4120,7 @@ static void Mod_GenerateLightmaps_DestroyTriangleInformation(dp_model_t *model)
 
 float lmaxis[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
 
-static void Mod_GenerateLightmaps_CreateLightmaps(dp_model_t *model)
+static void Mod_GenerateLightmaps_CreateLightmaps(model_t *model)
 {
        msurface_t *surface;
        int surfaceindex;
@@ -4298,14 +4374,14 @@ static void Mod_GenerateLightmaps_CreateLightmaps(dp_model_t *model)
        }
 }
 
-static void Mod_GenerateLightmaps_UpdateVertexColors(dp_model_t *model)
+static void Mod_GenerateLightmaps_UpdateVertexColors(model_t *model)
 {
        int i;
        for (i = 0;i < model->surfmesh.num_vertices;i++)
                Mod_GenerateLightmaps_VertexSample(model->surfmesh.data_vertex3f + 3*i, model->surfmesh.data_normal3f + 3*i, model->surfmesh.data_lightmapcolor4f + 4*i);
 }
 
-static void Mod_GenerateLightmaps_UpdateLightGrid(dp_model_t *model)
+static void Mod_GenerateLightmaps_UpdateLightGrid(model_t *model)
 {
        int x;
        int y;
@@ -4328,10 +4404,10 @@ static void Mod_GenerateLightmaps_UpdateLightGrid(dp_model_t *model)
 }
 
 extern cvar_t mod_q3bsp_nolightmaps;
-static void Mod_GenerateLightmaps(dp_model_t *model)
+static void Mod_GenerateLightmaps(model_t *model)
 {
        //lightmaptriangle_t *lightmaptriangles = Mem_Alloc(model->mempool, model->surfmesh.num_triangles * sizeof(lightmaptriangle_t));
-       dp_model_t *oldloadmodel = loadmodel;
+       model_t *oldloadmodel = loadmodel;
        loadmodel = model;
 
        Mod_GenerateLightmaps_InitSampleOffsets(model);
@@ -4364,39 +4440,40 @@ static void Mod_GenerateLightmaps_f(cmd_state_t *cmd)
        Mod_GenerateLightmaps(cl.worldmodel);
 }
 
-void Mod_Mesh_Create(dp_model_t *mod, const char *name)
+void Mod_Mesh_Create(model_t *mod, const char *name)
 {
        memset(mod, 0, sizeof(*mod));
-       strlcpy(mod->name, name, sizeof(mod->name));
+       dp_strlcpy(mod->name, name, sizeof(mod->name));
        mod->mempool = Mem_AllocPool(name, 0, NULL);
        mod->texturepool = R_AllocTexturePool();
-       mod->Draw = R_Q1BSP_Draw;
-       mod->DrawDepth = R_Q1BSP_DrawDepth;
-       mod->DrawDebug = R_Q1BSP_DrawDebug;
-       mod->DrawPrepass = R_Q1BSP_DrawPrepass;
-       mod->GetLightInfo = R_Q1BSP_GetLightInfo;
-       mod->DrawShadowMap = R_Q1BSP_DrawShadowMap;
-       mod->DrawLight = R_Q1BSP_DrawLight;
+       mod->Draw = R_Mod_Draw;
+       mod->DrawDepth = R_Mod_DrawDepth;
+       mod->DrawDebug = R_Mod_DrawDebug;
+       mod->DrawPrepass = R_Mod_DrawPrepass;
+       mod->GetLightInfo = R_Mod_GetLightInfo;
+       mod->DrawShadowMap = R_Mod_DrawShadowMap;
+       mod->DrawLight = R_Mod_DrawLight;
 }
 
-void Mod_Mesh_Destroy(dp_model_t *mod)
+void Mod_Mesh_Destroy(model_t *mod)
 {
        Mod_UnloadModel(mod);
 }
 
 // resets the mesh model to have no geometry to render, ready for a new frame -
 // the mesh will be prepared for rendering later using Mod_Mesh_Finalize
-void Mod_Mesh_Reset(dp_model_t *mod)
+void Mod_Mesh_Reset(model_t *mod)
 {
        mod->num_surfaces = 0;
        mod->surfmesh.num_vertices = 0;
        mod->surfmesh.num_triangles = 0;
-       memset(mod->surfmesh.data_vertexhash, -1, mod->surfmesh.num_vertexhashsize * sizeof(*mod->surfmesh.data_vertexhash));
+       if (mod->surfmesh.data_vertexhash) // UBSan: memset arg 1 isn't allowed to be null, but sometimes this is NULL.
+               memset(mod->surfmesh.data_vertexhash, -1, mod->surfmesh.num_vertexhashsize * sizeof(*mod->surfmesh.data_vertexhash));
        mod->DrawSky = NULL; // will be set if a texture needs it
        mod->DrawAddWaterPlanes = NULL; // will be set if a texture needs it
 }
 
-texture_t *Mod_Mesh_GetTexture(dp_model_t *mod, const char *name, int defaultdrawflags, int defaulttexflags, int defaultmaterialflags)
+texture_t *Mod_Mesh_GetTexture(model_t *mod, const char *name, int defaultdrawflags, int defaulttexflags, int defaultmaterialflags)
 {
        int i;
        texture_t *t;
@@ -4448,7 +4525,7 @@ texture_t *Mod_Mesh_GetTexture(dp_model_t *mod, const char *name, int defaultdra
        return t;
 }
 
-msurface_t *Mod_Mesh_AddSurface(dp_model_t *mod, texture_t *tex, qboolean batchwithprevioussurface)
+msurface_t *Mod_Mesh_AddSurface(model_t *mod, texture_t *tex, qbool batchwithprevioussurface)
 {
        msurface_t *surf;
        // batch if possible; primarily useful for UI rendering where bounding boxes don't matter
@@ -4459,7 +4536,7 @@ msurface_t *Mod_Mesh_AddSurface(dp_model_t *mod, texture_t *tex, qboolean batchw
        {
                mod->max_surfaces = 2 * max(mod->num_surfaces, 64);
                mod->data_surfaces = (msurface_t *)Mem_Realloc(mod->mempool, mod->data_surfaces, mod->max_surfaces * sizeof(*mod->data_surfaces));
-               mod->sortedmodelsurfaces = (int *)Mem_Realloc(mod->mempool, mod->sortedmodelsurfaces, mod->max_surfaces * sizeof(*mod->sortedmodelsurfaces));
+               mod->modelsurfaces_sorted = (int *)Mem_Realloc(mod->mempool, mod->modelsurfaces_sorted, mod->max_surfaces * sizeof(*mod->modelsurfaces_sorted));
        }
        surf = mod->data_surfaces + mod->num_surfaces;
        mod->num_surfaces++;
@@ -4468,13 +4545,13 @@ msurface_t *Mod_Mesh_AddSurface(dp_model_t *mod, texture_t *tex, qboolean batchw
        surf->num_firsttriangle = mod->surfmesh.num_triangles;
        surf->num_firstvertex = mod->surfmesh.num_vertices;
        if (tex->basematerialflags & (MATERIALFLAG_SKY))
-               mod->DrawSky = R_Q1BSP_DrawSky;
+               mod->DrawSky = R_Mod_DrawSky;
        if (tex->basematerialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA))
-               mod->DrawAddWaterPlanes = R_Q1BSP_DrawAddWaterPlanes;
+               mod->DrawAddWaterPlanes = R_Mod_DrawAddWaterPlanes;
        return surf;
 }
 
-int Mod_Mesh_IndexForVertex(dp_model_t *mod, msurface_t *surf, float x, float y, float z, float nx, float ny, float nz, float s, float t, float u, float v, float r, float g, float b, float a)
+int Mod_Mesh_IndexForVertex(model_t *mod, msurface_t *surf, float x, float y, float z, float nx, float ny, float nz, float s, float t, float u, float v, float r, float g, float b, float a)
 {
        int hashindex, h, vnum, mask;
        surfmesh_t *mesh = &mod->surfmesh;
@@ -4553,7 +4630,7 @@ int Mod_Mesh_IndexForVertex(dp_model_t *mod, msurface_t *surf, float x, float y,
        return vnum;
 }
 
-void Mod_Mesh_AddTriangle(dp_model_t *mod, msurface_t *surf, int e0, int e1, int e2)
+void Mod_Mesh_AddTriangle(model_t *mod, msurface_t *surf, int e0, int e1, int e2)
 {
        surfmesh_t *mesh = &mod->surfmesh;
        if (mesh->max_triangles == mesh->num_triangles)
@@ -4572,39 +4649,35 @@ void Mod_Mesh_AddTriangle(dp_model_t *mod, msurface_t *surf, int e0, int e1, int
        surf->num_triangles++;
 }
 
-static void Mod_Mesh_MakeSortedSurfaces(dp_model_t *mod)
+static void Mod_Mesh_MakeSortedSurfaces(model_t *mod)
 {
        int i, j;
        texture_t *tex;
-       msurface_t *surf, *surf2;
 
        // build the sorted surfaces list properly to reduce material setup
        // this is easy because we're just sorting on texture and don't care about the order of textures
-       mod->nummodelsurfaces = 0;
+       mod->submodelsurfaces_start = 0;
+       mod->submodelsurfaces_end = 0;
        for (i = 0; i < mod->num_surfaces; i++)
                mod->data_surfaces[i].included = false;
        for (i = 0; i < mod->num_surfaces; i++)
        {
-               surf = mod->data_surfaces + i;
-               if (surf->included)
+               if (mod->data_surfaces[i].included)
                        continue;
-               tex = surf->texture;
+               tex = mod->data_surfaces[i].texture;
                // j = i is intentional
                for (j = i; j < mod->num_surfaces; j++)
                {
-                       surf2 = mod->data_surfaces + j;
-                       if (surf2->included)
-                               continue;
-                       if (surf2->texture == tex)
+                       if (!mod->data_surfaces[j].included && mod->data_surfaces[j].texture == tex)
                        {
-                               surf2->included = true;
-                               mod->sortedmodelsurfaces[mod->nummodelsurfaces++] = j;
+                               mod->data_surfaces[j].included = 1;
+                               mod->modelsurfaces_sorted[mod->submodelsurfaces_end++] = j;
                        }
                }
        }
 }
 
-static void Mod_Mesh_ComputeBounds(dp_model_t *mod)
+static void Mod_Mesh_ComputeBounds(model_t *mod)
 {
        int i;
        vec_t x2a, x2b, y2a, y2b, z2a, z2b, x2, y2, z2, yawradius, rotatedradius;
@@ -4660,10 +4733,10 @@ static void Mod_Mesh_ComputeBounds(dp_model_t *mod)
        }
 }
 
-void Mod_Mesh_Validate(dp_model_t *mod)
+void Mod_Mesh_Validate(model_t *mod)
 {
        int i;
-       qboolean warned = false;
+       qbool warned = false;
        for (i = 0; i < mod->num_surfaces; i++)
        {
                msurface_t *surf = mod->data_surfaces + i;
@@ -4684,7 +4757,7 @@ void Mod_Mesh_Validate(dp_model_t *mod)
        }
 }
 
-static void Mod_Mesh_UploadDynamicBuffers(dp_model_t *mod)
+static void Mod_Mesh_UploadDynamicBuffers(model_t *mod)
 {
        mod->surfmesh.data_element3s_indexbuffer = mod->surfmesh.data_element3s ? R_BufferData_Store(mod->surfmesh.num_triangles * sizeof(short[3]), mod->surfmesh.data_element3s, R_BUFFERDATA_INDEX16, &mod->surfmesh.data_element3s_bufferoffset) : NULL;
        mod->surfmesh.data_element3i_indexbuffer = mod->surfmesh.data_element3i ? R_BufferData_Store(mod->surfmesh.num_triangles * sizeof(int[3]), mod->surfmesh.data_element3i, R_BUFFERDATA_INDEX32, &mod->surfmesh.data_element3i_bufferoffset) : NULL;
@@ -4699,7 +4772,7 @@ static void Mod_Mesh_UploadDynamicBuffers(dp_model_t *mod)
        mod->surfmesh.data_skeletalweight4ub_vertexbuffer = mod->surfmesh.data_skeletalweight4ub ? R_BufferData_Store(mod->surfmesh.num_vertices * sizeof(unsigned char[4]), mod->surfmesh.data_skeletalweight4ub, R_BUFFERDATA_VERTEX, &mod->surfmesh.data_skeletalweight4ub_bufferoffset) : NULL;
 }
 
-void Mod_Mesh_Finalize(dp_model_t *mod)
+void Mod_Mesh_Finalize(model_t *mod)
 {
        if (gl_paranoid.integer)
                Mod_Mesh_Validate(mod);