river.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. #ifndef _RIVER_H_
  23. #define _RIVER_H_
  24. #ifndef _SCENEOBJECT_H_
  25. #include "scene/sceneObject.h"
  26. #endif
  27. #ifndef _GFXTEXTUREHANDLE_H_
  28. #include "gfx/gfxTextureHandle.h"
  29. #endif
  30. #ifndef _GFXVERTEXBUFFER_H_
  31. #include "gfx/gfxVertexBuffer.h"
  32. #endif
  33. #ifndef _GFXPRIMITIVEBUFFER_H_
  34. #include "gfx/gfxPrimitiveBuffer.h"
  35. #endif
  36. #ifndef _CLIPPEDPOLYLIST_H_
  37. #include "collision/clippedPolyList.h"
  38. #endif
  39. #ifndef _GFXSTATEBLOCK_H_
  40. #include "gfx/gfxStateBlock.h"
  41. #endif
  42. #ifndef _WATEROBJECT_H_
  43. #include "waterObject.h"
  44. #endif
  45. //-------------------------------------------------------------------------
  46. // RiverSplineNode Class
  47. //-------------------------------------------------------------------------
  48. class Path;
  49. class TerrainBlock;
  50. struct ObjectRenderInst;
  51. class RiverSplineNode
  52. {
  53. public:
  54. RiverSplineNode() :x(0.0f), y(0.0f), z(0.0f), width(0.0f), depth(0.0f) {}
  55. F32 x;
  56. F32 y;
  57. F32 z;
  58. F32 width;
  59. F32 depth;
  60. VectorF normal;
  61. RiverSplineNode& operator=(const RiverSplineNode&);
  62. RiverSplineNode operator+(const RiverSplineNode&) const;
  63. RiverSplineNode operator-(const RiverSplineNode&) const;
  64. RiverSplineNode operator*(const F32) const;
  65. F32 len();
  66. };
  67. inline F32 RiverSplineNode::len()
  68. {
  69. return mSqrt(F32(x*x + y*y + z*z));
  70. }
  71. inline RiverSplineNode& RiverSplineNode::operator=(const RiverSplineNode &_node)
  72. {
  73. x = _node.x;
  74. y = _node.y;
  75. z = _node.z;
  76. width = _node.width;
  77. depth = _node.depth;
  78. normal = _node.normal;
  79. return *this;
  80. }
  81. inline RiverSplineNode RiverSplineNode::operator+(const RiverSplineNode& _add) const
  82. {
  83. RiverSplineNode result;
  84. result.x = x + _add.x;
  85. result.y = y + _add.y;
  86. result.z = z + _add.z;
  87. result.width = width + _add.width;
  88. result.depth = depth + _add.depth;
  89. result.normal = normal + _add.normal;
  90. return result;
  91. }
  92. inline RiverSplineNode RiverSplineNode::operator-(const RiverSplineNode& _rSub) const
  93. {
  94. RiverSplineNode result;
  95. result.x = x - _rSub.x;
  96. result.y = y - _rSub.y;
  97. result.z = z - _rSub.z;
  98. result.width = width - _rSub.width;
  99. result.depth = depth - _rSub.depth;
  100. result.normal = normal - _rSub.normal;
  101. return result;
  102. }
  103. inline RiverSplineNode operator*(const F32 mul, const RiverSplineNode& multiplicand)
  104. {
  105. return multiplicand * mul;
  106. }
  107. inline RiverSplineNode RiverSplineNode::operator*(const F32 _mul) const
  108. {
  109. RiverSplineNode result;
  110. result.x = x * _mul;
  111. result.y = y * _mul;
  112. result.z = z * _mul;
  113. result.width = width * _mul;
  114. result.depth = depth * _mul;
  115. result.normal = normal * _mul;
  116. return result;
  117. }
  118. //-------------------------------------------------------------------------
  119. // Structures
  120. //-------------------------------------------------------------------------
  121. struct RiverRenderBatch
  122. {
  123. U32 startSegmentIdx;
  124. U32 endSegmentIdx;
  125. U32 startVert;
  126. U32 endVert;
  127. U32 startIndex;
  128. U32 endIndex;
  129. U32 totalRows;
  130. U32 indexCount;
  131. U32 vertCount;
  132. U32 triangleCount;
  133. };
  134. typedef Vector<RiverRenderBatch> RiverBatchVector;
  135. struct RiverNode
  136. {
  137. // The 3D position of the node
  138. Point3F point;
  139. // The width of the River at this node (meters)
  140. F32 width;
  141. // The depth of the River at this node (meters)
  142. F32 depth;
  143. VectorF normal;
  144. };
  145. typedef Vector<RiverNode> RiverNodeVector;
  146. struct RiverSlice
  147. {
  148. RiverSlice()
  149. {
  150. p0.zero();
  151. p1.zero();
  152. p2.zero();
  153. pb0.zero();
  154. pb2.zero();
  155. uvec.zero();
  156. fvec.zero();
  157. rvec.zero();
  158. normal.zero();
  159. width = 0.0f;
  160. depth = 0.0f;
  161. texCoordV = 0.0f;
  162. parentNodeIdx = -1;
  163. };
  164. Point3F p0; // upper left
  165. Point3F p1; // upper center
  166. Point3F p2; // upper right
  167. Point3F pb0; // bottom left
  168. Point3F pb2; // bottom right
  169. VectorF uvec;
  170. VectorF fvec;
  171. VectorF rvec;
  172. VectorF normal;
  173. F32 width;
  174. F32 depth;
  175. F32 texCoordV;
  176. U32 parentNodeIdx;
  177. };
  178. typedef Vector<RiverSlice> RiverSliceVector;
  179. //-------------------------------------------------------------------------
  180. // RiverSegment Class
  181. //-------------------------------------------------------------------------
  182. static Point3F sSegmentPointCompareReference;
  183. class RiverSegment
  184. {
  185. public:
  186. RiverSegment();
  187. RiverSegment( RiverSlice *rs0, RiverSlice *rs1 );
  188. void set( RiverSlice *rs0, RiverSlice *rs1 );
  189. F32 TexCoordStart() { return slice0->texCoordV; }
  190. F32 TexCoordEnd() { return slice1->texCoordV; }
  191. Point3F getP00() const { return slice0->p0; }
  192. Point3F getP01() const { return slice1->p0; }
  193. Point3F getP11() const { return slice1->p2; }
  194. Point3F getP10() const { return slice0->p2; }
  195. // NOTE:
  196. // For purposes of collision testing against a RiverSegment we represent
  197. // it as a set of 6 planes (one for each face) much like a frustum.
  198. // This is actually a flawed representation that will be more incorrect
  199. // the more twist/bend exists between the two RiverSlices making up
  200. // the segment. Basically we treat the four points that make up a "face"
  201. // as if they were coplanar.
  202. Point3F getSurfaceCenter() const { return (slice0->p1 + slice1->p1) / 2; }
  203. Point3F getSurfaceNormal() const { return ( -mPlanes[4].getNormal() ); }
  204. Point3F getFaceCenter( U32 faceIdx ) const;
  205. bool intersectBox( const Box3F &bounds ) const;
  206. bool containsPoint( const Point3F &pnt ) const;
  207. F32 distanceToSurface( const Point3F &pnt ) const;
  208. // Quick access to the segment's points
  209. Point3F& operator[](U32);
  210. const Point3F& operator[](U32) const;
  211. Point3F& operator[](S32 i) { return operator[](U32(i)); }
  212. const Point3F& operator[](S32 i ) const { return operator[](U32(i)); }
  213. RiverSlice *slice0;
  214. RiverSlice *slice1;
  215. PlaneF mPlanes[6];
  216. U32 mPlaneCount;
  217. U32 columns;
  218. U32 rows;
  219. U32 startVert;
  220. U32 endVert;
  221. U32 startIndex;
  222. U32 endIndex;
  223. U32 numVerts;
  224. U32 numTriangles;
  225. Box3F worldbounds;
  226. protected:
  227. PlaneF _getBestPlane( const Point3F *p0, const Point3F *p1, const Point3F *p2, const Point3F *p3 );
  228. };
  229. typedef Vector<RiverSegment> RiverSegmentVector;
  230. inline Point3F& RiverSegment::operator[](U32 index)
  231. {
  232. AssertFatal(index < 8, "RiverSegment::operator[] - out of bounds array access!");
  233. RiverSlice *slice = NULL;
  234. if ( index > 3 )
  235. {
  236. slice = slice1;
  237. index -= 4;
  238. }
  239. else
  240. {
  241. slice = slice0;
  242. }
  243. if ( index == 0 )
  244. return slice->p0;
  245. if ( index == 1 )
  246. return slice->p2;
  247. if ( index == 2 )
  248. return slice->pb0;
  249. else //( index == 3 )
  250. return slice->pb2;
  251. }
  252. inline const Point3F& RiverSegment::operator[](U32 index) const
  253. {
  254. AssertFatal(index < 8, "RiverSegment::operator[] - out of bounds array access!");
  255. RiverSlice *slice = NULL;
  256. if ( index > 3 )
  257. {
  258. slice = slice1;
  259. index -= 4;
  260. }
  261. else
  262. {
  263. slice = slice0;
  264. }
  265. if ( index == 0 )
  266. return slice->p0;
  267. if ( index == 1 )
  268. return slice->p2;
  269. if ( index == 2 )
  270. return slice->pb0;
  271. else// ( index == 3 )
  272. return slice->pb2;
  273. }
  274. //------------------------------------------------------------------------------
  275. // River Class
  276. //------------------------------------------------------------------------------
  277. class ParticleEmitter;
  278. class ParticleEmitterData;
  279. struct RiverNodeList;
  280. class River : public WaterObject
  281. {
  282. private:
  283. friend class GuiRiverEditorCtrl;
  284. friend class GuiRiverEditorUndoAction;
  285. typedef WaterObject Parent;
  286. protected:
  287. enum
  288. {
  289. RiverMask = Parent::NextFreeMask,
  290. NodeMask = Parent::NextFreeMask << 1,
  291. RegenMask = Parent::NextFreeMask << 2,
  292. InitialUpdateMask = Parent::NextFreeMask << 3,
  293. SelectedMask = Parent::NextFreeMask << 4,
  294. MaterialMask = Parent::NextFreeMask << 5,
  295. NextFreeMask = Parent::NextFreeMask << 6,
  296. };
  297. public:
  298. River();
  299. ~River();
  300. DECLARE_CONOBJECT(River);
  301. // ConObject.
  302. static void initPersistFields();
  303. static void consoleInit();
  304. // SimObject
  305. bool onAdd();
  306. void onRemove();
  307. void inspectPostApply();
  308. void onStaticModified(const char* slotName, const char*newValue = NULL);
  309. void writeFields(Stream &stream, U32 tabStop);
  310. bool writeField( StringTableEntry fieldname, const char *value );
  311. // NetObject
  312. U32 packUpdate(NetConnection *, U32, BitStream *);
  313. void unpackUpdate(NetConnection *, BitStream *);
  314. // SceneObject
  315. virtual void setTransform( const MatrixF &mat );
  316. virtual void setScale( const VectorF &scale );
  317. virtual bool castRay(const Point3F &start, const Point3F &end, RayInfo* info);
  318. virtual bool collideBox(const Point3F &start, const Point3F &end, RayInfo* info);
  319. virtual bool containsPoint( const Point3F& point ) const { return containsPoint( point, NULL ); }
  320. virtual bool buildPolyList( PolyListContext context, AbstractPolyList* polyList, const Box3F& box, const SphereF& sphere );
  321. // WaterObject
  322. virtual F32 getWaterCoverage( const Box3F &worldBox ) const;
  323. virtual F32 getSurfaceHeight( const Point2F &pos ) const;
  324. virtual VectorF getFlow( const Point3F &pos ) const;
  325. virtual void onReflectionInfoChanged();
  326. virtual void updateUnderwaterEffect( SceneRenderState *state );
  327. virtual bool isUnderwater( const Point3F &pnt ) const;
  328. F32 distanceToSurface( const Point3F &pnt, U32 segmentIdx );
  329. bool containsPoint( const Point3F &worldPos, U32 *nodeIdx ) const;
  330. // Cast a ray against the river -- THE TOP SURFACE ONLY
  331. bool collideRay( const Point3F &origin, const Point3F &direction, U32 *nodeIdx = NULL, Point3F *collisionPnt = NULL ) const;
  332. void regenerate();
  333. void setMetersPerSegment( F32 meters );
  334. void setBatchSize( U32 level );
  335. void setShowNodes( bool enabled );
  336. void setMaxDivisionSize( F32 meters );
  337. void setColumnCount( S32 count );
  338. U32 insertNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal, const U32 &idx );
  339. U32 addNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal );
  340. void setNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal, const U32 &idx );
  341. void deleteNode( U32 idx );
  342. void buildNodesFromList( RiverNodeList* list );
  343. bool getClosestNode( const Point3F &pos, U32 &idx ) const;
  344. Point3F getNodePosition( U32 idx ) const;
  345. void setNodePosition( U32 idx, const Point3F &pos );
  346. MatrixF getNodeTransform( U32 idx ) const;
  347. F32 getNodeWidth( U32 idx ) const;
  348. void setNodeWidth( U32 idx, F32 width );
  349. F32 getNodeDepth( U32 idx ) const;
  350. void setNodeDepth( U32 idx, F32 depth );
  351. void setNodeHeight( U32 idx, F32 height );
  352. void setNodeNormal( U32 idx, const VectorF &normal );
  353. VectorF getNodeNormal( U32 idx ) const;
  354. /// Protected 'Component' Field setter that will add a component to the list.
  355. static bool addNodeFromField( void *object, const char *index, const char *data );
  356. static SimSet* getServerSet();
  357. static bool smEditorOpen;
  358. static bool smWireframe;
  359. static bool smShowWalls;
  360. static bool smShowNodes;
  361. static bool smShowSpline;
  362. static bool smShowRiver;
  363. static SimObjectPtr<SimSet> smServerRiverSet;
  364. protected:
  365. void _loadRenderData();
  366. bool _setRenderData();
  367. void _render( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *matInst );
  368. void _makeRenderBatches( const Point3F &cameraPos );
  369. void _makeHighLODBuffers();
  370. U32 _insertNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal, const U32 &idx );
  371. U32 _addNode( const Point3F &pos, const F32 &width, const F32 &depth, const VectorF &normal );
  372. void _regenerate();
  373. void _generateSlices();
  374. void _generateSegments();
  375. void _generateVerts();
  376. bool _getTerrainHeight( const Point2F &pos, F32 &height );
  377. bool _getTerrainHeight( F32 x, F32 y, F32 &height );
  378. // WaterObject
  379. virtual void setShaderParams( SceneRenderState *state, BaseMatInstance *mat, const WaterMatParams &paramHandles );
  380. virtual void innerRender( SceneRenderState *state );
  381. virtual void _getWaterPlane( const Point3F &camPos, PlaneF &outPlane, Point3F &outPos );
  382. protected:
  383. RiverSliceVector mSlices;
  384. RiverNodeVector mNodes;
  385. RiverSegmentVector mSegments;
  386. GFXVertexBufferHandle<GFXWaterVertex> mVB_high; // the high detail vertex buffer
  387. GFXVertexBufferHandle<GFXWaterVertex> mVB_low; // the low detail vertex buffer
  388. GFXPrimitiveBufferHandle mPB_high; // the high detail prim buffer
  389. GFXPrimitiveBufferHandle mPB_low; // the low detail prim buffer
  390. RiverBatchVector mHighLODBatches;
  391. RiverBatchVector mLowLODBatches;
  392. U32 mLowVertCount;
  393. U32 mHighVertCount;
  394. U32 mLowTriangleCount;
  395. U32 mHighTriangleCount;
  396. // Fields.
  397. U32 mSegmentsPerBatch;
  398. F32 mMetersPerSegment;
  399. F32 mDepthScale;
  400. F32 mFlowMagnitude;
  401. F32 mLodDistance;
  402. // Divide segments that are greater than this length
  403. // into sections of this size, or as close as possible
  404. // while maintaining an equal division size.
  405. F32 mMaxDivisionSize;
  406. F32 mMinDivisionSize;
  407. U32 mColumnCount;
  408. };
  409. #endif // _RIVER_H_