2 BobToolz plugin for GtkRadiant
3 Copyright (C) 2001 Gordon Biggans
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // DBrush.cpp: implementation of the DBrush class.
22 //////////////////////////////////////////////////////////////////////
27 #pragma warning(disable : 4786)
40 #include "dialogs/dialogs-gtk.h"
46 #include "generic/referencecounted.h"
50 //////////////////////////////////////////////////////////////////////
51 // Construction/Destruction
52 //////////////////////////////////////////////////////////////////////
54 DBrush::DBrush(int ID)
67 //////////////////////////////////////////////////////////////////////
69 //////////////////////////////////////////////////////////////////////
71 DPlane* DBrush::AddFace(const vec3_t va, const vec3_t vb, const vec3_t vc, const _QERFaceData* texData)
74 // Sys_Printf("(%f %f %f) (%f %f %f) (%f %f %f)\n", va[0], va[1], va[2], vb[0], vb[1], vb[2], vc[0], vc[1], vc[2]);
77 DPlane* newFace = new DPlane(va, vb, vc, texData);
78 faceList.push_back(newFace);
83 int DBrush::BuildPoints()
87 if(faceList.size() <= 3) // if less than 3 faces, there can be no points
88 return 0; // with only 3 faces u can't have a bounded soild
90 for(std::list<DPlane *>::const_iterator p1=faceList.begin(); p1!=faceList.end(); p1++)
92 std::list<DPlane *>::const_iterator p2=p1;
93 for(p2++; p2!=faceList.end(); p2++)
95 std::list<DPlane *>::const_iterator p3=p2;
96 for(p3++; p3!=faceList.end(); p3++)
99 if((*p1)->PlaneIntersection(*p2, *p3, pnt))
101 int pos = PointPosition(pnt);
103 if(pos == POINT_IN_BRUSH)
104 { // ???? shouldn't happen here
105 globalErrorStream() << "ERROR:: Build Brush Points: Point IN brush!!!\n";
107 else if(pos == POINT_ON_BRUSH)
112 Sys_Printf("Duplicate Point Found, pyramids ahoy!!!!!\n");*/
113 // point lies on more that 3 planes
116 // otherwise point is removed due to another plane..
118 // Sys_Printf("(%f, %f, %f)\n", pnt[0], pnt[1], pnt[2]);
125 // Sys_Printf("%i points on brush\n", pointList.size());
128 return pointList.size();
131 void DBrush_addFace(DBrush& brush, const _QERFaceData& faceData)
133 brush.AddFace(vector3_to_array(faceData.m_p0), vector3_to_array(faceData.m_p1), vector3_to_array(faceData.m_p2), 0);
135 typedef ReferenceCaller1<DBrush, const _QERFaceData&, DBrush_addFace> DBrushAddFaceCaller;
137 void DBrush_addFaceTextured(DBrush& brush, const _QERFaceData& faceData)
139 brush.AddFace(vector3_to_array(faceData.m_p0), vector3_to_array(faceData.m_p1), vector3_to_array(faceData.m_p2), &faceData);
141 typedef ReferenceCaller1<DBrush, const _QERFaceData&, DBrush_addFaceTextured> DBrushAddFaceTexturedCaller;
143 void DBrush::LoadFromBrush(scene::Node& brush, bool textured)
148 GlobalBrushCreator().forEachBrushFace(brush, textured ? BrushFaceDataCallback(DBrushAddFaceTexturedCaller(*this)) : BrushFaceDataCallback(DBrushAddFaceCaller(*this)));
153 int DBrush::PointPosition(vec3_t pnt)
155 int state = POINT_IN_BRUSH; // if nothing happens point is inside brush
157 for(std::list<DPlane *>::const_iterator chkPlane=faceList.begin(); chkPlane!=faceList.end(); chkPlane++)
159 float dist = (*chkPlane)->DistanceToPoint(pnt);
161 if(dist > MAX_ROUND_ERROR)
162 return POINT_OUT_BRUSH; // if point is in front of plane, it CANT be in the brush
163 else if(fabs(dist) < MAX_ROUND_ERROR)
164 state = POINT_ON_BRUSH; // if point is ON plane point is either ON the brush
165 // or outside it, it can no longer be in it
171 void DBrush::ClearPoints()
173 for(std::list<DPoint *>::const_iterator deadPoint=pointList.begin(); deadPoint!=pointList.end(); deadPoint++) {
179 void DBrush::ClearFaces()
181 bBoundsBuilt = false;
182 for(std::list<DPlane *>::const_iterator deadPlane=faceList.begin(); deadPlane!=faceList.end(); deadPlane++)
189 void DBrush::AddPoint(vec3_t pnt)
191 DPoint* newPoint = new DPoint;
192 VectorCopy(pnt, newPoint->_pnt);
193 pointList.push_back(newPoint);
196 bool DBrush::HasPoint(vec3_t pnt)
198 for(std::list<DPoint *>::const_iterator chkPoint=pointList.begin(); chkPoint!=pointList.end(); chkPoint++)
200 if(**chkPoint == pnt)
207 int DBrush::RemoveRedundantPlanes()
210 std::list<DPlane *>::iterator chkPlane;
212 // find duplicate planes
213 std::list<DPlane *>::iterator p1=faceList.begin();
215 while( p1!=faceList.end() )
217 std::list<DPlane *>::iterator p2 = p1;
219 for(p2++; p2!=faceList.end(); p2++)
223 if(!strcmp((*p1)->m_shader.c_str(), "textures/common/caulk"))
226 p1 = faceList.erase(p1); // duplicate plane
231 p2 = faceList.erase(p2); // duplicate plane
239 if( p2 == faceList.end() )
243 //+djbob kill planes with bad normal, they are more of a nuisance than losing a brush
244 chkPlane=faceList.begin();
245 while( chkPlane!=faceList.end() )
247 if(VectorLength((*chkPlane)->normal) == 0) // plane has bad normal
250 chkPlane = faceList.erase(chkPlane);
258 if(pointList.size() == 0) // if points may not have been built, build them
259 /* if(BuildPoints() == 0) // just let the planes die if they are all bad
263 chkPlane=faceList.begin();
264 while(chkPlane != faceList.end())
266 if((*chkPlane)->IsRedundant(pointList)) // checks that plane "0wnz" :), 3 or more points
269 chkPlane = faceList.erase(chkPlane);
279 bool DBrush::GetBounds(vec3_t min, vec3_t max)
286 VectorCopy(bbox_min, min);
287 VectorCopy(bbox_max, max);
292 bool DBrush::BBoxCollision(DBrush* chkBrush)
297 GetBounds(min1, max1);
298 chkBrush->GetBounds(min2, max2);
300 if(min1[0] >= max2[0])
302 if(min1[1] >= max2[1])
304 if(min1[2] >= max2[2])
307 if(max1[0] <= min2[0])
309 if(max1[1] <= min2[1])
311 if(max1[2] <= min2[2])
317 DPlane* DBrush::HasPlane(DPlane* chkPlane)
319 for(std::list<DPlane *>::const_iterator brushPlane=faceList.begin(); brushPlane!=faceList.end(); brushPlane++)
321 if(**brushPlane == *chkPlane)
327 bool DBrush::IsCutByPlane(DPlane *cuttingPlane)
331 if(pointList.size() == 0)
332 if(BuildPoints() == 0)
335 std::list<DPoint *>::const_iterator chkPnt = pointList.begin();
337 if(chkPnt == pointList.end())
340 float dist = cuttingPlane->DistanceToPoint((*chkPnt)->_pnt);
342 if(dist > MAX_ROUND_ERROR)
344 else if(dist < MAX_ROUND_ERROR)
349 for(chkPnt++=pointList.begin(); chkPnt!=pointList.end(); chkPnt++)
351 dist = cuttingPlane->DistanceToPoint((*chkPnt)->_pnt);
353 if(dist > MAX_ROUND_ERROR)
358 else if(dist < MAX_ROUND_ERROR)
371 scene::Node* DBrush::BuildInRadiant(bool allowDestruction, int* changeCnt, scene::Node* entity)
377 for(std::list<DPlane *>::const_iterator chkPlane=faceList.begin(); chkPlane!=faceList.end(); chkPlane++)
379 if((*chkPlane)->m_bChkOk)
389 //+djbob: fixed bug when brush had no faces "phantom brush" in radiant.
390 if(faceList.size() < 4)
392 globalErrorStream() << "Possible Phantom Brush Found, will not rebuild\n";
397 scene::Node& node = GlobalBrushCreator().createBrush();
399 for(std::list<DPlane *>::const_iterator buildPlane=faceList.begin(); buildPlane!=faceList.end(); buildPlane++) {
400 if((*buildPlane)->AddToBrush(node) && changeCnt) {
406 Node_getTraversable(*entity)->insert(node);
408 Node_getTraversable(GlobalRadiant().getMapWorldEntity())->insert(node);
414 void DBrush::CutByPlane(DPlane *cutPlane, DBrush **newBrush1, DBrush **newBrush2)
416 if(!IsCutByPlane(cutPlane))
423 DBrush* b1 = new DBrush;
424 DBrush* b2 = new DBrush;
426 for(std::list<DPlane *>::const_iterator parsePlane=faceList.begin(); parsePlane!=faceList.end(); parsePlane++)
428 b1->AddFace((*parsePlane)->points[0], (*parsePlane)->points[1], (*parsePlane)->points[2], NULL);
429 b2->AddFace((*parsePlane)->points[0], (*parsePlane)->points[1], (*parsePlane)->points[2], NULL);
432 b1->AddFace(cutPlane->points[0], cutPlane->points[1], cutPlane->points[2], NULL);
433 b2->AddFace(cutPlane->points[2], cutPlane->points[1], cutPlane->points[0], NULL);
435 b1->RemoveRedundantPlanes();
436 b2->RemoveRedundantPlanes();
442 bool DBrush::IntersectsWith(DBrush *chkBrush)
444 if(pointList.size() == 0)
445 if(BuildPoints() == 0)
446 return false; // invalid brush!!!!
448 if(chkBrush->pointList.size() == 0)
449 if(chkBrush->BuildPoints() == 0)
450 return false; // invalid brush!!!!
452 if(!BBoxCollision(chkBrush))
455 std::list<DPlane *>::const_iterator iplPlane;
457 for( iplPlane=faceList.begin(); iplPlane!=faceList.end(); iplPlane++)
460 bool allInFront = true;
461 for(std::list<DPoint *>::const_iterator iPoint=chkBrush->pointList.begin(); iPoint!=chkBrush->pointList.end(); iPoint++)
463 if((*iplPlane)->DistanceToPoint((*iPoint)->_pnt) < -MAX_ROUND_ERROR)
473 for( iplPlane=chkBrush->faceList.begin(); iplPlane!=chkBrush->faceList.end(); iplPlane++)
475 bool allInFront = true;
476 for(std::list<DPoint *>::const_iterator iPoint=pointList.begin(); iPoint!=pointList.end(); iPoint++)
478 if((*iplPlane)->DistanceToPoint((*iPoint)->_pnt) < -MAX_ROUND_ERROR)
491 bool DBrush::IntersectsWith(DPlane* p1, DPlane* p2, vec3_t v) {
492 vec3_t vDown = { 0, 0, -1 };
494 std::list<DPlane *>::const_iterator iplPlane;
495 for( iplPlane = faceList.begin(); iplPlane != faceList.end(); iplPlane++) {
496 DPlane* p = (*iplPlane);
498 vec_t d = DotProduct( p->normal, vDown );
502 if(p->PlaneIntersection(p1, p2, v)) {
503 if(PointPosition( v ) != POINT_OUT_BRUSH) {
512 void DBrush::BuildBounds()
516 if(pointList.size() == 0) // if points may not have been built, build them
517 if(BuildPoints() == 0)
520 std::list<DPoint *>::const_iterator first = pointList.begin();
521 VectorCopy((*first)->_pnt, bbox_min);
522 VectorCopy((*first)->_pnt, bbox_max);
524 std::list<DPoint *>::const_iterator point=pointList.begin();
525 for( point++; point!=pointList.end(); point++)
527 if((*point)->_pnt[0] > bbox_max[0])
528 bbox_max[0] = (*point)->_pnt[0];
529 if((*point)->_pnt[1] > bbox_max[1])
530 bbox_max[1] = (*point)->_pnt[1];
531 if((*point)->_pnt[2] > bbox_max[2])
532 bbox_max[2] = (*point)->_pnt[2];
534 if((*point)->_pnt[0] < bbox_min[0])
535 bbox_min[0] = (*point)->_pnt[0];
536 if((*point)->_pnt[1] < bbox_min[1])
537 bbox_min[1] = (*point)->_pnt[1];
538 if((*point)->_pnt[2] < bbox_min[2])
539 bbox_min[2] = (*point)->_pnt[2];
546 bool DBrush::BBoxTouch(DBrush *chkBrush)
551 GetBounds(min1, max1);
552 chkBrush->GetBounds(min2, max2);
554 if((min1[0] - max2[0]) > MAX_ROUND_ERROR)
556 if((min1[1] - max2[1]) > MAX_ROUND_ERROR)
558 if((min1[2] - max2[2]) > MAX_ROUND_ERROR)
561 if((min2[0] - max1[0]) > MAX_ROUND_ERROR)
563 if((min2[1] - max1[1]) > MAX_ROUND_ERROR)
565 if((min2[2] - max1[2]) > MAX_ROUND_ERROR)
570 if((min2[0] - max1[0]) == 0)
573 if((min2[1] - max1[1]) == 0)
576 if((min2[2] - max1[2]) == 0)
579 if((min1[0] - max2[0]) == 0)
582 if((min1[1] - max2[1]) == 0)
585 if((min1[2] - max2[2]) == 0)
594 void DBrush::ResetChecks(std::list<Str>* exclusionList)
596 for(std::list<DPlane *>::const_iterator resetPlane=faceList.begin(); resetPlane!=faceList.end(); resetPlane++)
602 for(std::list<Str>::iterator eTexture = exclusionList->begin(); eTexture != exclusionList->end(); eTexture++)
604 if(strstr((*resetPlane)->m_shader.c_str(), eTexture->GetBuffer()))
612 (*resetPlane)->m_bChkOk = set;
616 DPlane* DBrush::HasPlaneInverted(DPlane *chkPlane)
618 for(std::list<DPlane *>::const_iterator brushPlane=faceList.begin(); brushPlane!=faceList.end(); brushPlane++)
620 if(**brushPlane != *chkPlane)
622 if(fabs((*brushPlane)->_d + chkPlane->_d) < 0.1)
623 return (*brushPlane);
629 bool DBrush::HasTexture(const char *textureName)
631 for(std::list<DPlane *>::const_iterator chkPlane=faceList.begin(); chkPlane!=faceList.end(); chkPlane++)
633 if(strstr((*chkPlane)->m_shader.c_str(), textureName))
640 bool DBrush::IsDetail()
642 for(std::list<DPlane *>::const_iterator chkPlane=faceList.begin(); chkPlane!=faceList.end(); chkPlane++)
644 if((*chkPlane)->texInfo.contents & FACE_DETAIL)
651 void DBrush::BuildFromWinding(DWinding *w)
655 globalErrorStream() << "Winding has invalid number of points";
659 DPlane* wPlane = w->WindingPlane();
662 w2 = w->CopyWinding();
664 for(i = 0; i < w2->numpoints; i++)
665 VectorAdd(w2->p[i], wPlane->normal, w2->p[i]);
667 AddFace(w2->p[0], w2->p[1], w2->p[2], NULL);
668 AddFace(w->p[2], w->p[1], w->p[0], NULL);
670 for(i = 0; i < w->numpoints-1; i++)
671 AddFace(w2->p[i], w->p[i], w->p[i+1], NULL);
672 AddFace(w2->p[w->numpoints-1], w->p[w->numpoints-1], w->p[0], NULL);
678 void DBrush::SaveToFile(FILE *pFile)
680 fprintf(pFile, "{\n");
682 for(std::list<DPlane *>::const_iterator pp=faceList.begin(); pp!=faceList.end(); pp++)
686 sprintf(buffer, "( %.0f %.0f %.0f ) ( %.0f %.0f %.0f ) ( %.0f %.0f %.0f ) %s %.0f %.0f %f %f %.0f 0 0 0\n",
687 (*pp)->points[0][0], (*pp)->points[0][1], (*pp)->points[0][2],
688 (*pp)->points[1][0], (*pp)->points[1][1], (*pp)->points[1][2],
689 (*pp)->points[2][0], (*pp)->points[2][1], (*pp)->points[2][2],
690 (*pp)->m_shader.c_str(),
691 (*pp)->texInfo.m_texdef.shift[0], (*pp)->texInfo.m_texdef.shift[1],
692 (*pp)->texInfo.m_texdef.scale[0], (*pp)->texInfo.m_texdef.scale[0],
693 (*pp)->texInfo.m_texdef.rotate);
695 fprintf(pFile, buffer);
698 fprintf(pFile, "}\n");
701 void DBrush::Rotate(vec3_t vOrigin, vec3_t vRotation)
703 for(std::list<DPlane *>::const_iterator rotPlane=faceList.begin(); rotPlane!=faceList.end(); rotPlane++)
705 for(int i = 0; i < 3; i++)
706 VectorRotate((*rotPlane)->points[i], vRotation, vOrigin);
708 (*rotPlane)->Rebuild();
712 void DBrush::RotateAboutCentre(vec3_t vRotation)
714 vec3_t min, max, centre;
716 VectorAdd(min, max, centre);
717 VectorScale(centre, 0.5f, centre);
719 Rotate(centre, vRotation);
722 bool DBrush::ResetTextures(const char* textureName, float fScale[2], float fShift[2], int rotation, const char* newTextureName,
723 int bResetTextureName, int bResetScale[2], int bResetShift[2], int bResetRotation)
727 bool changed = false;
728 for(std::list<DPlane *>::const_iterator resetPlane=faceList.begin(); resetPlane!=faceList.end(); resetPlane++)
730 if(!strcmp((*resetPlane)->m_shader.c_str(), textureName))
732 if(bResetTextureName)
733 (*resetPlane)->m_shader = newTextureName;
736 (*resetPlane)->texInfo.m_texdef.scale[0] = fScale[0];
738 (*resetPlane)->texInfo.m_texdef.scale[1] = fScale[1];
741 (*resetPlane)->texInfo.m_texdef.shift[0] = fShift[0];
743 (*resetPlane)->texInfo.m_texdef.shift[1] = fShift[1];
746 (*resetPlane)->texInfo.m_texdef.rotate = (float)rotation;
751 return changed; // no point rebuilding unless we need to, only slows things down
755 for(std::list<DPlane *>::const_iterator resetPlane=faceList.begin(); resetPlane!=faceList.end(); resetPlane++)
757 if(bResetTextureName)
758 (*resetPlane)->m_shader = newTextureName;
761 (*resetPlane)->texInfo.m_texdef.scale[0] = fScale[0];
763 (*resetPlane)->texInfo.m_texdef.scale[1] = fScale[1];
766 (*resetPlane)->texInfo.m_texdef.shift[0] = fShift[0];
768 (*resetPlane)->texInfo.m_texdef.shift[1] = fShift[1];
771 (*resetPlane)->texInfo.m_texdef.rotate = (float)rotation;
777 bool DBrush::operator ==(DBrush* other)
779 std::list<DPlane *>::const_iterator chkPlane;
781 for(chkPlane=faceList.begin(); chkPlane!=faceList.end(); chkPlane++)
783 if(!other->HasPlane((*chkPlane)))
787 for(chkPlane=faceList.begin(); chkPlane!=faceList.end(); chkPlane++)
789 if(!HasPlane((*chkPlane)))
796 DPlane* DBrush::AddFace(const vec3_t va, const vec3_t vb, const vec3_t vc, const char *textureName, bool bDetail)
798 bBoundsBuilt = false;
799 DPlane* newFace = new DPlane(va, vb, vc, textureName, bDetail);
800 faceList.push_back(newFace);
805 DPlane* DBrush::FindPlaneWithClosestNormal( vec_t* normal ) {
807 DPlane* bestDotPlane = NULL;
808 std::list<DPlane *>::const_iterator chkPlane;
809 for( chkPlane = faceList.begin(); chkPlane != faceList.end(); chkPlane++ ) {
810 DPlane* pPlane = (*chkPlane);
812 vec_t dot = DotProduct( pPlane->normal, normal );
813 if( dot > bestDot ) {
815 bestDotPlane = pPlane;
822 int DBrush::FindPointsForPlane( DPlane* plane, DPoint** pnts, int maxpnts ) {
831 for( std::list<DPoint *>::const_iterator points = pointList.begin(); points != pointList.end(); points++ ) {
832 DPoint* point = (*points);
834 if( fabs(plane->DistanceToPoint( point->_pnt )) < MAX_ROUND_ERROR ) {
835 pnts[numpnts] = point;
838 if(numpnts >= maxpnts) {
848 void DBrush::RemovePlane( DPlane* plane ) {
849 bBoundsBuilt = false;
850 for( std::list<DPlane *>::const_iterator deadPlane = faceList.begin(); deadPlane != faceList.end(); deadPlane++ ) {
851 if(*deadPlane == plane) {
853 faceList.remove( plane );