terrCollision.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "terrain/terrCollision.h"
  24. #include "terrain/terrData.h"
  25. #include "collision/abstractPolyList.h"
  26. #include "collision/collision.h"
  27. const F32 TerrainThickness = 0.5f;
  28. static const U32 MaxExtent = 256;
  29. #define MAX_FLOAT 1e20f
  30. //----------------------------------------------------------------------------
  31. Convex sTerrainConvexList;
  32. // Number of vertices followed by point index
  33. S32 sVertexList[5][5] = {
  34. { 3, 1,2,3 }, // 135 B
  35. { 3, 0,1,3 }, // 135 A
  36. { 3, 0,2,3 }, // 45 B
  37. { 3, 0,1,2 }, // 45 A
  38. { 4, 0,1,2,3 } // Convex square
  39. };
  40. // Number of edges followed by edge index pairs
  41. S32 sEdgeList45[16][11] = {
  42. { 0 }, //
  43. { 0 },
  44. { 0 },
  45. { 1, 0,1 }, // 0-1
  46. { 0 },
  47. { 1, 0,1 }, // 0-2
  48. { 1, 0,1 }, // 1-2
  49. { 3, 0,1,1,2,2,0 }, // 0-1,1-2,2-0
  50. { 0 },
  51. { 0,}, //
  52. { 0 },
  53. { 1, 0,1 }, // 0-1,
  54. { 0, }, //
  55. { 1, 0,1 }, // 0-2,
  56. { 1, 0,1 }, // 1-2
  57. { 3, 0,1,1,2,0,2 },
  58. };
  59. S32 sEdgeList135[16][11] = {
  60. { 0 },
  61. { 0 },
  62. { 0 },
  63. { 1, 0,1 }, // 0-1
  64. { 0 },
  65. { 0 },
  66. { 1, 0,1 }, // 1-2
  67. { 2, 0,1,1,2 }, // 0-1,1-2
  68. { 0 },
  69. { 0, }, //
  70. { 1, 0,1 }, // 1-3
  71. { 2, 0,1,1,2 }, // 0-1,1-3,
  72. { 0 }, //
  73. { 0 }, //
  74. { 2, 0,1,2,0 }, // 1-2,3-1
  75. { 3, 0,1,1,2,1,3 },
  76. };
  77. // On split squares, the FaceA diagnal is also removed
  78. S32 sEdgeList45A[16][11] = {
  79. { 0 }, //
  80. { 0 },
  81. { 0 },
  82. { 1, 0,1 }, // 0-1
  83. { 0 },
  84. { 0 }, //
  85. { 1, 0,1 }, // 1-2
  86. { 2, 0,1,1,2 }, // 0-1,1-2
  87. { 0 },
  88. { 0,}, //
  89. { 0 },
  90. { 1, 0,1 }, // 0-1
  91. { 0, }, //
  92. { 0, 0,1 }, //
  93. { 1, 0,1 }, // 1-2
  94. { 3, 0,1,1,2 },
  95. };
  96. S32 sEdgeList135A[16][11] = {
  97. { 0 },
  98. { 0 },
  99. { 0 },
  100. { 1, 0,1 }, // 0-1
  101. { 0 },
  102. { 0 },
  103. { 1, 0,1 }, // 1-2
  104. { 2, 0,1,1,2 }, // 0-1,1-2
  105. { 0 },
  106. { 0 }, //
  107. { 0 }, //
  108. { 1, 0,1 }, // 0-1
  109. { 0 }, //
  110. { 0 }, //
  111. { 1, 0,1 }, // 1-2
  112. { 3, 0,1,1,2 },
  113. };
  114. // Number of faces followed by normal index and vertices
  115. S32 sFaceList45[16][9] = {
  116. { 0 },
  117. { 0 },
  118. { 0 },
  119. { 0 },
  120. { 0 },
  121. { 0 },
  122. { 0 },
  123. { 1, 0,0,1,2 },
  124. { 0 },
  125. { 0 },
  126. { 0 },
  127. { 0 },
  128. { 0 },
  129. { 1, 1,0,1,2 },
  130. { 0 },
  131. { 2, 0,0,1,2, 1,0,2,3 },
  132. };
  133. S32 sFaceList135[16][9] = {
  134. { 0 },
  135. { 0 },
  136. { 0 },
  137. { 0 },
  138. { 0 },
  139. { 0 },
  140. { 0 },
  141. { 0 },
  142. { 0 },
  143. { 0 },
  144. { 0 },
  145. { 1, 0,0,1,2 },
  146. { 0 },
  147. { 0 },
  148. { 1, 1,0,1,2 },
  149. { 2, 0,0,1,3, 1,1,2,3 },
  150. };
  151. TerrainConvex::TerrainConvex()
  152. {
  153. mType = TerrainConvexType;
  154. }
  155. TerrainConvex::TerrainConvex( const TerrainConvex &cv )
  156. {
  157. mType = TerrainConvexType;
  158. // Only a partial copy...
  159. mObject = cv.mObject;
  160. split45 = cv.split45;
  161. squareId = cv.squareId;
  162. material = cv.material;
  163. point[0] = cv.point[0];
  164. point[1] = cv.point[1];
  165. point[2] = cv.point[2];
  166. point[3] = cv.point[3];
  167. normal[0] = cv.normal[0];
  168. normal[1] = cv.normal[1];
  169. box = cv.box;
  170. }
  171. Box3F TerrainConvex::getBoundingBox() const
  172. {
  173. return box;
  174. }
  175. Box3F TerrainConvex::getBoundingBox(const MatrixF&, const Point3F& ) const
  176. {
  177. // Function should not be called....
  178. return box;
  179. }
  180. Point3F TerrainConvex::support(const VectorF& v) const
  181. {
  182. S32 *vp;
  183. if (halfA)
  184. vp = square ? sVertexList[(split45 << 1) | 1]: sVertexList[4];
  185. else
  186. vp = square ? sVertexList[(split45 << 1)] : sVertexList[4];
  187. S32 *ve = vp + vp[0] + 1;
  188. const Point3F *bp = &point[vp[1]];
  189. F32 bd = mDot(*bp,v);
  190. for (vp += 2; vp < ve; vp++) {
  191. const Point3F* cp = &point[*vp];
  192. F32 dd = mDot(*cp,v);
  193. if (dd > bd) {
  194. bd = dd;
  195. bp = cp;
  196. }
  197. }
  198. return *bp;
  199. }
  200. inline bool isOnPlane(Point3F& p,PlaneF& plane)
  201. {
  202. F32 dist = mDot(plane,p) + plane.d;
  203. return dist < 0.1 && dist > -0.1;
  204. }
  205. void TerrainConvex::getFeatures(const MatrixF& mat,const VectorF& n, ConvexFeature* cf)
  206. {
  207. U32 i;
  208. cf->material = 0;
  209. cf->object = mObject;
  210. // Plane is normal n + support point
  211. PlaneF plane;
  212. plane.set(support(n),n);
  213. S32 vertexCount = cf->mVertexList.size();
  214. // Emit vertices on the plane
  215. S32* vertexListPointer;
  216. if (halfA)
  217. vertexListPointer = square ? sVertexList[(split45 << 1) | 1]: sVertexList[4];
  218. else
  219. vertexListPointer = square ? sVertexList[(split45 << 1)] : sVertexList[4];
  220. S32 pm = 0;
  221. S32 numVerts = *vertexListPointer;
  222. vertexListPointer += 1;
  223. for (i = 0; i < numVerts; i++)
  224. {
  225. Point3F& cp = point[vertexListPointer[i]];
  226. cf->mVertexList.increment();
  227. mat.mulP(cp,&cf->mVertexList.last());
  228. pm |= 1 << vertexListPointer[i];
  229. }
  230. // Emit Edges
  231. S32* ep = (square && halfA)?
  232. (split45 ? sEdgeList45A[pm]: sEdgeList135A[pm]):
  233. (split45 ? sEdgeList45[pm]: sEdgeList135[pm]);
  234. S32 numEdges = *ep;
  235. S32 edgeListStart = cf->mEdgeList.size();
  236. cf->mEdgeList.increment(numEdges);
  237. ep += 1;
  238. for (i = 0; i < numEdges; i++)
  239. {
  240. cf->mEdgeList[edgeListStart + i].vertex[0] = vertexCount + ep[i * 2 + 0];
  241. cf->mEdgeList[edgeListStart + i].vertex[1] = vertexCount + ep[i * 2 + 1];
  242. }
  243. // Emit faces
  244. S32* fp = split45 ? sFaceList45[pm]: sFaceList135[pm];
  245. S32 numFaces = *fp;
  246. fp += 1;
  247. S32 faceListStart = cf->mFaceList.size();
  248. cf->mFaceList.increment(numFaces);
  249. for (i = 0; i < numFaces; i++)
  250. {
  251. ConvexFeature::Face& face = cf->mFaceList[faceListStart + i];
  252. face.normal = normal[fp[i * 4 + 0]];
  253. face.vertex[0] = vertexCount + fp[i * 4 + 1];
  254. face.vertex[1] = vertexCount + fp[i * 4 + 2];
  255. face.vertex[2] = vertexCount + fp[i * 4 + 3];
  256. }
  257. }
  258. void TerrainConvex::getPolyList(AbstractPolyList* list)
  259. {
  260. list->setTransform(&mObject->getTransform(), mObject->getScale());
  261. list->setObject(mObject);
  262. // Emit vertices
  263. U32 array[4];
  264. U32 curr = 0;
  265. S32 numVerts;
  266. S32* vertsStart;
  267. if (halfA)
  268. {
  269. numVerts = square ? sVertexList[(split45 << 1) | 1][0] : sVertexList[4][0];
  270. vertsStart = square ? &sVertexList[(split45 << 1) | 1][1] : &sVertexList[4][1];
  271. }
  272. else
  273. {
  274. numVerts = square ? sVertexList[(split45 << 1)][0] : sVertexList[4][0];
  275. vertsStart = square ? &sVertexList[(split45 << 1)][1] : &sVertexList[4][1];
  276. }
  277. S32 pointMask = 0;
  278. for (U32 i = 0; i < numVerts; i++) {
  279. const Point3F& cp = point[vertsStart[i]];
  280. array[curr++] = list->addPoint(cp);
  281. pointMask |= (1 << vertsStart[i]);
  282. }
  283. S32 numFaces = split45 ? sFaceList45[pointMask][0] : sFaceList135[pointMask][0];
  284. S32* faceStart = split45 ? &sFaceList45[pointMask][1] : &sFaceList135[pointMask][1];
  285. for (U32 j = 0; j < numFaces; j++) {
  286. S32 plane = faceStart[0];
  287. S32 v0 = faceStart[1];
  288. S32 v1 = faceStart[2];
  289. S32 v2 = faceStart[3];
  290. list->begin(0, plane);
  291. list->vertex(array[v0]);
  292. list->vertex(array[v1]);
  293. list->vertex(array[v2]);
  294. list->plane(array[v0], array[v1], array[v2]);
  295. list->end();
  296. faceStart += 4;
  297. }
  298. }
  299. //----------------------------------------------------------------------------
  300. void TerrainBlock::buildConvex(const Box3F& box,Convex* convex)
  301. {
  302. PROFILE_SCOPE( TerrainBlock_buildConvex );
  303. sTerrainConvexList.collectGarbage();
  304. // First check to see if the query misses the
  305. // terrain elevation range.
  306. const Point3F &terrainPos = getPosition();
  307. if ( box.maxExtents.z - terrainPos.z < -TerrainThickness ||
  308. box.minExtents.z - terrainPos.z > fixedToFloat( mFile->getMaxHeight() ) )
  309. return;
  310. // Transform the bounding sphere into the object's coord space. Note that this
  311. // not really optimal.
  312. Box3F osBox = box;
  313. mWorldToObj.mul(osBox);
  314. AssertWarn(mObjScale == Point3F(1, 1, 1), "Error, handle the scale transform on the terrain");
  315. S32 xStart = (S32)mFloor( osBox.minExtents.x / mSquareSize );
  316. S32 xEnd = (S32)mCeil ( osBox.maxExtents.x / mSquareSize );
  317. S32 yStart = (S32)mFloor( osBox.minExtents.y / mSquareSize );
  318. S32 yEnd = (S32)mCeil ( osBox.maxExtents.y / mSquareSize );
  319. S32 xExt = xEnd - xStart;
  320. if (xExt > MaxExtent)
  321. xExt = MaxExtent;
  322. U16 heightMax = floatToFixed(osBox.maxExtents.z);
  323. U16 heightMin = (osBox.minExtents.z < 0)? 0: floatToFixed(osBox.minExtents.z);
  324. const U32 BlockMask = mFile->mSize - 1;
  325. for ( S32 y = yStart; y < yEnd; y++ )
  326. {
  327. S32 yi = y & BlockMask;
  328. //
  329. for ( S32 x = xStart; x < xEnd; x++ )
  330. {
  331. S32 xi = x & BlockMask;
  332. const TerrainSquare *sq = mFile->findSquare( 0, xi, yi );
  333. if ( x != xi || y != yi )
  334. continue;
  335. // holes only in the primary terrain block
  336. if ( ( ( sq->flags & TerrainSquare::Empty ) && x == xi && y == yi ) ||
  337. sq->minHeight > heightMax ||
  338. sq->maxHeight < heightMin )
  339. continue;
  340. U32 sid = (x << 16) + (y & ((1 << 16) - 1));
  341. Convex *cc = 0;
  342. // See if the square already exists as part of the working set.
  343. CollisionWorkingList& wl = convex->getWorkingList();
  344. for (CollisionWorkingList* itr = wl.wLink.mNext; itr != &wl; itr = itr->wLink.mNext)
  345. if (itr->mConvex->getType() == TerrainConvexType &&
  346. static_cast<TerrainConvex*>(itr->mConvex)->squareId == sid) {
  347. cc = itr->mConvex;
  348. break;
  349. }
  350. if (cc)
  351. continue;
  352. // Create a new convex.
  353. TerrainConvex* cp = new TerrainConvex;
  354. sTerrainConvexList.registerObject(cp);
  355. convex->addToWorkingList(cp);
  356. cp->halfA = true;
  357. cp->square = 0;
  358. cp->mObject = this;
  359. cp->squareId = sid;
  360. cp->material = mFile->getLayerIndex( xi, yi );
  361. cp->box.minExtents.set((F32)(x * mSquareSize), (F32)(y * mSquareSize), fixedToFloat( sq->minHeight ));
  362. cp->box.maxExtents.x = cp->box.minExtents.x + mSquareSize;
  363. cp->box.maxExtents.y = cp->box.minExtents.y + mSquareSize;
  364. cp->box.maxExtents.z = fixedToFloat( sq->maxHeight );
  365. mObjToWorld.mul(cp->box);
  366. // Build points
  367. Point3F* pos = cp->point;
  368. for (S32 i = 0; i < 4 ; i++,pos++) {
  369. S32 dx = i >> 1;
  370. S32 dy = dx ^ (i & 1);
  371. pos->x = (F32)((x + dx) * mSquareSize);
  372. pos->y = (F32)((y + dy) * mSquareSize);
  373. pos->z = fixedToFloat( mFile->getHeight(xi + dx, yi + dy) );
  374. }
  375. // Build normals, then split into two Convex objects if the
  376. // square is concave
  377. if ((cp->split45 = sq->flags & TerrainSquare::Split45) == true) {
  378. VectorF *vp = cp->point;
  379. mCross(vp[0] - vp[1],vp[2] - vp[1],&cp->normal[0]);
  380. cp->normal[0].normalize();
  381. mCross(vp[2] - vp[3],vp[0] - vp[3],&cp->normal[1]);
  382. cp->normal[1].normalize();
  383. if (mDot(vp[3] - vp[1],cp->normal[0]) > 0) {
  384. TerrainConvex* nc = new TerrainConvex(*cp);
  385. sTerrainConvexList.registerObject(nc);
  386. convex->addToWorkingList(nc);
  387. nc->halfA = false;
  388. nc->square = cp;
  389. cp->square = nc;
  390. }
  391. }
  392. else {
  393. VectorF *vp = cp->point;
  394. mCross(vp[3] - vp[0],vp[1] - vp[0],&cp->normal[0]);
  395. cp->normal[0].normalize();
  396. mCross(vp[1] - vp[2],vp[3] - vp[2],&cp->normal[1]);
  397. cp->normal[1].normalize();
  398. if (mDot(vp[2] - vp[0],cp->normal[0]) > 0) {
  399. TerrainConvex* nc = new TerrainConvex(*cp);
  400. sTerrainConvexList.registerObject(nc);
  401. convex->addToWorkingList(nc);
  402. nc->halfA = false;
  403. nc->square = cp;
  404. cp->square = nc;
  405. }
  406. }
  407. }
  408. }
  409. }
  410. static inline void swap(U32*& a,U32*& b)
  411. {
  412. U32* t = b;
  413. b = a;
  414. a = t;
  415. }
  416. static void clrbuf(U32* p, U32 s)
  417. {
  418. U32* e = p + s;
  419. while (p != e)
  420. *p++ = U32_MAX;
  421. }
  422. bool TerrainBlock::buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF&)
  423. {
  424. PROFILE_SCOPE( TerrainBlock_buildPolyList );
  425. // First check to see if the query misses the
  426. // terrain elevation range.
  427. const Point3F &terrainPos = getPosition();
  428. if ( box.maxExtents.z - terrainPos.z < -TerrainThickness ||
  429. box.minExtents.z - terrainPos.z > fixedToFloat( mFile->getMaxHeight() ) )
  430. return false;
  431. // Transform the bounding sphere into the object's coord
  432. // space. Note that this is really optimal.
  433. Box3F osBox = box;
  434. mWorldToObj.mul(osBox);
  435. AssertWarn(mObjScale == Point3F::One, "Error, handle the scale transform on the terrain");
  436. // Setup collision state data
  437. polyList->setTransform(&getTransform(), getScale());
  438. polyList->setObject(this);
  439. S32 xStart = (S32)mFloor( osBox.minExtents.x / mSquareSize );
  440. S32 xEnd = (S32)mCeil ( osBox.maxExtents.x / mSquareSize );
  441. S32 yStart = (S32)mFloor( osBox.minExtents.y / mSquareSize );
  442. S32 yEnd = (S32)mCeil ( osBox.maxExtents.y / mSquareSize );
  443. if ( xStart < 0 )
  444. xStart = 0;
  445. S32 xExt = xEnd - xStart;
  446. if ( xExt > MaxExtent )
  447. xExt = MaxExtent;
  448. xEnd = xStart + xExt;
  449. U32 heightMax = floatToFixed(osBox.maxExtents.z);
  450. U32 heightMin = (osBox.minExtents.z < 0.0f)? 0.0f: floatToFixed(osBox.minExtents.z);
  451. // Index of shared points
  452. U32 bp[(MaxExtent + 1) * 2],*vb[2];
  453. vb[0] = &bp[0];
  454. vb[1] = &bp[xExt + 1];
  455. clrbuf(vb[1],xExt + 1);
  456. const U32 BlockMask = mFile->mSize - 1;
  457. bool emitted = false;
  458. for (S32 y = yStart; y < yEnd; y++)
  459. {
  460. S32 yi = y & BlockMask;
  461. swap(vb[0],vb[1]);
  462. clrbuf(vb[1],xExt + 1);
  463. F32 wy1 = y * mSquareSize, wy2 = (y + 1) * mSquareSize;
  464. if(context == PLC_Navigation &&
  465. ((wy1 > osBox.maxExtents.y && wy2 > osBox.maxExtents.y) ||
  466. (wy1 < osBox.minExtents.y && wy2 < osBox.minExtents.y)))
  467. continue;
  468. //
  469. for (S32 x = xStart; x < xEnd; x++)
  470. {
  471. S32 xi = x & BlockMask;
  472. const TerrainSquare *sq = mFile->findSquare( 0, xi, yi );
  473. F32 wx1 = x * mSquareSize, wx2 = (x + 1) * mSquareSize;
  474. if(context == PLC_Navigation &&
  475. ((wx1 > osBox.maxExtents.x && wx2 > osBox.maxExtents.x) ||
  476. (wx1 < osBox.minExtents.x && wx2 < osBox.minExtents.x)))
  477. continue;
  478. if ( x != xi || y != yi )
  479. continue;
  480. // holes only in the primary terrain block
  481. if ( ( ( sq->flags & TerrainSquare::Empty ) && x == xi && y == yi ) ||
  482. sq->minHeight > heightMax ||
  483. sq->maxHeight < heightMin )
  484. continue;
  485. emitted = true;
  486. // Add the missing points
  487. U32 vi[5];
  488. for (int i = 0; i < 4 ; i++)
  489. {
  490. S32 dx = i >> 1;
  491. S32 dy = dx ^ (i & 1);
  492. U32* vp = &vb[dy][x - xStart + dx];
  493. if (*vp == U32_MAX)
  494. {
  495. Point3F pos;
  496. pos.x = (F32)((x + dx) * mSquareSize);
  497. pos.y = (F32)((y + dy) * mSquareSize);
  498. pos.z = fixedToFloat( mFile->getHeight(xi + dx, yi + dy) );
  499. *vp = polyList->addPoint(pos);
  500. }
  501. vi[i] = *vp;
  502. }
  503. U32* vp = &vi[0];
  504. if ( !( sq->flags & TerrainSquare::Split45 ) )
  505. vi[4] = vi[0], vp++;
  506. BaseMatInstance *material = NULL; //getMaterialInst( xi, yi );
  507. U32 surfaceKey = ((xi << 16) + yi) << 1;
  508. polyList->begin(material,surfaceKey);
  509. polyList->vertex(vp[0]);
  510. polyList->vertex(vp[1]);
  511. polyList->vertex(vp[2]);
  512. polyList->plane(vp[0],vp[1],vp[2]);
  513. polyList->end();
  514. polyList->begin(material,surfaceKey + 1);
  515. polyList->vertex(vp[0]);
  516. polyList->vertex(vp[2]);
  517. polyList->vertex(vp[3]);
  518. polyList->plane(vp[0],vp[2],vp[3]);
  519. polyList->end();
  520. }
  521. }
  522. return emitted;
  523. }
  524. //----------------------------------------------------------------------------
  525. static F32 calcInterceptV(F32 vStart, F32 invDeltaV, F32 intercept)
  526. {
  527. return (intercept - vStart) * invDeltaV;
  528. }
  529. static F32 calcInterceptNone(F32, F32, F32)
  530. {
  531. return MAX_FLOAT;
  532. }
  533. static F32 (*calcInterceptX)(F32, F32, F32);
  534. static F32 (*calcInterceptY)(F32, F32, F32);
  535. static U32 lineCount;
  536. static Point3F lineStart, lineEnd;
  537. bool TerrainBlock::castRay(const Point3F &start, const Point3F &end, RayInfo *info)
  538. {
  539. PROFILE_SCOPE( TerrainBlock_castRay );
  540. if ( !castRayI(start, end, info, false) )
  541. return false;
  542. // Set intersection point.
  543. info->setContactPoint( start, end );
  544. getTransform().mulP( info->point ); // transform to world coordinates for getGridPos
  545. // Set material at contact point.
  546. Point2I gridPos = getGridPos( info->point );
  547. U8 layer = mFile->getLayerIndex( gridPos.x, gridPos.y );
  548. info->material = mFile->getMaterialMapping( layer );
  549. return true;
  550. }
  551. bool TerrainBlock::castRayI(const Point3F &start, const Point3F &end, RayInfo *info, bool collideEmpty)
  552. {
  553. lineCount = 0;
  554. lineStart = start;
  555. lineEnd = end;
  556. info->object = this;
  557. if(start.x == end.x && start.y == end.y)
  558. {
  559. if (end.z == start.z)
  560. return false;
  561. F32 height;
  562. if(!getNormalAndHeight(Point2F(start.x, start.y), &info->normal, &height, true))
  563. return false;
  564. F32 t = (height - start.z) / (end.z - start.z);
  565. if(t < 0 || t > 1)
  566. return false;
  567. info->t = t;
  568. return true;
  569. }
  570. F32 invBlockWorldSize = 1 / getWorldBlockSize();
  571. Point3F pStart(start.x * invBlockWorldSize, start.y * invBlockWorldSize, start.z);
  572. Point3F pEnd(end.x * invBlockWorldSize, end.y * invBlockWorldSize, end.z);
  573. S32 blockX = (S32)mFloor(pStart.x);
  574. S32 blockY = (S32)mFloor(pStart.y);
  575. S32 dx, dy;
  576. F32 invDeltaX;
  577. if(pEnd.x == pStart.x)
  578. {
  579. calcInterceptX = calcInterceptNone;
  580. invDeltaX = 0;
  581. dx = 0;
  582. }
  583. else
  584. {
  585. invDeltaX = 1 / (pEnd.x - pStart.x);
  586. calcInterceptX = calcInterceptV;
  587. if(pEnd.x < pStart.x)
  588. dx = -1;
  589. else
  590. dx = 1;
  591. }
  592. F32 invDeltaY;
  593. if(pEnd.y == pStart.y)
  594. {
  595. calcInterceptY = calcInterceptNone;
  596. invDeltaY = 0;
  597. dy = 0;
  598. }
  599. else
  600. {
  601. invDeltaY = 1 / (pEnd.y - pStart.y);
  602. calcInterceptY = calcInterceptV;
  603. if(pEnd.y < pStart.y)
  604. dy = -1;
  605. else
  606. dy = 1;
  607. }
  608. const U32 BlockSquareWidth = mFile->mSize;
  609. const U32 GridLevels = mFile->mGridLevels;
  610. F32 startT = 0;
  611. for(;;)
  612. {
  613. F32 nextXInt = calcInterceptX(pStart.x, invDeltaX, (F32)(blockX + (dx == 1)));
  614. F32 nextYInt = calcInterceptY(pStart.y, invDeltaY, (F32)(blockY + (dy == 1)));
  615. F32 intersectT = 1;
  616. if(nextXInt < intersectT)
  617. intersectT = nextXInt;
  618. if(nextYInt < intersectT)
  619. intersectT = nextYInt;
  620. if ( castRayBlock( pStart,
  621. pEnd,
  622. Point2I( blockX * BlockSquareWidth,
  623. blockY * BlockSquareWidth ),
  624. GridLevels,
  625. invDeltaX,
  626. invDeltaY,
  627. startT,
  628. intersectT,
  629. info,
  630. collideEmpty ) )
  631. {
  632. info->normal.z *= BlockSquareWidth * mSquareSize;
  633. info->normal.normalize();
  634. return true;
  635. }
  636. startT = intersectT;
  637. if(intersectT >= 1)
  638. break;
  639. if(nextXInt < nextYInt)
  640. blockX += dx;
  641. else if(nextYInt < nextXInt)
  642. blockY += dy;
  643. else
  644. {
  645. blockX += dx;
  646. blockY += dy;
  647. }
  648. }
  649. return false;
  650. }
  651. struct TerrLOSStackNode
  652. {
  653. F32 startT;
  654. F32 endT;
  655. Point2I blockPos;
  656. U32 level;
  657. };
  658. bool TerrainBlock::castRayBlock( const Point3F &pStart,
  659. const Point3F &pEnd,
  660. const Point2I &aBlockPos,
  661. U32 aLevel,
  662. F32 invDeltaX,
  663. F32 invDeltaY,
  664. F32 aStartT,
  665. F32 aEndT,
  666. RayInfo *info,
  667. bool collideEmpty )
  668. {
  669. const U32 BlockSquareWidth = mFile->mSize;
  670. const U32 GridLevels = mFile->mGridLevels;
  671. const U32 BlockMask = mFile->mSize - 1;
  672. F32 invBlockSize = 1 / F32( BlockSquareWidth );
  673. static Vector<TerrLOSStackNode> stack;
  674. stack.setSize( GridLevels * 3 + 1 );
  675. U32 stackSize = 1;
  676. stack[0].startT = aStartT;
  677. stack[0].endT = aEndT;
  678. stack[0].blockPos = aBlockPos;
  679. stack[0].level = aLevel;
  680. if( !aBlockPos.isZero() )
  681. return false;
  682. while(stackSize--)
  683. {
  684. TerrLOSStackNode *sn = stack.address() + stackSize;
  685. U32 level = sn->level;
  686. F32 startT = sn->startT;
  687. F32 endT = sn->endT;
  688. Point2I blockPos = sn->blockPos;
  689. const TerrainSquare *sq = mFile->findSquare( level, blockPos.x, blockPos.y );
  690. F32 startZ = startT * (pEnd.z - pStart.z) + pStart.z;
  691. F32 endZ = endT * (pEnd.z - pStart.z) + pStart.z;
  692. F32 minHeight = fixedToFloat(sq->minHeight);
  693. if(startZ <= minHeight && endZ <= minHeight)
  694. continue;
  695. F32 maxHeight = fixedToFloat(sq->maxHeight);
  696. if(startZ >= maxHeight && endZ >= maxHeight)
  697. continue;
  698. if ( !collideEmpty && ( sq->flags & TerrainSquare::Empty ) &&
  699. blockPos.x == ( blockPos.x & BlockMask ) && blockPos.y == ( blockPos.y & BlockMask ))
  700. continue;
  701. if(level == 0)
  702. {
  703. F32 xs = blockPos.x * invBlockSize;
  704. F32 ys = blockPos.y * invBlockSize;
  705. F32 zBottomLeft = fixedToFloat( mFile->getHeight(blockPos.x, blockPos.y) );
  706. F32 zBottomRight= fixedToFloat( mFile->getHeight(blockPos.x + 1, blockPos.y) );
  707. F32 zTopLeft = fixedToFloat( mFile->getHeight(blockPos.x, blockPos.y + 1) );
  708. F32 zTopRight = fixedToFloat( mFile->getHeight(blockPos.x + 1, blockPos.y + 1) );
  709. PlaneF p1, p2;
  710. PlaneF divider;
  711. Point3F planePoint;
  712. if(sq->flags & TerrainSquare::Split45)
  713. {
  714. p1.set(zBottomLeft - zBottomRight, zBottomRight - zTopRight, invBlockSize);
  715. p2.set(zTopLeft - zTopRight, zBottomLeft - zTopLeft, invBlockSize);
  716. planePoint.set(xs, ys, zBottomLeft);
  717. divider.x = 1;
  718. divider.y = -1;
  719. divider.z = 0;
  720. }
  721. else
  722. {
  723. p1.set(zTopLeft - zTopRight, zBottomRight - zTopRight, invBlockSize);
  724. p2.set(zBottomLeft - zBottomRight, zBottomLeft - zTopLeft, invBlockSize);
  725. planePoint.set(xs + invBlockSize, ys, zBottomRight);
  726. divider.x = 1;
  727. divider.y = 1;
  728. divider.z = 0;
  729. }
  730. p1.setPoint(planePoint);
  731. p2.setPoint(planePoint);
  732. divider.setPoint(planePoint);
  733. F32 t1 = p1.intersect(pStart, pEnd);
  734. F32 t2 = p2.intersect(pStart, pEnd);
  735. F32 td = divider.intersect(pStart, pEnd);
  736. F32 dStart = divider.distToPlane(pStart);
  737. F32 dEnd = divider.distToPlane(pEnd);
  738. // see if the line crosses the divider
  739. if((dStart >= 0 && dEnd < 0) || (dStart < 0 && dEnd >= 0))
  740. {
  741. if(dStart < 0)
  742. {
  743. F32 temp = t1;
  744. t1 = t2;
  745. t2 = temp;
  746. }
  747. if(t1 >= startT && t1 && t1 <= td && t1 <= endT)
  748. {
  749. info->t = t1;
  750. info->normal = p1;
  751. return true;
  752. }
  753. if(t2 >= td && t2 >= startT && t2 <= endT)
  754. {
  755. info->t = t2;
  756. info->normal = p2;
  757. return true;
  758. }
  759. }
  760. else
  761. {
  762. F32 t;
  763. if(dStart >= 0) {
  764. t = t1;
  765. info->normal = p1;
  766. }
  767. else {
  768. t = t2;
  769. info->normal = p2;
  770. }
  771. if(t >= startT && t <= endT)
  772. {
  773. info->t = t;
  774. return true;
  775. }
  776. }
  777. continue;
  778. }
  779. S32 subSqWidth = 1 << (level - 1);
  780. F32 xIntercept = (blockPos.x + subSqWidth) * invBlockSize;
  781. F32 xInt = calcInterceptX(pStart.x, invDeltaX, xIntercept);
  782. F32 yIntercept = (blockPos.y + subSqWidth) * invBlockSize;
  783. F32 yInt = calcInterceptY(pStart.y, invDeltaY, yIntercept);
  784. F32 startX = startT * (pEnd.x - pStart.x) + pStart.x;
  785. F32 startY = startT * (pEnd.y - pStart.y) + pStart.y;
  786. if(xInt < startT)
  787. xInt = MAX_FLOAT;
  788. if(yInt < startT)
  789. yInt = MAX_FLOAT;
  790. U32 x0 = (startX > xIntercept) * subSqWidth;
  791. U32 y0 = (startY > yIntercept) * subSqWidth;
  792. U32 x1 = subSqWidth - x0;
  793. U32 y1 = subSqWidth - y0;
  794. U32 nextLevel = level - 1;
  795. // push the items on the stack in reverse order of processing
  796. if(xInt > endT && yInt > endT)
  797. {
  798. // only test the square the point started in:
  799. stack[stackSize].blockPos.set(blockPos.x + x0, blockPos.y + y0);
  800. stack[stackSize].level = nextLevel;
  801. stackSize++;
  802. }
  803. else if(xInt < yInt)
  804. {
  805. F32 nextIntersect = endT;
  806. if(yInt <= endT)
  807. {
  808. stack[stackSize].blockPos.set(blockPos.x + x1, blockPos.y + y1);
  809. stack[stackSize].startT = yInt;
  810. stack[stackSize].endT = endT;
  811. stack[stackSize].level = nextLevel;
  812. nextIntersect = yInt;
  813. stackSize++;
  814. }
  815. stack[stackSize].blockPos.set(blockPos.x + x1, blockPos.y + y0);
  816. stack[stackSize].startT = xInt;
  817. stack[stackSize].endT = nextIntersect;
  818. stack[stackSize].level = nextLevel;
  819. stack[stackSize+1].blockPos.set(blockPos.x + x0, blockPos.y + y0);
  820. stack[stackSize+1].startT = startT;
  821. stack[stackSize+1].endT = xInt;
  822. stack[stackSize+1].level = nextLevel;
  823. stackSize += 2;
  824. }
  825. else if(yInt < xInt)
  826. {
  827. F32 nextIntersect = endT;
  828. if(xInt <= endT)
  829. {
  830. stack[stackSize].blockPos.set(blockPos.x + x1, blockPos.y + y1);
  831. stack[stackSize].startT = xInt;
  832. stack[stackSize].endT = endT;
  833. stack[stackSize].level = nextLevel;
  834. nextIntersect = xInt;
  835. stackSize++;
  836. }
  837. stack[stackSize].blockPos.set(blockPos.x + x0, blockPos.y + y1);
  838. stack[stackSize].startT = yInt;
  839. stack[stackSize].endT = nextIntersect;
  840. stack[stackSize].level = nextLevel;
  841. stack[stackSize+1].blockPos.set(blockPos.x + x0, blockPos.y + y0);
  842. stack[stackSize+1].startT = startT;
  843. stack[stackSize+1].endT = yInt;
  844. stack[stackSize+1].level = nextLevel;
  845. stackSize += 2;
  846. }
  847. else
  848. {
  849. stack[stackSize].blockPos.set(blockPos.x + x1, blockPos.y + y1);
  850. stack[stackSize].startT = xInt;
  851. stack[stackSize].endT = endT;
  852. stack[stackSize].level = nextLevel;
  853. stack[stackSize+1].blockPos.set(blockPos.x + x0, blockPos.y + y0);
  854. stack[stackSize+1].startT = startT;
  855. stack[stackSize+1].endT = xInt;
  856. stack[stackSize+1].level = nextLevel;
  857. stackSize += 2;
  858. }
  859. }
  860. return false;
  861. }