2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 // note: model_shared.c sets up r_notexture, and r_surf_notexture
29 qbyte mod_q1bsp_novis[(MAX_MAP_LEAFS + 7)/ 8];
31 //cvar_t r_subdivide_size = {CVAR_SAVE, "r_subdivide_size", "128"};
32 cvar_t halflifebsp = {0, "halflifebsp", "0"};
33 cvar_t r_novis = {0, "r_novis", "0"};
34 cvar_t r_miplightmaps = {CVAR_SAVE, "r_miplightmaps", "0"};
35 cvar_t r_lightmaprgba = {0, "r_lightmaprgba", "1"};
36 cvar_t r_nosurftextures = {0, "r_nosurftextures", "0"};
37 cvar_t mod_q3bsp_curves_subdivide_level = {0, "mod_q3bsp_curves_subdivide_level", "2"};
38 cvar_t mod_q3bsp_curves_collisions = {0, "mod_q3bsp_curves_collisions", "1"};
39 cvar_t mod_q3bsp_optimizedtraceline = {0, "mod_q3bsp_optimizedtraceline", "1"};
40 cvar_t mod_q3bsp_debugtracebrush = {0, "mod_q3bsp_debugtracebrush", "0"};
42 static void Mod_Q1BSP_Collision_Init (void);
43 void Mod_BrushInit(void)
45 // Cvar_RegisterVariable(&r_subdivide_size);
46 Cvar_RegisterVariable(&halflifebsp);
47 Cvar_RegisterVariable(&r_novis);
48 Cvar_RegisterVariable(&r_miplightmaps);
49 Cvar_RegisterVariable(&r_lightmaprgba);
50 Cvar_RegisterVariable(&r_nosurftextures);
51 Cvar_RegisterVariable(&mod_q3bsp_curves_subdivide_level);
52 Cvar_RegisterVariable(&mod_q3bsp_curves_collisions);
53 Cvar_RegisterVariable(&mod_q3bsp_optimizedtraceline);
54 Cvar_RegisterVariable(&mod_q3bsp_debugtracebrush);
55 memset(mod_q1bsp_novis, 0xff, sizeof(mod_q1bsp_novis));
56 Mod_Q1BSP_Collision_Init();
59 static mleaf_t *Mod_Q1BSP_PointInLeaf(model_t *model, const vec3_t p)
66 Mod_CheckLoaded(model);
68 // LordHavoc: modified to start at first clip node,
69 // in other words: first node of the (sub)model
70 node = model->brushq1.nodes + model->brushq1.hulls[0].firstclipnode;
71 while (node->contents == 0)
72 node = node->children[(node->plane->type < 3 ? p[node->plane->type] : DotProduct(p,node->plane->normal)) < node->plane->dist];
74 return (mleaf_t *)node;
77 static void Mod_Q1BSP_AmbientSoundLevelsForPoint(model_t *model, const vec3_t p, qbyte *out, int outsize)
81 leaf = Mod_Q1BSP_PointInLeaf(model, p);
84 i = min(outsize, (int)sizeof(leaf->ambient_sound_level));;
87 memcpy(out, leaf->ambient_sound_level, i);
93 memset(out, 0, outsize);
96 static int Mod_Q1BSP_BoxTouchingPVS(model_t *model, const qbyte *pvs, const vec3_t mins, const vec3_t maxs)
98 int clusterindex, side, nodestackindex = 0;
99 mnode_t *node, *nodestack[1024];
100 node = model->brushq1.nodes + model->brushq1.hulls[0].firstclipnode;
105 // node - recurse down the BSP tree
106 side = BoxOnPlaneSide(mins, maxs, node->plane) - 1;
109 // box is on one side of plane, take that path
110 node = node->children[side];
114 // box crosses plane, take one path and remember the other
115 nodestack[nodestackindex++] = node->children[0];
116 node = node->children[1];
121 // leaf - check cluster bit
122 clusterindex = ((mleaf_t *)node)->clusterindex;
123 if (CHECKPVSBIT(pvs, clusterindex))
125 // it is visible, return immediately with the news
130 // nothing to see here, try another path we didn't take earlier
131 if (nodestackindex == 0)
133 node = nodestack[--nodestackindex];
142 static int Mod_Q1BSP_PointContents(model_t *model, const vec3_t p)
147 return CONTENTS_EMPTY;
149 Mod_CheckLoaded(model);
151 // LordHavoc: modified to start at first clip node,
152 // in other words: first node of the (sub)model
153 node = model->brushq1.nodes + model->brushq1.hulls[0].firstclipnode;
154 while (node->contents == 0)
155 node = node->children[(node->plane->type < 3 ? p[node->plane->type] : DotProduct(p,node->plane->normal)) < node->plane->dist];
157 return ((mleaf_t *)node)->contents;
161 typedef struct findnonsolidlocationinfo_s
169 findnonsolidlocationinfo_t;
172 extern cvar_t samelevel;
174 static void Mod_Q1BSP_FindNonSolidLocation_r_Leaf(findnonsolidlocationinfo_t *info, mleaf_t *leaf)
176 int i, surfnum, k, *tri, *mark;
177 float dist, f, vert[3][3], edge[3][3], facenormal[3], edgenormal[3][3], point[3];
182 for (surfnum = 0, mark = leaf->firstmarksurface;surfnum < leaf->nummarksurfaces;surfnum++, mark++)
184 surf = info->model->brushq1.surfaces + *mark;
185 if (surf->flags & SURF_SOLIDCLIP)
188 VectorCopy(surf->plane->normal, surfnormal);
189 if (surf->flags & SURF_PLANEBACK)
190 VectorNegate(surfnormal, surfnormal);
192 for (k = 0;k < surf->mesh.num_triangles;k++)
194 tri = surf->mesh.data_element3i + k * 3;
195 VectorCopy((surf->mesh.data_vertex3f + tri[0] * 3), vert[0]);
196 VectorCopy((surf->mesh.data_vertex3f + tri[1] * 3), vert[1]);
197 VectorCopy((surf->mesh.data_vertex3f + tri[2] * 3), vert[2]);
198 VectorSubtract(vert[1], vert[0], edge[0]);
199 VectorSubtract(vert[2], vert[1], edge[1]);
200 CrossProduct(edge[1], edge[0], facenormal);
201 if (facenormal[0] || facenormal[1] || facenormal[2])
203 VectorNormalize(facenormal);
205 if (VectorDistance(facenormal, surfnormal) > 0.01f)
206 Con_Printf("a2! %f %f %f != %f %f %f\n", facenormal[0], facenormal[1], facenormal[2], surfnormal[0], surfnormal[1], surfnormal[2]);
208 f = DotProduct(info->center, facenormal) - DotProduct(vert[0], facenormal);
209 if (f <= info->bestdist && f >= -info->bestdist)
211 VectorSubtract(vert[0], vert[2], edge[2]);
212 VectorNormalize(edge[0]);
213 VectorNormalize(edge[1]);
214 VectorNormalize(edge[2]);
215 CrossProduct(facenormal, edge[0], edgenormal[0]);
216 CrossProduct(facenormal, edge[1], edgenormal[1]);
217 CrossProduct(facenormal, edge[2], edgenormal[2]);
219 if (samelevel.integer & 1)
220 VectorNegate(edgenormal[0], edgenormal[0]);
221 if (samelevel.integer & 2)
222 VectorNegate(edgenormal[1], edgenormal[1]);
223 if (samelevel.integer & 4)
224 VectorNegate(edgenormal[2], edgenormal[2]);
225 for (i = 0;i < 3;i++)
226 if (DotProduct(vert[0], edgenormal[i]) > DotProduct(vert[i], edgenormal[i]) + 0.1f
227 || DotProduct(vert[1], edgenormal[i]) > DotProduct(vert[i], edgenormal[i]) + 0.1f
228 || DotProduct(vert[2], edgenormal[i]) > DotProduct(vert[i], edgenormal[i]) + 0.1f)
229 Con_Printf("a! %i : %f %f %f (%f %f %f)\n", i, edgenormal[i][0], edgenormal[i][1], edgenormal[i][2], facenormal[0], facenormal[1], facenormal[2]);
232 if (DotProduct(info->center, edgenormal[0]) < DotProduct(vert[0], edgenormal[0])
233 && DotProduct(info->center, edgenormal[1]) < DotProduct(vert[1], edgenormal[1])
234 && DotProduct(info->center, edgenormal[2]) < DotProduct(vert[2], edgenormal[2]))
236 // we got lucky, the center is within the face
237 dist = DotProduct(info->center, facenormal) - DotProduct(vert[0], facenormal);
241 if (info->bestdist > dist)
243 info->bestdist = dist;
244 VectorScale(facenormal, (info->radius - -dist), info->nudge);
249 if (info->bestdist > dist)
251 info->bestdist = dist;
252 VectorScale(facenormal, (info->radius - dist), info->nudge);
258 // check which edge or vertex the center is nearest
259 for (i = 0;i < 3;i++)
261 f = DotProduct(info->center, edge[i]);
262 if (f >= DotProduct(vert[0], edge[i])
263 && f <= DotProduct(vert[1], edge[i]))
266 VectorMA(info->center, -f, edge[i], point);
267 dist = sqrt(DotProduct(point, point));
268 if (info->bestdist > dist)
270 info->bestdist = dist;
271 VectorScale(point, (info->radius / dist), info->nudge);
273 // skip both vertex checks
274 // (both are further away than this edge)
279 // not on edge, check first vertex of edge
280 VectorSubtract(info->center, vert[i], point);
281 dist = sqrt(DotProduct(point, point));
282 if (info->bestdist > dist)
284 info->bestdist = dist;
285 VectorScale(point, (info->radius / dist), info->nudge);
297 static void Mod_Q1BSP_FindNonSolidLocation_r(findnonsolidlocationinfo_t *info, mnode_t *node)
301 if (((mleaf_t *)node)->nummarksurfaces)
302 Mod_Q1BSP_FindNonSolidLocation_r_Leaf(info, (mleaf_t *)node);
306 float f = PlaneDiff(info->center, node->plane);
307 if (f >= -info->bestdist)
308 Mod_Q1BSP_FindNonSolidLocation_r(info, node->children[0]);
309 if (f <= info->bestdist)
310 Mod_Q1BSP_FindNonSolidLocation_r(info, node->children[1]);
314 static void Mod_Q1BSP_FindNonSolidLocation(model_t *model, const vec3_t in, vec3_t out, float radius)
317 findnonsolidlocationinfo_t info;
323 VectorCopy(in, info.center);
324 info.radius = radius;
329 VectorClear(info.nudge);
330 info.bestdist = radius;
331 Mod_Q1BSP_FindNonSolidLocation_r(&info, model->brushq1.nodes + model->brushq1.hulls[0].firstclipnode);
332 VectorAdd(info.center, info.nudge, info.center);
334 while (info.bestdist < radius && ++i < 10);
335 VectorCopy(info.center, out);
338 int Mod_Q1BSP_SuperContentsFromNativeContents(model_t *model, int nativecontents)
340 switch(nativecontents)
345 return SUPERCONTENTS_SOLID;
347 return SUPERCONTENTS_WATER;
349 return SUPERCONTENTS_SLIME;
351 return SUPERCONTENTS_LAVA;
353 return SUPERCONTENTS_SKY;
358 int Mod_Q1BSP_NativeContentsFromSuperContents(model_t *model, int supercontents)
360 if (supercontents & SUPERCONTENTS_SOLID)
361 return CONTENTS_SOLID;
362 if (supercontents & SUPERCONTENTS_SKY)
364 if (supercontents & SUPERCONTENTS_LAVA)
365 return CONTENTS_LAVA;
366 if (supercontents & SUPERCONTENTS_SLIME)
367 return CONTENTS_SLIME;
368 if (supercontents & SUPERCONTENTS_WATER)
369 return CONTENTS_WATER;
370 return CONTENTS_EMPTY;
375 // the hull we're tracing through
378 // the trace structure to fill in
381 // start, end, and end - start (in model space)
386 RecursiveHullCheckTraceInfo_t;
388 // 1/32 epsilon to keep floating point happy
389 #define DIST_EPSILON (0.03125)
391 #define HULLCHECKSTATE_EMPTY 0
392 #define HULLCHECKSTATE_SOLID 1
393 #define HULLCHECKSTATE_DONE 2
395 static int Mod_Q1BSP_RecursiveHullCheck(RecursiveHullCheckTraceInfo_t *t, int num, double p1f, double p2f, double p1[3], double p2[3])
397 // status variables, these don't need to be saved on the stack when
398 // recursing... but are because this should be thread-safe
399 // (note: tracing against a bbox is not thread-safe, yet)
404 // variables that need to be stored on the stack when recursing
409 // LordHavoc: a goto! everyone flee in terror... :)
414 num = Mod_Q1BSP_SuperContentsFromNativeContents(NULL, num);
415 if (!t->trace->startfound)
417 t->trace->startfound = true;
418 t->trace->startsupercontents |= num;
420 if (num & SUPERCONTENTS_LIQUIDSMASK)
421 t->trace->inwater = true;
423 t->trace->inopen = true;
424 if (num & t->trace->hitsupercontentsmask)
426 // if the first leaf is solid, set startsolid
427 if (t->trace->allsolid)
428 t->trace->startsolid = true;
429 #if COLLISIONPARANOID >= 3
432 return HULLCHECKSTATE_SOLID;
436 t->trace->allsolid = false;
437 #if COLLISIONPARANOID >= 3
440 return HULLCHECKSTATE_EMPTY;
444 // find the point distances
445 node = t->hull->clipnodes + num;
447 plane = t->hull->planes + node->planenum;
450 t1 = p1[plane->type] - plane->dist;
451 t2 = p2[plane->type] - plane->dist;
455 t1 = DotProduct (plane->normal, p1) - plane->dist;
456 t2 = DotProduct (plane->normal, p2) - plane->dist;
463 #if COLLISIONPARANOID >= 3
466 num = node->children[1];
475 #if COLLISIONPARANOID >= 3
478 num = node->children[0];
484 // the line intersects, find intersection point
485 // LordHavoc: this uses the original trace for maximum accuracy
486 #if COLLISIONPARANOID >= 3
491 t1 = t->start[plane->type] - plane->dist;
492 t2 = t->end[plane->type] - plane->dist;
496 t1 = DotProduct (plane->normal, t->start) - plane->dist;
497 t2 = DotProduct (plane->normal, t->end) - plane->dist;
500 midf = t1 / (t1 - t2);
501 midf = bound(p1f, midf, p2f);
502 VectorMA(t->start, midf, t->dist, mid);
504 // recurse both sides, front side first
505 ret = Mod_Q1BSP_RecursiveHullCheck(t, node->children[side], p1f, midf, p1, mid);
506 // if this side is not empty, return what it is (solid or done)
507 if (ret != HULLCHECKSTATE_EMPTY)
510 ret = Mod_Q1BSP_RecursiveHullCheck(t, node->children[side ^ 1], midf, p2f, mid, p2);
511 // if other side is not solid, return what it is (empty or done)
512 if (ret != HULLCHECKSTATE_SOLID)
515 // front is air and back is solid, this is the impact point...
518 t->trace->plane.dist = -plane->dist;
519 VectorNegate (plane->normal, t->trace->plane.normal);
523 t->trace->plane.dist = plane->dist;
524 VectorCopy (plane->normal, t->trace->plane.normal);
527 // calculate the true fraction
528 t1 = DotProduct(t->trace->plane.normal, t->start) - t->trace->plane.dist;
529 t2 = DotProduct(t->trace->plane.normal, t->end) - t->trace->plane.dist;
530 midf = t1 / (t1 - t2);
531 t->trace->realfraction = bound(0, midf, 1);
533 // calculate the return fraction which is nudged off the surface a bit
534 midf = (t1 - DIST_EPSILON) / (t1 - t2);
535 t->trace->fraction = bound(0, midf, 1);
537 #if COLLISIONPARANOID >= 3
540 return HULLCHECKSTATE_DONE;
543 #if COLLISIONPARANOID < 2
544 static int Mod_Q1BSP_RecursiveHullCheckPoint(RecursiveHullCheckTraceInfo_t *t, int num)
547 num = t->hull->clipnodes[num].children[(t->hull->planes[t->hull->clipnodes[num].planenum].type < 3 ? t->start[t->hull->planes[t->hull->clipnodes[num].planenum].type] : DotProduct(t->hull->planes[t->hull->clipnodes[num].planenum].normal, t->start)) < t->hull->planes[t->hull->clipnodes[num].planenum].dist];
548 num = Mod_Q1BSP_SuperContentsFromNativeContents(NULL, num);
549 t->trace->startsupercontents |= num;
550 if (num & SUPERCONTENTS_LIQUIDSMASK)
551 t->trace->inwater = true;
553 t->trace->inopen = true;
554 if (num & t->trace->hitsupercontentsmask)
556 t->trace->allsolid = t->trace->startsolid = true;
557 return HULLCHECKSTATE_SOLID;
561 t->trace->allsolid = t->trace->startsolid = false;
562 return HULLCHECKSTATE_EMPTY;
567 static void Mod_Q1BSP_TraceBox(struct model_s *model, int frame, trace_t *trace, const vec3_t boxstartmins, const vec3_t boxstartmaxs, const vec3_t boxendmins, const vec3_t boxendmaxs, int hitsupercontentsmask)
569 // this function currently only supports same size start and end
571 RecursiveHullCheckTraceInfo_t rhc;
573 memset(&rhc, 0, sizeof(rhc));
574 memset(trace, 0, sizeof(trace_t));
576 rhc.trace->hitsupercontentsmask = hitsupercontentsmask;
577 rhc.trace->fraction = 1;
578 rhc.trace->realfraction = 1;
579 rhc.trace->allsolid = true;
580 VectorSubtract(boxstartmaxs, boxstartmins, boxsize);
582 rhc.hull = &model->brushq1.hulls[0]; // 0x0x0
583 else if (model->brush.ishlbsp)
585 // LordHavoc: this has to have a minor tolerance (the .1) because of
586 // minor float precision errors from the box being transformed around
587 if (boxsize[0] < 32.1)
589 if (boxsize[2] < 54) // pick the nearest of 36 or 72
590 rhc.hull = &model->brushq1.hulls[3]; // 32x32x36
592 rhc.hull = &model->brushq1.hulls[1]; // 32x32x72
595 rhc.hull = &model->brushq1.hulls[2]; // 64x64x64
599 // LordHavoc: this has to have a minor tolerance (the .1) because of
600 // minor float precision errors from the box being transformed around
601 if (boxsize[0] < 32.1)
602 rhc.hull = &model->brushq1.hulls[1]; // 32x32x56
604 rhc.hull = &model->brushq1.hulls[2]; // 64x64x88
606 VectorSubtract(boxstartmins, rhc.hull->clip_mins, rhc.start);
607 VectorSubtract(boxendmins, rhc.hull->clip_mins, rhc.end);
608 VectorSubtract(rhc.end, rhc.start, rhc.dist);
609 #if COLLISIONPARANOID >= 2
610 Con_Printf("t(%f %f %f,%f %f %f,%i %f %f %f)", rhc.start[0], rhc.start[1], rhc.start[2], rhc.end[0], rhc.end[1], rhc.end[2], rhc.hull - model->brushq1.hulls, rhc.hull->clip_mins[0], rhc.hull->clip_mins[1], rhc.hull->clip_mins[2]);
611 Mod_Q1BSP_RecursiveHullCheck(&rhc, rhc.hull->firstclipnode, 0, 1, rhc.start, rhc.end);
614 if (DotProduct(rhc.dist, rhc.dist))
615 Mod_Q1BSP_RecursiveHullCheck(&rhc, rhc.hull->firstclipnode, 0, 1, rhc.start, rhc.end);
617 Mod_Q1BSP_RecursiveHullCheckPoint(&rhc, rhc.hull->firstclipnode);
621 static hull_t box_hull;
622 static dclipnode_t box_clipnodes[6];
623 static mplane_t box_planes[6];
625 static void Mod_Q1BSP_Collision_Init (void)
630 //Set up the planes and clipnodes so that the six floats of a bounding box
631 //can just be stored out and get a proper hull_t structure.
633 box_hull.clipnodes = box_clipnodes;
634 box_hull.planes = box_planes;
635 box_hull.firstclipnode = 0;
636 box_hull.lastclipnode = 5;
638 for (i = 0;i < 6;i++)
640 box_clipnodes[i].planenum = i;
644 box_clipnodes[i].children[side] = CONTENTS_EMPTY;
646 box_clipnodes[i].children[side^1] = i + 1;
648 box_clipnodes[i].children[side^1] = CONTENTS_SOLID;
650 box_planes[i].type = i>>1;
651 box_planes[i].normal[i>>1] = 1;
655 void Collision_ClipTrace_Box(trace_t *trace, const vec3_t cmins, const vec3_t cmaxs, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int hitsupercontentsmask, int boxsupercontents)
659 colplanef_t cbox_planes[6];
660 cbox.supercontents = boxsupercontents;
663 cbox.numtriangles = 0;
664 cbox.planes = cbox_planes;
666 cbox.elements = NULL;
674 cbox_planes[0].normal[0] = 1;cbox_planes[0].normal[1] = 0;cbox_planes[0].normal[2] = 0;cbox_planes[0].dist = cmaxs[0] - mins[0];
675 cbox_planes[1].normal[0] = -1;cbox_planes[1].normal[1] = 0;cbox_planes[1].normal[2] = 0;cbox_planes[1].dist = maxs[0] - cmins[0];
676 cbox_planes[2].normal[0] = 0;cbox_planes[2].normal[1] = 1;cbox_planes[2].normal[2] = 0;cbox_planes[2].dist = cmaxs[1] - mins[1];
677 cbox_planes[3].normal[0] = 0;cbox_planes[3].normal[1] = -1;cbox_planes[3].normal[2] = 0;cbox_planes[3].dist = maxs[1] - cmins[1];
678 cbox_planes[4].normal[0] = 0;cbox_planes[4].normal[1] = 0;cbox_planes[4].normal[2] = 1;cbox_planes[4].dist = cmaxs[2] - mins[2];
679 cbox_planes[5].normal[0] = 0;cbox_planes[5].normal[1] = 0;cbox_planes[5].normal[2] = -1;cbox_planes[5].dist = maxs[2] - cmins[2];
680 memset(trace, 0, sizeof(trace_t));
681 trace->hitsupercontentsmask = hitsupercontentsmask;
683 trace->realfraction = 1;
684 Collision_TraceLineBrushFloat(trace, start, end, &cbox, &cbox);
686 RecursiveHullCheckTraceInfo_t rhc;
687 // fill in a default trace
688 memset(&rhc, 0, sizeof(rhc));
689 memset(trace, 0, sizeof(trace_t));
690 //To keep everything totally uniform, bounding boxes are turned into small
691 //BSP trees instead of being compared directly.
692 // create a temp hull from bounding box sizes
693 box_planes[0].dist = cmaxs[0] - mins[0];
694 box_planes[1].dist = cmins[0] - maxs[0];
695 box_planes[2].dist = cmaxs[1] - mins[1];
696 box_planes[3].dist = cmins[1] - maxs[1];
697 box_planes[4].dist = cmaxs[2] - mins[2];
698 box_planes[5].dist = cmins[2] - maxs[2];
699 #if COLLISIONPARANOID >= 3
700 Con_Printf("box_planes %f:%f %f:%f %f:%f\ncbox %f %f %f:%f %f %f\nbox %f %f %f:%f %f %f\n", box_planes[0].dist, box_planes[1].dist, box_planes[2].dist, box_planes[3].dist, box_planes[4].dist, box_planes[5].dist, cmins[0], cmins[1], cmins[2], cmaxs[0], cmaxs[1], cmaxs[2], mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2]);
702 // trace a line through the generated clipping hull
703 //rhc.boxsupercontents = boxsupercontents;
704 rhc.hull = &box_hull;
706 rhc.trace->hitsupercontentsmask = hitsupercontentsmask;
707 rhc.trace->fraction = 1;
708 rhc.trace->realfraction = 1;
709 rhc.trace->allsolid = true;
710 VectorCopy(start, rhc.start);
711 VectorCopy(end, rhc.end);
712 VectorSubtract(rhc.end, rhc.start, rhc.dist);
713 Mod_Q1BSP_RecursiveHullCheck(&rhc, rhc.hull->firstclipnode, 0, 1, rhc.start, rhc.end);
714 //VectorMA(rhc.start, rhc.trace->fraction, rhc.dist, rhc.trace->endpos);
715 if (rhc.trace->startsupercontents)
716 rhc.trace->startsupercontents = boxsupercontents;
720 static int Mod_Q1BSP_LightPoint_RecursiveBSPNode(vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal, const mnode_t *node, float x, float y, float startz, float endz)
722 int side, distz = endz - startz;
727 if (node->contents < 0)
728 return false; // didn't hit anything
730 switch (node->plane->type)
733 node = node->children[x < node->plane->dist];
736 node = node->children[y < node->plane->dist];
739 side = startz < node->plane->dist;
740 if ((endz < node->plane->dist) == side)
742 node = node->children[side];
745 // found an intersection
746 mid = node->plane->dist;
749 back = front = x * node->plane->normal[0] + y * node->plane->normal[1];
750 front += startz * node->plane->normal[2];
751 back += endz * node->plane->normal[2];
752 side = front < node->plane->dist;
753 if ((back < node->plane->dist) == side)
755 node = node->children[side];
758 // found an intersection
759 mid = startz + distz * (front - node->plane->dist) / (front - back);
763 // go down front side
764 if (node->children[side]->contents >= 0 && Mod_Q1BSP_LightPoint_RecursiveBSPNode(ambientcolor, diffusecolor, diffusenormal, node->children[side], x, y, startz, mid))
765 return true; // hit something
768 // check for impact on this node
769 if (node->numsurfaces)
774 surf = cl.worldmodel->brushq1.surfaces + node->firstsurface;
775 for (i = 0;i < node->numsurfaces;i++, surf++)
777 if (!(surf->flags & SURF_LIGHTMAP))
778 continue; // no lightmaps
780 ds = (int) (x * surf->texinfo->vecs[0][0] + y * surf->texinfo->vecs[0][1] + mid * surf->texinfo->vecs[0][2] + surf->texinfo->vecs[0][3]);
781 dt = (int) (x * surf->texinfo->vecs[1][0] + y * surf->texinfo->vecs[1][1] + mid * surf->texinfo->vecs[1][2] + surf->texinfo->vecs[1][3]);
783 if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
786 ds -= surf->texturemins[0];
787 dt -= surf->texturemins[1];
789 if (ds > surf->extents[0] || dt > surf->extents[1])
795 int maps, line3, size3, dsfrac = ds & 15, dtfrac = dt & 15, scale = 0, r00 = 0, g00 = 0, b00 = 0, r01 = 0, g01 = 0, b01 = 0, r10 = 0, g10 = 0, b10 = 0, r11 = 0, g11 = 0, b11 = 0;
796 line3 = ((surf->extents[0]>>4)+1)*3;
797 size3 = ((surf->extents[0]>>4)+1) * ((surf->extents[1]>>4)+1)*3; // LordHavoc: *3 for colored lighting
799 lightmap = surf->samples + ((dt>>4) * ((surf->extents[0]>>4)+1) + (ds>>4))*3; // LordHavoc: *3 for color
801 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
803 scale = d_lightstylevalue[surf->styles[maps]];
804 r00 += lightmap[ 0] * scale;g00 += lightmap[ 1] * scale;b00 += lightmap[ 2] * scale;
805 r01 += lightmap[ 3] * scale;g01 += lightmap[ 4] * scale;b01 += lightmap[ 5] * scale;
806 r10 += lightmap[line3+0] * scale;g10 += lightmap[line3+1] * scale;b10 += lightmap[line3+2] * scale;
807 r11 += lightmap[line3+3] * scale;g11 += lightmap[line3+4] * scale;b11 += lightmap[line3+5] * scale;
812 LordHavoc: here's the readable version of the interpolation
813 code, not quite as easy for the compiler to optimize...
815 dsfrac is the X position in the lightmap pixel, * 16
816 dtfrac is the Y position in the lightmap pixel, * 16
817 r00 is top left corner, r01 is top right corner
818 r10 is bottom left corner, r11 is bottom right corner
819 g and b are the same layout.
820 r0 and r1 are the top and bottom intermediate results
822 first we interpolate the top two points, to get the top
825 r0 = (((r01-r00) * dsfrac) >> 4) + r00;
826 g0 = (((g01-g00) * dsfrac) >> 4) + g00;
827 b0 = (((b01-b00) * dsfrac) >> 4) + b00;
829 then we interpolate the bottom two points, to get the
832 r1 = (((r11-r10) * dsfrac) >> 4) + r10;
833 g1 = (((g11-g10) * dsfrac) >> 4) + g10;
834 b1 = (((b11-b10) * dsfrac) >> 4) + b10;
836 then we interpolate the top and bottom samples to get the
837 middle sample (the one which was requested)
839 r = (((r1-r0) * dtfrac) >> 4) + r0;
840 g = (((g1-g0) * dtfrac) >> 4) + g0;
841 b = (((b1-b0) * dtfrac) >> 4) + b0;
844 ambientcolor[0] += (float) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)) * (1.0f / 32768.0f);
845 ambientcolor[1] += (float) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)) * (1.0f / 32768.0f);
846 ambientcolor[2] += (float) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)) * (1.0f / 32768.0f);
848 return true; // success
853 node = node->children[side ^ 1];
855 distz = endz - startz;
860 void Mod_Q1BSP_LightPoint(model_t *model, const vec3_t p, vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal)
862 Mod_Q1BSP_LightPoint_RecursiveBSPNode(ambientcolor, diffusecolor, diffusenormal, cl.worldmodel->brushq1.nodes + cl.worldmodel->brushq1.hulls[0].firstclipnode, p[0], p[1], p[2], p[2] - 65536);
865 static void Mod_Q1BSP_DecompressVis(const qbyte *in, const qbyte *inend, qbyte *out, qbyte *outend)
868 qbyte *outstart = out;
873 Con_DPrintf("Mod_Q1BSP_DecompressVis: input underrun on model \"%s\" (decompressed %i of %i output bytes)\n", loadmodel->name, out - outstart, outend - outstart);
883 Con_DPrintf("Mod_Q1BSP_DecompressVis: input underrun (during zero-run) on model \"%s\" (decompressed %i of %i output bytes)\n", loadmodel->name, out - outstart, outend - outstart);
886 for (c = *in++;c > 0;c--)
890 Con_DPrintf("Mod_Q1BSP_DecompressVis: output overrun on model \"%s\" (decompressed %i of %i output bytes)\n", loadmodel->name, out - outstart, outend - outstart);
899 static void Mod_Q1BSP_LoadTextures(lump_t *l)
901 int i, j, k, num, max, altmax, mtwidth, mtheight, *dofs, incomplete;
903 texture_t *tx, *tx2, *anims[10], *altanims[10];
905 qbyte *data, *mtdata;
908 loadmodel->brushq1.textures = NULL;
910 // add two slots for notexture walls and notexture liquids
913 m = (dmiptexlump_t *)(mod_base + l->fileofs);
914 m->nummiptex = LittleLong (m->nummiptex);
915 loadmodel->brushq1.numtextures = m->nummiptex + 2;
920 loadmodel->brushq1.numtextures = 2;
923 loadmodel->brushq1.textures = Mem_Alloc(loadmodel->mempool, loadmodel->brushq1.numtextures * sizeof(texture_t));
925 // fill out all slots with notexture
926 for (i = 0, tx = loadmodel->brushq1.textures;i < loadmodel->brushq1.numtextures;i++, tx++)
929 strcpy(tx->name, "NO TEXTURE FOUND");
932 tx->skin.base = r_notexture;
933 tx->shader = &Cshader_wall_lightmap;
934 tx->flags = SURF_SOLIDCLIP;
935 if (i == loadmodel->brushq1.numtextures - 1)
937 tx->flags |= SURF_DRAWTURB | SURF_LIGHTBOTHSIDES;
938 tx->shader = &Cshader_water;
940 tx->currentframe = tx;
946 // just to work around bounds checking when debugging with it (array index out of bounds error thing)
948 // LordHavoc: mostly rewritten map texture loader
949 for (i = 0;i < m->nummiptex;i++)
951 dofs[i] = LittleLong(dofs[i]);
952 if (dofs[i] == -1 || r_nosurftextures.integer)
954 dmiptex = (miptex_t *)((qbyte *)m + dofs[i]);
956 // make sure name is no more than 15 characters
957 for (j = 0;dmiptex->name[j] && j < 15;j++)
958 name[j] = dmiptex->name[j];
961 mtwidth = LittleLong(dmiptex->width);
962 mtheight = LittleLong(dmiptex->height);
964 j = LittleLong(dmiptex->offsets[0]);
968 if (j < 40 || j + mtwidth * mtheight > l->filelen)
970 Con_Printf("Texture \"%s\" in \"%s\"is corrupt or incomplete\n", dmiptex->name, loadmodel->name);
973 mtdata = (qbyte *)dmiptex + j;
976 if ((mtwidth & 15) || (mtheight & 15))
977 Con_Printf("warning: texture \"%s\" in \"%s\" is not 16 aligned", dmiptex->name, loadmodel->name);
979 // LordHavoc: force all names to lowercase
980 for (j = 0;name[j];j++)
981 if (name[j] >= 'A' && name[j] <= 'Z')
982 name[j] += 'a' - 'A';
984 tx = loadmodel->brushq1.textures + i;
985 strcpy(tx->name, name);
987 tx->height = mtheight;
991 sprintf(tx->name, "unnamed%i", i);
992 Con_Printf("warning: unnamed texture in %s, renaming to %s\n", loadmodel->name, tx->name);
995 // LordHavoc: HL sky textures are entirely different than quake
996 if (!loadmodel->brush.ishlbsp && !strncmp(tx->name, "sky", 3) && mtwidth == 256 && mtheight == 128)
998 if (loadmodel->isworldmodel)
1000 data = loadimagepixels(tx->name, false, 0, 0);
1003 if (image_width == 256 && image_height == 128)
1011 Con_Printf("Invalid replacement texture for sky \"%s\" in %\"%s\", must be 256x128 pixels\n", tx->name, loadmodel->name);
1013 R_InitSky(mtdata, 1);
1016 else if (mtdata != NULL)
1017 R_InitSky(mtdata, 1);
1022 if (!Mod_LoadSkinFrame(&tx->skin, tx->name, TEXF_MIPMAP | TEXF_ALPHA | TEXF_PRECACHE, false, true, true))
1024 // did not find external texture, load it from the bsp or wad3
1025 if (loadmodel->brush.ishlbsp)
1027 // internal texture overrides wad
1028 qbyte *pixels, *freepixels, *fogpixels;
1029 pixels = freepixels = NULL;
1031 pixels = W_ConvertWAD3Texture(dmiptex);
1033 pixels = freepixels = W_GetTexture(tx->name);
1036 tx->width = image_width;
1037 tx->height = image_height;
1038 tx->skin.base = tx->skin.merged = R_LoadTexture2D(loadmodel->texturepool, tx->name, image_width, image_height, pixels, TEXTYPE_RGBA, TEXF_MIPMAP | TEXF_ALPHA | TEXF_PRECACHE, NULL);
1039 if (Image_CheckAlpha(pixels, image_width * image_height, true))
1041 fogpixels = Mem_Alloc(tempmempool, image_width * image_height * 4);
1042 for (j = 0;j < image_width * image_height * 4;j += 4)
1044 fogpixels[j + 0] = 255;
1045 fogpixels[j + 1] = 255;
1046 fogpixels[j + 2] = 255;
1047 fogpixels[j + 3] = pixels[j + 3];
1049 tx->skin.fog = R_LoadTexture2D(loadmodel->texturepool, tx->name, image_width, image_height, pixels, TEXTYPE_RGBA, TEXF_MIPMAP | TEXF_ALPHA | TEXF_PRECACHE, NULL);
1050 Mem_Free(fogpixels);
1054 Mem_Free(freepixels);
1056 else if (mtdata) // texture included
1057 Mod_LoadSkinFrame_Internal(&tx->skin, tx->name, TEXF_MIPMAP | TEXF_PRECACHE, false, true, tx->name[0] != '*' && r_fullbrights.integer, mtdata, tx->width, tx->height);
1060 if (tx->skin.base == NULL)
1065 tx->skin.base = r_notexture;
1068 if (tx->name[0] == '*')
1070 // turb does not block movement
1071 tx->flags &= ~SURF_SOLIDCLIP;
1072 tx->flags |= SURF_DRAWTURB | SURF_LIGHTBOTHSIDES;
1073 // LordHavoc: some turbulent textures should be fullbright and solid
1074 if (!strncmp(tx->name,"*lava",5)
1075 || !strncmp(tx->name,"*teleport",9)
1076 || !strncmp(tx->name,"*rift",5)) // Scourge of Armagon texture
1077 tx->flags |= SURF_DRAWFULLBRIGHT | SURF_DRAWNOALPHA;
1079 tx->flags |= SURF_WATERALPHA;
1080 tx->shader = &Cshader_water;
1082 else if (tx->name[0] == 's' && tx->name[1] == 'k' && tx->name[2] == 'y')
1084 tx->flags |= SURF_DRAWSKY;
1085 tx->shader = &Cshader_sky;
1089 tx->flags |= SURF_LIGHTMAP;
1091 tx->flags |= SURF_SHADOWCAST | SURF_SHADOWLIGHT;
1092 tx->shader = &Cshader_wall_lightmap;
1095 // start out with no animation
1096 tx->currentframe = tx;
1099 // sequence the animations
1100 for (i = 0;i < m->nummiptex;i++)
1102 tx = loadmodel->brushq1.textures + i;
1103 if (!tx || tx->name[0] != '+' || tx->name[1] == 0 || tx->name[2] == 0)
1105 if (tx->anim_total[0] || tx->anim_total[1])
1106 continue; // already sequenced
1108 // find the number of frames in the animation
1109 memset(anims, 0, sizeof(anims));
1110 memset(altanims, 0, sizeof(altanims));
1112 for (j = i;j < m->nummiptex;j++)
1114 tx2 = loadmodel->brushq1.textures + j;
1115 if (!tx2 || tx2->name[0] != '+' || strcmp(tx2->name+2, tx->name+2))
1119 if (num >= '0' && num <= '9')
1120 anims[num - '0'] = tx2;
1121 else if (num >= 'a' && num <= 'j')
1122 altanims[num - 'a'] = tx2;
1124 Con_Printf("Bad animating texture %s\n", tx->name);
1128 for (j = 0;j < 10;j++)
1135 //Con_Printf("linking animation %s (%i:%i frames)\n\n", tx->name, max, altmax);
1138 for (j = 0;j < max;j++)
1142 Con_Printf("Missing frame %i of %s\n", j, tx->name);
1146 for (j = 0;j < altmax;j++)
1150 Con_Printf("Missing altframe %i of %s\n", j, tx->name);
1159 // if there is no alternate animation, duplicate the primary
1160 // animation into the alternate
1162 for (k = 0;k < 10;k++)
1163 altanims[k] = anims[k];
1166 // link together the primary animation
1167 for (j = 0;j < max;j++)
1170 tx2->animated = true;
1171 tx2->anim_total[0] = max;
1172 tx2->anim_total[1] = altmax;
1173 for (k = 0;k < 10;k++)
1175 tx2->anim_frames[0][k] = anims[k];
1176 tx2->anim_frames[1][k] = altanims[k];
1180 // if there really is an alternate anim...
1181 if (anims[0] != altanims[0])
1183 // link together the alternate animation
1184 for (j = 0;j < altmax;j++)
1187 tx2->animated = true;
1188 // the primary/alternate are reversed here
1189 tx2->anim_total[0] = altmax;
1190 tx2->anim_total[1] = max;
1191 for (k = 0;k < 10;k++)
1193 tx2->anim_frames[0][k] = altanims[k];
1194 tx2->anim_frames[1][k] = anims[k];
1201 static void Mod_Q1BSP_LoadLighting(lump_t *l)
1204 qbyte *in, *out, *data, d;
1205 char litfilename[1024];
1206 loadmodel->brushq1.lightdata = NULL;
1207 if (loadmodel->brush.ishlbsp) // LordHavoc: load the colored lighting data straight
1209 loadmodel->brushq1.lightdata = Mem_Alloc(loadmodel->mempool, l->filelen);
1210 memcpy(loadmodel->brushq1.lightdata, mod_base + l->fileofs, l->filelen);
1212 else // LordHavoc: bsp version 29 (normal white lighting)
1214 // LordHavoc: hope is not lost yet, check for a .lit file to load
1215 strlcpy (litfilename, loadmodel->name, sizeof (litfilename));
1216 FS_StripExtension (litfilename, litfilename, sizeof (litfilename));
1217 strlcat (litfilename, ".lit", sizeof (litfilename));
1218 data = (qbyte*) FS_LoadFile(litfilename, false);
1221 if (fs_filesize > 8 && data[0] == 'Q' && data[1] == 'L' && data[2] == 'I' && data[3] == 'T')
1223 i = LittleLong(((int *)data)[1]);
1226 Con_DPrintf("loaded %s\n", litfilename);
1227 loadmodel->brushq1.lightdata = Mem_Alloc(loadmodel->mempool, fs_filesize - 8);
1228 memcpy(loadmodel->brushq1.lightdata, data + 8, fs_filesize - 8);
1234 Con_Printf("Unknown .lit file version (%d)\n", i);
1240 if (fs_filesize == 8)
1241 Con_Printf("Empty .lit file, ignoring\n");
1243 Con_Printf("Corrupt .lit file (old version?), ignoring\n");
1247 // LordHavoc: oh well, expand the white lighting data
1250 loadmodel->brushq1.lightdata = Mem_Alloc(loadmodel->mempool, l->filelen*3);
1251 in = loadmodel->brushq1.lightdata + l->filelen*2; // place the file at the end, so it will not be overwritten until the very last write
1252 out = loadmodel->brushq1.lightdata;
1253 memcpy(in, mod_base + l->fileofs, l->filelen);
1254 for (i = 0;i < l->filelen;i++)
1264 static void Mod_Q1BSP_LoadLightList(void)
1266 int a, n, numlights;
1267 char lightsfilename[1024], *s, *t, *lightsstring;
1270 strlcpy (lightsfilename, loadmodel->name, sizeof (lightsfilename));
1271 FS_StripExtension (lightsfilename, lightsfilename, sizeof(lightsfilename));
1272 strlcat (lightsfilename, ".lights", sizeof (lightsfilename));
1273 s = lightsstring = (char *) FS_LoadFile(lightsfilename, false);
1279 while (*s && *s != '\n')
1283 Mem_Free(lightsstring);
1284 Host_Error("lights file must end with a newline\n");
1289 loadmodel->brushq1.lights = Mem_Alloc(loadmodel->mempool, numlights * sizeof(mlight_t));
1292 while (*s && n < numlights)
1295 while (*s && *s != '\n')
1299 Mem_Free(lightsstring);
1300 Host_Error("misparsed lights file!\n");
1302 e = loadmodel->brushq1.lights + n;
1304 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);
1308 Mem_Free(lightsstring);
1309 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);
1316 Mem_Free(lightsstring);
1317 Host_Error("misparsed lights file!\n");
1319 loadmodel->brushq1.numlights = numlights;
1320 Mem_Free(lightsstring);
1324 static void Mod_Q1BSP_LoadVisibility(lump_t *l)
1326 loadmodel->brushq1.num_compressedpvs = 0;
1327 loadmodel->brushq1.data_compressedpvs = NULL;
1330 loadmodel->brushq1.num_compressedpvs = l->filelen;
1331 loadmodel->brushq1.data_compressedpvs = Mem_Alloc(loadmodel->mempool, l->filelen);
1332 memcpy(loadmodel->brushq1.data_compressedpvs, mod_base + l->fileofs, l->filelen);
1335 // used only for HalfLife maps
1336 static void Mod_Q1BSP_ParseWadsFromEntityLump(const char *data)
1338 char key[128], value[4096];
1343 if (!COM_ParseToken(&data, false))
1345 if (com_token[0] != '{')
1349 if (!COM_ParseToken(&data, false))
1351 if (com_token[0] == '}')
1352 break; // end of worldspawn
1353 if (com_token[0] == '_')
1354 strcpy(key, com_token + 1);
1356 strcpy(key, com_token);
1357 while (key[strlen(key)-1] == ' ') // remove trailing spaces
1358 key[strlen(key)-1] = 0;
1359 if (!COM_ParseToken(&data, false))
1361 strcpy(value, com_token);
1362 if (!strcmp("wad", key)) // for HalfLife maps
1364 if (loadmodel->brush.ishlbsp)
1367 for (i = 0;i < 4096;i++)
1368 if (value[i] != ';' && value[i] != '\\' && value[i] != '/' && value[i] != ':')
1374 // ignore path - the \\ check is for HalfLife... stupid windoze 'programmers'...
1375 if (value[i] == '\\' || value[i] == '/' || value[i] == ':')
1377 else if (value[i] == ';' || value[i] == 0)
1381 strcpy(wadname, "textures/");
1382 strcat(wadname, &value[j]);
1383 W_LoadTextureWadFile(wadname, false);
1395 static void Mod_Q1BSP_LoadEntities(lump_t *l)
1397 loadmodel->brush.entities = NULL;
1400 loadmodel->brush.entities = Mem_Alloc(loadmodel->mempool, l->filelen);
1401 memcpy(loadmodel->brush.entities, mod_base + l->fileofs, l->filelen);
1402 if (loadmodel->brush.ishlbsp)
1403 Mod_Q1BSP_ParseWadsFromEntityLump(loadmodel->brush.entities);
1407 static void Mod_Q1BSP_LoadVertexes(lump_t *l)
1413 in = (void *)(mod_base + l->fileofs);
1414 if (l->filelen % sizeof(*in))
1415 Host_Error("Mod_Q1BSP_LoadVertexes: funny lump size in %s",loadmodel->name);
1416 count = l->filelen / sizeof(*in);
1417 out = Mem_Alloc(loadmodel->mempool, count*sizeof(*out));
1419 loadmodel->brushq1.vertexes = out;
1420 loadmodel->brushq1.numvertexes = count;
1422 for ( i=0 ; i<count ; i++, in++, out++)
1424 out->position[0] = LittleFloat(in->point[0]);
1425 out->position[1] = LittleFloat(in->point[1]);
1426 out->position[2] = LittleFloat(in->point[2]);
1430 static void Mod_Q1BSP_LoadSubmodels(lump_t *l)
1436 in = (void *)(mod_base + l->fileofs);
1437 if (l->filelen % sizeof(*in))
1438 Host_Error("Mod_Q1BSP_LoadSubmodels: funny lump size in %s",loadmodel->name);
1439 count = l->filelen / sizeof(*in);
1440 out = Mem_Alloc(loadmodel->mempool, count*sizeof(*out));
1442 loadmodel->brushq1.submodels = out;
1443 loadmodel->brush.numsubmodels = count;
1445 for ( i=0 ; i<count ; i++, in++, out++)
1447 for (j=0 ; j<3 ; j++)
1449 // spread the mins / maxs by a pixel
1450 out->mins[j] = LittleFloat(in->mins[j]) - 1;
1451 out->maxs[j] = LittleFloat(in->maxs[j]) + 1;
1452 out->origin[j] = LittleFloat(in->origin[j]);
1454 for (j=0 ; j<MAX_MAP_HULLS ; j++)
1455 out->headnode[j] = LittleLong(in->headnode[j]);
1456 out->visleafs = LittleLong(in->visleafs);
1457 out->firstface = LittleLong(in->firstface);
1458 out->numfaces = LittleLong(in->numfaces);
1462 static void Mod_Q1BSP_LoadEdges(lump_t *l)
1468 in = (void *)(mod_base + l->fileofs);
1469 if (l->filelen % sizeof(*in))
1470 Host_Error("Mod_Q1BSP_LoadEdges: funny lump size in %s",loadmodel->name);
1471 count = l->filelen / sizeof(*in);
1472 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
1474 loadmodel->brushq1.edges = out;
1475 loadmodel->brushq1.numedges = count;
1477 for ( i=0 ; i<count ; i++, in++, out++)
1479 out->v[0] = (unsigned short)LittleShort(in->v[0]);
1480 out->v[1] = (unsigned short)LittleShort(in->v[1]);
1484 static void Mod_Q1BSP_LoadTexinfo(lump_t *l)
1488 int i, j, k, count, miptex;
1490 in = (void *)(mod_base + l->fileofs);
1491 if (l->filelen % sizeof(*in))
1492 Host_Error("Mod_Q1BSP_LoadTexinfo: funny lump size in %s",loadmodel->name);
1493 count = l->filelen / sizeof(*in);
1494 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
1496 loadmodel->brushq1.texinfo = out;
1497 loadmodel->brushq1.numtexinfo = count;
1499 for (i = 0;i < count;i++, in++, out++)
1501 for (k = 0;k < 2;k++)
1502 for (j = 0;j < 4;j++)
1503 out->vecs[k][j] = LittleFloat(in->vecs[k][j]);
1505 miptex = LittleLong(in->miptex);
1506 out->flags = LittleLong(in->flags);
1508 out->texture = NULL;
1509 if (loadmodel->brushq1.textures)
1511 if ((unsigned int) miptex >= (unsigned int) loadmodel->brushq1.numtextures)
1512 Con_Printf("error in model \"%s\": invalid miptex index %i(of %i)\n", loadmodel->name, miptex, loadmodel->brushq1.numtextures);
1514 out->texture = loadmodel->brushq1.textures + miptex;
1516 if (out->flags & TEX_SPECIAL)
1518 // if texture chosen is NULL or the shader needs a lightmap,
1519 // force to notexture water shader
1520 if (out->texture == NULL || out->texture->shader->flags & SHADERFLAGS_NEEDLIGHTMAP)
1521 out->texture = loadmodel->brushq1.textures + (loadmodel->brushq1.numtextures - 1);
1525 // if texture chosen is NULL, force to notexture
1526 if (out->texture == NULL)
1527 out->texture = loadmodel->brushq1.textures + (loadmodel->brushq1.numtextures - 2);
1533 void BoundPoly(int numverts, float *verts, vec3_t mins, vec3_t maxs)
1538 mins[0] = mins[1] = mins[2] = 9999;
1539 maxs[0] = maxs[1] = maxs[2] = -9999;
1541 for (i = 0;i < numverts;i++)
1543 for (j = 0;j < 3;j++, v++)
1553 #define MAX_SUBDIVPOLYTRIANGLES 4096
1554 #define MAX_SUBDIVPOLYVERTS(MAX_SUBDIVPOLYTRIANGLES * 3)
1556 static int subdivpolyverts, subdivpolytriangles;
1557 static int subdivpolyindex[MAX_SUBDIVPOLYTRIANGLES][3];
1558 static float subdivpolyvert[MAX_SUBDIVPOLYVERTS][3];
1560 static int subdivpolylookupvert(vec3_t v)
1563 for (i = 0;i < subdivpolyverts;i++)
1564 if (subdivpolyvert[i][0] == v[0]
1565 && subdivpolyvert[i][1] == v[1]
1566 && subdivpolyvert[i][2] == v[2])
1568 if (subdivpolyverts >= MAX_SUBDIVPOLYVERTS)
1569 Host_Error("SubDividePolygon: ran out of vertices in buffer, please increase your r_subdivide_size");
1570 VectorCopy(v, subdivpolyvert[subdivpolyverts]);
1571 return subdivpolyverts++;
1574 static void SubdividePolygon(int numverts, float *verts)
1576 int i, i1, i2, i3, f, b, c, p;
1577 vec3_t mins, maxs, front[256], back[256];
1578 float m, *pv, *cv, dist[256], frac;
1581 Host_Error("SubdividePolygon: ran out of verts in buffer");
1583 BoundPoly(numverts, verts, mins, maxs);
1585 for (i = 0;i < 3;i++)
1587 m = (mins[i] + maxs[i]) * 0.5;
1588 m = r_subdivide_size.value * floor(m/r_subdivide_size.value + 0.5);
1589 if (maxs[i] - m < 8)
1591 if (m - mins[i] < 8)
1595 for (cv = verts, c = 0;c < numverts;c++, cv += 3)
1596 dist[c] = cv[i] - m;
1599 for (p = numverts - 1, c = 0, pv = verts + p * 3, cv = verts;c < numverts;p = c, c++, pv = cv, cv += 3)
1603 VectorCopy(pv, front[f]);
1608 VectorCopy(pv, back[b]);
1611 if (dist[p] == 0 || dist[c] == 0)
1613 if ((dist[p] > 0) != (dist[c] > 0) )
1616 frac = dist[p] / (dist[p] - dist[c]);
1617 front[f][0] = back[b][0] = pv[0] + frac * (cv[0] - pv[0]);
1618 front[f][1] = back[b][1] = pv[1] + frac * (cv[1] - pv[1]);
1619 front[f][2] = back[b][2] = pv[2] + frac * (cv[2] - pv[2]);
1625 SubdividePolygon(f, front[0]);
1626 SubdividePolygon(b, back[0]);
1630 i1 = subdivpolylookupvert(verts);
1631 i2 = subdivpolylookupvert(verts + 3);
1632 for (i = 2;i < numverts;i++)
1634 if (subdivpolytriangles >= MAX_SUBDIVPOLYTRIANGLES)
1636 Con_Printf("SubdividePolygon: ran out of triangles in buffer, please increase your r_subdivide_size\n");
1640 i3 = subdivpolylookupvert(verts + i * 3);
1641 subdivpolyindex[subdivpolytriangles][0] = i1;
1642 subdivpolyindex[subdivpolytriangles][1] = i2;
1643 subdivpolyindex[subdivpolytriangles][2] = i3;
1645 subdivpolytriangles++;
1649 //Breaks a polygon up along axial 64 unit
1650 //boundaries so that turbulent and sky warps
1651 //can be done reasonably.
1652 static void Mod_Q1BSP_GenerateWarpMesh(msurface_t *surf)
1658 subdivpolytriangles = 0;
1659 subdivpolyverts = 0;
1660 SubdividePolygon(surf->poly_numverts, surf->poly_verts);
1661 if (subdivpolytriangles < 1)
1662 Host_Error("Mod_Q1BSP_GenerateWarpMesh: no triangles?\n");
1664 surf->mesh = mesh = Mem_Alloc(loadmodel->mempool, sizeof(surfmesh_t) + subdivpolytriangles * sizeof(int[3]) + subdivpolyverts * sizeof(surfvertex_t));
1665 mesh->num_vertices = subdivpolyverts;
1666 mesh->num_triangles = subdivpolytriangles;
1667 mesh->vertex = (surfvertex_t *)(mesh + 1);
1668 mesh->index = (int *)(mesh->vertex + mesh->num_vertices);
1669 memset(mesh->vertex, 0, mesh->num_vertices * sizeof(surfvertex_t));
1671 for (i = 0;i < mesh->num_triangles;i++)
1672 for (j = 0;j < 3;j++)
1673 mesh->index[i*3+j] = subdivpolyindex[i][j];
1675 for (i = 0, v = mesh->vertex;i < subdivpolyverts;i++, v++)
1677 VectorCopy(subdivpolyvert[i], v->v);
1678 v->st[0] = DotProduct(v->v, surf->texinfo->vecs[0]);
1679 v->st[1] = DotProduct(v->v, surf->texinfo->vecs[1]);
1684 static surfmesh_t *Mod_Q1BSP_AllocSurfMesh(int numverts, int numtriangles)
1687 mesh = Mem_Alloc(loadmodel->mempool, sizeof(surfmesh_t) + numtriangles * sizeof(int[6]) + numverts * (3 + 2 + 2 + 2 + 3 + 3 + 3 + 1) * sizeof(float));
1688 mesh->num_vertices = numverts;
1689 mesh->num_triangles = numtriangles;
1690 mesh->data_vertex3f = (float *)(mesh + 1);
1691 mesh->data_texcoordtexture2f = mesh->data_vertex3f + mesh->num_vertices * 3;
1692 mesh->data_texcoordlightmap2f = mesh->data_texcoordtexture2f + mesh->num_vertices * 2;
1693 mesh->data_texcoorddetail2f = mesh->data_texcoordlightmap2f + mesh->num_vertices * 2;
1694 mesh->data_svector3f = (float *)(mesh->data_texcoorddetail2f + mesh->num_vertices * 2);
1695 mesh->data_tvector3f = mesh->data_svector3f + mesh->num_vertices * 3;
1696 mesh->data_normal3f = mesh->data_tvector3f + mesh->num_vertices * 3;
1697 mesh->data_lightmapoffsets = (int *)(mesh->data_normal3f + mesh->num_vertices * 3);
1698 mesh->data_element3i = mesh->data_lightmapoffsets + mesh->num_vertices;
1699 mesh->data_neighbor3i = mesh->data_element3i + mesh->num_triangles * 3;
1703 static void Mod_Q1BSP_GenerateSurfacePolygon(msurface_t *surf, int firstedge, int numedges)
1706 float *vec, *vert, mins[3], maxs[3], val, *v;
1709 // convert edges back to a normal polygon
1710 surf->poly_numverts = numedges;
1711 vert = surf->poly_verts = Mem_Alloc(loadmodel->mempool, sizeof(float[3]) * numedges);
1712 for (i = 0;i < numedges;i++)
1714 lindex = loadmodel->brushq1.surfedges[firstedge + i];
1716 vec = loadmodel->brushq1.vertexes[loadmodel->brushq1.edges[lindex].v[0]].position;
1718 vec = loadmodel->brushq1.vertexes[loadmodel->brushq1.edges[-lindex].v[1]].position;
1719 VectorCopy(vec, vert);
1723 // calculate polygon bounding box and center
1724 vert = surf->poly_verts;
1725 VectorCopy(vert, mins);
1726 VectorCopy(vert, maxs);
1728 for (i = 1;i < surf->poly_numverts;i++, vert += 3)
1730 if (mins[0] > vert[0]) mins[0] = vert[0];if (maxs[0] < vert[0]) maxs[0] = vert[0];
1731 if (mins[1] > vert[1]) mins[1] = vert[1];if (maxs[1] < vert[1]) maxs[1] = vert[1];
1732 if (mins[2] > vert[2]) mins[2] = vert[2];if (maxs[2] < vert[2]) maxs[2] = vert[2];
1734 VectorCopy(mins, surf->poly_mins);
1735 VectorCopy(maxs, surf->poly_maxs);
1736 surf->poly_center[0] = (mins[0] + maxs[0]) * 0.5f;
1737 surf->poly_center[1] = (mins[1] + maxs[1]) * 0.5f;
1738 surf->poly_center[2] = (mins[2] + maxs[2]) * 0.5f;
1740 // generate surface extents information
1741 tex = surf->texinfo;
1742 mins[0] = maxs[0] = DotProduct(surf->poly_verts, tex->vecs[0]) + tex->vecs[0][3];
1743 mins[1] = maxs[1] = DotProduct(surf->poly_verts, tex->vecs[1]) + tex->vecs[1][3];
1744 for (i = 1, v = surf->poly_verts + 3;i < surf->poly_numverts;i++, v += 3)
1746 for (j = 0;j < 2;j++)
1748 val = DotProduct(v, tex->vecs[j]) + tex->vecs[j][3];
1755 for (i = 0;i < 2;i++)
1757 surf->texturemins[i] = (int) floor(mins[i] / 16) * 16;
1758 surf->extents[i] = (int) ceil(maxs[i] / 16) * 16 - surf->texturemins[i];
1762 static void Mod_Q1BSP_LoadFaces(lump_t *l)
1766 int i, count, surfnum, planenum, ssize, tsize, firstedge, numedges, totalverts, totaltris, totalmeshes;
1770 in = (void *)(mod_base + l->fileofs);
1771 if (l->filelen % sizeof(*in))
1772 Host_Error("Mod_Q1BSP_LoadFaces: funny lump size in %s",loadmodel->name);
1773 count = l->filelen / sizeof(*in);
1774 loadmodel->brushq1.surfaces = Mem_Alloc(loadmodel->mempool, count*sizeof(msurface_t));
1776 loadmodel->brushq1.numsurfaces = count;
1777 loadmodel->brushq1.surfacevisframes = Mem_Alloc(loadmodel->mempool, count * sizeof(int));
1778 loadmodel->brushq1.surfacepvsframes = Mem_Alloc(loadmodel->mempool, count * sizeof(int));
1779 loadmodel->brushq1.pvssurflist = Mem_Alloc(loadmodel->mempool, count * sizeof(int));
1781 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++)
1783 surf->number = surfnum;
1784 // FIXME: validate edges, texinfo, etc?
1785 firstedge = LittleLong(in->firstedge);
1786 numedges = LittleShort(in->numedges);
1787 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)
1788 Host_Error("Mod_Q1BSP_LoadFaces: invalid edge range (firstedge %i, numedges %i, model edges %i)\n", firstedge, numedges, loadmodel->brushq1.numsurfedges);
1789 i = LittleShort(in->texinfo);
1790 if ((unsigned int) i >= (unsigned int) loadmodel->brushq1.numtexinfo)
1791 Host_Error("Mod_Q1BSP_LoadFaces: invalid texinfo index %i(model has %i texinfos)\n", i, loadmodel->brushq1.numtexinfo);
1792 surf->texinfo = loadmodel->brushq1.texinfo + i;
1793 surf->flags = surf->texinfo->texture->flags;
1795 planenum = LittleShort(in->planenum);
1796 if ((unsigned int) planenum >= (unsigned int) loadmodel->brushq1.numplanes)
1797 Host_Error("Mod_Q1BSP_LoadFaces: invalid plane index %i (model has %i planes)\n", planenum, loadmodel->brushq1.numplanes);
1799 if (LittleShort(in->side))
1800 surf->flags |= SURF_PLANEBACK;
1802 surf->plane = loadmodel->brushq1.planes + planenum;
1804 // clear lightmap (filled in later)
1805 surf->lightmaptexture = NULL;
1807 // force lightmap upload on first time seeing the surface
1808 surf->cached_dlight = true;
1810 Mod_Q1BSP_GenerateSurfacePolygon(surf, firstedge, numedges);
1812 ssize = (surf->extents[0] >> 4) + 1;
1813 tsize = (surf->extents[1] >> 4) + 1;
1816 for (i = 0;i < MAXLIGHTMAPS;i++)
1817 surf->styles[i] = in->styles[i];
1818 i = LittleLong(in->lightofs);
1820 surf->samples = NULL;
1821 else if (loadmodel->brush.ishlbsp) // LordHavoc: HalfLife map (bsp version 30)
1822 surf->samples = loadmodel->brushq1.lightdata + i;
1823 else // LordHavoc: white lighting (bsp version 29)
1824 surf->samples = loadmodel->brushq1.lightdata + (i * 3);
1826 if (surf->texinfo->texture->shader == &Cshader_wall_lightmap)
1828 if ((surf->extents[0] >> 4) + 1 > (256) || (surf->extents[1] >> 4) + 1 > (256))
1829 Host_Error("Bad surface extents");
1830 // stainmap for permanent marks on walls
1831 surf->stainsamples = Mem_Alloc(loadmodel->mempool, ssize * tsize * 3);
1833 memset(surf->stainsamples, 255, ssize * tsize * 3);
1837 loadmodel->brushq1.entiremesh = Mod_Q1BSP_AllocSurfMesh(totalverts, totaltris);
1839 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++)
1842 mesh->num_vertices = surf->poly_numverts;
1843 mesh->num_triangles = surf->poly_numverts - 2;
1844 mesh->data_vertex3f = loadmodel->brushq1.entiremesh->data_vertex3f + totalverts * 3;
1845 mesh->data_texcoordtexture2f = loadmodel->brushq1.entiremesh->data_texcoordtexture2f + totalverts * 2;
1846 mesh->data_texcoordlightmap2f = loadmodel->brushq1.entiremesh->data_texcoordlightmap2f + totalverts * 2;
1847 mesh->data_texcoorddetail2f = loadmodel->brushq1.entiremesh->data_texcoorddetail2f + totalverts * 2;
1848 mesh->data_svector3f = loadmodel->brushq1.entiremesh->data_svector3f + totalverts * 3;
1849 mesh->data_tvector3f = loadmodel->brushq1.entiremesh->data_tvector3f + totalverts * 3;
1850 mesh->data_normal3f = loadmodel->brushq1.entiremesh->data_normal3f + totalverts * 3;
1851 mesh->data_lightmapoffsets = loadmodel->brushq1.entiremesh->data_lightmapoffsets + totalverts;
1852 mesh->data_element3i = loadmodel->brushq1.entiremesh->data_element3i + totaltris * 3;
1853 mesh->data_neighbor3i = loadmodel->brushq1.entiremesh->data_neighbor3i + totaltris * 3;
1855 surf->lightmaptexturestride = 0;
1856 surf->lightmaptexture = NULL;
1858 for (i = 0;i < mesh->num_vertices;i++)
1860 mesh->data_vertex3f[i * 3 + 0] = surf->poly_verts[i * 3 + 0];
1861 mesh->data_vertex3f[i * 3 + 1] = surf->poly_verts[i * 3 + 1];
1862 mesh->data_vertex3f[i * 3 + 2] = surf->poly_verts[i * 3 + 2];
1863 s = DotProduct((mesh->data_vertex3f + i * 3), surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3];
1864 t = DotProduct((mesh->data_vertex3f + i * 3), surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3];
1865 mesh->data_texcoordtexture2f[i * 2 + 0] = s / surf->texinfo->texture->width;
1866 mesh->data_texcoordtexture2f[i * 2 + 1] = t / surf->texinfo->texture->height;
1867 mesh->data_texcoorddetail2f[i * 2 + 0] = s * (1.0f / 16.0f);
1868 mesh->data_texcoorddetail2f[i * 2 + 1] = t * (1.0f / 16.0f);
1869 mesh->data_texcoordlightmap2f[i * 2 + 0] = 0;
1870 mesh->data_texcoordlightmap2f[i * 2 + 1] = 0;
1871 mesh->data_lightmapoffsets[i] = 0;
1874 for (i = 0;i < mesh->num_triangles;i++)
1876 mesh->data_element3i[i * 3 + 0] = 0;
1877 mesh->data_element3i[i * 3 + 1] = i + 1;
1878 mesh->data_element3i[i * 3 + 2] = i + 2;
1881 Mod_BuildTriangleNeighbors(mesh->data_neighbor3i, mesh->data_element3i, mesh->num_triangles);
1882 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);
1884 if (surf->texinfo->texture->shader == &Cshader_wall_lightmap)
1886 int i, iu, iv, smax, tmax;
1887 float u, v, ubase, vbase, uscale, vscale;
1889 smax = surf->extents[0] >> 4;
1890 tmax = surf->extents[1] >> 4;
1892 surf->flags |= SURF_LIGHTMAP;
1893 if (r_miplightmaps.integer)
1895 surf->lightmaptexturestride = smax+1;
1896 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);
1900 surf->lightmaptexturestride = R_CompatibleFragmentWidth(smax+1, loadmodel->brushq1.lightmaprgba ? TEXTYPE_RGBA : TEXTYPE_RGB, 0);
1901 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);
1903 R_FragmentLocation(surf->lightmaptexture, NULL, NULL, &ubase, &vbase, &uscale, &vscale);
1904 uscale = (uscale - ubase) / (smax + 1);
1905 vscale = (vscale - vbase) / (tmax + 1);
1907 for (i = 0;i < mesh->num_vertices;i++)
1909 u = ((DotProduct((mesh->data_vertex3f + i * 3), surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3]) + 8 - surf->texturemins[0]) * (1.0 / 16.0);
1910 v = ((DotProduct((mesh->data_vertex3f + i * 3), surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3]) + 8 - surf->texturemins[1]) * (1.0 / 16.0);
1911 mesh->data_texcoordlightmap2f[i * 2 + 0] = u * uscale + ubase;
1912 mesh->data_texcoordlightmap2f[i * 2 + 1] = v * vscale + vbase;
1913 // LordHavoc: calc lightmap data offset for vertex lighting to use
1916 mesh->data_lightmapoffsets[i] = (bound(0, iv, tmax) * (smax+1) + bound(0, iu, smax)) * 3;
1922 static void Mod_Q1BSP_SetParent(mnode_t *node, mnode_t *parent)
1924 node->parent = parent;
1925 if (node->contents < 0)
1927 Mod_Q1BSP_SetParent(node->children[0], node);
1928 Mod_Q1BSP_SetParent(node->children[1], node);
1931 static void Mod_Q1BSP_LoadNodes(lump_t *l)
1937 in = (void *)(mod_base + l->fileofs);
1938 if (l->filelen % sizeof(*in))
1939 Host_Error("Mod_Q1BSP_LoadNodes: funny lump size in %s",loadmodel->name);
1940 count = l->filelen / sizeof(*in);
1941 out = Mem_Alloc(loadmodel->mempool, count*sizeof(*out));
1943 loadmodel->brushq1.nodes = out;
1944 loadmodel->brushq1.numnodes = count;
1946 for ( i=0 ; i<count ; i++, in++, out++)
1948 for (j=0 ; j<3 ; j++)
1950 out->mins[j] = LittleShort(in->mins[j]);
1951 out->maxs[j] = LittleShort(in->maxs[j]);
1954 p = LittleLong(in->planenum);
1955 out->plane = loadmodel->brushq1.planes + p;
1957 out->firstsurface = LittleShort(in->firstface);
1958 out->numsurfaces = LittleShort(in->numfaces);
1960 for (j=0 ; j<2 ; j++)
1962 p = LittleShort(in->children[j]);
1964 out->children[j] = loadmodel->brushq1.nodes + p;
1966 out->children[j] = (mnode_t *)(loadmodel->brushq1.data_leafs + (-1 - p));
1970 Mod_Q1BSP_SetParent(loadmodel->brushq1.nodes, NULL); // sets nodes and leafs
1973 static void Mod_Q1BSP_LoadLeafs(lump_t *l)
1979 in = (void *)(mod_base + l->fileofs);
1980 if (l->filelen % sizeof(*in))
1981 Host_Error("Mod_Q1BSP_LoadLeafs: funny lump size in %s",loadmodel->name);
1982 count = l->filelen / sizeof(*in);
1983 out = Mem_Alloc(loadmodel->mempool, count*sizeof(*out));
1985 loadmodel->brushq1.data_leafs = out;
1986 loadmodel->brushq1.num_leafs = count;
1987 // get visleafs from the submodel data
1988 loadmodel->brush.num_pvsclusters = loadmodel->brushq1.submodels[0].visleafs;
1989 loadmodel->brush.num_pvsclusterbytes = (loadmodel->brush.num_pvsclusters+7)>>3;
1990 loadmodel->brush.data_pvsclusters = Mem_Alloc(loadmodel->mempool, loadmodel->brush.num_pvsclusters * loadmodel->brush.num_pvsclusterbytes);
1991 memset(loadmodel->brush.data_pvsclusters, 0xFF, loadmodel->brush.num_pvsclusters * loadmodel->brush.num_pvsclusterbytes);
1993 for ( i=0 ; i<count ; i++, in++, out++)
1995 for (j=0 ; j<3 ; j++)
1997 out->mins[j] = LittleShort(in->mins[j]);
1998 out->maxs[j] = LittleShort(in->maxs[j]);
2001 // FIXME: this function could really benefit from some error checking
2003 out->contents = LittleLong(in->contents);
2005 out->firstmarksurface = loadmodel->brushq1.marksurfaces + LittleShort(in->firstmarksurface);
2006 out->nummarksurfaces = LittleShort(in->nummarksurfaces);
2007 if (out->firstmarksurface < 0 || LittleShort(in->firstmarksurface) + out->nummarksurfaces > loadmodel->brushq1.nummarksurfaces)
2009 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);
2010 out->firstmarksurface = NULL;
2011 out->nummarksurfaces = 0;
2014 out->clusterindex = i - 1;
2015 if (out->clusterindex >= loadmodel->brush.num_pvsclusters)
2016 out->clusterindex = -1;
2018 p = LittleLong(in->visofs);
2019 // ignore visofs errors on leaf 0 (solid)
2020 if (p >= 0 && out->clusterindex >= 0)
2022 if (p >= loadmodel->brushq1.num_compressedpvs)
2023 Con_Printf("Mod_Q1BSP_LoadLeafs: invalid visofs\n");
2025 Mod_Q1BSP_DecompressVis(loadmodel->brushq1.data_compressedpvs + p, loadmodel->brushq1.data_compressedpvs + loadmodel->brushq1.num_compressedpvs, loadmodel->brush.data_pvsclusters + out->clusterindex * loadmodel->brush.num_pvsclusterbytes, loadmodel->brush.data_pvsclusters + (out->clusterindex + 1) * loadmodel->brush.num_pvsclusterbytes);
2028 for (j = 0;j < 4;j++)
2029 out->ambient_sound_level[j] = in->ambient_level[j];
2031 // FIXME: Insert caustics here
2035 static void Mod_Q1BSP_LoadClipnodes(lump_t *l)
2037 dclipnode_t *in, *out;
2041 in = (void *)(mod_base + l->fileofs);
2042 if (l->filelen % sizeof(*in))
2043 Host_Error("Mod_Q1BSP_LoadClipnodes: funny lump size in %s",loadmodel->name);
2044 count = l->filelen / sizeof(*in);
2045 out = Mem_Alloc(loadmodel->mempool, count*sizeof(*out));
2047 loadmodel->brushq1.clipnodes = out;
2048 loadmodel->brushq1.numclipnodes = count;
2050 if (loadmodel->brush.ishlbsp)
2052 hull = &loadmodel->brushq1.hulls[1];
2053 hull->clipnodes = out;
2054 hull->firstclipnode = 0;
2055 hull->lastclipnode = count-1;
2056 hull->planes = loadmodel->brushq1.planes;
2057 hull->clip_mins[0] = -16;
2058 hull->clip_mins[1] = -16;
2059 hull->clip_mins[2] = -36;
2060 hull->clip_maxs[0] = 16;
2061 hull->clip_maxs[1] = 16;
2062 hull->clip_maxs[2] = 36;
2063 VectorSubtract(hull->clip_maxs, hull->clip_mins, hull->clip_size);
2065 hull = &loadmodel->brushq1.hulls[2];
2066 hull->clipnodes = out;
2067 hull->firstclipnode = 0;
2068 hull->lastclipnode = count-1;
2069 hull->planes = loadmodel->brushq1.planes;
2070 hull->clip_mins[0] = -32;
2071 hull->clip_mins[1] = -32;
2072 hull->clip_mins[2] = -32;
2073 hull->clip_maxs[0] = 32;
2074 hull->clip_maxs[1] = 32;
2075 hull->clip_maxs[2] = 32;
2076 VectorSubtract(hull->clip_maxs, hull->clip_mins, hull->clip_size);
2078 hull = &loadmodel->brushq1.hulls[3];
2079 hull->clipnodes = out;
2080 hull->firstclipnode = 0;
2081 hull->lastclipnode = count-1;
2082 hull->planes = loadmodel->brushq1.planes;
2083 hull->clip_mins[0] = -16;
2084 hull->clip_mins[1] = -16;
2085 hull->clip_mins[2] = -18;
2086 hull->clip_maxs[0] = 16;
2087 hull->clip_maxs[1] = 16;
2088 hull->clip_maxs[2] = 18;
2089 VectorSubtract(hull->clip_maxs, hull->clip_mins, hull->clip_size);
2093 hull = &loadmodel->brushq1.hulls[1];
2094 hull->clipnodes = out;
2095 hull->firstclipnode = 0;
2096 hull->lastclipnode = count-1;
2097 hull->planes = loadmodel->brushq1.planes;
2098 hull->clip_mins[0] = -16;
2099 hull->clip_mins[1] = -16;
2100 hull->clip_mins[2] = -24;
2101 hull->clip_maxs[0] = 16;
2102 hull->clip_maxs[1] = 16;
2103 hull->clip_maxs[2] = 32;
2104 VectorSubtract(hull->clip_maxs, hull->clip_mins, hull->clip_size);
2106 hull = &loadmodel->brushq1.hulls[2];
2107 hull->clipnodes = out;
2108 hull->firstclipnode = 0;
2109 hull->lastclipnode = count-1;
2110 hull->planes = loadmodel->brushq1.planes;
2111 hull->clip_mins[0] = -32;
2112 hull->clip_mins[1] = -32;
2113 hull->clip_mins[2] = -24;
2114 hull->clip_maxs[0] = 32;
2115 hull->clip_maxs[1] = 32;
2116 hull->clip_maxs[2] = 64;
2117 VectorSubtract(hull->clip_maxs, hull->clip_mins, hull->clip_size);
2120 for (i=0 ; i<count ; i++, out++, in++)
2122 out->planenum = LittleLong(in->planenum);
2123 out->children[0] = LittleShort(in->children[0]);
2124 out->children[1] = LittleShort(in->children[1]);
2125 if (out->children[0] >= count || out->children[1] >= count)
2126 Host_Error("Corrupt clipping hull(out of range child)\n");
2130 //Duplicate the drawing hull structure as a clipping hull
2131 static void Mod_Q1BSP_MakeHull0(void)
2138 hull = &loadmodel->brushq1.hulls[0];
2140 in = loadmodel->brushq1.nodes;
2141 out = Mem_Alloc(loadmodel->mempool, loadmodel->brushq1.numnodes * sizeof(dclipnode_t));
2143 hull->clipnodes = out;
2144 hull->firstclipnode = 0;
2145 hull->lastclipnode = loadmodel->brushq1.numnodes - 1;
2146 hull->planes = loadmodel->brushq1.planes;
2148 for (i = 0;i < loadmodel->brushq1.numnodes;i++, out++, in++)
2150 out->planenum = in->plane - loadmodel->brushq1.planes;
2151 out->children[0] = in->children[0]->contents < 0 ? in->children[0]->contents : in->children[0] - loadmodel->brushq1.nodes;
2152 out->children[1] = in->children[1]->contents < 0 ? in->children[1]->contents : in->children[1] - loadmodel->brushq1.nodes;
2156 static void Mod_Q1BSP_LoadMarksurfaces(lump_t *l)
2161 in = (void *)(mod_base + l->fileofs);
2162 if (l->filelen % sizeof(*in))
2163 Host_Error("Mod_Q1BSP_LoadMarksurfaces: funny lump size in %s",loadmodel->name);
2164 loadmodel->brushq1.nummarksurfaces = l->filelen / sizeof(*in);
2165 loadmodel->brushq1.marksurfaces = Mem_Alloc(loadmodel->mempool, loadmodel->brushq1.nummarksurfaces * sizeof(int));
2167 for (i = 0;i < loadmodel->brushq1.nummarksurfaces;i++)
2169 j = (unsigned) LittleShort(in[i]);
2170 if (j >= loadmodel->brushq1.numsurfaces)
2171 Host_Error("Mod_Q1BSP_LoadMarksurfaces: bad surface number");
2172 loadmodel->brushq1.marksurfaces[i] = j;
2176 static void Mod_Q1BSP_LoadSurfedges(lump_t *l)
2181 in = (void *)(mod_base + l->fileofs);
2182 if (l->filelen % sizeof(*in))
2183 Host_Error("Mod_Q1BSP_LoadSurfedges: funny lump size in %s",loadmodel->name);
2184 loadmodel->brushq1.numsurfedges = l->filelen / sizeof(*in);
2185 loadmodel->brushq1.surfedges = Mem_Alloc(loadmodel->mempool, loadmodel->brushq1.numsurfedges * sizeof(int));
2187 for (i = 0;i < loadmodel->brushq1.numsurfedges;i++)
2188 loadmodel->brushq1.surfedges[i] = LittleLong(in[i]);
2192 static void Mod_Q1BSP_LoadPlanes(lump_t *l)
2198 in = (void *)(mod_base + l->fileofs);
2199 if (l->filelen % sizeof(*in))
2200 Host_Error("Mod_Q1BSP_LoadPlanes: funny lump size in %s", loadmodel->name);
2202 loadmodel->brushq1.numplanes = l->filelen / sizeof(*in);
2203 loadmodel->brushq1.planes = out = Mem_Alloc(loadmodel->mempool, loadmodel->brushq1.numplanes * sizeof(*out));
2205 for (i = 0;i < loadmodel->brushq1.numplanes;i++, in++, out++)
2207 out->normal[0] = LittleFloat(in->normal[0]);
2208 out->normal[1] = LittleFloat(in->normal[1]);
2209 out->normal[2] = LittleFloat(in->normal[2]);
2210 out->dist = LittleFloat(in->dist);
2216 typedef struct portal_s
2219 mnode_t *nodes[2]; // [0] = front side of plane
2220 struct portal_s *next[2];
2222 struct portal_s *chain; // all portals are linked into a list
2226 static portal_t *portalchain;
2233 static portal_t *AllocPortal(void)
2236 p = Mem_Alloc(loadmodel->mempool, sizeof(portal_t));
2237 p->chain = portalchain;
2242 static void FreePortal(portal_t *p)
2247 static void Mod_Q1BSP_RecursiveRecalcNodeBBox(mnode_t *node)
2249 // calculate children first
2250 if (node->children[0]->contents >= 0)
2251 Mod_Q1BSP_RecursiveRecalcNodeBBox(node->children[0]);
2252 if (node->children[1]->contents >= 0)
2253 Mod_Q1BSP_RecursiveRecalcNodeBBox(node->children[1]);
2255 // make combined bounding box from children
2256 node->mins[0] = min(node->children[0]->mins[0], node->children[1]->mins[0]);
2257 node->mins[1] = min(node->children[0]->mins[1], node->children[1]->mins[1]);
2258 node->mins[2] = min(node->children[0]->mins[2], node->children[1]->mins[2]);
2259 node->maxs[0] = max(node->children[0]->maxs[0], node->children[1]->maxs[0]);
2260 node->maxs[1] = max(node->children[0]->maxs[1], node->children[1]->maxs[1]);
2261 node->maxs[2] = max(node->children[0]->maxs[2], node->children[1]->maxs[2]);
2264 static void Mod_Q1BSP_FinalizePortals(void)
2266 int i, j, numportals, numpoints;
2267 portal_t *p, *pnext;
2270 mleaf_t *leaf, *endleaf;
2273 // recalculate bounding boxes for all leafs(because qbsp is very sloppy)
2274 leaf = loadmodel->brushq1.data_leafs;
2275 endleaf = leaf + loadmodel->brushq1.num_leafs;
2276 for (;leaf < endleaf;leaf++)
2278 VectorSet(leaf->mins, 2000000000, 2000000000, 2000000000);
2279 VectorSet(leaf->maxs, -2000000000, -2000000000, -2000000000);
2286 for (i = 0;i < 2;i++)
2288 leaf = (mleaf_t *)p->nodes[i];
2290 for (j = 0;j < w->numpoints;j++)
2292 if (leaf->mins[0] > w->points[j][0]) leaf->mins[0] = w->points[j][0];
2293 if (leaf->mins[1] > w->points[j][1]) leaf->mins[1] = w->points[j][1];
2294 if (leaf->mins[2] > w->points[j][2]) leaf->mins[2] = w->points[j][2];
2295 if (leaf->maxs[0] < w->points[j][0]) leaf->maxs[0] = w->points[j][0];
2296 if (leaf->maxs[1] < w->points[j][1]) leaf->maxs[1] = w->points[j][1];
2297 if (leaf->maxs[2] < w->points[j][2]) leaf->maxs[2] = w->points[j][2];
2304 Mod_Q1BSP_RecursiveRecalcNodeBBox(loadmodel->brushq1.nodes);
2306 // tally up portal and point counts
2312 // note: this check must match the one below or it will usually corrupt memory
2313 // 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
2314 if (p->winding && p->nodes[0] != p->nodes[1]
2315 && p->nodes[0]->contents != CONTENTS_SOLID && p->nodes[1]->contents != CONTENTS_SOLID
2316 && p->nodes[0]->contents != CONTENTS_SKY && p->nodes[1]->contents != CONTENTS_SKY)
2319 numpoints += p->winding->numpoints * 2;
2323 loadmodel->brushq1.portals = Mem_Alloc(loadmodel->mempool, numportals * sizeof(mportal_t) + numpoints * sizeof(mvertex_t));
2324 loadmodel->brushq1.numportals = numportals;
2325 loadmodel->brushq1.portalpoints = (void *)((qbyte *) loadmodel->brushq1.portals + numportals * sizeof(mportal_t));
2326 loadmodel->brushq1.numportalpoints = numpoints;
2327 // clear all leaf portal chains
2328 for (i = 0;i < loadmodel->brushq1.num_leafs;i++)
2329 loadmodel->brushq1.data_leafs[i].portals = NULL;
2330 // process all portals in the global portal chain, while freeing them
2331 portal = loadmodel->brushq1.portals;
2332 point = loadmodel->brushq1.portalpoints;
2341 // note: this check must match the one above or it will usually corrupt memory
2342 // 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
2343 if (p->nodes[0] != p->nodes[1]
2344 && p->nodes[0]->contents != CONTENTS_SOLID && p->nodes[1]->contents != CONTENTS_SOLID
2345 && p->nodes[0]->contents != CONTENTS_SKY && p->nodes[1]->contents != CONTENTS_SKY)
2347 // first make the back to front portal(forward portal)
2348 portal->points = point;
2349 portal->numpoints = p->winding->numpoints;
2350 portal->plane.dist = p->plane.dist;
2351 VectorCopy(p->plane.normal, portal->plane.normal);
2352 portal->here = (mleaf_t *)p->nodes[1];
2353 portal->past = (mleaf_t *)p->nodes[0];
2355 for (j = 0;j < portal->numpoints;j++)
2357 VectorCopy(p->winding->points[j], point->position);
2360 PlaneClassify(&portal->plane);
2362 // link into leaf's portal chain
2363 portal->next = portal->here->portals;
2364 portal->here->portals = portal;
2366 // advance to next portal
2369 // then make the front to back portal(backward portal)
2370 portal->points = point;
2371 portal->numpoints = p->winding->numpoints;
2372 portal->plane.dist = -p->plane.dist;
2373 VectorNegate(p->plane.normal, portal->plane.normal);
2374 portal->here = (mleaf_t *)p->nodes[0];
2375 portal->past = (mleaf_t *)p->nodes[1];
2377 for (j = portal->numpoints - 1;j >= 0;j--)
2379 VectorCopy(p->winding->points[j], point->position);
2382 PlaneClassify(&portal->plane);
2384 // link into leaf's portal chain
2385 portal->next = portal->here->portals;
2386 portal->here->portals = portal;
2388 // advance to next portal
2391 Winding_Free(p->winding);
2403 static void AddPortalToNodes(portal_t *p, mnode_t *front, mnode_t *back)
2406 Host_Error("AddPortalToNodes: NULL front node");
2408 Host_Error("AddPortalToNodes: NULL back node");
2409 if (p->nodes[0] || p->nodes[1])
2410 Host_Error("AddPortalToNodes: already included");
2411 // 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
2413 p->nodes[0] = front;
2414 p->next[0] = (portal_t *)front->portals;
2415 front->portals = (mportal_t *)p;
2418 p->next[1] = (portal_t *)back->portals;
2419 back->portals = (mportal_t *)p;
2424 RemovePortalFromNode
2427 static void RemovePortalFromNodes(portal_t *portal)
2431 void **portalpointer;
2433 for (i = 0;i < 2;i++)
2435 node = portal->nodes[i];
2437 portalpointer = (void **) &node->portals;
2442 Host_Error("RemovePortalFromNodes: portal not in leaf");
2446 if (portal->nodes[0] == node)
2448 *portalpointer = portal->next[0];
2449 portal->nodes[0] = NULL;
2451 else if (portal->nodes[1] == node)
2453 *portalpointer = portal->next[1];
2454 portal->nodes[1] = NULL;
2457 Host_Error("RemovePortalFromNodes: portal not bounding leaf");
2461 if (t->nodes[0] == node)
2462 portalpointer = (void **) &t->next[0];
2463 else if (t->nodes[1] == node)
2464 portalpointer = (void **) &t->next[1];
2466 Host_Error("RemovePortalFromNodes: portal not bounding leaf");
2471 static void Mod_Q1BSP_RecursiveNodePortals(mnode_t *node)
2474 mnode_t *front, *back, *other_node;
2475 mplane_t clipplane, *plane;
2476 portal_t *portal, *nextportal, *nodeportal, *splitportal, *temp;
2477 winding_t *nodeportalwinding, *frontwinding, *backwinding;
2479 // if a leaf, we're done
2483 plane = node->plane;
2485 front = node->children[0];
2486 back = node->children[1];
2488 Host_Error("Mod_Q1BSP_RecursiveNodePortals: corrupt node hierarchy");
2490 // create the new portal by generating a polygon for the node plane,
2491 // and clipping it by all of the other portals(which came from nodes above this one)
2492 nodeportal = AllocPortal();
2493 nodeportal->plane = *plane;
2495 nodeportalwinding = Winding_NewFromPlane(nodeportal->plane.normal[0], nodeportal->plane.normal[1], nodeportal->plane.normal[2], nodeportal->plane.dist);
2496 side = 0; // shut up compiler warning
2497 for (portal = (portal_t *)node->portals;portal;portal = portal->next[side])
2499 clipplane = portal->plane;
2500 if (portal->nodes[0] == portal->nodes[1])
2501 Host_Error("Mod_Q1BSP_RecursiveNodePortals: portal has same node on both sides(1)");
2502 if (portal->nodes[0] == node)
2504 else if (portal->nodes[1] == node)
2506 clipplane.dist = -clipplane.dist;
2507 VectorNegate(clipplane.normal, clipplane.normal);
2511 Host_Error("Mod_Q1BSP_RecursiveNodePortals: mislinked portal");
2513 nodeportalwinding = Winding_Clip(nodeportalwinding, clipplane.normal[0], clipplane.normal[1], clipplane.normal[2], clipplane.dist, true);
2514 if (!nodeportalwinding)
2516 Con_Printf("Mod_Q1BSP_RecursiveNodePortals: WARNING: new portal was clipped away\n");
2521 if (nodeportalwinding)
2523 // if the plane was not clipped on all sides, there was an error
2524 nodeportal->winding = nodeportalwinding;
2525 AddPortalToNodes(nodeportal, front, back);
2528 // split the portals of this node along this node's plane and assign them to the children of this node
2529 // (migrating the portals downward through the tree)
2530 for (portal = (portal_t *)node->portals;portal;portal = nextportal)
2532 if (portal->nodes[0] == portal->nodes[1])
2533 Host_Error("Mod_Q1BSP_RecursiveNodePortals: portal has same node on both sides(2)");
2534 if (portal->nodes[0] == node)
2536 else if (portal->nodes[1] == node)
2539 Host_Error("Mod_Q1BSP_RecursiveNodePortals: mislinked portal");
2540 nextportal = portal->next[side];
2542 other_node = portal->nodes[!side];
2543 RemovePortalFromNodes(portal);
2545 // cut the portal into two portals, one on each side of the node plane
2546 Winding_Divide(portal->winding, plane->normal[0], plane->normal[1], plane->normal[2], plane->dist, &frontwinding, &backwinding);
2551 AddPortalToNodes(portal, back, other_node);
2553 AddPortalToNodes(portal, other_node, back);
2559 AddPortalToNodes(portal, front, other_node);
2561 AddPortalToNodes(portal, other_node, front);
2565 // the winding is split
2566 splitportal = AllocPortal();
2567 temp = splitportal->chain;
2568 *splitportal = *portal;
2569 splitportal->chain = temp;
2570 splitportal->winding = backwinding;
2571 Winding_Free(portal->winding);
2572 portal->winding = frontwinding;
2576 AddPortalToNodes(portal, front, other_node);
2577 AddPortalToNodes(splitportal, back, other_node);
2581 AddPortalToNodes(portal, other_node, front);
2582 AddPortalToNodes(splitportal, other_node, back);
2586 Mod_Q1BSP_RecursiveNodePortals(front);
2587 Mod_Q1BSP_RecursiveNodePortals(back);
2590 static void Mod_Q1BSP_MakePortals(void)
2593 Mod_Q1BSP_RecursiveNodePortals(loadmodel->brushq1.nodes);
2594 Mod_Q1BSP_FinalizePortals();
2597 static void Mod_Q1BSP_BuildSurfaceNeighbors(msurface_t *surfaces, int numsurfaces, mempool_t *mempool)
2600 int surfnum, vertnum, vertnum2, snum, vnum, vnum2;
2601 msurface_t *surf, *s;
2602 float *v0, *v1, *v2, *v3;
2603 for (surf = surfaces, surfnum = 0;surfnum < numsurfaces;surf++, surfnum++)
2604 surf->neighborsurfaces = Mem_Alloc(mempool, surf->poly_numverts * sizeof(msurface_t *));
2605 for (surf = surfaces, surfnum = 0;surfnum < numsurfaces;surf++, surfnum++)
2607 for (vertnum = surf->poly_numverts - 1, vertnum2 = 0, v0 = surf->poly_verts + (surf->poly_numverts - 1) * 3, v1 = surf->poly_verts;vertnum2 < surf->poly_numverts;vertnum = vertnum2, vertnum2++, v0 = v1, v1 += 3)
2609 if (surf->neighborsurfaces[vertnum])
2611 surf->neighborsurfaces[vertnum] = NULL;
2612 for (s = surfaces, snum = 0;snum < numsurfaces;s++, snum++)
2614 if (s->poly_mins[0] > (surf->poly_maxs[0] + 1) || s->poly_maxs[0] < (surf->poly_mins[0] - 1)
2615 || s->poly_mins[1] > (surf->poly_maxs[1] + 1) || s->poly_maxs[1] < (surf->poly_mins[1] - 1)
2616 || s->poly_mins[2] > (surf->poly_maxs[2] + 1) || s->poly_maxs[2] < (surf->poly_mins[2] - 1)
2619 for (vnum = 0;vnum < s->poly_numverts;vnum++)
2620 if (s->neighborsurfaces[vnum] == surf)
2622 if (vnum < s->poly_numverts)
2624 for (vnum = s->poly_numverts - 1, vnum2 = 0, v2 = s->poly_verts + (s->poly_numverts - 1) * 3, v3 = s->poly_verts;vnum2 < s->poly_numverts;vnum = vnum2, vnum2++, v2 = v3, v3 += 3)
2626 if (s->neighborsurfaces[vnum] == NULL
2627 && ((v0[0] == v2[0] && v0[1] == v2[1] && v0[2] == v2[2] && v1[0] == v3[0] && v1[1] == v3[1] && v1[2] == v3[2])
2628 || (v1[0] == v2[0] && v1[1] == v2[1] && v1[2] == v2[2] && v0[0] == v3[0] && v0[1] == v3[1] && v0[2] == v3[2])))
2630 surf->neighborsurfaces[vertnum] = s;
2631 s->neighborsurfaces[vnum] = surf;
2635 if (vnum < s->poly_numverts)
2643 static void Mod_Q1BSP_BuildLightmapUpdateChains(mempool_t *mempool, model_t *model)
2645 int i, j, stylecounts[256], totalcount, remapstyles[256];
2647 memset(stylecounts, 0, sizeof(stylecounts));
2648 for (i = 0;i < model->brushq1.nummodelsurfaces;i++)
2650 surf = model->brushq1.surfaces + model->brushq1.firstmodelsurface + i;
2651 for (j = 0;j < MAXLIGHTMAPS;j++)
2652 stylecounts[surf->styles[j]]++;
2655 model->brushq1.light_styles = 0;
2656 for (i = 0;i < 255;i++)
2660 remapstyles[i] = model->brushq1.light_styles++;
2661 totalcount += stylecounts[i] + 1;
2666 model->brushq1.light_style = Mem_Alloc(mempool, model->brushq1.light_styles * sizeof(qbyte));
2667 model->brushq1.light_stylevalue = Mem_Alloc(mempool, model->brushq1.light_styles * sizeof(int));
2668 model->brushq1.light_styleupdatechains = Mem_Alloc(mempool, model->brushq1.light_styles * sizeof(msurface_t **));
2669 model->brushq1.light_styleupdatechainsbuffer = Mem_Alloc(mempool, totalcount * sizeof(msurface_t *));
2670 model->brushq1.light_styles = 0;
2671 for (i = 0;i < 255;i++)
2673 model->brushq1.light_style[model->brushq1.light_styles++] = i;
2675 for (i = 0;i < model->brushq1.light_styles;i++)
2677 model->brushq1.light_styleupdatechains[i] = model->brushq1.light_styleupdatechainsbuffer + j;
2678 j += stylecounts[model->brushq1.light_style[i]] + 1;
2680 for (i = 0;i < model->brushq1.nummodelsurfaces;i++)
2682 surf = model->brushq1.surfaces + model->brushq1.firstmodelsurface + i;
2683 for (j = 0;j < MAXLIGHTMAPS;j++)
2684 if (surf->styles[j] != 255)
2685 *model->brushq1.light_styleupdatechains[remapstyles[surf->styles[j]]]++ = surf;
2688 for (i = 0;i < model->brushq1.light_styles;i++)
2690 *model->brushq1.light_styleupdatechains[i] = NULL;
2691 model->brushq1.light_styleupdatechains[i] = model->brushq1.light_styleupdatechainsbuffer + j;
2692 j += stylecounts[model->brushq1.light_style[i]] + 1;
2696 static void Mod_Q1BSP_BuildPVSTextureChains(model_t *model)
2699 for (i = 0;i < model->brushq1.numtextures;i++)
2700 model->brushq1.pvstexturechainslength[i] = 0;
2701 for (i = 0, j = model->brushq1.firstmodelsurface;i < model->brushq1.nummodelsurfaces;i++, j++)
2703 if (model->brushq1.surfacepvsframes[j] == model->brushq1.pvsframecount)
2705 model->brushq1.pvssurflist[model->brushq1.pvssurflistlength++] = j;
2706 model->brushq1.pvstexturechainslength[model->brushq1.surfaces[j].texinfo->texture->number]++;
2709 for (i = 0, j = 0;i < model->brushq1.numtextures;i++)
2711 if (model->brushq1.pvstexturechainslength[i])
2713 model->brushq1.pvstexturechains[i] = model->brushq1.pvstexturechainsbuffer + j;
2714 j += model->brushq1.pvstexturechainslength[i] + 1;
2717 model->brushq1.pvstexturechains[i] = NULL;
2719 for (i = 0, j = model->brushq1.firstmodelsurface;i < model->brushq1.nummodelsurfaces;i++, j++)
2720 if (model->brushq1.surfacepvsframes[j] == model->brushq1.pvsframecount)
2721 *model->brushq1.pvstexturechains[model->brushq1.surfaces[j].texinfo->texture->number]++ = model->brushq1.surfaces + j;
2722 for (i = 0;i < model->brushq1.numtextures;i++)
2724 if (model->brushq1.pvstexturechainslength[i])
2726 *model->brushq1.pvstexturechains[i] = NULL;
2727 model->brushq1.pvstexturechains[i] -= model->brushq1.pvstexturechainslength[i];
2732 static void Mod_Q1BSP_FatPVS_RecursiveBSPNode(model_t *model, const vec3_t org, vec_t radius, qbyte *pvsbuffer, int pvsbytes, mnode_t *node)
2736 float d = PlaneDiff(org, node->plane);
2738 node = node->children[0];
2739 else if (d < -radius)
2740 node = node->children[1];
2743 // go down both sides
2744 Mod_Q1BSP_FatPVS_RecursiveBSPNode(model, org, radius, pvsbuffer, pvsbytes, node->children[0]);
2745 node = node->children[1];
2748 // if this leaf is in a cluster, accumulate the pvs bits
2749 if (((mleaf_t *)node)->clusterindex >= 0)
2752 qbyte *pvs = model->brush.data_pvsclusters + ((mleaf_t *)node)->clusterindex * model->brush.num_pvsclusterbytes;
2753 for (i = 0;i < pvsbytes;i++)
2754 pvsbuffer[i] |= pvs[i];
2758 //Calculates a PVS that is the inclusive or of all leafs within radius pixels
2759 //of the given point.
2760 static int Mod_Q1BSP_FatPVS(model_t *model, const vec3_t org, vec_t radius, qbyte *pvsbuffer, int pvsbufferlength)
2762 int bytes = ((model->brushq1.num_leafs - 1) + 7) >> 3;
2763 bytes = min(bytes, pvsbufferlength);
2764 if (r_novis.integer)
2766 memset(pvsbuffer, 0xFF, bytes);
2769 memset(pvsbuffer, 0, bytes);
2770 Mod_Q1BSP_FatPVS_RecursiveBSPNode(model, org, radius, pvsbuffer, bytes, model->brushq1.nodes);
2774 //Returns PVS data for a given point
2775 //(note: can return NULL)
2776 static qbyte *Mod_Q1BSP_GetPVS(model_t *model, const vec3_t p)
2779 Mod_CheckLoaded(model);
2780 node = model->brushq1.nodes;
2782 node = node->children[(node->plane->type < 3 ? p[node->plane->type] : DotProduct(p,node->plane->normal)) < node->plane->dist];
2783 if (((mleaf_t *)node)->clusterindex >= 0)
2784 return model->brush.data_pvsclusters + ((mleaf_t *)node)->clusterindex * model->brush.num_pvsclusterbytes;
2789 static void Mod_Q1BSP_RoundUpToHullSize(model_t *cmodel, const vec3_t inmins, const vec3_t inmaxs, vec3_t outmins, vec3_t outmaxs)
2794 VectorSubtract(inmaxs, inmins, size);
2795 if (cmodel->brush.ishlbsp)
2798 hull = &cmodel->brushq1.hulls[0]; // 0x0x0
2799 else if (size[0] <= 32)
2801 if (size[2] < 54) // pick the nearest of 36 or 72
2802 hull = &cmodel->brushq1.hulls[3]; // 32x32x36
2804 hull = &cmodel->brushq1.hulls[1]; // 32x32x72
2807 hull = &cmodel->brushq1.hulls[2]; // 64x64x64
2812 hull = &cmodel->brushq1.hulls[0]; // 0x0x0
2813 else if (size[0] <= 32)
2814 hull = &cmodel->brushq1.hulls[1]; // 32x32x56
2816 hull = &cmodel->brushq1.hulls[2]; // 64x64x88
2818 VectorCopy(inmins, outmins);
2819 VectorAdd(inmins, hull->clip_size, outmaxs);
2822 extern void R_Model_Brush_DrawSky(entity_render_t *ent);
2823 extern void R_Model_Brush_Draw(entity_render_t *ent);
2824 extern void R_Model_Brush_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius);
2825 extern void R_Model_Brush_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap);
2826 void Mod_Q1BSP_Load(model_t *mod, void *buffer)
2831 mempool_t *mainmempool;
2833 model_t *originalloadmodel;
2834 float dist, modelyawradius, modelradius, *vec;
2837 mod->type = mod_brush;
2839 header = (dheader_t *)buffer;
2841 i = LittleLong(header->version);
2842 if (i != BSPVERSION && i != 30)
2843 Host_Error("Mod_Q1BSP_Load: %s has wrong version number(%i should be %i(Quake) or 30(HalfLife))", mod->name, i, BSPVERSION);
2844 mod->brush.ishlbsp = i == 30;
2846 mod->soundfromcenter = true;
2847 mod->TraceBox = Mod_Q1BSP_TraceBox;
2848 mod->brush.SuperContentsFromNativeContents = Mod_Q1BSP_SuperContentsFromNativeContents;
2849 mod->brush.NativeContentsFromSuperContents = Mod_Q1BSP_NativeContentsFromSuperContents;
2850 mod->brush.GetPVS = Mod_Q1BSP_GetPVS;
2851 mod->brush.FatPVS = Mod_Q1BSP_FatPVS;
2852 mod->brush.BoxTouchingPVS = Mod_Q1BSP_BoxTouchingPVS;
2853 mod->brush.LightPoint = Mod_Q1BSP_LightPoint;
2854 mod->brush.FindNonSolidLocation = Mod_Q1BSP_FindNonSolidLocation;
2855 mod->brush.AmbientSoundLevelsForPoint = Mod_Q1BSP_AmbientSoundLevelsForPoint;
2856 mod->brush.RoundUpToHullSize = Mod_Q1BSP_RoundUpToHullSize;
2857 mod->brushq1.PointInLeaf = Mod_Q1BSP_PointInLeaf;
2858 mod->brushq1.BuildPVSTextureChains = Mod_Q1BSP_BuildPVSTextureChains;
2860 if (loadmodel->isworldmodel)
2862 Cvar_SetValue("halflifebsp", mod->brush.ishlbsp);
2863 // until we get a texture for it...
2867 // swap all the lumps
2868 mod_base = (qbyte *)header;
2870 for (i = 0;i < (int) sizeof(dheader_t) / 4;i++)
2871 ((int *)header)[i] = LittleLong(((int *)header)[i]);
2875 // store which lightmap format to use
2876 mod->brushq1.lightmaprgba = r_lightmaprgba.integer;
2878 Mod_Q1BSP_LoadEntities(&header->lumps[LUMP_ENTITIES]);
2879 Mod_Q1BSP_LoadVertexes(&header->lumps[LUMP_VERTEXES]);
2880 Mod_Q1BSP_LoadEdges(&header->lumps[LUMP_EDGES]);
2881 Mod_Q1BSP_LoadSurfedges(&header->lumps[LUMP_SURFEDGES]);
2882 Mod_Q1BSP_LoadTextures(&header->lumps[LUMP_TEXTURES]);
2883 Mod_Q1BSP_LoadLighting(&header->lumps[LUMP_LIGHTING]);
2884 Mod_Q1BSP_LoadPlanes(&header->lumps[LUMP_PLANES]);
2885 Mod_Q1BSP_LoadTexinfo(&header->lumps[LUMP_TEXINFO]);
2886 Mod_Q1BSP_LoadFaces(&header->lumps[LUMP_FACES]);
2887 Mod_Q1BSP_LoadMarksurfaces(&header->lumps[LUMP_MARKSURFACES]);
2888 Mod_Q1BSP_LoadVisibility(&header->lumps[LUMP_VISIBILITY]);
2889 // load submodels before leafs because they contain the number of vis leafs
2890 Mod_Q1BSP_LoadSubmodels(&header->lumps[LUMP_MODELS]);
2891 Mod_Q1BSP_LoadLeafs(&header->lumps[LUMP_LEAFS]);
2892 Mod_Q1BSP_LoadNodes(&header->lumps[LUMP_NODES]);
2893 Mod_Q1BSP_LoadClipnodes(&header->lumps[LUMP_CLIPNODES]);
2895 if (mod->brushq1.data_compressedpvs)
2896 Mem_Free(mod->brushq1.data_compressedpvs);
2897 mod->brushq1.data_compressedpvs = NULL;
2898 mod->brushq1.num_compressedpvs = 0;
2900 Mod_Q1BSP_MakeHull0();
2901 Mod_Q1BSP_MakePortals();
2903 mod->numframes = 2; // regular and alternate animation
2905 mainmempool = mod->mempool;
2906 loadname = mod->name;
2908 Mod_Q1BSP_LoadLightList();
2909 originalloadmodel = loadmodel;
2912 // set up the submodels(FIXME: this is confusing)
2914 for (i = 0;i < mod->brush.numsubmodels;i++)
2916 bm = &mod->brushq1.submodels[i];
2918 mod->brushq1.hulls[0].firstclipnode = bm->headnode[0];
2919 for (j=1 ; j<MAX_MAP_HULLS ; j++)
2921 mod->brushq1.hulls[j].firstclipnode = bm->headnode[j];
2922 mod->brushq1.hulls[j].lastclipnode = mod->brushq1.numclipnodes - 1;
2925 mod->brushq1.firstmodelsurface = bm->firstface;
2926 mod->brushq1.nummodelsurfaces = bm->numfaces;
2928 // this gets altered below if sky is used
2929 mod->DrawSky = NULL;
2930 mod->Draw = R_Model_Brush_Draw;
2931 mod->DrawShadowVolume = R_Model_Brush_DrawShadowVolume;
2932 mod->DrawLight = R_Model_Brush_DrawLight;
2935 mod->brush.GetPVS = NULL;
2936 mod->brush.FatPVS = NULL;
2937 mod->brush.BoxTouchingPVS = NULL;
2938 mod->brush.LightPoint = NULL;
2939 mod->brush.AmbientSoundLevelsForPoint = NULL;
2941 mod->brushq1.pvstexturechains = Mem_Alloc(originalloadmodel->mempool, mod->brushq1.numtextures * sizeof(msurface_t **));
2942 mod->brushq1.pvstexturechainsbuffer = Mem_Alloc(originalloadmodel->mempool,(mod->brushq1.nummodelsurfaces + mod->brushq1.numtextures) * sizeof(msurface_t *));
2943 mod->brushq1.pvstexturechainslength = Mem_Alloc(originalloadmodel->mempool, mod->brushq1.numtextures * sizeof(int));
2944 Mod_Q1BSP_BuildPVSTextureChains(mod);
2945 Mod_Q1BSP_BuildLightmapUpdateChains(originalloadmodel->mempool, mod);
2946 if (mod->brushq1.nummodelsurfaces)
2948 // LordHavoc: calculate bmodel bounding box rather than trusting what it says
2949 mod->normalmins[0] = mod->normalmins[1] = mod->normalmins[2] = 1000000000.0f;
2950 mod->normalmaxs[0] = mod->normalmaxs[1] = mod->normalmaxs[2] = -1000000000.0f;
2953 for (j = 0, surf = &mod->brushq1.surfaces[mod->brushq1.firstmodelsurface];j < mod->brushq1.nummodelsurfaces;j++, surf++)
2955 // we only need to have a drawsky function if it is used(usually only on world model)
2956 if (surf->texinfo->texture->shader == &Cshader_sky)
2957 mod->DrawSky = R_Model_Brush_DrawSky;
2958 // LordHavoc: submodels always clip, even if water
2959 if (mod->brush.numsubmodels - 1)
2960 surf->flags |= SURF_SOLIDCLIP;
2961 // calculate bounding shapes
2962 for (k = 0, vec = surf->mesh.data_vertex3f;k < surf->mesh.num_vertices;k++, vec += 3)
2964 if (mod->normalmins[0] > vec[0]) mod->normalmins[0] = vec[0];
2965 if (mod->normalmins[1] > vec[1]) mod->normalmins[1] = vec[1];
2966 if (mod->normalmins[2] > vec[2]) mod->normalmins[2] = vec[2];
2967 if (mod->normalmaxs[0] < vec[0]) mod->normalmaxs[0] = vec[0];
2968 if (mod->normalmaxs[1] < vec[1]) mod->normalmaxs[1] = vec[1];
2969 if (mod->normalmaxs[2] < vec[2]) mod->normalmaxs[2] = vec[2];
2970 dist = vec[0]*vec[0]+vec[1]*vec[1];
2971 if (modelyawradius < dist)
2972 modelyawradius = dist;
2973 dist += vec[2]*vec[2];
2974 if (modelradius < dist)
2978 modelyawradius = sqrt(modelyawradius);
2979 modelradius = sqrt(modelradius);
2980 mod->yawmins[0] = mod->yawmins[1] = - (mod->yawmaxs[0] = mod->yawmaxs[1] = modelyawradius);
2981 mod->yawmins[2] = mod->normalmins[2];
2982 mod->yawmaxs[2] = mod->normalmaxs[2];
2983 mod->rotatedmins[0] = mod->rotatedmins[1] = mod->rotatedmins[2] = -modelradius;
2984 mod->rotatedmaxs[0] = mod->rotatedmaxs[1] = mod->rotatedmaxs[2] = modelradius;
2985 mod->radius = modelradius;
2986 mod->radius2 = modelradius * modelradius;
2990 // LordHavoc: empty submodel(lacrima.bsp has such a glitch)
2991 Con_Printf("warning: empty submodel *%i in %s\n", i+1, loadname);
2993 Mod_Q1BSP_BuildSurfaceNeighbors(mod->brushq1.surfaces + mod->brushq1.firstmodelsurface, mod->brushq1.nummodelsurfaces, originalloadmodel->mempool);
2995 mod->brushq1.num_visleafs = bm->visleafs;
2997 // LordHavoc: only register submodels if it is the world
2998 // (prevents bsp models from replacing world submodels)
2999 if (loadmodel->isworldmodel && i < (mod->brush.numsubmodels - 1))
3002 // duplicate the basic information
3003 sprintf(name, "*%i", i+1);
3004 loadmodel = Mod_FindName(name);
3006 strcpy(loadmodel->name, name);
3007 // textures and memory belong to the main model
3008 loadmodel->texturepool = NULL;
3009 loadmodel->mempool = NULL;
3014 loadmodel = originalloadmodel;
3015 //Mod_Q1BSP_ProcessLightList();
3017 if (developer.integer)
3018 Con_Printf("Some stats for q1bsp model \"%s\": %i faces, %i nodes, %i leafs, %i visleafs, %i visleafportals\n", loadmodel->name, loadmodel->brushq1.numsurfaces, loadmodel->brushq1.numnodes, loadmodel->brushq1.num_leafs, loadmodel->brushq1.num_visleafs, loadmodel->brushq1.numportals);
3021 static void Mod_Q2BSP_LoadEntities(lump_t *l)
3025 static void Mod_Q2BSP_LoadPlanes(lump_t *l)
3032 in = (void *)(mod_base + l->fileofs);
3033 if (l->filelen % sizeof(*in))
3034 Host_Error("Mod_Q2BSP_LoadPlanes: funny lump size in %s",loadmodel->name);
3035 count = l->filelen / sizeof(*in);
3036 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3039 loadmodel->num = count;
3041 for (i = 0;i < count;i++, in++, out++)
3047 static void Mod_Q2BSP_LoadVertices(lump_t *l)
3054 in = (void *)(mod_base + l->fileofs);
3055 if (l->filelen % sizeof(*in))
3056 Host_Error("Mod_Q2BSP_LoadVertices: funny lump size in %s",loadmodel->name);
3057 count = l->filelen / sizeof(*in);
3058 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3061 loadmodel->num = count;
3063 for (i = 0;i < count;i++, in++, out++)
3069 static void Mod_Q2BSP_LoadVisibility(lump_t *l)
3076 in = (void *)(mod_base + l->fileofs);
3077 if (l->filelen % sizeof(*in))
3078 Host_Error("Mod_Q2BSP_LoadVisibility: funny lump size in %s",loadmodel->name);
3079 count = l->filelen / sizeof(*in);
3080 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3083 loadmodel->num = count;
3085 for (i = 0;i < count;i++, in++, out++)
3091 static void Mod_Q2BSP_LoadNodes(lump_t *l)
3098 in = (void *)(mod_base + l->fileofs);
3099 if (l->filelen % sizeof(*in))
3100 Host_Error("Mod_Q2BSP_LoadNodes: funny lump size in %s",loadmodel->name);
3101 count = l->filelen / sizeof(*in);
3102 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3105 loadmodel->num = count;
3107 for (i = 0;i < count;i++, in++, out++)
3113 static void Mod_Q2BSP_LoadTexInfo(lump_t *l)
3120 in = (void *)(mod_base + l->fileofs);
3121 if (l->filelen % sizeof(*in))
3122 Host_Error("Mod_Q2BSP_LoadTexInfo: funny lump size in %s",loadmodel->name);
3123 count = l->filelen / sizeof(*in);
3124 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3127 loadmodel->num = count;
3129 for (i = 0;i < count;i++, in++, out++)
3135 static void Mod_Q2BSP_LoadFaces(lump_t *l)
3142 in = (void *)(mod_base + l->fileofs);
3143 if (l->filelen % sizeof(*in))
3144 Host_Error("Mod_Q2BSP_LoadFaces: funny lump size in %s",loadmodel->name);
3145 count = l->filelen / sizeof(*in);
3146 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3149 loadmodel->num = count;
3151 for (i = 0;i < count;i++, in++, out++)
3157 static void Mod_Q2BSP_LoadLighting(lump_t *l)
3164 in = (void *)(mod_base + l->fileofs);
3165 if (l->filelen % sizeof(*in))
3166 Host_Error("Mod_Q2BSP_LoadLighting: funny lump size in %s",loadmodel->name);
3167 count = l->filelen / sizeof(*in);
3168 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3171 loadmodel->num = count;
3173 for (i = 0;i < count;i++, in++, out++)
3179 static void Mod_Q2BSP_LoadLeafs(lump_t *l)
3186 in = (void *)(mod_base + l->fileofs);
3187 if (l->filelen % sizeof(*in))
3188 Host_Error("Mod_Q2BSP_LoadLeafs: funny lump size in %s",loadmodel->name);
3189 count = l->filelen / sizeof(*in);
3190 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3193 loadmodel->num = count;
3195 for (i = 0;i < count;i++, in++, out++)
3201 static void Mod_Q2BSP_LoadLeafFaces(lump_t *l)
3208 in = (void *)(mod_base + l->fileofs);
3209 if (l->filelen % sizeof(*in))
3210 Host_Error("Mod_Q2BSP_LoadLeafFaces: funny lump size in %s",loadmodel->name);
3211 count = l->filelen / sizeof(*in);
3212 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3215 loadmodel->num = count;
3217 for (i = 0;i < count;i++, in++, out++)
3223 static void Mod_Q2BSP_LoadLeafBrushes(lump_t *l)
3230 in = (void *)(mod_base + l->fileofs);
3231 if (l->filelen % sizeof(*in))
3232 Host_Error("Mod_Q2BSP_LoadLeafBrushes: funny lump size in %s",loadmodel->name);
3233 count = l->filelen / sizeof(*in);
3234 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3237 loadmodel->num = count;
3239 for (i = 0;i < count;i++, in++, out++)
3245 static void Mod_Q2BSP_LoadEdges(lump_t *l)
3252 in = (void *)(mod_base + l->fileofs);
3253 if (l->filelen % sizeof(*in))
3254 Host_Error("Mod_Q2BSP_LoadEdges: funny lump size in %s",loadmodel->name);
3255 count = l->filelen / sizeof(*in);
3256 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3259 loadmodel->num = count;
3261 for (i = 0;i < count;i++, in++, out++)
3267 static void Mod_Q2BSP_LoadSurfEdges(lump_t *l)
3274 in = (void *)(mod_base + l->fileofs);
3275 if (l->filelen % sizeof(*in))
3276 Host_Error("Mod_Q2BSP_LoadSurfEdges: funny lump size in %s",loadmodel->name);
3277 count = l->filelen / sizeof(*in);
3278 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3281 loadmodel->num = count;
3283 for (i = 0;i < count;i++, in++, out++)
3289 static void Mod_Q2BSP_LoadBrushes(lump_t *l)
3296 in = (void *)(mod_base + l->fileofs);
3297 if (l->filelen % sizeof(*in))
3298 Host_Error("Mod_Q2BSP_LoadBrushes: funny lump size in %s",loadmodel->name);
3299 count = l->filelen / sizeof(*in);
3300 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3303 loadmodel->num = count;
3305 for (i = 0;i < count;i++, in++, out++)
3311 static void Mod_Q2BSP_LoadBrushSides(lump_t *l)
3318 in = (void *)(mod_base + l->fileofs);
3319 if (l->filelen % sizeof(*in))
3320 Host_Error("Mod_Q2BSP_LoadBrushSides: funny lump size in %s",loadmodel->name);
3321 count = l->filelen / sizeof(*in);
3322 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3325 loadmodel->num = count;
3327 for (i = 0;i < count;i++, in++, out++)
3333 static void Mod_Q2BSP_LoadAreas(lump_t *l)
3340 in = (void *)(mod_base + l->fileofs);
3341 if (l->filelen % sizeof(*in))
3342 Host_Error("Mod_Q2BSP_LoadAreas: funny lump size in %s",loadmodel->name);
3343 count = l->filelen / sizeof(*in);
3344 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3347 loadmodel->num = count;
3349 for (i = 0;i < count;i++, in++, out++)
3355 static void Mod_Q2BSP_LoadAreaPortals(lump_t *l)
3362 in = (void *)(mod_base + l->fileofs);
3363 if (l->filelen % sizeof(*in))
3364 Host_Error("Mod_Q2BSP_LoadAreaPortals: funny lump size in %s",loadmodel->name);
3365 count = l->filelen / sizeof(*in);
3366 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3369 loadmodel->num = count;
3371 for (i = 0;i < count;i++, in++, out++)
3377 static void Mod_Q2BSP_LoadModels(lump_t *l)
3384 in = (void *)(mod_base + l->fileofs);
3385 if (l->filelen % sizeof(*in))
3386 Host_Error("Mod_Q2BSP_LoadModels: funny lump size in %s",loadmodel->name);
3387 count = l->filelen / sizeof(*in);
3388 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3391 loadmodel->num = count;
3393 for (i = 0;i < count;i++, in++, out++)
3399 void static Mod_Q2BSP_Load(model_t *mod, void *buffer)
3402 q2dheader_t *header;
3404 Host_Error("Mod_Q2BSP_Load: not yet implemented\n");
3406 mod->type = mod_brushq2;
3408 header = (q2dheader_t *)buffer;
3410 i = LittleLong(header->version);
3411 if (i != Q2BSPVERSION)
3412 Host_Error("Mod_Q2BSP_Load: %s has wrong version number (%i, should be %i)", mod->name, i, Q2BSPVERSION);
3413 mod->brush.ishlbsp = false;
3414 if (loadmodel->isworldmodel)
3416 Cvar_SetValue("halflifebsp", mod->brush.ishlbsp);
3417 // until we get a texture for it...
3421 mod_base = (qbyte *)header;
3423 // swap all the lumps
3424 for (i = 0;i < (int) sizeof(*header) / 4;i++)
3425 ((int *)header)[i] = LittleLong(((int *)header)[i]);
3427 // store which lightmap format to use
3428 mod->brushq1.lightmaprgba = r_lightmaprgba.integer;
3430 Mod_Q2BSP_LoadEntities(&header->lumps[Q2LUMP_ENTITIES]);
3431 Mod_Q2BSP_LoadPlanes(&header->lumps[Q2LUMP_PLANES]);
3432 Mod_Q2BSP_LoadVertices(&header->lumps[Q2LUMP_VERTEXES]);
3433 Mod_Q2BSP_LoadVisibility(&header->lumps[Q2LUMP_VISIBILITY]);
3434 Mod_Q2BSP_LoadNodes(&header->lumps[Q2LUMP_NODES]);
3435 Mod_Q2BSP_LoadTexInfo(&header->lumps[Q2LUMP_TEXINFO]);
3436 Mod_Q2BSP_LoadFaces(&header->lumps[Q2LUMP_FACES]);
3437 Mod_Q2BSP_LoadLighting(&header->lumps[Q2LUMP_LIGHTING]);
3438 Mod_Q2BSP_LoadLeafs(&header->lumps[Q2LUMP_LEAFS]);
3439 Mod_Q2BSP_LoadLeafFaces(&header->lumps[Q2LUMP_LEAFFACES]);
3440 Mod_Q2BSP_LoadLeafBrushes(&header->lumps[Q2LUMP_LEAFBRUSHES]);
3441 Mod_Q2BSP_LoadEdges(&header->lumps[Q2LUMP_EDGES]);
3442 Mod_Q2BSP_LoadSurfEdges(&header->lumps[Q2LUMP_SURFEDGES]);
3443 Mod_Q2BSP_LoadBrushes(&header->lumps[Q2LUMP_BRUSHES]);
3444 Mod_Q2BSP_LoadBrushSides(&header->lumps[Q2LUMP_BRUSHSIDES]);
3445 Mod_Q2BSP_LoadAreas(&header->lumps[Q2LUMP_AREAS]);
3446 Mod_Q2BSP_LoadAreaPortals(&header->lumps[Q2LUMP_AREAPORTALS]);
3447 // LordHavoc: must go last because this makes the submodels
3448 Mod_Q2BSP_LoadModels(&header->lumps[Q2LUMP_MODELS]);
3451 static int Mod_Q3BSP_SuperContentsFromNativeContents(model_t *model, int nativecontents);
3452 static int Mod_Q3BSP_NativeContentsFromSuperContents(model_t *model, int supercontents);
3454 static void Mod_Q3BSP_LoadEntities(lump_t *l)
3457 char key[128], value[4096];
3459 loadmodel->brushq3.num_lightgrid_cellsize[0] = 64;
3460 loadmodel->brushq3.num_lightgrid_cellsize[1] = 64;
3461 loadmodel->brushq3.num_lightgrid_cellsize[2] = 128;
3464 loadmodel->brush.entities = Mem_Alloc(loadmodel->mempool, l->filelen);
3465 memcpy(loadmodel->brush.entities, mod_base + l->fileofs, l->filelen);
3466 data = loadmodel->brush.entities;
3467 // some Q3 maps override the lightgrid_cellsize with a worldspawn key
3468 if (data && COM_ParseToken(&data, false) && com_token[0] == '{')
3472 if (!COM_ParseToken(&data, false))
3474 if (com_token[0] == '}')
3475 break; // end of worldspawn
3476 if (com_token[0] == '_')
3477 strcpy(key, com_token + 1);
3479 strcpy(key, com_token);
3480 while (key[strlen(key)-1] == ' ') // remove trailing spaces
3481 key[strlen(key)-1] = 0;
3482 if (!COM_ParseToken(&data, false))
3484 strcpy(value, com_token);
3485 if (!strcmp("gridsize", key))
3487 if (sscanf(value, "%f %f %f", &v[0], &v[1], &v[2]) == 3 && v[0] != 0 && v[1] != 0 && v[2] != 0)
3488 VectorCopy(v, loadmodel->brushq3.num_lightgrid_cellsize);
3494 static void Mod_Q3BSP_LoadTextures(lump_t *l)
3504 char shadername[Q3PATHLENGTH];
3506 in = (void *)(mod_base + l->fileofs);
3507 if (l->filelen % sizeof(*in))
3508 Host_Error("Mod_Q3BSP_LoadTextures: funny lump size in %s",loadmodel->name);
3509 count = l->filelen / sizeof(*in);
3510 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3512 loadmodel->brushq3.data_textures = out;
3513 loadmodel->brushq3.num_textures = count;
3515 for (i = 0;i < count;i++, in++, out++)
3518 strlcpy (out->name, in->name, sizeof (out->name));
3519 out->surfaceflags = LittleLong(in->surfaceflags);
3520 out->nativecontents = LittleLong(in->contents);
3521 out->supercontents = Mod_Q3BSP_SuperContentsFromNativeContents(loadmodel, out->nativecontents);
3522 Mod_LoadSkinFrame(&out->skin, out->name, TEXF_MIPMAP | TEXF_ALPHA | TEXF_PRECACHE, false, true, true);
3523 out->surfaceparms = -1;
3526 // do a quick parse of shader files to get surfaceparms
3527 if ((search = FS_Search("scripts/*.shader", true, false)))
3529 for (i = 0;i < search->numfilenames;i++)
3531 if ((f = FS_LoadFile(search->filenames[i], false)))
3534 while (COM_ParseToken(&text, false))
3536 snprintf(shadername, sizeof(shadername), "%s", com_token);
3538 if (COM_ParseToken(&text, false) && !strcasecmp(com_token, "{"))
3540 while (COM_ParseToken(&text, false))
3542 if (!strcasecmp(com_token, "}"))
3544 else if (!strcasecmp(com_token, "{"))
3546 while (COM_ParseToken(&text, false))
3548 if (!strcasecmp(com_token, "}"))
3552 else if (!strcasecmp(com_token, "surfaceparm"))
3554 if (COM_ParseToken(&text, true) && strcasecmp(com_token, "\n"))
3556 if (!strcasecmp(com_token, "alphashadow"))
3557 flags |= Q3SURFACEPARM_ALPHASHADOW;
3558 else if (!strcasecmp(com_token, "areaportal"))
3559 flags |= Q3SURFACEPARM_AREAPORTAL;
3560 else if (!strcasecmp(com_token, "clusterportal"))
3561 flags |= Q3SURFACEPARM_CLUSTERPORTAL;
3562 else if (!strcasecmp(com_token, "detail"))
3563 flags |= Q3SURFACEPARM_DETAIL;
3564 else if (!strcasecmp(com_token, "donotenter"))
3565 flags |= Q3SURFACEPARM_DONOTENTER;
3566 else if (!strcasecmp(com_token, "fog"))
3567 flags |= Q3SURFACEPARM_FOG;
3568 else if (!strcasecmp(com_token, "lava"))
3569 flags |= Q3SURFACEPARM_LAVA;
3570 else if (!strcasecmp(com_token, "lightfilter"))
3571 flags |= Q3SURFACEPARM_LIGHTFILTER;
3572 else if (!strcasecmp(com_token, "metalsteps"))
3573 flags |= Q3SURFACEPARM_METALSTEPS;
3574 else if (!strcasecmp(com_token, "nodamage"))
3575 flags |= Q3SURFACEPARM_NODAMAGE;
3576 else if (!strcasecmp(com_token, "nodlight"))
3577 flags |= Q3SURFACEPARM_NODLIGHT;
3578 else if (!strcasecmp(com_token, "nodraw"))
3579 flags |= Q3SURFACEPARM_NODRAW;
3580 else if (!strcasecmp(com_token, "nodrop"))
3581 flags |= Q3SURFACEPARM_NODROP;
3582 else if (!strcasecmp(com_token, "noimpact"))
3583 flags |= Q3SURFACEPARM_NOIMPACT;
3584 else if (!strcasecmp(com_token, "nolightmap"))
3585 flags |= Q3SURFACEPARM_NOLIGHTMAP;
3586 else if (!strcasecmp(com_token, "nomarks"))
3587 flags |= Q3SURFACEPARM_NOMARKS;
3588 else if (!strcasecmp(com_token, "nomipmaps"))
3589 flags |= Q3SURFACEPARM_NOMIPMAPS;
3590 else if (!strcasecmp(com_token, "nonsolid"))
3591 flags |= Q3SURFACEPARM_NONSOLID;
3592 else if (!strcasecmp(com_token, "origin"))
3593 flags |= Q3SURFACEPARM_ORIGIN;
3594 else if (!strcasecmp(com_token, "playerclip"))
3595 flags |= Q3SURFACEPARM_PLAYERCLIP;
3596 else if (!strcasecmp(com_token, "sky"))
3597 flags |= Q3SURFACEPARM_SKY;
3598 else if (!strcasecmp(com_token, "slick"))
3599 flags |= Q3SURFACEPARM_SLICK;
3600 else if (!strcasecmp(com_token, "slime"))
3601 flags |= Q3SURFACEPARM_SLIME;
3602 else if (!strcasecmp(com_token, "structural"))
3603 flags |= Q3SURFACEPARM_STRUCTURAL;
3604 else if (!strcasecmp(com_token, "trans"))
3605 flags |= Q3SURFACEPARM_TRANS;
3606 else if (!strcasecmp(com_token, "water"))
3607 flags |= Q3SURFACEPARM_WATER;
3609 Con_Printf("%s parsing warning: unknown surfaceparm \"%s\"\n", search->filenames[i], com_token);
3610 if (!COM_ParseToken(&text, true) || strcasecmp(com_token, "\n"))
3612 Con_Printf("%s parsing error: surfaceparm only takes one parameter.\n", search->filenames[i]);
3618 Con_Printf("%s parsing error: surfaceparm expects a parameter.\n", search->filenames[i]);
3624 // look for linebreak or }
3625 while(COM_ParseToken(&text, true) && strcasecmp(com_token, "\n") && strcasecmp(com_token, "}"));
3626 // break out to top level if it was }
3627 if (!strcasecmp(com_token, "}"))
3631 // add shader to list (shadername and flags)
3632 // actually here we just poke into the texture settings
3633 for (j = 0, out = loadmodel->brushq3.data_textures;j < loadmodel->brushq3.num_textures;j++, out++)
3634 if (!strcasecmp(out->name, shadername))
3635 out->surfaceparms = flags;
3639 Con_Printf("%s parsing error - expected \"{\", found \"%s\"\n", search->filenames[i], com_token);
3650 for (j = 0, out = loadmodel->brushq3.data_textures;j < loadmodel->brushq3.num_textures;j++, out++)
3652 if (out->surfaceparms == -1)
3655 Con_DPrintf("%s: No shader found for texture \"%s\"\n", loadmodel->name, out->name);
3656 out->surfaceparms = 0;
3657 // these are defaults
3658 if (!strcmp(out->name, "caulk") || !strcmp(out->name, "common/caulk") || !strcmp(out->name, "textures/common/caulk")
3659 || !strcmp(out->name, "nodraw") || !strcmp(out->name, "common/nodraw") || !strcmp(out->name, "textures/common/nodraw"))
3660 out->surfaceparms |= Q3SURFACEPARM_NODRAW;
3661 if (!strncmp(out->name, "textures/skies/", 15))
3662 out->surfaceparms |= Q3SURFACEPARM_SKY;
3663 if (R_TextureHasAlpha(out->skin.base))
3664 out->surfaceparms |= Q3SURFACEPARM_TRANS;
3667 Con_DPrintf("%s: %i textures missing shaders\n", loadmodel->name, c);
3670 static void Mod_Q3BSP_LoadPlanes(lump_t *l)
3676 in = (void *)(mod_base + l->fileofs);
3677 if (l->filelen % sizeof(*in))
3678 Host_Error("Mod_Q3BSP_LoadPlanes: funny lump size in %s",loadmodel->name);
3679 count = l->filelen / sizeof(*in);
3680 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3682 loadmodel->brushq3.data_planes = out;
3683 loadmodel->brushq3.num_planes = count;
3685 for (i = 0;i < count;i++, in++, out++)
3687 out->normal[0] = LittleLong(in->normal[0]);
3688 out->normal[1] = LittleLong(in->normal[1]);
3689 out->normal[2] = LittleLong(in->normal[2]);
3690 out->dist = LittleLong(in->dist);
3695 static void Mod_Q3BSP_LoadBrushSides(lump_t *l)
3698 q3mbrushside_t *out;
3701 in = (void *)(mod_base + l->fileofs);
3702 if (l->filelen % sizeof(*in))
3703 Host_Error("Mod_Q3BSP_LoadBrushSides: funny lump size in %s",loadmodel->name);
3704 count = l->filelen / sizeof(*in);
3705 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3707 loadmodel->brushq3.data_brushsides = out;
3708 loadmodel->brushq3.num_brushsides = count;
3710 for (i = 0;i < count;i++, in++, out++)
3712 n = LittleLong(in->planeindex);
3713 if (n < 0 || n >= loadmodel->brushq3.num_planes)
3714 Host_Error("Mod_Q3BSP_LoadBrushSides: invalid planeindex %i (%i planes)\n", n, loadmodel->brushq3.num_planes);
3715 out->plane = loadmodel->brushq3.data_planes + n;
3716 n = LittleLong(in->textureindex);
3717 if (n < 0 || n >= loadmodel->brushq3.num_textures)
3718 Host_Error("Mod_Q3BSP_LoadBrushSides: invalid textureindex %i (%i textures)\n", n, loadmodel->brushq3.num_textures);
3719 out->texture = loadmodel->brushq3.data_textures + n;
3723 static void Mod_Q3BSP_LoadBrushes(lump_t *l)
3727 int i, j, n, c, count, maxplanes;
3729 winding_t *temp1, *temp2;
3731 in = (void *)(mod_base + l->fileofs);
3732 if (l->filelen % sizeof(*in))
3733 Host_Error("Mod_Q3BSP_LoadBrushes: funny lump size in %s",loadmodel->name);
3734 count = l->filelen / sizeof(*in);
3735 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3737 loadmodel->brushq3.data_brushes = out;
3738 loadmodel->brushq3.num_brushes = count;
3740 temp1 = Winding_New(64);
3741 temp2 = Winding_New(64);
3746 for (i = 0;i < count;i++, in++, out++)
3748 n = LittleLong(in->firstbrushside);
3749 c = LittleLong(in->numbrushsides);
3750 if (n < 0 || n + c > loadmodel->brushq3.num_brushsides)
3751 Host_Error("Mod_Q3BSP_LoadBrushes: invalid brushside range %i : %i (%i brushsides)\n", n, n + c, loadmodel->brushq3.num_brushsides);
3752 out->firstbrushside = loadmodel->brushq3.data_brushsides + n;
3753 out->numbrushsides = c;
3754 n = LittleLong(in->textureindex);
3755 if (n < 0 || n >= loadmodel->brushq3.num_textures)
3756 Host_Error("Mod_Q3BSP_LoadBrushes: invalid textureindex %i (%i textures)\n", n, loadmodel->brushq3.num_textures);
3757 out->texture = loadmodel->brushq3.data_textures + n;
3759 // make a list of mplane_t structs to construct a colbrush from
3760 if (maxplanes < out->numbrushsides)
3762 maxplanes = out->numbrushsides;
3765 planes = Mem_Alloc(tempmempool, sizeof(mplane_t) * maxplanes);
3767 for (j = 0;j < out->numbrushsides;j++)
3769 VectorCopy(out->firstbrushside[j].plane->normal, planes[j].normal);
3770 planes[j].dist = out->firstbrushside[j].plane->dist;
3772 // make the colbrush from the planes
3773 out->colbrushf = Collision_NewBrushFromPlanes(loadmodel->mempool, out->numbrushsides, planes, out->texture->supercontents, temp1, temp2);
3777 Winding_Free(temp1);
3778 Winding_Free(temp2);
3781 static void Mod_Q3BSP_LoadEffects(lump_t *l)
3787 in = (void *)(mod_base + l->fileofs);
3788 if (l->filelen % sizeof(*in))
3789 Host_Error("Mod_Q3BSP_LoadEffects: funny lump size in %s",loadmodel->name);
3790 count = l->filelen / sizeof(*in);
3791 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3793 loadmodel->brushq3.data_effects = out;
3794 loadmodel->brushq3.num_effects = count;
3796 for (i = 0;i < count;i++, in++, out++)
3798 strlcpy (out->shadername, in->shadername, sizeof (out->shadername));
3799 n = LittleLong(in->brushindex);
3800 if (n < 0 || n >= loadmodel->brushq3.num_brushes)
3801 Host_Error("Mod_Q3BSP_LoadEffects: invalid brushindex %i (%i brushes)\n", n, loadmodel->brushq3.num_brushes);
3802 out->brush = loadmodel->brushq3.data_brushes + n;
3803 out->unknown = LittleLong(in->unknown);
3807 static void Mod_Q3BSP_LoadVertices(lump_t *l)
3812 in = (void *)(mod_base + l->fileofs);
3813 if (l->filelen % sizeof(*in))
3814 Host_Error("Mod_Q3BSP_LoadVertices: funny lump size in %s",loadmodel->name);
3815 loadmodel->brushq3.num_vertices = count = l->filelen / sizeof(*in);
3816 loadmodel->brushq3.data_vertex3f = Mem_Alloc(loadmodel->mempool, count * (sizeof(float) * (3 + 2 + 2 + 3 + 3 + 3 + 4)));
3817 loadmodel->brushq3.data_texcoordtexture2f = loadmodel->brushq3.data_vertex3f + count * 3;
3818 loadmodel->brushq3.data_texcoordlightmap2f = loadmodel->brushq3.data_texcoordtexture2f + count * 2;
3819 loadmodel->brushq3.data_svector3f = loadmodel->brushq3.data_texcoordlightmap2f + count * 2;
3820 loadmodel->brushq3.data_tvector3f = loadmodel->brushq3.data_svector3f + count * 3;
3821 loadmodel->brushq3.data_normal3f = loadmodel->brushq3.data_tvector3f + count * 3;
3822 loadmodel->brushq3.data_color4f = loadmodel->brushq3.data_normal3f + count * 3;
3824 for (i = 0;i < count;i++, in++)
3826 loadmodel->brushq3.data_vertex3f[i * 3 + 0] = LittleFloat(in->origin3f[0]);
3827 loadmodel->brushq3.data_vertex3f[i * 3 + 1] = LittleFloat(in->origin3f[1]);
3828 loadmodel->brushq3.data_vertex3f[i * 3 + 2] = LittleFloat(in->origin3f[2]);
3829 loadmodel->brushq3.data_texcoordtexture2f[i * 2 + 0] = LittleFloat(in->texcoord2f[0]);
3830 loadmodel->brushq3.data_texcoordtexture2f[i * 2 + 1] = LittleFloat(in->texcoord2f[1]);
3831 loadmodel->brushq3.data_texcoordlightmap2f[i * 2 + 0] = LittleFloat(in->lightmap2f[0]);
3832 loadmodel->brushq3.data_texcoordlightmap2f[i * 2 + 1] = LittleFloat(in->lightmap2f[1]);
3833 // svector/tvector are calculated later in face loading
3834 loadmodel->brushq3.data_svector3f[i * 3 + 0] = 0;
3835 loadmodel->brushq3.data_svector3f[i * 3 + 1] = 0;
3836 loadmodel->brushq3.data_svector3f[i * 3 + 2] = 0;
3837 loadmodel->brushq3.data_tvector3f[i * 3 + 0] = 0;
3838 loadmodel->brushq3.data_tvector3f[i * 3 + 1] = 0;
3839 loadmodel->brushq3.data_tvector3f[i * 3 + 2] = 0;
3840 loadmodel->brushq3.data_normal3f[i * 3 + 0] = LittleFloat(in->normal3f[0]);
3841 loadmodel->brushq3.data_normal3f[i * 3 + 1] = LittleFloat(in->normal3f[1]);
3842 loadmodel->brushq3.data_normal3f[i * 3 + 2] = LittleFloat(in->normal3f[2]);
3843 loadmodel->brushq3.data_color4f[i * 4 + 0] = in->color4ub[0] * (1.0f / 255.0f);
3844 loadmodel->brushq3.data_color4f[i * 4 + 1] = in->color4ub[1] * (1.0f / 255.0f);
3845 loadmodel->brushq3.data_color4f[i * 4 + 2] = in->color4ub[2] * (1.0f / 255.0f);
3846 loadmodel->brushq3.data_color4f[i * 4 + 3] = in->color4ub[3] * (1.0f / 255.0f);
3850 static void Mod_Q3BSP_LoadTriangles(lump_t *l)
3856 in = (void *)(mod_base + l->fileofs);
3857 if (l->filelen % sizeof(int[3]))
3858 Host_Error("Mod_Q3BSP_LoadTriangles: funny lump size in %s",loadmodel->name);
3859 count = l->filelen / sizeof(*in);
3860 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out) * 2);
3862 loadmodel->brushq3.num_triangles = count / 3;
3863 loadmodel->brushq3.data_element3i = out;
3864 loadmodel->brushq3.data_neighbor3i = out + count;
3866 for (i = 0;i < count;i++, in++, out++)
3868 *out = LittleLong(*in);
3869 if (*out < 0 || *out >= loadmodel->brushq3.num_vertices)
3871 Con_Printf("Mod_Q3BSP_LoadTriangles: invalid vertexindex %i (%i vertices), setting to 0\n", *out, loadmodel->brushq3.num_vertices);
3877 static void Mod_Q3BSP_LoadLightmaps(lump_t *l)
3885 in = (void *)(mod_base + l->fileofs);
3886 if (l->filelen % sizeof(*in))
3887 Host_Error("Mod_Q3BSP_LoadLightmaps: funny lump size in %s",loadmodel->name);
3888 count = l->filelen / sizeof(*in);
3889 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3891 loadmodel->brushq3.data_lightmaps = out;
3892 loadmodel->brushq3.num_lightmaps = count;
3894 for (i = 0;i < count;i++, in++, out++)
3895 *out = R_LoadTexture2D(loadmodel->texturepool, va("lightmap%04i", i), 128, 128, in->rgb, TEXTYPE_RGB, TEXF_FORCELINEAR | TEXF_PRECACHE, NULL);
3898 static void Mod_Q3BSP_LoadFaces(lump_t *l)
3902 int i, j, n, count, invalidelements, patchsize[2], finalwidth, finalheight, xlevel, ylevel, row0, row1, x, y, *e, finalvertices, finaltriangles;
3903 //int *originalelement3i;
3904 //int *originalneighbor3i;
3905 float *originalvertex3f;
3906 //float *originalsvector3f;
3907 //float *originaltvector3f;
3908 //float *originalnormal3f;
3909 float *originalcolor4f;
3910 float *originaltexcoordtexture2f;
3911 float *originaltexcoordlightmap2f;
3914 in = (void *)(mod_base + l->fileofs);
3915 if (l->filelen % sizeof(*in))
3916 Host_Error("Mod_Q3BSP_LoadFaces: funny lump size in %s",loadmodel->name);
3917 count = l->filelen / sizeof(*in);
3918 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
3920 loadmodel->brushq3.data_faces = out;
3921 loadmodel->brushq3.num_faces = count;
3923 for (i = 0;i < count;i++, in++, out++)
3925 // check face type first
3926 out->type = LittleLong(in->type);
3927 if (out->type != Q3FACETYPE_POLYGON
3928 && out->type != Q3FACETYPE_PATCH
3929 && out->type != Q3FACETYPE_MESH
3930 && out->type != Q3FACETYPE_FLARE)
3932 Con_DPrintf("Mod_Q3BSP_LoadFaces: face #%i: unknown face type %i\n", i, out->type);
3933 out->num_vertices = 0;
3934 out->num_triangles = 0;
3935 out->type = 0; // error
3939 n = LittleLong(in->textureindex);
3940 if (n < 0 || n >= loadmodel->brushq3.num_textures)
3942 Con_DPrintf("Mod_Q3BSP_LoadFaces: face #%i: invalid textureindex %i (%i textures)\n", i, n, loadmodel->brushq3.num_textures);
3943 out->num_vertices = 0;
3944 out->num_triangles = 0;
3945 out->type = 0; // error
3949 out->texture = loadmodel->brushq3.data_textures + n;
3950 n = LittleLong(in->effectindex);
3951 if (n < -1 || n >= loadmodel->brushq3.num_effects)
3953 Con_DPrintf("Mod_Q3BSP_LoadFaces: face #%i (texture \"%s\"): invalid effectindex %i (%i effects)\n", i, out->texture->name, n, loadmodel->brushq3.num_effects);
3959 out->effect = loadmodel->brushq3.data_effects + n;
3960 n = LittleLong(in->lightmapindex);
3961 if (n < -1 || n >= loadmodel->brushq3.num_lightmaps)
3963 Con_DPrintf("Mod_Q3BSP_LoadFaces: face #%i (texture \"%s\"): invalid lightmapindex %i (%i lightmaps)\n", i, out->texture->name, n, loadmodel->brushq3.num_lightmaps);
3967 out->lightmaptexture = NULL;
3969 out->lightmaptexture = loadmodel->brushq3.data_lightmaps[n];
3971 out->firstvertex = LittleLong(in->firstvertex);
3972 out->num_vertices = LittleLong(in->numvertices);
3973 out->firstelement = LittleLong(in->firstelement);
3974 out->num_triangles = LittleLong(in->numelements) / 3;
3975 if (out->num_triangles * 3 != LittleLong(in->numelements))
3977 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));
3978 out->num_vertices = 0;
3979 out->num_triangles = 0;
3980 out->type = 0; // error
3983 if (out->firstvertex < 0 || out->firstvertex + out->num_vertices > loadmodel->brushq3.num_vertices)
3985 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);
3986 out->num_vertices = 0;
3987 out->num_triangles = 0;
3988 out->type = 0; // error
3991 if (out->firstelement < 0 || out->firstelement + out->num_triangles * 3 > loadmodel->brushq3.num_triangles * 3)
3993 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);
3994 out->num_vertices = 0;
3995 out->num_triangles = 0;
3996 out->type = 0; // error
4001 case Q3FACETYPE_POLYGON:
4002 case Q3FACETYPE_MESH:
4003 // no processing necessary
4004 out->data_vertex3f = loadmodel->brushq3.data_vertex3f + out->firstvertex * 3;
4005 out->data_texcoordtexture2f = loadmodel->brushq3.data_texcoordtexture2f + out->firstvertex * 2;
4006 out->data_texcoordlightmap2f = loadmodel->brushq3.data_texcoordlightmap2f + out->firstvertex * 2;
4007 out->data_svector3f = loadmodel->brushq3.data_svector3f + out->firstvertex * 3;
4008 out->data_tvector3f = loadmodel->brushq3.data_tvector3f + out->firstvertex * 3;
4009 out->data_normal3f = loadmodel->brushq3.data_normal3f + out->firstvertex * 3;
4010 out->data_color4f = loadmodel->brushq3.data_color4f + out->firstvertex * 4;
4011 out->data_element3i = loadmodel->brushq3.data_element3i + out->firstelement;
4012 out->data_neighbor3i = loadmodel->brushq3.data_neighbor3i + out->firstelement;
4014 case Q3FACETYPE_PATCH:
4015 patchsize[0] = LittleLong(in->specific.patch.patchsize[0]);
4016 patchsize[1] = LittleLong(in->specific.patch.patchsize[1]);
4017 if (patchsize[0] < 1 || patchsize[1] < 1)
4019 Con_Printf("Mod_Q3BSP_LoadFaces: face #%i (texture \"%s\"): invalid patchsize %ix%i\n", i, out->texture->name, patchsize[0], patchsize[1]);
4020 out->num_vertices = 0;
4021 out->num_triangles = 0;
4022 out->type = 0; // error
4025 // convert patch to Q3FACETYPE_MESH
4026 xlevel = mod_q3bsp_curves_subdivide_level.integer;
4027 ylevel = mod_q3bsp_curves_subdivide_level.integer;
4028 finalwidth = ((patchsize[0] - 1) << xlevel) + 1;
4029 finalheight = ((patchsize[1] - 1) << ylevel) + 1;
4030 finalvertices = finalwidth * finalheight;
4031 finaltriangles = (finalwidth - 1) * (finalheight - 1) * 2;
4032 originalvertex3f = loadmodel->brushq3.data_vertex3f + out->firstvertex * 3;
4033 //originalsvector3f = loadmodel->brushq3.data_svector3f + out->firstvertex * 3;
4034 //originaltvector3f = loadmodel->brushq3.data_tvector3f + out->firstvertex * 3;
4035 //originalnormal3f = loadmodel->brushq3.data_normal3f + out->firstvertex * 3;
4036 originaltexcoordtexture2f = loadmodel->brushq3.data_texcoordtexture2f + out->firstvertex * 2;
4037 originaltexcoordlightmap2f = loadmodel->brushq3.data_texcoordlightmap2f + out->firstvertex * 2;
4038 originalcolor4f = loadmodel->brushq3.data_color4f + out->firstvertex * 4;
4039 //originalelement3i = loadmodel->brushq3.data_element3i + out->firstelement;
4040 //originalneighbor3i = loadmodel->brushq3.data_neighbor3i + out->firstelement;
4042 originalvertex3f = out->data_vertex3f;
4043 //originalsvector3f = out->data_svector3f;
4044 //originaltvector3f = out->data_tvector3f;
4045 //originalnormal3f = out->data_normal3f;
4046 originalcolor4f = out->data_color4f;
4047 originaltexcoordtexture2f = out->data_texcoordtexture2f;
4048 originaltexcoordlightmap2f = out->data_texcoordlightmap2f;
4049 //originalelement3i = out->data_element3i;
4050 //originalneighbor3i = out->data_neighbor3i;
4052 out->data_vertex3f = Mem_Alloc(loadmodel->mempool, sizeof(float[20]) * finalvertices);
4053 out->data_svector3f = out->data_vertex3f + finalvertices * 3;
4054 out->data_tvector3f = out->data_svector3f + finalvertices * 3;
4055 out->data_normal3f = out->data_tvector3f + finalvertices * 3;
4056 out->data_color4f = out->data_normal3f + finalvertices * 3;
4057 out->data_texcoordtexture2f = out->data_color4f + finalvertices * 4;
4058 out->data_texcoordlightmap2f = out->data_texcoordtexture2f + finalvertices * 2;
4059 out->data_element3i = Mem_Alloc(loadmodel->mempool, sizeof(int[6]) * finaltriangles);
4060 out->data_neighbor3i = out->data_element3i + finaltriangles * 3;
4061 out->type = Q3FACETYPE_MESH;
4062 out->firstvertex = -1;
4063 out->num_vertices = finalvertices;
4064 out->firstelement = -1;
4065 out->num_triangles = finaltriangles;
4066 // generate geometry
4067 // (note: normals are skipped because they get recalculated)
4068 QuadraticSplinePatchSubdivideFloatBuffer(patchsize[0], patchsize[1], xlevel, ylevel, 3, originalvertex3f, out->data_vertex3f);
4069 QuadraticSplinePatchSubdivideFloatBuffer(patchsize[0], patchsize[1], xlevel, ylevel, 2, originaltexcoordtexture2f, out->data_texcoordtexture2f);
4070 QuadraticSplinePatchSubdivideFloatBuffer(patchsize[0], patchsize[1], xlevel, ylevel, 2, originaltexcoordlightmap2f, out->data_texcoordlightmap2f);
4071 QuadraticSplinePatchSubdivideFloatBuffer(patchsize[0], patchsize[1], xlevel, ylevel, 4, originalcolor4f, out->data_color4f);
4072 // generate elements
4073 e = out->data_element3i;
4074 for (y = 0;y < finalheight - 1;y++)
4076 row0 = (y + 0) * finalwidth;
4077 row1 = (y + 1) * finalwidth;
4078 for (x = 0;x < finalwidth - 1;x++)
4090 out->num_triangles = Mod_RemoveDegenerateTriangles(out->num_triangles, out->data_element3i, out->data_element3i, out->data_vertex3f);
4091 if (developer.integer)
4093 if (out->num_triangles < finaltriangles)
4094 Con_Printf("Mod_Q3BSP_LoadFaces: %ix%i curve subdivided to %i vertices / %i triangles, %i degenerate triangles removed (leaving %i)\n", patchsize[0], patchsize[1], out->num_vertices, finaltriangles, finaltriangles - out->num_triangles, out->num_triangles);
4096 Con_Printf("Mod_Q3BSP_LoadFaces: %ix%i curve subdivided to %i vertices / %i triangles\n", patchsize[0], patchsize[1], out->num_vertices, out->num_triangles);
4098 // q3map does not put in collision brushes for curves... ugh
4099 out->collisions = true;
4101 case Q3FACETYPE_FLARE:
4102 Con_DPrintf("Mod_Q3BSP_LoadFaces: face #%i (texture \"%s\"): Q3FACETYPE_FLARE not supported (yet)\n", i, out->texture->name);
4104 out->num_vertices = 0;
4105 out->num_triangles = 0;
4109 for (j = 0, invalidelements = 0;j < out->num_triangles * 3;j++)
4110 if (out->data_element3i[j] < 0 || out->data_element3i[j] >= out->num_vertices)
4112 if (invalidelements)
4114 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);
4115 for (j = 0;j < out->num_triangles * 3;j++)
4117 Con_Printf(" %i", out->data_element3i[j]);
4118 if (out->data_element3i[j] < 0 || out->data_element3i[j] >= out->num_vertices)
4119 out->data_element3i[j] = 0;
4123 // for shadow volumes
4124 Mod_BuildTriangleNeighbors(out->data_neighbor3i, out->data_element3i, out->num_triangles);
4125 // for per pixel lighting
4126 Mod_BuildTextureVectorsAndNormals(out->num_vertices, out->num_triangles, out->data_vertex3f, out->data_texcoordtexture2f, out->data_element3i, out->data_svector3f, out->data_tvector3f, out->data_normal3f);
4127 // calculate a bounding box
4128 VectorClear(out->mins);
4129 VectorClear(out->maxs);
4130 if (out->num_vertices)
4132 VectorCopy(out->data_vertex3f, out->mins);
4133 VectorCopy(out->data_vertex3f, out->maxs);
4134 for (j = 1, v = out->data_vertex3f + 3;j < out->num_vertices;j++, v += 3)
4136 out->mins[0] = min(out->mins[0], v[0]);
4137 out->maxs[0] = max(out->maxs[0], v[0]);
4138 out->mins[1] = min(out->mins[1], v[1]);
4139 out->maxs[1] = max(out->maxs[1], v[1]);
4140 out->mins[2] = min(out->mins[2], v[2]);
4141 out->maxs[2] = max(out->maxs[2], v[2]);
4143 out->mins[0] -= 1.0f;
4144 out->mins[1] -= 1.0f;
4145 out->mins[2] -= 1.0f;
4146 out->maxs[0] += 1.0f;
4147 out->maxs[1] += 1.0f;
4148 out->maxs[2] += 1.0f;
4152 // LordHavoc: experimental array merger (disabled because it wastes time and uses 2x memory while merging)
4155 int totalverts, totaltris;
4156 int originalnum_vertices;
4157 float *originaldata_vertex3f;
4158 float *originaldata_texcoordtexture2f;
4159 float *originaldata_texcoordlightmap2f;
4160 float *originaldata_svector3f;
4161 float *originaldata_tvector3f;
4162 float *originaldata_normal3f;
4163 float *originaldata_color4f;
4164 int originalnum_triangles;
4165 int *originaldata_element3i;
4166 int *originaldata_neighbor3i;
4170 for (i = 0, out = loadmodel->brushq3.data_faces;i < count;i++, out++)
4174 totalverts += out->num_vertices;
4175 totaltris += out->num_triangles;
4178 originalnum_vertices = loadmodel->brushq3.num_vertices;
4179 originaldata_vertex3f = loadmodel->brushq3.data_vertex3f;
4180 originaldata_texcoordtexture2f = loadmodel->brushq3.data_texcoordtexture2f;
4181 originaldata_texcoordlightmap2f = loadmodel->brushq3.data_texcoordlightmap2f;
4182 originaldata_svector3f = loadmodel->brushq3.data_svector3f;
4183 originaldata_tvector3f = loadmodel->brushq3.data_tvector3f;
4184 originaldata_normal3f = loadmodel->brushq3.data_normal3f;
4185 originaldata_color4f = loadmodel->brushq3.data_color4f;
4186 originalnum_triangles = loadmodel->brushq3.num_triangles;
4187 originaldata_element3i = loadmodel->brushq3.data_element3i;
4188 originaldata_neighbor3i = loadmodel->brushq3.data_neighbor3i;
4189 loadmodel->brushq3.num_vertices = totalverts;
4190 loadmodel->brushq3.data_vertex3f = Mem_Alloc(loadmodel->mempool, totalverts * (sizeof(float) * (3 + 2 + 2 + 3 + 3 + 3 + 4)) + totaltris * (sizeof(int) * (3 * 2)));
4191 loadmodel->brushq3.data_texcoordtexture2f = loadmodel->brushq3.data_vertex3f + totalverts * 3;
4192 loadmodel->brushq3.data_texcoordlightmap2f = loadmodel->brushq3.data_texcoordtexture2f + totalverts * 2;
4193 loadmodel->brushq3.data_svector3f = loadmodel->brushq3.data_texcoordlightmap2f + totalverts * 2;
4194 loadmodel->brushq3.data_tvector3f = loadmodel->brushq3.data_svector3f + totalverts * 3;
4195 loadmodel->brushq3.data_normal3f = loadmodel->brushq3.data_tvector3f + totalverts * 3;
4196 loadmodel->brushq3.data_color4f = loadmodel->brushq3.data_normal3f + totalverts * 3;
4197 loadmodel->brushq3.num_triangles = totaltris;
4198 loadmodel->brushq3.data_element3i = (int *)(loadmodel->brushq3.data_color4f + totalverts * 4);
4199 loadmodel->brushq3.data_neighbor3i = loadmodel->brushq3.data_element3i + totaltris * 3;
4202 for (i = 0, out = loadmodel->brushq3.data_faces;i < count;i++, out++)
4206 Con_Printf("totalverts %i, totaltris %i\n", totalverts, totaltris);
4207 memcpy(loadmodel->brushq3.data_vertex3f + totalverts * 3, out->data_vertex3f, out->num_vertices * 3 * sizeof(float));
4208 memcpy(loadmodel->brushq3.data_texcoordtexture2f + totalverts * 2, out->data_texcoordtexture2f, out->num_vertices * 2 * sizeof(float));
4209 memcpy(loadmodel->brushq3.data_texcoordlightmap2f + totalverts * 2, out->data_texcoordlightmap2f, out->num_vertices * 2 * sizeof(float));
4210 memcpy(loadmodel->brushq3.data_svector3f + totalverts * 3, out->data_svector3f, out->num_vertices * 3 * sizeof(float));
4211 memcpy(loadmodel->brushq3.data_tvector3f + totalverts * 3, out->data_tvector3f, out->num_vertices * 3 * sizeof(float));
4212 memcpy(loadmodel->brushq3.data_normal3f + totalverts * 3, out->data_normal3f, out->num_vertices * 3 * sizeof(float));
4213 memcpy(loadmodel->brushq3.data_color4f + totalverts * 4, out->data_color4f, out->num_vertices * 4 * sizeof(float));
4214 memcpy(loadmodel->brushq3.data_element3i + totaltris * 3, out->data_element3i, out->num_triangles * 3 * sizeof(int));
4215 memcpy(loadmodel->brushq3.data_neighbor3i + totaltris * 3, out->data_neighbor3i, out->num_triangles * 3 * sizeof(int));
4216 if (out->firstvertex == -1)
4217 Mem_Free(out->data_vertex3f);
4218 if (out->firstelement == -1)
4219 Mem_Free(out->data_element3i);
4220 out->firstvertex = totalverts;
4221 out->data_vertex3f = loadmodel->brushq3.data_vertex3f + out->firstvertex * 3;
4222 out->data_texcoordtexture2f = loadmodel->brushq3.data_texcoordtexture2f + out->firstvertex * 2;
4223 out->data_texcoordlightmap2f = loadmodel->brushq3.data_texcoordlightmap2f + out->firstvertex * 2;
4224 out->data_svector3f = loadmodel->brushq3.data_svector3f + out->firstvertex * 3;
4225 out->data_tvector3f = loadmodel->brushq3.data_tvector3f + out->firstvertex * 3;
4226 out->data_normal3f = loadmodel->brushq3.data_normal3f + out->firstvertex * 3;
4227 out->data_color4f = loadmodel->brushq3.data_color4f + out->firstvertex * 4;
4228 out->firstelement = totaltris * 3;
4229 out->data_element3i = loadmodel->brushq3.data_element3i + out->firstelement;
4230 out->data_neighbor3i = loadmodel->brushq3.data_neighbor3i + out->firstelement;
4231 //for (j = 0;j < out->numtriangles * 3;j++)
4232 // out->data_element3i[j] += totalverts - out->firstvertex;
4233 totalverts += out->num_vertices;
4234 totaltris += out->num_triangles;
4236 Mem_Free(originaldata_vertex3f);
4237 Mem_Free(originaldata_element3i);
4242 static void Mod_Q3BSP_LoadModels(lump_t *l)
4246 int i, j, n, c, count;
4248 in = (void *)(mod_base + l->fileofs);
4249 if (l->filelen % sizeof(*in))
4250 Host_Error("Mod_Q3BSP_LoadModels: funny lump size in %s",loadmodel->name);
4251 count = l->filelen / sizeof(*in);
4252 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
4254 loadmodel->brushq3.data_models = out;
4255 loadmodel->brushq3.num_models = count;
4257 for (i = 0;i < count;i++, in++, out++)
4259 for (j = 0;j < 3;j++)
4261 out->mins[j] = LittleFloat(in->mins[j]);
4262 out->maxs[j] = LittleFloat(in->maxs[j]);
4264 n = LittleLong(in->firstface);
4265 c = LittleLong(in->numfaces);
4266 if (n < 0 || n + c > loadmodel->brushq3.num_faces)
4267 Host_Error("Mod_Q3BSP_LoadModels: invalid face range %i : %i (%i faces)\n", n, n + c, loadmodel->brushq3.num_faces);
4268 out->firstface = loadmodel->brushq3.data_faces + n;
4270 n = LittleLong(in->firstbrush);
4271 c = LittleLong(in->numbrushes);
4272 if (n < 0 || n + c > loadmodel->brushq3.num_brushes)
4273 Host_Error("Mod_Q3BSP_LoadModels: invalid brush range %i : %i (%i brushes)\n", n, n + c, loadmodel->brushq3.num_brushes);
4274 out->firstbrush = loadmodel->brushq3.data_brushes + n;
4275 out->numbrushes = c;
4279 static void Mod_Q3BSP_LoadLeafBrushes(lump_t *l)
4285 in = (void *)(mod_base + l->fileofs);
4286 if (l->filelen % sizeof(*in))
4287 Host_Error("Mod_Q3BSP_LoadLeafBrushes: funny lump size in %s",loadmodel->name);
4288 count = l->filelen / sizeof(*in);
4289 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
4291 loadmodel->brushq3.data_leafbrushes = out;
4292 loadmodel->brushq3.num_leafbrushes = count;
4294 for (i = 0;i < count;i++, in++, out++)
4296 n = LittleLong(*in);
4297 if (n < 0 || n >= loadmodel->brushq3.num_brushes)
4298 Host_Error("Mod_Q3BSP_LoadLeafBrushes: invalid brush index %i (%i brushes)\n", n, loadmodel->brushq3.num_brushes);
4299 *out = loadmodel->brushq3.data_brushes + n;
4303 static void Mod_Q3BSP_LoadLeafFaces(lump_t *l)
4309 in = (void *)(mod_base + l->fileofs);
4310 if (l->filelen % sizeof(*in))
4311 Host_Error("Mod_Q3BSP_LoadLeafFaces: funny lump size in %s",loadmodel->name);
4312 count = l->filelen / sizeof(*in);
4313 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
4315 loadmodel->brushq3.data_leaffaces = out;
4316 loadmodel->brushq3.num_leaffaces = count;
4318 for (i = 0;i < count;i++, in++, out++)
4320 n = LittleLong(*in);
4321 if (n < 0 || n >= loadmodel->brushq3.num_faces)
4322 Host_Error("Mod_Q3BSP_LoadLeafFaces: invalid face index %i (%i faces)\n", n, loadmodel->brushq3.num_faces);
4323 *out = loadmodel->brushq3.data_faces + n;
4327 static void Mod_Q3BSP_LoadLeafs(lump_t *l)
4331 int i, j, n, c, count;
4333 in = (void *)(mod_base + l->fileofs);
4334 if (l->filelen % sizeof(*in))
4335 Host_Error("Mod_Q3BSP_LoadLeafs: funny lump size in %s",loadmodel->name);
4336 count = l->filelen / sizeof(*in);
4337 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
4339 loadmodel->brushq3.data_leafs = out;
4340 loadmodel->brushq3.num_leafs = count;
4342 for (i = 0;i < count;i++, in++, out++)
4346 out->clusterindex = LittleLong(in->clusterindex);
4347 out->areaindex = LittleLong(in->areaindex);
4348 for (j = 0;j < 3;j++)
4350 // yes the mins/maxs are ints
4351 out->mins[j] = LittleLong(in->mins[j]) - 1;
4352 out->maxs[j] = LittleLong(in->maxs[j]) + 1;
4354 n = LittleLong(in->firstleafface);
4355 c = LittleLong(in->numleaffaces);
4356 if (n < 0 || n + c > loadmodel->brushq3.num_leaffaces)
4357 Host_Error("Mod_Q3BSP_LoadLeafs: invalid leafface range %i : %i (%i leaffaces)\n", n, n + c, loadmodel->brushq3.num_leaffaces);
4358 out->firstleafface = loadmodel->brushq3.data_leaffaces + n;
4359 out->numleaffaces = c;
4360 n = LittleLong(in->firstleafbrush);
4361 c = LittleLong(in->numleafbrushes);
4362 if (n < 0 || n + c > loadmodel->brushq3.num_leafbrushes)
4363 Host_Error("Mod_Q3BSP_LoadLeafs: invalid leafbrush range %i : %i (%i leafbrushes)\n", n, n + c, loadmodel->brushq3.num_leafbrushes);
4364 out->firstleafbrush = loadmodel->brushq3.data_leafbrushes + n;
4365 out->numleafbrushes = c;
4369 static void Mod_Q3BSP_LoadNodes_RecursiveSetParent(q3mnode_t *node, q3mnode_t *parent)
4372 Host_Error("Mod_Q3BSP_LoadNodes_RecursiveSetParent: runaway recursion\n");
4373 node->parent = parent;
4376 Mod_Q3BSP_LoadNodes_RecursiveSetParent(node->children[0], node);
4377 Mod_Q3BSP_LoadNodes_RecursiveSetParent(node->children[1], node);
4381 static void Mod_Q3BSP_LoadNodes(lump_t *l)
4387 in = (void *)(mod_base + l->fileofs);
4388 if (l->filelen % sizeof(*in))
4389 Host_Error("Mod_Q3BSP_LoadNodes: funny lump size in %s",loadmodel->name);
4390 count = l->filelen / sizeof(*in);
4391 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
4393 loadmodel->brushq3.data_nodes = out;
4394 loadmodel->brushq3.num_nodes = count;
4396 for (i = 0;i < count;i++, in++, out++)
4399 n = LittleLong(in->planeindex);
4400 if (n < 0 || n >= loadmodel->brushq3.num_planes)
4401 Host_Error("Mod_Q3BSP_LoadNodes: invalid planeindex %i (%i planes)\n", n, loadmodel->brushq3.num_planes);
4402 out->plane = loadmodel->brushq3.data_planes + n;
4403 for (j = 0;j < 2;j++)
4405 n = LittleLong(in->childrenindex[j]);
4408 if (n >= loadmodel->brushq3.num_nodes)
4409 Host_Error("Mod_Q3BSP_LoadNodes: invalid child node index %i (%i nodes)\n", n, loadmodel->brushq3.num_nodes);
4410 out->children[j] = loadmodel->brushq3.data_nodes + n;
4415 if (n >= loadmodel->brushq3.num_leafs)
4416 Host_Error("Mod_Q3BSP_LoadNodes: invalid child leaf index %i (%i leafs)\n", n, loadmodel->brushq3.num_leafs);
4417 out->children[j] = (q3mnode_t *)(loadmodel->brushq3.data_leafs + n);
4420 for (j = 0;j < 3;j++)
4422 // yes the mins/maxs are ints
4423 out->mins[j] = LittleLong(in->mins[j]) - 1;
4424 out->maxs[j] = LittleLong(in->maxs[j]) + 1;
4428 // set the parent pointers
4429 Mod_Q3BSP_LoadNodes_RecursiveSetParent(loadmodel->brushq3.data_nodes, NULL);
4432 static void Mod_Q3BSP_LoadLightGrid(lump_t *l)
4435 q3dlightgrid_t *out;
4438 in = (void *)(mod_base + l->fileofs);
4439 if (l->filelen % sizeof(*in))
4440 Host_Error("Mod_Q3BSP_LoadLightGrid: funny lump size in %s",loadmodel->name);
4441 loadmodel->brushq3.num_lightgrid_scale[0] = 1.0f / loadmodel->brushq3.num_lightgrid_cellsize[0];
4442 loadmodel->brushq3.num_lightgrid_scale[1] = 1.0f / loadmodel->brushq3.num_lightgrid_cellsize[1];
4443 loadmodel->brushq3.num_lightgrid_scale[2] = 1.0f / loadmodel->brushq3.num_lightgrid_cellsize[2];
4444 loadmodel->brushq3.num_lightgrid_imins[0] = ceil(loadmodel->brushq3.data_models->mins[0] * loadmodel->brushq3.num_lightgrid_scale[0]);
4445 loadmodel->brushq3.num_lightgrid_imins[1] = ceil(loadmodel->brushq3.data_models->mins[1] * loadmodel->brushq3.num_lightgrid_scale[1]);
4446 loadmodel->brushq3.num_lightgrid_imins[2] = ceil(loadmodel->brushq3.data_models->mins[2] * loadmodel->brushq3.num_lightgrid_scale[2]);
4447 loadmodel->brushq3.num_lightgrid_imaxs[0] = floor(loadmodel->brushq3.data_models->maxs[0] * loadmodel->brushq3.num_lightgrid_scale[0]);
4448 loadmodel->brushq3.num_lightgrid_imaxs[1] = floor(loadmodel->brushq3.data_models->maxs[1] * loadmodel->brushq3.num_lightgrid_scale[1]);
4449 loadmodel->brushq3.num_lightgrid_imaxs[2] = floor(loadmodel->brushq3.data_models->maxs[2] * loadmodel->brushq3.num_lightgrid_scale[2]);
4450 loadmodel->brushq3.num_lightgrid_isize[0] = loadmodel->brushq3.num_lightgrid_imaxs[0] - loadmodel->brushq3.num_lightgrid_imins[0] + 1;
4451 loadmodel->brushq3.num_lightgrid_isize[1] = loadmodel->brushq3.num_lightgrid_imaxs[1] - loadmodel->brushq3.num_lightgrid_imins[1] + 1;
4452 loadmodel->brushq3.num_lightgrid_isize[2] = loadmodel->brushq3.num_lightgrid_imaxs[2] - loadmodel->brushq3.num_lightgrid_imins[2] + 1;
4453 count = loadmodel->brushq3.num_lightgrid_isize[0] * loadmodel->brushq3.num_lightgrid_isize[1] * loadmodel->brushq3.num_lightgrid_isize[2];
4456 if (l->filelen < count * (int)sizeof(*in))
4457 Host_Error("Mod_Q3BSP_LoadLightGrid: invalid lightgrid lump size %i bytes, should be %i bytes (%ix%ix%i)\n", l->filelen, count * sizeof(*in), loadmodel->brushq3.num_lightgrid_dimensions[0], loadmodel->brushq3.num_lightgrid_dimensions[1], loadmodel->brushq3.num_lightgrid_dimensions[2]);
4458 if (l->filelen != count * (int)sizeof(*in))
4459 Con_Printf("Mod_Q3BSP_LoadLightGrid: Warning: calculated lightgrid size %i bytes does not match lump size %i\n", count * sizeof(*in), l->filelen);
4462 out = Mem_Alloc(loadmodel->mempool, count * sizeof(*out));
4463 loadmodel->brushq3.data_lightgrid = out;
4464 loadmodel->brushq3.num_lightgrid = count;
4466 // no swapping or validation necessary
4468 memcpy(out, in, count * (int)sizeof(*out));
4471 // no data, fill with white
4473 for (i = 0;i < count;i++)
4475 out[i].ambientrgb[0] = 128;
4476 out[i].ambientrgb[1] = 128;
4477 out[i].ambientrgb[2] = 128;
4478 out[i].diffusergb[0] = 0;
4479 out[i].diffusergb[1] = 0;
4480 out[i].diffusergb[2] = 0;
4481 out[i].diffusepitch = 0;
4482 out[i].diffuseyaw = 0;
4486 Matrix4x4_CreateScale3(&loadmodel->brushq3.num_lightgrid_indexfromworld, loadmodel->brushq3.num_lightgrid_scale[0], loadmodel->brushq3.num_lightgrid_scale[1], loadmodel->brushq3.num_lightgrid_scale[2]);
4487 Matrix4x4_ConcatTranslate(&loadmodel->brushq3.num_lightgrid_indexfromworld, -loadmodel->brushq3.num_lightgrid_imins[0] * loadmodel->brushq3.num_lightgrid_cellsize[0], -loadmodel->brushq3.num_lightgrid_imins[1] * loadmodel->brushq3.num_lightgrid_cellsize[1], -loadmodel->brushq3.num_lightgrid_imins[2] * loadmodel->brushq3.num_lightgrid_cellsize[2]);
4490 static void Mod_Q3BSP_LoadPVS(lump_t *l)
4495 if (l->filelen == 0)
4498 // unvised maps often have cluster indices even without pvs, so check
4499 // leafs to find real number of clusters
4500 loadmodel->brush.num_pvsclusters = 1;
4501 for (i = 0;i < loadmodel->brushq3.num_leafs;i++)
4502 loadmodel->brush.num_pvsclusters = min(loadmodel->brush.num_pvsclusters, loadmodel->brushq3.data_leafs[i].clusterindex + 1);
4505 loadmodel->brush.num_pvsclusterbytes = (loadmodel->brush.num_pvsclusters + 7) / 8;
4506 totalchains = loadmodel->brush.num_pvsclusterbytes * loadmodel->brush.num_pvsclusters;
4507 loadmodel->brush.data_pvsclusters = Mem_Alloc(loadmodel->mempool, totalchains);
4508 memset(loadmodel->brush.data_pvsclusters, 0xFF, totalchains);
4512 in = (void *)(mod_base + l->fileofs);
4514 Host_Error("Mod_Q3BSP_LoadPVS: funny lump size in %s",loadmodel->name);
4516 loadmodel->brush.num_pvsclusters = LittleLong(in->numclusters);
4517 loadmodel->brush.num_pvsclusterbytes = LittleLong(in->chainlength);
4518 if (loadmodel->brush.num_pvsclusterbytes < ((loadmodel->brush.num_pvsclusters + 7) / 8))
4519 Host_Error("Mod_Q3BSP_LoadPVS: (chainlength = %i) < ((numclusters = %i) + 7) / 8\n", loadmodel->brush.num_pvsclusterbytes, loadmodel->brush.num_pvsclusters);
4520 totalchains = loadmodel->brush.num_pvsclusterbytes * loadmodel->brush.num_pvsclusters;
4521 if (l->filelen < totalchains + (int)sizeof(*in))
4522 Host_Error("Mod_Q3BSP_LoadPVS: lump too small ((numclusters = %i) * (chainlength = %i) + sizeof(q3dpvs_t) == %i bytes, lump is %i bytes)\n", loadmodel->brush.num_pvsclusters, loadmodel->brush.num_pvsclusterbytes, totalchains + sizeof(*in), l->filelen);
4524 loadmodel->brush.data_pvsclusters = Mem_Alloc(loadmodel->mempool, totalchains);
4525 memcpy(loadmodel->brush.data_pvsclusters, (qbyte *)(in + 1), totalchains);
4528 static void Mod_Q3BSP_FindNonSolidLocation(model_t *model, const vec3_t in, vec3_t out, vec_t radius)
4530 // FIXME: finish this code
4531 VectorCopy(in, out);
4534 static void Mod_Q3BSP_LightPoint(model_t *model, const vec3_t p, vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal)
4536 int i, j, k, index[3];
4537 float transformed[3], blend1, blend2, blend, yaw, pitch, sinpitch;
4538 q3dlightgrid_t *a, *s;
4539 // FIXME: write this
4540 if (!model->brushq3.num_lightgrid)
4542 ambientcolor[0] = 1;
4543 ambientcolor[1] = 1;
4544 ambientcolor[2] = 1;
4547 Matrix4x4_Transform(&model->brushq3.num_lightgrid_indexfromworld, p, transformed);
4548 //Matrix4x4_Print(&model->brushq3.num_lightgrid_indexfromworld);
4549 //Con_Printf("%f %f %f transformed %f %f %f clamped ", p[0], p[1], p[2], transformed[0], transformed[1], transformed[2]);
4550 transformed[0] = bound(0, transformed[0], model->brushq3.num_lightgrid_isize[0] - 1);
4551 transformed[1] = bound(0, transformed[1], model->brushq3.num_lightgrid_isize[1] - 1);
4552 transformed[2] = bound(0, transformed[2], model->brushq3.num_lightgrid_isize[2] - 1);
4553 index[0] = (int)floor(transformed[0]);
4554 index[1] = (int)floor(transformed[1]);
4555 index[2] = (int)floor(transformed[2]);
4556 //Con_Printf("%f %f %f index %i %i %i:\n", transformed[0], transformed[1], transformed[2], index[0], index[1], index[2]);
4557 // now lerp the values
4558 VectorClear(diffusenormal);
4559 a = &model->brushq3.data_lightgrid[(index[2] * model->brushq3.num_lightgrid_isize[1] + index[1]) * model->brushq3.num_lightgrid_isize[0] + index[0]];
4560 for (k = 0;k < 2;k++)
4562 blend1 = (k ? (transformed[2] - index[2]) : (1 - (transformed[2] - index[2])));
4563 if (blend1 < 0.001f || index[2] + k >= model->brushq3.num_lightgrid_isize[2])
4565 for (j = 0;j < 2;j++)
4567 blend2 = blend1 * (j ? (transformed[1] - index[1]) : (1 - (transformed[1] - index[1])));
4568 if (blend2 < 0.001f || index[1] + j >= model->brushq3.num_lightgrid_isize[1])
4570 for (i = 0;i < 2;i++)
4572 blend = blend2 * (i ? (transformed[0] - index[0]) : (1 - (transformed[0] - index[0])));
4573 if (blend < 0.001f || index[0] + i >= model->brushq3.num_lightgrid_isize[0])
4575 s = a + (k * model->brushq3.num_lightgrid_isize[1] + j) * model->brushq3.num_lightgrid_isize[0] + i;
4576 VectorMA(ambientcolor, blend * (1.0f / 128.0f), s->ambientrgb, ambientcolor);
4577 VectorMA(diffusecolor, blend * (1.0f / 128.0f), s->diffusergb, diffusecolor);
4578 pitch = s->diffusepitch * M_PI / 128;
4579 yaw = s->diffuseyaw * M_PI / 128;
4580 sinpitch = sin(pitch);
4581 diffusenormal[0] += blend * (cos(yaw) * sinpitch);
4582 diffusenormal[1] += blend * (sin(yaw) * sinpitch);
4583 diffusenormal[2] += blend * (cos(pitch));
4584 //Con_Printf("blend %f: ambient %i %i %i, diffuse %i %i %i, diffusepitch %i diffuseyaw %i (%f %f, normal %f %f %f)\n", blend, s->ambientrgb[0], s->ambientrgb[1], s->ambientrgb[2], s->diffusergb[0], s->diffusergb[1], s->diffusergb[2], s->diffusepitch, s->diffuseyaw, pitch, yaw, (cos(yaw) * cospitch), (sin(yaw) * cospitch), (-sin(pitch)));
4588 VectorNormalize(diffusenormal);
4589 //Con_Printf("result: ambient %f %f %f diffuse %f %f %f diffusenormal %f %f %f\n", ambientcolor[0], ambientcolor[1], ambientcolor[2], diffusecolor[0], diffusecolor[1], diffusecolor[2], diffusenormal[0], diffusenormal[1], diffusenormal[2]);
4592 static void Mod_Q3BSP_TracePoint_RecursiveBSPNode(trace_t *trace, q3mnode_t *node, const vec3_t point, int markframe)
4597 // find which leaf the point is in
4599 node = node->children[DotProduct(point, node->plane->normal) < node->plane->dist];
4600 // point trace the brushes
4601 leaf = (q3mleaf_t *)node;
4602 for (i = 0;i < leaf->numleafbrushes;i++)
4604 brush = leaf->firstleafbrush[i]->colbrushf;
4605 if (brush && brush->markframe != markframe && BoxesOverlap(point, point, brush->mins, brush->maxs))
4607 brush->markframe = markframe;
4608 Collision_TracePointBrushFloat(trace, point, leaf->firstleafbrush[i]->colbrushf);
4611 // can't do point traces on curves (they have no thickness)
4614 static void Mod_Q3BSP_TraceLine_RecursiveBSPNode(trace_t *trace, q3mnode_t *node, const vec3_t start, const vec3_t end, vec_t startfrac, vec_t endfrac, const vec3_t linestart, const vec3_t lineend, int markframe, const vec3_t segmentmins, const vec3_t segmentmaxs)
4616 int i, startside, endside;
4617 float dist1, dist2, midfrac, mid[3], nodesegmentmins[3], nodesegmentmaxs[3];
4621 if (startfrac > trace->realfraction)
4623 // note: all line fragments past first impact fraction are ignored
4624 if (VectorCompare(start, end))
4626 // find which leaf the point is in
4628 node = node->children[DotProduct(start, node->plane->normal) < node->plane->dist];
4632 // find which nodes the line is in and recurse for them
4635 // recurse down node sides
4636 dist1 = PlaneDiff(start, node->plane);
4637 dist2 = PlaneDiff(end, node->plane);
4638 startside = dist1 < 0;
4639 endside = dist2 < 0;
4640 if (startside == endside)
4642 // most of the time the line fragment is on one side of the plane
4643 node = node->children[startside];
4647 // line crosses node plane, split the line
4648 midfrac = dist1 / (dist1 - dist2);
4649 VectorLerp(start, midfrac, end, mid);
4650 // take the near side first
4651 Mod_Q3BSP_TraceLine_RecursiveBSPNode(trace, node->children[startside], start, mid, startfrac, midfrac, linestart, lineend, markframe, segmentmins, segmentmaxs);
4652 if (midfrac <= trace->realfraction)
4653 Mod_Q3BSP_TraceLine_RecursiveBSPNode(trace, node->children[endside], mid, end, midfrac, endfrac, linestart, lineend, markframe, segmentmins, segmentmaxs);
4659 nodesegmentmins[0] = min(start[0], end[0]);
4660 nodesegmentmins[1] = min(start[1], end[1]);
4661 nodesegmentmins[2] = min(start[2], end[2]);
4662 nodesegmentmaxs[0] = max(start[0], end[0]);
4663 nodesegmentmaxs[1] = max(start[1], end[1]);
4664 nodesegmentmaxs[2] = max(start[2], end[2]);
4665 // line trace the brushes
4666 leaf = (q3mleaf_t *)node;
4667 for (i = 0;i < leaf->numleafbrushes;i++)
4669 brush = leaf->firstleafbrush[i]->colbrushf;
4670 if (brush && brush->markframe != markframe && BoxesOverlap(nodesegmentmins, nodesegmentmaxs, brush->mins, brush->maxs))
4672 brush->markframe = markframe;
4673 Collision_TraceLineBrushFloat(trace, linestart, lineend, leaf->firstleafbrush[i]->colbrushf, leaf->firstleafbrush[i]->colbrushf);
4674 if (startfrac > trace->realfraction)
4678 // can't do point traces on curves (they have no thickness)
4679 if (mod_q3bsp_curves_collisions.integer && !VectorCompare(start, end))
4681 // line trace the curves
4682 for (i = 0;i < leaf->numleaffaces;i++)
4684 face = leaf->firstleafface[i];
4685 if (face->collisions && face->collisionmarkframe != markframe && BoxesOverlap(nodesegmentmins, nodesegmentmaxs, face->mins, face->maxs))
4687 face->collisionmarkframe = markframe;
4688 Collision_TraceLineTriangleMeshFloat(trace, linestart, lineend, face->num_triangles, face->data_element3i, face->data_vertex3f, face->texture->supercontents, segmentmins, segmentmaxs);
4689 if (startfrac > trace->realfraction)
4696 static void Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace_t *trace, q3mnode_t *node, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int markframe, const vec3_t segmentmins, const vec3_t segmentmaxs)
4700 float nodesegmentmins[3], nodesegmentmaxs[3];
4705 // find which nodes the line is in and recurse for them
4708 // recurse down node sides
4709 int startside, endside;
4710 float dist1near, dist1far, dist2near, dist2far;
4711 BoxPlaneCornerDistances(thisbrush_start->mins, thisbrush_start->maxs, node->plane, &dist1near, &dist1far);
4712 BoxPlaneCornerDistances(thisbrush_end->mins, thisbrush_end->maxs, node->plane, &dist2near, &dist2far);
4713 startside = dist1near < 0;
4714 startside = dist1near < 0 ? (dist1far < 0 ? 1 : 2) : (dist1far < 0 ? 2 : 0);
4715 endside = dist2near < 0 ? (dist2far < 0 ? 1 : 2) : (dist2far < 0 ? 2 : 0);
4716 if (startside == 2 || endside == 2)
4718 // brushes cross plane
4719 // do not clip anything, just take both sides
4720 Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, node->children[0], thisbrush_start, thisbrush_end, markframe, segmentmins, segmentmaxs);
4721 node = node->children[1];
4728 node = node->children[0];
4733 //midf0 = dist1near / (dist1near - dist2near);
4734 //midf1 = dist1far / (dist1far - dist2far);
4735 Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, node->children[0], thisbrush_start, thisbrush_end, markframe, segmentmins, segmentmaxs);
4736 node = node->children[1];
4744 //midf0 = dist1near / (dist1near - dist2near);
4745 //midf1 = dist1far / (dist1far - dist2far);
4746 Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, node->children[0], thisbrush_start, thisbrush_end, markframe, segmentmins, segmentmaxs);
4747 node = node->children[1];
4752 node = node->children[1];
4757 if (dist1near < 0 && dist2near < 0 && dist1far < 0 && dist2far < 0){node = node->children[1];continue;}
4758 if (dist1near < 0 && dist2near < 0 && dist1far < 0 && dist2far >= 0){Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, node->children[0], thisbrush_start, thisbrush_end, markframe, segmentmins, segmentmaxs);node = node->children[1];continue;}
4759 if (dist1near < 0 && dist2near < 0 && dist1far >= 0 && dist2far < 0){Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, node->children[0], thisbrush_start, thisbrush_end, markframe, segmentmins, segmentmaxs);node = node->children[1];continue;}
4760 if (dist1near < 0 && dist2near < 0 && dist1far >= 0 && dist2far >= 0){Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, node->children[0], thisbrush_start, thisbrush_end, markframe, segmentmins, segmentmaxs);node = node->children[1];continue;}
4761 if (dist1near < 0 && dist2near >= 0 && dist1far < 0 && dist2far < 0){node = node->children[1];continue;}
4762 if (dist1near < 0 && dist2near >= 0 && dist1far < 0 && dist2far >= 0){}
4763 if (dist1near < 0 && dist2near >= 0 && dist1far >= 0 && dist2far < 0){Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, node->children[0], thisbrush_start, thisbrush_end, markframe, segmentmins, segmentmaxs);node = node->children[1];continue;}
4764 if (dist1near < 0 && dist2near >= 0 && dist1far >= 0 && dist2far >= 0){Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, node->children[0], thisbrush_start, thisbrush_end, markframe, segmentmins, segmentmaxs);node = node->children[1];continue;}
4765 if (dist1near >= 0 && dist2near < 0 && dist1far < 0 && dist2far < 0){node = node->children[1];continue;}
4766 if (dist1near >= 0 && dist2near < 0 && dist1far < 0 && dist2far >= 0){Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, node->children[0], thisbrush_start, thisbrush_end, markframe, segmentmins, segmentmaxs);node = node->children[1];continue;}
4767 if (dist1near >= 0 && dist2near < 0 && dist1far >= 0 && dist2far < 0){}
4768 if (dist1near >= 0 && dist2near < 0 && dist1far >= 0 && dist2far >= 0){Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, node->children[0], thisbrush_start, thisbrush_end, markframe, segmentmins, segmentmaxs);node = node->children[1];continue;}
4769 if (dist1near >= 0 && dist2near >= 0 && dist1far < 0 && dist2far < 0){Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, node->children[0], thisbrush_start, thisbrush_end, markframe, segmentmins, segmentmaxs);node = node->children[1];continue;}
4770 if (dist1near >= 0 && dist2near >= 0 && dist1far < 0 && dist2far >= 0){node = node->children[0];continue;}
4771 if (dist1near >= 0 && dist2near >= 0 && dist1far >= 0 && dist2far < 0){node = node->children[0];continue;}
4772 if (dist1near >= 0 && dist2near >= 0 && dist1far >= 0 && dist2far >= 0){node = node->children[0];continue;}
4774 if (dist2near < 0) // d1n<0 && d2n<0
4776 if (dist2near < 0) // d1n<0 && d2n<0
4778 if (dist2near < 0) // d1n<0 && d2n<0
4781 else // d1n<0 && d2n>0
4785 else // d1n<0 && d2n>0
4787 if (dist2near < 0) // d1n<0 && d2n<0
4790 else // d1n<0 && d2n>0
4795 else // d1n<0 && d2n>0
4801 if (dist2near < 0) // d1n>0 && d2n<0
4804 else // d1n>0 && d2n>0
4808 if (dist1near < 0 == dist1far < 0 == dist2near < 0 == dist2far < 0)
4810 node = node->children[startside];
4813 if (dist1near < dist2near)
4818 node = node->children[0];
4823 node = node->children[1];
4826 // dist1near < 0 && dist2far >= 0
4832 startside = dist1near < 0 ? (dist1far < 0 ? 1 : 2) : (dist1far < 0 ? 2 : 0);
4833 endside = dist2near < 0 ? (dist2far < 0 ? 1 : 2) : (dist2far < 0 ? 2 : 0);
4834 if (startside == 2 || endside == 2)
4836 // brushes cross plane
4837 // do not clip anything, just take both sides
4838 Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, node->children[0], thisbrush_start, thisbrush_end, markframe, segmentmins, segmentmaxs);
4839 node = node->children[1];
4841 else if (startside == endside)
4842 node = node->children[startside];
4843 else if (startside == 0) // endside = 1 (start infront, end behind)
4846 else // startside == 1 endside = 0 (start behind, end infront)
4852 node = node->children[startside];
4855 // start and end brush cross plane
4861 if (dist1near < 0 && dist1far < 0 && dist2near < 0 && dist2far < 0)
4862 node = node->children[1];
4863 else if (dist1near < 0 && dist1far < 0 && dist2near >= 0 && dist2far >= 0)
4864 else if (dist1near >= 0 && dist1far >= 0 && dist2near < 0 && dist2far < 0)
4865 else if (dist1near >= 0 && dist1far >= 0 && dist2near >= 0 && dist2far >= 0)
4866 node = node->children[0];
4868 if (dist1near < 0 && dist1far < 0 && dist2near < 0 && dist2far < 0)
4869 if (dist1near < 0 && dist1far < 0 && dist2near < 0 && dist2far < 0)
4870 if (dist1near < 0 && dist1far < 0 && dist2near < 0 && dist2far < 0)
4871 if (dist1near < 0 && dist1far < 0 && dist2near < 0 && dist2far < 0)
4872 if (dist1near < 0 && dist1far < 0 && dist2near < 0 && dist2far < 0)
4875 else if (dist1near >= 0 && dist1far >= 0)
4878 else // mixed (lying on plane)
4882 if (dist2near < 0 && dist2far < 0)
4886 node = node->children[1];
4888 if (dist1near < 0 && dist1far < 0 && dist2near < 0 && dist2far < 0)
4889 node = node->children[0];
4890 else if (dist1near >= 0 && dist1far >= 0 && dist2near >= 0 && dist2far >= 0)
4891 node = node->children[1];
4895 Mod_Q3BSP_TraceLine_RecursiveBSPNode(trace, node->children[startside], start, mid, startfrac, midfrac, linestart, lineend, markframe, segmentmins, segmentmaxs);
4896 node = node->children[1];
4898 sides = dist1near || dist1near < 0 | dist1far < 0 | dist2near < 0 | dist
4899 startside = dist1 < 0;
4900 endside = dist2 < 0;
4901 if (startside == endside)
4903 // most of the time the line fragment is on one side of the plane
4904 node = node->children[startside];
4908 // line crosses node plane, split the line
4909 midfrac = dist1 / (dist1 - dist2);
4910 VectorLerp(start, midfrac, end, mid);
4911 // take the near side first
4912 Mod_Q3BSP_TraceLine_RecursiveBSPNode(trace, node->children[startside], start, mid, startfrac, midfrac, linestart, lineend, markframe, segmentmins, segmentmaxs);
4913 if (midfrac <= trace->fraction)
4914 Mod_Q3BSP_TraceLine_RecursiveBSPNode(trace, node->children[endside], mid, end, midfrac, endfrac, linestart, lineend, markframe, segmentmins, segmentmaxs);
4922 nodesegmentmins[0] = max(segmentmins[0], node->mins[0]);
4923 nodesegmentmins[1] = max(segmentmins[1], node->mins[1]);
4924 nodesegmentmins[2] = max(segmentmins[2], node->mins[2]);
4925 nodesegmentmaxs[0] = min(segmentmaxs[0], node->maxs[0]);
4926 nodesegmentmaxs[1] = min(segmentmaxs[1], node->maxs[1]);
4927 nodesegmentmaxs[2] = min(segmentmaxs[2], node->maxs[2]);
4928 if (nodesegmentmins[0] > nodesegmentmaxs[0] || nodesegmentmins[1] > nodesegmentmaxs[1] || nodesegmentmins[2] > nodesegmentmaxs[2])
4932 Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, node->children[0], thisbrush_start, thisbrush_end, markframe, segmentmins, segmentmaxs);
4933 node = node->children[1];
4936 // FIXME: could be made faster by copying TraceLine code and making it use
4937 // box plane distances... (variant on the BoxOnPlaneSide code)
4940 nodesegmentmins[0] = max(segmentmins[0], node->mins[0]);
4941 nodesegmentmins[1] = max(segmentmins[1], node->mins[1]);
4942 nodesegmentmins[2] = max(segmentmins[2], node->mins[2]);
4943 nodesegmentmaxs[0] = min(segmentmaxs[0], node->maxs[0]);
4944 nodesegmentmaxs[1] = min(segmentmaxs[1], node->maxs[1]);
4945 nodesegmentmaxs[2] = min(segmentmaxs[2], node->maxs[2]);
4946 if (nodesegmentmins[0] > nodesegmentmaxs[0] || nodesegmentmins[1] > nodesegmentmaxs[1] || nodesegmentmins[2] > nodesegmentmaxs[2])
4950 if (mod_q3bsp_debugtracebrush.integer == 2)
4952 Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, node->children[0], thisbrush_start, thisbrush_end, markframe, segmentmins, segmentmaxs);
4953 node = node->children[1];
4956 else if (mod_q3bsp_debugtracebrush.integer == 1)
4958 // recurse down node sides
4959 sides = BoxOnPlaneSide(nodesegmentmins, nodesegmentmaxs, node->plane);
4962 // segment box crosses plane
4963 Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, node->children[0], thisbrush_start, thisbrush_end, markframe, segmentmins, segmentmaxs);
4964 node = node->children[1];
4967 // take whichever side the segment box is on
4968 node = node->children[sides - 1];
4973 // recurse down node sides
4974 sides = BoxOnPlaneSide(nodesegmentmins, nodesegmentmaxs, node->plane);
4977 // segment box crosses plane
4978 // now check start and end brush boxes to handle a lot of 'diagonal' cases more efficiently...
4979 sides = BoxOnPlaneSide(thisbrush_start->mins, thisbrush_start->maxs, node->plane) | BoxOnPlaneSide(thisbrush_end->mins, thisbrush_end->maxs, node->plane);
4982 Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, node->children[0], thisbrush_start, thisbrush_end, markframe, segmentmins, segmentmaxs);
4983 node = node->children[1];
4987 // take whichever side the segment box is on
4988 node = node->children[sides - 1];
4994 // FIXME: could be made faster by copying TraceLine code and making it use
4995 // box plane distances... (variant on the BoxOnPlaneSide code)
4998 nodesegmentmins[0] = max(segmentmins[0], node->mins[0]);
4999 nodesegmentmins[1] = max(segmentmins[1], node->mins[1]);
5000 nodesegmentmins[2] = max(segmentmins[2], node->mins[2]);
5001 nodesegmentmaxs[0] = min(segmentmaxs[0], node->maxs[0]);
5002 nodesegmentmaxs[1] = min(segmentmaxs[1], node->maxs[1]);
5003 nodesegmentmaxs[2] = min(segmentmaxs[2], node->maxs[2]);
5004 if (nodesegmentmins[0] > nodesegmentmaxs[0] || nodesegmentmins[1] > nodesegmentmaxs[1] || nodesegmentmins[2] > nodesegmentmaxs[2])
5008 if (mod_q3bsp_debugtracebrush.integer == 2)
5010 Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, node->children[0], thisbrush_start, thisbrush_end, markframe, segmentmins, segmentmaxs);
5011 node = node->children[1];
5013 else if (mod_q3bsp_debugtracebrush.integer == 1)
5015 // recurse down node sides
5016 sides = BoxOnPlaneSide(nodesegmentmins, nodesegmentmaxs, node->plane);
5019 // segment box crosses plane
5020 Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, node->children[0], thisbrush_start, thisbrush_end, markframe, segmentmins, segmentmaxs);
5021 node = node->children[1];
5025 // take whichever side the segment box is on
5026 node = node->children[sides - 1];
5031 // recurse down node sides
5032 sides = BoxOnPlaneSide(nodesegmentmins, nodesegmentmaxs, node->plane);
5035 // segment box crosses plane
5036 // now check start and end brush boxes to handle a lot of 'diagonal' cases more efficiently...
5037 sides = BoxOnPlaneSide(thisbrush_start->mins, thisbrush_start->maxs, node->plane) | BoxOnPlaneSide(thisbrush_end->mins, thisbrush_end->maxs, node->plane);
5040 Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, node->children[0], thisbrush_start, thisbrush_end, markframe, segmentmins, segmentmaxs);
5044 // take whichever side the segment box is on
5045 node = node->children[sides - 1];
5050 leaf = (q3mleaf_t *)node;
5051 for (i = 0;i < leaf->numleafbrushes;i++)
5053 brush = leaf->firstleafbrush[i]->colbrushf;
5054 if (brush && brush->markframe != markframe && BoxesOverlap(nodesegmentmins, nodesegmentmaxs, brush->mins, brush->maxs))
5056 brush->markframe = markframe;
5057 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, leaf->firstleafbrush[i]->colbrushf, leaf->firstleafbrush[i]->colbrushf);
5060 if (mod_q3bsp_curves_collisions.integer)
5062 for (i = 0;i < leaf->numleaffaces;i++)
5064 face = leaf->firstleafface[i];
5065 if (face->collisions && face->markframe != markframe && BoxesOverlap(nodesegmentmins, nodesegmentmaxs, face->mins, face->maxs))
5067 face->markframe = markframe;
5068 Collision_TraceBrushTriangleMeshFloat(trace, thisbrush_start, thisbrush_end, face->num_triangles, face->data_element3i, face->data_vertex3f, face->texture->supercontents, segmentmins, segmentmaxs);
5074 static void Mod_Q3BSP_TraceBox(model_t *model, int frame, trace_t *trace, const vec3_t boxstartmins, const vec3_t boxstartmaxs, const vec3_t boxendmins, const vec3_t boxendmaxs, int hitsupercontentsmask)
5077 float segmentmins[3], segmentmaxs[3];
5078 colbrushf_t *thisbrush_start, *thisbrush_end;
5079 matrix4x4_t startmatrix, endmatrix;
5080 static int markframe = 0;
5082 memset(trace, 0, sizeof(*trace));
5083 trace->fraction = 1;
5084 trace->realfraction = 1;
5085 trace->hitsupercontentsmask = hitsupercontentsmask;
5086 Matrix4x4_CreateIdentity(&startmatrix);
5087 Matrix4x4_CreateIdentity(&endmatrix);
5088 segmentmins[0] = min(boxstartmins[0], boxendmins[0]);
5089 segmentmins[1] = min(boxstartmins[1], boxendmins[1]);
5090 segmentmins[2] = min(boxstartmins[2], boxendmins[2]);
5091 segmentmaxs[0] = max(boxstartmaxs[0], boxendmaxs[0]);
5092 segmentmaxs[1] = max(boxstartmaxs[1], boxendmaxs[1]);
5093 segmentmaxs[2] = max(boxstartmaxs[2], boxendmaxs[2]);
5094 if (mod_q3bsp_optimizedtraceline.integer && VectorCompare(boxstartmins, boxstartmaxs) && VectorCompare(boxendmins, boxendmaxs))
5096 if (VectorCompare(boxstartmins, boxendmins))
5099 if (model->brushq3.submodel)
5101 for (i = 0;i < model->brushq3.data_thismodel->numbrushes;i++)
5102 if (model->brushq3.data_thismodel->firstbrush[i].colbrushf)
5103 Collision_TracePointBrushFloat(trace, boxstartmins, model->brushq3.data_thismodel->firstbrush[i].colbrushf);
5106 Mod_Q3BSP_TracePoint_RecursiveBSPNode(trace, model->brushq3.data_nodes, boxstartmins, ++markframe);
5111 if (model->brushq3.submodel)
5113 for (i = 0;i < model->brushq3.data_thismodel->numbrushes;i++)
5114 if (model->brushq3.data_thismodel->firstbrush[i].colbrushf)
5115 Collision_TraceLineBrushFloat(trace, boxstartmins, boxendmins, model->brushq3.data_thismodel->firstbrush[i].colbrushf, model->brushq3.data_thismodel->firstbrush[i].colbrushf);
5116 if (mod_q3bsp_curves_collisions.integer)
5118 for (i = 0;i < model->brushq3.data_thismodel->numfaces;i++)
5120 face = model->brushq3.data_thismodel->firstface + i;
5121 if (face->collisions)
5122 Collision_TraceLineTriangleMeshFloat(trace, boxstartmins, boxendmins, face->num_triangles, face->data_element3i, face->data_vertex3f, face->texture->supercontents, segmentmins, segmentmaxs);
5127 Mod_Q3BSP_TraceLine_RecursiveBSPNode(trace, model->brushq3.data_nodes, boxstartmins, boxendmins, 0, 1, boxstartmins, boxendmins, ++markframe, segmentmins, segmentmaxs);
5132 // box trace, performed as brush trace
5133 thisbrush_start = Collision_BrushForBox(&startmatrix, boxstartmins, boxstartmaxs);
5134 thisbrush_end = Collision_BrushForBox(&endmatrix, boxendmins, boxendmaxs);
5135 if (model->brushq3.submodel)
5137 for (i = 0;i < model->brushq3.data_thismodel->numbrushes;i++)
5138 if (model->brushq3.data_thismodel->firstbrush[i].colbrushf)
5139 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, model->brushq3.data_thismodel->firstbrush[i].colbrushf, model->brushq3.data_thismodel->firstbrush[i].colbrushf);
5140 if (mod_q3bsp_curves_collisions.integer)
5142 for (i = 0;i < model->brushq3.data_thismodel->numfaces;i++)
5144 face = model->brushq3.data_thismodel->firstface + i;
5145 if (face->collisions)
5146 Collision_TraceBrushTriangleMeshFloat(trace, thisbrush_start, thisbrush_end, face->num_triangles, face->data_element3i, face->data_vertex3f, face->texture->supercontents, segmentmins, segmentmaxs);
5151 Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, model->brushq3.data_nodes, thisbrush_start, thisbrush_end, ++markframe, segmentmins, segmentmaxs);
5155 static int Mod_Q3BSP_BoxTouchingPVS(model_t *model, const qbyte *pvs, const vec3_t mins, const vec3_t maxs)
5157 int clusterindex, side, nodestackindex = 0;
5158 q3mnode_t *node, *nodestack[1024];
5159 node = model->brushq3.data_nodes;
5160 if (!loadmodel->brush.num_pvsclusters)
5166 // node - recurse down the BSP tree
5167 side = BoxOnPlaneSide(mins, maxs, node->plane) - 1;
5170 // box is on one side of plane, take that path
5171 node = node->children[side];
5175 // box crosses plane, take one path and remember the other
5176 nodestack[nodestackindex++] = node->children[0];
5177 node = node->children[1];
5182 // leaf - check cluster bit
5183 clusterindex = ((q3mleaf_t *)node)->clusterindex;
5185 if (clusterindex >= loadmodel->brush.num_pvsclusters)
5187 Con_Printf("%i >= %i\n", clusterindex, loadmodel->brush.num_pvsclusters);
5191 if (CHECKPVSBIT(pvs, clusterindex))
5193 // it is visible, return immediately with the news
5198 // nothing to see here, try another path we didn't take earlier
5199 if (nodestackindex == 0)
5201 node = nodestack[--nodestackindex];
5205 // it is not visible
5209 //Returns PVS data for a given point
5210 //(note: can return NULL)
5211 static qbyte *Mod_Q3BSP_GetPVS(model_t *model, const vec3_t p)
5214 Mod_CheckLoaded(model);
5215 node = model->brushq3.data_nodes;
5217 node = node->children[(node->plane->type < 3 ? p[node->plane->type] : DotProduct(p,node->plane->normal)) < node->plane->dist];
5218 if (((q3mleaf_t *)node)->clusterindex >= 0)
5219 return model->brush.data_pvsclusters + ((q3mleaf_t *)node)->clusterindex * model->brush.num_pvsclusterbytes;
5224 static void Mod_Q3BSP_FatPVS_RecursiveBSPNode(model_t *model, const vec3_t org, vec_t radius, qbyte *pvsbuffer, int pvsbytes, q3mnode_t *node)
5228 float d = PlaneDiff(org, node->plane);
5230 node = node->children[0];
5231 else if (d < -radius)
5232 node = node->children[1];
5235 // go down both sides
5236 Mod_Q3BSP_FatPVS_RecursiveBSPNode(model, org, radius, pvsbuffer, pvsbytes, node->children[0]);
5237 node = node->children[1];
5240 // if this leaf is in a cluster, accumulate the pvs bits
5241 if (((q3mleaf_t *)node)->clusterindex >= 0)
5244 qbyte *pvs = model->brush.data_pvsclusters + ((q3mleaf_t *)node)->clusterindex * model->brush.num_pvsclusterbytes;
5245 for (i = 0;i < pvsbytes;i++)
5246 pvsbuffer[i] |= pvs[i];
5250 //Calculates a PVS that is the inclusive or of all leafs within radius pixels
5251 //of the given point.
5252 static int Mod_Q3BSP_FatPVS(model_t *model, const vec3_t org, vec_t radius, qbyte *pvsbuffer, int pvsbufferlength)
5254 int bytes = model->brush.num_pvsclusterbytes;
5255 bytes = min(bytes, pvsbufferlength);
5256 if (r_novis.integer || !loadmodel->brush.num_pvsclusters)
5258 memset(pvsbuffer, 0xFF, bytes);
5261 memset(pvsbuffer, 0, bytes);
5262 Mod_Q3BSP_FatPVS_RecursiveBSPNode(model, org, radius, pvsbuffer, bytes, model->brushq3.data_nodes);
5267 static int Mod_Q3BSP_SuperContentsFromNativeContents(model_t *model, int nativecontents)
5269 int supercontents = 0;
5270 if (nativecontents & Q2CONTENTS_SOLID)
5271 supercontents |= SUPERCONTENTS_SOLID;
5272 if (nativecontents & Q2CONTENTS_WATER)
5273 supercontents |= SUPERCONTENTS_WATER;
5274 if (nativecontents & Q2CONTENTS_SLIME)
5275 supercontents |= SUPERCONTENTS_SLIME;
5276 if (nativecontents & Q2CONTENTS_LAVA)
5277 supercontents |= SUPERCONTENTS_LAVA;
5278 return supercontents;
5281 static int Mod_Q3BSP_NativeContentsFromSuperContents(model_t *model, int supercontents)
5283 int nativecontents = 0;
5284 if (supercontents & SUPERCONTENTS_SOLID)
5285 nativecontents |= Q2CONTENTS_SOLID;
5286 if (supercontents & SUPERCONTENTS_WATER)
5287 nativecontents |= Q2CONTENTS_WATER;
5288 if (supercontents & SUPERCONTENTS_SLIME)
5289 nativecontents |= Q2CONTENTS_SLIME;
5290 if (supercontents & SUPERCONTENTS_LAVA)
5291 nativecontents |= Q2CONTENTS_LAVA;
5292 return nativecontents;
5295 extern void R_Q3BSP_DrawSky(struct entity_render_s *ent);
5296 extern void R_Q3BSP_Draw(struct entity_render_s *ent);
5297 extern void R_Q3BSP_DrawShadowVolume(struct entity_render_s *ent, vec3_t relativelightorigin, float lightradius);
5298 extern void R_Q3BSP_DrawLight(struct entity_render_s *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap);
5299 void Mod_Q3BSP_Load(model_t *mod, void *buffer)
5302 q3dheader_t *header;
5303 float corner[3], yawradius, modelradius;
5305 mod->type = mod_brushq3;
5309 header = (q3dheader_t *)buffer;
5311 i = LittleLong(header->version);
5312 if (i != Q3BSPVERSION)
5313 Host_Error("Mod_Q3BSP_Load: %s has wrong version number (%i, should be %i)", mod->name, i, Q3BSPVERSION);
5314 if (loadmodel->isworldmodel)
5316 Cvar_SetValue("halflifebsp", false);
5317 // until we get a texture for it...
5321 mod->soundfromcenter = true;
5322 mod->TraceBox = Mod_Q3BSP_TraceBox;
5323 mod->brush.SuperContentsFromNativeContents = Mod_Q3BSP_SuperContentsFromNativeContents;
5324 mod->brush.NativeContentsFromSuperContents = Mod_Q3BSP_NativeContentsFromSuperContents;
5325 mod->brush.GetPVS = Mod_Q3BSP_GetPVS;
5326 mod->brush.FatPVS = Mod_Q3BSP_FatPVS;
5327 mod->brush.BoxTouchingPVS = Mod_Q3BSP_BoxTouchingPVS;
5328 mod->brush.LightPoint = Mod_Q3BSP_LightPoint;
5329 mod->brush.FindNonSolidLocation = Mod_Q3BSP_FindNonSolidLocation;
5330 //mod->DrawSky = R_Q3BSP_DrawSky;
5331 mod->Draw = R_Q3BSP_Draw;
5332 mod->DrawShadowVolume = R_Q3BSP_DrawShadowVolume;
5333 mod->DrawLight = R_Q3BSP_DrawLight;
5335 mod_base = (qbyte *)header;
5337 // swap all the lumps
5338 for (i = 0;i < (int) sizeof(*header) / 4;i++)
5339 ((int *)header)[i] = LittleLong(((int *)header)[i]);
5341 Mod_Q3BSP_LoadEntities(&header->lumps[Q3LUMP_ENTITIES]);
5342 Mod_Q3BSP_LoadTextures(&header->lumps[Q3LUMP_TEXTURES]);
5343 Mod_Q3BSP_LoadPlanes(&header->lumps[Q3LUMP_PLANES]);
5344 Mod_Q3BSP_LoadBrushSides(&header->lumps[Q3LUMP_BRUSHSIDES]);
5345 Mod_Q3BSP_LoadBrushes(&header->lumps[Q3LUMP_BRUSHES]);
5346 Mod_Q3BSP_LoadEffects(&header->lumps[Q3LUMP_EFFECTS]);
5347 Mod_Q3BSP_LoadVertices(&header->lumps[Q3LUMP_VERTICES]);
5348 Mod_Q3BSP_LoadTriangles(&header->lumps[Q3LUMP_TRIANGLES]);
5349 Mod_Q3BSP_LoadLightmaps(&header->lumps[Q3LUMP_LIGHTMAPS]);
5350 Mod_Q3BSP_LoadFaces(&header->lumps[Q3LUMP_FACES]);
5351 Mod_Q3BSP_LoadModels(&header->lumps[Q3LUMP_MODELS]);
5352 Mod_Q3BSP_LoadLeafBrushes(&header->lumps[Q3LUMP_LEAFBRUSHES]);
5353 Mod_Q3BSP_LoadLeafFaces(&header->lumps[Q3LUMP_LEAFFACES]);
5354 Mod_Q3BSP_LoadLeafs(&header->lumps[Q3LUMP_LEAFS]);
5355 Mod_Q3BSP_LoadNodes(&header->lumps[Q3LUMP_NODES]);
5356 Mod_Q3BSP_LoadLightGrid(&header->lumps[Q3LUMP_LIGHTGRID]);
5357 Mod_Q3BSP_LoadPVS(&header->lumps[Q3LUMP_PVS]);
5358 loadmodel->brush.numsubmodels = loadmodel->brushq3.num_models;
5360 for (i = 0;i < loadmodel->brushq3.num_models;i++)
5367 // LordHavoc: only register submodels if it is the world
5368 // (prevents bsp models from replacing world submodels)
5369 if (!loadmodel->isworldmodel)
5371 // duplicate the basic information
5372 sprintf(name, "*%i", i);
5373 mod = Mod_FindName(name);
5375 strcpy(mod->name, name);
5376 // textures and memory belong to the main model
5377 mod->texturepool = NULL;
5378 mod->mempool = NULL;
5380 mod->brushq3.data_thismodel = loadmodel->brushq3.data_models + i;
5381 mod->brushq3.submodel = i;
5383 VectorCopy(mod->brushq3.data_thismodel->mins, mod->normalmins);
5384 VectorCopy(mod->brushq3.data_thismodel->maxs, mod->normalmaxs);
5385 corner[0] = max(fabs(mod->normalmins[0]), fabs(mod->normalmaxs[0]));
5386 corner[1] = max(fabs(mod->normalmins[1]), fabs(mod->normalmaxs[1]));
5387 corner[2] = max(fabs(mod->normalmins[2]), fabs(mod->normalmaxs[2]));
5388 modelradius = sqrt(corner[0]*corner[0]+corner[1]*corner[1]+corner[2]*corner[2]);
5389 yawradius = sqrt(corner[0]*corner[0]+corner[1]*corner[1]);
5390 mod->rotatedmins[0] = mod->rotatedmins[1] = mod->rotatedmins[2] = -modelradius;
5391 mod->rotatedmaxs[0] = mod->rotatedmaxs[1] = mod->rotatedmaxs[2] = modelradius;
5392 mod->yawmaxs[0] = mod->yawmaxs[1] = yawradius;
5393 mod->yawmins[0] = mod->yawmins[1] = -yawradius;
5394 mod->yawmins[2] = mod->normalmins[2];
5395 mod->yawmaxs[2] = mod->normalmaxs[2];
5396 mod->radius = modelradius;
5397 mod->radius2 = modelradius * modelradius;
5399 for (j = 0;j < mod->brushq3.data_thismodel->numfaces;j++)
5400 if (mod->brushq3.data_thismodel->firstface[j].texture->surfaceflags & Q3SURFACEFLAG_SKY)
5402 if (j < mod->brushq3.data_thismodel->numfaces)
5403 mod->DrawSky = R_Q3BSP_DrawSky;
5407 void Mod_IBSP_Load(model_t *mod, void *buffer)
5409 int i = LittleLong(((int *)buffer)[1]);
5410 if (i == Q3BSPVERSION)
5411 Mod_Q3BSP_Load(mod,buffer);
5412 else if (i == Q2BSPVERSION)
5413 Mod_Q2BSP_Load(mod,buffer);
5415 Host_Error("Mod_IBSP_Load: unknown/unsupported version %i\n", i);
5418 void Mod_MAP_Load(model_t *mod, void *buffer)
5420 Host_Error("Mod_MAP_Load: not yet implemented\n");