decalRoad.h 7.3 KB

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