meshRoad.h 18 KB

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