waterBlock.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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 "environment/waterBlock.h"
  24. #include "core/util/safeDelete.h"
  25. #include "scene/sceneRenderState.h"
  26. #include "scene/sceneManager.h"
  27. #include "lighting/lightInfo.h"
  28. #include "core/stream/bitStream.h"
  29. #include "math/mathIO.h"
  30. #include "console/consoleTypes.h"
  31. #include "gui/3d/guiTSControl.h"
  32. #include "gfx/primBuilder.h"
  33. #include "gfx/gfxTransformSaver.h"
  34. #include "gfx/gfxDebugEvent.h"
  35. #include "gfx/gfxOcclusionQuery.h"
  36. #include "renderInstance/renderPassManager.h"
  37. #include "sim/netConnection.h"
  38. #include "scene/reflectionManager.h"
  39. #include "ts/tsShapeInstance.h"
  40. #include "postFx/postEffect.h"
  41. #include "math/util/matrixSet.h"
  42. IMPLEMENT_CO_NETOBJECT_V1(WaterBlock);
  43. ConsoleDocClass( WaterBlock,
  44. "@brief A block shaped water volume defined by a 3D scale and orientation.\n\n"
  45. "@see WaterObject for inherited functionality.\n\n"
  46. "@ingroup Water"
  47. );
  48. WaterBlock::WaterBlock()
  49. {
  50. mGridElementSize = 5.0f;
  51. mObjScale.set( 100.0f, 100.0f, 10.0f );
  52. mNetFlags.set(Ghostable | ScopeAlways);
  53. mObjBox.minExtents.set( -0.5f, -0.5f, -0.5f );
  54. mObjBox.maxExtents.set( 0.5f, 0.5f, 0.5f );
  55. mElapsedTime = 0.0f;
  56. mGenerateVB = true;
  57. }
  58. WaterBlock::~WaterBlock()
  59. {
  60. }
  61. bool WaterBlock::onAdd()
  62. {
  63. if ( !Parent::onAdd() )
  64. return false;
  65. resetWorldBox();
  66. addToScene();
  67. return true;
  68. }
  69. void WaterBlock::onRemove()
  70. {
  71. clearVertBuffers();
  72. removeFromScene();
  73. Parent::onRemove();
  74. }
  75. //-----------------------------------------------------------------------------
  76. // packUpdate
  77. //-----------------------------------------------------------------------------
  78. U32 WaterBlock::packUpdate(NetConnection* con, U32 mask, BitStream* stream)
  79. {
  80. U32 retMask = Parent::packUpdate(con, mask, stream);
  81. stream->write( mGridElementSize );
  82. if ( stream->writeFlag( mask & UpdateMask ) )
  83. {
  84. // This is set to allow the user to modify the size of the water dynamically
  85. // in the editor
  86. mathWrite( *stream, mObjScale );
  87. stream->writeAffineTransform( mObjToWorld );
  88. }
  89. return retMask;
  90. }
  91. //-----------------------------------------------------------------------------
  92. // unpackUpdate
  93. //-----------------------------------------------------------------------------
  94. void WaterBlock::unpackUpdate(NetConnection* con, BitStream* stream)
  95. {
  96. Parent::unpackUpdate(con, stream);
  97. F32 gridSize = mGridElementSize;
  98. stream->read( &mGridElementSize );
  99. if ( gridSize != mGridElementSize )
  100. mGenerateVB = true;
  101. if( stream->readFlag() ) // UpdateMask
  102. {
  103. Point3F scale;
  104. mathRead( *stream, &scale );
  105. setScale( scale );
  106. MatrixF objToWorld;
  107. stream->readAffineTransform( &objToWorld );
  108. setTransform( objToWorld );
  109. }
  110. }
  111. //-----------------------------------------------------------------------------
  112. // Setup vertex and index buffers
  113. //-----------------------------------------------------------------------------
  114. void WaterBlock::setupVBIB()
  115. {
  116. clearVertBuffers();
  117. const U32 maxIndexedVerts = 65536; // max number of indexed verts with U16 size indices
  118. if( mObjScale.x < mGridElementSize ||
  119. mObjScale.y < mGridElementSize )
  120. {
  121. F32 oldGridSize = mGridElementSize;
  122. mGridElementSize = getMin(mObjScale.x, mObjScale.y);
  123. logWarning("gridElementSize %g is larger than scale (%g, %g), clamping gridElementSize to %g",
  124. oldGridSize, mObjScale.x, mObjScale.y, mGridElementSize);
  125. }
  126. Point3F div = getScale() / mGridElementSize;
  127. // Add one to width and height for the edge.
  128. mWidth = (U32)mCeil(div.x) + 1;
  129. mHeight = (U32)mCeil(div.y) + 1;
  130. if( mWidth > maxIndexedVerts / 2 )
  131. mWidth = maxIndexedVerts / 2;
  132. // figure out how many blocks are needed and their size
  133. U32 maxBlockRows = maxIndexedVerts / mWidth;
  134. U32 rowOffset = 0;
  135. while( (rowOffset+1) < mHeight )
  136. {
  137. U32 numRows = mHeight - rowOffset;
  138. if( numRows == 1 ) numRows++;
  139. if( numRows > maxBlockRows )
  140. {
  141. numRows = maxBlockRows;
  142. }
  143. setupVertexBlock( mWidth, numRows, rowOffset );
  144. setupPrimitiveBlock( mWidth, numRows );
  145. rowOffset += numRows - 1;
  146. }
  147. }
  148. //-----------------------------------------------------------------------------
  149. // Set up a block of vertices - the width is always the width of the entire
  150. // waterBlock, so this is a block of full rows.
  151. //-----------------------------------------------------------------------------
  152. void WaterBlock::setupVertexBlock( U32 width, U32 height, U32 rowOffset )
  153. {
  154. RayInfo rInfo;
  155. VectorF sunVector(-0.61f, 0.354f, 0.707f);
  156. if ( LIGHTMGR )
  157. {
  158. LightInfo* linfo = LIGHTMGR->getSpecialLight( LightManager::slSunLightType );
  159. if ( linfo )
  160. sunVector = linfo->getDirection();
  161. }
  162. sunVector.normalize();
  163. U32 numVerts = width * height;
  164. GFXWaterVertex *verts = new GFXWaterVertex[ numVerts ];
  165. U32 index = 0;
  166. for( U32 i=0; i<height; i++ )
  167. {
  168. for( U32 j=0; j<width; j++, index++ )
  169. {
  170. F32 vertX = getMin((-mObjScale.x / 2.0f) + mGridElementSize * j, mObjScale.x / 2.0f);
  171. F32 vertY = getMin((-mObjScale.y / 2.0f) + mGridElementSize * (i + rowOffset), mObjScale.y / 2.0f);
  172. GFXWaterVertex *vert = &verts[index];
  173. vert->point.x = vertX;
  174. vert->point.y = vertY;
  175. vert->point.z = 0.0;
  176. vert->normal.set(0,0,1);
  177. vert->undulateData.set( vertX, vertY );
  178. vert->horizonFactor.set( 0, 0, 0, 0 );
  179. // Calculate the water depth
  180. /*
  181. vert->depthData.set( 0.0f, 0.0f );
  182. Point3F start, end;
  183. Point3F worldPoint = vert->point + pos;
  184. start.x = end.x = worldPoint.x;
  185. start.y = end.y = worldPoint.y;
  186. start.z = -2000; // Really high, might be over kill
  187. end.z = 2000; // really low, might be overkill
  188. // Cast a ray to see how deep the water is. We are
  189. // currently just testing for terrain and atlas
  190. // objects, but potentially any object that responds
  191. // to a ray cast could detected.
  192. if(gClientContainer.castRay(start, end,
  193. //StaticObjectType |
  194. //InteriorObjectType |
  195. //ShapeBaseObjectType |
  196. //StaticShapeObjectType |
  197. //ItemObjectType |
  198. //StaticTSObjectType |
  199. TerrainObjectType
  200. , &rInfo))
  201. {
  202. F32 depth = -(rInfo.point.z - pos.z);
  203. if(depth <= 0.0f)
  204. {
  205. depth = 1.0f;
  206. }
  207. else
  208. {
  209. depth = depth / mVisibilityDepth;
  210. if(depth > 1.0f)
  211. {
  212. depth = 1.0f;
  213. }
  214. depth = 1.0f - depth;
  215. }
  216. vert->depthData.x = depth;
  217. }
  218. else
  219. {
  220. vert->depthData.x = 0.0f;
  221. }
  222. // Cast a ray to do some AO-style shadowing.
  223. F32 &shadow = vert->depthData.y;
  224. if(gClientContainer.castRay(worldPoint, worldPoint + sunVector * 9000.f,
  225. //StaticObjectType |
  226. //InteriorObjectType |
  227. //ShapeBaseObjectType |
  228. //StaticShapeObjectType |
  229. //ItemObjectType |
  230. //StaticTSObjectType |
  231. TerrainObjectType
  232. , &rInfo))
  233. {
  234. shadow = 0.f;
  235. }
  236. else
  237. {
  238. shadow = 1.f;
  239. }
  240. */
  241. }
  242. }
  243. // copy to vertex buffer
  244. GFXVertexBufferHandle <GFXWaterVertex> * vertBuff = new GFXVertexBufferHandle <GFXWaterVertex>;
  245. vertBuff->set( GFX, numVerts, GFXBufferTypeStatic );
  246. GFXWaterVertex *vbVerts = vertBuff->lock();
  247. dMemcpy( vbVerts, verts, sizeof(GFXWaterVertex) * numVerts );
  248. vertBuff->unlock();
  249. mVertBuffList.push_back( vertBuff );
  250. delete [] verts;
  251. }
  252. //-----------------------------------------------------------------------------
  253. // Set up a block of indices to match the block of vertices. The width is
  254. // always the width of the entire waterBlock, so this is a block of full rows.
  255. //-----------------------------------------------------------------------------
  256. void WaterBlock::setupPrimitiveBlock( U32 width, U32 height )
  257. {
  258. AssertFatal( height > 1, "WaterBlock::setupPrimitiveBlock() - invalid height" );
  259. // setup vertex / primitive buffers
  260. U32 numIndices = (width-1) * (height-1) * 6;
  261. U16 *indices = new U16[ numIndices ];
  262. U32 numVerts = width * height;
  263. // This uses indexed triangle lists instead of strips, but it shouldn't be
  264. // significantly slower if the indices cache well.
  265. // Rough diagram of the index order
  266. // 0----2----+ ...
  267. // | / | |
  268. // |/ | |
  269. // 1----3----+ ...
  270. // | | |
  271. // | | |
  272. // +----+----+ ...
  273. U32 index = 0;
  274. for( U32 i=0; i<(height-1); i++ )
  275. {
  276. for( U32 j=0; j<(width-1); j++, index+=6 )
  277. {
  278. // Process one quad at a time. Note it will re-use the same indices from
  279. // previous quad, thus optimizing vert cache. Cache will run out at
  280. // end of each row with this implementation however.
  281. indices[index+0] = (i) * mWidth + j; // 0
  282. indices[index+1] = (i+1) * mWidth + j; // 1
  283. indices[index+2] = i * mWidth + j+1; // 2
  284. indices[index+3] = (i+1) * mWidth + j; // 1
  285. indices[index+4] = (i+1) * mWidth + j+1; // 3
  286. indices[index+5] = i * mWidth + j+1; // 2
  287. }
  288. }
  289. GFXPrimitiveBufferHandle *indexBuff = new GFXPrimitiveBufferHandle;
  290. GFXPrimitive pInfo;
  291. pInfo.type = GFXTriangleList;
  292. pInfo.numPrimitives = numIndices / 3;
  293. pInfo.startIndex = 0;
  294. pInfo.minIndex = 0;
  295. pInfo.numVertices = numVerts;
  296. U16 *ibIndices;
  297. GFXPrimitive *piInput;
  298. indexBuff->set( GFX, numIndices, 1, GFXBufferTypeStatic );
  299. indexBuff->lock( &ibIndices, &piInput );
  300. dMemcpy( ibIndices, indices, numIndices * sizeof(U16) );
  301. dMemcpy( piInput, &pInfo, sizeof(GFXPrimitive) );
  302. indexBuff->unlock();
  303. mPrimBuffList.push_back( indexBuff );
  304. delete [] indices;
  305. }
  306. //------------------------------------------------------------------------------
  307. // Setup scenegraph data structure for materials
  308. //------------------------------------------------------------------------------
  309. SceneData WaterBlock::setupSceneGraphInfo( SceneRenderState *state )
  310. {
  311. SceneData sgData;
  312. sgData.lights[0] = LIGHTMGR->getSpecialLight( LightManager::slSunLightType );
  313. // fill in water's transform
  314. sgData.objTrans = &getRenderTransform();
  315. // fog
  316. sgData.setFogParams( state->getSceneManager()->getFogData() );
  317. // misc
  318. sgData.backBuffTex = REFLECTMGR->getRefractTex();
  319. sgData.reflectTex = mPlaneReflector.reflectTex;
  320. sgData.wireframe = GFXDevice::getWireframe() || smWireframe;
  321. return sgData;
  322. }
  323. //-----------------------------------------------------------------------------
  324. // set shader parameters
  325. //-----------------------------------------------------------------------------
  326. void WaterBlock::setShaderParams( SceneRenderState *state, BaseMatInstance *mat, const WaterMatParams &paramHandles)
  327. {
  328. // Set variables that will be assigned to shader consts within WaterCommon
  329. // before calling Parent::setShaderParams
  330. mUndulateMaxDist = F32_MAX;
  331. Parent::setShaderParams( state, mat, paramHandles );
  332. // Now set the rest of the shader consts that are either unique to this
  333. // class or that WaterObject leaves to us to handle...
  334. MaterialParameters* matParams = mat->getMaterialParameters();
  335. // set vertex shader constants
  336. //-----------------------------------
  337. MatrixF modelMat( getRenderTransform() );
  338. if ( paramHandles.mModelMatSC->isValid() )
  339. matParams->set(paramHandles.mModelMatSC, modelMat, GFXSCT_Float4x4);
  340. matParams->setSafe(paramHandles.mGridElementSizeSC, (F32)mGridElementSize);
  341. // set pixel shader constants
  342. //-----------------------------------
  343. ColorF c( mWaterFogData.color );
  344. matParams->setSafe( paramHandles.mBaseColorSC, c );
  345. // By default we need to show a true reflection is fullReflect is enabled and
  346. // we are above water.
  347. F32 reflect = mPlaneReflector.isEnabled() && !isUnderwater( state->getCameraPosition() );
  348. // If we were occluded the last frame a query was fetched ( not necessarily last frame )
  349. // and we weren't updated last frame... we don't have a valid texture to show
  350. // so use the cubemap / fake reflection color this frame.
  351. if ( mPlaneReflector.lastUpdateMs != REFLECTMGR->getLastUpdateMs() && mPlaneReflector.isOccluded() )
  352. reflect = false;
  353. Point4F reflectParams( mWaterPos.z, 0.0f, 1000.0f, !reflect );
  354. matParams->setSafe( paramHandles.mReflectParamsSC, reflectParams );
  355. VectorF reflectNorm = mReflectNormalUp ? VectorF(0,0,1) : static_cast<VectorF>(mPlaneReflector.refplane);
  356. matParams->setSafe(paramHandles.mReflectNormalSC, reflectNorm );
  357. }
  358. void WaterBlock::innerRender( SceneRenderState *state )
  359. {
  360. GFXDEBUGEVENT_SCOPE( WaterBlock_innerRender, ColorI( 255, 0, 0 ) );
  361. if ( mGenerateVB )
  362. {
  363. setupVBIB();
  364. mGenerateVB = false;
  365. }
  366. // Setup SceneData
  367. SceneData sgData = setupSceneGraphInfo( state );
  368. const Point3F &camPosition = state->getCameraPosition();
  369. // set the material
  370. S32 matIdx = getMaterialIndex( camPosition );
  371. if ( !initMaterial( matIdx ) )
  372. return;
  373. BaseMatInstance *mat = mMatInstances[matIdx];
  374. WaterMatParams matParams = mMatParamHandles[matIdx];
  375. // render the geometry
  376. if ( mat )
  377. {
  378. // setup proj/world transform
  379. mMatrixSet->setWorld(getRenderTransform());
  380. mMatrixSet->restoreSceneViewProjection();
  381. setShaderParams( state, mat, matParams );
  382. while ( mat->setupPass( state, sgData ) )
  383. {
  384. mat->setSceneInfo(state, sgData);
  385. mat->setTransforms(*mMatrixSet, state);
  386. setCustomTextures( matIdx, mat->getCurPass(), matParams );
  387. for ( U32 i = 0; i < mVertBuffList.size(); i++ )
  388. {
  389. GFX->setVertexBuffer( *mVertBuffList[i] );
  390. GFXPrimitiveBuffer *primBuff = *mPrimBuffList[i];
  391. GFX->setPrimitiveBuffer( primBuff );
  392. GFX->drawPrimitives();
  393. }
  394. }
  395. }
  396. }
  397. bool WaterBlock::setGridSizeProperty( void *obj, const char *index, const char *data )
  398. {
  399. WaterBlock* object = static_cast<WaterBlock*>(obj);
  400. F32 gridSize = dAtof(data);
  401. Point3F scale = object->getScale();
  402. if(gridSize < 0.001f)
  403. {
  404. object->logWarning("gridSize cannot be <= 0, clamping to scale");
  405. gridSize = getMin(scale.x, scale.y);
  406. }
  407. if(gridSize > scale.x || gridSize > scale.y)
  408. {
  409. object->logWarning("gridSize cannot be > scale. Your scale is (%g, %g) and your gridsize is %g",
  410. scale.x, scale.y, gridSize);
  411. gridSize = getMin(scale.x, scale.y);
  412. }
  413. object->mGridElementSize = gridSize;
  414. // This is a hack so the console system doesn't go in and set our variable
  415. // again, after we've already set it (possibly with a different value...)
  416. return false;
  417. }
  418. //-----------------------------------------------------------------------------
  419. // initPersistFields
  420. //-----------------------------------------------------------------------------
  421. void WaterBlock::initPersistFields()
  422. {
  423. addGroup( "WaterBlock" );
  424. addProtectedField( "gridElementSize", TypeF32, Offset( mGridElementSize, WaterBlock ),
  425. &setGridSizeProperty, &defaultProtectedGetFn, "Spacing between vertices in the WaterBlock mesh" );
  426. addProtectedField( "gridSize", TypeF32, Offset( mGridElementSize, WaterBlock ),
  427. &setGridSizeProperty, &defaultProtectedGetFn, "Duplicate of gridElementSize for backwards compatility" );
  428. endGroup( "WaterBlock" );
  429. Parent::initPersistFields();
  430. }
  431. bool WaterBlock::isUnderwater( const Point3F &pnt ) const
  432. {
  433. // Transform point into object space so we can test if it is within
  434. // the WaterBlock's object box, include rotation/scale.
  435. Point3F objPnt = pnt;
  436. mWorldToObj.mulP( pnt, &objPnt );
  437. objPnt.z -= 0.1f;
  438. Box3F testBox = mObjBox;
  439. testBox.scale( mObjScale );
  440. // We already tested if below the surface plane,
  441. // so clamping the z height of the box is not really necessary.
  442. testBox.maxExtents.z = testBox.getCenter().z;
  443. if ( testBox.isContained( objPnt ) )
  444. return true;
  445. return false;
  446. }
  447. void WaterBlock::clearVertBuffers()
  448. {
  449. for( U32 i=0; i<mVertBuffList.size(); i++ )
  450. delete mVertBuffList[i];
  451. mVertBuffList.clear();
  452. for( U32 i=0; i<mPrimBuffList.size(); i++ )
  453. delete mPrimBuffList[i];
  454. mPrimBuffList.clear();
  455. }
  456. void WaterBlock::inspectPostApply()
  457. {
  458. Parent::inspectPostApply();
  459. VectorF scale = getScale();
  460. if( scale.x < mGridElementSize )
  461. scale.x = mGridElementSize;
  462. if( scale.y < mGridElementSize )
  463. scale.y = mGridElementSize;
  464. if( scale != getScale() )
  465. setScale( scale );
  466. setMaskBits( UpdateMask );
  467. }
  468. void WaterBlock::setTransform( const MatrixF &mat )
  469. {
  470. // If our transform changes we need to recalculate the
  471. // per vertex depth/shadow info. Would be nice if this could
  472. // be done independently of generating the whole VBIB...
  473. Parent::setTransform( mat );
  474. // We don't need to regen our vb anymore since we aren't calculating
  475. // per vert depth/shadow on the cpu anymore.
  476. //if ( oldMat != mObjToWorld )
  477. // mGenerateVB = true;
  478. // Keep mWaterPlane up to date.
  479. mWaterFogData.plane.set( 0, 0, 1, -getPosition().z );
  480. }
  481. void WaterBlock::setScale( const Point3F &scale )
  482. {
  483. Point3F oldScale = mObjScale;
  484. Parent::setScale( scale );
  485. if ( oldScale != mObjScale )
  486. mGenerateVB = true;
  487. }
  488. void WaterBlock::onStaticModified( const char* slotName, const char*newValue )
  489. {
  490. Parent::onStaticModified( slotName, newValue );
  491. if ( dStricmp( slotName, "surfMaterial" ) == 0 )
  492. setMaskBits( MaterialMask );
  493. if ( dStricmp( slotName, "gridElementSize" ) == 0 )
  494. {
  495. mGenerateVB = true;
  496. setMaskBits( UpdateMask );
  497. }
  498. }
  499. bool WaterBlock::castRay( const Point3F &start, const Point3F &end, RayInfo *info )
  500. {
  501. // Simply look for the hit on the water plane
  502. // and ignore any future issues with waves, etc.
  503. const Point3F norm(0,0,1);
  504. PlaneF plane( Point3F::Zero, norm );
  505. F32 hit = plane.intersect( start, end );
  506. if ( hit < 0.0f || hit > 1.0f )
  507. return false;
  508. info->t = hit;
  509. info->object = this;
  510. info->point = start + ( ( end - start ) * hit );
  511. info->normal = norm;
  512. info->material = mMatInstances[WaterMat];
  513. return mObjBox.isContained(info->point);
  514. }
  515. bool WaterBlock::buildPolyList( PolyListContext context, AbstractPolyList* polyList, const Box3F& box, const SphereF& )
  516. {
  517. if(context == PLC_Navigation && box.isOverlapped(mWorldBox))
  518. {
  519. polyList->setObject( this );
  520. MatrixF mat(true);
  521. Point3F pos = getPosition();
  522. pos.x = pos.y = 0;
  523. mat.setPosition(pos);
  524. polyList->setTransform( &mat, Point3F(1, 1, 1) );
  525. Box3F ov = box.getOverlap(mWorldBox);
  526. Point3F
  527. p0(ov.minExtents.x, ov.maxExtents.y, 0),
  528. p1(ov.maxExtents.x, ov.maxExtents.y, 0),
  529. p2(ov.maxExtents.x, ov.minExtents.y, 0),
  530. p3(ov.minExtents.x, ov.minExtents.y, 0);
  531. // Add vertices to poly list.
  532. U32 v0 = polyList->addPoint(p0);
  533. polyList->addPoint(p1);
  534. polyList->addPoint(p2);
  535. polyList->addPoint(p3);
  536. // Add plane between first three vertices.
  537. polyList->begin(0, 0);
  538. polyList->vertex(v0);
  539. polyList->vertex(v0+1);
  540. polyList->vertex(v0+2);
  541. polyList->plane(v0, v0+1, v0+2);
  542. polyList->end();
  543. // Add plane between last three vertices.
  544. polyList->begin(0, 1);
  545. polyList->vertex(v0+2);
  546. polyList->vertex(v0+3);
  547. polyList->vertex(v0);
  548. polyList->plane(v0+2, v0+3, v0);
  549. polyList->end();
  550. return true;
  551. }
  552. return false;
  553. }
  554. F32 WaterBlock::getWaterCoverage( const Box3F &testBox ) const
  555. {
  556. Box3F wbox = getWorldBox();
  557. wbox.maxExtents.z = wbox.getCenter().z;
  558. F32 coverage = 0.0f;
  559. if ( wbox.isOverlapped(testBox) )
  560. {
  561. if (wbox.maxExtents.z < testBox.maxExtents.z)
  562. coverage = (wbox.maxExtents.z - testBox.minExtents.z) / (testBox.maxExtents.z - testBox.minExtents.z);
  563. else
  564. coverage = 1.0f;
  565. }
  566. return coverage;
  567. }
  568. F32 WaterBlock::getSurfaceHeight( const Point2F &pos ) const
  569. {
  570. if ( !mWorldBox.isContained( pos ) )
  571. return -1.0f;
  572. return getPosition().z;
  573. }
  574. void WaterBlock::_getWaterPlane( const Point3F &camPos, PlaneF &outPlane, Point3F &outPos )
  575. {
  576. outPos = getPosition();
  577. if ( mReflectNormalUp )
  578. outPlane.set( outPos, Point3F(0,0,1) );
  579. else
  580. {
  581. Point3F normal;
  582. getRenderTransform().getColumn( 2, &normal );
  583. outPlane.set( outPos, normal );
  584. }
  585. }
  586. F32 WaterBlock::distanceTo( const Point3F& point ) const
  587. {
  588. Box3F waterBox = getWorldBox();
  589. waterBox.maxExtents.z = getPosition().z;
  590. return waterBox.getDistanceToPoint( point );
  591. }