waterBlock.cpp 22 KB

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