decalRoad.h

Engine/source/environment/decalRoad.h

More...

Classes:

Public Defines

define

Public Typedefs

RoadBatchVector 
RoadEdgeVector 
RoadNodeVector 

Detailed Description

Public Defines

MIN_METERS_PER_SEGMENT() 1.0f
StepSize_Normal() 10.0f

Public Typedefs

typedef Vector< RoadBatch > RoadBatchVector 
typedef Vector< RoadEdge > RoadEdgeVector 
typedef Vector< RoadNode > RoadNodeVector 
  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _DECALROAD_H_
 25#define _DECALROAD_H_
 26
 27#ifndef _SCENEOBJECT_H_
 28#include "scene/sceneObject.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
 40class Path;
 41class TerrainBlock;
 42struct ObjectRenderInst;
 43class Material;
 44struct DecalRoadNodeList;
 45
 46
 47class DecalRoadUpdateEvent : public SimEvent
 48{
 49   typedef SimEvent Parent;
 50public:
 51
 52   DecalRoadUpdateEvent( U32 mask, U32 ms ) { mMask = mask; mMs = ms; }
 53   virtual void process( SimObject *object );
 54
 55   U32 mMask;
 56   U32 mMs;
 57};
 58
 59
 60struct RoadNode
 61{
 62   /// The 3D position of the node.
 63   Point3F point;
 64
 65   /// The width of the road at this node.
 66   F32 width;
 67
 68   /// Alpha of the road at this node.
 69   //F32 alpha;
 70};
 71typedef Vector<RoadNode> RoadNodeVector;
 72
 73struct RoadEdge
 74{
 75   RoadEdge() 
 76   {
 77      p0.zero();
 78      p1.zero();
 79      p2.zero();
 80
 81      uvec.zero();
 82      fvec.zero();
 83      rvec.zero();
 84
 85      width = 0.0f;
 86
 87      parentNodeIdx = -1;
 88   };
 89
 90   Point3F p0;
 91   Point3F p1;
 92   Point3F p2;      
 93
 94   VectorF uvec;
 95   VectorF fvec;
 96   VectorF rvec;
 97
 98   F32 width;      
 99
100   U32 parentNodeIdx;
101};
102typedef Vector<RoadEdge> RoadEdgeVector;
103
104struct RoadBatch
105{
106   U32 startVert;
107   U32 endVert;
108
109   U32 startIndex;
110   U32 endIndex;
111
112   Box3F bounds;
113};
114typedef Vector<RoadBatch> RoadBatchVector;
115
116
117//------------------------------------------------------------------------------
118// DecalRoad Class
119//------------------------------------------------------------------------------
120class DecalRoad : public SceneObject
121{
122private:
123
124   friend class DecalRoadUpdateEvent;
125   friend class GuiRoadEditorCtrl;
126   friend class GuiRoadEditorUndoAction;
127   typedef SceneObject     Parent;
128
129protected:
130   // Internal defines, enums, structs, classes
131
132   struct Triangle
133   {
134      GFXVertexPT v0, v1, v2;
135   };
136
137   enum 
138   { 
139      DecalRoadMask        = Parent::NextFreeMask,
140      NodeMask             = Parent::NextFreeMask << 1,      
141      GenEdgesMask         = Parent::NextFreeMask << 2,
142      ReClipMask           = Parent::NextFreeMask << 3,
143      TerrainChangedMask   = Parent::NextFreeMask << 4,
144      NextFreeMask         = Parent::NextFreeMask << 5,
145   };   
146
147   #define StepSize_Normal 10.0f  
148   #define MIN_METERS_PER_SEGMENT 1.0f
149
150public:
151
152   DecalRoad();
153   ~DecalRoad();
154
155   DECLARE_CONOBJECT(DecalRoad);
156
157   // ConsoleObject
158   static void initPersistFields();
159   static void consoleInit();
160
161   // SimObject      
162   bool onAdd();
163   void onRemove();
164   void onEditorEnable();
165   void onEditorDisable();
166   void inspectPostApply();
167   void onStaticModified(const char* slotName, const char*newValue = NULL);
168   void writeFields(Stream &stream, U32 tabStop);
169   bool writeField( StringTableEntry fieldname, const char *value );
170   
171   // NetObject
172   U32 packUpdate(NetConnection *, U32, BitStream *);
173   void unpackUpdate(NetConnection *, BitStream *);   
174
175   // SceneObject
176   virtual void prepRenderImage( SceneRenderState* state );
177   virtual void setTransform( const MatrixF &mat );
178   virtual void setScale( const VectorF &scale );
179   virtual bool containsPoint( const Point3F& point ) const { return containsPoint( point, NULL ); } 
180
181   // fxRoad Public Methods
182   void scheduleUpdate( U32 updateMask );
183   void scheduleUpdate( U32 updateMask, U32 delayMs, bool restartTimer );
184   void regenerate();   
185   void setTextureLength( F32 meters );
186   void setBreakAngle( F32 degrees );
187
188   /// Insert a node anywhere in the road.
189   /// Pass idx zero to add to the front and idx U32_MAX to add to the end
190   U32 insertNode( const Point3F &pos, const F32 &width, const U32 &idx );
191
192   U32 addNode( const Point3F &pos, F32 width = 10.0f );   
193   void deleteNode( U32 idx );
194
195   void buildNodesFromList( DecalRoadNodeList* list );
196
197   bool getClosestNode( const Point3F &pos, U32 &idx );
198
199   bool containsPoint( const Point3F &worldPos, U32 *nodeIdx ) const;
200   bool castray( const Point3F &start, const Point3F &end ) const;
201   
202   Point3F getNodePosition( U32 idx );
203   void setNodePosition( U32 idx, const Point3F &pos );
204
205   F32 getNodeWidth( U32 idx );
206   void setNodeWidth( U32 idx, F32 width );   
207
208   /// Protected 'Node' Field setter that will add a node to the list.
209   static bool addNodeFromField( void *object, const char *index, const char *data );  
210
211   static SimSet* getServerSet();
212  
213protected:
214         
215   // Internal Helper Methods   
216
217   void _initMaterial();   
218   void _debugRender( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *matInst );
219
220   U32 _insertNode( const Point3F &pos, const F32 &width, const U32 &idx );
221   U32 _addNode( const Point3F &pos, F32 width );
222   void _generateEdges();
223   void _captureVerts();
224
225   bool _getTerrainHeight( Point3F &pos );
226   bool _getTerrainHeight( const Point2F &pos, F32 &height );
227   bool _getTerrainHeight( const F32 &x, const F32 &y, F32 &height );
228
229   void _onTerrainChanged( U32 type, TerrainBlock* tblock, const Point2I &min, const Point2I &max );
230
231   // static protected field set methods
232   static bool ptSetBreakAngle( void *object, const char *index, const char *data );
233   static bool ptSetTextureLength( void *object, const char *index, const char *data );
234  
235protected:
236
237   // Field Vars
238   F32 mBreakAngle;
239   U32 mSegmentsPerBatch;
240   F32 mTextureLength;
241   String mMaterialName;
242   U32 mRenderPriority;
243
244   // Static ConsoleVars for editor
245   static bool smEditorOpen;
246   static bool smWireframe;
247   static bool smShowBatches;
248   static bool smDiscardAll;   
249   static bool smShowSpline;
250   static bool smShowRoad; 
251   static S32 smUpdateDelay;
252   
253   static SimObjectPtr<SimSet> smServerDecalRoadSet;
254
255   // Other Internal Vars
256
257   RoadEdgeVector mEdges;
258   RoadNodeVector mNodes;
259   RoadBatchVector mBatches;
260   
261   bool mLoadRenderData;
262   
263   SimObjectPtr<Material> mMaterial;
264   BaseMatInstance *mMatInst;
265
266   GFXVertexBufferHandle<GFXVertexPNTBT> mVB;
267   GFXPrimitiveBufferHandle mPB;
268
269   U32 mTriangleCount;
270   U32 mVertCount;
271
272   S32 mUpdateEventId;   
273   DecalRoadUpdateEvent *mLastEvent;
274
275   Box3F mTerrainUpdateRect;
276};
277
278
279#endif // _DECALROAD_H_
280