waterBlock.cpp 22 KB

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