decalRoad.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 _DECALROAD_H_
  23. #define _DECALROAD_H_
  24. #ifndef _SCENEOBJECT_H_
  25. #include "scene/sceneObject.h"
  26. #endif
  27. #ifndef _GFXVERTEXBUFFER_H_
  28. #include "gfx/gfxVertexBuffer.h"
  29. #endif
  30. #ifndef _GFXPRIMITIVEBUFFER_H_
  31. #include "gfx/gfxPrimitiveBuffer.h"
  32. #endif
  33. #ifndef _CLIPPEDPOLYLIST_H_
  34. #include "collision/clippedPolyList.h"
  35. #endif
  36. #include "T3D/assets/MaterialAsset.h"
  37. class Path;
  38. class TerrainBlock;
  39. struct ObjectRenderInst;
  40. class Material;
  41. struct DecalRoadNodeList;
  42. class DecalRoadUpdateEvent : public SimEvent
  43. {
  44. typedef SimEvent Parent;
  45. public:
  46. DecalRoadUpdateEvent( U32 mask, U32 ms ) { mMask = mask; mMs = ms; }
  47. virtual void process( SimObject *object );
  48. U32 mMask;
  49. U32 mMs;
  50. };
  51. struct RoadNode
  52. {
  53. /// The 3D position of the node.
  54. Point3F point;
  55. /// The width of the road at this node.
  56. F32 width;
  57. /// Alpha of the road at this node.
  58. //F32 alpha;
  59. };
  60. typedef Vector<RoadNode> RoadNodeVector;
  61. struct RoadEdge
  62. {
  63. RoadEdge()
  64. {
  65. p0.zero();
  66. p1.zero();
  67. p2.zero();
  68. uvec.zero();
  69. fvec.zero();
  70. rvec.zero();
  71. width = 0.0f;
  72. parentNodeIdx = -1;
  73. };
  74. Point3F p0;
  75. Point3F p1;
  76. Point3F p2;
  77. VectorF uvec;
  78. VectorF fvec;
  79. VectorF rvec;
  80. F32 width;
  81. U32 parentNodeIdx;
  82. };
  83. typedef Vector<RoadEdge> RoadEdgeVector;
  84. struct RoadBatch
  85. {
  86. U32 startVert;
  87. U32 endVert;
  88. U32 startIndex;
  89. U32 endIndex;
  90. Box3F bounds;
  91. };
  92. typedef Vector<RoadBatch> RoadBatchVector;
  93. //------------------------------------------------------------------------------
  94. // DecalRoad Class
  95. //------------------------------------------------------------------------------
  96. class DecalRoad : public SceneObject
  97. {
  98. private:
  99. friend class DecalRoadUpdateEvent;
  100. friend class GuiRoadEditorCtrl;
  101. friend class GuiRoadEditorUndoAction;
  102. typedef SceneObject Parent;
  103. protected:
  104. // Internal defines, enums, structs, classes
  105. struct Triangle
  106. {
  107. GFXVertexPT v0, v1, v2;
  108. };
  109. enum
  110. {
  111. DecalRoadMask = Parent::NextFreeMask,
  112. NodeMask = Parent::NextFreeMask << 1,
  113. GenEdgesMask = Parent::NextFreeMask << 2,
  114. ReClipMask = Parent::NextFreeMask << 3,
  115. TerrainChangedMask = Parent::NextFreeMask << 4,
  116. NextFreeMask = Parent::NextFreeMask << 5,
  117. };
  118. #define StepSize_Normal 10.0f
  119. #define MIN_METERS_PER_SEGMENT 1.0f
  120. public:
  121. DecalRoad();
  122. ~DecalRoad();
  123. DECLARE_CONOBJECT(DecalRoad);
  124. // ConsoleObject
  125. static void initPersistFields();
  126. static void consoleInit();
  127. // SimObject
  128. bool onAdd();
  129. void onRemove();
  130. void onEditorEnable();
  131. void onEditorDisable();
  132. void inspectPostApply();
  133. void onStaticModified(const char* slotName, const char*newValue = NULL);
  134. void writeFields(Stream &stream, U32 tabStop);
  135. bool writeField( StringTableEntry fieldname, const char *value );
  136. // NetObject
  137. U32 packUpdate(NetConnection *, U32, BitStream *);
  138. void unpackUpdate(NetConnection *, BitStream *);
  139. // SceneObject
  140. virtual void prepRenderImage( SceneRenderState* state );
  141. virtual void setTransform( const MatrixF &mat );
  142. virtual void setScale( const VectorF &scale );
  143. virtual bool containsPoint( const Point3F& point ) const { return containsPoint( point, NULL ); }
  144. // fxRoad Public Methods
  145. void scheduleUpdate( U32 updateMask );
  146. void scheduleUpdate( U32 updateMask, U32 delayMs, bool restartTimer );
  147. void regenerate();
  148. void setTextureLength( F32 meters );
  149. void setBreakAngle( F32 degrees );
  150. /// Insert a node anywhere in the road.
  151. /// Pass idx zero to add to the front and idx U32_MAX to add to the end
  152. U32 insertNode( const Point3F &pos, const F32 &width, const U32 &idx );
  153. U32 addNode( const Point3F &pos, F32 width = 10.0f );
  154. void deleteNode( U32 idx );
  155. void buildNodesFromList( DecalRoadNodeList* list );
  156. bool getClosestNode( const Point3F &pos, U32 &idx );
  157. bool containsPoint( const Point3F &worldPos, U32 *nodeIdx ) const;
  158. bool castray( const Point3F &start, const Point3F &end ) const;
  159. Point3F getNodePosition( U32 idx );
  160. void setNodePosition( U32 idx, const Point3F &pos );
  161. F32 getNodeWidth( U32 idx );
  162. void setNodeWidth( U32 idx, F32 width );
  163. /// Protected 'Node' Field setter that will add a node to the list.
  164. static bool addNodeFromField( void *object, const char *index, const char *data );
  165. static SimSet* getServerSet();
  166. protected:
  167. // Internal Helper Methods
  168. void _initMaterial();
  169. void _debugRender( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *matInst );
  170. U32 _insertNode( const Point3F &pos, const F32 &width, const U32 &idx );
  171. U32 _addNode( const Point3F &pos, F32 width );
  172. void _generateEdges();
  173. void _captureVerts();
  174. bool _getTerrainHeight( Point3F &pos );
  175. bool _getTerrainHeight( const Point2F &pos, F32 &height );
  176. bool _getTerrainHeight( const F32 &x, const F32 &y, F32 &height );
  177. void _onTerrainChanged( U32 type, TerrainBlock* tblock, const Point2I &min, const Point2I &max );
  178. // static protected field set methods
  179. static bool ptSetBreakAngle( void *object, const char *index, const char *data );
  180. static bool ptSetTextureLength( void *object, const char *index, const char *data );
  181. protected:
  182. // Field Vars
  183. F32 mBreakAngle;
  184. U32 mSegmentsPerBatch;
  185. F32 mTextureLength;
  186. BaseMatInstance* mMaterialInst;
  187. DECLARE_MATERIALASSET(DecalRoad, Material);
  188. DECLARE_ASSET_NET_SETGET(DecalRoad, Material, DecalRoadMask);
  189. U32 mRenderPriority;
  190. // Static ConsoleVars for editor
  191. static bool smEditorOpen;
  192. static bool smWireframe;
  193. static bool smShowBatches;
  194. static bool smDiscardAll;
  195. static bool smShowSpline;
  196. static bool smShowRoad;
  197. static S32 smUpdateDelay;
  198. static SimObjectPtr<SimSet> smServerDecalRoadSet;
  199. // Other Internal Vars
  200. RoadEdgeVector mEdges;
  201. RoadNodeVector mNodes;
  202. RoadBatchVector mBatches;
  203. bool mLoadRenderData;
  204. GFXVertexBufferHandle<GFXVertexPNTBT> mVB;
  205. GFXPrimitiveBufferHandle mPB;
  206. U32 mTriangleCount;
  207. U32 mVertCount;
  208. S32 mUpdateEventId;
  209. DecalRoadUpdateEvent *mLastEvent;
  210. Box3F mTerrainUpdateRect;
  211. };
  212. #endif // _DECALROAD_H_