waterBlock.cpp 21 KB

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