+ if (tx->skin.base == NULL)
+ {
+ // no texture found
+ tx->width = 16;
+ tx->height = 16;
+ tx->skin.base = r_notexture;
+ }
+
+ if (tx->name[0] == '*')
+ {
+ // turb does not block movement
+ tx->flags &= ~SURF_SOLIDCLIP;
+ tx->flags |= SURF_DRAWTURB | SURF_LIGHTBOTHSIDES;
+ // LordHavoc: some turbulent textures should be fullbright and solid
+ if (!strncmp(tx->name,"*lava",5)
+ || !strncmp(tx->name,"*teleport",9)
+ || !strncmp(tx->name,"*rift",5)) // Scourge of Armagon texture
+ tx->flags |= SURF_DRAWFULLBRIGHT | SURF_DRAWNOALPHA;
+ else
+ tx->flags |= SURF_WATERALPHA;
+ tx->shader = &Cshader_water;
+ }
+ else if (tx->name[0] == 's' && tx->name[1] == 'k' && tx->name[2] == 'y')
+ {
+ tx->flags |= SURF_DRAWSKY;
+ tx->shader = &Cshader_sky;
+ }
+ else
+ {
+ tx->flags |= SURF_LIGHTMAP;
+ if (!tx->skin.fog)
+ tx->flags |= SURF_SHADOWCAST | SURF_SHADOWLIGHT;
+ tx->shader = &Cshader_wall_lightmap;
+ }
+
+ // start out with no animation
+ tx->currentframe = tx;
+ }
+
+ // sequence the animations
+ for (i = 0;i < m->nummiptex;i++)
+ {
+ tx = loadmodel->brushq1.textures + i;
+ if (!tx || tx->name[0] != '+' || tx->name[1] == 0 || tx->name[2] == 0)
+ continue;
+ if (tx->anim_total[0] || tx->anim_total[1])
+ continue; // already sequenced
+
+ // find the number of frames in the animation
+ memset(anims, 0, sizeof(anims));
+ memset(altanims, 0, sizeof(altanims));
+
+ for (j = i;j < m->nummiptex;j++)
+ {
+ tx2 = loadmodel->brushq1.textures + j;
+ if (!tx2 || tx2->name[0] != '+' || strcmp(tx2->name+2, tx->name+2))
+ continue;
+
+ num = tx2->name[1];
+ if (num >= '0' && num <= '9')
+ anims[num - '0'] = tx2;
+ else if (num >= 'a' && num <= 'j')
+ altanims[num - 'a'] = tx2;
+ else
+ Con_Printf("Bad animating texture %s\n", tx->name);
+ }
+
+ max = altmax = 0;
+ for (j = 0;j < 10;j++)
+ {
+ if (anims[j])
+ max = j + 1;
+ if (altanims[j])
+ altmax = j + 1;
+ }
+ //Con_Printf("linking animation %s (%i:%i frames)\n\n", tx->name, max, altmax);
+
+ incomplete = false;
+ for (j = 0;j < max;j++)
+ {
+ if (!anims[j])
+ {
+ Con_Printf("Missing frame %i of %s\n", j, tx->name);
+ incomplete = true;
+ }
+ }
+ for (j = 0;j < altmax;j++)
+ {
+ if (!altanims[j])
+ {
+ Con_Printf("Missing altframe %i of %s\n", j, tx->name);
+ incomplete = true;
+ }
+ }
+ if (incomplete)
+ continue;
+
+ if (altmax < 1)
+ {
+ // if there is no alternate animation, duplicate the primary
+ // animation into the alternate
+ altmax = max;
+ for (k = 0;k < 10;k++)
+ altanims[k] = anims[k];
+ }
+
+ // link together the primary animation
+ for (j = 0;j < max;j++)
+ {
+ tx2 = anims[j];
+ tx2->animated = true;
+ tx2->anim_total[0] = max;
+ tx2->anim_total[1] = altmax;
+ for (k = 0;k < 10;k++)
+ {
+ tx2->anim_frames[0][k] = anims[k];
+ tx2->anim_frames[1][k] = altanims[k];
+ }
+ }
+
+ // if there really is an alternate anim...
+ if (anims[0] != altanims[0])
+ {
+ // link together the alternate animation
+ for (j = 0;j < altmax;j++)
+ {
+ tx2 = altanims[j];
+ tx2->animated = true;
+ // the primary/alternate are reversed here
+ tx2->anim_total[0] = altmax;
+ tx2->anim_total[1] = max;
+ for (k = 0;k < 10;k++)
+ {
+ tx2->anim_frames[0][k] = altanims[k];
+ tx2->anim_frames[1][k] = anims[k];
+ }
+ }
+ }
+ }
+}
+
+static void Mod_Q1BSP_LoadLighting(lump_t *l)
+{
+ int i;
+ qbyte *in, *out, *data, d;
+ char litfilename[1024];
+ loadmodel->brushq1.lightdata = NULL;
+ 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);
+ }
+ else // LordHavoc: bsp version 29 (normal white lighting)
+ {
+ // LordHavoc: hope is not lost yet, check for a .lit file to load
+ strlcpy (litfilename, loadmodel->name, sizeof (litfilename));
+ FS_StripExtension (litfilename, litfilename, sizeof (litfilename));
+ strlcat (litfilename, ".lit", sizeof (litfilename));
+ data = (qbyte*) FS_LoadFile(litfilename, false);
+ if (data)
+ {
+ if (fs_filesize > 8 && data[0] == 'Q' && data[1] == 'L' && data[2] == 'I' && data[3] == 'T')
+ {
+ i = LittleLong(((int *)data)[1]);
+ if (i == 1)
+ {
+ Con_DPrintf("loaded %s\n", litfilename);
+ loadmodel->brushq1.lightdata = Mem_Alloc(loadmodel->mempool, fs_filesize - 8);
+ memcpy(loadmodel->brushq1.lightdata, data + 8, fs_filesize - 8);
+ Mem_Free(data);
+ return;
+ }
+ else
+ {
+ Con_Printf("Unknown .lit file version (%d)\n", i);
+ Mem_Free(data);
+ }
+ }
+ else
+ {
+ if (fs_filesize == 8)
+ Con_Printf("Empty .lit file, ignoring\n");
+ else
+ Con_Printf("Corrupt .lit file (old version?), ignoring\n");
+ Mem_Free(data);
+ }
+ }
+ // LordHavoc: oh well, expand the white lighting data
+ if (!l->filelen)
+ return;
+ loadmodel->brushq1.lightdata = Mem_Alloc(loadmodel->mempool, l->filelen*3);
+ in = loadmodel->brushq1.lightdata + l->filelen*2; // place the file at the end, so it will not be overwritten until the very last write
+ out = loadmodel->brushq1.lightdata;
+ memcpy(in, mod_base + l->fileofs, l->filelen);
+ for (i = 0;i < l->filelen;i++)
+ {
+ d = *in++;
+ *out++ = d;
+ *out++ = d;
+ *out++ = d;
+ }
+ }
+}
+
+static void Mod_Q1BSP_LoadLightList(void)
+{
+ int a, n, numlights;
+ char lightsfilename[1024], *s, *t, *lightsstring;
+ mlight_t *e;
+
+ strlcpy (lightsfilename, loadmodel->name, sizeof (lightsfilename));
+ FS_StripExtension (lightsfilename, lightsfilename, sizeof(lightsfilename));
+ strlcat (lightsfilename, ".lights", sizeof (lightsfilename));
+ s = lightsstring = (char *) FS_LoadFile(lightsfilename, false);
+ if (s)
+ {
+ numlights = 0;
+ while (*s)
+ {
+ while (*s && *s != '\n')
+ s++;
+ if (!*s)
+ {
+ Mem_Free(lightsstring);
+ Host_Error("lights file must end with a newline\n");
+ }
+ s++;
+ numlights++;
+ }
+ loadmodel->brushq1.lights = Mem_Alloc(loadmodel->mempool, numlights * sizeof(mlight_t));
+ s = lightsstring;
+ n = 0;
+ while (*s && n < numlights)
+ {
+ t = s;
+ while (*s && *s != '\n')
+ s++;
+ if (!*s)
+ {
+ Mem_Free(lightsstring);
+ Host_Error("misparsed lights file!\n");
+ }
+ e = loadmodel->brushq1.lights + n;
+ *s = 0;
+ a = sscanf(t, "%f %f %f %f %f %f %f %f %f %f %f %f %f %d", &e->origin[0], &e->origin[1], &e->origin[2], &e->falloff, &e->light[0], &e->light[1], &e->light[2], &e->subtract, &e->spotdir[0], &e->spotdir[1], &e->spotdir[2], &e->spotcone, &e->distbias, &e->style);
+ *s = '\n';
+ if (a != 14)
+ {
+ Mem_Free(lightsstring);
+ Host_Error("invalid lights file, found %d parameters on line %i, should be 14 parameters (origin[0] origin[1] origin[2] falloff light[0] light[1] light[2] subtract spotdir[0] spotdir[1] spotdir[2] spotcone distancebias style)\n", a, n + 1);
+ }
+ s++;
+ n++;
+ }
+ if (*s)
+ {
+ Mem_Free(lightsstring);
+ Host_Error("misparsed lights file!\n");
+ }
+ loadmodel->brushq1.numlights = numlights;
+ Mem_Free(lightsstring);
+ }
+}
+
+static void Mod_Q1BSP_LoadVisibility(lump_t *l)
+{
+ loadmodel->brushq1.num_compressedpvs = 0;
+ loadmodel->brushq1.data_compressedpvs = NULL;
+ if (!l->filelen)
+ return;
+ loadmodel->brushq1.num_compressedpvs = l->filelen;
+ loadmodel->brushq1.data_compressedpvs = Mem_Alloc(loadmodel->mempool, l->filelen);
+ memcpy(loadmodel->brushq1.data_compressedpvs, mod_base + l->fileofs, l->filelen);
+}
+
+// used only for HalfLife maps
+static void Mod_Q1BSP_ParseWadsFromEntityLump(const char *data)
+{
+ char key[128], value[4096];
+ char wadname[128];
+ int i, j, k;
+ if (!data)
+ return;
+ if (!COM_ParseToken(&data, false))
+ return; // error
+ if (com_token[0] != '{')
+ return; // error
+ while (1)
+ {
+ if (!COM_ParseToken(&data, false))
+ return; // error
+ if (com_token[0] == '}')
+ break; // end of worldspawn
+ if (com_token[0] == '_')
+ strcpy(key, com_token + 1);
+ else
+ strcpy(key, com_token);
+ while (key[strlen(key)-1] == ' ') // remove trailing spaces
+ key[strlen(key)-1] = 0;
+ if (!COM_ParseToken(&data, false))
+ return; // error
+ strcpy(value, com_token);
+ if (!strcmp("wad", key)) // for HalfLife maps
+ {
+ if (loadmodel->brush.ishlbsp)
+ {
+ j = 0;
+ for (i = 0;i < 4096;i++)
+ if (value[i] != ';' && value[i] != '\\' && value[i] != '/' && value[i] != ':')
+ break;
+ if (value[i])
+ {
+ for (;i < 4096;i++)
+ {
+ // ignore path - the \\ check is for HalfLife... stupid windoze 'programmers'...
+ if (value[i] == '\\' || value[i] == '/' || value[i] == ':')
+ j = i+1;
+ else if (value[i] == ';' || value[i] == 0)
+ {
+ k = value[i];
+ value[i] = 0;
+ strcpy(wadname, "textures/");
+ strcat(wadname, &value[j]);
+ W_LoadTextureWadFile(wadname, false);
+ j = i+1;
+ if (!k)
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+}
+
+static void Mod_Q1BSP_LoadEntities(lump_t *l)
+{
+ loadmodel->brush.entities = NULL;
+ if (!l->filelen)
+ return;
+ loadmodel->brush.entities = Mem_Alloc(loadmodel->mempool, l->filelen);
+ memcpy(loadmodel->brush.entities, mod_base + l->fileofs, l->filelen);
+ if (loadmodel->brush.ishlbsp)
+ Mod_Q1BSP_ParseWadsFromEntityLump(loadmodel->brush.entities);
+}
+
+
+static void Mod_Q1BSP_LoadVertexes(lump_t *l)
+{
+ dvertex_t *in;
+ mvertex_t *out;
+ int i, count;
+
+ in = (void *)(mod_base + l->fileofs);
+ if (l->filelen % sizeof(*in))
+ Host_Error("Mod_Q1BSP_LoadVertexes: funny lump size in %s",loadmodel->name);
+ count = l->filelen / sizeof(*in);
+ out = Mem_Alloc(loadmodel->mempool, count*sizeof(*out));
+
+ loadmodel->brushq1.vertexes = out;
+ loadmodel->brushq1.numvertexes = count;
+
+ for ( i=0 ; i<count ; i++, in++, out++)
+ {
+ out->position[0] = LittleFloat(in->point[0]);
+ out->position[1] = LittleFloat(in->point[1]);
+ out->position[2] = LittleFloat(in->point[2]);
+ }
+}
+
+static void Mod_Q1BSP_LoadSubmodels(lump_t *l)
+{
+ dmodel_t *in;
+ dmodel_t *out;
+ int i, j, count;
+
+ in = (void *)(mod_base + l->fileofs);
+ if (l->filelen % sizeof(*in))
+ Host_Error("Mod_Q1BSP_LoadSubmodels: funny lump size in %s",loadmodel->name);
+ count = l->filelen / sizeof(*in);
+ out = Mem_Alloc(loadmodel->mempool, count*sizeof(*out));
+
+ loadmodel->brushq1.submodels = out;
+ loadmodel->brush.numsubmodels = count;
+
+ for ( i=0 ; i<count ; i++, in++, out++)
+ {
+ for (j=0 ; j<3 ; j++)
+ {
+ // spread the mins / maxs by a pixel
+ out->mins[j] = LittleFloat(in->mins[j]) - 1;
+ out->maxs[j] = LittleFloat(in->maxs[j]) + 1;
+ out->origin[j] = LittleFloat(in->origin[j]);
+ }
+ for (j=0 ; j<MAX_MAP_HULLS ; j++)
+ out->headnode[j] = LittleLong(in->headnode[j]);
+ out->visleafs = LittleLong(in->visleafs);
+ out->firstface = LittleLong(in->firstface);
+ out->numfaces = LittleLong(in->numfaces);
+ }
+}
+
+static void Mod_Q1BSP_LoadEdges(lump_t *l)
+{
+ dedge_t *in;
+ medge_t *out;
+ int i, count;
+
+ in = (void *)(mod_base + l->fileofs);
+ if (l->filelen % sizeof(*in))
+ Host_Error("Mod_Q1BSP_LoadEdges: funny lump size in %s",loadmodel->name);
+ count = l->filelen / sizeof(*in);
+ out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
+
+ loadmodel->brushq1.edges = out;
+ loadmodel->brushq1.numedges = count;
+
+ for ( i=0 ; i<count ; i++, in++, out++)
+ {
+ out->v[0] = (unsigned short)LittleShort(in->v[0]);
+ out->v[1] = (unsigned short)LittleShort(in->v[1]);
+ }
+}
+
+static void Mod_Q1BSP_LoadTexinfo(lump_t *l)
+{
+ texinfo_t *in;
+ mtexinfo_t *out;
+ int i, j, k, count, miptex;
+
+ in = (void *)(mod_base + l->fileofs);
+ if (l->filelen % sizeof(*in))
+ Host_Error("Mod_Q1BSP_LoadTexinfo: funny lump size in %s",loadmodel->name);
+ count = l->filelen / sizeof(*in);
+ out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
+
+ loadmodel->brushq1.texinfo = out;
+ loadmodel->brushq1.numtexinfo = count;
+
+ for (i = 0;i < count;i++, in++, out++)
+ {
+ for (k = 0;k < 2;k++)
+ for (j = 0;j < 4;j++)
+ out->vecs[k][j] = LittleFloat(in->vecs[k][j]);
+
+ miptex = LittleLong(in->miptex);
+ out->flags = LittleLong(in->flags);
+
+ out->texture = NULL;
+ if (loadmodel->brushq1.textures)
+ {
+ if ((unsigned int) miptex >= (unsigned int) loadmodel->brushq1.numtextures)
+ Con_Printf("error in model \"%s\": invalid miptex index %i(of %i)\n", loadmodel->name, miptex, loadmodel->brushq1.numtextures);
+ else
+ out->texture = loadmodel->brushq1.textures + miptex;
+ }
+ if (out->flags & TEX_SPECIAL)
+ {
+ // if texture chosen is NULL or the shader needs a lightmap,
+ // force to notexture water shader
+ if (out->texture == NULL || out->texture->shader->flags & SHADERFLAGS_NEEDLIGHTMAP)
+ out->texture = loadmodel->brushq1.textures + (loadmodel->brushq1.numtextures - 1);
+ }
+ else
+ {
+ // if texture chosen is NULL, force to notexture
+ if (out->texture == NULL)
+ out->texture = loadmodel->brushq1.textures + (loadmodel->brushq1.numtextures - 2);
+ }
+ }
+}
+
+#if 0
+void BoundPoly(int numverts, float *verts, vec3_t mins, vec3_t maxs)
+{
+ int i, j;
+ float *v;
+
+ mins[0] = mins[1] = mins[2] = 9999;
+ maxs[0] = maxs[1] = maxs[2] = -9999;
+ v = verts;
+ for (i = 0;i < numverts;i++)
+ {
+ for (j = 0;j < 3;j++, v++)
+ {
+ if (*v < mins[j])
+ mins[j] = *v;
+ if (*v > maxs[j])
+ maxs[j] = *v;
+ }
+ }
+}
+
+#define MAX_SUBDIVPOLYTRIANGLES 4096
+#define MAX_SUBDIVPOLYVERTS(MAX_SUBDIVPOLYTRIANGLES * 3)
+
+static int subdivpolyverts, subdivpolytriangles;
+static int subdivpolyindex[MAX_SUBDIVPOLYTRIANGLES][3];
+static float subdivpolyvert[MAX_SUBDIVPOLYVERTS][3];
+
+static int subdivpolylookupvert(vec3_t v)
+{
+ int i;
+ for (i = 0;i < subdivpolyverts;i++)
+ if (subdivpolyvert[i][0] == v[0]
+ && subdivpolyvert[i][1] == v[1]
+ && subdivpolyvert[i][2] == v[2])
+ return i;
+ if (subdivpolyverts >= MAX_SUBDIVPOLYVERTS)
+ Host_Error("SubDividePolygon: ran out of vertices in buffer, please increase your r_subdivide_size");
+ VectorCopy(v, subdivpolyvert[subdivpolyverts]);
+ return subdivpolyverts++;
+}
+
+static void SubdividePolygon(int numverts, float *verts)
+{
+ int i, i1, i2, i3, f, b, c, p;
+ vec3_t mins, maxs, front[256], back[256];
+ float m, *pv, *cv, dist[256], frac;
+
+ if (numverts > 250)
+ Host_Error("SubdividePolygon: ran out of verts in buffer");
+
+ BoundPoly(numverts, verts, mins, maxs);
+
+ for (i = 0;i < 3;i++)
+ {
+ m = (mins[i] + maxs[i]) * 0.5;
+ m = r_subdivide_size.value * floor(m/r_subdivide_size.value + 0.5);
+ if (maxs[i] - m < 8)
+ continue;
+ if (m - mins[i] < 8)
+ continue;
+
+ // cut it
+ for (cv = verts, c = 0;c < numverts;c++, cv += 3)
+ dist[c] = cv[i] - m;
+
+ f = b = 0;
+ for (p = numverts - 1, c = 0, pv = verts + p * 3, cv = verts;c < numverts;p = c, c++, pv = cv, cv += 3)
+ {
+ if (dist[p] >= 0)
+ {
+ VectorCopy(pv, front[f]);
+ f++;
+ }
+ if (dist[p] <= 0)
+ {
+ VectorCopy(pv, back[b]);
+ b++;
+ }
+ if (dist[p] == 0 || dist[c] == 0)
+ continue;
+ if ((dist[p] > 0) != (dist[c] > 0) )
+ {
+ // clip point
+ frac = dist[p] / (dist[p] - dist[c]);
+ front[f][0] = back[b][0] = pv[0] + frac * (cv[0] - pv[0]);
+ front[f][1] = back[b][1] = pv[1] + frac * (cv[1] - pv[1]);
+ front[f][2] = back[b][2] = pv[2] + frac * (cv[2] - pv[2]);
+ f++;
+ b++;
+ }
+ }
+
+ SubdividePolygon(f, front[0]);
+ SubdividePolygon(b, back[0]);
+ return;
+ }
+
+ i1 = subdivpolylookupvert(verts);
+ i2 = subdivpolylookupvert(verts + 3);
+ for (i = 2;i < numverts;i++)
+ {
+ if (subdivpolytriangles >= MAX_SUBDIVPOLYTRIANGLES)
+ {
+ Con_Printf("SubdividePolygon: ran out of triangles in buffer, please increase your r_subdivide_size\n");
+ return;
+ }
+
+ i3 = subdivpolylookupvert(verts + i * 3);
+ subdivpolyindex[subdivpolytriangles][0] = i1;
+ subdivpolyindex[subdivpolytriangles][1] = i2;
+ subdivpolyindex[subdivpolytriangles][2] = i3;
+ i2 = i3;
+ subdivpolytriangles++;
+ }
+}
+
+//Breaks a polygon up along axial 64 unit
+//boundaries so that turbulent and sky warps
+//can be done reasonably.
+static void Mod_Q1BSP_GenerateWarpMesh(msurface_t *surf)
+{
+ int i, j;
+ surfvertex_t *v;
+ surfmesh_t *mesh;
+
+ subdivpolytriangles = 0;
+ subdivpolyverts = 0;
+ SubdividePolygon(surf->poly_numverts, surf->poly_verts);
+ if (subdivpolytriangles < 1)
+ Host_Error("Mod_Q1BSP_GenerateWarpMesh: no triangles?\n");
+
+ surf->mesh = mesh = Mem_Alloc(loadmodel->mempool, sizeof(surfmesh_t) + subdivpolytriangles * sizeof(int[3]) + subdivpolyverts * sizeof(surfvertex_t));
+ mesh->num_vertices = subdivpolyverts;
+ mesh->num_triangles = subdivpolytriangles;
+ mesh->vertex = (surfvertex_t *)(mesh + 1);
+ mesh->index = (int *)(mesh->vertex + mesh->num_vertices);
+ memset(mesh->vertex, 0, mesh->num_vertices * sizeof(surfvertex_t));
+
+ for (i = 0;i < mesh->num_triangles;i++)
+ for (j = 0;j < 3;j++)
+ mesh->index[i*3+j] = subdivpolyindex[i][j];
+
+ for (i = 0, v = mesh->vertex;i < subdivpolyverts;i++, v++)
+ {
+ VectorCopy(subdivpolyvert[i], v->v);
+ v->st[0] = DotProduct(v->v, surf->texinfo->vecs[0]);
+ v->st[1] = DotProduct(v->v, surf->texinfo->vecs[1]);
+ }
+}
+#endif
+
+static surfmesh_t *Mod_Q1BSP_AllocSurfMesh(int numverts, int numtriangles)
+{
+ surfmesh_t *mesh;
+ mesh = Mem_Alloc(loadmodel->mempool, sizeof(surfmesh_t) + numtriangles * sizeof(int[6]) + numverts * (3 + 2 + 2 + 2 + 3 + 3 + 3 + 1) * sizeof(float));
+ mesh->num_vertices = numverts;
+ mesh->num_triangles = numtriangles;
+ mesh->data_vertex3f = (float *)(mesh + 1);
+ mesh->data_texcoordtexture2f = mesh->data_vertex3f + mesh->num_vertices * 3;
+ mesh->data_texcoordlightmap2f = mesh->data_texcoordtexture2f + mesh->num_vertices * 2;
+ mesh->data_texcoorddetail2f = mesh->data_texcoordlightmap2f + mesh->num_vertices * 2;
+ mesh->data_svector3f = (float *)(mesh->data_texcoorddetail2f + mesh->num_vertices * 2);
+ mesh->data_tvector3f = mesh->data_svector3f + mesh->num_vertices * 3;
+ mesh->data_normal3f = mesh->data_tvector3f + mesh->num_vertices * 3;
+ mesh->data_lightmapoffsets = (int *)(mesh->data_normal3f + mesh->num_vertices * 3);
+ mesh->data_element3i = mesh->data_lightmapoffsets + mesh->num_vertices;
+ mesh->data_neighbor3i = mesh->data_element3i + mesh->num_triangles * 3;
+ return mesh;
+}
+
+static void Mod_Q1BSP_GenerateSurfacePolygon(msurface_t *surf, int firstedge, int numedges)
+{
+ int i, lindex, j;
+ float *vec, *vert, mins[3], maxs[3], val, *v;
+ mtexinfo_t *tex;
+
+ // convert edges back to a normal polygon
+ surf->poly_numverts = numedges;
+ vert = surf->poly_verts = Mem_Alloc(loadmodel->mempool, sizeof(float[3]) * numedges);
+ for (i = 0;i < numedges;i++)
+ {
+ lindex = loadmodel->brushq1.surfedges[firstedge + i];
+ if (lindex > 0)
+ vec = loadmodel->brushq1.vertexes[loadmodel->brushq1.edges[lindex].v[0]].position;
+ else
+ vec = loadmodel->brushq1.vertexes[loadmodel->brushq1.edges[-lindex].v[1]].position;
+ VectorCopy(vec, vert);
+ vert += 3;
+ }
+
+ // calculate polygon bounding box and center
+ vert = surf->poly_verts;
+ VectorCopy(vert, mins);
+ VectorCopy(vert, maxs);
+ vert += 3;
+ for (i = 1;i < surf->poly_numverts;i++, vert += 3)
+ {
+ if (mins[0] > vert[0]) mins[0] = vert[0];if (maxs[0] < vert[0]) maxs[0] = vert[0];
+ if (mins[1] > vert[1]) mins[1] = vert[1];if (maxs[1] < vert[1]) maxs[1] = vert[1];
+ if (mins[2] > vert[2]) mins[2] = vert[2];if (maxs[2] < vert[2]) maxs[2] = vert[2];
+ }
+ VectorCopy(mins, surf->poly_mins);
+ VectorCopy(maxs, surf->poly_maxs);
+ surf->poly_center[0] = (mins[0] + maxs[0]) * 0.5f;
+ surf->poly_center[1] = (mins[1] + maxs[1]) * 0.5f;
+ surf->poly_center[2] = (mins[2] + maxs[2]) * 0.5f;
+
+ // generate surface extents information
+ tex = surf->texinfo;
+ mins[0] = maxs[0] = DotProduct(surf->poly_verts, tex->vecs[0]) + tex->vecs[0][3];
+ mins[1] = maxs[1] = DotProduct(surf->poly_verts, tex->vecs[1]) + tex->vecs[1][3];
+ for (i = 1, v = surf->poly_verts + 3;i < surf->poly_numverts;i++, v += 3)
+ {
+ for (j = 0;j < 2;j++)
+ {
+ val = DotProduct(v, tex->vecs[j]) + tex->vecs[j][3];
+ if (mins[j] > val)
+ mins[j] = val;
+ if (maxs[j] < val)
+ maxs[j] = val;
+ }
+ }
+ for (i = 0;i < 2;i++)
+ {
+ surf->texturemins[i] = (int) floor(mins[i] / 16) * 16;
+ surf->extents[i] = (int) ceil(maxs[i] / 16) * 16 - surf->texturemins[i];
+ }
+}
+
+static void Mod_Q1BSP_LoadFaces(lump_t *l)
+{
+ dface_t *in;
+ msurface_t *surf;
+ int i, count, surfnum, planenum, ssize, tsize, firstedge, numedges, totalverts, totaltris, totalmeshes;
+ surfmesh_t *mesh;
+ float s, t;
+
+ in = (void *)(mod_base + l->fileofs);
+ if (l->filelen % sizeof(*in))
+ Host_Error("Mod_Q1BSP_LoadFaces: funny lump size in %s",loadmodel->name);
+ count = l->filelen / sizeof(*in);
+ loadmodel->brushq1.surfaces = Mem_Alloc(loadmodel->mempool, count*sizeof(msurface_t));
+
+ loadmodel->brushq1.numsurfaces = count;
+ loadmodel->brushq1.surfacevisframes = Mem_Alloc(loadmodel->mempool, count * sizeof(int));
+ loadmodel->brushq1.surfacepvsframes = Mem_Alloc(loadmodel->mempool, count * sizeof(int));
+ loadmodel->brushq1.pvssurflist = Mem_Alloc(loadmodel->mempool, count * sizeof(int));
+
+ for (surfnum = 0, surf = loadmodel->brushq1.surfaces, totalverts = 0, totaltris = 0, totalmeshes = 0;surfnum < count;surfnum++, totalverts += surf->poly_numverts, totaltris += surf->poly_numverts - 2, totalmeshes++, in++, surf++)
+ {
+ surf->number = surfnum;
+ // FIXME: validate edges, texinfo, etc?
+ firstedge = LittleLong(in->firstedge);
+ numedges = LittleShort(in->numedges);
+ if ((unsigned int) firstedge > (unsigned int) loadmodel->brushq1.numsurfedges || (unsigned int) numedges > (unsigned int) loadmodel->brushq1.numsurfedges || (unsigned int) firstedge + (unsigned int) numedges > (unsigned int) loadmodel->brushq1.numsurfedges)
+ Host_Error("Mod_Q1BSP_LoadFaces: invalid edge range (firstedge %i, numedges %i, model edges %i)\n", firstedge, numedges, loadmodel->brushq1.numsurfedges);
+ i = LittleShort(in->texinfo);
+ if ((unsigned int) i >= (unsigned int) loadmodel->brushq1.numtexinfo)
+ Host_Error("Mod_Q1BSP_LoadFaces: invalid texinfo index %i(model has %i texinfos)\n", i, loadmodel->brushq1.numtexinfo);
+ surf->texinfo = loadmodel->brushq1.texinfo + i;
+ surf->flags = surf->texinfo->texture->flags;
+
+ planenum = LittleShort(in->planenum);
+ if ((unsigned int) planenum >= (unsigned int) loadmodel->brushq1.numplanes)
+ Host_Error("Mod_Q1BSP_LoadFaces: invalid plane index %i (model has %i planes)\n", planenum, loadmodel->brushq1.numplanes);
+
+ if (LittleShort(in->side))
+ surf->flags |= SURF_PLANEBACK;
+
+ surf->plane = loadmodel->brushq1.planes + planenum;
+
+ // clear lightmap (filled in later)
+ surf->lightmaptexture = NULL;
+
+ // force lightmap upload on first time seeing the surface
+ surf->cached_dlight = true;
+
+ Mod_Q1BSP_GenerateSurfacePolygon(surf, firstedge, numedges);
+
+ ssize = (surf->extents[0] >> 4) + 1;
+ tsize = (surf->extents[1] >> 4) + 1;
+
+ // lighting info
+ for (i = 0;i < MAXLIGHTMAPS;i++)
+ surf->styles[i] = in->styles[i];
+ i = LittleLong(in->lightofs);
+ if (i == -1)
+ surf->samples = NULL;
+ else if (loadmodel->brush.ishlbsp) // LordHavoc: HalfLife map (bsp version 30)
+ surf->samples = loadmodel->brushq1.lightdata + i;
+ else // LordHavoc: white lighting (bsp version 29)
+ surf->samples = loadmodel->brushq1.lightdata + (i * 3);
+
+ if (surf->texinfo->texture->shader == &Cshader_wall_lightmap)
+ {
+ if ((surf->extents[0] >> 4) + 1 > (256) || (surf->extents[1] >> 4) + 1 > (256))
+ Host_Error("Bad surface extents");
+ // stainmap for permanent marks on walls
+ surf->stainsamples = Mem_Alloc(loadmodel->mempool, ssize * tsize * 3);
+ // clear to white
+ memset(surf->stainsamples, 255, ssize * tsize * 3);
+ }
+ }
+
+ loadmodel->brushq1.entiremesh = Mod_Q1BSP_AllocSurfMesh(totalverts, totaltris);
+
+ for (surfnum = 0, surf = loadmodel->brushq1.surfaces, totalverts = 0, totaltris = 0, totalmeshes = 0;surfnum < count;surfnum++, totalverts += surf->poly_numverts, totaltris += surf->poly_numverts - 2, totalmeshes++, surf++)
+ {
+ mesh = &surf->mesh;
+ mesh->num_vertices = surf->poly_numverts;
+ mesh->num_triangles = surf->poly_numverts - 2;
+ mesh->data_vertex3f = loadmodel->brushq1.entiremesh->data_vertex3f + totalverts * 3;
+ mesh->data_texcoordtexture2f = loadmodel->brushq1.entiremesh->data_texcoordtexture2f + totalverts * 2;
+ mesh->data_texcoordlightmap2f = loadmodel->brushq1.entiremesh->data_texcoordlightmap2f + totalverts * 2;
+ mesh->data_texcoorddetail2f = loadmodel->brushq1.entiremesh->data_texcoorddetail2f + totalverts * 2;
+ mesh->data_svector3f = loadmodel->brushq1.entiremesh->data_svector3f + totalverts * 3;
+ mesh->data_tvector3f = loadmodel->brushq1.entiremesh->data_tvector3f + totalverts * 3;
+ mesh->data_normal3f = loadmodel->brushq1.entiremesh->data_normal3f + totalverts * 3;
+ mesh->data_lightmapoffsets = loadmodel->brushq1.entiremesh->data_lightmapoffsets + totalverts;
+ mesh->data_element3i = loadmodel->brushq1.entiremesh->data_element3i + totaltris * 3;
+ mesh->data_neighbor3i = loadmodel->brushq1.entiremesh->data_neighbor3i + totaltris * 3;
+
+ surf->lightmaptexturestride = 0;
+ surf->lightmaptexture = NULL;
+
+ for (i = 0;i < mesh->num_vertices;i++)
+ {
+ mesh->data_vertex3f[i * 3 + 0] = surf->poly_verts[i * 3 + 0];
+ mesh->data_vertex3f[i * 3 + 1] = surf->poly_verts[i * 3 + 1];
+ mesh->data_vertex3f[i * 3 + 2] = surf->poly_verts[i * 3 + 2];
+ s = DotProduct((mesh->data_vertex3f + i * 3), surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3];
+ t = DotProduct((mesh->data_vertex3f + i * 3), surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3];
+ mesh->data_texcoordtexture2f[i * 2 + 0] = s / surf->texinfo->texture->width;
+ mesh->data_texcoordtexture2f[i * 2 + 1] = t / surf->texinfo->texture->height;
+ mesh->data_texcoorddetail2f[i * 2 + 0] = s * (1.0f / 16.0f);
+ mesh->data_texcoorddetail2f[i * 2 + 1] = t * (1.0f / 16.0f);
+ mesh->data_texcoordlightmap2f[i * 2 + 0] = 0;
+ mesh->data_texcoordlightmap2f[i * 2 + 1] = 0;
+ mesh->data_lightmapoffsets[i] = 0;
+ }
+
+ for (i = 0;i < mesh->num_triangles;i++)
+ {
+ mesh->data_element3i[i * 3 + 0] = 0;
+ mesh->data_element3i[i * 3 + 1] = i + 1;
+ mesh->data_element3i[i * 3 + 2] = i + 2;
+ }
+
+ Mod_BuildTriangleNeighbors(mesh->data_neighbor3i, mesh->data_element3i, mesh->num_triangles);
+ Mod_BuildTextureVectorsAndNormals(mesh->num_vertices, mesh->num_triangles, mesh->data_vertex3f, mesh->data_texcoordtexture2f, mesh->data_element3i, mesh->data_svector3f, mesh->data_tvector3f, mesh->data_normal3f);
+
+ if (surf->texinfo->texture->shader == &Cshader_wall_lightmap)
+ {
+ int i, iu, iv, smax, tmax;
+ float u, v, ubase, vbase, uscale, vscale;
+
+ smax = surf->extents[0] >> 4;
+ tmax = surf->extents[1] >> 4;
+
+ surf->flags |= SURF_LIGHTMAP;
+ if (r_miplightmaps.integer)
+ {
+ surf->lightmaptexturestride = smax+1;
+ surf->lightmaptexture = R_LoadTexture2D(loadmodel->texturepool, NULL, surf->lightmaptexturestride, (surf->extents[1]>>4)+1, NULL, loadmodel->brushq1.lightmaprgba ? TEXTYPE_RGBA : TEXTYPE_RGB, TEXF_MIPMAP | TEXF_FORCELINEAR | TEXF_PRECACHE, NULL);
+ }
+ else
+ {
+ surf->lightmaptexturestride = R_CompatibleFragmentWidth(smax+1, loadmodel->brushq1.lightmaprgba ? TEXTYPE_RGBA : TEXTYPE_RGB, 0);
+ surf->lightmaptexture = R_LoadTexture2D(loadmodel->texturepool, NULL, surf->lightmaptexturestride, (surf->extents[1]>>4)+1, NULL, loadmodel->brushq1.lightmaprgba ? TEXTYPE_RGBA : TEXTYPE_RGB, TEXF_FRAGMENT | TEXF_FORCELINEAR | TEXF_PRECACHE, NULL);
+ }
+ R_FragmentLocation(surf->lightmaptexture, NULL, NULL, &ubase, &vbase, &uscale, &vscale);
+ uscale = (uscale - ubase) / (smax + 1);
+ vscale = (vscale - vbase) / (tmax + 1);
+
+ for (i = 0;i < mesh->num_vertices;i++)
+ {
+ u = ((DotProduct((mesh->data_vertex3f + i * 3), surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3]) + 8 - surf->texturemins[0]) * (1.0 / 16.0);
+ v = ((DotProduct((mesh->data_vertex3f + i * 3), surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3]) + 8 - surf->texturemins[1]) * (1.0 / 16.0);
+ mesh->data_texcoordlightmap2f[i * 2 + 0] = u * uscale + ubase;
+ mesh->data_texcoordlightmap2f[i * 2 + 1] = v * vscale + vbase;
+ // LordHavoc: calc lightmap data offset for vertex lighting to use
+ iu = (int) u;
+ iv = (int) v;
+ mesh->data_lightmapoffsets[i] = (bound(0, iv, tmax) * (smax+1) + bound(0, iu, smax)) * 3;
+ }
+ }
+ }
+}
+
+static void Mod_Q1BSP_SetParent(mnode_t *node, mnode_t *parent)
+{
+ node->parent = parent;
+ if (node->contents < 0)
+ return;
+ Mod_Q1BSP_SetParent(node->children[0], node);
+ Mod_Q1BSP_SetParent(node->children[1], node);
+}
+
+static void Mod_Q1BSP_LoadNodes(lump_t *l)
+{
+ int i, j, count, p;
+ dnode_t *in;
+ mnode_t *out;
+
+ in = (void *)(mod_base + l->fileofs);
+ if (l->filelen % sizeof(*in))
+ Host_Error("Mod_Q1BSP_LoadNodes: funny lump size in %s",loadmodel->name);
+ count = l->filelen / sizeof(*in);
+ out = Mem_Alloc(loadmodel->mempool, count*sizeof(*out));
+
+ loadmodel->brushq1.nodes = out;
+ loadmodel->brushq1.numnodes = count;
+
+ for ( i=0 ; i<count ; i++, in++, out++)
+ {
+ for (j=0 ; j<3 ; j++)
+ {
+ out->mins[j] = LittleShort(in->mins[j]);
+ out->maxs[j] = LittleShort(in->maxs[j]);
+ }
+
+ p = LittleLong(in->planenum);
+ out->plane = loadmodel->brushq1.planes + p;
+
+ out->firstsurface = LittleShort(in->firstface);
+ out->numsurfaces = LittleShort(in->numfaces);
+
+ for (j=0 ; j<2 ; j++)
+ {
+ p = LittleShort(in->children[j]);
+ if (p >= 0)
+ out->children[j] = loadmodel->brushq1.nodes + p;
+ else
+ out->children[j] = (mnode_t *)(loadmodel->brushq1.leafs + (-1 - p));
+ }
+ }
+
+ Mod_Q1BSP_SetParent(loadmodel->brushq1.nodes, NULL); // sets nodes and leafs
+}
+
+static void Mod_Q1BSP_LoadLeafs(lump_t *l)
+{
+ dleaf_t *in;
+ mleaf_t *out;
+ int i, j, count, p, pvschainbytes;
+ qbyte *pvs;
+
+ in = (void *)(mod_base + l->fileofs);
+ if (l->filelen % sizeof(*in))
+ Host_Error("Mod_Q1BSP_LoadLeafs: funny lump size in %s",loadmodel->name);
+ count = l->filelen / sizeof(*in);
+ out = Mem_Alloc(loadmodel->mempool, count*sizeof(*out));
+
+ loadmodel->brushq1.leafs = out;
+ loadmodel->brushq1.numleafs = count;
+ // get visleafs from the submodel data
+ pvschainbytes = (loadmodel->brushq1.submodels[0].visleafs+7)>>3;
+ loadmodel->brushq1.data_decompressedpvs = pvs = Mem_Alloc(loadmodel->mempool, loadmodel->brushq1.numleafs * pvschainbytes);
+
+ for ( i=0 ; i<count ; i++, in++, out++)
+ {
+ for (j=0 ; j<3 ; j++)
+ {
+ out->mins[j] = LittleShort(in->mins[j]);
+ out->maxs[j] = LittleShort(in->maxs[j]);
+ }
+
+ // FIXME: this function could really benefit from some error checking
+
+ out->contents = LittleLong(in->contents);
+
+ out->firstmarksurface = loadmodel->brushq1.marksurfaces + LittleShort(in->firstmarksurface);
+ out->nummarksurfaces = LittleShort(in->nummarksurfaces);
+ if (out->firstmarksurface < 0 || LittleShort(in->firstmarksurface) + out->nummarksurfaces > loadmodel->brushq1.nummarksurfaces)
+ {
+ Con_Printf("Mod_Q1BSP_LoadLeafs: invalid marksurface range %i:%i outside range %i:%i\n", out->firstmarksurface, out->firstmarksurface + out->nummarksurfaces, 0, loadmodel->brushq1.nummarksurfaces);
+ out->firstmarksurface = NULL;
+ out->nummarksurfaces = 0;
+ }
+
+ out->pvsdata = pvs;
+ memset(out->pvsdata, 0xFF, pvschainbytes);
+ pvs += pvschainbytes;
+
+ p = LittleLong(in->visofs);
+ if (p >= 0 && i > 0) // ignore visofs errors on leaf 0 (solid)
+ {
+ if (p >= loadmodel->brushq1.num_compressedpvs)
+ Con_Printf("Mod_Q1BSP_LoadLeafs: invalid visofs\n");
+ else
+ Mod_Q1BSP_DecompressVis(loadmodel->brushq1.data_compressedpvs + p, loadmodel->brushq1.data_compressedpvs + loadmodel->brushq1.num_compressedpvs, out->pvsdata, out->pvsdata + pvschainbytes);
+ }
+
+ for (j = 0;j < 4;j++)
+ out->ambient_sound_level[j] = in->ambient_level[j];
+
+ // FIXME: Insert caustics here
+ }
+}
+
+static void Mod_Q1BSP_LoadClipnodes(lump_t *l)
+{
+ dclipnode_t *in, *out;
+ int i, count;
+ hull_t *hull;
+
+ in = (void *)(mod_base + l->fileofs);
+ if (l->filelen % sizeof(*in))
+ Host_Error("Mod_Q1BSP_LoadClipnodes: funny lump size in %s",loadmodel->name);
+ count = l->filelen / sizeof(*in);
+ out = Mem_Alloc(loadmodel->mempool, count*sizeof(*out));
+
+ loadmodel->brushq1.clipnodes = out;
+ loadmodel->brushq1.numclipnodes = count;
+
+ if (loadmodel->brush.ishlbsp)
+ {
+ hull = &loadmodel->brushq1.hulls[1];
+ hull->clipnodes = out;
+ hull->firstclipnode = 0;
+ hull->lastclipnode = count-1;
+ hull->planes = loadmodel->brushq1.planes;
+ hull->clip_mins[0] = -16;
+ hull->clip_mins[1] = -16;
+ hull->clip_mins[2] = -36;
+ hull->clip_maxs[0] = 16;
+ hull->clip_maxs[1] = 16;
+ hull->clip_maxs[2] = 36;
+ VectorSubtract(hull->clip_maxs, hull->clip_mins, hull->clip_size);
+
+ hull = &loadmodel->brushq1.hulls[2];
+ hull->clipnodes = out;
+ hull->firstclipnode = 0;
+ hull->lastclipnode = count-1;
+ hull->planes = loadmodel->brushq1.planes;
+ hull->clip_mins[0] = -32;
+ hull->clip_mins[1] = -32;
+ hull->clip_mins[2] = -32;
+ hull->clip_maxs[0] = 32;
+ hull->clip_maxs[1] = 32;
+ hull->clip_maxs[2] = 32;
+ VectorSubtract(hull->clip_maxs, hull->clip_mins, hull->clip_size);
+
+ hull = &loadmodel->brushq1.hulls[3];
+ hull->clipnodes = out;
+ hull->firstclipnode = 0;
+ hull->lastclipnode = count-1;
+ hull->planes = loadmodel->brushq1.planes;
+ hull->clip_mins[0] = -16;
+ hull->clip_mins[1] = -16;
+ hull->clip_mins[2] = -18;
+ hull->clip_maxs[0] = 16;
+ hull->clip_maxs[1] = 16;
+ hull->clip_maxs[2] = 18;
+ VectorSubtract(hull->clip_maxs, hull->clip_mins, hull->clip_size);
+ }
+ else
+ {
+ hull = &loadmodel->brushq1.hulls[1];
+ hull->clipnodes = out;
+ hull->firstclipnode = 0;
+ hull->lastclipnode = count-1;
+ hull->planes = loadmodel->brushq1.planes;
+ hull->clip_mins[0] = -16;
+ hull->clip_mins[1] = -16;
+ hull->clip_mins[2] = -24;
+ hull->clip_maxs[0] = 16;
+ hull->clip_maxs[1] = 16;
+ hull->clip_maxs[2] = 32;
+ VectorSubtract(hull->clip_maxs, hull->clip_mins, hull->clip_size);
+
+ hull = &loadmodel->brushq1.hulls[2];
+ hull->clipnodes = out;
+ hull->firstclipnode = 0;
+ hull->lastclipnode = count-1;
+ hull->planes = loadmodel->brushq1.planes;
+ hull->clip_mins[0] = -32;
+ hull->clip_mins[1] = -32;
+ hull->clip_mins[2] = -24;
+ hull->clip_maxs[0] = 32;
+ hull->clip_maxs[1] = 32;
+ hull->clip_maxs[2] = 64;
+ VectorSubtract(hull->clip_maxs, hull->clip_mins, hull->clip_size);
+ }
+
+ for (i=0 ; i<count ; i++, out++, in++)
+ {
+ out->planenum = LittleLong(in->planenum);
+ out->children[0] = LittleShort(in->children[0]);
+ out->children[1] = LittleShort(in->children[1]);
+ if (out->children[0] >= count || out->children[1] >= count)
+ Host_Error("Corrupt clipping hull(out of range child)\n");
+ }
+}
+
+//Duplicate the drawing hull structure as a clipping hull
+static void Mod_Q1BSP_MakeHull0(void)
+{
+ mnode_t *in;
+ dclipnode_t *out;
+ int i;
+ hull_t *hull;
+
+ hull = &loadmodel->brushq1.hulls[0];
+
+ in = loadmodel->brushq1.nodes;
+ out = Mem_Alloc(loadmodel->mempool, loadmodel->brushq1.numnodes * sizeof(dclipnode_t));
+
+ hull->clipnodes = out;
+ hull->firstclipnode = 0;
+ hull->lastclipnode = loadmodel->brushq1.numnodes - 1;
+ hull->planes = loadmodel->brushq1.planes;
+
+ for (i = 0;i < loadmodel->brushq1.numnodes;i++, out++, in++)
+ {
+ out->planenum = in->plane - loadmodel->brushq1.planes;
+ out->children[0] = in->children[0]->contents < 0 ? in->children[0]->contents : in->children[0] - loadmodel->brushq1.nodes;
+ out->children[1] = in->children[1]->contents < 0 ? in->children[1]->contents : in->children[1] - loadmodel->brushq1.nodes;
+ }
+}
+
+static void Mod_Q1BSP_LoadMarksurfaces(lump_t *l)
+{
+ int i, j;
+ short *in;
+
+ in = (void *)(mod_base + l->fileofs);
+ if (l->filelen % sizeof(*in))
+ Host_Error("Mod_Q1BSP_LoadMarksurfaces: funny lump size in %s",loadmodel->name);
+ loadmodel->brushq1.nummarksurfaces = l->filelen / sizeof(*in);
+ loadmodel->brushq1.marksurfaces = Mem_Alloc(loadmodel->mempool, loadmodel->brushq1.nummarksurfaces * sizeof(int));
+
+ for (i = 0;i < loadmodel->brushq1.nummarksurfaces;i++)
+ {
+ j = (unsigned) LittleShort(in[i]);
+ if (j >= loadmodel->brushq1.numsurfaces)
+ Host_Error("Mod_Q1BSP_LoadMarksurfaces: bad surface number");
+ loadmodel->brushq1.marksurfaces[i] = j;
+ }
+}
+
+static void Mod_Q1BSP_LoadSurfedges(lump_t *l)
+{
+ int i;
+ int *in;
+
+ in = (void *)(mod_base + l->fileofs);
+ if (l->filelen % sizeof(*in))
+ Host_Error("Mod_Q1BSP_LoadSurfedges: funny lump size in %s",loadmodel->name);
+ loadmodel->brushq1.numsurfedges = l->filelen / sizeof(*in);
+ loadmodel->brushq1.surfedges = Mem_Alloc(loadmodel->mempool, loadmodel->brushq1.numsurfedges * sizeof(int));
+
+ for (i = 0;i < loadmodel->brushq1.numsurfedges;i++)
+ loadmodel->brushq1.surfedges[i] = LittleLong(in[i]);
+}
+
+
+static void Mod_Q1BSP_LoadPlanes(lump_t *l)
+{
+ int i;
+ mplane_t *out;
+ dplane_t *in;
+
+ in = (void *)(mod_base + l->fileofs);
+ if (l->filelen % sizeof(*in))
+ Host_Error("Mod_Q1BSP_LoadPlanes: funny lump size in %s", loadmodel->name);
+
+ loadmodel->brushq1.numplanes = l->filelen / sizeof(*in);
+ loadmodel->brushq1.planes = out = Mem_Alloc(loadmodel->mempool, loadmodel->brushq1.numplanes * sizeof(*out));
+
+ for (i = 0;i < loadmodel->brushq1.numplanes;i++, in++, out++)
+ {
+ out->normal[0] = LittleFloat(in->normal[0]);
+ out->normal[1] = LittleFloat(in->normal[1]);
+ out->normal[2] = LittleFloat(in->normal[2]);
+ out->dist = LittleFloat(in->dist);
+
+ PlaneClassify(out);
+ }
+}
+
+typedef struct portal_s
+{
+ mplane_t plane;
+ mnode_t *nodes[2]; // [0] = front side of plane
+ struct portal_s *next[2];
+ winding_t *winding;
+ struct portal_s *chain; // all portals are linked into a list
+}
+portal_t;
+
+static portal_t *portalchain;
+
+/*
+===========
+AllocPortal
+===========
+*/
+static portal_t *AllocPortal(void)
+{
+ portal_t *p;
+ p = Mem_Alloc(loadmodel->mempool, sizeof(portal_t));
+ p->chain = portalchain;
+ portalchain = p;
+ return p;
+}
+
+static void FreePortal(portal_t *p)
+{
+ Mem_Free(p);
+}
+
+static void Mod_Q1BSP_RecursiveRecalcNodeBBox(mnode_t *node)
+{
+ // calculate children first
+ if (node->children[0]->contents >= 0)
+ Mod_Q1BSP_RecursiveRecalcNodeBBox(node->children[0]);
+ if (node->children[1]->contents >= 0)
+ Mod_Q1BSP_RecursiveRecalcNodeBBox(node->children[1]);
+
+ // make combined bounding box from children
+ node->mins[0] = min(node->children[0]->mins[0], node->children[1]->mins[0]);
+ node->mins[1] = min(node->children[0]->mins[1], node->children[1]->mins[1]);
+ node->mins[2] = min(node->children[0]->mins[2], node->children[1]->mins[2]);
+ node->maxs[0] = max(node->children[0]->maxs[0], node->children[1]->maxs[0]);
+ node->maxs[1] = max(node->children[0]->maxs[1], node->children[1]->maxs[1]);
+ node->maxs[2] = max(node->children[0]->maxs[2], node->children[1]->maxs[2]);
+}
+
+static void Mod_Q1BSP_FinalizePortals(void)
+{
+ int i, j, numportals, numpoints;
+ portal_t *p, *pnext;
+ 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.leafs;
+ endleaf = leaf + loadmodel->brushq1.numleafs;
+ for (;leaf < endleaf;leaf++)
+ {
+ VectorSet(leaf->mins, 2000000000, 2000000000, 2000000000);
+ VectorSet(leaf->maxs, -2000000000, -2000000000, -2000000000);
+ }
+ p = portalchain;
+ while (p)
+ {
+ if (p->winding)
+ {
+ for (i = 0;i < 2;i++)
+ {
+ leaf = (mleaf_t *)p->nodes[i];
+ w = p->winding;
+ for (j = 0;j < w->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];
+ }
+ }
+ }
+ p = p->chain;
+ }
+
+ Mod_Q1BSP_RecursiveRecalcNodeBBox(loadmodel->brushq1.nodes);
+
+ // tally up portal and point counts
+ p = portalchain;
+ numportals = 0;
+ numpoints = 0;
+ while (p)
+ {
+ // 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]
+ && 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;
+ }
+ p = p->chain;
+ }
+ loadmodel->brushq1.portals = Mem_Alloc(loadmodel->mempool, numportals * sizeof(mportal_t) + numpoints * sizeof(mvertex_t));
+ loadmodel->brushq1.numportals = numportals;
+ loadmodel->brushq1.portalpoints = (void *)((qbyte *) loadmodel->brushq1.portals + numportals * sizeof(mportal_t));
+ loadmodel->brushq1.numportalpoints = numpoints;
+ // clear all leaf portal chains
+ for (i = 0;i < loadmodel->brushq1.numleafs;i++)
+ loadmodel->brushq1.leafs[i].portals = NULL;
+ // process all portals in the global portal chain, while freeing them
+ portal = loadmodel->brushq1.portals;
+ point = loadmodel->brushq1.portalpoints;
+ p = portalchain;
+ portalchain = NULL;
+ while (p)
+ {
+ pnext = p->chain;
+
+ if (p->winding)
+ {
+ // 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
+ if (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)
+ {
+ // first make the back to front portal(forward portal)
+ portal->points = point;
+ portal->numpoints = p->winding->numpoints;
+ portal->plane.dist = p->plane.dist;
+ VectorCopy(p->plane.normal, portal->plane.normal);
+ portal->here = (mleaf_t *)p->nodes[1];
+ portal->past = (mleaf_t *)p->nodes[0];
+ // copy points
+ for (j = 0;j < portal->numpoints;j++)
+ {
+ VectorCopy(p->winding->points[j], point->position);
+ point++;
+ }
+ PlaneClassify(&portal->plane);
+
+ // link into leaf's portal chain
+ portal->next = portal->here->portals;
+ portal->here->portals = portal;
+
+ // advance to next portal
+ portal++;
+
+ // then make the front to back portal(backward portal)
+ portal->points = point;
+ portal->numpoints = p->winding->numpoints;
+ portal->plane.dist = -p->plane.dist;
+ VectorNegate(p->plane.normal, portal->plane.normal);
+ portal->here = (mleaf_t *)p->nodes[0];
+ portal->past = (mleaf_t *)p->nodes[1];
+ // copy points
+ for (j = portal->numpoints - 1;j >= 0;j--)
+ {
+ VectorCopy(p->winding->points[j], point->position);
+ point++;
+ }
+ PlaneClassify(&portal->plane);
+
+ // link into leaf's portal chain
+ portal->next = portal->here->portals;
+ portal->here->portals = portal;
+
+ // advance to next portal
+ portal++;
+ }
+ Winding_Free(p->winding);
+ }
+ FreePortal(p);
+ p = pnext;
+ }
+}
+
+/*
+=============
+AddPortalToNodes
+=============
+*/
+static void AddPortalToNodes(portal_t *p, mnode_t *front, mnode_t *back)
+{
+ if (!front)
+ Host_Error("AddPortalToNodes: NULL front node");
+ if (!back)
+ Host_Error("AddPortalToNodes: NULL back node");
+ if (p->nodes[0] || p->nodes[1])
+ Host_Error("AddPortalToNodes: already included");
+ // note: front == back is handled gracefully, because leaf 0 is the shared solid leaf, it can often have portals with the same leaf on both sides
+
+ p->nodes[0] = front;
+ p->next[0] = (portal_t *)front->portals;
+ front->portals = (mportal_t *)p;
+
+ p->nodes[1] = back;
+ p->next[1] = (portal_t *)back->portals;
+ back->portals = (mportal_t *)p;
+}
+
+/*
+=============
+RemovePortalFromNode
+=============
+*/
+static void RemovePortalFromNodes(portal_t *portal)
+{
+ int i;
+ mnode_t *node;
+ void **portalpointer;
+ portal_t *t;
+ for (i = 0;i < 2;i++)
+ {
+ node = portal->nodes[i];
+
+ portalpointer = (void **) &node->portals;
+ while (1)
+ {
+ t = *portalpointer;
+ if (!t)
+ Host_Error("RemovePortalFromNodes: portal not in leaf");
+
+ if (t == portal)
+ {
+ if (portal->nodes[0] == node)
+ {
+ *portalpointer = portal->next[0];
+ portal->nodes[0] = NULL;
+ }
+ else if (portal->nodes[1] == node)
+ {
+ *portalpointer = portal->next[1];
+ portal->nodes[1] = NULL;
+ }
+ else
+ Host_Error("RemovePortalFromNodes: portal not bounding leaf");
+ break;
+ }
+
+ if (t->nodes[0] == node)
+ portalpointer = (void **) &t->next[0];
+ else if (t->nodes[1] == node)
+ portalpointer = (void **) &t->next[1];
+ else
+ Host_Error("RemovePortalFromNodes: portal not bounding leaf");
+ }
+ }
+}
+
+static void Mod_Q1BSP_RecursiveNodePortals(mnode_t *node)
+{
+ int side;
+ mnode_t *front, *back, *other_node;
+ mplane_t clipplane, *plane;
+ portal_t *portal, *nextportal, *nodeportal, *splitportal, *temp;
+ winding_t *nodeportalwinding, *frontwinding, *backwinding;
+
+ // if a leaf, we're done
+ if (node->contents)
+ return;
+
+ plane = node->plane;
+
+ front = node->children[0];
+ back = node->children[1];
+ if (front == back)
+ Host_Error("Mod_Q1BSP_RecursiveNodePortals: corrupt node hierarchy");
+
+ // create the new portal by generating a polygon for the node plane,
+ // and clipping it by all of the other portals(which came from nodes above this one)
+ nodeportal = AllocPortal();
+ nodeportal->plane = *plane;
+
+ nodeportalwinding = Winding_NewFromPlane(nodeportal->plane.normal[0], nodeportal->plane.normal[1], nodeportal->plane.normal[2], nodeportal->plane.dist);
+ side = 0; // shut up compiler warning
+ for (portal = (portal_t *)node->portals;portal;portal = portal->next[side])
+ {
+ clipplane = portal->plane;
+ if (portal->nodes[0] == portal->nodes[1])
+ Host_Error("Mod_Q1BSP_RecursiveNodePortals: portal has same node on both sides(1)");
+ if (portal->nodes[0] == node)
+ side = 0;
+ else if (portal->nodes[1] == node)
+ {
+ clipplane.dist = -clipplane.dist;
+ VectorNegate(clipplane.normal, clipplane.normal);
+ side = 1;
+ }
+ 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_Printf("Mod_Q1BSP_RecursiveNodePortals: WARNING: new portal was clipped away\n");
+ break;
+ }