terrCell.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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/terrCell.h"
  24. #include "math/util/frustum.h"
  25. #include "terrain/terrData.h"
  26. #include "terrain/terrCellMaterial.h"
  27. #include "scene/sceneRenderState.h"
  28. #include "lighting/lightManager.h"
  29. #include "gfx/gfxDrawUtil.h"
  30. GFXImplementVertexFormat( TerrVertex )
  31. {
  32. addElement( "POSITION", GFXDeclType_Float3 );
  33. addElement( "NORMAL", GFXDeclType_Float3 );
  34. addElement( "TangentZ", GFXDeclType_Float, 0 );
  35. addElement( "Empty", GFXDeclType_Float, 1 );
  36. };
  37. const U32 TerrCell::smMinCellSize = 64;
  38. const U32 TerrCell::smVBStride = TerrCell::smMinCellSize + 1; // 129
  39. const U32 TerrCell::smVBSize = ( TerrCell::smVBStride * TerrCell::smVBStride ) +
  40. ( TerrCell::smVBStride * 4 ); // 17,157
  41. const U32 TerrCell::smPBSize = ( TerrCell::smMinCellSize * TerrCell::smMinCellSize * 6 ) +
  42. ( TerrCell::smMinCellSize * 4 * 6 ); // 101,376
  43. const U32 TerrCell::smTriCount = TerrCell::smPBSize / 3; // 33,792
  44. TerrCell::TerrCell()
  45. : mMaterials( 0 ),
  46. mMaterial( NULL ),
  47. mIsInteriorOnly( false ),
  48. mTriCount( 0 ),
  49. mHasEmpty( false )
  50. {
  51. dMemset( mChildren, 0, sizeof( mChildren ) );
  52. }
  53. TerrCell::~TerrCell()
  54. {
  55. SAFE_DELETE( mMaterial );
  56. for ( U32 i=0; i < 4; i++ )
  57. SAFE_DELETE( mChildren[i] );
  58. }
  59. void TerrCell::createPrimBuffer( GFXPrimitiveBufferHandle *primBuffer )
  60. {
  61. PROFILE_SCOPE( TerrCell_AllocPrimBuffer );
  62. primBuffer->set( GFX, smPBSize, 1, GFXBufferTypeStatic, "TerrCell" );
  63. // We don't use the primitive for normal clipmap
  64. // rendering, but it is used for the shadow pass.
  65. GFXPrimitive *prim = primBuffer->getPointer()->mPrimitiveArray;
  66. prim->type = GFXTriangleList;
  67. prim->numPrimitives = smTriCount;
  68. prim->numVertices = smVBSize;
  69. //
  70. // The vertex pattern for the terrain is as
  71. // follows...
  72. //
  73. // 0----1----2.....n
  74. // |\ | /|
  75. // | \ | / |
  76. // | \ | / |
  77. // | \|/ |
  78. // n----n----n
  79. // | /|\ |
  80. // | / | \ |
  81. // | / | \ |
  82. // |/ | \|
  83. // n----n----n
  84. //
  85. // Lock and fill it up!
  86. U16 *idxBuff;
  87. primBuffer->lock( &idxBuff );
  88. U32 counter = 0;
  89. U32 maxIndex = 0;
  90. for ( U32 y = 0; y < smMinCellSize; y++ )
  91. {
  92. const U32 yTess = y % 2;
  93. for ( U32 x = 0; x < smMinCellSize; x++ )
  94. {
  95. U32 index = ( y * smVBStride ) + x;
  96. const U32 xTess = x % 2;
  97. if ( ( xTess == 0 && yTess == 0 ) ||
  98. ( xTess != 0 && yTess != 0 ) )
  99. {
  100. idxBuff[0] = index + 0;
  101. idxBuff[1] = index + smVBStride;
  102. idxBuff[2] = index + smVBStride + 1;
  103. idxBuff[3] = index + 0;
  104. idxBuff[4] = index + smVBStride + 1;
  105. idxBuff[5] = index + 1;
  106. }
  107. else
  108. {
  109. idxBuff[0] = index + 1;
  110. idxBuff[1] = index;
  111. idxBuff[2] = index + smVBStride;
  112. idxBuff[3] = index + 1;
  113. idxBuff[4] = index + smVBStride;
  114. idxBuff[5] = index + smVBStride + 1;
  115. }
  116. idxBuff += 6;
  117. maxIndex = index + 1 + smVBStride;
  118. counter += 6;
  119. }
  120. }
  121. // Now add indices for the 'skirts'.
  122. // These could probably be reduced to a loop.
  123. // Temporaries that hold triangle indices.
  124. // Top/Bottom - 0,1
  125. U32 t0, t1, b0, b1;
  126. // Top edge skirt...
  127. // Index to the first vert of the top row.
  128. U32 startIndex = 0;
  129. // Index to the first vert of the skirt under the top row.
  130. U32 skirtStartIdx = smVBStride * smVBStride;
  131. // Step to go one vert to the right.
  132. U32 step = 1;
  133. for ( U32 i = 0; i < smMinCellSize; i++ )
  134. {
  135. t0 = startIndex + i * step;
  136. t1 = t0 + step;
  137. b0 = skirtStartIdx + i;
  138. b1 = skirtStartIdx + i + 1;
  139. idxBuff[0] = b0;
  140. idxBuff[1] = t0;
  141. idxBuff[2] = t1;
  142. idxBuff[3] = b1;
  143. idxBuff[4] = b0;
  144. idxBuff[5] = t1;
  145. idxBuff += 6;
  146. maxIndex = b1;
  147. counter += 6;
  148. }
  149. // Bottom edge skirt...
  150. // Index to the first vert of the bottom row.
  151. startIndex = smVBStride * smVBStride - smVBStride;
  152. // Index to the first vert of the skirt under the bottom row.
  153. skirtStartIdx = startIndex + smVBStride * 2;
  154. // Step to go one vert to the right.
  155. step = 1;
  156. for ( U32 i = 0; i < smMinCellSize; i++ )
  157. {
  158. t0 = startIndex + ( i * step );
  159. t1 = t0 + step;
  160. b0 = skirtStartIdx + i;
  161. b1 = skirtStartIdx + i + 1;
  162. idxBuff[0] = t1;
  163. idxBuff[1] = t0;
  164. idxBuff[2] = b0;
  165. idxBuff[3] = t1;
  166. idxBuff[4] = b0;
  167. idxBuff[5] = b1;
  168. idxBuff += 6;
  169. maxIndex = b1;
  170. counter += 6;
  171. }
  172. // Left edge skirt...
  173. // Index to the first vert of the left column.
  174. startIndex = 0;
  175. // Index to the first vert of the skirt under the left column.
  176. skirtStartIdx = smVBStride * smVBStride + smVBStride * 2;
  177. // Step to go one vert down.
  178. step = smVBStride;
  179. for ( U32 i = 0; i < smMinCellSize; i++ )
  180. {
  181. t0 = startIndex + ( i * step );
  182. t1 = t0 + step;
  183. b0 = skirtStartIdx + i;
  184. b1 = skirtStartIdx + i + 1;
  185. idxBuff[0] = t1;
  186. idxBuff[1] = t0;
  187. idxBuff[2] = b0;
  188. idxBuff[3] = t1;
  189. idxBuff[4] = b0;
  190. idxBuff[5] = b1;
  191. idxBuff += 6;
  192. maxIndex = b1;
  193. counter += 6;
  194. }
  195. // Right edge skirt...
  196. // Index to the first vert of the right column.
  197. startIndex = smVBStride - 1;
  198. // Index to the first vert of the skirt under the right column.
  199. skirtStartIdx = smVBStride * smVBStride + smVBStride * 3;
  200. // Step to go one vert down.
  201. step = smVBStride;
  202. for ( U32 i = 0; i < smMinCellSize; i++ )
  203. {
  204. t0 = startIndex + ( i * step );
  205. t1 = t0 + step;
  206. b0 = skirtStartIdx + i;
  207. b1 = skirtStartIdx + i + 1;
  208. idxBuff[0] = b0;
  209. idxBuff[1] = t0;
  210. idxBuff[2] = t1;
  211. idxBuff[3] = b1;
  212. idxBuff[4] = b0;
  213. idxBuff[5] = t1;
  214. idxBuff += 6;
  215. maxIndex = b1;
  216. counter += 6;
  217. }
  218. primBuffer->unlock();
  219. }
  220. TerrCell* TerrCell::init( TerrainBlock *terrain )
  221. {
  222. // Just create the root cell and call the inner init.
  223. TerrCell *root = new TerrCell;
  224. root->_init( terrain,
  225. Point2I( 0, 0 ),
  226. terrain->getBlockSize(),
  227. 0 );
  228. // Set initial states of OBBs.
  229. root->updateOBBs();
  230. return root;
  231. }
  232. void TerrCell::_init( TerrainBlock *terrain,
  233. const Point2I &point,
  234. U32 size,
  235. U32 level )
  236. {
  237. PROFILE_SCOPE( TerrCell_Init );
  238. mTerrain = terrain;
  239. mPoint = point;
  240. mSize = size;
  241. mLevel = level;
  242. // Generate a VB (and maybe a PB) for this cell, unless we are the Root cell.
  243. if ( level > 0 )
  244. {
  245. _updateVertexBuffer();
  246. _updatePrimitiveBuffer();
  247. }
  248. if ( mSize <= smMinCellSize )
  249. {
  250. // Update our bounds and materials... the
  251. // parent will use it to update itself.
  252. _updateBounds();
  253. _updateMaterials();
  254. return;
  255. }
  256. // Create our children and update our
  257. // bounds and materials from them.
  258. const U32 childSize = mSize / 2;
  259. const U32 childLevel = mLevel + 1;
  260. mChildren[0] = new TerrCell;
  261. mChildren[0]->_init( mTerrain,
  262. Point2I( mPoint.x, mPoint.y ),
  263. childSize,
  264. childLevel );
  265. mBounds = mChildren[0]->getBounds();
  266. mMaterials = mChildren[0]->getMaterials();
  267. mChildren[1] = new TerrCell;
  268. mChildren[1]->_init( mTerrain,
  269. Point2I( mPoint.x + childSize, mPoint.y ),
  270. childSize,
  271. childLevel );
  272. mBounds.intersect( mChildren[1]->getBounds() );
  273. mMaterials |= mChildren[1]->getMaterials();
  274. mChildren[2] = new TerrCell;
  275. mChildren[2]->_init( mTerrain,
  276. Point2I( mPoint.x, mPoint.y + childSize ),
  277. childSize,
  278. childLevel );
  279. mBounds.intersect( mChildren[2]->getBounds() );
  280. mMaterials |= mChildren[2]->getMaterials();
  281. mChildren[3] = new TerrCell;
  282. mChildren[3]->_init( mTerrain,
  283. Point2I( mPoint.x + childSize, mPoint.y + childSize ),
  284. childSize,
  285. childLevel );
  286. mBounds.intersect( mChildren[3]->getBounds() );
  287. mMaterials |= mChildren[3]->getMaterials();
  288. mRadius = mBounds.len() * 0.5f;
  289. _updateOBB();
  290. }
  291. void TerrCell::updateGrid( const RectI &gridRect, bool opacityOnly )
  292. {
  293. PROFILE_SCOPE( TerrCell_UpdateGrid );
  294. // If we have a VB... then update it.
  295. if ( mVertexBuffer.isValid() && !opacityOnly )
  296. _updateVertexBuffer();
  297. // Update our PB, if any
  298. _updatePrimitiveBuffer();
  299. // If we don't have children... then we're
  300. // a leaf at the bottom of the cell quadtree
  301. // and we should just update our bounds.
  302. if ( !mChildren[0] )
  303. {
  304. if ( !opacityOnly )
  305. _updateBounds();
  306. _updateMaterials();
  307. return;
  308. }
  309. // Otherwise, we must call updateGrid on our children
  310. // and then update our bounds/materials AFTER to contain them.
  311. mMaterials = 0;
  312. for ( U32 i = 0; i < 4; i++ )
  313. {
  314. TerrCell *cell = mChildren[i];
  315. // The overlap test doesn't hit shared edges
  316. // so grow it a bit when we create it.
  317. const RectI cellRect( cell->mPoint.x - 1,
  318. cell->mPoint.y - 1,
  319. cell->mSize + 2,
  320. cell->mSize + 2 );
  321. // We do an overlap and containment test as it
  322. // properly handles zero sized rects.
  323. if ( cellRect.contains( gridRect ) ||
  324. cellRect.overlaps( gridRect ) )
  325. cell->updateGrid( gridRect, opacityOnly );
  326. // Update the bounds from our children.
  327. if ( !opacityOnly )
  328. {
  329. if ( i == 0 )
  330. mBounds = mChildren[i]->getBounds();
  331. else
  332. mBounds.intersect( mChildren[i]->getBounds() );
  333. mRadius = mBounds.len() * 0.5f;
  334. }
  335. // Update the material flags.
  336. mMaterials |= mChildren[i]->getMaterials();
  337. }
  338. if ( mMaterial )
  339. mMaterial->init( mTerrain, mMaterials );
  340. }
  341. void TerrCell::_updateVertexBuffer()
  342. {
  343. PROFILE_SCOPE( TerrCell_UpdateVertexBuffer );
  344. // Start off with no empty squares
  345. mHasEmpty = false;
  346. mEmptyVertexList.clear();
  347. mVertexBuffer.set( GFX, smVBSize, GFXBufferTypeStatic );
  348. const F32 squareSize = mTerrain->getSquareSize();
  349. const U32 blockSize = mTerrain->getBlockSize();
  350. const U32 stepSize = mSize / smMinCellSize;
  351. U32 vbcounter = 0;
  352. TerrVertex *vert = mVertexBuffer.lock();
  353. Point2I gridPt;
  354. Point2F point;
  355. F32 height;
  356. Point3F normal;
  357. const TerrainFile *file = mTerrain->getFile();
  358. for ( U32 y = 0; y < smVBStride; y++ )
  359. {
  360. for ( U32 x = 0; x < smVBStride; x++ )
  361. {
  362. // We clamp here to keep the geometry from reading across
  363. // one side of the height map to the other causing walls
  364. // around the edges of the terrain.
  365. gridPt.x = mClamp( mPoint.x + x * stepSize, 0, blockSize - 1 );
  366. gridPt.y = mClamp( mPoint.y + y * stepSize, 0, blockSize - 1 );
  367. // Setup this point.
  368. point.x = (F32)gridPt.x * squareSize;
  369. point.y = (F32)gridPt.y * squareSize;
  370. height = fixedToFloat( file->getHeight( gridPt.x, gridPt.y ) );
  371. vert->point.x = point.x;
  372. vert->point.y = point.y;
  373. vert->point.z = height;
  374. // Get the normal.
  375. mTerrain->getSmoothNormal( point, &normal, true, false );
  376. vert->normal = normal;
  377. // Get the tangent z.
  378. vert->tangentZ = fixedToFloat( file->getHeight( gridPt.x + 1, gridPt.y ) ) - height;
  379. // Test the empty state for this vert.
  380. if ( file->isEmptyAt( gridPt.x, gridPt.y ) )
  381. {
  382. mHasEmpty = true;
  383. mEmptyVertexList.push_back( vbcounter );
  384. }
  385. vbcounter++;
  386. ++vert;
  387. }
  388. }
  389. // Add verts for 'skirts' around/beneath the edge verts of this cell.
  390. // This could probably be reduced to a loop...
  391. const F32 skirtDepth = mSize / smMinCellSize * mTerrain->getSquareSize();
  392. // Top edge skirt
  393. for ( U32 i = 0; i < smVBStride; i++ )
  394. {
  395. gridPt.x = mClamp( mPoint.x + i * stepSize, 0, blockSize - 1 );
  396. gridPt.y = mClamp( mPoint.y, 0, blockSize - 1 );
  397. point.x = (F32)gridPt.x * squareSize;
  398. point.y = (F32)gridPt.y * squareSize;
  399. height = fixedToFloat( file->getHeight( gridPt.x, gridPt.y ) );
  400. vert->point.x = point.x;
  401. vert->point.y = point.y;
  402. vert->point.z = height - skirtDepth;
  403. // Get the normal.
  404. mTerrain->getNormal( point, &normal, true, false );
  405. vert->normal = normal;
  406. // Get the tangent.
  407. vert->tangentZ = height - fixedToFloat( file->getHeight( gridPt.x + 1, gridPt.y ) );
  408. vbcounter++;
  409. ++vert;
  410. }
  411. // Bottom edge skirt
  412. for ( U32 i = 0; i < smVBStride; i++ )
  413. {
  414. gridPt.x = mClamp( mPoint.x + i * stepSize, 0, blockSize - 1 );
  415. gridPt.y = mClamp( mPoint.y + smMinCellSize * stepSize, 0, blockSize - 1 );
  416. point.x = (F32)gridPt.x * squareSize;
  417. point.y = (F32)gridPt.y * squareSize;
  418. height = fixedToFloat( file->getHeight( gridPt.x, gridPt.y ) );
  419. vert->point.x = point.x;
  420. vert->point.y = point.y;
  421. vert->point.z = height - skirtDepth;
  422. // Get the normal.
  423. mTerrain->getNormal( point, &normal, true, false );
  424. vert->normal = normal;
  425. // Get the tangent.
  426. vert->tangentZ = height - fixedToFloat( file->getHeight( gridPt.x + 1, gridPt.y ) );
  427. vbcounter++;
  428. ++vert;
  429. }
  430. // Left edge skirt
  431. for ( U32 i = 0; i < smVBStride; i++ )
  432. {
  433. gridPt.x = mClamp( mPoint.x, 0, blockSize - 1 );
  434. gridPt.y = mClamp( mPoint.y + i * stepSize, 0, blockSize - 1 );
  435. point.x = (F32)gridPt.x * squareSize;
  436. point.y = (F32)gridPt.y * squareSize;
  437. height = fixedToFloat( file->getHeight( gridPt.x, gridPt.y ) );
  438. vert->point.x = point.x;
  439. vert->point.y = point.y;
  440. vert->point.z = height - skirtDepth;
  441. // Get the normal.
  442. mTerrain->getNormal( point, &normal, true, false );
  443. vert->normal = normal;
  444. // Get the tangent.
  445. vert->tangentZ = height - fixedToFloat( file->getHeight( gridPt.x + 1, gridPt.y ) );
  446. vbcounter++;
  447. ++vert;
  448. }
  449. // Right edge skirt
  450. for ( U32 i = 0; i < smVBStride; i++ )
  451. {
  452. gridPt.x = mClamp( mPoint.x + smMinCellSize * stepSize, 0, blockSize - 1 );
  453. gridPt.y = mClamp( mPoint.y + i * stepSize, 0, blockSize - 1 );
  454. point.x = (F32)gridPt.x * squareSize;
  455. point.y = (F32)gridPt.y * squareSize;
  456. height = fixedToFloat( file->getHeight( gridPt.x, gridPt.y ) );
  457. vert->point.x = point.x;
  458. vert->point.y = point.y;
  459. vert->point.z = height - skirtDepth;
  460. // Get the normal.
  461. mTerrain->getNormal( point, &normal, true, false );
  462. vert->normal = normal;
  463. // Get the tangent.
  464. vert->tangentZ = height - fixedToFloat( file->getHeight( gridPt.x + 1, gridPt.y ) );
  465. vbcounter++;
  466. ++vert;
  467. }
  468. AssertFatal( vbcounter == smVBSize, "bad" );
  469. mVertexBuffer.unlock();
  470. }
  471. void TerrCell::_updatePrimitiveBuffer()
  472. {
  473. PROFILE_SCOPE( TerrCell_UpdatePrimitiveBuffer );
  474. if ( !mHasEmpty )
  475. {
  476. if ( mPrimBuffer.isValid() )
  477. {
  478. // There are no more empty squares for this cell, so
  479. // get rid of the primitive buffer to use the standard one.
  480. mPrimBuffer = NULL;
  481. }
  482. return;
  483. }
  484. // Build our custom primitive buffer. We're setting it to the maximum allowed
  485. // size, but should be just shy of it depending on the number of empty squares
  486. // in this cell. We could calculate it, but note that it would be different
  487. // from mEmptyVertexList.size() as that can include vertices on the edges that
  488. // are really considered part of another cell's squares. So we take a slightly
  489. // larger buffer over running through the calculation.
  490. mPrimBuffer.set( GFX, smPBSize, 1, GFXBufferTypeStatic, "TerrCell" );
  491. GFXPrimitive *prim = mPrimBuffer.getPointer()->mPrimitiveArray;
  492. prim->type = GFXTriangleList;
  493. prim->numVertices = smVBSize;
  494. mTriCount = 0;
  495. // Lock and fill it up!
  496. U16 *idxBuff;
  497. mPrimBuffer.lock( &idxBuff );
  498. U32 counter = 0;
  499. U32 maxIndex = 0;
  500. for ( U32 y = 0; y < smMinCellSize; y++ )
  501. {
  502. const U32 yTess = y % 2;
  503. for ( U32 x = 0; x < smMinCellSize; x++ )
  504. {
  505. U32 index = ( y * smVBStride ) + x;
  506. // Should this square be skipped?
  507. if ( _isVertIndexEmpty(index) )
  508. continue;
  509. const U32 xTess = x % 2;
  510. if ( ( xTess == 0 && yTess == 0 ) ||
  511. ( xTess != 0 && yTess != 0 ) )
  512. {
  513. idxBuff[0] = index + 0;
  514. idxBuff[1] = index + smVBStride;
  515. idxBuff[2] = index + smVBStride + 1;
  516. idxBuff[3] = index + 0;
  517. idxBuff[4] = index + smVBStride + 1;
  518. idxBuff[5] = index + 1;
  519. }
  520. else
  521. {
  522. idxBuff[0] = index + 1;
  523. idxBuff[1] = index;
  524. idxBuff[2] = index + smVBStride;
  525. idxBuff[3] = index + 1;
  526. idxBuff[4] = index + smVBStride;
  527. idxBuff[5] = index + smVBStride + 1;
  528. }
  529. idxBuff += 6;
  530. maxIndex = index + 1 + smVBStride;
  531. counter += 6;
  532. mTriCount += 2;
  533. }
  534. }
  535. // Now add indices for the 'skirts'.
  536. // These could probably be reduced to a loop.
  537. // Temporaries that hold triangle indices.
  538. // Top/Bottom - 0,1
  539. U32 t0, t1, b0, b1;
  540. // Top edge skirt...
  541. // Index to the first vert of the top row.
  542. U32 startIndex = 0;
  543. // Index to the first vert of the skirt under the top row.
  544. U32 skirtStartIdx = smVBStride * smVBStride;
  545. // Step to go one vert to the right.
  546. U32 step = 1;
  547. for ( U32 i = 0; i < smMinCellSize; i++ )
  548. {
  549. t0 = startIndex + i * step;
  550. // Should this square be skipped?
  551. if ( _isVertIndexEmpty(t0) )
  552. continue;
  553. t1 = t0 + step;
  554. b0 = skirtStartIdx + i;
  555. b1 = skirtStartIdx + i + 1;
  556. idxBuff[0] = b0;
  557. idxBuff[1] = t0;
  558. idxBuff[2] = t1;
  559. idxBuff[3] = b1;
  560. idxBuff[4] = b0;
  561. idxBuff[5] = t1;
  562. idxBuff += 6;
  563. maxIndex = b1;
  564. counter += 6;
  565. mTriCount += 2;
  566. }
  567. // Bottom edge skirt...
  568. // Index to the first vert of the bottom row.
  569. startIndex = smVBStride * smVBStride - smVBStride;
  570. // Index to the first vert of the skirt under the bottom row.
  571. skirtStartIdx = startIndex + smVBStride * 2;
  572. // Step to go one vert to the right.
  573. step = 1;
  574. for ( U32 i = 0; i < smMinCellSize; i++ )
  575. {
  576. t0 = startIndex + ( i * step );
  577. // Should this square be skipped? We actually need to test
  578. // the vertex one row down as it defines the empty state
  579. // for this square.
  580. if ( _isVertIndexEmpty( t0 - smVBStride ) )
  581. continue;
  582. t1 = t0 + step;
  583. b0 = skirtStartIdx + i;
  584. b1 = skirtStartIdx + i + 1;
  585. idxBuff[0] = t1;
  586. idxBuff[1] = t0;
  587. idxBuff[2] = b0;
  588. idxBuff[3] = t1;
  589. idxBuff[4] = b0;
  590. idxBuff[5] = b1;
  591. idxBuff += 6;
  592. maxIndex = b1;
  593. counter += 6;
  594. mTriCount += 2;
  595. }
  596. // Left edge skirt...
  597. // Index to the first vert of the left column.
  598. startIndex = 0;
  599. // Index to the first vert of the skirt under the left column.
  600. skirtStartIdx = smVBStride * smVBStride + smVBStride * 2;
  601. // Step to go one vert down.
  602. step = smVBStride;
  603. for ( U32 i = 0; i < smMinCellSize; i++ )
  604. {
  605. t0 = startIndex + ( i * step );
  606. // Should this square be skipped?
  607. if ( _isVertIndexEmpty(t0) )
  608. continue;
  609. t1 = t0 + step;
  610. b0 = skirtStartIdx + i;
  611. b1 = skirtStartIdx + i + 1;
  612. idxBuff[0] = t1;
  613. idxBuff[1] = t0;
  614. idxBuff[2] = b0;
  615. idxBuff[3] = t1;
  616. idxBuff[4] = b0;
  617. idxBuff[5] = b1;
  618. idxBuff += 6;
  619. maxIndex = b1;
  620. counter += 6;
  621. mTriCount += 2;
  622. }
  623. // Right edge skirt...
  624. // Index to the first vert of the right column.
  625. startIndex = smVBStride - 1;
  626. // Index to the first vert of the skirt under the right column.
  627. skirtStartIdx = smVBStride * smVBStride + smVBStride * 3;
  628. // Step to go one vert down.
  629. step = smVBStride;
  630. for ( U32 i = 0; i < smMinCellSize; i++ )
  631. {
  632. t0 = startIndex + ( i * step );
  633. // Should this square be skipped? We actually need to test
  634. // the vertex one column to the left as it defines the empty
  635. // state for this square.
  636. if ( _isVertIndexEmpty( t0 - 1 ) )
  637. continue;
  638. t1 = t0 + step;
  639. b0 = skirtStartIdx + i;
  640. b1 = skirtStartIdx + i + 1;
  641. idxBuff[0] = b0;
  642. idxBuff[1] = t0;
  643. idxBuff[2] = t1;
  644. idxBuff[3] = b1;
  645. idxBuff[4] = b0;
  646. idxBuff[5] = t1;
  647. idxBuff += 6;
  648. maxIndex = b1;
  649. counter += 6;
  650. mTriCount += 2;
  651. }
  652. mPrimBuffer.unlock();
  653. prim->numPrimitives = mTriCount;
  654. }
  655. void TerrCell::_updateMaterials()
  656. {
  657. PROFILE_SCOPE( TerrCell_UpdateMaterials );
  658. // This should really only be called for cells of smMinCellSize,
  659. // in which case stepSize is always one.
  660. U32 stepSize = mSize / smMinCellSize;
  661. mMaterials = 0;
  662. U8 index;
  663. U32 x, y;
  664. const TerrainFile *file = mTerrain->getFile();
  665. // Step thru the samples in the map then.
  666. for ( y = 0; y < smVBStride; y++ )
  667. {
  668. for ( x = 0; x < smVBStride; x++ )
  669. {
  670. index = file->getLayerIndex( mPoint.x + x * stepSize,
  671. mPoint.y + y * stepSize );
  672. // Skip empty layers and anything that doesn't fit
  673. // the 64bit material flags.
  674. if ( index == U8_MAX || index > 63 )
  675. continue;
  676. mMaterials |= (U64)((U64)1<<index);
  677. }
  678. }
  679. if ( mMaterial )
  680. mMaterial->init( mTerrain, mMaterials );
  681. }
  682. void TerrCell::_updateBounds()
  683. {
  684. PROFILE_SCOPE( TerrCell_UpdateBounds );
  685. const F32 squareSize = mTerrain->getSquareSize();
  686. // This should really only be called for cells of smMinCellSize,
  687. // in which case stepSize is always one.
  688. const U32 stepSize = mSize / smMinCellSize;
  689. // Prepare to expand the bounds.
  690. mBounds.minExtents.set( F32_MAX, F32_MAX, F32_MAX );
  691. mBounds.maxExtents.set( -F32_MAX, -F32_MAX, -F32_MAX );
  692. Point3F vert;
  693. Point2F texCoord;
  694. const TerrainFile *file = mTerrain->getFile();
  695. for ( U32 y = 0; y < smVBStride; y++ )
  696. {
  697. for ( U32 x = 0; x < smVBStride; x++ )
  698. {
  699. // Setup this point.
  700. vert.x = (F32)( mPoint.x + x * stepSize ) * squareSize;
  701. vert.y = (F32)( mPoint.y + y * stepSize ) * squareSize;
  702. vert.z = fixedToFloat( file->getHeight( mPoint.x + x,
  703. mPoint.y + y ) );
  704. // HACK: Call it twice to deal with the inverted
  705. // inital bounds state... shouldn't be a perf issue.
  706. mBounds.extend( vert );
  707. mBounds.extend( vert );
  708. }
  709. }
  710. mRadius = mBounds.len() * 0.5;
  711. _updateOBB();
  712. }
  713. void TerrCell::_updateOBB()
  714. {
  715. mOBB.set( mTerrain->getTransform(), mBounds );
  716. }
  717. void TerrCell::updateOBBs()
  718. {
  719. _updateOBB();
  720. // Update children.
  721. if( mChildren[ 0 ] )
  722. for( U32 i = 0; i < 4; ++ i )
  723. mChildren[ i ]->updateOBBs();
  724. }
  725. void TerrCell::updateZoning( const SceneZoneSpaceManager *zoneManager )
  726. {
  727. PROFILE_SCOPE( TerrCell_UpdateZoning );
  728. mZoneOverlap.setSize( zoneManager->getNumZones() );
  729. mZoneOverlap.clear();
  730. mIsInteriorOnly = true;
  731. if ( mChildren[0] == NULL )
  732. {
  733. Box3F worldBounds( mBounds );
  734. mTerrain->getTransform().mul( worldBounds );
  735. Vector<U32> zones;
  736. zoneManager->findZones( worldBounds, zones );
  737. for ( U32 i=0; i < zones.size(); i++ )
  738. {
  739. // Set overlap bit for zone except it's the outdoor zone.
  740. if( zones[ i ] != SceneZoneSpaceManager::RootZoneId )
  741. mZoneOverlap.set( zones[i] );
  742. else
  743. mIsInteriorOnly = false;
  744. }
  745. return;
  746. }
  747. for ( U32 i = 0; i < 4; i++ )
  748. {
  749. TerrCell *cell = mChildren[i];
  750. cell->updateZoning( zoneManager );
  751. mZoneOverlap.combineOR( cell->getZoneOverlap() );
  752. mIsInteriorOnly &= cell->mIsInteriorOnly;
  753. }
  754. }
  755. void TerrCell::cullCells( const SceneRenderState *state,
  756. const Point3F &objLodPos,
  757. Vector<TerrCell*> *outCells )
  758. {
  759. // If we have a VB and no children then just add
  760. // ourselves to the results and return.
  761. if ( mVertexBuffer.isValid() && !mChildren[0] )
  762. {
  763. outCells->push_back( this );
  764. return;
  765. }
  766. const F32 screenError = mTerrain->getScreenError();
  767. const BitVector &zoneState = state->getCullingState().getZoneVisibilityFlags();
  768. for ( U32 i = 0; i < 4; i++ )
  769. {
  770. TerrCell *cell = mChildren[i];
  771. // Test cell visibility for interior zones.
  772. const bool visibleInside = !cell->getZoneOverlap().empty() ? zoneState.testAny( cell->getZoneOverlap() ) : false;
  773. // Test cell visibility for outdoor zone, but only
  774. // if we need to.
  775. bool visibleOutside = false;
  776. if( !mIsInteriorOnly && !visibleInside )
  777. {
  778. U32 outdoorZone = SceneZoneSpaceManager::RootZoneId;
  779. visibleOutside = !state->getCullingState().isCulled( cell->mOBB, &outdoorZone, 1 );
  780. }
  781. // Skip cell if neither visible indoors nor outdoors.
  782. if( !visibleInside && !visibleOutside )
  783. continue;
  784. // Lod based on screen error...
  785. // If far enough, just add this child cells vb ( skipping its children ).
  786. F32 dist = cell->getDistanceTo( objLodPos );
  787. F32 errorMeters = ( cell->mSize / smMinCellSize ) * mTerrain->getSquareSize();
  788. U32 errorPixels = mCeil( state->projectRadius( dist, errorMeters ) );
  789. if ( errorPixels < screenError )
  790. {
  791. if ( cell->mVertexBuffer.isValid() )
  792. outCells->push_back( cell );
  793. }
  794. else
  795. cell->cullCells( state, objLodPos, outCells );
  796. }
  797. }
  798. void TerrCell::getRenderPrimitive( GFXPrimitive *prim,
  799. GFXVertexBufferHandleBase *vertBuff,
  800. GFXPrimitiveBufferHandle *primBuff ) const
  801. {
  802. *vertBuff = mVertexBuffer;
  803. // Only supply a primitive buffer if we're using our own
  804. // due to empty squares.
  805. bool useStaticPrimBuffer = true;
  806. if ( mPrimBuffer.isValid() )
  807. {
  808. useStaticPrimBuffer = false;
  809. *primBuff = mPrimBuffer;
  810. }
  811. prim->type = GFXTriangleList;
  812. prim->startVertex = 0;
  813. prim->minIndex = 0;
  814. prim->startIndex = 0;
  815. prim->numVertices = smVBSize;
  816. if ( useStaticPrimBuffer )
  817. {
  818. // Use the standard primitive buffer count
  819. prim->numPrimitives = smTriCount;
  820. }
  821. else
  822. {
  823. // Use our triangle count that matches out primitive buffer
  824. prim->numPrimitives = mTriCount;
  825. }
  826. }
  827. void TerrCell::renderBounds() const
  828. {
  829. ColorI color;
  830. color.interpolate( ColorI::RED, ColorI::GREEN, (F32)mLevel / 3.0f );
  831. GFXStateBlockDesc desc;
  832. desc.setZReadWrite( true, false );
  833. desc.fillMode = GFXFillWireframe;
  834. GFX->getDrawUtil()->drawCube( desc, mBounds, color );
  835. }
  836. void TerrCell::preloadMaterials()
  837. {
  838. PROFILE_SCOPE( TerrCell_PreloadMaterials );
  839. // If we have a VB then we need a material.
  840. if ( mVertexBuffer.isValid() )
  841. {
  842. TerrainCellMaterial *material = getMaterial();
  843. material->getReflectMat();
  844. if ( GFX->getPixelShaderVersion() > 2.0f &&
  845. dStrcmp( LIGHTMGR->getId(), "BLM" ) != 0)
  846. material->getPrePassMat();
  847. }
  848. for ( U32 i = 0; i < 4; i++ )
  849. if ( mChildren[i] )
  850. mChildren[i]->preloadMaterials();
  851. }
  852. TerrainCellMaterial* TerrCell::getMaterial()
  853. {
  854. if ( !mMaterial )
  855. {
  856. mMaterial = new TerrainCellMaterial;
  857. mMaterial->init( mTerrain, mMaterials );
  858. }
  859. return mMaterial;
  860. }
  861. void TerrCell::deleteMaterials()
  862. {
  863. SAFE_DELETE( mMaterial );
  864. for ( U32 i = 0; i < 4; i++ )
  865. if ( mChildren[i] )
  866. mChildren[i]->deleteMaterials();
  867. }