csg.cpp
来自「quake3工具源码。包括生成bsp文件」· C++ 代码 · 共 661 行 · 第 1/2 页
CPP
661 行
}
//
if (Winding_PlanesConcave(face1->face_winding, face2->face_winding,
face1->plane.normal, face2->plane.normal,
face1->plane.dist, face2->plane.dist))
{
return NULL;
}
}
}
}
}
//
newbrush = Brush_Alloc();
//
for (brush1 = brushlist; brush1; brush1 = brush1->next)
{
for (face1 = brush1->brush_faces; face1; face1 = face1->next)
{
// don't add face1 to the new brush if it touches another brush
for (brush2 = brushlist; brush2; brush2 = brush2->next)
{
if (brush2 == brush1) continue;
for (face2 = brush2->brush_faces; face2; face2 = face2->next)
{
if (Plane_Equal(&face1->plane, &face2->plane, true))
{
break;
}
}
if (face2) break;
}
if (brush2) continue;
// don't add faces with the same plane twice
for (f = newbrush->brush_faces; f; f = f->next)
{
if (Plane_Equal(&face1->plane, &f->plane, false))
break;
if (Plane_Equal(&face1->plane, &f->plane, true))
break;
}
if (f)
continue;
//
newface = Face_Alloc();
newface->texdef = face1->texdef;
VectorCopy(face1->planepts[0], newface->planepts[0]);
VectorCopy(face1->planepts[1], newface->planepts[1]);
VectorCopy(face1->planepts[2], newface->planepts[2]);
newface->plane = face1->plane;
newface->next = newbrush->brush_faces;
newbrush->brush_faces = newface;
}
}
// link the new brush to an entity
Entity_LinkBrush (brushlist->owner, newbrush);
// build windings for the faces
Brush_BuildWindings( newbrush, false);
return newbrush;
}
/*
=============
Brush_Subtract
Returns a list of brushes that remain after B is subtracted from A.
May by empty if A is contained inside B.
The originals are undisturbed.
=============
*/
brush_t *Brush_Subtract(brush_t *a, brush_t *b)
{
// a - b = out (list)
brush_t *front, *back;
brush_t *in, *out, *next;
face_t *f;
in = a;
out = NULL;
for (f = b->brush_faces; f && in; f = f->next)
{
Brush_SplitBrushByFace(in, f, &front, &back);
if (in != a) Brush_Free(in);
if (front)
{ // add to list
front->next = out;
out = front;
}
in = back;
}
//NOTE: in != a just in case brush b has no faces
if (in && in != a)
{
Brush_Free(in);
}
else
{ //didn't really intersect
for (b = out; b; b = next)
{
next = b->next;
b->next = b->prev = NULL;
Brush_Free(b);
}
return a;
}
return out;
}
/*
=============
CSG_Subtract
=============
*/
void CSG_Subtract (void)
{
brush_t *b, *s, *fragments, *nextfragment, *frag, *next, *snext;
brush_t fragmentlist;
int i, numfragments, numbrushes;
Sys_Printf ("Subtracting...\n");
if (selected_brushes.next == &selected_brushes)
{
Sys_Printf("No brushes selected.\n");
return;
}
fragmentlist.next = &fragmentlist;
fragmentlist.prev = &fragmentlist;
numfragments = 0;
numbrushes = 0;
for (b = selected_brushes.next ; b != &selected_brushes ; b=next)
{
next = b->next;
if (b->owner->eclass->fixedsize)
continue; // can't use texture from a fixed entity, so don't subtract
// chop all fragments further up
for (s = fragmentlist.next; s != &fragmentlist; s = snext)
{
snext = s->next;
for (i=0 ; i<3 ; i++)
if (b->mins[i] >= s->maxs[i] - ON_EPSILON
|| b->maxs[i] <= s->mins[i] + ON_EPSILON)
break;
if (i != 3)
continue; // definately don't touch
fragments = Brush_Subtract(s, b);
// if the brushes did not really intersect
if (fragments == s)
continue;
// try to merge fragments
fragments = Brush_MergeListPairs(fragments, true);
// add the fragments to the list
for (frag = fragments; frag; frag = nextfragment)
{
nextfragment = frag->next;
frag->next = NULL;
frag->owner = s->owner;
Brush_AddToList(frag, &fragmentlist);
}
// free the original brush
Brush_Free(s);
}
// chop any active brushes up
for (s = active_brushes.next; s != &active_brushes; s = snext)
{
snext = s->next;
if (s->owner->eclass->fixedsize || s->patchBrush || s->hiddenBrush)
continue;
//face_t *pFace = s->brush_faces;
if (s->brush_faces->d_texture->bFromShader && (s->brush_faces->d_texture->nShaderFlags & QER_NOCARVE))
{
continue;
}
for (i=0 ; i<3 ; i++)
if (b->mins[i] >= s->maxs[i] - ON_EPSILON
|| b->maxs[i] <= s->mins[i] + ON_EPSILON)
break;
if (i != 3)
continue; // definately don't touch
fragments = Brush_Subtract(s, b);
// if the brushes did not really intersect
if (fragments == s)
continue;
//
Undo_AddBrush(s);
// one extra brush chopped up
numbrushes++;
// try to merge fragments
fragments = Brush_MergeListPairs(fragments, true);
// add the fragments to the list
for (frag = fragments; frag; frag = nextfragment)
{
nextfragment = frag->next;
frag->next = NULL;
frag->owner = s->owner;
Brush_AddToList(frag, &fragmentlist);
}
// free the original brush
Brush_Free(s);
}
}
// move all fragments to the active brush list
for (frag = fragmentlist.next; frag != &fragmentlist; frag = nextfragment)
{
nextfragment = frag->next;
numfragments++;
Brush_RemoveFromList(frag);
Brush_AddToList(frag, &active_brushes);
Undo_EndBrush(frag);
}
if (numfragments == 0)
{
Sys_Printf("Selected brush%s did not intersect with any other brushes.\n",
(selected_brushes.next->next == &selected_brushes) ? "":"es");
return;
}
Sys_Printf("done. (created %d fragment%s out of %d brush%s)\n", numfragments, (numfragments == 1)?"":"s",
numbrushes, (numbrushes == 1)?"":"es");
Sys_UpdateWindows(W_ALL);
}
/*
=============
CSG_Merge
=============
*/
void CSG_Merge(void)
{
brush_t *b, *next, *newlist, *newbrush;
struct entity_s *owner;
Sys_Printf ("Merging...\n");
if (selected_brushes.next == &selected_brushes)
{
Sys_Printf("No brushes selected.\n");
return;
}
if (selected_brushes.next->next == &selected_brushes)
{
Sys_Printf("At least two brushes have to be selected.\n");
return;
}
owner = selected_brushes.next->owner;
for (b = selected_brushes.next; b != &selected_brushes; b = next)
{
next = b->next;
if (b->owner->eclass->fixedsize)
{
// can't use texture from a fixed entity, so don't subtract
Sys_Printf("Cannot add fixed size entities.\n");
return;
}
if (b->patchBrush)
{
Sys_Printf("Cannot add patches.\n");
return;
}
if (b->brush_faces->d_texture->bFromShader && (b->brush_faces->d_texture->nShaderFlags & QER_NOCARVE))
{
Sys_Printf("Cannot add brushes using shaders that don't allows CSG operations.\n");
return;
}
if (b->owner != owner)
{
Sys_Printf("Cannot add brushes from different entities.\n");
return;
}
}
newlist = NULL;
for (b = selected_brushes.next; b != &selected_brushes; b = next)
{
next = b->next;
Brush_RemoveFromList(b);
b->next = newlist;
b->prev = NULL;
newlist = b;
}
newbrush = Brush_MergeList(newlist, true);
// if the new brush would not be convex
if (!newbrush)
{
// add the brushes back into the selection
for (b = newlist; b; b = next)
{
next = b->next;
b->next = NULL;
b->prev = NULL;
Brush_AddToList(b, &selected_brushes);
}
Sys_Printf("Cannot add a set of brushes with a concave hull.\n");
return;
}
// free the original brushes
for (b = newlist; b; b = next)
{
next = b->next;
b->next = NULL;
b->prev = NULL;
Brush_Free(b);
}
Brush_AddToList(newbrush, &selected_brushes);
Sys_Printf ("done.\n");
Sys_UpdateWindows (W_ALL);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?