meshRoad.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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 _MESHROAD_H_
  23. #define _MESHROAD_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 _MATINSTANCE_H_
  40. #include "materials/matInstance.h"
  41. #endif
  42. #ifndef _CONVEX_H_
  43. #include "collision/convex.h"
  44. #endif
  45. //extern U32 gIdxArray[6][2][3];
  46. struct MeshRoadHitSegment
  47. {
  48. U32 idx;
  49. F32 t;
  50. };
  51. class MeshRoad;
  52. //-------------------------------------------------------------------------
  53. // MeshRoadConvex Class
  54. //-------------------------------------------------------------------------
  55. class MeshRoadConvex : public Convex
  56. {
  57. typedef Convex Parent;
  58. friend class MeshRoad;
  59. protected:
  60. MeshRoad *pRoad;
  61. public:
  62. U32 faceId;
  63. U32 triangleId;
  64. U32 segmentId;
  65. Point3F verts[4];
  66. PlaneF normal;
  67. Box3F box;
  68. public:
  69. MeshRoadConvex() { mType = MeshRoadConvexType; }
  70. MeshRoadConvex( const MeshRoadConvex& cv ) {
  71. mType = MeshRoadConvexType;
  72. mObject = cv.mObject;
  73. pRoad = cv.pRoad;
  74. faceId = cv.faceId;
  75. triangleId = cv.triangleId;
  76. segmentId = cv.segmentId;
  77. verts[0] = cv.verts[0];
  78. verts[1] = cv.verts[1];
  79. verts[2] = cv.verts[2];
  80. verts[3] = cv.verts[3];
  81. normal = cv.normal;
  82. box = cv.box;
  83. }
  84. const MatrixF& getTransform() const;
  85. Box3F getBoundingBox() const;
  86. Box3F getBoundingBox(const MatrixF& mat, const Point3F& scale) const;
  87. Point3F support(const VectorF& vec) const;
  88. void getFeatures(const MatrixF& mat,const VectorF& n, ConvexFeature* cf);
  89. void getPolyList(AbstractPolyList* list);
  90. };
  91. //-------------------------------------------------------------------------
  92. // MeshRoadSplineNode Class
  93. //-------------------------------------------------------------------------
  94. class Path;
  95. class TerrainBlock;
  96. struct ObjectRenderInst;
  97. class MeshRoadSplineNode
  98. {
  99. public:
  100. MeshRoadSplineNode() {}
  101. F32 x;
  102. F32 y;
  103. F32 z;
  104. F32 width;
  105. F32 depth;
  106. VectorF normal;
  107. MeshRoadSplineNode& operator=(const MeshRoadSplineNode&);
  108. MeshRoadSplineNode operator+(const MeshRoadSplineNode&) const;
  109. MeshRoadSplineNode operator-(const MeshRoadSplineNode&) const;
  110. MeshRoadSplineNode operator*(const F32) const;
  111. F32 len();
  112. Point3F getPosition() const { return Point3F(x,y,z); };
  113. };
  114. inline F32 MeshRoadSplineNode::len()
  115. {
  116. return mSqrt(F32(x*x + y*y + z*z));
  117. }
  118. inline MeshRoadSplineNode& MeshRoadSplineNode::operator=(const MeshRoadSplineNode &_node)
  119. {
  120. x = _node.x;
  121. y = _node.y;
  122. z = _node.z;
  123. width = _node.width;
  124. depth = _node.depth;
  125. normal = _node.normal;
  126. return *this;
  127. }
  128. inline MeshRoadSplineNode MeshRoadSplineNode::operator+(const MeshRoadSplineNode& _add) const
  129. {
  130. MeshRoadSplineNode result;
  131. result.x = x + _add.x;
  132. result.y = y + _add.y;
  133. result.z = z + _add.z;
  134. result.width = width + _add.width;
  135. result.depth = depth + _add.depth;
  136. result.normal = normal + _add.normal;
  137. return result;
  138. }
  139. inline MeshRoadSplineNode MeshRoadSplineNode::operator-(const MeshRoadSplineNode& _rSub) const
  140. {
  141. MeshRoadSplineNode result;
  142. result.x = x - _rSub.x;
  143. result.y = y - _rSub.y;
  144. result.z = z - _rSub.z;
  145. result.width = width - _rSub.width;
  146. result.depth = depth - _rSub.depth;
  147. result.normal = normal - _rSub.normal;
  148. return result;
  149. }
  150. inline MeshRoadSplineNode operator*(const F32 mul, const MeshRoadSplineNode& multiplicand)
  151. {
  152. return multiplicand * mul;
  153. }
  154. inline MeshRoadSplineNode MeshRoadSplineNode::operator*(const F32 _mul) const
  155. {
  156. MeshRoadSplineNode result;
  157. result.x = x * _mul;
  158. result.y = y * _mul;
  159. result.z = z * _mul;
  160. result.width = width * _mul;
  161. result.depth = depth * _mul;
  162. result.normal = normal * _mul;
  163. return result;
  164. }
  165. //-------------------------------------------------------------------------
  166. // Structures
  167. //-------------------------------------------------------------------------
  168. struct MeshRoadRenderBatch
  169. {
  170. U32 startSegmentIdx;
  171. U32 endSegmentIdx;
  172. U32 startVert;
  173. U32 endVert;
  174. U32 startIndex;
  175. U32 endIndex;
  176. U32 totalRows;
  177. U32 indexCount;
  178. U32 vertCount;
  179. U32 triangleCount;
  180. };
  181. typedef Vector<MeshRoadRenderBatch> MeshRoadBatchVector;
  182. struct MeshRoadNode
  183. {
  184. // The 3D position of the node
  185. Point3F point;
  186. // The width of the River at this node (meters)
  187. F32 width;
  188. // The depth of the River at this node (meters)
  189. F32 depth;
  190. VectorF normal;
  191. };
  192. typedef Vector<MeshRoadNode> MeshRoadNodeVector;
  193. struct MeshRoadSlice
  194. {
  195. MeshRoadSlice()
  196. {
  197. p0.zero();
  198. p1.zero();
  199. p2.zero();
  200. pb0.zero();
  201. pb2.zero();
  202. uvec.zero();
  203. fvec.zero();
  204. rvec.zero();
  205. width = 0.0f;
  206. depth = 0.0f;
  207. normal.set(0,0,1);
  208. texCoordV = 0.0f;
  209. parentNodeIdx = -1;
  210. };
  211. Point3F p0; // upper left
  212. Point3F p1; // upper center
  213. Point3F p2; // upper right
  214. Point3F pb0; // bottom left
  215. Point3F pb2; // bottom right
  216. VectorF uvec;
  217. VectorF fvec;
  218. VectorF rvec;
  219. F32 width;
  220. F32 depth;
  221. Point3F normal;
  222. F32 t;
  223. F32 texCoordV;
  224. U32 parentNodeIdx;
  225. };
  226. typedef Vector<MeshRoadSlice> MeshRoadSliceVector;
  227. //-------------------------------------------------------------------------
  228. // MeshRoadSegment Class
  229. //-------------------------------------------------------------------------
  230. class MeshRoadSegment
  231. {
  232. public:
  233. MeshRoadSegment();
  234. MeshRoadSegment( MeshRoadSlice *rs0, MeshRoadSlice *rs1, const MatrixF &roadMat );
  235. void set( MeshRoadSlice *rs0, MeshRoadSlice *rs1 );
  236. F32 TexCoordStart() const { return slice0->texCoordV; }
  237. F32 TexCoordEnd() const { return slice1->texCoordV; }
  238. const Point3F& getP00() const { return slice0->p0; }
  239. const Point3F& getP01() const { return slice1->p0; }
  240. const Point3F& getP11() const { return slice1->p2; }
  241. const Point3F& getP10() const { return slice0->p2; }
  242. Point3F getSurfaceCenter() const { return ( slice0->p1 + slice1->p1 ) / 2.0f; }
  243. Point3F getSurfaceNormal() const { return -mPlanes[4].getNormal(); }
  244. bool intersectBox( const Box3F &bounds ) const;
  245. bool containsPoint( const Point3F &pnt ) const;
  246. F32 distanceToSurface( const Point3F &pnt ) const;
  247. F32 length() const { return ( slice1->p1 - slice0->p1 ).len(); }
  248. // Quick access to the segment's points
  249. Point3F& operator[](U32);
  250. const Point3F& operator[](U32) const;
  251. Point3F& operator[](S32 i) { return operator[](U32(i)); }
  252. const Point3F& operator[](S32 i ) const { return operator[](U32(i)); }
  253. const Box3F& getWorldBounds() const { return worldbounds; }
  254. MeshRoadSlice *slice0;
  255. MeshRoadSlice *slice1;
  256. protected:
  257. PlaneF mPlanes[6];
  258. U32 mPlaneCount;
  259. U32 columns;
  260. U32 rows;
  261. U32 startVert;
  262. U32 endVert;
  263. U32 startIndex;
  264. U32 endIndex;
  265. U32 numVerts;
  266. U32 numTriangles;
  267. Box3F objectbounds;
  268. Box3F worldbounds;
  269. };
  270. typedef Vector<MeshRoadSegment> MeshRoadSegmentVector;
  271. inline Point3F& MeshRoadSegment::operator[](U32 index)
  272. {
  273. AssertFatal(index < 8, "MeshRoadSegment::operator[] - out of bounds array access!");
  274. MeshRoadSlice *slice = NULL;
  275. if ( index > 3 )
  276. {
  277. slice = slice1;
  278. index -= 4;
  279. }
  280. else
  281. {
  282. slice = slice0;
  283. }
  284. if ( index == 0 )
  285. return slice->p0;
  286. if ( index == 1 )
  287. return slice->p2;
  288. if ( index == 2 )
  289. return slice->pb0;
  290. else //( index == 3 )
  291. return slice->pb2;
  292. }
  293. inline const Point3F& MeshRoadSegment::operator[](U32 index) const
  294. {
  295. AssertFatal(index < 8, "MeshRoadSegment::operator[] - out of bounds array access!");
  296. MeshRoadSlice *slice = NULL;
  297. if ( index > 3 )
  298. {
  299. slice = slice1;
  300. index -= 4;
  301. }
  302. else
  303. {
  304. slice = slice0;
  305. }
  306. if ( index == 0 )
  307. return slice->p0;
  308. if ( index == 1 )
  309. return slice->p2;
  310. if ( index == 2 )
  311. return slice->pb0;
  312. else// ( index == 3 )
  313. return slice->pb2;
  314. }
  315. //------------------------------------------------------------------------------
  316. // MeshRoad Class
  317. //------------------------------------------------------------------------------
  318. class PhysicsBody;
  319. struct MeshRoadNodeList;
  320. class MeshRoad : public SceneObject
  321. {
  322. private:
  323. friend class GuiMeshRoadEditorCtrl;
  324. friend class GuiMeshRoadEditorUndoAction;
  325. friend class MeshRoadConvex;
  326. typedef SceneObject Parent;
  327. enum
  328. {
  329. MeshRoadMask = Parent::NextFreeMask,
  330. NodeMask = Parent::NextFreeMask << 1,
  331. RegenMask = Parent::NextFreeMask << 2,
  332. InitialUpdateMask = Parent::NextFreeMask << 3,
  333. SelectedMask = Parent::NextFreeMask << 4,
  334. MaterialMask = Parent::NextFreeMask << 5,
  335. NextFreeMask = Parent::NextFreeMask << 6,
  336. };
  337. public:
  338. MeshRoad();
  339. ~MeshRoad();
  340. DECLARE_CONOBJECT(MeshRoad);
  341. // ConObject.
  342. static void initPersistFields();
  343. static void consoleInit();
  344. // SimObject
  345. bool onAdd();
  346. void onRemove();
  347. void onEditorEnable();
  348. void onEditorDisable();
  349. void inspectPostApply();
  350. void onStaticModified(const char* slotName, const char*newValue = NULL);
  351. void writeFields(Stream &stream, U32 tabStop);
  352. bool writeField( StringTableEntry fieldname, const char *value );
  353. // NetObject
  354. U32 packUpdate(NetConnection *, U32, BitStream *);
  355. void unpackUpdate(NetConnection *, BitStream *);
  356. // SceneObject
  357. virtual void prepRenderImage( SceneRenderState* sceneState );
  358. virtual void setTransform( const MatrixF &mat );
  359. virtual void setScale( const VectorF &scale );
  360. // SceneObject - Collision
  361. virtual void buildConvex(const Box3F& box,Convex* convex);
  362. virtual bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere);
  363. virtual bool castRay(const Point3F &start, const Point3F &end, RayInfo* info);
  364. virtual bool collideBox(const Point3F &start, const Point3F &end, RayInfo* info);
  365. // MeshRoad
  366. void regenerate();
  367. void setBatchSize( U32 level );
  368. void setTextureFile( StringTableEntry file );
  369. void setTextureRepeat( F32 meters );
  370. bool collideRay( const Point3F &origin, const Point3F &direction, U32 *nodeIdx = NULL, Point3F *collisionPnt = NULL );
  371. bool buildSegmentPolyList( AbstractPolyList* polyList, U32 startSegIdx, U32 endSegIdx, bool capFront, bool capEnd );
  372. void buildNodesFromList( MeshRoadNodeList* list );
  373. U32 insertNode( const Point3F &pos, const F32 &width, const F32 &depth, const Point3F &normal, const U32 &idx );
  374. U32 addNode( const Point3F &pos, const F32 &width, const F32 &depth, const Point3F &normal );
  375. void deleteNode( U32 idx );
  376. void setNode( const Point3F &pos, const F32 &width, const F32 &depth, const Point3F &normal, const U32 &idx );
  377. const MeshRoadNode& getNode( U32 idx );
  378. VectorF getNodeNormal( U32 idx );
  379. void setNodeNormal( U32 idx, const VectorF &normal );
  380. Point3F getNodePosition( U32 idx );
  381. void setNodePosition( U32 idx, const Point3F &pos );
  382. F32 getNodeWidth( U32 idx );
  383. void setNodeWidth( U32 idx, F32 width );
  384. F32 getNodeDepth( U32 idx );
  385. void setNodeDepth( U32 idx, F32 depth );
  386. MatrixF getNodeTransform( U32 idx );
  387. void calcSliceTransform( U32 idx, MatrixF &mat );
  388. bool isEndNode( U32 idx ) { return ( mNodes.size() > 0 && ( idx == 0 || idx == mNodes.size() - 1 ) ); }
  389. U32 getSegmentCount() { return mSegments.size(); }
  390. const MeshRoadSegment& getSegment( U32 idx ) { return mSegments[idx]; }
  391. F32 getRoadLength() const;
  392. static SimSet* getServerSet();
  393. /// Protected 'Component' Field setter that will add a component to the list.
  394. static bool addNodeFromField( void *object, const char *index, const char *data );
  395. static bool smEditorOpen;
  396. static bool smWireframe;
  397. static bool smShowBatches;
  398. static bool smShowSpline;
  399. static bool smShowRoad;
  400. static SimObjectPtr<SimSet> smServerMeshRoadSet;
  401. protected:
  402. void _initMaterial();
  403. void _debugRender( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance* );
  404. U32 _insertNode( const Point3F &pos, const F32 &width, const F32 &depth, const Point3F &normal, const U32 &idx );
  405. U32 _addNode( const Point3F &pos, const F32 &width, const F32 &depth, const Point3F &normal );
  406. void _regenerate();
  407. void _generateSlices();
  408. void _generateSegments();
  409. void _generateVerts();
  410. protected:
  411. MeshRoadSliceVector mSlices;
  412. MeshRoadNodeVector mNodes;
  413. MeshRoadSegmentVector mSegments;
  414. MeshRoadBatchVector mBatches;
  415. static GFXStateBlockRef smWireframeSB;
  416. enum {
  417. Top = 0,
  418. Bottom = 1,
  419. Side = 2,
  420. SurfaceCount = 3
  421. };
  422. GFXVertexBufferHandle<GFXVertexPNTT> mVB[SurfaceCount];
  423. GFXPrimitiveBufferHandle mPB[SurfaceCount];
  424. String mMaterialName[SurfaceCount];
  425. SimObjectPtr<Material> mMaterial[SurfaceCount];
  426. BaseMatInstance *mMatInst[SurfaceCount];
  427. U32 mVertCount[SurfaceCount];
  428. U32 mTriangleCount[SurfaceCount];
  429. // Fields.
  430. F32 mTextureLength;
  431. F32 mBreakAngle;
  432. S32 mWidthSubdivisions;
  433. // Collision and Physics.
  434. Convex* mConvexList;
  435. Vector<MeshRoadConvex*> mDebugConvex;
  436. PhysicsBody *mPhysicsRep;
  437. };
  438. #endif // _MESHROAD_H_