📄 csg.c
字号:
if (p == maxplanenums[0] || p == maxplanenums[1]
|| p == minplanenums[0] || p == minplanenums[1])
{
brush->sides[i].texinfo = TEXINFO_NODE;
brush->sides[i].flags &= ~SFL_VISIBLE;
}
}
return brush;
} //end of the function ClipBrushToBox
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bspbrush_t *MakeBspBrushList(int startbrush, int endbrush,
vec3_t clipmins, vec3_t clipmaxs)
{
mapbrush_t *mb;
bspbrush_t *brushlist, *newbrush;
int i, j;
int c_faces;
int c_brushes;
int numsides;
int vis;
vec3_t normal;
float dist;
for (i=0 ; i<2 ; i++)
{
VectorClear (normal);
normal[i] = 1;
dist = clipmaxs[i];
maxplanenums[i] = FindFloatPlane(normal, dist);
dist = clipmins[i];
minplanenums[i] = FindFloatPlane(normal, dist);
}
brushlist = NULL;
c_faces = 0;
c_brushes = 0;
for (i=startbrush ; i<endbrush ; i++)
{
mb = &mapbrushes[i];
numsides = mb->numsides;
if (!numsides)
continue;
// make sure the brush has at least one face showing
vis = 0;
for (j=0 ; j<numsides ; j++)
if ((mb->original_sides[j].flags & SFL_VISIBLE) && mb->original_sides[j].winding)
vis++;
#if 0
if (!vis)
continue; // no faces at all
#endif
// if the brush is outside the clip area, skip it
for (j=0 ; j<3 ; j++)
if (mb->mins[j] >= clipmaxs[j]
|| mb->maxs[j] <= clipmins[j])
break;
if (j != 3)
continue;
//
// make a copy of the brush
//
newbrush = AllocBrush (mb->numsides);
newbrush->original = mb;
newbrush->numsides = mb->numsides;
memcpy (newbrush->sides, mb->original_sides, numsides*sizeof(side_t));
for (j=0 ; j<numsides ; j++)
{
if (newbrush->sides[j].winding)
newbrush->sides[j].winding = CopyWinding (newbrush->sides[j].winding);
if (newbrush->sides[j].surf & SURF_HINT)
newbrush->sides[j].flags |= SFL_VISIBLE; // hints are always visible
}
VectorCopy (mb->mins, newbrush->mins);
VectorCopy (mb->maxs, newbrush->maxs);
//
// carve off anything outside the clip box
//
newbrush = ClipBrushToBox (newbrush, clipmins, clipmaxs);
if (!newbrush)
continue;
c_faces += vis;
c_brushes++;
newbrush->next = brushlist;
brushlist = newbrush;
}
return brushlist;
} //end of the function MakeBspBrushList
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bspbrush_t *AddBrushListToTail (bspbrush_t *list, bspbrush_t *tail)
{
bspbrush_t *walk, *next;
for (walk=list ; walk ; walk=next)
{ // add to end of list
next = walk->next;
walk->next = NULL;
tail->next = walk;
tail = walk;
} //end for
return tail;
} //end of the function AddBrushListToTail
//===========================================================================
// Builds a new list that doesn't hold the given brush
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bspbrush_t *CullList(bspbrush_t *list, bspbrush_t *skip1)
{
bspbrush_t *newlist;
bspbrush_t *next;
newlist = NULL;
for ( ; list ; list = next)
{
next = list->next;
if (list == skip1)
{
FreeBrush (list);
continue;
}
list->next = newlist;
newlist = list;
}
return newlist;
} //end of the function CullList
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
/*
void WriteBrushMap(char *name, bspbrush_t *list)
{
FILE *f;
side_t *s;
int i;
winding_t *w;
Log_Print("writing %s\n", name);
f = fopen (name, "wb");
if (!f)
Error ("Can't write %s\b", name);
fprintf (f, "{\n\"classname\" \"worldspawn\"\n");
for ( ; list ; list=list->next )
{
fprintf (f, "{\n");
for (i=0,s=list->sides ; i<list->numsides ; i++,s++)
{
w = BaseWindingForPlane (mapplanes[s->planenum].normal, mapplanes[s->planenum].dist);
fprintf (f,"( %i %i %i ) ", (int)w->p[0][0], (int)w->p[0][1], (int)w->p[0][2]);
fprintf (f,"( %i %i %i ) ", (int)w->p[1][0], (int)w->p[1][1], (int)w->p[1][2]);
fprintf (f,"( %i %i %i ) ", (int)w->p[2][0], (int)w->p[2][1], (int)w->p[2][2]);
fprintf (f, "%s 0 0 0 1 1\n", texinfo[s->texinfo].texture);
FreeWinding (w);
}
fprintf (f, "}\n");
}
fprintf (f, "}\n");
fclose (f);
} //end of the function WriteBrushMap
*/
//===========================================================================
// Returns true if b1 is allowed to bite b2
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
qboolean BrushGE (bspbrush_t *b1, bspbrush_t *b2)
{
#ifdef ME
if (create_aas)
{
if (b1->original->expansionbbox != b2->original->expansionbbox)
{
return false;
} //end if
//never have something else bite a ladder brush
//never have a ladder brush bite something else
if ( (b1->original->contents & CONTENTS_LADDER)
&& !(b2->original->contents & CONTENTS_LADDER))
{
return false;
} //end if
} //end if
#endif //ME
// detail brushes never bite structural brushes
if ( (b1->original->contents & CONTENTS_DETAIL)
&& !(b2->original->contents & CONTENTS_DETAIL) )
{
return false;
} //end if
if (b1->original->contents & CONTENTS_SOLID)
{
return true;
} //end if
return false;
} //end of the function BrushGE
//===========================================================================
// Carves any intersecting solid brushes into the minimum number
// of non-intersecting brushes.
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bspbrush_t *ChopBrushes (bspbrush_t *head)
{
bspbrush_t *b1, *b2, *next;
bspbrush_t *tail;
bspbrush_t *keep;
bspbrush_t *sub, *sub2;
int c1, c2;
int num_csg_iterations;
Log_Print("-------- Brush CSG ---------\n");
Log_Print("%6d original brushes\n", CountBrushList (head));
num_csg_iterations = 0;
qprintf("%6d output brushes", num_csg_iterations);
#if 0
if (startbrush == 0)
WriteBrushList ("before.gl", head, false);
#endif
keep = NULL;
newlist:
// find tail
if (!head) return NULL;
for (tail = head; tail->next; tail = tail->next)
;
for (b1=head ; b1 ; b1=next)
{
next = b1->next;
//if the conversion is cancelled
if (cancelconversion)
{
b1->next = keep;
keep = b1;
continue;
} //end if
for (b2 = b1->next; b2; b2 = b2->next)
{
if (BrushesDisjoint (b1, b2))
continue;
sub = NULL;
sub2 = NULL;
c1 = 999999;
c2 = 999999;
if (BrushGE (b2, b1))
{
sub = SubtractBrush (b1, b2);
if (sub == b1)
{
continue; // didn't really intersect
} //end if
if (!sub)
{ // b1 is swallowed by b2
head = CullList (b1, b1);
goto newlist;
}
c1 = CountBrushList (sub);
}
if ( BrushGE (b1, b2) )
{
sub2 = SubtractBrush (b2, b1);
if (sub2 == b2)
continue; // didn't really intersect
if (!sub2)
{ // b2 is swallowed by b1
FreeBrushList (sub);
head = CullList (b1, b2);
goto newlist;
}
c2 = CountBrushList (sub2);
}
if (!sub && !sub2)
continue; // neither one can bite
// only accept if it didn't fragment
// (commenting this out allows full fragmentation)
if (c1 > 1 && c2 > 1)
{
if (sub2)
FreeBrushList (sub2);
if (sub)
FreeBrushList (sub);
continue;
}
if (c1 < c2)
{
if (sub2) FreeBrushList (sub2);
tail = AddBrushListToTail (sub, tail);
head = CullList (b1, b1);
goto newlist;
} //end if
else
{
if (sub) FreeBrushList (sub);
tail = AddBrushListToTail (sub2, tail);
head = CullList (b1, b2);
goto newlist;
} //end else
} //end for
if (!b2)
{ // b1 is no longer intersecting anything, so keep it
b1->next = keep;
keep = b1;
} //end if
num_csg_iterations++;
qprintf("\r%6d", num_csg_iterations);
} //end for
if (cancelconversion) return keep;
//
qprintf("\n");
Log_Write("%6d output brushes\r\n", num_csg_iterations);
#if 0
{
WriteBrushList ("after.gl", keep, false);
WriteBrushMap ("after.map", keep);
}
#endif
return keep;
} //end of the function ChopBrushes
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bspbrush_t *InitialBrushList (bspbrush_t *list)
{
bspbrush_t *b;
bspbrush_t *out, *newb;
int i;
// only return brushes that have visible faces
out = NULL;
for (b=list ; b ; b=b->next)
{
#if 0
for (i=0 ; i<b->numsides ; i++)
if (b->sides[i].flags & SFL_VISIBLE)
break;
if (i == b->numsides)
continue;
#endif
newb = CopyBrush (b);
newb->next = out;
out = newb;
// clear visible, so it must be set by MarkVisibleFaces_r
// to be used in the optimized list
for (i=0 ; i<b->numsides ; i++)
{
newb->sides[i].original = &b->sides[i];
// newb->sides[i].visible = true;
b->sides[i].flags &= ~SFL_VISIBLE;
}
}
return out;
} //end of the function InitialBrushList
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bspbrush_t *OptimizedBrushList (bspbrush_t *list)
{
bspbrush_t *b;
bspbrush_t *out, *newb;
int i;
// only return brushes that have visible faces
out = NULL;
for (b=list ; b ; b=b->next)
{
for (i=0 ; i<b->numsides ; i++)
if (b->sides[i].flags & SFL_VISIBLE)
break;
if (i == b->numsides)
continue;
newb = CopyBrush (b);
newb->next = out;
out = newb;
} //end for
// WriteBrushList ("vis.gl", out, true);
return out;
} //end of the function OptimizeBrushList
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
tree_t *ProcessWorldBrushes(int brush_start, int brush_end)
{
bspbrush_t *brushes;
tree_t *tree;
node_t *node;
vec3_t mins, maxs;
//take the whole world
mins[0] = map_mins[0] - 8;
mins[1] = map_mins[1] - 8;
mins[2] = map_mins[2] - 8;
maxs[0] = map_maxs[0] + 8;
maxs[1] = map_maxs[1] + 8;
maxs[2] = map_maxs[2] + 8;
//reset the brush bsp
ResetBrushBSP();
// the makelist and chopbrushes could be cached between the passes...
//create a list with brushes that are within the given mins/maxs
//some brushes will be cut and only the part that falls within the
//mins/maxs will be in the bush list
brushes = MakeBspBrushList(brush_start, brush_end, mins, maxs);
//
if (!brushes)
{
node = AllocNode ();
node->planenum = PLANENUM_LEAF;
node->contents = CONTENTS_SOLID;
tree = Tree_Alloc();
tree->headnode = node;
VectorCopy(mins, tree->mins);
VectorCopy(maxs, tree->maxs);
} //end if
else
{
//Carves any intersecting solid brushes into the minimum number
//of non-intersecting brushes.
if (!nocsg)
{
brushes = ChopBrushes(brushes);
/*
if (create_aas)
{
brushes = MergeBrushes(brushes);
} //end if*/
} //end if
//if the conversion is cancelled
if (cancelconversion)
{
FreeBrushList(brushes);
return NULL;
} //end if
//create the actual bsp tree
tree = BrushBSP(brushes, mins, maxs);
} //end else
//return the tree
return tree;
} //end of the function ProcessWorldBrushes
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -