convexShape.h

Engine/source/T3D/convexShape.h

More...

Classes:

Public Typedefs

ConvexVert
VertexType 

Public Functions

Detailed Description

Public Typedefs

typedef ConvexVert VertexType 

Public Functions

GFXDeclareVertexFormat(ConvexVert )

  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 _CONVEXSHAPE_H_
 25#define _CONVEXSHAPE_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 _CONVEX_H_
 37#include "collision/convex.h"
 38#endif
 39
 40class ConvexShape;
 41
 42// Crap name, but whatcha gonna do.
 43class ConvexShapeCollisionConvex : public Convex
 44{
 45   typedef Convex Parent;
 46   friend class ConvexShape;
 47
 48protected:
 49
 50   ConvexShape *pShape;
 51
 52public:
 53
 54   ConvexShapeCollisionConvex() { pShape = NULL; mType = ConvexShapeCollisionConvexType; }
 55
 56   ConvexShapeCollisionConvex( const ConvexShapeCollisionConvex& cv ) {
 57      mType      = ConvexShapeCollisionConvexType;
 58      mObject    = cv.mObject;
 59      pShape     = cv.pShape;    
 60   }
 61
 62   Point3F support(const VectorF& vec) const;
 63   void getFeatures(const MatrixF& mat,const VectorF& n, ConvexFeature* cf);
 64   void getPolyList(AbstractPolyList* list);
 65};
 66
 67
 68GFXDeclareVertexFormat( ConvexVert )
 69{
 70   Point3F point;
 71   GFXVertexColor color;
 72   Point3F normal;
 73   Point3F tangent;
 74   Point2F texCoord;
 75};
 76
 77class PhysicsBody;
 78
 79// Define our vertex format here so we don't have to
 80// change it in multiple spots later
 81typedef ConvexVert VertexType;
 82
 83class ConvexShape : public SceneObject
 84{
 85   typedef SceneObject Parent;
 86   friend class GuiConvexEditorCtrl;
 87   friend class GuiConvexEditorUndoAction;
 88   friend class ConvexShapeCollisionConvex;
 89
 90public:
 91
 92   enum NetBits {
 93      TransformMask = Parent::NextFreeMask,
 94      UpdateMask = Parent::NextFreeMask << 1,
 95      NextFreeMask = Parent::NextFreeMask << 2
 96   };
 97
 98   // Declaring these structs directly within ConvexShape to prevent
 99   // the otherwise excessively deep scoping we had.
100   // eg. ConvexShape::Face::Triangle ...
101
102   struct Edge
103   {
104      U32 p0;
105      U32 p1;
106   };
107
108   struct Triangle
109   {
110      U32 p0;
111      U32 p1;
112      U32 p2;
113
114      U32 operator []( U32 index ) const
115      {
116         AssertFatal( index >= 0 && index <= 2, "index out of range" );
117         return *( (&p0) + index );
118      }
119   };
120
121   struct Face
122   {
123      Vector< Edge> edges;
124      Vector< U32> points;
125      Vector< U32> winding;
126      Vector< Point2F> texcoords;
127      Vector< Triangle> triangles;       
128      Point3F tangent;
129      Point3F normal;
130      Point3F centroid;
131      F32 area;
132      S32 id;
133   }; 
134
135   struct surfaceMaterial
136   {
137      // The name of the Material we will use for rendering
138      String            materialName;
139
140      // The actual Material instance
141      BaseMatInstance*  materialInst;
142
143      surfaceMaterial()
144      {
145         materialName = "";
146         materialInst = NULL;
147      }
148   };
149
150   struct surfaceUV
151   {
152      S32 matID;
153      Point2F offset;
154      Point2F scale;
155      float   zRot;
156      bool horzFlip;
157      bool vertFlip;
158
159      surfaceUV() : matID(0), offset(Point2F(0.0f, 0.0f)), scale(Point2F(1.0f, 1.0f)), zRot(0.0f), horzFlip(false), vertFlip(false) {}
160   };
161
162   struct surfaceBuffers
163   {
164      // The GFX vertex and primitive buffers
165      GFXVertexBufferHandle< VertexType> mVertexBuffer;
166      GFXPrimitiveBufferHandle            mPrimitiveBuffer;
167
168      U32 mVertCount;
169      U32 mPrimCount;
170   };
171
172   struct Geometry
173   {  
174      void generate(const Vector< PlaneF> &planes, const Vector< Point3F> &tangents, const Vector< surfaceMaterial> surfaceTextures, const Vector< Point2F> texOffset, const Vector< Point2F> texScale, const Vector< bool> horzFlip, const Vector< bool> vertFlip);
175
176      Vector< Point3F> points;      
177      Vector< Face> faces;
178   };
179
180   static bool smRenderEdges;   
181
182   // To prevent bitpack overflows.
183   // This is only indirectly enforced by trucation when serializing.
184   static const S32 smMaxSurfaces = 100;
185
186public:
187   
188   ConvexShape();
189   virtual ~ConvexShape();
190
191   DECLARE_CONOBJECT( ConvexShape );
192
193   // ConsoleObject
194   static void initPersistFields();
195
196   // SimObject 
197   virtual void inspectPostApply();
198   virtual bool onAdd();
199   virtual void onRemove();
200   virtual void writeFields(Stream &stream, U32 tabStop);
201   virtual bool writeField( StringTableEntry fieldname, const char *value );
202
203   // NetObject
204   virtual U32 packUpdate( NetConnection *conn, U32 mask, BitStream *stream );
205   virtual void unpackUpdate( NetConnection *conn, BitStream *stream );
206
207   // SceneObject
208   virtual void onScaleChanged();
209   virtual void setTransform( const MatrixF &mat );   
210   virtual void prepRenderImage( SceneRenderState *state );
211   virtual void buildConvex( const Box3F &box, Convex *convex );
212   virtual bool buildPolyList( PolyListContext context, AbstractPolyList *polyList, const Box3F &box, const SphereF &sphere );
213   virtual bool buildExportPolyList(ColladaUtils::ExportData* exportData, const Box3F &box, const SphereF &);
214   virtual bool castRay( const Point3F &start, const Point3F &end, RayInfo *info );
215   virtual bool collideBox( const Point3F &start, const Point3F &end, RayInfo *info );
216
217
218   void updateBounds( bool recenter );
219   void recenter();
220
221   /// Geometry access.
222   /// @{
223         
224      MatrixF getSurfaceWorldMat( S32 faceid, bool scaled = false ) const;
225      void cullEmptyPlanes( Vector< U32> *removedPlanes );
226      void exportToCollada();
227      void resizePlanes( const Point3F &size );
228      void getSurfaceLineList( S32 surfId, Vector< Point3F> &lineList );
229      Geometry& getGeometry() { return mGeometry; }
230      Vector<MatrixF>& getSurfaces() { return mSurfaces; }
231      void getSurfaceTriangles( S32 surfId, Vector< Point3F> *outPoints, Vector< Point2F> *outCoords, bool worldSpace );
232
233   /// @}
234
235   /// Geometry Visualization.
236   /// @{
237
238      void renderFaceEdges( S32 faceid, const ColorI &color = ColorI::WHITE, F32 lineWidth = 1.0f );
239
240   /// @}
241
242      String getMaterialName() { return mMaterialName; }
243
244protected:
245
246   void _updateMaterial();
247   void _updateGeometry( bool updateCollision = false );
248   void _updateCollision();
249   void _export( OptimizedPolyList *plist, const Box3F &box, const SphereF &sphere );
250
251   void _renderDebug( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *mat );
252
253   static S32 QSORT_CALLBACK _comparePlaneDist( const void *a, const void *b );
254
255   static bool protectedSetSurface( void *object, const char *index, const char *data );
256
257   static bool protectedSetSurfaceTexture( void *object, const char *index, const char *data );
258   static bool protectedSetSurfaceUV(void *object, const char *index, const char *data);
259  
260protected:
261   
262   // The name of the Material we will use for rendering
263   String            mMaterialName;
264
265   // The actual Material instance
266   BaseMatInstance*  mMaterialInst;
267
268   // The GFX vertex and primitive buffers
269   /*GFXVertexBufferHandle< VertexType > mVertexBuffer;
270   GFXPrimitiveBufferHandle            mPrimitiveBuffer;
271
272   U32 mVertCount;
273   U32 mPrimCount;*/
274
275   Geometry mGeometry;  
276
277   Vector< PlaneF> mPlanes;
278
279   Vector< MatrixF> mSurfaces;
280
281   Vector< Point3F> mFaceCenters;
282
283   //this is mostly for storage purposes, so we can save the texture mods
284   Vector< surfaceMaterial> mSurfaceTextures;
285   Vector< surfaceUV> mSurfaceUVs;
286   Vector< surfaceBuffers> mSurfaceBuffers;
287
288   Convex *mConvexList;
289
290   PhysicsBody *mPhysicsRep; 
291
292   /// Geometry visualization
293   /// @{      
294
295      F32 mNormalLength;   
296
297   /// @}
298
299};
300
301#endif // _CONVEXSHAPE_H_
302