]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - model_brush.c
fix a mingw warning
[xonotic/darkplaces.git] / model_brush.c
index e62037231e962cd64e690c435958dbf261e30480..46515fcf72c3934d3f0775756c2b546938bde9ec 100644 (file)
@@ -28,7 +28,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 //cvar_t r_subdivide_size = {CVAR_SAVE, "r_subdivide_size", "128", "how large water polygons should be (smaller values produce more polygons which give better warping effects)"};
 cvar_t r_novis = {0, "r_novis", "0", "draws whole level, see also sv_cullentities_pvs 0"};
-cvar_t r_picmipworld = {CVAR_SAVE, "r_picmipworld", "1", "whether gl_picmip shall apply to world textures too"};
 cvar_t r_nosurftextures = {0, "r_nosurftextures", "0", "pretends there was no texture lump found in the q1bsp/hlbsp loading (useful for debugging this rare case)"};
 cvar_t r_subdivisions_tolerance = {0, "r_subdivisions_tolerance", "4", "maximum error tolerance on curve subdivision for rendering purposes (in other words, the curves will be given as many polygons as necessary to represent curves at this quality)"};
 cvar_t r_subdivisions_mintess = {0, "r_subdivisions_mintess", "0", "minimum number of subdivisions (values above 0 will smooth curves that don't need it)"};
@@ -62,7 +61,6 @@ void Mod_BrushInit(void)
 {
 //     Cvar_RegisterVariable(&r_subdivide_size);
        Cvar_RegisterVariable(&r_novis);
-       Cvar_RegisterVariable(&r_picmipworld);
        Cvar_RegisterVariable(&r_nosurftextures);
        Cvar_RegisterVariable(&r_subdivisions_tolerance);
        Cvar_RegisterVariable(&r_subdivisions_mintess);
@@ -1335,8 +1333,8 @@ void R_Q1BSP_LoadSplitSky (unsigned char *src, int width, int height, int bytesp
        int x, y;
        int w = width/2;
        int h = height;
-       unsigned *solidpixels = Mem_Alloc(tempmempool, w*h*sizeof(unsigned char[4]));
-       unsigned *alphapixels = Mem_Alloc(tempmempool, w*h*sizeof(unsigned char[4]));
+       unsigned int *solidpixels = (unsigned int *)Mem_Alloc(tempmempool, w*h*sizeof(unsigned char[4]));
+       unsigned int *alphapixels = (unsigned int *)Mem_Alloc(tempmempool, w*h*sizeof(unsigned char[4]));
 
        // allocate a texture pool if we need it
        if (loadmodel->texturepool == NULL && cls.state != ca_dedicated)
@@ -1578,7 +1576,9 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
                        // LordHavoc: HL sky textures are entirely different than quake
                        if (!loadmodel->brush.ishlbsp && !strncmp(tx->name, "sky", 3) && mtwidth == mtheight * 2)
                        {
-                               data = loadimagepixelsbgra(tx->name, false, false, r_texture_convertsRGB_skin.integer);
+                               data = loadimagepixelsbgra(gamemode == GAME_TENEBRAE ? tx->name : va("textures/%s/%s", mapname, tx->name), false, false, r_texture_convertsRGB_skin.integer != 0, NULL);
+                               if (!data)
+                                       data = loadimagepixelsbgra(gamemode == GAME_TENEBRAE ? tx->name : va("textures/%s", tx->name), false, false, r_texture_convertsRGB_skin.integer != 0, NULL);
                                if (data && image_width == image_height * 2)
                                {
                                        R_Q1BSP_LoadSplitSky(data, image_width, image_height, 4);
@@ -1589,9 +1589,9 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
                        }
                        else
                        {
-                               skinframe = R_SkinFrame_LoadExternal(gamemode == GAME_TENEBRAE ? tx->name : va("textures/%s/%s", mapname, tx->name), TEXF_ALPHA | TEXF_MIPMAP | (r_picmipworld.integer ? TEXF_PICMIP : 0) | TEXF_COMPRESS, false);
+                               skinframe = R_SkinFrame_LoadExternal(gamemode == GAME_TENEBRAE ? tx->name : va("textures/%s/%s", mapname, tx->name), TEXF_ALPHA | TEXF_MIPMAP | TEXF_ISWORLD | TEXF_PICMIP | TEXF_COMPRESS, false);
                                if (!skinframe)
-                                       skinframe = R_SkinFrame_LoadExternal(gamemode == GAME_TENEBRAE ? tx->name : va("textures/%s", tx->name), TEXF_ALPHA | TEXF_MIPMAP | (r_picmipworld.integer ? TEXF_PICMIP : 0) | TEXF_COMPRESS, false);
+                                       skinframe = R_SkinFrame_LoadExternal(gamemode == GAME_TENEBRAE ? tx->name : va("textures/%s", tx->name), TEXF_ALPHA | TEXF_MIPMAP | TEXF_ISWORLD | TEXF_PICMIP | TEXF_COMPRESS, false);
                                if (!skinframe)
                                {
                                        // did not find external texture, load it from the bsp or wad3
@@ -1608,13 +1608,13 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
                                                {
                                                        tx->width = image_width;
                                                        tx->height = image_height;
-                                                       skinframe = R_SkinFrame_LoadInternalBGRA(tx->name, TEXF_ALPHA | TEXF_MIPMAP | (r_picmipworld.integer ? TEXF_PICMIP : 0), pixels, image_width, image_height);
+                                                       skinframe = R_SkinFrame_LoadInternalBGRA(tx->name, TEXF_ALPHA | TEXF_MIPMAP | TEXF_ISWORLD | TEXF_PICMIP, pixels, image_width, image_height);
                                                }
                                                if (freepixels)
                                                        Mem_Free(freepixels);
                                        }
                                        else if (mtdata) // texture included
-                                               skinframe = R_SkinFrame_LoadInternalQuake(tx->name, TEXF_MIPMAP | (r_picmipworld.integer ? TEXF_PICMIP : 0), false, r_fullbrights.integer, mtdata, tx->width, tx->height);
+                                               skinframe = R_SkinFrame_LoadInternalQuake(tx->name, TEXF_MIPMAP | TEXF_ISWORLD | TEXF_PICMIP, false, r_fullbrights.integer, mtdata, tx->width, tx->height);
                                }
                                // if skinframe is still NULL the "missing" texture will be used
                                if (skinframe)
@@ -2454,11 +2454,11 @@ static void Mod_Q1BSP_LoadFaces(lump_t *l)
                                        loadmodel->texturepool = R_AllocTexturePool();
                                // could not find room, make a new lightmap
                                loadmodel->brushq3.num_mergedlightmaps = lightmapnumber + 1;
-                               loadmodel->brushq3.data_lightmaps = Mem_Realloc(loadmodel->mempool, loadmodel->brushq3.data_lightmaps, loadmodel->brushq3.num_mergedlightmaps * sizeof(loadmodel->brushq3.data_lightmaps[0]));
-                               loadmodel->brushq3.data_deluxemaps = Mem_Realloc(loadmodel->mempool, loadmodel->brushq3.data_deluxemaps, loadmodel->brushq3.num_mergedlightmaps * sizeof(loadmodel->brushq3.data_deluxemaps[0]));
-                               loadmodel->brushq3.data_lightmaps[lightmapnumber] = lightmaptexture = R_LoadTexture2D(loadmodel->texturepool, va("lightmap%i", lightmapnumber), lightmapsize, lightmapsize, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_ALLOWUPDATES, NULL);
+                               loadmodel->brushq3.data_lightmaps = (rtexture_t **)Mem_Realloc(loadmodel->mempool, loadmodel->brushq3.data_lightmaps, loadmodel->brushq3.num_mergedlightmaps * sizeof(loadmodel->brushq3.data_lightmaps[0]));
+                               loadmodel->brushq3.data_deluxemaps = (rtexture_t **)Mem_Realloc(loadmodel->mempool, loadmodel->brushq3.data_deluxemaps, loadmodel->brushq3.num_mergedlightmaps * sizeof(loadmodel->brushq3.data_deluxemaps[0]));
+                               loadmodel->brushq3.data_lightmaps[lightmapnumber] = lightmaptexture = R_LoadTexture2D(loadmodel->texturepool, va("lightmap%i", lightmapnumber), lightmapsize, lightmapsize, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_ALLOWUPDATES, -1, NULL);
                                if (loadmodel->brushq1.nmaplightdata)
-                                       loadmodel->brushq3.data_deluxemaps[lightmapnumber] = deluxemaptexture = R_LoadTexture2D(loadmodel->texturepool, va("deluxemap%i", lightmapnumber), lightmapsize, lightmapsize, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_ALLOWUPDATES, NULL);
+                                       loadmodel->brushq3.data_deluxemaps[lightmapnumber] = deluxemaptexture = R_LoadTexture2D(loadmodel->texturepool, va("deluxemap%i", lightmapnumber), lightmapsize, lightmapsize, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_ALLOWUPDATES, -1, NULL);
                                lightmapnumber++;
                                Mod_AllocLightmap_Reset(&allocState);
                                Mod_AllocLightmap_Block(&allocState, ssize, tsize, &lightmapx, &lightmapy);
@@ -3182,7 +3182,7 @@ static void Mod_Q1BSP_RecursiveNodePortals(mnode_t *node)
        if (portalpointsbuffersize < portalpointsbufferoffset + 6*MAX_PORTALPOINTS)
        {
                portalpointsbuffersize = portalpointsbufferoffset * 2;
-               portalpointsbuffer = Mem_Realloc(loadmodel->mempool, portalpointsbuffer, portalpointsbuffersize * sizeof(*portalpointsbuffer));
+               portalpointsbuffer = (double *)Mem_Realloc(loadmodel->mempool, portalpointsbuffer, portalpointsbuffersize * sizeof(*portalpointsbuffer));
        }
        frontpoints = portalpointsbuffer + portalpointsbufferoffset;
        portalpointsbufferoffset += 3*MAX_PORTALPOINTS;
@@ -3315,7 +3315,7 @@ static void Mod_Q1BSP_MakePortals(void)
        Mem_ExpandableArray_NewArray(&portalarray, loadmodel->mempool, sizeof(portal_t), 1020*1024/sizeof(portal_t));
        portalpointsbufferoffset = 0;
        portalpointsbuffersize = 6*MAX_PORTALPOINTS*128;
-       portalpointsbuffer = Mem_Alloc(loadmodel->mempool, portalpointsbuffersize * sizeof(*portalpointsbuffer));
+       portalpointsbuffer = (double *)Mem_Alloc(loadmodel->mempool, portalpointsbuffersize * sizeof(*portalpointsbuffer));
        Mod_Q1BSP_RecursiveNodePortals(loadmodel->brush.data_nodes + loadmodel->brushq1.hulls[0].firstclipnode);
        Mem_Free(portalpointsbuffer);
        portalpointsbuffer = NULL;
@@ -3686,7 +3686,7 @@ void Mod_Q1BSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
                                mod->DrawSky = R_Q1BSP_DrawSky;
 
                        for (j = 0, surface = &mod->data_surfaces[mod->firstmodelsurface];j < mod->nummodelsurfaces;j++, surface++)
-                               if (surface->texture->basematerialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION))
+                               if (surface->texture->basematerialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA))
                                        break;
                        if (j < mod->nummodelsurfaces)
                                mod->DrawAddWaterPlanes = R_Q1BSP_DrawAddWaterPlanes;
@@ -3738,11 +3738,13 @@ void Mod_Q1BSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
 
                if (mod_q1bsp_polygoncollisions.integer)
                {
-                       Mod_MakeCollisionBIH(mod, true);
+                       Mod_MakeCollisionBIH(mod, true, &mod->collision_bih);
                        // point traces and contents checks still use the bsp tree
                        mod->TraceLine = Mod_CollisionBIH_TraceLine;
                        mod->TraceBox = Mod_CollisionBIH_TraceBox;
                }
+               else
+                       Mod_MakeCollisionBIH(mod, true, &mod->render_bih);
 
                // generate VBOs and other shared data before cloning submodels
                if (i == 0)
@@ -4281,13 +4283,10 @@ static void Mod_Q3BSP_LoadTextures(lump_t *l)
                strlcpy (out[i].name, in[i].name, sizeof (out[i].name));
                out[i].surfaceflags = LittleLong(in[i].surfaceflags);
                out[i].supercontents = Mod_Q3BSP_SuperContentsFromNativeContents(loadmodel, LittleLong(in[i].contents));
-               if (cls.state != ca_dedicated)
-               {
-                       Mod_LoadTextureFromQ3Shader(out + i, out[i].name, true, true, TEXF_MIPMAP | (r_picmipworld.integer ? TEXF_PICMIP : 0) | TEXF_COMPRESS);
-                       // restore the surfaceflags and supercontents
-                       out[i].surfaceflags = LittleLong(in[i].surfaceflags);
-                       out[i].supercontents = Mod_Q3BSP_SuperContentsFromNativeContents(loadmodel, LittleLong(in[i].contents));
-               }
+               Mod_LoadTextureFromQ3Shader(out + i, out[i].name, true, true, TEXF_MIPMAP | TEXF_ISWORLD | TEXF_PICMIP | TEXF_COMPRESS);
+               // restore the surfaceflags and supercontents
+               out[i].surfaceflags = LittleLong(in[i].surfaceflags);
+               out[i].supercontents = Mod_Q3BSP_SuperContentsFromNativeContents(loadmodel, LittleLong(in[i].contents));
        }
 }
 
@@ -4576,7 +4575,7 @@ static void Mod_Q3BSP_LoadLightmaps(lump_t *l, lump_t *faceslump)
                if (developer_loading.integer)
                        Con_Printf("Using external lightmaps\n");
                FS_StripExtension(loadmodel->name, mapname, sizeof(mapname));
-               inpixels[0] = loadimagepixelsbgra(va("%s/lm_%04d", mapname, 0), false, false, false);
+               inpixels[0] = loadimagepixelsbgra(va("%s/lm_%04d", mapname, 0), false, false, false, NULL);
                if(!inpixels[0])
                        return;
 
@@ -4596,7 +4595,7 @@ static void Mod_Q3BSP_LoadLightmaps(lump_t *l, lump_t *faceslump)
 
                for(count = 1; ; ++count)
                {
-                       inpixels[count] = loadimagepixelsbgra(va("%s/lm_%04d", mapname, count), false, false, false);
+                       inpixels[count] = loadimagepixelsbgra(va("%s/lm_%04d", mapname, count), false, false, false, NULL);
                        if(!inpixels[count])
                                break; // we got all of them
                        if(image_width != size || image_height != size)
@@ -4730,9 +4729,9 @@ static void Mod_Q3BSP_LoadLightmaps(lump_t *l, lump_t *faceslump)
                                        ;
                                if (developer_loading.integer)
                                        Con_Printf("lightmap merge texture #%i is %ix%i (%i of %i used)\n", lightmapindex, mergewidth*size, mergeheight*size, min(j, mergewidth*mergeheight), mergewidth*mergeheight);
-                               loadmodel->brushq3.data_lightmaps[lightmapindex] = R_LoadTexture2D(loadmodel->texturepool, va("lightmap%04i", lightmapindex), mergewidth * size, mergeheight * size, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | (gl_texturecompression_q3bsplightmaps.integer ? TEXF_COMPRESS : TEXF_ALLOWUPDATES), NULL);
+                               loadmodel->brushq3.data_lightmaps[lightmapindex] = R_LoadTexture2D(loadmodel->texturepool, va("lightmap%04i", lightmapindex), mergewidth * size, mergeheight * size, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | (gl_texturecompression_q3bsplightmaps.integer ? TEXF_COMPRESS : TEXF_ALLOWUPDATES), -1, NULL);
                                if (loadmodel->brushq3.data_deluxemaps)
-                                       loadmodel->brushq3.data_deluxemaps[lightmapindex] = R_LoadTexture2D(loadmodel->texturepool, va("deluxemap%04i", lightmapindex), mergewidth * size, mergeheight * size, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | (gl_texturecompression_q3bspdeluxemaps.integer ? TEXF_COMPRESS : TEXF_ALLOWUPDATES), NULL);
+                                       loadmodel->brushq3.data_deluxemaps[lightmapindex] = R_LoadTexture2D(loadmodel->texturepool, va("deluxemap%04i", lightmapindex), mergewidth * size, mergeheight * size, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | (gl_texturecompression_q3bspdeluxemaps.integer ? TEXF_COMPRESS : TEXF_ALLOWUPDATES), -1, NULL);
                        }
                        mergewidth = R_TextureWidth(loadmodel->brushq3.data_lightmaps[lightmapindex]) / size;
                        mergeheight = R_TextureHeight(loadmodel->brushq3.data_lightmaps[lightmapindex]) / size;
@@ -4746,9 +4745,9 @@ static void Mod_Q3BSP_LoadLightmaps(lump_t *l, lump_t *faceslump)
                {
                        // figure out which merged lightmap texture this fits into
                        if (loadmodel->brushq3.deluxemapping && (i & 1))
-                               loadmodel->brushq3.data_deluxemaps[lightmapindex] = R_LoadTexture2D(loadmodel->texturepool, va("deluxemap%04i", lightmapindex), size, size, convertedpixels, TEXTYPE_BGRA, TEXF_FORCELINEAR | (gl_texturecompression_q3bspdeluxemaps.integer ? TEXF_COMPRESS : 0), NULL);
+                               loadmodel->brushq3.data_deluxemaps[lightmapindex] = R_LoadTexture2D(loadmodel->texturepool, va("deluxemap%04i", lightmapindex), size, size, convertedpixels, TEXTYPE_BGRA, TEXF_FORCELINEAR | (gl_texturecompression_q3bspdeluxemaps.integer ? TEXF_COMPRESS : 0), -1, NULL);
                        else
-                               loadmodel->brushq3.data_lightmaps [lightmapindex] = R_LoadTexture2D(loadmodel->texturepool, va("lightmap%04i", lightmapindex), size, size, convertedpixels, TEXTYPE_BGRA, TEXF_FORCELINEAR | (gl_texturecompression_q3bsplightmaps.integer ? TEXF_COMPRESS : 0), NULL);
+                               loadmodel->brushq3.data_lightmaps [lightmapindex] = R_LoadTexture2D(loadmodel->texturepool, va("lightmap%04i", lightmapindex), size, size, convertedpixels, TEXTYPE_BGRA, TEXF_FORCELINEAR | (gl_texturecompression_q3bsplightmaps.integer ? TEXF_COMPRESS : 0), -1, NULL);
                }
        }
 
@@ -5056,8 +5055,8 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
        Mod_AllocSurfMesh(loadmodel->mempool, meshvertices, meshtriangles, false, true, false);
        if (collisiontriangles)
        {
-               loadmodel->brush.data_collisionvertex3f = Mem_Alloc(loadmodel->mempool, collisionvertices * sizeof(float[3]));
-               loadmodel->brush.data_collisionelement3i = Mem_Alloc(loadmodel->mempool, collisiontriangles * sizeof(int[3]));
+               loadmodel->brush.data_collisionvertex3f = (float *)Mem_Alloc(loadmodel->mempool, collisionvertices * sizeof(float[3]));
+               loadmodel->brush.data_collisionelement3i = (int *)Mem_Alloc(loadmodel->mempool, collisiontriangles * sizeof(int[3]));
        }
        meshvertices = 0;
        meshtriangles = 0;
@@ -5761,7 +5760,7 @@ static void Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace_t *trace, dp_model
        vec_t backdist1;
        vec_t backdist2;
        vec_t backfrac;
-       vec3_t clipped[2];
+       vec3_t clipped, newstart, newend;
 #endif
        vec3_t segmentmins;
        vec3_t segmentmaxs;
@@ -5826,8 +5825,7 @@ static void Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace_t *trace, dp_model
                        // START end START END
 #ifdef BIHLINECLIP
                        frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       VectorLerp(start, frontfrac, end, clipped[0]);
-                       start = clipped[0];
+                       VectorLerp(start, frontfrac, end, newstart); start = newstart;
                        segmentmins[0] = min(start[0], end[0]);
                        segmentmins[1] = min(start[1], end[1]);
                        segmentmins[2] = min(start[2], end[2]);
@@ -5841,8 +5839,7 @@ static void Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace_t *trace, dp_model
 #ifdef BIHLINECLIP
                        // start END START END
                        frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       VectorLerp(start, frontfrac, end, clipped[0]);
-                       end = clipped[0];
+                       VectorLerp(start, frontfrac, end, newend); end = newend;
                        segmentmins[0] = min(start[0], end[0]);
                        segmentmins[1] = min(start[1], end[1]);
                        segmentmins[2] = min(start[2], end[2]);
@@ -5860,8 +5857,7 @@ static void Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace_t *trace, dp_model
                        Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
 #ifdef BIHLINECLIP
                        backfrac = backdist1 / (backdist1 - backdist2);
-                       VectorLerp(start, backfrac, end, clipped[0]);
-                       end = clipped[0];
+                       VectorLerp(start, backfrac, end, newend); end = newend;
                        segmentmins[0] = min(start[0], end[0]);
                        segmentmins[1] = min(start[1], end[1]);
                        segmentmins[2] = min(start[2], end[2]);
@@ -5875,11 +5871,10 @@ static void Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace_t *trace, dp_model
                        // START end start END
 #ifdef BIHLINECLIP
                        frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       VectorLerp(start, frontfrac, end, clipped[1]);
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, clipped[1], end, linestart, lineend);
+                       VectorLerp(start, frontfrac, end, clipped);
+                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, clipped, end, linestart, lineend);
                        backfrac = backdist1 / (backdist1 - backdist2);
-                       VectorLerp(start, backfrac, end, clipped[0]);
-                       end = clipped[0];
+                       VectorLerp(start, backfrac, end, newend); end = newend;
                        segmentmins[0] = min(start[0], end[0]);
                        segmentmins[1] = min(start[1], end[1]);
                        segmentmins[2] = min(start[2], end[2]);
@@ -5895,11 +5890,10 @@ static void Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace_t *trace, dp_model
                        // start END start END
 #ifdef BIHLINECLIP
                        frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       VectorLerp(start, frontfrac, end, clipped[1]);
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, clipped[1], linestart, lineend);
+                       VectorLerp(start, frontfrac, end, clipped);
+                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, clipped, linestart, lineend);
                        backfrac = backdist1 / (backdist1 - backdist2);
-                       VectorLerp(start, backfrac, end, clipped[0]);
-                       end = clipped[0];
+                       VectorLerp(start, backfrac, end, newend); end = newend;
                        segmentmins[0] = min(start[0], end[0]);
                        segmentmins[1] = min(start[1], end[1]);
                        segmentmins[2] = min(start[2], end[2]);
@@ -5915,8 +5909,7 @@ static void Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace_t *trace, dp_model
                        // START END start END
 #ifdef BIHLINECLIP
                        backfrac = backdist1 / (backdist1 - backdist2);
-                       VectorLerp(start, backfrac, end, clipped[0]);
-                       end = clipped[0];
+                       VectorLerp(start, backfrac, end, newend); end = newend;
                        segmentmins[0] = min(start[0], end[0]);
                        segmentmins[1] = min(start[1], end[1]);
                        segmentmins[2] = min(start[2], end[2]);
@@ -5931,8 +5924,7 @@ static void Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace_t *trace, dp_model
                        Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
 #ifdef BIHLINECLIP
                        backfrac = backdist1 / (backdist1 - backdist2);
-                       VectorLerp(start, backfrac, end, clipped[0]);
-                       start = clipped[0];
+                       VectorLerp(start, backfrac, end, newstart); start = newstart;
                        segmentmins[0] = min(start[0], end[0]);
                        segmentmins[1] = min(start[1], end[1]);
                        segmentmins[2] = min(start[2], end[2]);
@@ -5946,11 +5938,10 @@ static void Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace_t *trace, dp_model
                        // START end START end
 #ifdef BIHLINECLIP
                        frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       VectorLerp(start, frontfrac, end, clipped[1]);
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, clipped[1], end, linestart, lineend);
+                       VectorLerp(start, frontfrac, end, clipped);
+                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, clipped, end, linestart, lineend);
                        backfrac = backdist1 / (backdist1 - backdist2);
-                       VectorLerp(start, backfrac, end, clipped[0]);
-                       start = clipped[0];
+                       VectorLerp(start, backfrac, end, newstart); start = newstart;
                        segmentmins[0] = min(start[0], end[0]);
                        segmentmins[1] = min(start[1], end[1]);
                        segmentmins[2] = min(start[2], end[2]);
@@ -5966,11 +5957,10 @@ static void Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace_t *trace, dp_model
                        // start END START end
 #ifdef BIHLINECLIP
                        frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       VectorLerp(start, frontfrac, end, clipped[1]);
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, clipped[1], linestart, lineend);
+                       VectorLerp(start, frontfrac, end, clipped);
+                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, clipped, linestart, lineend);
                        backfrac = backdist1 / (backdist1 - backdist2);
-                       VectorLerp(start, backfrac, end, clipped[0]);
-                       start = clipped[0];
+                       VectorLerp(start, backfrac, end, newstart); start = newstart;
                        segmentmins[0] = min(start[0], end[0]);
                        segmentmins[1] = min(start[1], end[1]);
                        segmentmins[2] = min(start[2], end[2]);
@@ -5986,8 +5976,7 @@ static void Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace_t *trace, dp_model
                        // START END START end
 #ifdef BIHLINECLIP
                        backfrac = backdist1 / (backdist1 - backdist2);
-                       VectorLerp(start, backfrac, end, clipped[0]);
-                       start = clipped[0];
+                       VectorLerp(start, backfrac, end, newstart); start = newstart;
                        segmentmins[0] = min(start[0], end[0]);
                        segmentmins[1] = min(start[1], end[1]);
                        segmentmins[2] = min(start[2], end[2]);
@@ -6006,8 +5995,8 @@ static void Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace_t *trace, dp_model
                        // START end start end
 #ifdef BIHLINECLIP
                        frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       VectorLerp(start, frontfrac, end, clipped[1]);
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, clipped[1], end, linestart, lineend);
+                       VectorLerp(start, frontfrac, end, clipped);
+                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, clipped, end, linestart, lineend);
 #else
                        Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
 #endif
@@ -6017,8 +6006,8 @@ static void Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace_t *trace, dp_model
                        // start END start end
 #ifdef BIHLINECLIP
                        frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       VectorLerp(start, frontfrac, end, clipped[1]);
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, clipped[1], linestart, lineend);
+                       VectorLerp(start, frontfrac, end, clipped);
+                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, clipped, linestart, lineend);
 #else
                        Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
 #endif
@@ -6602,7 +6591,7 @@ static int Mod_Q3BSP_PointSuperContents(struct model_s *model, int frame, const
        return supercontents;
 }
 
-void Mod_MakeCollisionBIH(dp_model_t *model, qboolean userendersurfaces)
+bih_t *Mod_MakeCollisionBIH(dp_model_t *model, qboolean userendersurfaces, bih_t *out)
 {
        int j;
        int bihnumleafs;
@@ -6639,17 +6628,17 @@ void Mod_MakeCollisionBIH(dp_model_t *model, qboolean userendersurfaces)
                for (j = 0, surface = model->data_surfaces + model->firstmodelsurface;j < nummodelsurfaces;j++, surface++)
                {
                        if (surface->texture->basematerialflags & MATERIALFLAG_MESHCOLLISIONS)
-                               bihnumleafs += surface->num_triangles;
+                               bihnumleafs += surface->num_triangles + surface->num_collisiontriangles;
                        else
                                bihnumleafs += surface->num_collisiontriangles;
                }
        }
 
        if (!bihnumleafs)
-               return;
+               return NULL;
 
        // allocate the memory for the BIH leaf nodes
-       bihleafs = Mem_Alloc(loadmodel->mempool, sizeof(bih_leaf_t) * bihnumleafs);
+       bihleafs = (bih_leaf_t *)Mem_Alloc(loadmodel->mempool, sizeof(bih_leaf_t) * bihnumleafs);
 
        // now populate the BIH leaf nodes
        bihleafindex = 0;
@@ -6665,6 +6654,7 @@ void Mod_MakeCollisionBIH(dp_model_t *model, qboolean userendersurfaces)
                                continue;
                        bihleafs[bihleafindex].type = BIH_RENDERTRIANGLE;
                        bihleafs[bihleafindex].textureindex = surface->texture - model->data_textures;
+                       bihleafs[bihleafindex].surfaceindex = surface - model->data_surfaces;
                        bihleafs[bihleafindex].itemindex = triangleindex+surface->num_firsttriangle;
                        bihleafs[bihleafindex].mins[0] = min(rendervertex3f[3*e[0]+0], min(rendervertex3f[3*e[1]+0], rendervertex3f[3*e[2]+0])) - 1;
                        bihleafs[bihleafindex].mins[1] = min(rendervertex3f[3*e[0]+1], min(rendervertex3f[3*e[1]+1], rendervertex3f[3*e[2]+1])) - 1;
@@ -6685,6 +6675,7 @@ void Mod_MakeCollisionBIH(dp_model_t *model, qboolean userendersurfaces)
                                continue;
                        bihleafs[bihleafindex].type = BIH_BRUSH;
                        bihleafs[bihleafindex].textureindex = brush->texture - model->data_textures;
+                       bihleafs[bihleafindex].surfaceindex = -1;
                        bihleafs[bihleafindex].itemindex = brushindex+model->firstmodelbrush;
                        VectorCopy(brush->colbrushf->mins, bihleafs[bihleafindex].mins);
                        VectorCopy(brush->colbrushf->maxs, bihleafs[bihleafindex].maxs);
@@ -6700,6 +6691,7 @@ void Mod_MakeCollisionBIH(dp_model_t *model, qboolean userendersurfaces)
                        {
                                bihleafs[bihleafindex].type = BIH_COLLISIONTRIANGLE;
                                bihleafs[bihleafindex].textureindex = surface->texture - model->data_textures;
+                               bihleafs[bihleafindex].surfaceindex = surface - model->data_surfaces;
                                bihleafs[bihleafindex].itemindex = triangleindex+surface->num_firstcollisiontriangle;
                                bihleafs[bihleafindex].mins[0] = min(collisionvertex3f[3*e[0]+0], min(collisionvertex3f[3*e[1]+0], collisionvertex3f[3*e[2]+0])) - 1;
                                bihleafs[bihleafindex].mins[1] = min(collisionvertex3f[3*e[0]+1], min(collisionvertex3f[3*e[1]+1], collisionvertex3f[3*e[2]+1])) - 1;
@@ -6714,22 +6706,24 @@ void Mod_MakeCollisionBIH(dp_model_t *model, qboolean userendersurfaces)
 
        // allocate buffers for the produced and temporary data
        bihmaxnodes = bihnumleafs - 1;
-       bihnodes = Mem_Alloc(loadmodel->mempool, sizeof(bih_node_t) * bihmaxnodes);
-       temp_leafsort = Mem_Alloc(loadmodel->mempool, sizeof(int) * bihnumleafs * 2);
+       bihnodes = (bih_node_t *)Mem_Alloc(loadmodel->mempool, sizeof(bih_node_t) * bihmaxnodes);
+       temp_leafsort = (int *)Mem_Alloc(loadmodel->mempool, sizeof(int) * bihnumleafs * 2);
        temp_leafsortscratch = temp_leafsort + bihnumleafs;
 
        // now build it
-       BIH_Build(&model->collision_bih, bihnumleafs, bihleafs, bihmaxnodes, bihnodes, temp_leafsort, temp_leafsortscratch);
+       BIH_Build(out, bihnumleafs, bihleafs, bihmaxnodes, bihnodes, temp_leafsort, temp_leafsortscratch);
 
        // we're done with the temporary data
        Mem_Free(temp_leafsort);
 
        // resize the BIH nodes array if it over-allocated
-       if (model->collision_bih.maxnodes > model->collision_bih.numnodes)
+       if (out->maxnodes > out->numnodes)
        {
-               model->collision_bih.maxnodes = model->collision_bih.numnodes;
-               model->collision_bih.nodes = Mem_Realloc(loadmodel->mempool, model->collision_bih.nodes, model->collision_bih.numnodes * sizeof(bih_node_t));
+               out->maxnodes = out->numnodes;
+               out->nodes = (bih_node_t *)Mem_Realloc(loadmodel->mempool, out->nodes, out->numnodes * sizeof(bih_node_t));
        }
+
+       return out;
 }
 
 static int Mod_Q3BSP_SuperContentsFromNativeContents(dp_model_t *model, int nativecontents)
@@ -7040,12 +7034,13 @@ void Mod_Q3BSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
                        mod->DrawSky = R_Q1BSP_DrawSky;
 
                for (j = 0;j < mod->nummodelsurfaces;j++)
-                       if (mod->data_surfaces[j + mod->firstmodelsurface].texture->basematerialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION))
+                       if (mod->data_surfaces[j + mod->firstmodelsurface].texture->basematerialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA))
                                break;
                if (j < mod->nummodelsurfaces)
                        mod->DrawAddWaterPlanes = R_Q1BSP_DrawAddWaterPlanes;
 
-               Mod_MakeCollisionBIH(mod, false);
+               Mod_MakeCollisionBIH(mod, false, &mod->collision_bih);
+               Mod_MakeCollisionBIH(mod, true, &mod->render_bih);
 
                // generate VBOs and other shared data before cloning submodels
                if (i == 0)
@@ -7363,13 +7358,13 @@ void Mod_OBJ_Load(dp_model_t *mod, void *buffer, void *bufferend)
 
        // allocate storage for triangles
        loadmodel->num_surfaces = loadmodel->nummodelsurfaces = numsurfaces = numtextures;
-       loadmodel->surfmesh.data_element3i = Mem_Alloc(loadmodel->mempool, numtriangles * sizeof(int[3]));
+       loadmodel->surfmesh.data_element3i = (int *)Mem_Alloc(loadmodel->mempool, numtriangles * sizeof(int[3]));
        loadmodel->data_surfaces = (msurface_t *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t));
        // allocate vertex hash structures to build an optimal vertex subset
        vertexhashsize = numtriangles*2;
-       vertexhashtable = Mem_Alloc(loadmodel->mempool, sizeof(int) * vertexhashsize);
+       vertexhashtable = (int *)Mem_Alloc(loadmodel->mempool, sizeof(int) * vertexhashsize);
        memset(vertexhashtable, 0xFF, sizeof(int) * vertexhashsize);
-       vertexhashdata = Mem_Alloc(loadmodel->mempool, sizeof(*vertexhashdata) * numtriangles*3);
+       vertexhashdata = (objvertex_t *)Mem_Alloc(loadmodel->mempool, sizeof(*vertexhashdata) * numtriangles*3);
        vertexhashcount = 0;
 
        // gather surface stats for assigning vertex/triangle ranges
@@ -7482,7 +7477,8 @@ void Mod_OBJ_Load(dp_model_t *mod, void *buffer, void *bufferend)
        Mod_BuildTextureVectorsFromNormals(0, loadmodel->surfmesh.num_vertices, loadmodel->surfmesh.num_triangles, loadmodel->surfmesh.data_vertex3f, loadmodel->surfmesh.data_texcoordtexture2f, loadmodel->surfmesh.data_normal3f, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.data_svector3f, loadmodel->surfmesh.data_tvector3f, true);
        Mod_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles);
 
-       Mod_MakeCollisionBIH(loadmodel, true);
+       Mod_MakeCollisionBIH(loadmodel, true, &mod->collision_bih);
+       mod->render_bih = mod->collision_bih;
 }
 
 
@@ -8174,7 +8170,7 @@ void Mod_OBJ_Load(dp_model_t *mod, void *buffer, void *bufferend)
        loadmodel->num_textures = numtextures;
        loadmodel->data_textures = Mem_Alloc(loadmodel->mempool, loadmodel->num_textures * sizeof(texture_t));
        for (i = 0;i < numtextures;i++)
-               Mod_LoadTextureFromQ3Shader(loadmodel->data_textures + i, texturenames[i], true, true, TEXF_MIPMAP | TEXF_ALPHA | (r_picmipworld.integer ? TEXF_PICMIP : 0) | TEXF_COMPRESS);
+               Mod_LoadTextureFromQ3Shader(loadmodel->data_textures + i, texturenames[i], true, true, TEXF_MIPMAP | TEXF_ALPHA | TEXF_ISWORLD | TEXF_PICMIP | TEXF_COMPRESS);
 
        // free the texturenames array since we are now done with it
        for (i = 0;i < numtextures;i++)