meshRoad.h 15 KB

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