]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - model_brush.c
Rework game specific hacks to have a special group for Nexuiz-derived games.
[xonotic/darkplaces.git] / model_brush.c
index 50ef572b9724819bc0535f5f5f14029196b94c21..b8e64da458f37cbd8582ee13b863ee3013112289 100644 (file)
@@ -40,6 +40,7 @@ cvar_t r_subdivisions_collision_maxtess = {0, "r_subdivisions_collision_maxtess"
 cvar_t r_subdivisions_collision_maxvertices = {0, "r_subdivisions_collision_maxvertices", "4225", "maximum vertices allowed per subdivided curve"};
 cvar_t r_trippy = {0, "r_trippy", "0", "easter egg"};
 cvar_t mod_noshader_default_offsetmapping = {CVAR_SAVE, "mod_noshader_default_offsetmapping", "1", "use offsetmapping by default on all surfaces that are not using q3 shader files"};
+cvar_t mod_obj_orientation = {0, "mod_obj_orientation", "1", "fix orientation of OBJ models to the usual conventions (if zero, use coordinates as is)"};
 cvar_t mod_q3bsp_curves_collisions = {0, "mod_q3bsp_curves_collisions", "1", "enables collisions with curves (SLOW)"};
 cvar_t mod_q3bsp_curves_collisions_stride = {0, "mod_q3bsp_curves_collisions_stride", "16", "collisions against curves: optimize performance by doing a combined collision check for this triangle amount first (-1 avoids any box tests)"};
 cvar_t mod_q3bsp_curves_stride = {0, "mod_q3bsp_curves_stride", "16", "particle effect collisions against curves: optimize performance by doing a combined collision check for this triangle amount first (-1 avoids any box tests)"};
@@ -55,11 +56,11 @@ cvar_t mod_q3shader_default_offsetmapping_bias = {CVAR_SAVE, "mod_q3shader_defau
 cvar_t mod_q3shader_default_polygonfactor = {0, "mod_q3shader_default_polygonfactor", "0", "biases depth values of 'polygonoffset' shaders to prevent z-fighting artifacts"};
 cvar_t mod_q3shader_default_polygonoffset = {0, "mod_q3shader_default_polygonoffset", "-2", "biases depth values of 'polygonoffset' shaders to prevent z-fighting artifacts"};
 cvar_t mod_q3shader_force_addalpha = {0, "mod_q3shader_force_addalpha", "0", "treat GL_ONE GL_ONE (or add) blendfunc as GL_SRC_ALPHA GL_ONE for compatibility with older DarkPlaces releases"};
+cvar_t mod_q3shader_force_terrain_alphaflag = {0, "mod_q3shader_force_terrain_alphaflag", "0", "for multilayered terrain shaders force TEXF_ALPHA flag on both layers"};
+
 cvar_t mod_q1bsp_polygoncollisions = {0, "mod_q1bsp_polygoncollisions", "0", "disables use of precomputed cliphulls and instead collides with polygons (uses Bounding Interval Hierarchy optimizations)"};
 cvar_t mod_collision_bih = {0, "mod_collision_bih", "1", "enables use of generated Bounding Interval Hierarchy tree instead of compiled bsp tree in collision code"};
 cvar_t mod_recalculatenodeboxes = {0, "mod_recalculatenodeboxes", "1", "enables use of generated node bounding boxes based on BSP tree portal reconstruction, rather than the node boxes supplied by the map compiler"};
-extern cvar_t vid_sRGB;
-extern cvar_t vid_sRGB_fallback;
 
 static texture_t mod_q1bsp_texture_solid;
 static texture_t mod_q1bsp_texture_sky;
@@ -83,6 +84,7 @@ void Mod_BrushInit(void)
        Cvar_RegisterVariable(&r_subdivisions_collision_maxvertices);
        Cvar_RegisterVariable(&r_trippy);
        Cvar_RegisterVariable(&mod_noshader_default_offsetmapping);
+       Cvar_RegisterVariable(&mod_obj_orientation);
        Cvar_RegisterVariable(&mod_q3bsp_curves_collisions);
        Cvar_RegisterVariable(&mod_q3bsp_curves_collisions_stride);
        Cvar_RegisterVariable(&mod_q3bsp_curves_stride);
@@ -98,6 +100,7 @@ void Mod_BrushInit(void)
        Cvar_RegisterVariable(&mod_q3shader_default_polygonfactor);
        Cvar_RegisterVariable(&mod_q3shader_default_polygonoffset);
        Cvar_RegisterVariable(&mod_q3shader_force_addalpha);
+       Cvar_RegisterVariable(&mod_q3shader_force_terrain_alphaflag);
        Cvar_RegisterVariable(&mod_q1bsp_polygoncollisions);
        Cvar_RegisterVariable(&mod_collision_bih);
        Cvar_RegisterVariable(&mod_recalculatenodeboxes);
@@ -900,6 +903,8 @@ static void Mod_Q1BSP_TracePoint(struct model_s *model, const frameblend_t *fram
        Mod_Q1BSP_RecursiveHullCheckPoint(&rhc, rhc.hull->firstclipnode);
 }
 
+static void Mod_Q1BSP_TraceLineAgainstSurfaces(struct model_s *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t end, int hitsupercontentsmask);
+
 static void Mod_Q1BSP_TraceLine(struct model_s *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t end, int hitsupercontentsmask)
 {
        RecursiveHullCheckTraceInfo_t rhc;
@@ -910,6 +915,13 @@ static void Mod_Q1BSP_TraceLine(struct model_s *model, const frameblend_t *frame
                return;
        }
 
+       // sometimes we want to traceline against polygons so we can report the texture that was hit rather than merely a contents, but using this method breaks one of negke's maps so it must be a cvar check...
+       if (sv_gameplayfix_q1bsptracelinereportstexture.integer)
+       {
+               Mod_Q1BSP_TraceLineAgainstSurfaces(model, frameblend, skeleton, trace, start, end, hitsupercontentsmask);
+               return;
+       }
+
        memset(&rhc, 0, sizeof(rhc));
        memset(trace, 0, sizeof(trace_t));
        rhc.trace = trace;
@@ -1228,7 +1240,8 @@ loc0:
                // check for impact on this node
                if (node->numsurfaces)
                {
-                       int i, dsi, dti, lmwidth, lmheight;
+                       unsigned int i;
+                       int dsi, dti, lmwidth, lmheight;
                        float ds, dt;
                        msurface_t *surface;
                        unsigned char *lightmap;
@@ -1313,7 +1326,7 @@ static void Mod_Q1BSP_LightPoint(dp_model_t *model, const vec3_t p, vec3_t ambie
 
 static const texture_t *Mod_Q1BSP_TraceLineAgainstSurfacesFindTextureOnNode(RecursiveHullCheckTraceInfo_t *t, const dp_model_t *model, const mnode_t *node, double mid[3])
 {
-       int i;
+       unsigned int i;
        int j;
        int k;
        const msurface_t *surface;
@@ -1600,18 +1613,17 @@ static void R_Q1BSP_LoadSplitSky (unsigned char *src, int width, int height, int
        Mem_Free(alphapixels);
 }
 
-static void Mod_Q1BSP_LoadTextures(lump_t *l)
+static void Mod_Q1BSP_LoadTextures(sizebuf_t *sb)
 {
-       int i, j, k, num, max, altmax, mtwidth, mtheight, *dofs, incomplete;
+       int i, j, k, num, max, altmax, mtwidth, mtheight, doffset, incomplete, nummiptex = 0;
        skinframe_t *skinframe;
-       miptex_t *dmiptex;
        texture_t *tx, *tx2, *anims[10], *altanims[10];
        texture_t backuptex;
-       dmiptexlump_t *m;
        unsigned char *data, *mtdata;
        const char *s;
        char mapname[MAX_QPATH], name[MAX_QPATH];
        unsigned char zeroopaque[4], zerotrans[4];
+       sizebuf_t miptexsb;
        char vabuf[1024];
        Vector4Set(zeroopaque, 0, 0, 0, 255);
        Vector4Set(zerotrans, 0, 0, 0, 128);
@@ -1619,16 +1631,14 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
        loadmodel->data_textures = NULL;
 
        // add two slots for notexture walls and notexture liquids
-       if (l->filelen)
+       if (sb->cursize)
        {
-               m = (dmiptexlump_t *)(mod_base + l->fileofs);
-               m->nummiptex = LittleLong (m->nummiptex);
-               loadmodel->num_textures = m->nummiptex + 2;
+               nummiptex = MSG_ReadLittleLong(sb);
+               loadmodel->num_textures = nummiptex + 2;
                loadmodel->num_texturesperskin = loadmodel->num_textures;
        }
        else
        {
-               m = NULL;
                loadmodel->num_textures = 2;
                loadmodel->num_texturesperskin = loadmodel->num_textures;
        }
@@ -1679,9 +1689,12 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
                tx->offsetbias = 0;
                tx->specularscalemod = 1;
                tx->specularpowermod = 1;
+               tx->transparentsort = TRANSPARENTSORT_DISTANCE;
+               // WHEN ADDING DEFAULTS HERE, REMEMBER TO PUT DEFAULTS IN ALL LOADERS
+               // JUST GREP FOR "specularscalemod = 1".
        }
 
-       if (!m)
+       if (!sb->cursize)
        {
                Con_Printf("%s: no miptex lump to load textures from\n", loadmodel->name);
                return;
@@ -1693,26 +1706,29 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
        FS_StripExtension(s, mapname, sizeof(mapname));
 
        // just to work around bounds checking when debugging with it (array index out of bounds error thing)
-       dofs = m->dataofs;
        // LordHavoc: mostly rewritten map texture loader
-       for (i = 0;i < m->nummiptex;i++)
+       for (i = 0;i < nummiptex;i++)
        {
-               dofs[i] = LittleLong(dofs[i]);
+               doffset = MSG_ReadLittleLong(sb);
                if (r_nosurftextures.integer)
                        continue;
-               if (dofs[i] == -1)
+               if (doffset == -1)
                {
                        Con_DPrintf("%s: miptex #%i missing\n", loadmodel->name, i);
                        continue;
                }
-               dmiptex = (miptex_t *)((unsigned char *)m + dofs[i]);
+
+               MSG_InitReadBuffer(&miptexsb, sb->data + doffset, sb->cursize - doffset);
 
                // copy name, but only up to 16 characters
                // (the output buffer can hold more than this, but the input buffer is
                //  only 16)
-               for (j = 0;j < 16 && dmiptex->name[j];j++)
-                       name[j] = dmiptex->name[j];
+               for (j = 0;j < 16;j++)
+                       name[j] = MSG_ReadByte(&miptexsb);
                name[j] = 0;
+               // pretty up the buffer (replacing any trailing garbage with 0)
+               for (j = strlen(name);j < 16;j++)
+                       name[j] = 0;
 
                if (!name[0])
                {
@@ -1720,23 +1736,23 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
                        Con_DPrintf("%s: warning: renaming unnamed texture to %s\n", loadmodel->name, name);
                }
 
-               mtwidth = LittleLong(dmiptex->width);
-               mtheight = LittleLong(dmiptex->height);
+               mtwidth = MSG_ReadLittleLong(&miptexsb);
+               mtheight = MSG_ReadLittleLong(&miptexsb);
                mtdata = NULL;
-               j = LittleLong(dmiptex->offsets[0]);
+               j = MSG_ReadLittleLong(&miptexsb);
                if (j)
                {
                        // texture included
-                       if (j < 40 || j + mtwidth * mtheight > l->filelen)
+                       if (j < 40 || j + mtwidth * mtheight > miptexsb.cursize)
                        {
-                               Con_Printf("%s: Texture \"%s\" is corrupt or incomplete\n", loadmodel->name, dmiptex->name);
+                               Con_Printf("%s: Texture \"%s\" is corrupt or incomplete\n", loadmodel->name, name);
                                continue;
                        }
-                       mtdata = (unsigned char *)dmiptex + j;
+                       mtdata = miptexsb.data + j;
                }
 
                if ((mtwidth & 15) || (mtheight & 15))
-                       Con_DPrintf("%s: warning: texture \"%s\" is not 16 aligned\n", loadmodel->name, dmiptex->name);
+                       Con_DPrintf("%s: warning: texture \"%s\" is not 16 aligned\n", loadmodel->name, name);
 
                // LordHavoc: force all names to lowercase
                for (j = 0;name[j];j++)
@@ -1745,7 +1761,7 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
 
                // LordHavoc: backup the texture_t because q3 shader loading overwrites it
                backuptex = loadmodel->data_textures[i];
-               if (dmiptex->name[0] && Mod_LoadTextureFromQ3Shader(loadmodel->data_textures + i, name, false, false, 0))
+               if (name[0] && Mod_LoadTextureFromQ3Shader(loadmodel->data_textures + i, name, false, false, 0))
                        continue;
                loadmodel->data_textures[i] = backuptex;
 
@@ -1776,6 +1792,8 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
                {
                        tx->supercontents = mod_q1bsp_texture_sky.supercontents;
                        tx->surfaceflags = mod_q1bsp_texture_sky.surfaceflags;
+                       // for the surface traceline we need to hit this surface as a solid...
+                       tx->supercontents |= SUPERCONTENTS_SOLID;
                }
                else
                {
@@ -1815,7 +1833,7 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
                                                unsigned char *pixels, *freepixels;
                                                pixels = freepixels = NULL;
                                                if (mtdata)
-                                                       pixels = W_ConvertWAD3TextureBGRA(dmiptex);
+                                                       pixels = W_ConvertWAD3TextureBGRA(&miptexsb);
                                                if (pixels == NULL)
                                                        pixels = freepixels = W_GetTextureBGRA(tx->name);
                                                if (pixels != NULL)
@@ -1875,7 +1893,7 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
        }
 
        // sequence the animations
-       for (i = 0;i < m->nummiptex;i++)
+       for (i = 0;i < nummiptex;i++)
        {
                tx = loadmodel->data_textures + i;
                if (!tx || tx->name[0] != '+' || tx->name[1] == 0 || tx->name[2] == 0)
@@ -1887,7 +1905,7 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
                memset(anims, 0, sizeof(anims));
                memset(altanims, 0, sizeof(altanims));
 
-               for (j = i;j < m->nummiptex;j++)
+               for (j = i;j < nummiptex;j++)
                {
                        tx2 = loadmodel->data_textures + j;
                        if (!tx2 || tx2->name[0] != '+' || strcmp(tx2->name+2, tx->name+2))
@@ -1976,7 +1994,7 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
        }
 }
 
-static void Mod_Q1BSP_LoadLighting(lump_t *l)
+static void Mod_Q1BSP_LoadLighting(sizebuf_t *sb)
 {
        int i;
        unsigned char *in, *out, *data, d;
@@ -1985,9 +2003,9 @@ static void Mod_Q1BSP_LoadLighting(lump_t *l)
        fs_offset_t filesize;
        if (loadmodel->brush.ishlbsp) // LordHavoc: load the colored lighting data straight
        {
-               loadmodel->brushq1.lightdata = (unsigned char *)Mem_Alloc(loadmodel->mempool, l->filelen);
-               for (i=0; i<l->filelen; i++)
-                       loadmodel->brushq1.lightdata[i] = mod_base[l->fileofs+i] >>= 1;
+               loadmodel->brushq1.lightdata = (unsigned char *)Mem_Alloc(loadmodel->mempool, sb->cursize);
+               for (i = 0;i < sb->cursize;i++)
+                       loadmodel->brushq1.lightdata[i] = sb->data[i] >>= 1;
        }
        else // LordHavoc: bsp version 29 (normal white lighting)
        {
@@ -2000,7 +2018,7 @@ static void Mod_Q1BSP_LoadLighting(lump_t *l)
                data = (unsigned char*) FS_LoadFile(litfilename, tempmempool, false, &filesize);
                if (data)
                {
-                       if (filesize == (fs_offset_t)(8 + l->filelen * 3) && data[0] == 'Q' && data[1] == 'L' && data[2] == 'I' && data[3] == 'T')
+                       if (filesize == (fs_offset_t)(8 + sb->cursize * 3) && data[0] == 'Q' && data[1] == 'L' && data[2] == 'I' && data[3] == 'T')
                        {
                                i = LittleLong(((int *)data)[1]);
                                if (i == 1)
@@ -2013,7 +2031,7 @@ static void Mod_Q1BSP_LoadLighting(lump_t *l)
                                        data = (unsigned char*) FS_LoadFile(dlitfilename, tempmempool, false, &filesize);
                                        if (data)
                                        {
-                                               if (filesize == (fs_offset_t)(8 + l->filelen * 3) && data[0] == 'Q' && data[1] == 'L' && data[2] == 'I' && data[3] == 'T')
+                                               if (filesize == (fs_offset_t)(8 + sb->cursize * 3) && data[0] == 'Q' && data[1] == 'L' && data[2] == 'I' && data[3] == 'T')
                                                {
                                                        i = LittleLong(((int *)data)[1]);
                                                        if (i == 1)
@@ -2037,7 +2055,7 @@ static void Mod_Q1BSP_LoadLighting(lump_t *l)
                        else if (filesize == 8)
                                Con_Print("Empty .lit file, ignoring\n");
                        else
-                               Con_Printf("Corrupt .lit file (file size %i bytes, should be %i bytes), ignoring\n", (int) filesize, (int) (8 + l->filelen * 3));
+                               Con_Printf("Corrupt .lit file (file size %i bytes, should be %i bytes), ignoring\n", (int) filesize, (int) (8 + sb->cursize * 3));
                        if (data)
                        {
                                Mem_Free(data);
@@ -2045,12 +2063,12 @@ static void Mod_Q1BSP_LoadLighting(lump_t *l)
                        }
                }
                // LordHavoc: oh well, expand the white lighting data
-               if (!l->filelen)
+               if (!sb->cursize)
                        return;
-               loadmodel->brushq1.lightdata = (unsigned char *)Mem_Alloc(loadmodel->mempool, l->filelen*3);
-               in = mod_base + l->fileofs;
+               loadmodel->brushq1.lightdata = (unsigned char *)Mem_Alloc(loadmodel->mempool, sb->cursize*3);
+               in = sb->data;
                out = loadmodel->brushq1.lightdata;
-               for (i = 0;i < l->filelen;i++)
+               for (i = 0;i < sb->cursize;i++)
                {
                        d = *in++;
                        *out++ = d;
@@ -2060,15 +2078,15 @@ static void Mod_Q1BSP_LoadLighting(lump_t *l)
        }
 }
 
-static void Mod_Q1BSP_LoadVisibility(lump_t *l)
+static void Mod_Q1BSP_LoadVisibility(sizebuf_t *sb)
 {
        loadmodel->brushq1.num_compressedpvs = 0;
        loadmodel->brushq1.data_compressedpvs = NULL;
-       if (!l->filelen)
+       if (!sb->cursize)
                return;
-       loadmodel->brushq1.num_compressedpvs = l->filelen;
-       loadmodel->brushq1.data_compressedpvs = (unsigned char *)Mem_Alloc(loadmodel->mempool, l->filelen);
-       memcpy(loadmodel->brushq1.data_compressedpvs, mod_base + l->fileofs, l->filelen);
+       loadmodel->brushq1.num_compressedpvs = sb->cursize;
+       loadmodel->brushq1.data_compressedpvs = (unsigned char *)Mem_Alloc(loadmodel->mempool, sb->cursize);
+       MSG_ReadBytes(sb, sb->cursize, loadmodel->brushq1.data_compressedpvs);
 }
 
 // used only for HalfLife maps
@@ -2128,78 +2146,52 @@ static void Mod_Q1BSP_ParseWadsFromEntityLump(const char *data)
        }
 }
 
-static void Mod_Q1BSP_LoadEntities(lump_t *l)
+static void Mod_Q1BSP_LoadEntities(sizebuf_t *sb)
 {
        loadmodel->brush.entities = NULL;
-       if (!l->filelen)
+       if (!sb->cursize)
                return;
-       loadmodel->brush.entities = (char *)Mem_Alloc(loadmodel->mempool, l->filelen + 1);
-       memcpy(loadmodel->brush.entities, mod_base + l->fileofs, l->filelen);
-       loadmodel->brush.entities[l->filelen] = 0;
+       loadmodel->brush.entities = (char *)Mem_Alloc(loadmodel->mempool, sb->cursize + 1);
+       MSG_ReadBytes(sb, sb->cursize, (unsigned char *)loadmodel->brush.entities);
+       loadmodel->brush.entities[sb->cursize] = 0;
        if (loadmodel->brush.ishlbsp)
                Mod_Q1BSP_ParseWadsFromEntityLump(loadmodel->brush.entities);
 }
 
 
-static void Mod_Q1BSP_LoadVertexes(lump_t *l)
+static void Mod_Q1BSP_LoadVertexes(sizebuf_t *sb)
 {
-       dvertex_t       *in;
        mvertex_t       *out;
        int                     i, count;
+       size_t          structsize = 12;
 
-       in = (dvertex_t *)(mod_base + l->fileofs);
-       if (l->filelen % sizeof(*in))
+       if (sb->cursize % structsize)
                Host_Error("Mod_Q1BSP_LoadVertexes: funny lump size in %s",loadmodel->name);
-       count = l->filelen / sizeof(*in);
+       count = sb->cursize / structsize;
        out = (mvertex_t *)Mem_Alloc(loadmodel->mempool, count*sizeof(*out));
 
        loadmodel->brushq1.vertexes = out;
        loadmodel->brushq1.numvertexes = count;
 
-       for ( i=0 ; i<count ; i++, in++, out++)
+       for ( i=0 ; i<count ; i++, out++)
        {
-               out->position[0] = LittleFloat(in->point[0]);
-               out->position[1] = LittleFloat(in->point[1]);
-               out->position[2] = LittleFloat(in->point[2]);
+               out->position[0] = MSG_ReadLittleFloat(sb);
+               out->position[1] = MSG_ReadLittleFloat(sb);
+               out->position[2] = MSG_ReadLittleFloat(sb);
        }
 }
 
-// The following two functions should be removed and MSG_* or SZ_* function sets adjusted so they
-// can be used for this
-// REMOVEME
-static int SB_ReadInt (unsigned char **buffer)
-{
-       int     i;
-       i = ((*buffer)[0]) + 256*((*buffer)[1]) + 65536*((*buffer)[2]) + 16777216*((*buffer)[3]);
-       (*buffer) += 4;
-       return i;
-}
-
-// REMOVEME
-static float SB_ReadFloat (unsigned char **buffer)
+static void Mod_Q1BSP_LoadSubmodels(sizebuf_t *sb, hullinfo_t *hullinfo)
 {
-       union
-       {
-               int             i;
-               float   f;
-       } u;
-
-       u.i = SB_ReadInt (buffer);
-       return u.f;
-}
-
-static void Mod_Q1BSP_LoadSubmodels(lump_t *l, hullinfo_t *hullinfo)
-{
-       unsigned char           *index;
-       dmodel_t        *out;
+       mmodel_t        *out;
        int                     i, j, count;
+       size_t structsize = (48+4*hullinfo->filehulls);
 
-       index = (unsigned char *)(mod_base + l->fileofs);
-       if (l->filelen % (48+4*hullinfo->filehulls))
+       if (sb->cursize % structsize)
                Host_Error ("Mod_Q1BSP_LoadSubmodels: funny lump size in %s", loadmodel->name);
 
-       count = l->filelen / (48+4*hullinfo->filehulls);
-       out = (dmodel_t *)Mem_Alloc (loadmodel->mempool, count*sizeof(*out));
+       count = sb->cursize / structsize;
+       out = (mmodel_t *)Mem_Alloc (loadmodel->mempool, count*sizeof(*out));
 
        loadmodel->brushq1.submodels = out;
        loadmodel->brush.numsubmodels = count;
@@ -2207,43 +2199,50 @@ static void Mod_Q1BSP_LoadSubmodels(lump_t *l, hullinfo_t *hullinfo)
        for (i = 0; i < count; i++, out++)
        {
        // spread out the mins / maxs by a pixel
-               out->mins[0] = SB_ReadFloat (&index) - 1;
-               out->mins[1] = SB_ReadFloat (&index) - 1;
-               out->mins[2] = SB_ReadFloat (&index) - 1;
-               out->maxs[0] = SB_ReadFloat (&index) + 1;
-               out->maxs[1] = SB_ReadFloat (&index) + 1;
-               out->maxs[2] = SB_ReadFloat (&index) + 1;
-               out->origin[0] = SB_ReadFloat (&index);
-               out->origin[1] = SB_ReadFloat (&index);
-               out->origin[2] = SB_ReadFloat (&index);
+               out->mins[0] = MSG_ReadLittleFloat(sb) - 1;
+               out->mins[1] = MSG_ReadLittleFloat(sb) - 1;
+               out->mins[2] = MSG_ReadLittleFloat(sb) - 1;
+               out->maxs[0] = MSG_ReadLittleFloat(sb) + 1;
+               out->maxs[1] = MSG_ReadLittleFloat(sb) + 1;
+               out->maxs[2] = MSG_ReadLittleFloat(sb) + 1;
+               out->origin[0] = MSG_ReadLittleFloat(sb);
+               out->origin[1] = MSG_ReadLittleFloat(sb);
+               out->origin[2] = MSG_ReadLittleFloat(sb);
                for (j = 0; j < hullinfo->filehulls; j++)
-                       out->headnode[j] = SB_ReadInt (&index);
-               out->visleafs = SB_ReadInt (&index);
-               out->firstface = SB_ReadInt (&index);
-               out->numfaces = SB_ReadInt (&index);
+                       out->headnode[j] = MSG_ReadLittleLong(sb);
+               out->visleafs  = MSG_ReadLittleLong(sb);
+               out->firstface = MSG_ReadLittleLong(sb);
+               out->numfaces  = MSG_ReadLittleLong(sb);
        }
 }
 
-static void Mod_Q1BSP_LoadEdges(lump_t *l)
+static void Mod_Q1BSP_LoadEdges(sizebuf_t *sb)
 {
-       dedge_t *in;
        medge_t *out;
        int     i, count;
+       size_t  structsize = loadmodel->brush.isbsp2 ? 8 : 4;
 
-       in = (dedge_t *)(mod_base + l->fileofs);
-       if (l->filelen % sizeof(*in))
+       if (sb->cursize % structsize)
                Host_Error("Mod_Q1BSP_LoadEdges: funny lump size in %s",loadmodel->name);
-       count = l->filelen / sizeof(*in);
+       count = sb->cursize / structsize;
        out = (medge_t *)Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
 
        loadmodel->brushq1.edges = out;
        loadmodel->brushq1.numedges = count;
 
-       for ( i=0 ; i<count ; i++, in++, out++)
+       for ( i=0 ; i<count ; i++, out++)
        {
-               out->v[0] = (unsigned short)LittleShort(in->v[0]);
-               out->v[1] = (unsigned short)LittleShort(in->v[1]);
-               if (out->v[0] >= loadmodel->brushq1.numvertexes || out->v[1] >= loadmodel->brushq1.numvertexes)
+               if (loadmodel->brush.isbsp2)
+               {
+                       out->v[0] = (unsigned int)MSG_ReadLittleLong(sb);
+                       out->v[1] = (unsigned int)MSG_ReadLittleLong(sb);
+               }
+               else
+               {
+                       out->v[0] = (unsigned short)MSG_ReadLittleShort(sb);
+                       out->v[1] = (unsigned short)MSG_ReadLittleShort(sb);
+               }
+               if ((int)out->v[0] >= loadmodel->brushq1.numvertexes || (int)out->v[1] >= loadmodel->brushq1.numvertexes)
                {
                        Con_Printf("Mod_Q1BSP_LoadEdges: %s has invalid vertex indices in edge %i (vertices %i %i >= numvertices %i)\n", loadmodel->name, i, out->v[0], out->v[1], loadmodel->brushq1.numvertexes);
                        if(!loadmodel->brushq1.numvertexes)
@@ -2255,29 +2254,28 @@ static void Mod_Q1BSP_LoadEdges(lump_t *l)
        }
 }
 
-static void Mod_Q1BSP_LoadTexinfo(lump_t *l)
+static void Mod_Q1BSP_LoadTexinfo(sizebuf_t *sb)
 {
-       texinfo_t *in;
        mtexinfo_t *out;
        int i, j, k, count, miptex;
+       size_t structsize = 40;
 
-       in = (texinfo_t *)(mod_base + l->fileofs);
-       if (l->filelen % sizeof(*in))
+       if (sb->cursize % structsize)
                Host_Error("Mod_Q1BSP_LoadTexinfo: funny lump size in %s",loadmodel->name);
-       count = l->filelen / sizeof(*in);
+       count = sb->cursize / structsize;
        out = (mtexinfo_t *)Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
 
        loadmodel->brushq1.texinfo = out;
        loadmodel->brushq1.numtexinfo = count;
 
-       for (i = 0;i < count;i++, in++, out++)
+       for (i = 0;i < count;i++, out++)
        {
                for (k = 0;k < 2;k++)
                        for (j = 0;j < 4;j++)
-                               out->vecs[k][j] = LittleFloat(in->vecs[k][j]);
+                               out->vecs[k][j] = MSG_ReadLittleFloat(sb);
 
-               miptex = LittleLong(in->miptex);
-               out->flags = LittleLong(in->flags);
+               miptex = MSG_ReadLittleLong(sb);
+               out->flags = MSG_ReadLittleLong(sb);
 
                out->texture = NULL;
                if (loadmodel->data_textures)
@@ -2456,19 +2454,18 @@ static void Mod_Q1BSP_GenerateWarpMesh(msurface_t *surface)
 #endif
 
 extern cvar_t gl_max_lightmapsize;
-static void Mod_Q1BSP_LoadFaces(lump_t *l)
+static void Mod_Q1BSP_LoadFaces(sizebuf_t *sb)
 {
-       dface_t *in;
        msurface_t *surface;
-       int i, j, count, surfacenum, planenum, smax, tmax, ssize, tsize, firstedge, numedges, totalverts, totaltris, lightmapnumber, lightmapsize, totallightmapsamples;
+       int i, j, count, surfacenum, planenum, smax, tmax, ssize, tsize, firstedge, numedges, totalverts, totaltris, lightmapnumber, lightmapsize, totallightmapsamples, lightmapoffset, texinfoindex;
        float texmins[2], texmaxs[2], val;
        rtexture_t *lightmaptexture, *deluxemaptexture;
        char vabuf[1024];
+       size_t structsize = loadmodel->brush.isbsp2 ? 28 : 20;
 
-       in = (dface_t *)(mod_base + l->fileofs);
-       if (l->filelen % sizeof(*in))
+       if (sb->cursize % structsize)
                Host_Error("Mod_Q1BSP_LoadFaces: funny lump size in %s",loadmodel->name);
-       count = l->filelen / sizeof(*in);
+       count = sb->cursize / structsize;
        loadmodel->data_surfaces = (msurface_t *)Mem_Alloc(loadmodel->mempool, count*sizeof(msurface_t));
        loadmodel->data_surfaces_lightmapinfo = (msurface_lightmapinfo_t *)Mem_Alloc(loadmodel->mempool, count*sizeof(msurface_lightmapinfo_t));
 
@@ -2479,9 +2476,12 @@ static void Mod_Q1BSP_LoadFaces(lump_t *l)
 
        totalverts = 0;
        totaltris = 0;
-       for (surfacenum = 0, in = (dface_t *)(mod_base + l->fileofs);surfacenum < count;surfacenum++, in++)
+       for (surfacenum = 0;surfacenum < count;surfacenum++)
        {
-               numedges = (unsigned short)LittleShort(in->numedges);
+               if (loadmodel->brush.isbsp2)
+                       numedges = BuffLittleLong(sb->data + structsize * surfacenum + 12);
+               else
+                       numedges = BuffLittleShort(sb->data + structsize * surfacenum + 8);
                totalverts += numedges;
                totaltris += numedges - 2;
        }
@@ -2496,25 +2496,29 @@ static void Mod_Q1BSP_LoadFaces(lump_t *l)
 
        totalverts = 0;
        totaltris = 0;
-       for (surfacenum = 0, in = (dface_t *)(mod_base + l->fileofs), surface = loadmodel->data_surfaces;surfacenum < count;surfacenum++, in++, surface++)
+       for (surfacenum = 0, surface = loadmodel->data_surfaces;surfacenum < count;surfacenum++, surface++)
        {
                surface->lightmapinfo = loadmodel->data_surfaces_lightmapinfo + surfacenum;
+               planenum = loadmodel->brush.isbsp2 ? MSG_ReadLittleLong(sb) : (unsigned short)MSG_ReadLittleShort(sb);
+               /*side = */loadmodel->brush.isbsp2 ? MSG_ReadLittleLong(sb) : (unsigned short)MSG_ReadLittleShort(sb);
+               firstedge = MSG_ReadLittleLong(sb);
+               numedges = loadmodel->brush.isbsp2 ? MSG_ReadLittleLong(sb) : (unsigned short)MSG_ReadLittleShort(sb);
+               texinfoindex = loadmodel->brush.isbsp2 ? MSG_ReadLittleLong(sb) : (unsigned short)MSG_ReadLittleShort(sb);
+               for (i = 0;i < MAXLIGHTMAPS;i++)
+                       surface->lightmapinfo->styles[i] = MSG_ReadByte(sb);
+               lightmapoffset = MSG_ReadLittleLong(sb);
 
                // FIXME: validate edges, texinfo, etc?
-               firstedge = LittleLong(in->firstedge);
-               numedges = (unsigned short)LittleShort(in->numedges);
                if ((unsigned int) firstedge > (unsigned int) loadmodel->brushq1.numsurfedges || (unsigned int) numedges > (unsigned int) loadmodel->brushq1.numsurfedges || (unsigned int) firstedge + (unsigned int) numedges > (unsigned int) loadmodel->brushq1.numsurfedges)
                        Host_Error("Mod_Q1BSP_LoadFaces: invalid edge range (firstedge %i, numedges %i, model edges %i)", firstedge, numedges, loadmodel->brushq1.numsurfedges);
-               i = (unsigned short)LittleShort(in->texinfo);
-               if ((unsigned int) i >= (unsigned int) loadmodel->brushq1.numtexinfo)
-                       Host_Error("Mod_Q1BSP_LoadFaces: invalid texinfo index %i(model has %i texinfos)", i, loadmodel->brushq1.numtexinfo);
-               surface->lightmapinfo->texinfo = loadmodel->brushq1.texinfo + i;
-               surface->texture = surface->lightmapinfo->texinfo->texture;
-
-               planenum = (unsigned short)LittleShort(in->planenum);
+               if ((unsigned int) texinfoindex >= (unsigned int) loadmodel->brushq1.numtexinfo)
+                       Host_Error("Mod_Q1BSP_LoadFaces: invalid texinfo index %i(model has %i texinfos)", texinfoindex, loadmodel->brushq1.numtexinfo);
                if ((unsigned int) planenum >= (unsigned int) loadmodel->brush.num_planes)
                        Host_Error("Mod_Q1BSP_LoadFaces: invalid plane index %i (model has %i planes)", planenum, loadmodel->brush.num_planes);
 
+               surface->lightmapinfo->texinfo = loadmodel->brushq1.texinfo + texinfoindex;
+               surface->texture = surface->lightmapinfo->texinfo->texture;
+
                //surface->flags = surface->texture->flags;
                //if (LittleShort(in->side))
                //      surface->flags |= SURF_PLANEBACK;
@@ -2582,12 +2586,9 @@ static void Mod_Q1BSP_LoadFaces(lump_t *l)
                tsize = (surface->lightmapinfo->extents[1] >> 4) + 1;
 
                // lighting info
-               for (i = 0;i < MAXLIGHTMAPS;i++)
-                       surface->lightmapinfo->styles[i] = in->styles[i];
                surface->lightmaptexture = NULL;
                surface->deluxemaptexture = r_texture_blanknormalmap;
-               i = LittleLong(in->lightofs);
-               if (i == -1)
+               if (lightmapoffset == -1)
                {
                        surface->lightmapinfo->samples = NULL;
 #if 1
@@ -2601,12 +2602,12 @@ static void Mod_Q1BSP_LoadFaces(lump_t *l)
 #endif
                }
                else if (loadmodel->brush.ishlbsp) // LordHavoc: HalfLife map (bsp version 30)
-                       surface->lightmapinfo->samples = loadmodel->brushq1.lightdata + i;
+                       surface->lightmapinfo->samples = loadmodel->brushq1.lightdata + lightmapoffset;
                else // LordHavoc: white lighting (bsp version 29)
                {
-                       surface->lightmapinfo->samples = loadmodel->brushq1.lightdata + (i * 3);
+                       surface->lightmapinfo->samples = loadmodel->brushq1.lightdata + (lightmapoffset * 3);
                        if (loadmodel->brushq1.nmaplightdata)
-                               surface->lightmapinfo->nmapsamples = loadmodel->brushq1.nmaplightdata + (i * 3);
+                               surface->lightmapinfo->nmapsamples = loadmodel->brushq1.nmaplightdata + (lightmapoffset * 3);
                }
 
                // check if we should apply a lightmap to this
@@ -2764,16 +2765,15 @@ static void Mod_Q1BSP_LoadNodes_RecursiveSetParent(mnode_t *node, mnode_t *paren
        }
 }
 
-static void Mod_Q1BSP_LoadNodes(lump_t *l)
+static void Mod_Q1BSP_LoadNodes(sizebuf_t *sb)
 {
-       int                     i, j, count, p;
-       dnode_t         *in;
+       int                     i, j, count, p, child[2];
        mnode_t         *out;
+       size_t structsize = loadmodel->brush.isbsp2rmqe ? 32 : (loadmodel->brush.isbsp2 ? 44 : 24);
 
-       in = (dnode_t *)(mod_base + l->fileofs);
-       if (l->filelen % sizeof(*in))
+       if (sb->cursize % structsize)
                Host_Error("Mod_Q1BSP_LoadNodes: funny lump size in %s",loadmodel->name);
-       count = l->filelen / sizeof(*in);
+       count = sb->cursize / structsize;
        if (count == 0)
                Host_Error("Mod_Q1BSP_LoadNodes: missing BSP tree in %s",loadmodel->name);
        out = (mnode_t *)Mem_Alloc(loadmodel->mempool, count*sizeof(*out));
@@ -2781,19 +2781,56 @@ static void Mod_Q1BSP_LoadNodes(lump_t *l)
        loadmodel->brush.data_nodes = out;
        loadmodel->brush.num_nodes = count;
 
-       for ( i=0 ; i<count ; i++, in++, out++)
+       for ( i=0 ; i<count ; i++, out++)
        {
-               for (j=0 ; j<3 ; j++)
-               {
-                       out->mins[j] = LittleShort(in->mins[j]);
-                       out->maxs[j] = LittleShort(in->maxs[j]);
+               p = MSG_ReadLittleLong(sb);
+               out->plane = loadmodel->brush.data_planes + p;
+
+               if (loadmodel->brush.isbsp2rmqe)
+               {
+                       child[0] = MSG_ReadLittleLong(sb);
+                       child[1] = MSG_ReadLittleLong(sb);
+                       out->mins[0] = MSG_ReadLittleShort(sb);
+                       out->mins[1] = MSG_ReadLittleShort(sb);
+                       out->mins[2] = MSG_ReadLittleShort(sb);
+                       out->maxs[0] = MSG_ReadLittleShort(sb);
+                       out->maxs[1] = MSG_ReadLittleShort(sb);
+                       out->maxs[2] = MSG_ReadLittleShort(sb);
+                       out->firstsurface = MSG_ReadLittleLong(sb);
+                       out->numsurfaces = MSG_ReadLittleLong(sb);
+               }
+               else if (loadmodel->brush.isbsp2)
+               {
+                       child[0] = MSG_ReadLittleLong(sb);
+                       child[1] = MSG_ReadLittleLong(sb);
+                       out->mins[0] = MSG_ReadLittleFloat(sb);
+                       out->mins[1] = MSG_ReadLittleFloat(sb);
+                       out->mins[2] = MSG_ReadLittleFloat(sb);
+                       out->maxs[0] = MSG_ReadLittleFloat(sb);
+                       out->maxs[1] = MSG_ReadLittleFloat(sb);
+                       out->maxs[2] = MSG_ReadLittleFloat(sb);
+                       out->firstsurface = MSG_ReadLittleLong(sb);
+                       out->numsurfaces = MSG_ReadLittleLong(sb);
                }
+               else
+               {
+                       child[0] = (unsigned short)MSG_ReadLittleShort(sb);
+                       child[1] = (unsigned short)MSG_ReadLittleShort(sb);
+                       if (child[0] >= count)
+                               child[0] -= 65536;
+                       if (child[1] >= count)
+                               child[1] -= 65536;
 
-               p = LittleLong(in->planenum);
-               out->plane = loadmodel->brush.data_planes + p;
+                       out->mins[0] = MSG_ReadLittleShort(sb);
+                       out->mins[1] = MSG_ReadLittleShort(sb);
+                       out->mins[2] = MSG_ReadLittleShort(sb);
+                       out->maxs[0] = MSG_ReadLittleShort(sb);
+                       out->maxs[1] = MSG_ReadLittleShort(sb);
+                       out->maxs[2] = MSG_ReadLittleShort(sb);
 
-               out->firstsurface = (unsigned short)LittleShort(in->firstface);
-               out->numsurfaces = (unsigned short)LittleShort(in->numfaces);
+                       out->firstsurface = (unsigned short)MSG_ReadLittleShort(sb);
+                       out->numsurfaces = (unsigned short)MSG_ReadLittleShort(sb);
+               }
 
                for (j=0 ; j<2 ; j++)
                {
@@ -2801,8 +2838,8 @@ static void Mod_Q1BSP_LoadNodes(lump_t *l)
                        // arguire qbsp which can produce more than 32768 nodes, any value
                        // below count is assumed to be a node number, any other value is
                        // assumed to be a leaf number
-                       p = (unsigned short)LittleShort(in->children[j]);
-                       if (p < count)
+                       p = child[j];
+                       if (p >= 0)
                        {
                                if (p < loadmodel->brush.num_nodes)
                                        out->children[j] = loadmodel->brush.data_nodes + p;
@@ -2815,8 +2852,8 @@ static void Mod_Q1BSP_LoadNodes(lump_t *l)
                        }
                        else
                        {
-                               // note this uses 65535 intentionally, -1 is leaf 0
-                               p = 65535 - p;
+                               // get leaf index as a positive value starting at 0 (-1 becomes 0, -2 becomes 1, etc)
+                               p = -(p+1);
                                if (p < loadmodel->brush.num_leafs)
                                        out->children[j] = (mnode_t *)(loadmodel->brush.data_leafs + p);
                                else
@@ -2832,16 +2869,15 @@ static void Mod_Q1BSP_LoadNodes(lump_t *l)
        Mod_Q1BSP_LoadNodes_RecursiveSetParent(loadmodel->brush.data_nodes, NULL);      // sets nodes and leafs
 }
 
-static void Mod_Q1BSP_LoadLeafs(lump_t *l)
+static void Mod_Q1BSP_LoadLeafs(sizebuf_t *sb)
 {
-       dleaf_t *in;
        mleaf_t *out;
-       int i, j, count, p;
+       int i, j, count, p, firstmarksurface, nummarksurfaces;
+       size_t structsize = loadmodel->brush.isbsp2rmqe ? 32 : (loadmodel->brush.isbsp2 ? 44 : 28);
 
-       in = (dleaf_t *)(mod_base + l->fileofs);
-       if (l->filelen % sizeof(*in))
+       if (sb->cursize % structsize)
                Host_Error("Mod_Q1BSP_LoadLeafs: funny lump size in %s",loadmodel->name);
-       count = l->filelen / sizeof(*in);
+       count = sb->cursize / structsize;
        out = (mleaf_t *)Mem_Alloc(loadmodel->mempool, count*sizeof(*out));
 
        loadmodel->brush.data_leafs = out;
@@ -2852,32 +2888,16 @@ static void Mod_Q1BSP_LoadLeafs(lump_t *l)
        loadmodel->brush.data_pvsclusters = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->brush.num_pvsclusters * loadmodel->brush.num_pvsclusterbytes);
        memset(loadmodel->brush.data_pvsclusters, 0xFF, loadmodel->brush.num_pvsclusters * loadmodel->brush.num_pvsclusterbytes);
 
-       for ( i=0 ; i<count ; i++, in++, out++)
+       // FIXME: this function could really benefit from some error checking
+       for ( i=0 ; i<count ; i++, out++)
        {
-               for (j=0 ; j<3 ; j++)
-               {
-                       out->mins[j] = LittleShort(in->mins[j]);
-                       out->maxs[j] = LittleShort(in->maxs[j]);
-               }
-
-               // FIXME: this function could really benefit from some error checking
-
-               out->contents = LittleLong(in->contents);
-
-               out->firstleafsurface = loadmodel->brush.data_leafsurfaces + (unsigned short)LittleShort(in->firstmarksurface);
-               out->numleafsurfaces = (unsigned short)LittleShort(in->nummarksurfaces);
-               if ((unsigned short)LittleShort(in->firstmarksurface) + out->numleafsurfaces > loadmodel->brush.num_leafsurfaces)
-               {
-                       Con_Printf("Mod_Q1BSP_LoadLeafs: invalid leafsurface range %i:%i outside range %i:%i\n", (int)(out->firstleafsurface - loadmodel->brush.data_leafsurfaces), (int)(out->firstleafsurface + out->numleafsurfaces - loadmodel->brush.data_leafsurfaces), 0, loadmodel->brush.num_leafsurfaces);
-                       out->firstleafsurface = NULL;
-                       out->numleafsurfaces = 0;
-               }
+               out->contents = MSG_ReadLittleLong(sb);
 
                out->clusterindex = i - 1;
                if (out->clusterindex >= loadmodel->brush.num_pvsclusters)
                        out->clusterindex = -1;
 
-               p = LittleLong(in->visofs);
+               p = MSG_ReadLittleLong(sb);
                // ignore visofs errors on leaf 0 (solid)
                if (p >= 0 && out->clusterindex >= 0)
                {
@@ -2887,10 +2907,57 @@ static void Mod_Q1BSP_LoadLeafs(lump_t *l)
                                Mod_Q1BSP_DecompressVis(loadmodel->brushq1.data_compressedpvs + p, loadmodel->brushq1.data_compressedpvs + loadmodel->brushq1.num_compressedpvs, loadmodel->brush.data_pvsclusters + out->clusterindex * loadmodel->brush.num_pvsclusterbytes, loadmodel->brush.data_pvsclusters + (out->clusterindex + 1) * loadmodel->brush.num_pvsclusterbytes);
                }
 
-               for (j = 0;j < 4;j++)
-                       out->ambient_sound_level[j] = in->ambient_level[j];
+               if (loadmodel->brush.isbsp2rmqe)
+               {
+                       out->mins[0] = MSG_ReadLittleShort(sb);
+                       out->mins[1] = MSG_ReadLittleShort(sb);
+                       out->mins[2] = MSG_ReadLittleShort(sb);
+                       out->maxs[0] = MSG_ReadLittleShort(sb);
+                       out->maxs[1] = MSG_ReadLittleShort(sb);
+                       out->maxs[2] = MSG_ReadLittleShort(sb);
+       
+                       firstmarksurface = MSG_ReadLittleLong(sb);
+                       nummarksurfaces = MSG_ReadLittleLong(sb);
+               }
+               else if (loadmodel->brush.isbsp2)
+               {
+                       out->mins[0] = MSG_ReadLittleFloat(sb);
+                       out->mins[1] = MSG_ReadLittleFloat(sb);
+                       out->mins[2] = MSG_ReadLittleFloat(sb);
+                       out->maxs[0] = MSG_ReadLittleFloat(sb);
+                       out->maxs[1] = MSG_ReadLittleFloat(sb);
+                       out->maxs[2] = MSG_ReadLittleFloat(sb);
+       
+                       firstmarksurface = MSG_ReadLittleLong(sb);
+                       nummarksurfaces = MSG_ReadLittleLong(sb);
+               }
+               else
+               {
+                       out->mins[0] = MSG_ReadLittleShort(sb);
+                       out->mins[1] = MSG_ReadLittleShort(sb);
+                       out->mins[2] = MSG_ReadLittleShort(sb);
+                       out->maxs[0] = MSG_ReadLittleShort(sb);
+                       out->maxs[1] = MSG_ReadLittleShort(sb);
+                       out->maxs[2] = MSG_ReadLittleShort(sb);
+       
+                       firstmarksurface = (unsigned short)MSG_ReadLittleShort(sb);
+                       nummarksurfaces  = (unsigned short)MSG_ReadLittleShort(sb);
+               }
 
-               // FIXME: Insert caustics here
+               if (firstmarksurface >= 0 && firstmarksurface + nummarksurfaces <= loadmodel->brush.num_leafsurfaces)
+               {
+                       out->firstleafsurface = loadmodel->brush.data_leafsurfaces + firstmarksurface;
+                       out->numleafsurfaces = nummarksurfaces;
+               }
+               else
+               {
+                       Con_Printf("Mod_Q1BSP_LoadLeafs: invalid leafsurface range %i:%i outside range %i:%i\n", firstmarksurface, firstmarksurface+nummarksurfaces, 0, loadmodel->brush.num_leafsurfaces);
+                       out->firstleafsurface = NULL;
+                       out->numleafsurfaces = 0;
+               }
+
+               for (j = 0;j < 4;j++)
+                       out->ambient_sound_level[j] = MSG_ReadByte(sb);
        }
 }
 
@@ -2917,17 +2984,16 @@ static qboolean Mod_Q1BSP_CheckWaterAlphaSupport(void)
        return false;
 }
 
-static void Mod_Q1BSP_LoadClipnodes(lump_t *l, hullinfo_t *hullinfo)
+static void Mod_Q1BSP_LoadClipnodes(sizebuf_t *sb, hullinfo_t *hullinfo)
 {
-       dclipnode_t *in;
        mclipnode_t *out;
        int                     i, count;
        hull_t          *hull;
+       size_t structsize = loadmodel->brush.isbsp2 ? 12 : 8;
 
-       in = (dclipnode_t *)(mod_base + l->fileofs);
-       if (l->filelen % sizeof(*in))
+       if (sb->cursize % structsize)
                Host_Error("Mod_Q1BSP_LoadClipnodes: funny lump size in %s",loadmodel->name);
-       count = l->filelen / sizeof(*in);
+       count = sb->cursize / structsize;
        out = (mclipnode_t *)Mem_Alloc(loadmodel->mempool, count*sizeof(*out));
 
        loadmodel->brushq1.clipnodes = out;
@@ -2949,18 +3015,30 @@ static void Mod_Q1BSP_LoadClipnodes(lump_t *l, hullinfo_t *hullinfo)
                VectorSubtract(hull->clip_maxs, hull->clip_mins, hull->clip_size);
        }
 
-       for (i=0 ; i<count ; i++, out++, in++)
+       for (i=0 ; i<count ; i++, out++)
        {
-               out->planenum = LittleLong(in->planenum);
-               // LordHavoc: this code supports arguire qbsp's broken clipnodes indices (more than 32768 clipnodes), values above count are assumed to be contents values
-               out->children[0] = (unsigned short)LittleShort(in->children[0]);
-               out->children[1] = (unsigned short)LittleShort(in->children[1]);
-               if (out->children[0] >= count)
-                       out->children[0] -= 65536;
-               if (out->children[1] >= count)
-                       out->children[1] -= 65536;
+               out->planenum = MSG_ReadLittleLong(sb);
                if (out->planenum < 0 || out->planenum >= loadmodel->brush.num_planes)
-                       Host_Error("Corrupt clipping hull(out of range planenum)");
+                       Host_Error("%s: Corrupt clipping hull(out of range planenum)", loadmodel->name);
+               if (loadmodel->brush.isbsp2)
+               {
+                       out->children[0] = MSG_ReadLittleLong(sb);
+                       out->children[1] = MSG_ReadLittleLong(sb);
+                       if (out->children[0] >= count)
+                               Host_Error("%s: Corrupt clipping hull (invalid child index)", loadmodel->name);
+                       if (out->children[1] >= count)
+                               Host_Error("%s: Corrupt clipping hull (invalid child index)", loadmodel->name);
+               }
+               else
+               {
+                       // LordHavoc: this code supports arguire qbsp's broken clipnodes indices (more than 32768 clipnodes), values above count are assumed to be contents values
+                       out->children[0] = (unsigned short)MSG_ReadLittleShort(sb);
+                       out->children[1] = (unsigned short)MSG_ReadLittleShort(sb);
+                       if (out->children[0] >= count)
+                               out->children[0] -= 65536;
+                       if (out->children[1] >= count)
+                               out->children[1] -= 65536;
+               }
        }
 }
 
@@ -2990,62 +3068,71 @@ static void Mod_Q1BSP_MakeHull0(void)
        }
 }
 
-static void Mod_Q1BSP_LoadLeaffaces(lump_t *l)
+static void Mod_Q1BSP_LoadLeaffaces(sizebuf_t *sb)
 {
        int i, j;
-       short *in;
+       size_t structsize = loadmodel->brush.isbsp2 ? 4 : 2;
 
-       in = (short *)(mod_base + l->fileofs);
-       if (l->filelen % sizeof(*in))
+       if (sb->cursize % structsize)
                Host_Error("Mod_Q1BSP_LoadLeaffaces: funny lump size in %s",loadmodel->name);
-       loadmodel->brush.num_leafsurfaces = l->filelen / sizeof(*in);
+       loadmodel->brush.num_leafsurfaces = sb->cursize / structsize;
        loadmodel->brush.data_leafsurfaces = (int *)Mem_Alloc(loadmodel->mempool, loadmodel->brush.num_leafsurfaces * sizeof(int));
 
-       for (i = 0;i < loadmodel->brush.num_leafsurfaces;i++)
+       if (loadmodel->brush.isbsp2)
+       {
+               for (i = 0;i < loadmodel->brush.num_leafsurfaces;i++)
+               {
+                       j = MSG_ReadLittleLong(sb);
+                       if (j < 0 || j >= loadmodel->num_surfaces)
+                               Host_Error("Mod_Q1BSP_LoadLeaffaces: bad surface number");
+                       loadmodel->brush.data_leafsurfaces[i] = j;
+               }
+       }
+       else
        {
-               j = (unsigned short) LittleShort(in[i]);
-               if (j >= loadmodel->num_surfaces)
-                       Host_Error("Mod_Q1BSP_LoadLeaffaces: bad surface number");
-               loadmodel->brush.data_leafsurfaces[i] = j;
+               for (i = 0;i < loadmodel->brush.num_leafsurfaces;i++)
+               {
+                       j = (unsigned short) MSG_ReadLittleShort(sb);
+                       if (j >= loadmodel->num_surfaces)
+                               Host_Error("Mod_Q1BSP_LoadLeaffaces: bad surface number");
+                       loadmodel->brush.data_leafsurfaces[i] = j;
+               }
        }
 }
 
-static void Mod_Q1BSP_LoadSurfedges(lump_t *l)
+static void Mod_Q1BSP_LoadSurfedges(sizebuf_t *sb)
 {
        int             i;
-       int             *in;
+       size_t structsize = 4;
 
-       in = (int *)(mod_base + l->fileofs);
-       if (l->filelen % sizeof(*in))
+       if (sb->cursize % structsize)
                Host_Error("Mod_Q1BSP_LoadSurfedges: funny lump size in %s",loadmodel->name);
-       loadmodel->brushq1.numsurfedges = l->filelen / sizeof(*in);
+       loadmodel->brushq1.numsurfedges = sb->cursize / structsize;
        loadmodel->brushq1.surfedges = (int *)Mem_Alloc(loadmodel->mempool, loadmodel->brushq1.numsurfedges * sizeof(int));
 
        for (i = 0;i < loadmodel->brushq1.numsurfedges;i++)
-               loadmodel->brushq1.surfedges[i] = LittleLong(in[i]);
+               loadmodel->brushq1.surfedges[i] = MSG_ReadLittleLong(sb);
 }
 
 
-static void Mod_Q1BSP_LoadPlanes(lump_t *l)
+static void Mod_Q1BSP_LoadPlanes(sizebuf_t *sb)
 {
        int                     i;
        mplane_t        *out;
-       dplane_t        *in;
+       size_t structsize = 20;
 
-       in = (dplane_t *)(mod_base + l->fileofs);
-       if (l->filelen % sizeof(*in))
+       if (sb->cursize % structsize)
                Host_Error("Mod_Q1BSP_LoadPlanes: funny lump size in %s", loadmodel->name);
-
-       loadmodel->brush.num_planes = l->filelen / sizeof(*in);
+       loadmodel->brush.num_planes = sb->cursize / structsize;
        loadmodel->brush.data_planes = out = (mplane_t *)Mem_Alloc(loadmodel->mempool, loadmodel->brush.num_planes * sizeof(*out));
 
-       for (i = 0;i < loadmodel->brush.num_planes;i++, in++, out++)
+       for (i = 0;i < loadmodel->brush.num_planes;i++, out++)
        {
-               out->normal[0] = LittleFloat(in->normal[0]);
-               out->normal[1] = LittleFloat(in->normal[1]);
-               out->normal[2] = LittleFloat(in->normal[2]);
-               out->dist = LittleFloat(in->dist);
-
+               out->normal[0] = MSG_ReadLittleFloat(sb);
+               out->normal[1] = MSG_ReadLittleFloat(sb);
+               out->normal[2] = MSG_ReadLittleFloat(sb);
+               out->dist = MSG_ReadLittleFloat(sb);
+               MSG_ReadLittleLong(sb); // type is not used, we use PlaneClassify
                PlaneClassify(out);
        }
 }
@@ -3422,7 +3509,7 @@ static void Mod_Q1BSP_RecursiveNodePortals(mnode_t *node)
        // TODO: calculate node bounding boxes during recursion and calculate a maximum plane size accordingly to improve precision (as most maps do not need 1 billion unit plane polygons)
        PolygonD_QuadForPlane(nodeportal->points, nodeportal->plane.normal[0], nodeportal->plane.normal[1], nodeportal->plane.normal[2], nodeportal->plane.dist, 1024.0*1024.0*1024.0);
        nodeportal->numpoints = 4;
-       side = 0;       // shut up compiler warning
+       // side = 0;    // shut up compiler warning -> should be no longer needed, Host_Error is declared noreturn now
        for (portal = (portal_t *)node->portals;portal;portal = portal->next[side])
        {
                clipplane = portal->plane;
@@ -3437,7 +3524,10 @@ static void Mod_Q1BSP_RecursiveNodePortals(mnode_t *node)
                        side = 1;
                }
                else
+               {
                        Host_Error("Mod_Q1BSP_RecursiveNodePortals: mislinked portal");
+                       side = 0; // hush warning
+               }
 
                for (i = 0;i < nodeportal->numpoints*3;i++)
                        frontpoints[i] = nodeportal->points[i];
@@ -3470,7 +3560,10 @@ static void Mod_Q1BSP_RecursiveNodePortals(mnode_t *node)
                else if (portal->nodes[1] == node)
                        side = 1;
                else
+               {
                        Host_Error("Mod_Q1BSP_RecursiveNodePortals: mislinked portal");
+                       side = 0; // hush warning
+               }
                nextportal = portal->next[side];
                if (!portal->numpoints)
                        continue;
@@ -3663,33 +3756,52 @@ void Mod_CollisionBIH_TraceLineAgainstSurfaces(dp_model_t *model, const frameble
 void Mod_Q1BSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
 {
        int i, j, k;
-       dheader_t *header;
-       dmodel_t *bm;
+       sizebuf_t lumpsb[HEADER_LUMPS];
+       mmodel_t *bm;
        float dist, modelyawradius, modelradius;
        msurface_t *surface;
        hullinfo_t hullinfo;
        int totalstylesurfaces, totalstyles, stylecounts[256], remapstyles[256];
        model_brush_lightstyleinfo_t styleinfo[256];
        unsigned char *datapointer;
+       sizebuf_t sb;
 
-       mod->modeldatatypestring = "Q1BSP";
+       MSG_InitReadBuffer(&sb, (unsigned char *)buffer, (unsigned char *)bufferend - (unsigned char *)buffer);
 
        mod->type = mod_brushq1;
 
-       header = (dheader_t *)buffer;
-
-       i = LittleLong(header->version);
-       if (i != BSPVERSION && i != 30)
-               Host_Error("Mod_Q1BSP_Load: %s has wrong version number(%i should be %i(Quake) or 30(HalfLife)", mod->name, i, BSPVERSION);
-       mod->brush.ishlbsp = i == 30;
+       mod->brush.isbsp2 = false;
+       mod->brush.ishlbsp = false;
+       i = MSG_ReadLittleLong(&sb);
+       switch(i)
+       {
+       case BSPVERSION:
+               mod->modeldatatypestring = "Q1BSP";
+               break;
+       case 30:
+               mod->brush.ishlbsp = true;
+               mod->modeldatatypestring = "HLBSP";
+               break;
+       case ('2' + 'P' * 256 + 'S' * 65536 + 'B' * 16777216):
+               mod->brush.isbsp2 = true;
+               mod->brush.isbsp2rmqe = true; // like bsp2 except leaf/node bounds are 16bit (unexpanded)
+               mod->modeldatatypestring = "Q1BSP2rmqe";
+               break;
+       case ('B' + 'S' * 256 + 'P' * 65536 + '2' * 16777216):
+               mod->brush.isbsp2 = true;
+               mod->modeldatatypestring = "Q1BSP2";
+               break;
+       default:
+               mod->modeldatatypestring = "Unknown BSP";
+               Host_Error("Mod_Q1BSP_Load: %s has wrong version number %i: supported versions are 29 (Quake), 30 (Half-Life), \"BSP2\" or \"2PSB\" (rmqe)", mod->name, i);
+               return;
+       }
 
 // fill in hull info
        VectorClear (hullinfo.hullsizes[0][0]);
        VectorClear (hullinfo.hullsizes[0][1]);
        if (mod->brush.ishlbsp)
        {
-               mod->modeldatatypestring = "HLBSP";
-
                hullinfo.filehulls = 4;
                VectorSet (hullinfo.hullsizes[1][0], -16, -16, -36);
                VectorSet (hullinfo.hullsizes[1][1], 16, 16, 36);
@@ -3708,19 +3820,18 @@ void Mod_Q1BSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
        }
 
 // read lumps
-       mod_base = (unsigned char*)buffer;
        for (i = 0; i < HEADER_LUMPS; i++)
        {
-               header->lumps[i].fileofs = LittleLong(header->lumps[i].fileofs);
-               header->lumps[i].filelen = LittleLong(header->lumps[i].filelen);
+               int offset = MSG_ReadLittleLong(&sb);
+               int size = MSG_ReadLittleLong(&sb);
+               if (offset < 0 || offset + size > sb.cursize)
+                       Host_Error("Mod_Q1BSP_Load: %s has invalid lump %i (offset %i, size %i, file size %i)\n", mod->name, i, offset, size, (int)sb.cursize);
+               MSG_InitReadBuffer(&lumpsb[i], sb.data + offset, size);
        }
 
        mod->soundfromcenter = true;
        mod->TraceBox = Mod_Q1BSP_TraceBox;
-       if (sv_gameplayfix_q1bsptracelinereportstexture.integer)
-               mod->TraceLine = Mod_Q1BSP_TraceLineAgainstSurfaces; // LordHavoc: use the surface-hitting version of TraceLine in all cases
-       else
-               mod->TraceLine = Mod_Q1BSP_TraceLine;
+       mod->TraceLine = Mod_Q1BSP_TraceLine;
        mod->TracePoint = Mod_Q1BSP_TracePoint;
        mod->PointSuperContents = Mod_Q1BSP_PointSuperContents;
        mod->TraceLineAgainstSurfaces = Mod_Q1BSP_TraceLineAgainstSurfaces;
@@ -3758,30 +3869,33 @@ void Mod_Q1BSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
                int temp;
                if (i == LUMP_ENTITIES)
                        continue;
-               temp = Com_BlockChecksum(mod_base + header->lumps[i].fileofs, header->lumps[i].filelen);
+               temp = Com_BlockChecksum(lumpsb[i].data, lumpsb[i].cursize);
                mod->brush.qw_md4sum ^= LittleLong(temp);
                if (i == LUMP_VISIBILITY || i == LUMP_LEAFS || i == LUMP_NODES)
                        continue;
-               temp = Com_BlockChecksum(mod_base + header->lumps[i].fileofs, header->lumps[i].filelen);
                mod->brush.qw_md4sum2 ^= LittleLong(temp);
        }
 
-       Mod_Q1BSP_LoadEntities(&header->lumps[LUMP_ENTITIES]);
-       Mod_Q1BSP_LoadVertexes(&header->lumps[LUMP_VERTEXES]);
-       Mod_Q1BSP_LoadEdges(&header->lumps[LUMP_EDGES]);
-       Mod_Q1BSP_LoadSurfedges(&header->lumps[LUMP_SURFEDGES]);
-       Mod_Q1BSP_LoadTextures(&header->lumps[LUMP_TEXTURES]);
-       Mod_Q1BSP_LoadLighting(&header->lumps[LUMP_LIGHTING]);
-       Mod_Q1BSP_LoadPlanes(&header->lumps[LUMP_PLANES]);
-       Mod_Q1BSP_LoadTexinfo(&header->lumps[LUMP_TEXINFO]);
-       Mod_Q1BSP_LoadFaces(&header->lumps[LUMP_FACES]);
-       Mod_Q1BSP_LoadLeaffaces(&header->lumps[LUMP_MARKSURFACES]);
-       Mod_Q1BSP_LoadVisibility(&header->lumps[LUMP_VISIBILITY]);
+       Mod_Q1BSP_LoadEntities(&lumpsb[LUMP_ENTITIES]);
+       Mod_Q1BSP_LoadVertexes(&lumpsb[LUMP_VERTEXES]);
+       Mod_Q1BSP_LoadEdges(&lumpsb[LUMP_EDGES]);
+       Mod_Q1BSP_LoadSurfedges(&lumpsb[LUMP_SURFEDGES]);
+       Mod_Q1BSP_LoadTextures(&lumpsb[LUMP_TEXTURES]);
+       Mod_Q1BSP_LoadLighting(&lumpsb[LUMP_LIGHTING]);
+       Mod_Q1BSP_LoadPlanes(&lumpsb[LUMP_PLANES]);
+       Mod_Q1BSP_LoadTexinfo(&lumpsb[LUMP_TEXINFO]);
+       Mod_Q1BSP_LoadFaces(&lumpsb[LUMP_FACES]);
+       Mod_Q1BSP_LoadLeaffaces(&lumpsb[LUMP_MARKSURFACES]);
+       Mod_Q1BSP_LoadVisibility(&lumpsb[LUMP_VISIBILITY]);
        // load submodels before leafs because they contain the number of vis leafs
-       Mod_Q1BSP_LoadSubmodels(&header->lumps[LUMP_MODELS], &hullinfo);
-       Mod_Q1BSP_LoadLeafs(&header->lumps[LUMP_LEAFS]);
-       Mod_Q1BSP_LoadNodes(&header->lumps[LUMP_NODES]);
-       Mod_Q1BSP_LoadClipnodes(&header->lumps[LUMP_CLIPNODES], &hullinfo);
+       Mod_Q1BSP_LoadSubmodels(&lumpsb[LUMP_MODELS], &hullinfo);
+       Mod_Q1BSP_LoadLeafs(&lumpsb[LUMP_LEAFS]);
+       Mod_Q1BSP_LoadNodes(&lumpsb[LUMP_NODES]);
+       Mod_Q1BSP_LoadClipnodes(&lumpsb[LUMP_CLIPNODES], &hullinfo);
+
+       for (i = 0; i < HEADER_LUMPS; i++)
+               if (lumpsb[i].readcount != lumpsb[i].cursize && i != LUMP_TEXTURES && i != LUMP_LIGHTING)
+                       Host_Error("Lump %i incorrectly loaded (readcount %i, size %i)\n", i, lumpsb[i].readcount, lumpsb[i].cursize);
 
        // check if the map supports transparent water rendering
        loadmodel->brush.supportwateralpha = Mod_Q1BSP_CheckWaterAlphaSupport();
@@ -4736,13 +4850,10 @@ static void Mod_Q3BSP_LoadVertices(lump_t *l)
                        // working like this may be odd, but matches q3map2 -gamma 2.2
                        if(vid_sRGB.integer && vid_sRGB_fallback.integer && !vid.sRGB3D)
                        {
-                               // actually we do: Image_sRGBFloatFromLinear_Lightmap(Image_LinearFloatFromsRGBFloat(x))
-                               // neutral point is at Image_sRGBFloatFromLinearFloat(0.5)
-                               // so we need to map Image_sRGBFloatFromLinearFloat(0.5) to 0.5
-                               // factor is 0.5 / Image_sRGBFloatFromLinearFloat(0.5)
-                               loadmodel->brushq3.data_color4f[i * 4 + 0] = in->color4ub[0] * (1.0f / 255.0f) * 0.679942f; // fixes neutral level
-                               loadmodel->brushq3.data_color4f[i * 4 + 1] = in->color4ub[1] * (1.0f / 255.0f) * 0.679942f; // fixes neutral level
-                               loadmodel->brushq3.data_color4f[i * 4 + 2] = in->color4ub[2] * (1.0f / 255.0f) * 0.679942f; // fixes neutral level
+                               loadmodel->brushq3.data_color4f[i * 4 + 0] = in->color4ub[0] * (1.0f / 255.0f);
+                               loadmodel->brushq3.data_color4f[i * 4 + 1] = in->color4ub[1] * (1.0f / 255.0f);
+                               loadmodel->brushq3.data_color4f[i * 4 + 2] = in->color4ub[2] * (1.0f / 255.0f);
+                               // we fix the brightness consistently via lightmapscale
                        }
                        else
                        {
@@ -5056,20 +5167,8 @@ static void Mod_Q3BSP_LoadLightmaps(lump_t *l, lump_t *faceslump)
                                        textype_t t;
                                        if(vid_sRGB.integer && vid_sRGB_fallback.integer && !vid.sRGB3D)
                                        {
-                                               // TODO (should we do this, or should we instead knowingly render brighter in sRGB fallback mode?)
-                                               int n = mergedwidth * mergedheight * 4;
-                                               int i;
-                                               for(i = 0; i < n; i += 4)
-                                               {
-                                                       // actually we do: Image_sRGBFloatFromLinear_Lightmap(Image_LinearFloatFromsRGBFloat(x))
-                                                       // neutral point is at Image_sRGBFloatFromLinearFloat(0.5)
-                                                       // so we need to map Image_sRGBFloatFromLinearFloat(0.5) to 0.5
-                                                       // factor is 0.5 / Image_sRGBFloatFromLinearFloat(0.5)
-                                                       mergedpixels[i+0] = (mergedpixels[i+0] * (int)173 + 128) / 255;
-                                                       mergedpixels[i+1] = (mergedpixels[i+1] * (int)173 + 128) / 255;
-                                                       mergedpixels[i+2] = (mergedpixels[i+2] * (int)173 + 128) / 255;
-                                               }
                                                t = TEXTYPE_BGRA; // in stupid fallback mode, we upload lightmaps in sRGB form and just fix their brightness
+                                               // we fix the brightness consistently via lightmapscale
                                        }
                                        else
                                                t = TEXTYPE_SRGB_BGRA; // normally, we upload lightmaps in sRGB form (possibly downconverted to linear)
@@ -5872,20 +5971,7 @@ static void Mod_Q3BSP_LoadLightGrid(lump_t *l)
                {
                        if(vid_sRGB.integer && vid_sRGB_fallback.integer && !vid.sRGB3D)
                        {
-                               // TODO (should we do this, or should we instead knowingly render brighter in sRGB fallback mode?)
-                               for(i = 0; i < count; ++i)
-                               {
-                                       // actually we do: Image_sRGBFloatFromLinear_Lightmap(Image_LinearFloatFromsRGBFloat(x))
-                                       // neutral point is at Image_sRGBFloatFromLinearFloat(0.5)
-                                       // so we need to map Image_sRGBFloatFromLinearFloat(0.5) to 0.5
-                                       // factor is 0.5 / Image_sRGBFloatFromLinearFloat(0.5)
-                                       out[i].ambientrgb[0] = (out[i].ambientrgb[0] * (int)173 + 128) / 255; // fixes neutral level
-                                       out[i].ambientrgb[1] = (out[i].ambientrgb[1] * (int)173 + 128) / 255; // fixes neutral level
-                                       out[i].ambientrgb[2] = (out[i].ambientrgb[2] * (int)173 + 128) / 255; // fixes neutral level
-                                       out[i].diffusergb[0] = (out[i].diffusergb[0] * (int)173 + 128) / 255; // fixes neutral level
-                                       out[i].diffusergb[1] = (out[i].diffusergb[1] * (int)173 + 128) / 255; // fixes neutral level
-                                       out[i].diffusergb[2] = (out[i].diffusergb[2] * (int)173 + 128) / 255; // fixes neutral level
-                               }
+                               // we fix the brightness consistently via lightmapscale
                        }
                        else
                        {
@@ -7323,6 +7409,30 @@ static void Mod_Q3BSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
                        Mod_BuildVBOs();
        }
 
+       if (mod_q3bsp_sRGBlightmaps.integer)
+       {
+               if (vid_sRGB.integer && vid_sRGB_fallback.integer && !vid.sRGB3D)
+               {
+                       // actually we do in sRGB fallback with sRGB lightmaps: Image_sRGBFloatFromLinear_Lightmap(Image_LinearFloatFromsRGBFloat(x))
+                       // neutral point is at Image_sRGBFloatFromLinearFloat(0.5)
+                       // so we need to map Image_sRGBFloatFromLinearFloat(0.5) to 0.5
+                       // factor is 0.5 / Image_sRGBFloatFromLinearFloat(0.5)
+                       //loadmodel->lightmapscale *= 0.679942f; // fixes neutral level
+               }
+               else // if this is NOT set, regular rendering looks right by this requirement anyway
+               {
+                       /*
+                       // we want color 1 to do the same as without sRGB
+                       // so, we want to map 1 to Image_LinearFloatFromsRGBFloat(2) instead of to 2
+                       loadmodel->lightmapscale *= 2.476923f; // fixes max level
+                       */
+
+                       // neutral level 0.5 gets uploaded as sRGB and becomes Image_LinearFloatFromsRGBFloat(0.5)
+                       // we need to undo that
+                       loadmodel->lightmapscale *= 2.336f; // fixes neutral level
+               }
+       }
+
        Con_DPrintf("Stats for q3bsp model \"%s\": %i faces, %i nodes, %i leafs, %i clusters, %i clusterportals, mesh: %i vertices, %i triangles, %i surfaces\n", loadmodel->name, loadmodel->num_surfaces, loadmodel->brush.num_nodes, loadmodel->brush.num_leafs, mod->brush.num_pvsclusters, loadmodel->brush.num_portals, loadmodel->surfmesh.num_vertices, loadmodel->surfmesh.num_triangles, loadmodel->num_surfaces);
 }
 
@@ -7501,9 +7611,18 @@ void Mod_OBJ_Load(dp_model_t *mod, void *buffer, void *bufferend)
                                maxv = max(maxv * 2, 1024);
                                v = (float *)Mem_Realloc(tempmempool, v, maxv * sizeof(float[3]));
                        }
-                       v[numv*3+0] = atof(argv[1]);
-                       v[numv*3+2] = atof(argv[2]);
-                       v[numv*3+1] = atof(argv[3]);
+                       if(mod_obj_orientation.integer)
+                       {
+                               v[numv*3+0] = atof(argv[1]);
+                               v[numv*3+2] = atof(argv[2]);
+                               v[numv*3+1] = atof(argv[3]);
+                       }
+                       else
+                       {
+                               v[numv*3+0] = atof(argv[1]);
+                               v[numv*3+1] = atof(argv[2]);
+                               v[numv*3+2] = atof(argv[3]);
+                       }
                        numv++;
                }
                else if (!strcmp(argv[0], "vt"))
@@ -7524,9 +7643,18 @@ void Mod_OBJ_Load(dp_model_t *mod, void *buffer, void *bufferend)
                                maxvn = max(maxvn * 2, 1024);
                                vn = (float *)Mem_Realloc(tempmempool, vn, maxvn * sizeof(float[3]));
                        }
-                       vn[numvn*3+0] = atof(argv[1]);
-                       vn[numvn*3+2] = atof(argv[2]);
-                       vn[numvn*3+1] = atof(argv[3]);
+                       if(mod_obj_orientation.integer)
+                       {
+                               vn[numvn*3+0] = atof(argv[1]);
+                               vn[numvn*3+2] = atof(argv[2]);
+                               vn[numvn*3+1] = atof(argv[3]);
+                       }
+                       else
+                       {
+                               vn[numvn*3+0] = atof(argv[1]);
+                               vn[numvn*3+1] = atof(argv[2]);
+                               vn[numvn*3+2] = atof(argv[3]);
+                       }
                        numvn++;
                }
                else if (!strcmp(argv[0], "f"))
@@ -7595,9 +7723,18 @@ void Mod_OBJ_Load(dp_model_t *mod, void *buffer, void *bufferend)
                                                maxtriangles = max(maxtriangles * 2, 32768);
                                                vertices = (objvertex_t*)Mem_Realloc(loadmodel->mempool, vertices, maxtriangles * sizeof(objvertex_t[3]));
                                        }
-                                       vertices[numtriangles*3+0] = vfirst;
-                                       vertices[numtriangles*3+1] = vprev;
-                                       vertices[numtriangles*3+2] = vcurrent;
+                                       if(mod_obj_orientation.integer)
+                                       {
+                                               vertices[numtriangles*3+0] = vfirst;
+                                               vertices[numtriangles*3+1] = vprev;
+                                               vertices[numtriangles*3+2] = vcurrent;
+                                       }
+                                       else
+                                       {
+                                               vertices[numtriangles*3+0] = vfirst;
+                                               vertices[numtriangles*3+2] = vprev;
+                                               vertices[numtriangles*3+1] = vcurrent;
+                                       }
                                        numtriangles++;
                                }
                                vprev = vcurrent;