AASBuild_ledge.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code 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
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "AASBuild_local.h"
  23. #define LEDGE_EPSILON 0.1f
  24. //===============================================================
  25. //
  26. // idLedge
  27. //
  28. //===============================================================
  29. /*
  30. ============
  31. idLedge::idLedge
  32. ============
  33. */
  34. idLedge::idLedge( void ) {
  35. }
  36. /*
  37. ============
  38. idLedge::idLedge
  39. ============
  40. */
  41. idLedge::idLedge( const idVec3 &v1, const idVec3 &v2, const idVec3 &gravityDir, idBrushBSPNode *n ) {
  42. start = v1;
  43. end = v2;
  44. node = n;
  45. numPlanes = 4;
  46. planes[0].SetNormal( (v1 - v2).Cross( gravityDir ) );
  47. planes[0].Normalize();
  48. planes[0].FitThroughPoint( v1 );
  49. planes[1].SetNormal( (v1 - v2).Cross( planes[0].Normal() ) );
  50. planes[1].Normalize();
  51. planes[1].FitThroughPoint( v1 );
  52. planes[2].SetNormal( v1 - v2 );
  53. planes[2].Normalize();
  54. planes[2].FitThroughPoint( v1 );
  55. planes[3].SetNormal( v2 - v1 );
  56. planes[3].Normalize();
  57. planes[3].FitThroughPoint( v2 );
  58. }
  59. /*
  60. ============
  61. idLedge::AddPoint
  62. ============
  63. */
  64. void idLedge::AddPoint( const idVec3 &v ) {
  65. if ( planes[2].Distance( v ) > 0.0f ) {
  66. start = v;
  67. planes[2].FitThroughPoint( start );
  68. }
  69. if ( planes[3].Distance( v ) > 0.0f ) {
  70. end = v;
  71. planes[3].FitThroughPoint( end );
  72. }
  73. }
  74. /*
  75. ============
  76. idLedge::CreateBevels
  77. NOTE: this assumes the gravity is vertical
  78. ============
  79. */
  80. void idLedge::CreateBevels( const idVec3 &gravityDir ) {
  81. int i, j;
  82. idBounds bounds;
  83. idVec3 size, normal;
  84. bounds.Clear();
  85. bounds.AddPoint( start );
  86. bounds.AddPoint( end );
  87. size = bounds[1] - bounds[0];
  88. // plane through ledge
  89. planes[0].SetNormal( (start - end).Cross( gravityDir ) );
  90. planes[0].Normalize();
  91. planes[0].FitThroughPoint( start );
  92. // axial bevels at start and end point
  93. i = size[1] > size[0];
  94. normal = vec3_origin;
  95. normal[i] = 1.0f;
  96. j = end[i] > start[i];
  97. planes[1+j].SetNormal( normal );
  98. planes[1+!j].SetNormal( -normal );
  99. planes[1].FitThroughPoint( start );
  100. planes[2].FitThroughPoint( end );
  101. numExpandedPlanes = 3;
  102. // if additional bevels are required
  103. if ( idMath::Fabs( size[!i] ) > 0.01f ) {
  104. normal = vec3_origin;
  105. normal[!i] = 1.0f;
  106. j = end[!i] > start[!i];
  107. planes[3+j].SetNormal( normal );
  108. planes[3+!j].SetNormal( -normal );
  109. planes[3].FitThroughPoint( start );
  110. planes[4].FitThroughPoint( end );
  111. numExpandedPlanes = 5;
  112. }
  113. // opposite of first
  114. planes[numExpandedPlanes+0] = -planes[0];
  115. // number of planes used for splitting
  116. numSplitPlanes = numExpandedPlanes + 1;
  117. // top plane
  118. planes[numSplitPlanes+0].SetNormal( (start - end).Cross( planes[0].Normal() ) );
  119. planes[numSplitPlanes+0].Normalize();
  120. planes[numSplitPlanes+0].FitThroughPoint( start );
  121. // bottom plane
  122. planes[numSplitPlanes+1] = -planes[numSplitPlanes+0];
  123. // total number of planes
  124. numPlanes = numSplitPlanes + 2;
  125. }
  126. /*
  127. ============
  128. idLedge::Expand
  129. ============
  130. */
  131. void idLedge::Expand( const idBounds &bounds, float maxStepHeight ) {
  132. int i, j;
  133. idVec3 v;
  134. for ( i = 0; i < numExpandedPlanes; i++ ) {
  135. for ( j = 0; j < 3; j++ ) {
  136. if ( planes[i].Normal()[j] > 0.0f ) {
  137. v[j] = bounds[0][j];
  138. }
  139. else {
  140. v[j] = bounds[1][j];
  141. }
  142. }
  143. planes[i].SetDist( planes[i].Dist() + v * -planes[i].Normal() );
  144. }
  145. planes[numSplitPlanes+0].SetDist( planes[numSplitPlanes+0].Dist() + maxStepHeight );
  146. planes[numSplitPlanes+1].SetDist( planes[numSplitPlanes+1].Dist() + 1.0f );
  147. }
  148. /*
  149. ============
  150. idLedge::ChopWinding
  151. ============
  152. */
  153. idWinding *idLedge::ChopWinding( const idWinding *winding ) const {
  154. int i;
  155. idWinding *w;
  156. w = winding->Copy();
  157. for ( i = 0; i < numPlanes && w; i++ ) {
  158. w = w->Clip( -planes[i], ON_EPSILON, true );
  159. }
  160. return w;
  161. }
  162. /*
  163. ============
  164. idLedge::PointBetweenBounds
  165. ============
  166. */
  167. bool idLedge::PointBetweenBounds( const idVec3 &v ) const {
  168. return ( planes[2].Distance( v ) < LEDGE_EPSILON ) && ( planes[3].Distance( v ) < LEDGE_EPSILON );
  169. }
  170. //===============================================================
  171. //
  172. // idAASBuild
  173. //
  174. //===============================================================
  175. /*
  176. ============
  177. idAASBuild::LedgeSubdivFlood_r
  178. ============
  179. */
  180. void idAASBuild::LedgeSubdivFlood_r( idBrushBSPNode *node, const idLedge *ledge ) {
  181. int s1, i;
  182. idBrushBSPPortal *p1;
  183. idWinding *w;
  184. idList<idBrushBSPNode *> nodeList;
  185. if ( node->GetFlags() & NODE_VISITED ) {
  186. return;
  187. }
  188. // if this is not already a ledge area
  189. if ( !( node->GetFlags() & AREA_LEDGE ) ) {
  190. for ( p1 = node->GetPortals(); p1; p1 = p1->Next(s1) ) {
  191. s1 = (p1->GetNode(1) == node);
  192. if ( !(p1->GetFlags() & FACE_FLOOR) ) {
  193. continue;
  194. }
  195. // split the area if some part of the floor portal is inside the expanded ledge
  196. w = ledge->ChopWinding( p1->GetWinding() );
  197. if ( !w ) {
  198. continue;
  199. }
  200. delete w;
  201. for ( i = 0; i < ledge->numSplitPlanes; i++ ) {
  202. if ( node->PlaneSide( ledge->planes[i], 0.1f ) != SIDE_CROSS ) {
  203. continue;
  204. }
  205. if ( !node->Split( ledge->planes[i], -1 ) ) {
  206. continue;
  207. }
  208. numLedgeSubdivisions++;
  209. DisplayRealTimeString( "\r%6d", numLedgeSubdivisions );
  210. node->GetChild(0)->SetFlag( NODE_VISITED );
  211. LedgeSubdivFlood_r( node->GetChild(1), ledge );
  212. return;
  213. }
  214. node->SetFlag( AREA_LEDGE );
  215. break;
  216. }
  217. }
  218. node->SetFlag( NODE_VISITED );
  219. // get all nodes we might need to flood into
  220. for ( p1 = node->GetPortals(); p1; p1 = p1->Next(s1) ) {
  221. s1 = (p1->GetNode(1) == node);
  222. if ( p1->GetNode( !s1 )->GetContents() & AREACONTENTS_SOLID ) {
  223. continue;
  224. }
  225. // flood through this portal if the portal is partly inside the expanded ledge
  226. w = ledge->ChopWinding( p1->GetWinding() );
  227. if ( !w ) {
  228. continue;
  229. }
  230. delete w;
  231. // add to list, cannot flood directly cause portals might be split on the way
  232. nodeList.Append( p1->GetNode( !s1 ) );
  233. }
  234. // flood into other nodes
  235. for ( i = 0; i < nodeList.Num(); i++ ) {
  236. LedgeSubdivLeafNodes_r( nodeList[i], ledge );
  237. }
  238. }
  239. /*
  240. ============
  241. idAASBuild::LedgeSubdivLeafNodes_r
  242. The node the ledge was originally part of might be split by other ledges.
  243. Here we recurse down the tree from the original node to find all the new leaf nodes the ledge might be part of.
  244. ============
  245. */
  246. void idAASBuild::LedgeSubdivLeafNodes_r( idBrushBSPNode *node, const idLedge *ledge ) {
  247. if ( !node ) {
  248. return;
  249. }
  250. if ( !node->GetChild(0) && !node->GetChild(1) ) {
  251. LedgeSubdivFlood_r( node, ledge );
  252. return;
  253. }
  254. LedgeSubdivLeafNodes_r( node->GetChild(0), ledge );
  255. LedgeSubdivLeafNodes_r( node->GetChild(1), ledge );
  256. }
  257. /*
  258. ============
  259. idAASBuild::LedgeSubdiv
  260. ============
  261. */
  262. void idAASBuild::LedgeSubdiv( idBrushBSPNode *root ) {
  263. int i, j;
  264. idBrush *brush;
  265. idList<idBrushSide *> sideList;
  266. // create ledge bevels and expand ledges
  267. for ( i = 0; i < ledgeList.Num(); i++ ) {
  268. ledgeList[i].CreateBevels( aasSettings->gravityDir );
  269. ledgeList[i].Expand( aasSettings->boundingBoxes[0], aasSettings->maxStepHeight );
  270. // if we should write out a ledge map
  271. if ( ledgeMap ) {
  272. sideList.SetNum( 0 );
  273. for ( j = 0; j < ledgeList[i].numPlanes; j++ ) {
  274. sideList.Append( new idBrushSide( ledgeList[i].planes[j], -1 ) );
  275. }
  276. brush = new idBrush();
  277. brush->FromSides( sideList );
  278. ledgeMap->WriteBrush( brush );
  279. delete brush;
  280. }
  281. // flood tree from the ledge node and subdivide areas with the ledge
  282. LedgeSubdivLeafNodes_r( ledgeList[i].node, &ledgeList[i] );
  283. // remove the node visited flags
  284. ledgeList[i].node->RemoveFlagRecurseFlood( NODE_VISITED );
  285. }
  286. }
  287. /*
  288. ============
  289. idAASBuild::IsLedgeSide_r
  290. ============
  291. */
  292. bool idAASBuild::IsLedgeSide_r( idBrushBSPNode *node, idFixedWinding *w, const idPlane &plane, const idVec3 &normal, const idVec3 &origin, const float radius ) {
  293. int res, i;
  294. idFixedWinding back;
  295. float dist;
  296. if ( !node ) {
  297. return false;
  298. }
  299. while ( node->GetChild(0) && node->GetChild(1) ) {
  300. dist = node->GetPlane().Distance( origin );
  301. if ( dist > radius ) {
  302. res = SIDE_FRONT;
  303. }
  304. else if ( dist < -radius ) {
  305. res = SIDE_BACK;
  306. }
  307. else {
  308. res = w->Split( &back, node->GetPlane(), LEDGE_EPSILON );
  309. }
  310. if ( res == SIDE_FRONT ) {
  311. node = node->GetChild(0);
  312. }
  313. else if ( res == SIDE_BACK ) {
  314. node = node->GetChild(1);
  315. }
  316. else if ( res == SIDE_ON ) {
  317. // continue with the side the winding faces
  318. if ( node->GetPlane().Normal() * normal > 0.0f ) {
  319. node = node->GetChild(0);
  320. }
  321. else {
  322. node = node->GetChild(1);
  323. }
  324. }
  325. else {
  326. if ( IsLedgeSide_r( node->GetChild(1), &back, plane, normal, origin, radius ) ) {
  327. return true;
  328. }
  329. node = node->GetChild(0);
  330. }
  331. }
  332. if ( node->GetContents() & AREACONTENTS_SOLID ) {
  333. return false;
  334. }
  335. for ( i = 0; i < w->GetNumPoints(); i++ ) {
  336. if ( plane.Distance( (*w)[i].ToVec3() ) > 0.0f ) {
  337. return true;
  338. }
  339. }
  340. return false;
  341. }
  342. /*
  343. ============
  344. idAASBuild::AddLedge
  345. ============
  346. */
  347. void idAASBuild::AddLedge( const idVec3 &v1, const idVec3 &v2, idBrushBSPNode *node ) {
  348. int i, j, merged;
  349. // first try to merge the ledge with existing ledges
  350. merged = -1;
  351. for ( i = 0; i < ledgeList.Num(); i++ ) {
  352. for ( j = 0; j < 2; j++ ) {
  353. if ( idMath::Fabs( ledgeList[i].planes[j].Distance( v1 ) ) > LEDGE_EPSILON ) {
  354. break;
  355. }
  356. if ( idMath::Fabs( ledgeList[i].planes[j].Distance( v2 ) ) > LEDGE_EPSILON ) {
  357. break;
  358. }
  359. }
  360. if ( j < 2 ) {
  361. continue;
  362. }
  363. if ( !ledgeList[i].PointBetweenBounds( v1 ) &&
  364. !ledgeList[i].PointBetweenBounds( v2 ) ) {
  365. continue;
  366. }
  367. if ( merged == -1 ) {
  368. ledgeList[i].AddPoint( v1 );
  369. ledgeList[i].AddPoint( v2 );
  370. merged = i;
  371. }
  372. else {
  373. ledgeList[merged].AddPoint( ledgeList[i].start );
  374. ledgeList[merged].AddPoint( ledgeList[i].end );
  375. ledgeList.RemoveIndex(i);
  376. break;
  377. }
  378. }
  379. // if the ledge could not be merged
  380. if ( merged == -1 ) {
  381. ledgeList.Append( idLedge( v1, v2, aasSettings->gravityDir, node ) );
  382. }
  383. }
  384. /*
  385. ============
  386. idAASBuild::FindLeafNodeLedges
  387. ============
  388. */
  389. void idAASBuild::FindLeafNodeLedges( idBrushBSPNode *root, idBrushBSPNode *node ) {
  390. int s1, i;
  391. idBrushBSPPortal *p1;
  392. idWinding *w;
  393. idVec3 v1, v2, normal, origin;
  394. idFixedWinding winding;
  395. idBounds bounds;
  396. idPlane plane;
  397. float radius;
  398. for ( p1 = node->GetPortals(); p1; p1 = p1->Next(s1) ) {
  399. s1 = (p1->GetNode(1) == node);
  400. if ( !(p1->GetFlags() & FACE_FLOOR) ) {
  401. continue;
  402. }
  403. if ( s1 ) {
  404. plane = p1->GetPlane();
  405. w = p1->GetWinding()->Reverse();
  406. }
  407. else {
  408. plane = -p1->GetPlane();
  409. w = p1->GetWinding();
  410. }
  411. for ( i = 0; i < w->GetNumPoints(); i++ ) {
  412. v1 = (*w)[i].ToVec3();
  413. v2 = (*w)[(i+1)%w->GetNumPoints()].ToVec3();
  414. normal = (v2 - v1).Cross( aasSettings->gravityDir );
  415. if ( normal.Normalize() < 0.5f ) {
  416. continue;
  417. }
  418. winding.Clear();
  419. winding += v1 + normal * LEDGE_EPSILON * 0.5f;
  420. winding += v2 + normal * LEDGE_EPSILON * 0.5f;
  421. winding += winding[1].ToVec3() + ( aasSettings->maxStepHeight + 1.0f ) * aasSettings->gravityDir;
  422. winding += winding[0].ToVec3() + ( aasSettings->maxStepHeight + 1.0f ) * aasSettings->gravityDir;
  423. winding.GetBounds( bounds );
  424. origin = (bounds[1] - bounds[0]) * 0.5f;
  425. radius = origin.Length() + LEDGE_EPSILON;
  426. origin = bounds[0] + origin;
  427. plane.FitThroughPoint( v1 + aasSettings->maxStepHeight * aasSettings->gravityDir );
  428. if ( !IsLedgeSide_r( root, &winding, plane, normal, origin, radius ) ) {
  429. continue;
  430. }
  431. AddLedge( v1, v2, node );
  432. }
  433. if ( w != p1->GetWinding() ) {
  434. delete w;
  435. }
  436. }
  437. }
  438. /*
  439. ============
  440. idAASBuild::FindLedges_r
  441. ============
  442. */
  443. void idAASBuild::FindLedges_r( idBrushBSPNode *root, idBrushBSPNode *node ) {
  444. if ( !node ) {
  445. return;
  446. }
  447. if ( node->GetContents() & AREACONTENTS_SOLID ) {
  448. return;
  449. }
  450. if ( !node->GetChild(0) && !node->GetChild(1) ) {
  451. if ( node->GetFlags() & NODE_VISITED ) {
  452. return;
  453. }
  454. FindLeafNodeLedges( root, node );
  455. node->SetFlag( NODE_VISITED );
  456. return;
  457. }
  458. FindLedges_r( root, node->GetChild(0) );
  459. FindLedges_r( root, node->GetChild(1) );
  460. }
  461. /*
  462. ============
  463. idAASBuild::WriteLedgeMap
  464. ============
  465. */
  466. void idAASBuild::WriteLedgeMap( const idStr &fileName, const idStr &ext ) {
  467. ledgeMap = new idBrushMap( fileName, ext );
  468. ledgeMap->SetTexture( "textures/base_trim/bluetex4q_ed" );
  469. }
  470. /*
  471. ============
  472. idAASBuild::LedgeSubdivision
  473. NOTE: this assumes the bounding box is higher than the maximum step height
  474. only ledges with vertical sides are considered
  475. ============
  476. */
  477. void idAASBuild::LedgeSubdivision( idBrushBSP &bsp ) {
  478. numLedgeSubdivisions = 0;
  479. ledgeList.Clear();
  480. common->Printf( "[Ledge Subdivision]\n" );
  481. bsp.GetRootNode()->RemoveFlagRecurse( NODE_VISITED );
  482. FindLedges_r( bsp.GetRootNode(), bsp.GetRootNode() );
  483. bsp.GetRootNode()->RemoveFlagRecurse( NODE_VISITED );
  484. common->Printf( "\r%6d ledges\n", ledgeList.Num() );
  485. LedgeSubdiv( bsp.GetRootNode() );
  486. common->Printf( "\r%6d subdivisions\n", numLedgeSubdivisions );
  487. }