]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - model_brush.c
now parses more of q3 shaders to guess at proper rendering settings for surfaces...
[xonotic/darkplaces.git] / model_brush.c
index 591e9ef4692d5e0dfe96ffcad761cb3a085b7bef..5f432355234b0597e1da0e25f4698727c24d4908 100644 (file)
@@ -21,7 +21,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include "quakedef.h"
 #include "image.h"
 #include "r_shadow.h"
-#include "winding.h"
+#include "polygon.h"
 #include "curves.h"
 
 // note: model_shared.c sets up r_notexture, and r_surf_notexture
@@ -1202,7 +1202,8 @@ static void Mod_Q1BSP_LoadLighting(lump_t *l)
        if (loadmodel->brush.ishlbsp) // LordHavoc: load the colored lighting data straight
        {
                loadmodel->brushq1.lightdata = Mem_Alloc(loadmodel->mempool, l->filelen);
-               memcpy(loadmodel->brushq1.lightdata, mod_base + l->fileofs, l->filelen);
+               for (i=0; i<l->filelen; i++)
+                       loadmodel->brushq1.lightdata[i] = mod_base[l->fileofs+i] >>= 1;
        }
        else // LordHavoc: bsp version 29 (normal white lighting)
        {
@@ -2207,12 +2208,15 @@ static void Mod_Q1BSP_LoadPlanes(lump_t *l)
        }
 }
 
+#define MAX_PORTALPOINTS 64
+
 typedef struct portal_s
 {
        mplane_t plane;
        mnode_t *nodes[2];              // [0] = front side of plane
        struct portal_s *next[2];
-       winding_t *winding;
+       int numpoints;
+       double points[3*MAX_PORTALPOINTS];
        struct portal_s *chain; // all portals are linked into a list
 }
 portal_t;
@@ -2262,7 +2266,6 @@ static void Mod_Q1BSP_FinalizePortals(void)
        mportal_t *portal;
        mvertex_t *point;
        mleaf_t *leaf, *endleaf;
-       winding_t *w;
 
        // recalculate bounding boxes for all leafs(because qbsp is very sloppy)
        leaf = loadmodel->brushq1.data_leafs;
@@ -2275,20 +2278,19 @@ static void Mod_Q1BSP_FinalizePortals(void)
        p = portalchain;
        while (p)
        {
-               if (p->winding)
+               if (p->numpoints >= 3)
                {
                        for (i = 0;i < 2;i++)
                        {
                                leaf = (mleaf_t *)p->nodes[i];
-                               w = p->winding;
-                               for (j = 0;j < w->numpoints;j++)
+                               for (j = 0;j < p->numpoints;j++)
                                {
-                                       if (leaf->mins[0] > w->points[j][0]) leaf->mins[0] = w->points[j][0];
-                                       if (leaf->mins[1] > w->points[j][1]) leaf->mins[1] = w->points[j][1];
-                                       if (leaf->mins[2] > w->points[j][2]) leaf->mins[2] = w->points[j][2];
-                                       if (leaf->maxs[0] < w->points[j][0]) leaf->maxs[0] = w->points[j][0];
-                                       if (leaf->maxs[1] < w->points[j][1]) leaf->maxs[1] = w->points[j][1];
-                                       if (leaf->maxs[2] < w->points[j][2]) leaf->maxs[2] = w->points[j][2];
+                                       if (leaf->mins[0] > p->points[j*3+0]) leaf->mins[0] = p->points[j*3+0];
+                                       if (leaf->mins[1] > p->points[j*3+1]) leaf->mins[1] = p->points[j*3+1];
+                                       if (leaf->mins[2] > p->points[j*3+2]) leaf->mins[2] = p->points[j*3+2];
+                                       if (leaf->maxs[0] < p->points[j*3+0]) leaf->maxs[0] = p->points[j*3+0];
+                                       if (leaf->maxs[1] < p->points[j*3+1]) leaf->maxs[1] = p->points[j*3+1];
+                                       if (leaf->maxs[2] < p->points[j*3+2]) leaf->maxs[2] = p->points[j*3+2];
                                }
                        }
                }
@@ -2305,12 +2307,12 @@ static void Mod_Q1BSP_FinalizePortals(void)
        {
                // note: this check must match the one below or it will usually corrupt memory
                // the nodes[0] != nodes[1] check is because leaf 0 is the shared solid leaf, it can have many portals inside with leaf 0 on both sides
-               if (p->winding && p->nodes[0] != p->nodes[1]
+               if (p->numpoints >= 3 && p->nodes[0] != p->nodes[1]
                 && p->nodes[0]->contents != CONTENTS_SOLID && p->nodes[1]->contents != CONTENTS_SOLID
                 && p->nodes[0]->contents != CONTENTS_SKY && p->nodes[1]->contents != CONTENTS_SKY)
                {
                        numportals += 2;
-                       numpoints += p->winding->numpoints * 2;
+                       numpoints += p->numpoints * 2;
                }
                p = p->chain;
        }
@@ -2330,7 +2332,7 @@ static void Mod_Q1BSP_FinalizePortals(void)
        {
                pnext = p->chain;
 
-               if (p->winding)
+               if (p->numpoints >= 3)
                {
                        // note: this check must match the one above or it will usually corrupt memory
                        // the nodes[0] != nodes[1] check is because leaf 0 is the shared solid leaf, it can have many portals inside with leaf 0 on both sides
@@ -2340,7 +2342,7 @@ static void Mod_Q1BSP_FinalizePortals(void)
                        {
                                // first make the back to front portal(forward portal)
                                portal->points = point;
-                               portal->numpoints = p->winding->numpoints;
+                               portal->numpoints = p->numpoints;
                                portal->plane.dist = p->plane.dist;
                                VectorCopy(p->plane.normal, portal->plane.normal);
                                portal->here = (mleaf_t *)p->nodes[1];
@@ -2348,7 +2350,7 @@ static void Mod_Q1BSP_FinalizePortals(void)
                                // copy points
                                for (j = 0;j < portal->numpoints;j++)
                                {
-                                       VectorCopy(p->winding->points[j], point->position);
+                                       VectorCopy(p->points + j*3, point->position);
                                        point++;
                                }
                                PlaneClassify(&portal->plane);
@@ -2362,7 +2364,7 @@ static void Mod_Q1BSP_FinalizePortals(void)
 
                                // then make the front to back portal(backward portal)
                                portal->points = point;
-                               portal->numpoints = p->winding->numpoints;
+                               portal->numpoints = p->numpoints;
                                portal->plane.dist = -p->plane.dist;
                                VectorNegate(p->plane.normal, portal->plane.normal);
                                portal->here = (mleaf_t *)p->nodes[0];
@@ -2370,7 +2372,7 @@ static void Mod_Q1BSP_FinalizePortals(void)
                                // copy points
                                for (j = portal->numpoints - 1;j >= 0;j--)
                                {
-                                       VectorCopy(p->winding->points[j], point->position);
+                                       VectorCopy(p->points + j*3, point->position);
                                        point++;
                                }
                                PlaneClassify(&portal->plane);
@@ -2382,7 +2384,6 @@ static void Mod_Q1BSP_FinalizePortals(void)
                                // advance to next portal
                                portal++;
                        }
-                       Winding_Free(p->winding);
                }
                FreePortal(p);
                p = pnext;
@@ -2464,11 +2465,12 @@ static void RemovePortalFromNodes(portal_t *portal)
 
 static void Mod_Q1BSP_RecursiveNodePortals(mnode_t *node)
 {
-       int side;
+       int i, side;
        mnode_t *front, *back, *other_node;
        mplane_t clipplane, *plane;
        portal_t *portal, *nextportal, *nodeportal, *splitportal, *temp;
-       winding_t *nodeportalwinding, *frontwinding, *backwinding;
+       int numfrontpoints, numbackpoints;
+       double frontpoints[3*MAX_PORTALPOINTS], backpoints[3*MAX_PORTALPOINTS];
 
        // if a leaf, we're done
        if (node->contents)
@@ -2486,7 +2488,8 @@ static void Mod_Q1BSP_RecursiveNodePortals(mnode_t *node)
        nodeportal = AllocPortal();
        nodeportal->plane = *plane;
 
-       nodeportalwinding = Winding_NewFromPlane(nodeportal->plane.normal[0], nodeportal->plane.normal[1], nodeportal->plane.normal[2], nodeportal->plane.dist);
+       PolygonD_QuadForPlane(nodeportal->points, nodeportal->plane.normal[0], nodeportal->plane.normal[1], nodeportal->plane.normal[2], nodeportal->plane.dist, 1024.0*1024.0*1024.0);
+       nodeportal->numpoints = 4;
        side = 0;       // shut up compiler warning
        for (portal = (portal_t *)node->portals;portal;portal = portal->next[side])
        {
@@ -2504,76 +2507,86 @@ static void Mod_Q1BSP_RecursiveNodePortals(mnode_t *node)
                else
                        Host_Error("Mod_Q1BSP_RecursiveNodePortals: mislinked portal");
 
-               nodeportalwinding = Winding_Clip(nodeportalwinding, clipplane.normal[0], clipplane.normal[1], clipplane.normal[2], clipplane.dist, true);
-               if (!nodeportalwinding)
-               {
-                       Con_Print("Mod_Q1BSP_RecursiveNodePortals: WARNING: new portal was clipped away\n");
+               for (i = 0;i < nodeportal->numpoints*3;i++)
+                       frontpoints[i] = nodeportal->points[i];
+               PolygonD_Divide(nodeportal->numpoints, frontpoints, clipplane.normal[0], clipplane.normal[1], clipplane.normal[2], clipplane.dist, 1.0/32.0, MAX_PORTALPOINTS, nodeportal->points, &nodeportal->numpoints, 0, NULL, NULL);
+               if (nodeportal->numpoints <= 0 || nodeportal->numpoints >= MAX_PORTALPOINTS)
                        break;
-               }
        }
 
-       if (nodeportalwinding)
+       if (nodeportal->numpoints < 3)
        {
-               // if the plane was not clipped on all sides, there was an error
-               nodeportal->winding = nodeportalwinding;
-               AddPortalToNodes(nodeportal, front, back);
+               Con_Print("Mod_Q1BSP_RecursiveNodePortals: WARNING: new portal was clipped away\n");
+               nodeportal->numpoints = 0;
        }
-
-       // split the portals of this node along this node's plane and assign them to the children of this node
-       // (migrating the portals downward through the tree)
-       for (portal = (portal_t *)node->portals;portal;portal = nextportal)
+       else if (nodeportal->numpoints >= MAX_PORTALPOINTS)
        {
-               if (portal->nodes[0] == portal->nodes[1])
-                       Host_Error("Mod_Q1BSP_RecursiveNodePortals: portal has same node on both sides(2)");
-               if (portal->nodes[0] == node)
-                       side = 0;
-               else if (portal->nodes[1] == node)
-                       side = 1;
-               else
-                       Host_Error("Mod_Q1BSP_RecursiveNodePortals: mislinked portal");
-               nextportal = portal->next[side];
-
-               other_node = portal->nodes[!side];
-               RemovePortalFromNodes(portal);
-
-               // cut the portal into two portals, one on each side of the node plane
-               Winding_Divide(portal->winding, plane->normal[0], plane->normal[1], plane->normal[2], plane->dist, &frontwinding, &backwinding);
+               Con_Print("Mod_Q1BSP_RecursiveNodePortals: WARNING: new portal has too many points\n");
+               nodeportal->numpoints = 0;
+       }
+       else
+       {
+               AddPortalToNodes(nodeportal, front, back);
 
-               if (!frontwinding)
+               // split the portals of this node along this node's plane and assign them to the children of this node
+               // (migrating the portals downward through the tree)
+               for (portal = (portal_t *)node->portals;portal;portal = nextportal)
                {
-                       if (side == 0)
-                               AddPortalToNodes(portal, back, other_node);
+                       if (portal->nodes[0] == portal->nodes[1])
+                               Host_Error("Mod_Q1BSP_RecursiveNodePortals: portal has same node on both sides(2)");
+                       if (portal->nodes[0] == node)
+                               side = 0;
+                       else if (portal->nodes[1] == node)
+                               side = 1;
                        else
-                               AddPortalToNodes(portal, other_node, back);
-                       continue;
-               }
-               if (!backwinding)
-               {
+                               Host_Error("Mod_Q1BSP_RecursiveNodePortals: mislinked portal");
+                       nextportal = portal->next[side];
+
+                       other_node = portal->nodes[!side];
+                       RemovePortalFromNodes(portal);
+
+                       // cut the portal into two portals, one on each side of the node plane
+                       PolygonD_Divide(portal->numpoints, portal->points, plane->normal[0], plane->normal[1], plane->normal[2], plane->dist, 1.0/32.0, MAX_PORTALPOINTS, frontpoints, &numfrontpoints, MAX_PORTALPOINTS, backpoints, &numbackpoints); 
+
+                       if (!numfrontpoints)
+                       {
+                               if (side == 0)
+                                       AddPortalToNodes(portal, back, other_node);
+                               else
+                                       AddPortalToNodes(portal, other_node, back);
+                               continue;
+                       }
+                       if (!numbackpoints)
+                       {
+                               if (side == 0)
+                                       AddPortalToNodes(portal, front, other_node);
+                               else
+                                       AddPortalToNodes(portal, other_node, front);
+                               continue;
+                       }
+
+                       // the portal is split
+                       splitportal = AllocPortal();
+                       temp = splitportal->chain;
+                       *splitportal = *portal;
+                       splitportal->chain = temp;
+                       for (i = 0;i < numbackpoints*3;i++)
+                               splitportal->points[i] = backpoints[i];
+                       splitportal->numpoints = numbackpoints;
+                       for (i = 0;i < numfrontpoints*3;i++)
+                               portal->points[i] = frontpoints[i];
+                       portal->numpoints = numfrontpoints;
+
                        if (side == 0)
+                       {
                                AddPortalToNodes(portal, front, other_node);
+                               AddPortalToNodes(splitportal, back, other_node);
+                       }
                        else
+                       {
                                AddPortalToNodes(portal, other_node, front);
-                       continue;
-               }
-
-               // the winding is split
-               splitportal = AllocPortal();
-               temp = splitportal->chain;
-               *splitportal = *portal;
-               splitportal->chain = temp;
-               splitportal->winding = backwinding;
-               Winding_Free(portal->winding);
-               portal->winding = frontwinding;
-
-               if (side == 0)
-               {
-                       AddPortalToNodes(portal, front, other_node);
-                       AddPortalToNodes(splitportal, back, other_node);
-               }
-               else
-               {
-                       AddPortalToNodes(portal, other_node, front);
-                       AddPortalToNodes(splitportal, other_node, back);
+                               AddPortalToNodes(splitportal, other_node, back);
+                       }
                }
        }
 
@@ -2876,7 +2889,7 @@ void Mod_Q1BSP_Load(model_t *mod, void *buffer)
        msurface_t *surf;
        int numshadowmeshtriangles;
 
-       mod->type = mod_brush;
+       mod->type = mod_brushq1;
 
        header = (dheader_t *)buffer;
 
@@ -2909,8 +2922,12 @@ void Mod_Q1BSP_Load(model_t *mod, void *buffer)
 // swap all the lumps
        mod_base = (qbyte *)header;
 
-       for (i = 0;i < (int) sizeof(dheader_t) / 4;i++)
-               ((int *)header)[i] = LittleLong(((int *)header)[i]);
+       header->version = LittleLong(header->version);
+       for (i = 0;i < HEADER_LUMPS;i++)
+       {
+               header->lumps[i].fileofs = LittleLong(header->lumps[i].fileofs);
+               header->lumps[i].filelen = LittleLong(header->lumps[i].filelen);
+       }
 
 // load into heap
 
@@ -2984,12 +3001,14 @@ void Mod_Q1BSP_Load(model_t *mod, void *buffer)
                // LordHavoc: this code was originally at the end of this loop, but
                // has been transformed to something more readable at the start here.
 
-               // LordHavoc: only register submodels if it is the world
-               // (prevents external bsp models from replacing world submodels with
-               //  their own)
-               if (loadmodel->isworldmodel && i)
+               if (i > 0)
                {
                        char name[10];
+                       // LordHavoc: only register submodels if it is the world
+                       // (prevents external bsp models from replacing world submodels with
+                       //  their own)
+                       if (!loadmodel->isworldmodel)
+                               continue;
                        // duplicate the basic information
                        sprintf(name, "*%i", i);
                        mod = Mod_FindName(name);
@@ -3578,9 +3597,11 @@ static void Mod_Q3BSP_LoadTextures(lump_t *l)
        fssearch_t *search;
        char *f;
        const char *text;
-       int flags;
+       int flags, flags2, numparameters, passnumber;
        char shadername[Q3PATHLENGTH];
        char sky[Q3PATHLENGTH];
+       char firstpasstexturename[Q3PATHLENGTH];
+       char parameter[4][Q3PATHLENGTH];
 
        in = (void *)(mod_base + l->fileofs);
        if (l->filelen % sizeof(*in))
@@ -3598,7 +3619,6 @@ static void Mod_Q3BSP_LoadTextures(lump_t *l)
                out->surfaceflags = LittleLong(in->surfaceflags);
                out->nativecontents = LittleLong(in->contents);
                out->supercontents = Mod_Q3BSP_SuperContentsFromNativeContents(loadmodel, out->nativecontents);
-               Mod_LoadSkinFrame(&out->skin, out->name, TEXF_MIPMAP | TEXF_ALPHA | TEXF_PRECACHE | TEXF_PICMIP, false, true, true);
                out->surfaceparms = -1;
        }
 
@@ -3614,7 +3634,10 @@ static void Mod_Q3BSP_LoadTextures(lump_t *l)
                                {
                                        strlcpy (shadername, com_token, sizeof (shadername));
                                        flags = 0;
+                                       flags2 = 0;
                                        sky[0] = 0;
+                                       passnumber = 0;
+                                       firstpasstexturename[0] = 0;
                                        if (COM_ParseToken(&text, false) && !strcasecmp(com_token, "{"))
                                        {
                                                while (COM_ParseToken(&text, false))
@@ -3627,103 +3650,152 @@ static void Mod_Q3BSP_LoadTextures(lump_t *l)
                                                                {
                                                                        if (!strcasecmp(com_token, "}"))
                                                                                break;
+                                                                       if (!strcasecmp(com_token, "\n"))
+                                                                               continue;
+                                                                       numparameters = 0;
+                                                                       for (j = 0;strcasecmp(com_token, "\n") && strcasecmp(com_token, "}");j++)
+                                                                       {
+                                                                               if (j < 4)
+                                                                               {
+                                                                                       strlcpy(parameter[j], com_token, sizeof(parameter[j]));
+                                                                                       numparameters = j + 1;
+                                                                               }
+                                                                               if (!COM_ParseToken(&text, true))
+                                                                                       break;
+                                                                       }
+                                                                       Con_Printf("%s %i: ", shadername, passnumber);
+                                                                       for (j = 0;j < numparameters;j++)
+                                                                               Con_Printf(" %s", parameter[j]);
+                                                                       Con_Print("\n");
+                                                                       if (passnumber == 0 && numparameters >= 1)
+                                                                       {
+                                                                               if (!strcasecmp(parameter[0], "blendfunc"))
+                                                                               {
+                                                                                       Con_Printf("!\n");
+                                                                                       if (numparameters == 2 && !strcasecmp(parameter[1], "add"))
+                                                                                               flags2 |= Q3TEXTUREFLAG_ADDITIVE;
+                                                                                       else if (numparameters == 3 && !strcasecmp(parameter[1], "gl_one") && !strcasecmp(parameter[2], "gl_one"))
+                                                                                               flags2 |= Q3TEXTUREFLAG_ADDITIVE;
+                                                                                       else if (numparameters == 3 && !strcasecmp(parameter[1], "gl_src_alpha") && !strcasecmp(parameter[2], "gl_one"))
+                                                                                               flags2 |= Q3TEXTUREFLAG_ADDITIVE;
+                                                                               }
+                                                                               else if (numparameters >= 2 && (!strcasecmp(parameter[0], "map") || !strcasecmp(parameter[0], "clampmap")))
+                                                                                       strlcpy(firstpasstexturename, parameter[1], sizeof(firstpasstexturename));
+                                                                               else if (numparameters >= 3 && !strcasecmp(parameter[0], "animmap"))
+                                                                                       strlcpy(firstpasstexturename, parameter[2], sizeof(firstpasstexturename));
+                                                                       }
+                                                                       // break out a level if it was }
+                                                                       if (!strcasecmp(com_token, "}"))
+                                                                               break;
                                                                }
+                                                               passnumber++;
+                                                               continue;
                                                        }
-                                                       else if (!strcasecmp(com_token, "surfaceparm"))
+                                                       numparameters = 0;
+                                                       for (j = 0;strcasecmp(com_token, "\n") && strcasecmp(com_token, "}");j++)
                                                        {
-                                                               if (COM_ParseToken(&text, true) && strcasecmp(com_token, "\n"))
+                                                               if (j < 4)
                                                                {
-                                                                       if (!strcasecmp(com_token, "alphashadow"))
-                                                                               flags |= Q3SURFACEPARM_ALPHASHADOW;
-                                                                       else if (!strcasecmp(com_token, "areaportal"))
-                                                                               flags |= Q3SURFACEPARM_AREAPORTAL;
-                                                                       else if (!strcasecmp(com_token, "clusterportal"))
-                                                                               flags |= Q3SURFACEPARM_CLUSTERPORTAL;
-                                                                       else if (!strcasecmp(com_token, "detail"))
-                                                                               flags |= Q3SURFACEPARM_DETAIL;
-                                                                       else if (!strcasecmp(com_token, "donotenter"))
-                                                                               flags |= Q3SURFACEPARM_DONOTENTER;
-                                                                       else if (!strcasecmp(com_token, "fog"))
-                                                                               flags |= Q3SURFACEPARM_FOG;
-                                                                       else if (!strcasecmp(com_token, "lava"))
-                                                                               flags |= Q3SURFACEPARM_LAVA;
-                                                                       else if (!strcasecmp(com_token, "lightfilter"))
-                                                                               flags |= Q3SURFACEPARM_LIGHTFILTER;
-                                                                       else if (!strcasecmp(com_token, "metalsteps"))
-                                                                               flags |= Q3SURFACEPARM_METALSTEPS;
-                                                                       else if (!strcasecmp(com_token, "nodamage"))
-                                                                               flags |= Q3SURFACEPARM_NODAMAGE;
-                                                                       else if (!strcasecmp(com_token, "nodlight"))
-                                                                               flags |= Q3SURFACEPARM_NODLIGHT;
-                                                                       else if (!strcasecmp(com_token, "nodraw"))
-                                                                               flags |= Q3SURFACEPARM_NODRAW;
-                                                                       else if (!strcasecmp(com_token, "nodrop"))
-                                                                               flags |= Q3SURFACEPARM_NODROP;
-                                                                       else if (!strcasecmp(com_token, "noimpact"))
-                                                                               flags |= Q3SURFACEPARM_NOIMPACT;
-                                                                       else if (!strcasecmp(com_token, "nolightmap"))
-                                                                               flags |= Q3SURFACEPARM_NOLIGHTMAP;
-                                                                       else if (!strcasecmp(com_token, "nomarks"))
-                                                                               flags |= Q3SURFACEPARM_NOMARKS;
-                                                                       else if (!strcasecmp(com_token, "nomipmaps"))
-                                                                               flags |= Q3SURFACEPARM_NOMIPMAPS;
-                                                                       else if (!strcasecmp(com_token, "nonsolid"))
-                                                                               flags |= Q3SURFACEPARM_NONSOLID;
-                                                                       else if (!strcasecmp(com_token, "origin"))
-                                                                               flags |= Q3SURFACEPARM_ORIGIN;
-                                                                       else if (!strcasecmp(com_token, "playerclip"))
-                                                                               flags |= Q3SURFACEPARM_PLAYERCLIP;
-                                                                       else if (!strcasecmp(com_token, "sky"))
-                                                                               flags |= Q3SURFACEPARM_SKY;
-                                                                       else if (!strcasecmp(com_token, "slick"))
-                                                                               flags |= Q3SURFACEPARM_SLICK;
-                                                                       else if (!strcasecmp(com_token, "slime"))
-                                                                               flags |= Q3SURFACEPARM_SLIME;
-                                                                       else if (!strcasecmp(com_token, "structural"))
-                                                                               flags |= Q3SURFACEPARM_STRUCTURAL;
-                                                                       else if (!strcasecmp(com_token, "trans"))
-                                                                               flags |= Q3SURFACEPARM_TRANS;
-                                                                       else if (!strcasecmp(com_token, "water"))
-                                                                               flags |= Q3SURFACEPARM_WATER;
-                                                                       else
-                                                                               Con_Printf("%s parsing warning: unknown surfaceparm \"%s\"\n", search->filenames[i], com_token);
-                                                                       if (!COM_ParseToken(&text, true) || strcasecmp(com_token, "\n"))
-                                                                       {
-                                                                               Con_Printf("%s parsing error: surfaceparm only takes one parameter.\n", search->filenames[i]);
-                                                                               goto parseerror;
-                                                                       }
+                                                                       strlcpy(parameter[j], com_token, sizeof(parameter[j]));
+                                                                       numparameters = j + 1;
                                                                }
+                                                               if (!COM_ParseToken(&text, true))
+                                                                       break;
+                                                       }
+                                                       if (i == 0 && !strcasecmp(com_token, "}"))
+                                                               break;
+                                                       Con_Printf("%s: ", shadername);
+                                                       for (j = 0;j < numparameters;j++)
+                                                               Con_Printf(" %s", parameter[j]);
+                                                       Con_Print("\n");
+                                                       if (numparameters < 1)
+                                                               continue;
+                                                       if (!strcasecmp(parameter[0], "surfaceparm") && numparameters >= 2)
+                                                       {
+                                                               if (!strcasecmp(parameter[1], "alphashadow"))
+                                                                       flags |= Q3SURFACEPARM_ALPHASHADOW;
+                                                               else if (!strcasecmp(parameter[1], "areaportal"))
+                                                                       flags |= Q3SURFACEPARM_AREAPORTAL;
+                                                               else if (!strcasecmp(parameter[1], "clusterportal"))
+                                                                       flags |= Q3SURFACEPARM_CLUSTERPORTAL;
+                                                               else if (!strcasecmp(parameter[1], "detail"))
+                                                                       flags |= Q3SURFACEPARM_DETAIL;
+                                                               else if (!strcasecmp(parameter[1], "donotenter"))
+                                                                       flags |= Q3SURFACEPARM_DONOTENTER;
+                                                               else if (!strcasecmp(parameter[1], "fog"))
+                                                                       flags |= Q3SURFACEPARM_FOG;
+                                                               else if (!strcasecmp(parameter[1], "lava"))
+                                                                       flags |= Q3SURFACEPARM_LAVA;
+                                                               else if (!strcasecmp(parameter[1], "lightfilter"))
+                                                                       flags |= Q3SURFACEPARM_LIGHTFILTER;
+                                                               else if (!strcasecmp(parameter[1], "metalsteps"))
+                                                                       flags |= Q3SURFACEPARM_METALSTEPS;
+                                                               else if (!strcasecmp(parameter[1], "nodamage"))
+                                                                       flags |= Q3SURFACEPARM_NODAMAGE;
+                                                               else if (!strcasecmp(parameter[1], "nodlight"))
+                                                                       flags |= Q3SURFACEPARM_NODLIGHT;
+                                                               else if (!strcasecmp(parameter[1], "nodraw"))
+                                                                       flags |= Q3SURFACEPARM_NODRAW;
+                                                               else if (!strcasecmp(parameter[1], "nodrop"))
+                                                                       flags |= Q3SURFACEPARM_NODROP;
+                                                               else if (!strcasecmp(parameter[1], "noimpact"))
+                                                                       flags |= Q3SURFACEPARM_NOIMPACT;
+                                                               else if (!strcasecmp(parameter[1], "nolightmap"))
+                                                                       flags |= Q3SURFACEPARM_NOLIGHTMAP;
+                                                               else if (!strcasecmp(parameter[1], "nomarks"))
+                                                                       flags |= Q3SURFACEPARM_NOMARKS;
+                                                               else if (!strcasecmp(parameter[1], "nomipmaps"))
+                                                                       flags |= Q3SURFACEPARM_NOMIPMAPS;
+                                                               else if (!strcasecmp(parameter[1], "nonsolid"))
+                                                                       flags |= Q3SURFACEPARM_NONSOLID;
+                                                               else if (!strcasecmp(parameter[1], "origin"))
+                                                                       flags |= Q3SURFACEPARM_ORIGIN;
+                                                               else if (!strcasecmp(parameter[1], "playerclip"))
+                                                                       flags |= Q3SURFACEPARM_PLAYERCLIP;
+                                                               else if (!strcasecmp(parameter[1], "sky"))
+                                                                       flags |= Q3SURFACEPARM_SKY;
+                                                               else if (!strcasecmp(parameter[1], "slick"))
+                                                                       flags |= Q3SURFACEPARM_SLICK;
+                                                               else if (!strcasecmp(parameter[1], "slime"))
+                                                                       flags |= Q3SURFACEPARM_SLIME;
+                                                               else if (!strcasecmp(parameter[1], "structural"))
+                                                                       flags |= Q3SURFACEPARM_STRUCTURAL;
+                                                               else if (!strcasecmp(parameter[1], "trans"))
+                                                                       flags |= Q3SURFACEPARM_TRANS;
+                                                               else if (!strcasecmp(parameter[1], "water"))
+                                                                       flags |= Q3SURFACEPARM_WATER;
                                                                else
-                                                               {
-                                                                       Con_Printf("%s parsing error: surfaceparm expects a parameter.\n", search->filenames[i]);
-                                                                       goto parseerror;
-                                                               }
+                                                                       Con_Printf("%s parsing warning: unknown surfaceparm \"%s\"\n", search->filenames[i], parameter[1]);
                                                        }
-                                                       else if (!strcasecmp(com_token, "sky"))
+                                                       else if (!strcasecmp(parameter[0], "sky") && numparameters >= 2)
+                                                               strlcpy(sky, parameter[1], sizeof(sky));
+                                                       else if (!strcasecmp(parameter[0], "skyparms") && numparameters >= 2)
                                                        {
-                                                               if (COM_ParseToken(&text, true) && strcasecmp(com_token, "\n"))
-                                                                       if (strlen(com_token) < sizeof(sky))
-                                                                               strcpy(sky, com_token);
+                                                               if (!atoi(parameter[1]) && strcasecmp(parameter[1], "-"))
+                                                                       strlcpy(sky, parameter[1], sizeof(sky));
                                                        }
-                                                       else if (!strcasecmp(com_token, "skyparms"))
+                                                       else if (!strcasecmp(parameter[0], "cull") && numparameters >= 2)
                                                        {
-                                                               if (COM_ParseToken(&text, true) && strcasecmp(com_token, "\n"))
-                                                               {
-                                                                       if (strlen(com_token) < sizeof(sky) && !atoi(com_token) && strcasecmp(com_token, "-"))
-                                                                               strcpy(sky, com_token);
-                                                                       if (COM_ParseToken(&text, true) && strcasecmp(com_token, "\n"))
-                                                                               COM_ParseToken(&text, true);
-                                                               }
+                                                               if (!strcasecmp(parameter[1], "disable") || !strcasecmp(parameter[1], "none") || !strcasecmp(parameter[1], "twosided"))
+                                                                       flags2 |= Q3TEXTUREFLAG_TWOSIDED;
                                                        }
-                                                       else
+                                                       else if (!strcasecmp(parameter[0], "nomipmaps"))
+                                                               flags2 |= Q3TEXTUREFLAG_NOMIPMAPS;
+                                                       else if (!strcasecmp(parameter[0], "nopicmip"))
+                                                               flags2 |= Q3TEXTUREFLAG_NOPICMIP;
+                                                       else if (!strcasecmp(parameter[0], "deformvertexes") && numparameters >= 2)
                                                        {
-                                                               // look for linebreak or }
-                                                               while(COM_ParseToken(&text, true) && strcasecmp(com_token, "\n") && strcasecmp(com_token, "}"));
-                                                               // break out to top level if it was }
-                                                               if (!strcasecmp(com_token, "}"))
-                                                                       break;
+                                                               if (!strcasecmp(parameter[1], "autosprite") && numparameters == 2)
+                                                                       flags2 |= Q3TEXTUREFLAG_AUTOSPRITE;
+                                                               if (!strcasecmp(parameter[1], "autosprite2") && numparameters == 2)
+                                                                       flags2 |= Q3TEXTUREFLAG_AUTOSPRITE2;
                                                        }
                                                }
+                                               // force transparent render path for a number of odd
+                                               // shader effects to avoid bogging down the normal
+                                               // render path unnecessarily
+                                               if (flags2 & (Q3TEXTUREFLAG_ADDITIVE | Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
+                                                       flags |= Q3SURFACEPARM_TRANS;
                                                // add shader to list (shadername and flags)
                                                // actually here we just poke into the texture settings
                                                for (j = 0, out = loadmodel->brushq3.data_textures;j < loadmodel->brushq3.num_textures;j++, out++)
@@ -3731,6 +3803,8 @@ static void Mod_Q3BSP_LoadTextures(lump_t *l)
                                                        if (!strcasecmp(out->name, shadername))
                                                        {
                                                                out->surfaceparms = flags;
+                                                               out->textureflags = flags2;
+                                                               strlcpy(out->firstpasstexturename, firstpasstexturename, sizeof(out->firstpasstexturename));
                                                                if ((flags & Q3SURFACEPARM_SKY) && sky[0])
                                                                {
                                                                        // quake3 seems to append a _ to the skybox name, so this must do so as well
@@ -3760,14 +3834,16 @@ parseerror:
                        Con_DPrintf("%s: No shader found for texture \"%s\"\n", loadmodel->name, out->name);
                        out->surfaceparms = 0;
                        // these are defaults
-                       if (!strcmp(out->name, "caulk") || !strcmp(out->name, "common/caulk") || !strcmp(out->name, "textures/common/caulk")
-                        || !strcmp(out->name, "nodraw") || !strcmp(out->name, "common/nodraw") || !strcmp(out->name, "textures/common/nodraw"))
-                               out->surfaceparms |= Q3SURFACEPARM_NODRAW;
                        if (!strncmp(out->name, "textures/skies/", 15))
                                out->surfaceparms |= Q3SURFACEPARM_SKY;
-                       if (R_TextureHasAlpha(out->skin.base))
-                               out->surfaceparms |= Q3SURFACEPARM_TRANS;
+                       //if (!strcmp(out->name, "caulk") || !strcmp(out->name, "common/caulk") || !strcmp(out->name, "textures/common/caulk")
+                       // || !strcmp(out->name, "nodraw") || !strcmp(out->name, "common/nodraw") || !strcmp(out->name, "textures/common/nodraw"))
+                       //      out->surfaceparms |= Q3SURFACEPARM_NODRAW;
+                       //if (R_TextureHasAlpha(out->skin.base))
+                       //      out->surfaceparms |= Q3SURFACEPARM_TRANS;
                }
+               if (!Mod_LoadSkinFrame(&out->skin, out->name, (((out->textureflags & Q3TEXTUREFLAG_NOMIPMAPS) || (out->surfaceparms & Q3SURFACEPARM_NOMIPMAPS)) ? 0 : TEXF_MIPMAP) | TEXF_ALPHA | TEXF_PRECACHE | (out->textureflags & Q3TEXTUREFLAG_NOPICMIP ? 0 : TEXF_PICMIP), false, true, true))
+                       Mod_LoadSkinFrame(&out->skin, out->firstpasstexturename, (((out->textureflags & Q3TEXTUREFLAG_NOMIPMAPS) || (out->surfaceparms & Q3SURFACEPARM_NOMIPMAPS)) ? 0 : TEXF_MIPMAP) | TEXF_ALPHA | TEXF_PRECACHE | (out->textureflags & Q3TEXTUREFLAG_NOPICMIP ? 0 : TEXF_PICMIP), false, true, true);
        }
        Con_DPrintf("%s: %i textures missing shaders\n", loadmodel->name, c);
 }
@@ -3831,7 +3907,6 @@ static void Mod_Q3BSP_LoadBrushes(lump_t *l)
        q3mbrush_t *out;
        int i, j, n, c, count, maxplanes;
        mplane_t *planes;
-       winding_t *temp1, *temp2;
 
        in = (void *)(mod_base + l->fileofs);
        if (l->filelen % sizeof(*in))
@@ -3842,9 +3917,6 @@ static void Mod_Q3BSP_LoadBrushes(lump_t *l)
        loadmodel->brushq3.data_brushes = out;
        loadmodel->brushq3.num_brushes = count;
 
-       temp1 = Winding_New(64);
-       temp2 = Winding_New(64);
-
        maxplanes = 0;
        planes = NULL;
 
@@ -3875,12 +3947,10 @@ static void Mod_Q3BSP_LoadBrushes(lump_t *l)
                        planes[j].dist = out->firstbrushside[j].plane->dist;
                }
                // make the colbrush from the planes
-               out->colbrushf = Collision_NewBrushFromPlanes(loadmodel->mempool, out->numbrushsides, planes, out->texture->supercontents, temp1, temp2);
+               out->colbrushf = Collision_NewBrushFromPlanes(loadmodel->mempool, out->numbrushsides, planes, out->texture->supercontents);
        }
        if (planes)
                Mem_Free(planes);
-       Winding_Free(temp1);
-       Winding_Free(temp2);
 }
 
 static void Mod_Q3BSP_LoadEffects(lump_t *l)
@@ -4004,7 +4074,7 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
 {
        q3dface_t *in;
        q3msurface_t *out;
-       int i, j, n, count, invalidelements, patchsize[2], finalwidth, finalheight, xlevel, ylevel, row0, row1, x, y, *e, finalvertices, finaltriangles;
+       int i, j, n, count, invalidelements, patchsize[2], finalwidth, finalheight, xlevel, ylevel, row0, row1, x, y, *e, finalvertices, finaltriangles, firstvertex, firstelement, type;
        //int *originalelement3i;
        //int *originalneighbor3i;
        float *originalvertex3f;
@@ -4028,16 +4098,16 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
        for (i = 0;i < count;i++, in++, out++)
        {
                // check face type first
-               out->type = LittleLong(in->type);
-               if (out->type != Q3FACETYPE_POLYGON
-                && out->type != Q3FACETYPE_PATCH
-                && out->type != Q3FACETYPE_MESH
-                && out->type != Q3FACETYPE_FLARE)
+               type = LittleLong(in->type);
+               if (type != Q3FACETYPE_POLYGON
+                && type != Q3FACETYPE_PATCH
+                && type != Q3FACETYPE_MESH
+                && type != Q3FACETYPE_FLARE)
                {
-                       Con_DPrintf("Mod_Q3BSP_LoadFaces: face #%i: unknown face type %i\n", i, out->type);
+                       Con_DPrintf("Mod_Q3BSP_LoadFaces: face #%i: unknown face type %i\n", i, type);
                        out->num_vertices = 0;
                        out->num_triangles = 0;
-                       out->type = 0; // error
+                       type = 0; // error
                        continue;
                }
 
@@ -4047,7 +4117,7 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
                        Con_DPrintf("Mod_Q3BSP_LoadFaces: face #%i: invalid textureindex %i (%i textures)\n", i, n, loadmodel->brushq3.num_textures);
                        out->num_vertices = 0;
                        out->num_triangles = 0;
-                       out->type = 0; // error
+                       type = 0; // error
                        continue;
                        n = 0;
                }
@@ -4073,48 +4143,48 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
                else
                        out->lightmaptexture = loadmodel->brushq3.data_lightmaps[n];
 
-               out->firstvertex = LittleLong(in->firstvertex);
+               firstvertex = LittleLong(in->firstvertex);
                out->num_vertices = LittleLong(in->numvertices);
-               out->firstelement = LittleLong(in->firstelement);
+               firstelement = LittleLong(in->firstelement);
                out->num_triangles = LittleLong(in->numelements) / 3;
                if (out->num_triangles * 3 != LittleLong(in->numelements))
                {
                        Con_Printf("Mod_Q3BSP_LoadFaces: face #%i (texture \"%s\"): numelements %i is not a multiple of 3\n", i, out->texture->name, LittleLong(in->numelements));
                        out->num_vertices = 0;
                        out->num_triangles = 0;
-                       out->type = 0; // error
+                       type = 0; // error
                        continue;
                }
-               if (out->firstvertex < 0 || out->firstvertex + out->num_vertices > loadmodel->brushq3.num_vertices)
+               if (firstvertex < 0 || firstvertex + out->num_vertices > loadmodel->brushq3.num_vertices)
                {
-                       Con_Printf("Mod_Q3BSP_LoadFaces: face #%i (texture \"%s\"): invalid vertex range %i : %i (%i vertices)\n", i, out->texture->name, out->firstvertex, out->firstvertex + out->num_vertices, loadmodel->brushq3.num_vertices);
+                       Con_Printf("Mod_Q3BSP_LoadFaces: face #%i (texture \"%s\"): invalid vertex range %i : %i (%i vertices)\n", i, out->texture->name, firstvertex, firstvertex + out->num_vertices, loadmodel->brushq3.num_vertices);
                        out->num_vertices = 0;
                        out->num_triangles = 0;
-                       out->type = 0; // error
+                       type = 0; // error
                        continue;
                }
-               if (out->firstelement < 0 || out->firstelement + out->num_triangles * 3 > loadmodel->brushq3.num_triangles * 3)
+               if (firstelement < 0 || firstelement + out->num_triangles * 3 > loadmodel->brushq3.num_triangles * 3)
                {
-                       Con_Printf("Mod_Q3BSP_LoadFaces: face #%i (texture \"%s\"): invalid element range %i : %i (%i elements)\n", i, out->texture->name, out->firstelement, out->firstelement + out->num_triangles * 3, loadmodel->brushq3.num_triangles * 3);
+                       Con_Printf("Mod_Q3BSP_LoadFaces: face #%i (texture \"%s\"): invalid element range %i : %i (%i elements)\n", i, out->texture->name, firstelement, firstelement + out->num_triangles * 3, loadmodel->brushq3.num_triangles * 3);
                        out->num_vertices = 0;
                        out->num_triangles = 0;
-                       out->type = 0; // error
+                       type = 0; // error
                        continue;
                }
-               switch(out->type)
+               switch(type)
                {
                case Q3FACETYPE_POLYGON:
                case Q3FACETYPE_MESH:
                        // no processing necessary
-                       out->data_vertex3f = loadmodel->brushq3.data_vertex3f + out->firstvertex * 3;
-                       out->data_texcoordtexture2f = loadmodel->brushq3.data_texcoordtexture2f + out->firstvertex * 2;
-                       out->data_texcoordlightmap2f = loadmodel->brushq3.data_texcoordlightmap2f + out->firstvertex * 2;
-                       out->data_svector3f = loadmodel->brushq3.data_svector3f + out->firstvertex * 3;
-                       out->data_tvector3f = loadmodel->brushq3.data_tvector3f + out->firstvertex * 3;
-                       out->data_normal3f = loadmodel->brushq3.data_normal3f + out->firstvertex * 3;
-                       out->data_color4f = loadmodel->brushq3.data_color4f + out->firstvertex * 4;
-                       out->data_element3i = loadmodel->brushq3.data_element3i + out->firstelement;
-                       out->data_neighbor3i = loadmodel->brushq3.data_neighbor3i + out->firstelement;
+                       out->data_vertex3f = loadmodel->brushq3.data_vertex3f + firstvertex * 3;
+                       out->data_texcoordtexture2f = loadmodel->brushq3.data_texcoordtexture2f + firstvertex * 2;
+                       out->data_texcoordlightmap2f = loadmodel->brushq3.data_texcoordlightmap2f + firstvertex * 2;
+                       out->data_svector3f = loadmodel->brushq3.data_svector3f + firstvertex * 3;
+                       out->data_tvector3f = loadmodel->brushq3.data_tvector3f + firstvertex * 3;
+                       out->data_normal3f = loadmodel->brushq3.data_normal3f + firstvertex * 3;
+                       out->data_color4f = loadmodel->brushq3.data_color4f + firstvertex * 4;
+                       out->data_element3i = loadmodel->brushq3.data_element3i + firstelement;
+                       out->data_neighbor3i = loadmodel->brushq3.data_neighbor3i + firstelement;
                        break;
                case Q3FACETYPE_PATCH:
                        patchsize[0] = LittleLong(in->specific.patch.patchsize[0]);
@@ -4124,18 +4194,18 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
                                Con_Printf("Mod_Q3BSP_LoadFaces: face #%i (texture \"%s\"): invalid patchsize %ix%i\n", i, out->texture->name, patchsize[0], patchsize[1]);
                                out->num_vertices = 0;
                                out->num_triangles = 0;
-                               out->type = 0; // error
+                               type = 0; // error
                                continue;
                        }
-                       originalvertex3f = loadmodel->brushq3.data_vertex3f + out->firstvertex * 3;
-                       //originalsvector3f = loadmodel->brushq3.data_svector3f + out->firstvertex * 3;
-                       //originaltvector3f = loadmodel->brushq3.data_tvector3f + out->firstvertex * 3;
-                       //originalnormal3f = loadmodel->brushq3.data_normal3f + out->firstvertex * 3;
-                       originaltexcoordtexture2f = loadmodel->brushq3.data_texcoordtexture2f + out->firstvertex * 2;
-                       originaltexcoordlightmap2f = loadmodel->brushq3.data_texcoordlightmap2f + out->firstvertex * 2;
-                       originalcolor4f = loadmodel->brushq3.data_color4f + out->firstvertex * 4;
-                       //originalelement3i = loadmodel->brushq3.data_element3i + out->firstelement;
-                       //originalneighbor3i = loadmodel->brushq3.data_neighbor3i + out->firstelement;
+                       originalvertex3f = loadmodel->brushq3.data_vertex3f + firstvertex * 3;
+                       //originalsvector3f = loadmodel->brushq3.data_svector3f + firstvertex * 3;
+                       //originaltvector3f = loadmodel->brushq3.data_tvector3f + firstvertex * 3;
+                       //originalnormal3f = loadmodel->brushq3.data_normal3f + firstvertex * 3;
+                       originaltexcoordtexture2f = loadmodel->brushq3.data_texcoordtexture2f + firstvertex * 2;
+                       originaltexcoordlightmap2f = loadmodel->brushq3.data_texcoordlightmap2f + firstvertex * 2;
+                       originalcolor4f = loadmodel->brushq3.data_color4f + firstvertex * 4;
+                       //originalelement3i = loadmodel->brushq3.data_element3i + firstelement;
+                       //originalneighbor3i = loadmodel->brushq3.data_neighbor3i + firstelement;
                        /*
                        originalvertex3f = out->data_vertex3f;
                        //originalsvector3f = out->data_svector3f;
@@ -4148,8 +4218,8 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
                        //originalneighbor3i = out->data_neighbor3i;
                        */
                        // convert patch to Q3FACETYPE_MESH
-                       xlevel = QuadraticSplinePatchSubdivisionLevelOnX(patchsize[0], patchsize[1], 3, originalvertex3f, r_subdivisions_tolerance.value, 10);
-                       ylevel = QuadraticSplinePatchSubdivisionLevelOnY(patchsize[0], patchsize[1], 3, originalvertex3f, r_subdivisions_tolerance.value, 10);
+                       xlevel = QuadraticBSplinePatchSubdivisionLevelOnX(patchsize[0], patchsize[1], 3, originalvertex3f, r_subdivisions_tolerance.value, 10);
+                       ylevel = QuadraticBSplinePatchSubdivisionLevelOnY(patchsize[0], patchsize[1], 3, originalvertex3f, r_subdivisions_tolerance.value, 10);
                        // bound to user settings
                        xlevel = bound(r_subdivisions_minlevel.integer, xlevel, r_subdivisions_maxlevel.integer);
                        ylevel = bound(r_subdivisions_minlevel.integer, ylevel, r_subdivisions_maxlevel.integer);
@@ -4177,17 +4247,17 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
                        out->data_texcoordlightmap2f = out->data_texcoordtexture2f + finalvertices * 2;
                        out->data_element3i = Mem_Alloc(loadmodel->mempool, sizeof(int[6]) * finaltriangles);
                        out->data_neighbor3i = out->data_element3i + finaltriangles * 3;
-                       out->type = Q3FACETYPE_MESH;
-                       out->firstvertex = -1;
+                       type = Q3FACETYPE_MESH;
+                       firstvertex = -1;
                        out->num_vertices = finalvertices;
-                       out->firstelement = -1;
+                       firstelement = -1;
                        out->num_triangles = finaltriangles;
                        // generate geometry
                        // (note: normals are skipped because they get recalculated)
-                       QuadraticSplinePatchSubdivideFloatBuffer(patchsize[0], patchsize[1], xlevel, ylevel, 3, originalvertex3f, out->data_vertex3f);
-                       QuadraticSplinePatchSubdivideFloatBuffer(patchsize[0], patchsize[1], xlevel, ylevel, 2, originaltexcoordtexture2f, out->data_texcoordtexture2f);
-                       QuadraticSplinePatchSubdivideFloatBuffer(patchsize[0], patchsize[1], xlevel, ylevel, 2, originaltexcoordlightmap2f, out->data_texcoordlightmap2f);
-                       QuadraticSplinePatchSubdivideFloatBuffer(patchsize[0], patchsize[1], xlevel, ylevel, 4, originalcolor4f, out->data_color4f);
+                       QuadraticBSplinePatchSubdivideFloatBuffer(patchsize[0], patchsize[1], xlevel, ylevel, 3, originalvertex3f, out->data_vertex3f);
+                       QuadraticBSplinePatchSubdivideFloatBuffer(patchsize[0], patchsize[1], xlevel, ylevel, 2, originaltexcoordtexture2f, out->data_texcoordtexture2f);
+                       QuadraticBSplinePatchSubdivideFloatBuffer(patchsize[0], patchsize[1], xlevel, ylevel, 2, originaltexcoordlightmap2f, out->data_texcoordlightmap2f);
+                       QuadraticBSplinePatchSubdivideFloatBuffer(patchsize[0], patchsize[1], xlevel, ylevel, 4, originalcolor4f, out->data_color4f);
                        // generate elements
                        e = out->data_element3i;
                        for (y = 0;y < finalheight - 1;y++)
@@ -4216,9 +4286,8 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
                        }
                        // q3map does not put in collision brushes for curves... ugh
                        // build the lower quality collision geometry
-                       out->collisions = true;
-                       xlevel = QuadraticSplinePatchSubdivisionLevelOnX(patchsize[0], patchsize[1], 3, originalvertex3f, r_subdivisions_collision_tolerance.value, 10);
-                       ylevel = QuadraticSplinePatchSubdivisionLevelOnY(patchsize[0], patchsize[1], 3, originalvertex3f, r_subdivisions_collision_tolerance.value, 10);
+                       xlevel = QuadraticBSplinePatchSubdivisionLevelOnX(patchsize[0], patchsize[1], 3, originalvertex3f, r_subdivisions_collision_tolerance.value, 10);
+                       ylevel = QuadraticBSplinePatchSubdivisionLevelOnY(patchsize[0], patchsize[1], 3, originalvertex3f, r_subdivisions_collision_tolerance.value, 10);
                        // bound to user settings
                        xlevel = bound(r_subdivisions_collision_minlevel.integer, xlevel, r_subdivisions_collision_maxlevel.integer);
                        ylevel = bound(r_subdivisions_collision_minlevel.integer, ylevel, r_subdivisions_collision_maxlevel.integer);
@@ -4241,7 +4310,7 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
                        out->data_collisionelement3i = Mem_Alloc(loadmodel->mempool, sizeof(int[3]) * finaltriangles);
                        out->num_collisionvertices = finalvertices;
                        out->num_collisiontriangles = finaltriangles;
-                       QuadraticSplinePatchSubdivideFloatBuffer(patchsize[0], patchsize[1], xlevel, ylevel, 3, originalvertex3f, out->data_collisionvertex3f);
+                       QuadraticBSplinePatchSubdivideFloatBuffer(patchsize[0], patchsize[1], xlevel, ylevel, 3, originalvertex3f, out->data_collisionvertex3f);
                        // generate elements
                        e = out->data_collisionelement3i;
                        for (y = 0;y < finalheight - 1;y++)
@@ -4274,7 +4343,7 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
                        // don't render it
                        out->num_vertices = 0;
                        out->num_triangles = 0;
-                       out->type = 0;
+                       type = 0;
                        break;
                }
                for (j = 0, invalidelements = 0;j < out->num_triangles * 3;j++)
@@ -4282,7 +4351,7 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
                                invalidelements++;
                if (invalidelements)
                {
-                       Con_Printf("Mod_Q3BSP_LoadFaces: Warning: face #%i has %i invalid elements, type = %i, texture->name = \"%s\", texture->surfaceflags = %i, texture->nativecontents = %i, firstvertex = %i, numvertices = %i, firstelement = %i, numelements = %i, elements list:\n", i, invalidelements, out->type, out->texture->name, out->texture->surfaceflags, out->texture->nativecontents, out->firstvertex, out->num_vertices, out->firstelement, out->num_triangles * 3);
+                       Con_Printf("Mod_Q3BSP_LoadFaces: Warning: face #%i has %i invalid elements, type = %i, texture->name = \"%s\", texture->surfaceflags = %i, texture->nativecontents = %i, firstvertex = %i, numvertices = %i, firstelement = %i, numelements = %i, elements list:\n", i, invalidelements, type, out->texture->name, out->texture->surfaceflags, out->texture->nativecontents, firstvertex, out->num_vertices, firstelement, out->num_triangles * 3);
                        for (j = 0;j < out->num_triangles * 3;j++)
                        {
                                Con_Printf(" %i", out->data_element3i[j]);
@@ -4486,12 +4555,15 @@ static void Mod_Q3BSP_LoadLeafFaces(lump_t *l)
        loadmodel->brushq3.data_leaffaces = out;
        loadmodel->brushq3.num_leaffaces = count;
 
+       loadmodel->brushq3.data_leaffacenums = Mem_Alloc(loadmodel->mempool, count * sizeof(int));
+
        for (i = 0;i < count;i++, in++, out++)
        {
                n = LittleLong(*in);
                if (n < 0 || n >= loadmodel->brushq3.num_faces)
                        Host_Error("Mod_Q3BSP_LoadLeafFaces: invalid face index %i (%i faces)\n", n, loadmodel->brushq3.num_faces);
                *out = loadmodel->brushq3.data_faces + n;
+               loadmodel->brushq3.data_leaffacenums[i] = n;
        }
 }
 
@@ -4527,6 +4599,7 @@ static void Mod_Q3BSP_LoadLeafs(lump_t *l)
                if (n < 0 || n + c > loadmodel->brushq3.num_leaffaces)
                        Host_Error("Mod_Q3BSP_LoadLeafs: invalid leafface range %i : %i (%i leaffaces)\n", n, n + c, loadmodel->brushq3.num_leaffaces);
                out->firstleafface = loadmodel->brushq3.data_leaffaces + n;
+               out->firstleaffacenum = loadmodel->brushq3.data_leaffacenums + n;
                out->numleaffaces = c;
                n = LittleLong(in->firstleafbrush);
                c = LittleLong(in->numleafbrushes);
@@ -4853,7 +4926,7 @@ static void Mod_Q3BSP_TraceLine_RecursiveBSPNode(trace_t *trace, q3mnode_t *node
                for (i = 0;i < leaf->numleaffaces;i++)
                {
                        face = leaf->firstleafface[i];
-                       if (face->collisions && face->collisionmarkframe != markframe && BoxesOverlap(nodesegmentmins, nodesegmentmaxs, face->mins, face->maxs))
+                       if (face->num_collisiontriangles && face->collisionmarkframe != markframe && BoxesOverlap(nodesegmentmins, nodesegmentmaxs, face->mins, face->maxs))
                        {
                                face->collisionmarkframe = markframe;
                                Collision_TraceLineTriangleMeshFloat(trace, linestart, lineend, face->num_collisiontriangles, face->data_collisionelement3i, face->data_collisionvertex3f, face->texture->supercontents, segmentmins, segmentmaxs);
@@ -5233,9 +5306,9 @@ static void Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace_t *trace, q3mnode_t *nod
                for (i = 0;i < leaf->numleaffaces;i++)
                {
                        face = leaf->firstleafface[i];
-                       if (face->collisions && face->markframe != markframe && BoxesOverlap(nodesegmentmins, nodesegmentmaxs, face->mins, face->maxs))
+                       if (face->num_collisiontriangles && face->collisionmarkframe != markframe && BoxesOverlap(nodesegmentmins, nodesegmentmaxs, face->mins, face->maxs))
                        {
-                               face->markframe = markframe;
+                               face->collisionmarkframe = markframe;
                                Collision_TraceBrushTriangleMeshFloat(trace, thisbrush_start, thisbrush_end, face->num_collisiontriangles, face->data_collisionelement3i, face->data_collisionvertex3f, face->texture->supercontents, segmentmins, segmentmaxs);
                        }
                }
@@ -5289,7 +5362,7 @@ static void Mod_Q3BSP_TraceBox(model_t *model, int frame, trace_t *trace, const
                                        for (i = 0;i < model->brushq3.data_thismodel->numfaces;i++)
                                        {
                                                face = model->brushq3.data_thismodel->firstface + i;
-                                               if (face->collisions)
+                                               if (face->num_collisiontriangles)
                                                        Collision_TraceLineTriangleMeshFloat(trace, boxstartmins, boxendmins, face->num_collisiontriangles, face->data_collisionelement3i, face->data_collisionvertex3f, face->texture->supercontents, segmentmins, segmentmaxs);
                                        }
                                }
@@ -5313,7 +5386,7 @@ static void Mod_Q3BSP_TraceBox(model_t *model, int frame, trace_t *trace, const
                                for (i = 0;i < model->brushq3.data_thismodel->numfaces;i++)
                                {
                                        face = model->brushq3.data_thismodel->firstface + i;
-                                       if (face->collisions)
+                                       if (face->num_collisiontriangles)
                                                Collision_TraceBrushTriangleMeshFloat(trace, thisbrush_start, thisbrush_end, face->num_collisiontriangles, face->data_collisionelement3i, face->data_collisionvertex3f, face->texture->supercontents, segmentmins, segmentmaxs);
                                }
                        }
@@ -5527,6 +5600,43 @@ void Mod_Q3BSP_GetVisible(model_t *model, const vec3_t point, const vec3_t mins,
 }
 */
 
+void Mod_Q3BSP_BuildTextureFaceLists(void)
+{
+       int i, j;
+       loadmodel->brushq3.data_texturefaces = Mem_Alloc(loadmodel->mempool, loadmodel->nummodelsurfaces * sizeof(q3msurface_t *));
+       loadmodel->brushq3.data_texturefacenums = Mem_Alloc(loadmodel->mempool, loadmodel->nummodelsurfaces * sizeof(int));
+       for (i = 0;i < loadmodel->brushq3.num_textures;i++)
+               loadmodel->brushq3.data_textures[i].numfaces = 0;
+       for (i = 0;i < loadmodel->nummodelsurfaces;i++)
+               loadmodel->brushq3.data_faces[loadmodel->surfacelist[i]].texture->numfaces++;
+       j = 0;
+       for (i = 0;i < loadmodel->brushq3.num_textures;i++)
+       {
+               loadmodel->brushq3.data_textures[i].facelist = loadmodel->brushq3.data_texturefaces + j;
+               loadmodel->brushq3.data_textures[i].facenumlist = loadmodel->brushq3.data_texturefacenums + j;
+               j += loadmodel->brushq3.data_textures[i].numfaces;
+               loadmodel->brushq3.data_textures[i].numfaces = 0;
+       }
+       for (i = 0;i < loadmodel->nummodelsurfaces;i++)
+       {
+               loadmodel->brushq3.data_faces[loadmodel->surfacelist[i]].texture->facenumlist[loadmodel->brushq3.data_faces[loadmodel->surfacelist[i]].texture->numfaces] = loadmodel->surfacelist[i];
+               loadmodel->brushq3.data_faces[loadmodel->surfacelist[i]].texture->facelist[loadmodel->brushq3.data_faces[loadmodel->surfacelist[i]].texture->numfaces++] = loadmodel->brushq3.data_faces + loadmodel->surfacelist[i];
+       }
+}
+
+void Mod_Q3BSP_RecursiveFindNumLeafs(q3mnode_t *node)
+{
+       int numleafs;
+       while (node->plane)
+       {
+               Mod_Q3BSP_RecursiveFindNumLeafs(node->children[0]);
+               node = node->children[1];
+       }
+       numleafs = ((q3mleaf_t *)node - loadmodel->brushq3.data_leafs) + 1;
+       if (loadmodel->brushq3.num_leafs < numleafs)
+               loadmodel->brushq3.num_leafs = numleafs;
+}
+
 extern void R_Q3BSP_DrawSky(struct entity_render_s *ent);
 extern void R_Q3BSP_Draw(struct entity_render_s *ent);
 extern void R_Q3BSP_GetLightInfo(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, vec3_t outmins, vec3_t outmaxs, int *outclusterlist, qbyte *outclusterpvs, int *outnumclusterspointer, int *outsurfacelist, qbyte *outsurfacepvs, int *outnumsurfacespointer);
@@ -5573,8 +5683,13 @@ void Mod_Q3BSP_Load(model_t *mod, void *buffer)
        mod_base = (qbyte *)header;
 
        // swap all the lumps
-       for (i = 0;i < (int) sizeof(*header) / 4;i++)
-               ((int *)header)[i] = LittleLong(((int *)header)[i]);
+       header->ident = LittleLong(header->ident);
+       header->version = LittleLong(header->version);
+       for (i = 0;i < Q3HEADER_LUMPS;i++)
+       {
+               header->lumps[i].fileofs = LittleLong(header->lumps[i].fileofs);
+               header->lumps[i].filelen = LittleLong(header->lumps[i].filelen);
+       }
 
        Mod_Q3BSP_LoadEntities(&header->lumps[Q3LUMP_ENTITIES]);
        Mod_Q3BSP_LoadTextures(&header->lumps[Q3LUMP_TEXTURES]);
@@ -5607,16 +5722,19 @@ void Mod_Q3BSP_Load(model_t *mod, void *buffer)
                Mod_ShadowMesh_AddMesh(loadmodel->mempool, loadmodel->brush.shadowmesh, NULL, NULL, NULL, face->data_vertex3f, NULL, NULL, NULL, NULL, face->num_triangles, face->data_element3i);
        loadmodel->brush.shadowmesh = Mod_ShadowMesh_Finish(loadmodel->mempool, loadmodel->brush.shadowmesh, false, true);
        Mod_BuildTriangleNeighbors(loadmodel->brush.shadowmesh->neighbor3i, loadmodel->brush.shadowmesh->element3i, loadmodel->brush.shadowmesh->numtriangles);
-       
+
+       loadmodel->brushq3.num_leafs = 0;
+       Mod_Q3BSP_RecursiveFindNumLeafs(loadmodel->brushq3.data_nodes);
+
+       mod = loadmodel;
        for (i = 0;i < loadmodel->brushq3.num_models;i++)
        {
-               if (i == 0)
-                       mod = loadmodel;
-               else
+               if (i > 0)
                {
                        char name[10];
                        // LordHavoc: only register submodels if it is the world
-                       // (prevents bsp models from replacing world submodels)
+                       // (prevents external bsp models from replacing world submodels with
+                       //  their own)
                        if (!loadmodel->isworldmodel)
                                continue;
                        // duplicate the basic information
@@ -5664,6 +5782,8 @@ void Mod_Q3BSP_Load(model_t *mod, void *buffer)
                if (j < mod->brushq3.data_thismodel->numfaces)
                        mod->DrawSky = R_Q3BSP_DrawSky;
        }
+
+       Mod_Q3BSP_BuildTextureFaceLists();
 }
 
 void Mod_IBSP_Load(model_t *mod, void *buffer)