]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - model_brush.c
when loading q1bsp textures, do not allow q3 shader loading to overwrite
[xonotic/darkplaces.git] / model_brush.c
index f40d7299654c3e2152230890540ff0ee5eea696d..08040c79faa60e4d9a150c30e4bbf17f53894d60 100644 (file)
@@ -27,6 +27,7 @@ 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 mod_bsp_portalize = {0, "mod_bsp_portalize", "1", "enables portal generation from BSP tree (may take several seconds per map), used by r_drawportals, r_useportalculling, r_shadow_realtime_world_compileportalculling, sv_cullentities_portal"};
 cvar_t r_novis = {0, "r_novis", "0", "draws whole level, see also sv_cullentities_pvs 0"};
 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)"};
@@ -46,6 +47,8 @@ cvar_t mod_q3bsp_lightmapmergepower = {CVAR_SAVE, "mod_q3bsp_lightmapmergepower"
 cvar_t mod_q3bsp_nolightmaps = {CVAR_SAVE, "mod_q3bsp_nolightmaps", "0", "do not load lightmaps in Q3BSP maps (to save video RAM, but be warned: it looks ugly)"};
 cvar_t mod_q3bsp_tracelineofsight_brushes = {0, "mod_q3bsp_tracelineofsight_brushes", "0", "enables culling of entities behind detail brushes, curves, etc"};
 cvar_t mod_q3shader_default_offsetmapping = {CVAR_SAVE, "mod_q3shader_default_offsetmapping", "1", "use offsetmapping by default on all surfaces"};
+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_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"};
@@ -60,6 +63,7 @@ static texture_t mod_q1bsp_texture_water;
 void Mod_BrushInit(void)
 {
 //     Cvar_RegisterVariable(&r_subdivide_size);
+       Cvar_RegisterVariable(&mod_bsp_portalize);
        Cvar_RegisterVariable(&r_novis);
        Cvar_RegisterVariable(&r_nosurftextures);
        Cvar_RegisterVariable(&r_subdivisions_tolerance);
@@ -79,6 +83,8 @@ void Mod_BrushInit(void)
        Cvar_RegisterVariable(&mod_q3bsp_nolightmaps);
        Cvar_RegisterVariable(&mod_q3bsp_tracelineofsight_brushes);
        Cvar_RegisterVariable(&mod_q3shader_default_offsetmapping);
+       Cvar_RegisterVariable(&mod_q3shader_default_polygonfactor);
+       Cvar_RegisterVariable(&mod_q3shader_default_polygonoffset);
        Cvar_RegisterVariable(&mod_q1bsp_polygoncollisions);
        Cvar_RegisterVariable(&mod_collision_bih);
        Cvar_RegisterVariable(&mod_recalculatenodeboxes);
@@ -1287,6 +1293,188 @@ void Mod_Q1BSP_LightPoint(dp_model_t *model, const vec3_t p, vec3_t ambientcolor
        Mod_Q1BSP_LightPoint_RecursiveBSPNode(model, ambientcolor, diffusecolor, diffusenormal, model->brush.data_nodes + model->brushq1.hulls[0].firstclipnode, p[0], p[1], p[2] + 0.125, p[2] - 65536);
 }
 
+static const texture_t *Mod_Q1BSP_TraceLineAgainstSurfacesFindTextureOnNode(RecursiveHullCheckTraceInfo_t *t, const dp_model_t *model, const mnode_t *node, double mid[3])
+{
+       int i;
+       int j;
+       int k;
+       const msurface_t *surface;
+       float normal[3];
+       float v0[3];
+       float v1[3];
+       float edgedir[3];
+       float edgenormal[3];
+       float p[4];
+       float midf;
+       float t1;
+       float t2;
+       VectorCopy(mid, p);
+       p[3] = 1;
+       surface = model->data_surfaces + node->firstsurface;
+       for (i = 0;i < node->numsurfaces;i++, surface++)
+       {
+               // skip surfaces whose bounding box does not include the point
+//             if (!BoxesOverlap(mid, mid, surface->mins, surface->maxs))
+//                     continue;
+               // skip faces with contents we don't care about
+               if (!(t->trace->hitsupercontentsmask & surface->texture->supercontents))
+                       continue;
+               // get the surface normal - since it is flat we know any vertex normal will suffice
+               VectorCopy(model->surfmesh.data_normal3f + 3 * surface->num_firstvertex, normal);
+               // skip backfaces
+               if (DotProduct(t->dist, normal) > 0)
+                       continue;
+               // iterate edges and see if the point is outside one of them
+               for (j = 0, k = surface->num_vertices - 1;j < surface->num_vertices;k = j, j++)
+               {
+                       VectorCopy(model->surfmesh.data_vertex3f + 3 * (surface->num_firstvertex + k), v0);
+                       VectorCopy(model->surfmesh.data_vertex3f + 3 * (surface->num_firstvertex + j), v1);
+                       VectorSubtract(v0, v1, edgedir);
+                       CrossProduct(edgedir, normal, edgenormal);
+                       if (DotProduct(edgenormal, p) > DotProduct(edgenormal, v0))
+                               break;
+               }
+               // if the point is outside one of the edges, it is not within the surface
+               if (j < surface->num_vertices)
+                       continue;
+
+               // we hit a surface, this is the impact point...
+               VectorCopy(normal, t->trace->plane.normal);
+               t->trace->plane.dist = DotProduct(normal, p);
+
+               // calculate the true fraction
+               t1 = DotProduct(t->start, t->trace->plane.normal) - t->trace->plane.dist;
+               t2 = DotProduct(t->end, t->trace->plane.normal) - t->trace->plane.dist;
+               midf = t1 / (t1 - t2);
+               t->trace->realfraction = midf;
+
+               // calculate the return fraction which is nudged off the surface a bit
+               midf = (t1 - DIST_EPSILON) / (t1 - t2);
+               t->trace->fraction = bound(0, midf, 1);
+
+               if (collision_prefernudgedfraction.integer)
+                       t->trace->realfraction = t->trace->fraction;
+
+               t->trace->hittexture = surface->texture->currentframe;
+               t->trace->hitq3surfaceflags = t->trace->hittexture->surfaceflags;
+               t->trace->hitsupercontents = t->trace->hittexture->supercontents;
+               return surface->texture->currentframe;
+       }
+       return NULL;
+}
+
+static int Mod_Q1BSP_TraceLineAgainstSurfacesRecursiveBSPNode(RecursiveHullCheckTraceInfo_t *t, const dp_model_t *model, const mnode_t *node, const double p1[3], const double p2[3])
+{
+       const mplane_t *plane;
+       double t1, t2;
+       int side;
+       double midf, mid[3];
+       const mleaf_t *leaf;
+
+       while (node->plane)
+       {
+               plane = node->plane;
+               if (plane->type < 3)
+               {
+                       t1 = p1[plane->type] - plane->dist;
+                       t2 = p2[plane->type] - plane->dist;
+               }
+               else
+               {
+                       t1 = DotProduct (plane->normal, p1) - plane->dist;
+                       t2 = DotProduct (plane->normal, p2) - plane->dist;
+               }
+               if (t1 < 0)
+               {
+                       if (t2 < 0)
+                       {
+                               node = node->children[1];
+                               continue;
+                       }
+                       side = 1;
+               }
+               else
+               {
+                       if (t2 >= 0)
+                       {
+                               node = node->children[0];
+                               continue;
+                       }
+                       side = 0;
+               }
+
+               // the line intersects, find intersection point
+               // LordHavoc: this uses the original trace for maximum accuracy
+               if (plane->type < 3)
+               {
+                       t1 = t->start[plane->type] - plane->dist;
+                       t2 = t->end[plane->type] - plane->dist;
+               }
+               else
+               {
+                       t1 = DotProduct (plane->normal, t->start) - plane->dist;
+                       t2 = DotProduct (plane->normal, t->end) - plane->dist;
+               }
+       
+               midf = t1 / (t1 - t2);
+               VectorMA(t->start, midf, t->dist, mid);
+
+               // recurse both sides, front side first, return if we hit a surface
+               if (Mod_Q1BSP_TraceLineAgainstSurfacesRecursiveBSPNode(t, model, node->children[side], p1, mid) == HULLCHECKSTATE_DONE)
+                       return HULLCHECKSTATE_DONE;
+
+               // test each surface on the node
+               Mod_Q1BSP_TraceLineAgainstSurfacesFindTextureOnNode(t, model, node, mid);
+               if (t->trace->hittexture)
+                       return HULLCHECKSTATE_DONE;
+
+               // recurse back side
+               return Mod_Q1BSP_TraceLineAgainstSurfacesRecursiveBSPNode(t, model, node->children[side ^ 1], mid, p2);
+       }
+       leaf = (const mleaf_t *)node;
+       side = Mod_Q1BSP_SuperContentsFromNativeContents(NULL, leaf->contents);
+       if (!t->trace->startfound)
+       {
+               t->trace->startfound = true;
+               t->trace->startsupercontents |= side;
+       }
+       if (side & SUPERCONTENTS_LIQUIDSMASK)
+               t->trace->inwater = true;
+       if (side == 0)
+               t->trace->inopen = true;
+       if (side & t->trace->hitsupercontentsmask)
+       {
+               // if the first leaf is solid, set startsolid
+               if (t->trace->allsolid)
+                       t->trace->startsolid = true;
+               return HULLCHECKSTATE_SOLID;
+       }
+       else
+       {
+               t->trace->allsolid = false;
+               return HULLCHECKSTATE_EMPTY;
+       }
+}
+
+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)
+{
+       RecursiveHullCheckTraceInfo_t rhc;
+
+       memset(&rhc, 0, sizeof(rhc));
+       memset(trace, 0, sizeof(trace_t));
+       rhc.trace = trace;
+       rhc.trace->hitsupercontentsmask = hitsupercontentsmask;
+       rhc.trace->fraction = 1;
+       rhc.trace->realfraction = 1;
+       rhc.trace->allsolid = true;
+       rhc.hull = &model->brushq1.hulls[0]; // 0x0x0
+       VectorCopy(start, rhc.start);
+       VectorCopy(end, rhc.end);
+       VectorSubtract(rhc.end, rhc.start, rhc.dist);
+       Mod_Q1BSP_TraceLineAgainstSurfacesRecursiveBSPNode(&rhc, model, model->brush.data_nodes + rhc.hull->firstclipnode, rhc.start, rhc.end);
+       VectorMA(rhc.start, rhc.trace->fraction, rhc.dist, rhc.trace->endpos);
+}
+
 static void Mod_Q1BSP_DecompressVis(const unsigned char *in, const unsigned char *inend, unsigned char *out, unsigned char *outend)
 {
        int c;
@@ -1400,6 +1588,7 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
        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;
@@ -1534,8 +1723,11 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
                        if (name[j] >= 'A' && name[j] <= 'Z')
                                name[j] += 'a' - 'A';
 
+               // 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))
                        continue;
+               loadmodel->data_textures[i] = backuptex;
 
                tx = loadmodel->data_textures + i;
                strlcpy(tx->name, name, sizeof(tx->name));
@@ -2339,8 +2531,8 @@ static void Mod_Q1BSP_LoadFaces(lump_t *l)
                }
 
                // compile additional data about the surface geometry
-               Mod_BuildNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, loadmodel->surfmesh.data_vertex3f, (loadmodel->surfmesh.data_element3i + 3 * surface->num_firsttriangle), loadmodel->surfmesh.data_normal3f, true);
-               Mod_BuildTextureVectorsFromNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, loadmodel->surfmesh.data_vertex3f, loadmodel->surfmesh.data_texcoordtexture2f, loadmodel->surfmesh.data_normal3f, (loadmodel->surfmesh.data_element3i + 3 * surface->num_firsttriangle), loadmodel->surfmesh.data_svector3f, loadmodel->surfmesh.data_tvector3f, true);
+               Mod_BuildNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, loadmodel->surfmesh.data_vertex3f, (loadmodel->surfmesh.data_element3i + 3 * surface->num_firsttriangle), loadmodel->surfmesh.data_normal3f, r_smoothnormals_areaweighting.integer != 0);
+               Mod_BuildTextureVectorsFromNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, loadmodel->surfmesh.data_vertex3f, loadmodel->surfmesh.data_texcoordtexture2f, loadmodel->surfmesh.data_normal3f, (loadmodel->surfmesh.data_element3i + 3 * surface->num_firsttriangle), loadmodel->surfmesh.data_svector3f, loadmodel->surfmesh.data_tvector3f, r_smoothnormals_areaweighting.integer != 0);
                BoxFromPoints(surface->mins, surface->maxs, surface->num_vertices, (loadmodel->surfmesh.data_vertex3f + 3 * surface->num_firstvertex));
 
                // generate surface extents information
@@ -2413,6 +2605,7 @@ static void Mod_Q1BSP_LoadFaces(lump_t *l)
                        // lightmap is needed on this surface (rather than duplicating the
                        // logic above)
                        loadmodel->brushq1.lightmapupdateflags[surfacenum] = true;
+                       loadmodel->lit = true;
                }
        }
 
@@ -2558,6 +2751,8 @@ static void Mod_Q1BSP_LoadNodes(lump_t *l)
        if (l->filelen % sizeof(*in))
                Host_Error("Mod_Q1BSP_LoadNodes: funny lump size in %s",loadmodel->name);
        count = l->filelen / sizeof(*in);
+       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));
 
        loadmodel->brush.data_nodes = out;
@@ -3433,13 +3628,15 @@ static int Mod_Q1BSP_CreateShadowMesh(dp_model_t *mod)
        for (j = 0, surface = mod->data_surfaces;j < mod->num_surfaces;j++, surface++)
                if (surface->num_triangles > 0)
                        Mod_ShadowMesh_AddMesh(mod->mempool, mod->brush.shadowmesh, NULL, NULL, NULL, mod->surfmesh.data_vertex3f, NULL, NULL, NULL, NULL, surface->num_triangles, (mod->surfmesh.data_element3i + 3 * surface->num_firsttriangle));
-       mod->brush.shadowmesh = Mod_ShadowMesh_Finish(mod->mempool, mod->brush.shadowmesh, false, true, false);
-       if (mod->brush.shadowmesh)
+       mod->brush.shadowmesh = Mod_ShadowMesh_Finish(mod->mempool, mod->brush.shadowmesh, false, r_enableshadowvolumes.integer != 0, false);
+       if (mod->brush.shadowmesh && mod->brush.shadowmesh->neighbor3i)
                Mod_BuildTriangleNeighbors(mod->brush.shadowmesh->neighbor3i, mod->brush.shadowmesh->element3i, mod->brush.shadowmesh->numtriangles);
 
        return numshadowmeshtriangles;
 }
 
+void Mod_CollisionBIH_TraceLineAgainstSurfaces(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t end, int hitsupercontentsmask);
+
 void Mod_Q1BSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
 {
        int i, j, k;
@@ -3447,7 +3644,6 @@ void Mod_Q1BSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
        dmodel_t *bm;
        float dist, modelyawradius, modelradius;
        msurface_t *surface;
-       int numshadowmeshtriangles;
        hullinfo_t hullinfo;
        int totalstylesurfaces, totalstyles, stylecounts[256], remapstyles[256];
        model_brush_lightstyleinfo_t styleinfo[256];
@@ -3498,9 +3694,10 @@ void Mod_Q1BSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
 
        mod->soundfromcenter = true;
        mod->TraceBox = Mod_Q1BSP_TraceBox;
-       mod->TraceLine = Mod_Q1BSP_TraceLine;
+       mod->TraceLine = Mod_Q1BSP_TraceLineAgainstSurfaces; // LordHavoc: use the surface-hitting version of TraceLine in all cases
        mod->TracePoint = Mod_Q1BSP_TracePoint;
        mod->PointSuperContents = Mod_Q1BSP_PointSuperContents;
+       mod->TraceLineAgainstSurfaces = Mod_Q1BSP_TraceLineAgainstSurfaces;
        mod->brush.TraceLineOfSight = Mod_Q1BSP_TraceLineOfSight;
        mod->brush.SuperContentsFromNativeContents = Mod_Q1BSP_SuperContentsFromNativeContents;
        mod->brush.NativeContentsFromSuperContents = Mod_Q1BSP_NativeContentsFromSuperContents;
@@ -3569,13 +3766,14 @@ void Mod_Q1BSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
        mod->brushq1.num_compressedpvs = 0;
 
        Mod_Q1BSP_MakeHull0();
-       Mod_Q1BSP_MakePortals();
+       if (mod_bsp_portalize.integer)
+               Mod_Q1BSP_MakePortals();
 
        mod->numframes = 2;             // regular and alternate animation
        mod->numskins = 1;
 
        // make a single combined shadow mesh to allow optimized shadow volume creation
-       numshadowmeshtriangles = Mod_Q1BSP_CreateShadowMesh(loadmodel);
+       Mod_Q1BSP_CreateShadowMesh(loadmodel);
 
        if (loadmodel->brush.numsubmodels)
                loadmodel->brush.submodels = (dp_model_t **)Mem_Alloc(loadmodel->mempool, loadmodel->brush.numsubmodels * sizeof(dp_model_t *));
@@ -3751,15 +3949,18 @@ void Mod_Q1BSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
                }
                //mod->brushq1.num_visleafs = bm->visleafs;
 
+               // build a Bounding Interval Hierarchy for culling triangles in light rendering
+               Mod_MakeCollisionBIH(mod, true, &mod->render_bih);
+
                if (mod_q1bsp_polygoncollisions.integer)
                {
-                       Mod_MakeCollisionBIH(mod, true, &mod->collision_bih);
+                       mod->collision_bih = mod->render_bih;
                        // point traces and contents checks still use the bsp tree
                        mod->TraceLine = Mod_CollisionBIH_TraceLine;
                        mod->TraceBox = Mod_CollisionBIH_TraceBox;
+                       mod->TraceBrush = Mod_CollisionBIH_TraceBrush;
+                       mod->TraceLineAgainstSurfaces = Mod_CollisionBIH_TraceLineAgainstSurfaces;
                }
-               else
-                       Mod_MakeCollisionBIH(mod, true, &mod->render_bih);
 
                // generate VBOs and other shared data before cloning submodels
                if (i == 0)
@@ -4506,6 +4707,8 @@ static void Mod_Q3BSP_LoadVertices(lump_t *l)
                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);
                loadmodel->brushq3.data_color4f[i * 4 + 3] = in->color4ub[3] * (1.0f / 255.0f);
+               if(in->color4ub[0] != 255 || in->color4ub[1] != 255 || in->color4ub[2] != 255)
+                       loadmodel->lit = true;
        }
 }
 
@@ -4713,7 +4916,7 @@ static void Mod_Q3BSP_LoadLightmaps(lump_t *l, lump_t *faceslump)
        // figure out what the most reasonable merge power is within limits
 
        // find the appropriate NxN dimensions to merge to, to avoid wasted space
-       realcount = count >> loadmodel->brushq3.deluxemapping;
+       realcount = count >> (int)loadmodel->brushq3.deluxemapping;
 
        // figure out how big the merged texture has to be
        mergegoal = 128<<bound(0, mod_q3bsp_lightmapmergepower.integer, 6);
@@ -4761,7 +4964,7 @@ static void Mod_Q3BSP_LoadLightmaps(lump_t *l, lump_t *faceslump)
        for (i = 0;i < count;i++)
        {
                // figure out which merged lightmap texture this fits into
-               realindex = i >> loadmodel->brushq3.deluxemapping;
+               realindex = i >> (int)loadmodel->brushq3.deluxemapping;
                lightmapindex = i >> powerdxy;
 
                // choose the destination address
@@ -4957,6 +5160,7 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
                                out->lightmaptexture = loadmodel->brushq3.data_lightmaps[n >> loadmodel->brushq3.num_lightmapmergedwidthheightdeluxepower];
                                if (loadmodel->brushq3.deluxemapping)
                                        out->deluxemaptexture = loadmodel->brushq3.data_deluxemaps[n >> loadmodel->brushq3.num_lightmapmergedwidthheightdeluxepower];
+                               loadmodel->lit = true;
                        }
                }
 
@@ -5311,13 +5515,20 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
                if(out->num_vertices && out->num_triangles)
                        continue;
                if(out->num_vertices == 0)
-                       Con_Printf("Mod_Q3BSP_LoadFaces: surface %d has no vertices, ignoring\n", i);
-               if(out->num_triangles == 0)
-                       Con_Printf("Mod_Q3BSP_LoadFaces: surface %d has no triangles, ignoring\n", i);
+               {
+                       Con_Printf("Mod_Q3BSP_LoadFaces: surface %d (texture %s) has no vertices, ignoring\n", i, out->texture ? out->texture->name : "(none)");
+                       if(out->num_triangles == 0)
+                               Con_Printf("Mod_Q3BSP_LoadFaces: surface %d (texture %s) has no triangles, ignoring\n", i, out->texture ? out->texture->name : "(none)");
+               }
+               else if(out->num_triangles == 0)
+                       Con_Printf("Mod_Q3BSP_LoadFaces: surface %d (texture %s, near %f %f %f) has no triangles, ignoring\n", i, out->texture ? out->texture->name : "(none)",
+                                       (loadmodel->surfmesh.data_vertex3f + 3 * out->num_firstvertex)[0 * 3 + 0],
+                                       (loadmodel->surfmesh.data_vertex3f + 3 * out->num_firstvertex)[1 * 3 + 0],
+                                       (loadmodel->surfmesh.data_vertex3f + 3 * out->num_firstvertex)[2 * 3 + 0]);
        }
 
        // for per pixel lighting
-       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_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, r_smoothnormals_areaweighting.integer != 0);
 
        // generate ushort elements array if possible
        if (loadmodel->surfmesh.data_element3s)
@@ -5480,6 +5691,8 @@ static void Mod_Q3BSP_LoadNodes(lump_t *l)
        if (l->filelen % sizeof(*in))
                Host_Error("Mod_Q3BSP_LoadNodes: funny lump size in %s",loadmodel->name);
        count = l->filelen / sizeof(*in);
+       if (count == 0)
+               Host_Error("Mod_Q3BSP_LoadNodes: missing BSP tree in %s",loadmodel->name);
        out = (mnode_t *)Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
 
        loadmodel->brush.data_nodes = out;
@@ -5605,11 +5818,26 @@ static void Mod_Q3BSP_LoadPVS(lump_t *l)
 static void Mod_Q3BSP_LightPoint(dp_model_t *model, const vec3_t p, vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal)
 {
        int i, j, k, index[3];
-       float transformed[3], blend1, blend2, blend, stylescale;
+       float transformed[3], blend1, blend2, blend, stylescale = 1;
        q3dlightgrid_t *a, *s;
 
        // scale lighting by lightstyle[0] so that darkmode in dpmod works properly
-       stylescale = r_refdef.scene.rtlightstylevalue[0];
+       switch(vid.renderpath)
+       {
+       case RENDERPATH_GL20:
+       case RENDERPATH_D3D9:
+       case RENDERPATH_D3D10:
+       case RENDERPATH_D3D11:
+       case RENDERPATH_SOFT:
+       case RENDERPATH_GLES2:
+               // LordHavoc: FIXME: is this true?
+               stylescale = 1; // added while render
+               break;
+       case RENDERPATH_GL11:
+       case RENDERPATH_GL13:
+               stylescale = r_refdef.scene.rtlightstylevalue[0];
+               break;
+       }
 
        if (!model->brushq3.num_lightgrid)
        {
@@ -5745,418 +5973,177 @@ static qboolean Mod_Q3BSP_TraceLineOfSight(struct model_s *model, const vec3_t s
        }
 }
 
-static void Mod_CollisionBIH_TracePoint_RecursiveBIHNode(trace_t *trace, dp_model_t *model, int nodenum, const vec3_t point)
+void Mod_CollisionBIH_TracePoint(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, int hitsupercontentsmask)
 {
+       const bih_t *bih;
        const bih_leaf_t *leaf;
        const bih_node_t *node;
        const colbrushf_t *brush;
        int axis;
-       while (nodenum >= 0)
-       {
-               node = model->collision_bih.nodes + nodenum;
-               axis = node->type - BIH_SPLITX;
-               if (point[axis] <= node->backmax)
-               {
-                       if (point[axis] >= node->frontmin)
-                               Mod_CollisionBIH_TracePoint_RecursiveBIHNode(trace, model, node->front, point);
-                       nodenum = node->back;
-               }
-               else if (point[axis] >= node->frontmin)
-                       nodenum = node->front;
-               else // no overlap with either child?  just return
-                       return;
-       }
-       if (!model->collision_bih.leafs)
-               return;
-       leaf = model->collision_bih.leafs + (-1-nodenum);
-       switch(leaf->type)
-       {
-       case BIH_BRUSH:
-               brush = model->brush.data_brushes[leaf->itemindex].colbrushf;
-               Collision_TracePointBrushFloat(trace, point, brush);
-               break;
-       case BIH_COLLISIONTRIANGLE:
-               // collision triangle - skipped because they have no volume
-               break;
-       case BIH_RENDERTRIANGLE:
-               // render triangle - skipped because they have no volume
-               break;
-       }
-}
+       int nodenum;
+       int nodestackpos = 0;
+       int nodestack[1024];
 
-static void Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace_t *trace, dp_model_t *model, int nodenum, const vec3_t start, const vec3_t end, const vec3_t linestart, const vec3_t lineend)
-{
-       const bih_leaf_t *leaf;
-       const bih_node_t *node;
-       const colbrushf_t *brush;
-       const int *e;
-       const texture_t *texture;
-       int axis;
-#define BIHLINECLIP
-#ifdef BIHLINECLIP
-       int sideflags;
-       vec_t frontdist1;
-       vec_t frontdist2;
-       vec_t frontfrac;
-       vec_t backdist1;
-       vec_t backdist2;
-       vec_t backfrac;
-       vec3_t clipped, newstart, newend;
-#endif
-       vec3_t segmentmins;
-       vec3_t segmentmaxs;
-       segmentmins[0] = min(start[0], end[0]);
-       segmentmins[1] = min(start[1], end[1]);
-       segmentmins[2] = min(start[2], end[2]);
-       segmentmaxs[0] = max(start[0], end[0]);
-       segmentmaxs[1] = max(start[1], end[1]);
-       segmentmaxs[2] = max(start[2], end[2]);
-       while (nodenum >= 0)
-       {
-               node = model->collision_bih.nodes + nodenum;
-#if 0
-               if (!BoxesOverlap(segmentmins, segmentmaxs, node->mins, node->maxs))
-                       return;
+       memset(trace, 0, sizeof(*trace));
+       trace->fraction = 1;
+       trace->realfraction = 1;
+       trace->hitsupercontentsmask = hitsupercontentsmask;
+
+       bih = &model->collision_bih;
+       nodenum = bih->rootnode;
+       nodestack[nodestackpos++] = nodenum;
+       while (nodestackpos)
+       {
+               nodenum = nodestack[--nodestackpos];
+               node = bih->nodes + nodenum;
+#if 1
+               if (!BoxesOverlap(start, start, node->mins, node->maxs))
+                       continue;
 #endif
-               axis = node->type - BIH_SPLITX;
-#if 0
-               if (segmentmins[axis] <= node->backmax)
-               {
-                       if (segmentmaxs[axis] >= node->frontmin)
-                               Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
-                       nodenum = node->back;
-               }
-               else if (segmentmaxs[axis] >= node->frontmin)
-                       nodenum = node->front;
-               else
-                       return; // trace falls between children
-#else
-               frontdist1 = start[axis] - node->frontmin;
-               frontdist2 = end[axis] - node->frontmin;
-               backdist1 = start[axis] - node->backmax;
-               backdist2 = end[axis] - node->backmax;
-               sideflags = 0;
-               if (frontdist1 < 0)
-                       sideflags |= 1;
-               if (frontdist2 < 0)
-                       sideflags |= 2;
-               if (backdist1 < 0)
-                       sideflags |= 4;
-               if (backdist2 < 0)
-                       sideflags |= 8;
-#if 0
-               if (sideflags & 12)
+               if (node->type <= BIH_SPLITZ && nodestackpos+2 <= 1024)
                {
-                       if ((sideflags & 3) != 3)
-                               Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
-                       nodenum = node->back;
+                       axis = node->type - BIH_SPLITX;
+                       if (start[axis] >= node->frontmin)
+                               nodestack[nodestackpos++] = node->front;
+                       if (start[axis] <= node->backmax)
+                               nodestack[nodestackpos++] = node->back;
                }
-               else if ((sideflags & 3) != 3)
-                       nodenum = node->front;
-               else
-                       return; // trace falls between children
-#else
-               switch(sideflags)
+               else if (node->type == BIH_UNORDERED)
                {
-               case 0:
-                       // start end START END
-                       nodenum = node->front;
-                       continue;
-               case 1:
-                       // START end START END
-#ifdef BIHLINECLIP
-                       frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       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]);
-                       segmentmaxs[0] = max(start[0], end[0]);
-                       segmentmaxs[1] = max(start[1], end[1]);
-                       segmentmaxs[2] = max(start[2], end[2]);
-#endif
-                       nodenum = node->front;
-                       break;
-               case 2:
-#ifdef BIHLINECLIP
-                       // start END START END
-                       frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       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]);
-                       segmentmaxs[0] = max(start[0], end[0]);
-                       segmentmaxs[1] = max(start[1], end[1]);
-                       segmentmaxs[2] = max(start[2], end[2]);
-#endif
-                       nodenum = node->front;
-                       break;
-               case 3:
-                       // START END START END
-                       return; // line falls in gap between children
-               case 4:
-                       // start end start END
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
-#ifdef BIHLINECLIP
-                       backfrac = backdist1 / (backdist1 - backdist2);
-                       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]);
-                       segmentmaxs[0] = max(start[0], end[0]);
-                       segmentmaxs[1] = max(start[1], end[1]);
-                       segmentmaxs[2] = max(start[2], end[2]);
-#endif
-                       nodenum = node->back;
-                       break;
-               case 5:
-                       // START end start END
-#ifdef BIHLINECLIP
-                       frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       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, newend); end = newend;
-                       segmentmins[0] = min(start[0], end[0]);
-                       segmentmins[1] = min(start[1], end[1]);
-                       segmentmins[2] = min(start[2], end[2]);
-                       segmentmaxs[0] = max(start[0], end[0]);
-                       segmentmaxs[1] = max(start[1], end[1]);
-                       segmentmaxs[2] = max(start[2], end[2]);
-#else
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
-#endif
-                       nodenum = node->back;
-                       break;
-               case 6:
-                       // start END start END
-#ifdef BIHLINECLIP
-                       frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       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, newend); end = newend;
-                       segmentmins[0] = min(start[0], end[0]);
-                       segmentmins[1] = min(start[1], end[1]);
-                       segmentmins[2] = min(start[2], end[2]);
-                       segmentmaxs[0] = max(start[0], end[0]);
-                       segmentmaxs[1] = max(start[1], end[1]);
-                       segmentmaxs[2] = max(start[2], end[2]);
-#else
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
-#endif
-                       nodenum = node->back;
-                       break;
-               case 7:
-                       // START END start END
-#ifdef BIHLINECLIP
-                       backfrac = backdist1 / (backdist1 - backdist2);
-                       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]);
-                       segmentmaxs[0] = max(start[0], end[0]);
-                       segmentmaxs[1] = max(start[1], end[1]);
-                       segmentmaxs[2] = max(start[2], end[2]);
-#endif
-                       nodenum = node->back;
-                       break;
-               case 8:
-                       // start end START end
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
-#ifdef BIHLINECLIP
-                       backfrac = backdist1 / (backdist1 - backdist2);
-                       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]);
-                       segmentmaxs[0] = max(start[0], end[0]);
-                       segmentmaxs[1] = max(start[1], end[1]);
-                       segmentmaxs[2] = max(start[2], end[2]);
-#endif
-                       nodenum = node->back;
-                       break;
-               case 9:
-                       // START end START end
-#ifdef BIHLINECLIP
-                       frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       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, newstart); start = newstart;
-                       segmentmins[0] = min(start[0], end[0]);
-                       segmentmins[1] = min(start[1], end[1]);
-                       segmentmins[2] = min(start[2], end[2]);
-                       segmentmaxs[0] = max(start[0], end[0]);
-                       segmentmaxs[1] = max(start[1], end[1]);
-                       segmentmaxs[2] = max(start[2], end[2]);
-#else
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
-#endif
-                       nodenum = node->back;
-                       break;
-               case 10:
-                       // start END START end
-#ifdef BIHLINECLIP
-                       frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       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, newstart); start = newstart;
-                       segmentmins[0] = min(start[0], end[0]);
-                       segmentmins[1] = min(start[1], end[1]);
-                       segmentmins[2] = min(start[2], end[2]);
-                       segmentmaxs[0] = max(start[0], end[0]);
-                       segmentmaxs[1] = max(start[1], end[1]);
-                       segmentmaxs[2] = max(start[2], end[2]);
-#else
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
-#endif
-                       nodenum = node->back;
-                       break;
-               case 11:
-                       // START END START end
-#ifdef BIHLINECLIP
-                       backfrac = backdist1 / (backdist1 - backdist2);
-                       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]);
-                       segmentmaxs[0] = max(start[0], end[0]);
-                       segmentmaxs[1] = max(start[1], end[1]);
-                       segmentmaxs[2] = max(start[2], end[2]);
-#endif
-                       nodenum = node->back;
-                       break;
-               case 12:
-                       // start end start end
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
-                       nodenum = node->back;
-                       break;
-               case 13:
-                       // START end start end
-#ifdef BIHLINECLIP
-                       frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       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
-                       nodenum = node->back;
-                       break;
-               case 14:
-                       // start END start end
-#ifdef BIHLINECLIP
-                       frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       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
-                       nodenum = node->back;
-                       break;
-               case 15:
-                       // START END start end
-                       nodenum = node->back;
-                       continue;
-               }
-#endif
-#endif
-       }
-       if (!model->collision_bih.leafs)
-               return;
-       leaf = model->collision_bih.leafs + (-1-nodenum);
+                       for (axis = 0;axis < BIH_MAXUNORDEREDCHILDREN && node->children[axis] >= 0;axis++)
+                       {
+                               leaf = bih->leafs + node->children[axis];
 #if 1
-       if (!BoxesOverlap(segmentmins, segmentmaxs, leaf->mins, leaf->maxs))
-               return;
+                               if (!BoxesOverlap(start, start, leaf->mins, leaf->maxs))
+                                       continue;
 #endif
-       switch(leaf->type)
-       {
-       case BIH_BRUSH:
-               brush = model->brush.data_brushes[leaf->itemindex].colbrushf;
-               Collision_TraceLineBrushFloat(trace, linestart, lineend, brush, brush);
-               break;
-       case BIH_COLLISIONTRIANGLE:
-               if (!mod_q3bsp_curves_collisions.integer)
-                       return;
-               e = model->brush.data_collisionelement3i + 3*leaf->itemindex;
-               texture = model->data_textures + leaf->textureindex;
-               Collision_TraceLineTriangleFloat(trace, linestart, lineend, model->brush.data_collisionvertex3f + e[0] * 3, model->brush.data_collisionvertex3f + e[1] * 3, model->brush.data_collisionvertex3f + e[2] * 3, texture->supercontents, texture->surfaceflags, texture);
-               break;
-       case BIH_RENDERTRIANGLE:
-               e = model->surfmesh.data_element3i + 3*leaf->itemindex;
-               texture = model->data_textures + leaf->textureindex;
-               Collision_TraceLineTriangleFloat(trace, linestart, lineend, model->surfmesh.data_vertex3f + e[0] * 3, model->surfmesh.data_vertex3f + e[1] * 3, model->surfmesh.data_vertex3f + e[2] * 3, texture->supercontents, texture->surfaceflags, texture);
-               break;
+                               switch(leaf->type)
+                               {
+                               case BIH_BRUSH:
+                                       brush = model->brush.data_brushes[leaf->itemindex].colbrushf;
+                                       Collision_TracePointBrushFloat(trace, start, brush);
+                                       break;
+                               case BIH_COLLISIONTRIANGLE:
+                                       // collision triangle - skipped because they have no volume
+                                       break;
+                               case BIH_RENDERTRIANGLE:
+                                       // render triangle - skipped because they have no volume
+                                       break;
+                               }
+                       }
+               }
        }
 }
 
-static void Mod_CollisionBIH_TraceBrush_RecursiveBIHNode(trace_t *trace, dp_model_t *model, int nodenum, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, const vec3_t segmentmins, const vec3_t segmentmaxs)
+void Mod_CollisionBIH_TraceLineShared(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t end, int hitsupercontentsmask, const bih_t *bih)
 {
        const bih_leaf_t *leaf;
        const bih_node_t *node;
        const colbrushf_t *brush;
        const int *e;
        const texture_t *texture;
-       int axis;
-       while (nodenum >= 0)
+       vec3_t nodebigmins, nodebigmaxs, nodestart, nodeend, sweepnodemins, sweepnodemaxs;
+       vec_t d1, d2, d3, d4, f, nodestackline[1024][6];
+       int axis, nodenum, nodestackpos = 0, nodestack[1024];
+
+       if (VectorCompare(start, end))
        {
-               node = model->collision_bih.nodes + nodenum;
-               axis = node->type - BIH_SPLITX;
-#if 1
-               if (!BoxesOverlap(segmentmins, segmentmaxs, node->mins, node->maxs))
-                       return;
-#endif
-#if 0
-               Mod_CollisionBIH_TraceBrush_RecursiveBIHNode(trace, model, node->front, thisbrush_start, thisbrush_end, segmentmins, segmentmaxs);
-               nodenum = node->back;
-               continue;
-#endif
-               if (segmentmins[axis] <= node->backmax)
-               {
-                       if (segmentmaxs[axis] >= node->frontmin)
-                               Mod_CollisionBIH_TraceBrush_RecursiveBIHNode(trace, model, node->front, thisbrush_start, thisbrush_end, segmentmins, segmentmaxs);
-                       nodenum = node->back;
-               }
-               else if (segmentmaxs[axis] >= node->frontmin)
-                       nodenum = node->front;
-               else
-                       return; // trace falls between children
-       }
-       if (!model->collision_bih.leafs)
-               return;
-       leaf = model->collision_bih.leafs + (-1-nodenum);
-#if 1
-       if (!BoxesOverlap(segmentmins, segmentmaxs, leaf->mins, leaf->maxs))
+               Mod_CollisionBIH_TracePoint(model, frameblend, skeleton, trace, start, hitsupercontentsmask);
                return;
-#endif
-       switch(leaf->type)
-       {
-       case BIH_BRUSH:
-               brush = model->brush.data_brushes[leaf->itemindex].colbrushf;
-               Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, brush, brush);
-               break;
-       case BIH_COLLISIONTRIANGLE:
-               if (!mod_q3bsp_curves_collisions.integer)
-                       return;
-               e = model->brush.data_collisionelement3i + 3*leaf->itemindex;
-               texture = model->data_textures + leaf->textureindex;
-               Collision_TraceBrushTriangleFloat(trace, thisbrush_start, thisbrush_end, model->brush.data_collisionvertex3f + e[0] * 3, model->brush.data_collisionvertex3f + e[1] * 3, model->brush.data_collisionvertex3f + e[2] * 3, texture->supercontents, texture->surfaceflags, texture);
-               break;
-       case BIH_RENDERTRIANGLE:
-               e = model->surfmesh.data_element3i + 3*leaf->itemindex;
-               texture = model->data_textures + leaf->textureindex;
-               Collision_TraceBrushTriangleFloat(trace, thisbrush_start, thisbrush_end, model->surfmesh.data_vertex3f + e[0] * 3, model->surfmesh.data_vertex3f + e[1] * 3, model->surfmesh.data_vertex3f + e[2] * 3, texture->supercontents, texture->surfaceflags, texture);
-               break;
        }
-}
 
-void Mod_CollisionBIH_TracePoint(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, int hitsupercontentsmask)
-{
+       nodenum = bih->rootnode;
+
        memset(trace, 0, sizeof(*trace));
        trace->fraction = 1;
        trace->realfraction = 1;
        trace->hitsupercontentsmask = hitsupercontentsmask;
-       Mod_CollisionBIH_TracePoint_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, start);
+
+       // push first node
+       nodestackline[nodestackpos][0] = start[0];
+       nodestackline[nodestackpos][1] = start[1];
+       nodestackline[nodestackpos][2] = start[2];
+       nodestackline[nodestackpos][3] = end[0];
+       nodestackline[nodestackpos][4] = end[1];
+       nodestackline[nodestackpos][5] = end[2];
+       nodestack[nodestackpos++] = nodenum;
+       while (nodestackpos)
+       {
+               nodenum = nodestack[--nodestackpos];
+               node = bih->nodes + nodenum;
+               VectorCopy(nodestackline[nodestackpos], nodestart);
+               VectorCopy(nodestackline[nodestackpos] + 3, nodeend);
+               sweepnodemins[0] = min(nodestart[0], nodeend[0]); sweepnodemins[1] = min(nodestart[1], nodeend[1]); sweepnodemins[2] = min(nodestart[2], nodeend[2]); sweepnodemaxs[0] = max(nodestart[0], nodeend[0]); sweepnodemaxs[1] = max(nodestart[1], nodeend[1]); sweepnodemaxs[2] = max(nodestart[2], nodeend[2]);
+               if (!BoxesOverlap(sweepnodemins, sweepnodemaxs, node->mins, node->maxs))
+                       continue;
+               if (node->type <= BIH_SPLITZ && nodestackpos+2 <= 1024)
+               {
+                       // recurse children of the split
+                       axis = node->type - BIH_SPLITX;
+                       d1 = node->backmax - nodestart[axis];
+                       d2 = node->backmax - nodeend[axis];
+                       d3 = nodestart[axis] - node->frontmin;
+                       d4 = nodeend[axis] - node->frontmin;
+                       switch((d1 < 0) | ((d2 < 0) << 1) | ((d3 < 0) << 2) | ((d4 < 0) << 3))
+                       {
+                       case  0: /* >>>> */                     VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                      VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  1: /* <>>> */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                      VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  2: /* ><>> */ f = d1 / (d1 - d2); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                      VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  3: /* <<>> */                                                                                                                                                                                                                      VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  4: /* >><> */                     VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  5: /* <><> */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  6: /* ><<> */ f = d1 / (d1 - d2); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  7: /* <<<> */                                                                                                                                                                                                  f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  8: /* >>>< */                     VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  9: /* <>>< */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case 10: /* ><>< */ f = d1 / (d1 - d2); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case 11: /* <<>< */                                                                                                                                                                                                  f = d3 / (d3 - d4); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case 12: /* >><< */                     VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                                                                                                                                                                                                   break;
+                       case 13: /* <><< */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                                                                                                                                                                                                   break;
+                       case 14: /* ><<< */ f = d1 / (d1 - d2); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                                                                                                                                                                                                   break;
+                       case 15: /* <<<< */                                                                                                                                                                                                                                                                                                                                                                                                   break;
+                       }
+               }
+               else if (node->type == BIH_UNORDERED)
+               {
+                       // calculate sweep bounds for this node
+                       // copy node bounds into local variables
+                       VectorCopy(node->mins, nodebigmins);
+                       VectorCopy(node->maxs, nodebigmaxs);
+                       // clip line to this node bounds
+                       axis = 0; d1 = nodestart[axis] - nodebigmins[axis]; d2 = nodeend[axis] - nodebigmins[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } d1 = nodebigmaxs[axis] - nodestart[axis]; d2 = nodebigmaxs[axis] - nodeend[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); }
+                       axis = 1; d1 = nodestart[axis] - nodebigmins[axis]; d2 = nodeend[axis] - nodebigmins[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } d1 = nodebigmaxs[axis] - nodestart[axis]; d2 = nodebigmaxs[axis] - nodeend[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); }
+                       axis = 2; d1 = nodestart[axis] - nodebigmins[axis]; d2 = nodeend[axis] - nodebigmins[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } d1 = nodebigmaxs[axis] - nodestart[axis]; d2 = nodebigmaxs[axis] - nodeend[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); }
+                       // some of the line intersected the enlarged node box
+                       // calculate sweep bounds for this node
+                       sweepnodemins[0] = min(nodestart[0], nodeend[0]); sweepnodemins[1] = min(nodestart[1], nodeend[1]); sweepnodemins[2] = min(nodestart[2], nodeend[2]); sweepnodemaxs[0] = max(nodestart[0], nodeend[0]); sweepnodemaxs[1] = max(nodestart[1], nodeend[1]); sweepnodemaxs[2] = max(nodestart[2], nodeend[2]);
+                       for (axis = 0;axis < BIH_MAXUNORDEREDCHILDREN && node->children[axis] >= 0;axis++)
+                       {
+                               leaf = bih->leafs + node->children[axis];
+                               if (!BoxesOverlap(sweepnodemins, sweepnodemaxs, leaf->mins, leaf->maxs))
+                                       continue;
+                               switch(leaf->type)
+                               {
+                               case BIH_BRUSH:
+                                       brush = model->brush.data_brushes[leaf->itemindex].colbrushf;
+                                       Collision_TraceLineBrushFloat(trace, start, end, brush, brush);
+                                       break;
+                               case BIH_COLLISIONTRIANGLE:
+                                       if (!mod_q3bsp_curves_collisions.integer)
+                                               continue;
+                                       e = model->brush.data_collisionelement3i + 3*leaf->itemindex;
+                                       texture = model->data_textures + leaf->textureindex;
+                                       Collision_TraceLineTriangleFloat(trace, start, end, model->brush.data_collisionvertex3f + e[0] * 3, model->brush.data_collisionvertex3f + e[1] * 3, model->brush.data_collisionvertex3f + e[2] * 3, texture->supercontents, texture->surfaceflags, texture);
+                                       break;
+                               case BIH_RENDERTRIANGLE:
+                                       e = model->surfmesh.data_element3i + 3*leaf->itemindex;
+                                       texture = model->data_textures + leaf->textureindex;
+                                       Collision_TraceLineTriangleFloat(trace, start, end, model->surfmesh.data_vertex3f + e[0] * 3, model->surfmesh.data_vertex3f + e[1] * 3, model->surfmesh.data_vertex3f + e[2] * 3, texture->supercontents, texture->surfaceflags, texture);
+                                       break;
+                               }
+                       }
+               }
+       }
 }
 
 void Mod_CollisionBIH_TraceLine(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t end, int hitsupercontentsmask)
@@ -6166,55 +6153,157 @@ void Mod_CollisionBIH_TraceLine(dp_model_t *model, const frameblend_t *frameblen
                Mod_CollisionBIH_TracePoint(model, frameblend, skeleton, trace, start, hitsupercontentsmask);
                return;
        }
-
-       memset(trace, 0, sizeof(*trace));
-       trace->fraction = 1;
-       trace->realfraction = 1;
-       trace->hitsupercontentsmask = hitsupercontentsmask;
-       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, start, end, start, end);
+       Mod_CollisionBIH_TraceLineShared(model, frameblend, skeleton, trace, start, end, hitsupercontentsmask, &model->collision_bih);
 }
 
-void Mod_CollisionBIH_TraceBox(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t boxmins, const vec3_t boxmaxs, const vec3_t end, int hitsupercontentsmask)
+void Mod_CollisionBIH_TraceBrush(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, colbrushf_t *thisbrush_start, colbrushf_t *thisbrush_end, int hitsupercontentsmask)
 {
-       float segmentmins[3], segmentmaxs[3];
-       colboxbrushf_t thisbrush_start, thisbrush_end;
-       vec3_t boxstartmins, boxstartmaxs, boxendmins, boxendmaxs;
+       const bih_t *bih;
+       const bih_leaf_t *leaf;
+       const bih_node_t *node;
+       const colbrushf_t *brush;
+       const int *e;
+       const texture_t *texture;
+       vec3_t start, end, startmins, startmaxs, endmins, endmaxs, mins, maxs;
+       vec3_t nodebigmins, nodebigmaxs, nodestart, nodeend, sweepnodemins, sweepnodemaxs;
+       vec_t d1, d2, d3, d4, f, nodestackline[1024][6];
+       int axis, nodenum, nodestackpos = 0, nodestack[1024];
 
-       if (mod_q3bsp_optimizedtraceline.integer && VectorCompare(boxmins, boxmaxs))
+       if (mod_q3bsp_optimizedtraceline.integer && VectorCompare(thisbrush_start->mins, thisbrush_start->maxs) && VectorCompare(thisbrush_end->mins, thisbrush_end->maxs))
        {
-               vec3_t shiftstart, shiftend;
-               VectorAdd(start, boxmins, shiftstart);
-               VectorAdd(end, boxmins, shiftend);
-               if (VectorCompare(start, end))
-                       Mod_CollisionBIH_TracePoint(model, frameblend, skeleton, trace, shiftstart, hitsupercontentsmask);
+               if (VectorCompare(thisbrush_start->mins, thisbrush_end->mins))
+                       Mod_CollisionBIH_TracePoint(model, frameblend, skeleton, trace, thisbrush_start->mins, hitsupercontentsmask);
                else
-               {
-                       Mod_CollisionBIH_TraceLine(model, frameblend, skeleton, trace, shiftstart, shiftend, hitsupercontentsmask);
-                       VectorSubtract(trace->endpos, boxmins, trace->endpos);
-               }
+                       Mod_CollisionBIH_TraceLine(model, frameblend, skeleton, trace, thisbrush_start->mins, thisbrush_end->mins, hitsupercontentsmask);
                return;
        }
 
+       bih = &model->collision_bih;
+       nodenum = bih->rootnode;
+
        // box trace, performed as brush trace
        memset(trace, 0, sizeof(*trace));
        trace->fraction = 1;
        trace->realfraction = 1;
        trace->hitsupercontentsmask = hitsupercontentsmask;
-       segmentmins[0] = min(start[0], end[0]) + boxmins[0] - 1;
-       segmentmins[1] = min(start[1], end[1]) + boxmins[1] - 1;
-       segmentmins[2] = min(start[2], end[2]) + boxmins[2] - 1;
-       segmentmaxs[0] = max(start[0], end[0]) + boxmaxs[0] + 1;
-       segmentmaxs[1] = max(start[1], end[1]) + boxmaxs[1] + 1;
-       segmentmaxs[2] = max(start[2], end[2]) + boxmaxs[2] + 1;
+
+       // calculate tracebox-like parameters for efficient culling
+       VectorMAM(0.5f, thisbrush_start->mins, 0.5f, thisbrush_start->maxs, start);
+       VectorMAM(0.5f, thisbrush_end->mins, 0.5f, thisbrush_end->maxs, end);
+       VectorSubtract(thisbrush_start->mins, start, startmins);
+       VectorSubtract(thisbrush_start->maxs, start, startmaxs);
+       VectorSubtract(thisbrush_end->mins, end, endmins);
+       VectorSubtract(thisbrush_end->maxs, end, endmaxs);
+       mins[0] = min(startmins[0], endmins[0]);
+       mins[1] = min(startmins[1], endmins[1]);
+       mins[2] = min(startmins[2], endmins[2]);
+       maxs[0] = max(startmaxs[0], endmaxs[0]);
+       maxs[1] = max(startmaxs[1], endmaxs[1]);
+       maxs[2] = max(startmaxs[2], endmaxs[2]);
+
+       // push first node
+       nodestackline[nodestackpos][0] = start[0];
+       nodestackline[nodestackpos][1] = start[1];
+       nodestackline[nodestackpos][2] = start[2];
+       nodestackline[nodestackpos][3] = end[0];
+       nodestackline[nodestackpos][4] = end[1];
+       nodestackline[nodestackpos][5] = end[2];
+       nodestack[nodestackpos++] = nodenum;
+       while (nodestackpos)
+       {
+               nodenum = nodestack[--nodestackpos];
+               node = bih->nodes + nodenum;
+               VectorCopy(nodestackline[nodestackpos], nodestart);
+               VectorCopy(nodestackline[nodestackpos] + 3, nodeend);
+               sweepnodemins[0] = min(nodestart[0], nodeend[0]) + mins[0]; sweepnodemins[1] = min(nodestart[1], nodeend[1]) + mins[1]; sweepnodemins[2] = min(nodestart[2], nodeend[2]) + mins[2]; sweepnodemaxs[0] = max(nodestart[0], nodeend[0]) + maxs[0]; sweepnodemaxs[1] = max(nodestart[1], nodeend[1]) + maxs[1]; sweepnodemaxs[2] = max(nodestart[2], nodeend[2]) + maxs[2];
+               if (!BoxesOverlap(sweepnodemins, sweepnodemaxs, node->mins, node->maxs))
+                       continue;
+               if (node->type <= BIH_SPLITZ && nodestackpos+2 <= 1024)
+               {
+                       // recurse children of the split
+                       axis = node->type - BIH_SPLITX;
+                       d1 = node->backmax - nodestart[axis] - mins[axis];
+                       d2 = node->backmax - nodeend[axis] - mins[axis];
+                       d3 = nodestart[axis] - node->frontmin + maxs[axis];
+                       d4 = nodeend[axis] - node->frontmin + maxs[axis];
+                       switch((d1 < 0) | ((d2 < 0) << 1) | ((d3 < 0) << 2) | ((d4 < 0) << 3))
+                       {
+                       case  0: /* >>>> */                     VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                      VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  1: /* <>>> */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                      VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  2: /* ><>> */ f = d1 / (d1 - d2); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                      VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  3: /* <<>> */                                                                                                                                                                                                                      VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  4: /* >><> */                     VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  5: /* <><> */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  6: /* ><<> */ f = d1 / (d1 - d2); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  7: /* <<<> */                                                                                                                                                                                                  f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  8: /* >>>< */                     VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  9: /* <>>< */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case 10: /* ><>< */ f = d1 / (d1 - d2); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case 11: /* <<>< */                                                                                                                                                                                                  f = d3 / (d3 - d4); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case 12: /* >><< */                     VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                                                                                                                                                                                                   break;
+                       case 13: /* <><< */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                                                                                                                                                                                                   break;
+                       case 14: /* ><<< */ f = d1 / (d1 - d2); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                                                                                                                                                                                                   break;
+                       case 15: /* <<<< */                                                                                                                                                                                                                                                                                                                                                                                                   break;
+                       }
+               }
+               else if (node->type == BIH_UNORDERED)
+               {
+                       // calculate sweep bounds for this node
+                       // copy node bounds into local variables and expand to get Minkowski Sum of the two shapes
+                       VectorSubtract(node->mins, maxs, nodebigmins);
+                       VectorSubtract(node->maxs, mins, nodebigmaxs);
+                       // clip line to this node bounds
+                       axis = 0; d1 = nodestart[axis] - nodebigmins[axis]; d2 = nodeend[axis] - nodebigmins[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } d1 = nodebigmaxs[axis] - nodestart[axis]; d2 = nodebigmaxs[axis] - nodeend[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); }
+                       axis = 1; d1 = nodestart[axis] - nodebigmins[axis]; d2 = nodeend[axis] - nodebigmins[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } d1 = nodebigmaxs[axis] - nodestart[axis]; d2 = nodebigmaxs[axis] - nodeend[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); }
+                       axis = 2; d1 = nodestart[axis] - nodebigmins[axis]; d2 = nodeend[axis] - nodebigmins[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } d1 = nodebigmaxs[axis] - nodestart[axis]; d2 = nodebigmaxs[axis] - nodeend[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); }
+                       // some of the line intersected the enlarged node box
+                       // calculate sweep bounds for this node
+                       sweepnodemins[0] = min(nodestart[0], nodeend[0]) + mins[0]; sweepnodemins[1] = min(nodestart[1], nodeend[1]) + mins[1]; sweepnodemins[2] = min(nodestart[2], nodeend[2]) + mins[2]; sweepnodemaxs[0] = max(nodestart[0], nodeend[0]) + maxs[0]; sweepnodemaxs[1] = max(nodestart[1], nodeend[1]) + maxs[1]; sweepnodemaxs[2] = max(nodestart[2], nodeend[2]) + maxs[2];
+                       for (axis = 0;axis < BIH_MAXUNORDEREDCHILDREN && node->children[axis] >= 0;axis++)
+                       {
+                               leaf = bih->leafs + node->children[axis];
+                               if (!BoxesOverlap(sweepnodemins, sweepnodemaxs, leaf->mins, leaf->maxs))
+                                       continue;
+                               switch(leaf->type)
+                               {
+                               case BIH_BRUSH:
+                                       brush = model->brush.data_brushes[leaf->itemindex].colbrushf;
+                                       Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, brush, brush);
+                                       break;
+                               case BIH_COLLISIONTRIANGLE:
+                                       if (!mod_q3bsp_curves_collisions.integer)
+                                               continue;
+                                       e = model->brush.data_collisionelement3i + 3*leaf->itemindex;
+                                       texture = model->data_textures + leaf->textureindex;
+                                       Collision_TraceBrushTriangleFloat(trace, thisbrush_start, thisbrush_end, model->brush.data_collisionvertex3f + e[0] * 3, model->brush.data_collisionvertex3f + e[1] * 3, model->brush.data_collisionvertex3f + e[2] * 3, texture->supercontents, texture->surfaceflags, texture);
+                                       break;
+                               case BIH_RENDERTRIANGLE:
+                                       e = model->surfmesh.data_element3i + 3*leaf->itemindex;
+                                       texture = model->data_textures + leaf->textureindex;
+                                       Collision_TraceBrushTriangleFloat(trace, thisbrush_start, thisbrush_end, model->surfmesh.data_vertex3f + e[0] * 3, model->surfmesh.data_vertex3f + e[1] * 3, model->surfmesh.data_vertex3f + e[2] * 3, texture->supercontents, texture->surfaceflags, texture);
+                                       break;
+                               }
+                       }
+               }
+       }
+}
+
+void Mod_CollisionBIH_TraceBox(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t boxmins, const vec3_t boxmaxs, const vec3_t end, int hitsupercontentsmask)
+{
+       colboxbrushf_t thisbrush_start, thisbrush_end;
+       vec3_t boxstartmins, boxstartmaxs, boxendmins, boxendmaxs;
+
+       // box trace, performed as brush trace
        VectorAdd(start, boxmins, boxstartmins);
        VectorAdd(start, boxmaxs, boxstartmaxs);
        VectorAdd(end, boxmins, boxendmins);
        VectorAdd(end, boxmaxs, boxendmaxs);
        Collision_BrushForBox(&thisbrush_start, boxstartmins, boxstartmaxs, 0, 0, NULL);
        Collision_BrushForBox(&thisbrush_end, boxendmins, boxendmaxs, 0, 0, NULL);
-       Mod_CollisionBIH_TraceBrush_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, &thisbrush_start.brush, &thisbrush_end.brush, segmentmins, segmentmaxs);
+       Mod_CollisionBIH_TraceBrush(model, frameblend, skeleton, trace, &thisbrush_start.brush, &thisbrush_end.brush, hitsupercontentsmask);
 }
 
+
 int Mod_CollisionBIH_PointSuperContents(struct model_s *model, int frame, const vec3_t point)
 {
        trace_t trace;
@@ -6235,7 +6324,7 @@ void Mod_CollisionBIH_TracePoint_Mesh(dp_model_t *model, const frameblend_t *fra
        trace->realfraction = 1;
        trace->hitsupercontentsmask = hitsupercontentsmask;
 #if 0
-       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, start, end, start, end);
+       Mod_CollisionBIH_TraceLine(model, frameblend, skeleton, trace, start, end, hitsupercontentsmask);
        hitsupercontents = trace->hitsupercontents;
        memset(trace, 0, sizeof(*trace));
        trace->fraction = 1;
@@ -6256,7 +6345,7 @@ int Mod_CollisionBIH_PointSuperContents_Mesh(struct model_s *model, int frame, c
        trace.fraction = 1;
        trace.realfraction = 1;
        trace.hitsupercontentsmask = 0;
-       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(&trace, model, model->collision_bih.rootnode, start, end, start, end);
+       Mod_CollisionBIH_TraceLine(model, frameblend, skeleton, trace, start, end, hitsupercontentsmask);
        return trace.hitsupercontents;
 #else
        return 0;
@@ -6487,7 +6576,7 @@ static void Mod_Q3BSP_TracePoint(dp_model_t *model, const frameblend_t *frameble
        trace->realfraction = 1;
        trace->hitsupercontentsmask = hitsupercontentsmask;
        if (mod_collision_bih.integer)
-               Mod_CollisionBIH_TracePoint_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, start);
+               Mod_CollisionBIH_TracePoint(model, frameblend, skeleton, trace, start, hitsupercontentsmask);
        else if (model->brush.submodel)
        {
                for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
@@ -6522,7 +6611,7 @@ static void Mod_Q3BSP_TraceLine(dp_model_t *model, const frameblend_t *frameblen
        segmentmaxs[1] = max(start[1], end[1]) + 1;
        segmentmaxs[2] = max(start[2], end[2]) + 1;
        if (mod_collision_bih.integer)
-               Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, start, end, start, end);
+               Mod_CollisionBIH_TraceLine(model, frameblend, skeleton, trace, start, end, hitsupercontentsmask);
        else if (model->brush.submodel)
        {
                for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
@@ -6537,27 +6626,19 @@ static void Mod_Q3BSP_TraceLine(dp_model_t *model, const frameblend_t *frameblen
                Mod_Q3BSP_TraceLine_RecursiveBSPNode(trace, model, model->brush.data_nodes, start, end, 0, 1, start, end, ++markframe, segmentmins, segmentmaxs);
 }
 
-static void Mod_Q3BSP_TraceBox(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t boxmins, const vec3_t boxmaxs, const vec3_t end, int hitsupercontentsmask)
+void Mod_Q3BSP_TraceBrush(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, colbrushf_t *start, colbrushf_t *end, int hitsupercontentsmask)
 {
-       int i;
        float segmentmins[3], segmentmaxs[3];
+       int i;
        msurface_t *surface;
        q3mbrush_t *brush;
-       colboxbrushf_t thisbrush_start, thisbrush_end;
-       vec3_t boxstartmins, boxstartmaxs, boxendmins, boxendmaxs;
 
-       if (mod_q3bsp_optimizedtraceline.integer && VectorCompare(boxmins, boxmaxs))
+       if (mod_q3bsp_optimizedtraceline.integer && VectorCompare(start->mins, start->maxs) && VectorCompare(end->mins, end->maxs))
        {
-               vec3_t shiftstart, shiftend;
-               VectorAdd(start, boxmins, shiftstart);
-               VectorAdd(end, boxmins, shiftend);
-               if (VectorCompare(start, end))
-                       Mod_Q3BSP_TracePoint(model, frameblend, skeleton, trace, shiftstart, hitsupercontentsmask);
+               if (VectorCompare(start->mins, end->mins))
+                       Mod_Q3BSP_TracePoint(model, frameblend, skeleton, trace, start->mins, hitsupercontentsmask);
                else
-               {
-                       Mod_Q3BSP_TraceLine(model, frameblend, skeleton, trace, shiftstart, shiftend, hitsupercontentsmask);
-                       VectorSubtract(trace->endpos, boxmins, trace->endpos);
-               }
+                       Mod_Q3BSP_TraceLine(model, frameblend, skeleton, trace, start->mins, end->mins, hitsupercontentsmask);
                return;
        }
 
@@ -6566,32 +6647,41 @@ static void Mod_Q3BSP_TraceBox(dp_model_t *model, const frameblend_t *frameblend
        trace->fraction = 1;
        trace->realfraction = 1;
        trace->hitsupercontentsmask = hitsupercontentsmask;
-       segmentmins[0] = min(start[0], end[0]) + boxmins[0] - 1;
-       segmentmins[1] = min(start[1], end[1]) + boxmins[1] - 1;
-       segmentmins[2] = min(start[2], end[2]) + boxmins[2] - 1;
-       segmentmaxs[0] = max(start[0], end[0]) + boxmaxs[0] + 1;
-       segmentmaxs[1] = max(start[1], end[1]) + boxmaxs[1] + 1;
-       segmentmaxs[2] = max(start[2], end[2]) + boxmaxs[2] + 1;
-       VectorAdd(start, boxmins, boxstartmins);
-       VectorAdd(start, boxmaxs, boxstartmaxs);
-       VectorAdd(end, boxmins, boxendmins);
-       VectorAdd(end, boxmaxs, boxendmaxs);
-       Collision_BrushForBox(&thisbrush_start, boxstartmins, boxstartmaxs, 0, 0, NULL);
-       Collision_BrushForBox(&thisbrush_end, boxendmins, boxendmaxs, 0, 0, NULL);
+       segmentmins[0] = min(start->mins[0], end->mins[0]);
+       segmentmins[1] = min(start->mins[1], end->mins[1]);
+       segmentmins[2] = min(start->mins[2], end->mins[2]);
+       segmentmaxs[0] = max(start->maxs[0], end->maxs[0]);
+       segmentmaxs[1] = max(start->maxs[1], end->maxs[1]);
+       segmentmaxs[2] = max(start->maxs[2], end->maxs[2]);
        if (mod_collision_bih.integer)
-               Mod_CollisionBIH_TraceBrush_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, &thisbrush_start.brush, &thisbrush_end.brush, segmentmins, segmentmaxs);
+               Mod_CollisionBIH_TraceBrush(model, frameblend, skeleton, trace, start, end, hitsupercontentsmask);
        else if (model->brush.submodel)
        {
                for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
                        if (brush->colbrushf && BoxesOverlap(segmentmins, segmentmaxs, brush->colbrushf->mins, brush->colbrushf->maxs))
-                               Collision_TraceBrushBrushFloat(trace, &thisbrush_start.brush, &thisbrush_end.brush, brush->colbrushf, brush->colbrushf);
+                               Collision_TraceBrushBrushFloat(trace, start, end, brush->colbrushf, brush->colbrushf);
                if (mod_q3bsp_curves_collisions.integer)
                        for (i = 0, surface = model->data_surfaces + model->firstmodelsurface;i < model->nummodelsurfaces;i++, surface++)
                                if (surface->num_collisiontriangles && BoxesOverlap(segmentmins, segmentmaxs, surface->mins, surface->maxs))
-                                       Collision_TraceBrushTriangleMeshFloat(trace, &thisbrush_start.brush, &thisbrush_end.brush, surface->num_collisiontriangles, surface->deprecatedq3data_collisionelement3i, surface->deprecatedq3data_collisionvertex3f, surface->deprecatedq3num_collisionbboxstride, surface->deprecatedq3data_collisionbbox6f, surface->texture->supercontents, surface->texture->surfaceflags, surface->texture, segmentmins, segmentmaxs);
+                                       Collision_TraceBrushTriangleMeshFloat(trace, start, end, surface->num_collisiontriangles, surface->deprecatedq3data_collisionelement3i, surface->deprecatedq3data_collisionvertex3f, surface->deprecatedq3num_collisionbboxstride, surface->deprecatedq3data_collisionbbox6f, surface->texture->supercontents, surface->texture->surfaceflags, surface->texture, segmentmins, segmentmaxs);
        }
        else
-               Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, model, model->brush.data_nodes, &thisbrush_start.brush, &thisbrush_end.brush, ++markframe, segmentmins, segmentmaxs);
+               Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, model, model->brush.data_nodes, start, end, ++markframe, segmentmins, segmentmaxs);
+}
+
+static void Mod_Q3BSP_TraceBox(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t boxmins, const vec3_t boxmaxs, const vec3_t end, int hitsupercontentsmask)
+{
+       colboxbrushf_t thisbrush_start, thisbrush_end;
+       vec3_t boxstartmins, boxstartmaxs, boxendmins, boxendmaxs;
+
+       // box trace, performed as brush trace
+       VectorAdd(start, boxmins, boxstartmins);
+       VectorAdd(start, boxmaxs, boxstartmaxs);
+       VectorAdd(end, boxmins, boxendmins);
+       VectorAdd(end, boxmaxs, boxendmaxs);
+       Collision_BrushForBox(&thisbrush_start, boxstartmins, boxstartmaxs, 0, 0, NULL);
+       Collision_BrushForBox(&thisbrush_end, boxendmins, boxendmaxs, 0, 0, NULL);
+       Mod_Q3BSP_TraceBrush(model, frameblend, skeleton, trace, &thisbrush_start.brush, &thisbrush_end.brush, hitsupercontentsmask);
 }
 
 static int Mod_Q3BSP_PointSuperContents(struct model_s *model, int frame, const vec3_t point)
@@ -6632,6 +6722,12 @@ static int Mod_Q3BSP_PointSuperContents(struct model_s *model, int frame, const
        return supercontents;
 }
 
+void Mod_CollisionBIH_TraceLineAgainstSurfaces(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t end, int hitsupercontentsmask)
+{
+       Mod_CollisionBIH_TraceLineShared(model, frameblend, skeleton, trace, start, end, hitsupercontentsmask, &model->render_bih);
+}
+
+
 bih_t *Mod_MakeCollisionBIH(dp_model_t *model, qboolean userendersurfaces, bih_t *out)
 {
        int j;
@@ -6746,7 +6842,7 @@ bih_t *Mod_MakeCollisionBIH(dp_model_t *model, qboolean userendersurfaces, bih_t
        }
 
        // allocate buffers for the produced and temporary data
-       bihmaxnodes = bihnumleafs - 1;
+       bihmaxnodes = bihnumleafs + 1;
        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;
@@ -6842,7 +6938,7 @@ void Mod_Q3BSP_RecursiveFindNumLeafs(mnode_t *node)
 
 void Mod_Q3BSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
 {
-       int i, j, numshadowmeshtriangles, lumps;
+       int i, j, lumps;
        q3dheader_t *header;
        float corner[3], yawradius, modelradius;
 
@@ -6862,9 +6958,11 @@ void Mod_Q3BSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
 
        mod->soundfromcenter = true;
        mod->TraceBox = Mod_Q3BSP_TraceBox;
+       mod->TraceBrush = Mod_Q3BSP_TraceBrush;
        mod->TraceLine = Mod_Q3BSP_TraceLine;
        mod->TracePoint = Mod_Q3BSP_TracePoint;
        mod->PointSuperContents = Mod_Q3BSP_PointSuperContents;
+       mod->TraceLineAgainstSurfaces = Mod_CollisionBIH_TraceLine;
        mod->brush.TraceLineOfSight = Mod_Q3BSP_TraceLineOfSight;
        mod->brush.SuperContentsFromNativeContents = Mod_Q3BSP_SuperContentsFromNativeContents;
        mod->brush.NativeContentsFromSuperContents = Mod_Q3BSP_NativeContentsFromSuperContents;
@@ -6956,13 +7054,14 @@ void Mod_Q3BSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
        loadmodel->brush.numsubmodels = loadmodel->brushq3.num_models;
 
        // the MakePortals code works fine on the q3bsp data as well
-       Mod_Q1BSP_MakePortals();
+       if (mod_bsp_portalize.integer)
+               Mod_Q1BSP_MakePortals();
 
        // FIXME: shader alpha should replace r_wateralpha support in q3bsp
        loadmodel->brush.supportwateralpha = true;
 
        // make a single combined shadow mesh to allow optimized shadow volume creation
-       numshadowmeshtriangles = Mod_Q1BSP_CreateShadowMesh(loadmodel);
+       Mod_Q1BSP_CreateShadowMesh(loadmodel);
 
        loadmodel->brush.num_leafs = 0;
        Mod_Q3BSP_RecursiveFindNumLeafs(loadmodel->brush.data_nodes);
@@ -7159,8 +7258,10 @@ void Mod_OBJ_Load(dp_model_t *mod, void *buffer, void *bufferend)
        loadmodel->type = mod_obj;
        loadmodel->soundfromcenter = true;
        loadmodel->TraceBox = Mod_CollisionBIH_TraceBox;
+       loadmodel->TraceBrush = Mod_CollisionBIH_TraceBrush;
        loadmodel->TraceLine = Mod_CollisionBIH_TraceLine;
        loadmodel->TracePoint = Mod_CollisionBIH_TracePoint_Mesh;
+       loadmodel->TraceLineAgainstSurfaces = Mod_CollisionBIH_TraceLine;
        loadmodel->PointSuperContents = Mod_CollisionBIH_PointSuperContents_Mesh;
        loadmodel->brush.TraceLineOfSight = NULL;
        loadmodel->brush.SuperContentsFromNativeContents = NULL;
@@ -7313,9 +7414,12 @@ void Mod_OBJ_Load(dp_model_t *mod, void *buffer, void *bufferend)
                                vcurrent.nextindex = -1;
                                vcurrent.textureindex = textureindex;
                                vcurrent.submodelindex = submodelindex;
-                               VectorCopy(v + 3*index1, vcurrent.v);
-                               Vector2Copy(vt + 2*index2, vcurrent.vt);
-                               VectorCopy(vn + 3*index3, vcurrent.vn);
+                               if (v && index1 >= 0 && index1 < numv)
+                                       VectorCopy(v + 3*index1, vcurrent.v);
+                               if (vt && index2 >= 0 && index2 < numvt)
+                                       Vector2Copy(vt + 2*index2, vcurrent.vt);
+                               if (vn && index3 >= 0 && index3 < numvn)
+                                       VectorCopy(vn + 3*index3, vcurrent.vn);
                                if (numtriangles == 0)
                                {
                                        VectorCopy(vcurrent.v, mins);
@@ -7487,13 +7591,14 @@ void Mod_OBJ_Load(dp_model_t *mod, void *buffer, void *bufferend)
        // allocate storage for final mesh data
        loadmodel->num_textures = numtextures * loadmodel->numskins;
        loadmodel->num_texturesperskin = numtextures;
-       data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(int) + loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t) + numtriangles * sizeof(int[3]) + (numvertices <= 65536 ? numtriangles * sizeof(unsigned short[3]) : 0) + numvertices * sizeof(float[14]) + loadmodel->brush.numsubmodels * sizeof(dp_model_t *));
+       data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(int) + loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t) + numtriangles * sizeof(int[3]) + (numvertices <= 65536 ? numtriangles * sizeof(unsigned short[3]) : 0) + (r_enableshadowvolumes.integer ? numtriangles * sizeof(int[3]) : 0) + numvertices * sizeof(float[14]) + loadmodel->brush.numsubmodels * sizeof(dp_model_t *));
        loadmodel->brush.submodels = (dp_model_t **)data;data += loadmodel->brush.numsubmodels * sizeof(dp_model_t *);
        loadmodel->sortedmodelsurfaces = (int *)data;data += loadmodel->num_surfaces * sizeof(int);
        loadmodel->data_textures = (texture_t *)data;data += loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t);
        loadmodel->surfmesh.num_vertices = numvertices;
        loadmodel->surfmesh.num_triangles = numtriangles;
-       loadmodel->surfmesh.data_neighbor3i = (int *)data;data += numtriangles * sizeof(int[3]);
+       if (r_enableshadowvolumes.integer)
+               loadmodel->surfmesh.data_neighbor3i = (int *)data;data += numtriangles * sizeof(int[3]);
        loadmodel->surfmesh.data_vertex3f = (float *)data;data += numvertices * sizeof(float[3]);
        loadmodel->surfmesh.data_svector3f = (float *)data;data += numvertices * sizeof(float[3]);
        loadmodel->surfmesh.data_tvector3f = (float *)data;data += numvertices * sizeof(float[3]);
@@ -7537,9 +7642,10 @@ void Mod_OBJ_Load(dp_model_t *mod, void *buffer, void *bufferend)
        Mod_ValidateElements(loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles, 0, loadmodel->surfmesh.num_vertices, __FILE__, __LINE__);
        // generate normals if the file did not have them
        if (!VectorLength2(loadmodel->surfmesh.data_normal3f))
-               Mod_BuildNormals(0, loadmodel->surfmesh.num_vertices, loadmodel->surfmesh.num_triangles, loadmodel->surfmesh.data_vertex3f, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.data_normal3f, true);
-       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_BuildNormals(0, loadmodel->surfmesh.num_vertices, loadmodel->surfmesh.num_triangles, loadmodel->surfmesh.data_vertex3f, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.data_normal3f, r_smoothnormals_areaweighting.integer != 0);
+       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, r_smoothnormals_areaweighting.integer != 0);
+       if (loadmodel->surfmesh.data_neighbor3i)
+               Mod_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles);
 
        // if this is a worldmodel and has no BSP tree, create a fake one for the purpose
        loadmodel->brush.num_visleafs = 1;
@@ -8146,6 +8252,7 @@ void Mod_OBJ_Load(dp_model_t *mod, void *buffer, void *bufferend)
        loadmodel->TraceLine = Mod_OBJ_TraceLine;
        loadmodel->TracePoint = Mod_OBJ_TracePoint;
        loadmodel->PointSuperContents = Mod_OBJ_PointSuperContents;
+       loadmodel->TraceLineAgainstSurfaces = Mod_OBJ_TraceLineAgainstSurfaces;
        loadmodel->brush.TraceLineOfSight = Mod_OBJ_TraceLineOfSight;
        loadmodel->brush.SuperContentsFromNativeContents = Mod_OBJ_SuperContentsFromNativeContents;
        loadmodel->brush.NativeContentsFromSuperContents = Mod_OBJ_NativeContentsFromSuperContents;
@@ -8439,14 +8546,15 @@ void Mod_OBJ_Load(dp_model_t *mod, void *buffer, void *bufferend)
 
        loadmodel->num_surfaces = 1;
        loadmodel->nummodelsurfaces = loadmodel->num_surfaces;
-       data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int) + loadmodel->numframes * sizeof(animscene_t) + loadmodel->numframes * sizeof(float[6]) + loadmodel->surfmesh.num_triangles * sizeof(int[3]) + loadmodel->surfmesh.num_triangles * sizeof(int[3]));
+       data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int) + loadmodel->numframes * sizeof(animscene_t) + loadmodel->numframes * sizeof(float[6]) + loadmodel->surfmesh.num_triangles * sizeof(int[3]) + (r_enableshadowvolume.integer ? loadmodel->surfmesh.num_triangles * sizeof(int[3]) : 0));
        loadmodel->data_surfaces = (msurface_t *)data;data += loadmodel->num_surfaces * sizeof(msurface_t);
        loadmodel->sortedmodelsurfaces = (int *)data;data += loadmodel->num_surfaces * sizeof(int);
        loadmodel->sortedmodelsurfaces[0] = 0;
        loadmodel->animscenes = (animscene_t *)data;data += loadmodel->numframes * sizeof(animscene_t);
        loadmodel->surfmesh.data_morphmd2framesize6f = (float *)data;data += loadmodel->numframes * sizeof(float[6]);
        loadmodel->surfmesh.data_element3i = (int *)data;data += loadmodel->surfmesh.num_triangles * sizeof(int[3]);
-       loadmodel->surfmesh.data_neighbor3i = (int *)data;data += loadmodel->surfmesh.num_triangles * sizeof(int[3]);
+       if (r_enableshadowvolumes.integer)
+               loadmodel->surfmesh.data_neighbor3i = (int *)data;data += loadmodel->surfmesh.num_triangles * sizeof(int[3]);
 
        loadmodel->synctype = ST_RAND;
 
@@ -8588,7 +8696,8 @@ void Mod_OBJ_Load(dp_model_t *mod, void *buffer, void *bufferend)
        Mem_Free(vertremap);
 
        Mod_MakeSortedSurfaces(loadmodel);
-       Mod_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles);
+       if (loadmodel->surfmesh.data_neighbor3i)
+               Mod_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles);
        Mod_Alias_CalculateBoundingBox();
        Mod_Alias_MorphMesh_CompileFrames();