meshRoad.h

Engine/source/environment/meshRoad.h

More...

Classes:

Public Typedefs

MeshRoadBatchVector 
MeshRoadNodeVector 
MeshRoadSegmentVector 
MeshRoadSliceVector 

Public Functions

Detailed Description

Public Typedefs

typedef Vector< MeshRoadRenderBatch > MeshRoadBatchVector 
typedef Vector< MeshRoadNode > MeshRoadNodeVector 
typedef Vector< MeshRoadSegment > MeshRoadSegmentVector 
typedef Vector< MeshRoadSlice > MeshRoadSliceVector 

Public Functions

operator*(const F32 mul, const MeshRoadSplineNode & multiplicand)

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