Torque3D Documentation / _generateds / abstractPolyList.h

abstractPolyList.h

Engine/source/collision/abstractPolyList.h

More...

Classes:

class

A polygon filtering interface.

Detailed Description

  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 _ABSTRACTPOLYLIST_H_
 25#define _ABSTRACTPOLYLIST_H_
 26
 27#ifndef _MMATH_H_
 28#include "math/mMath.h"
 29#endif
 30#ifndef _MPLANETRANSFORMER_H_
 31#include "math/mPlaneTransformer.h"
 32#endif
 33#ifndef _TVECTOR_H_
 34#include "core/util/tVector.h"
 35#endif
 36
 37class SceneObject;
 38class BaseMatInstance;
 39
 40
 41/// A polygon filtering interface.
 42///
 43/// The various AbstractPolyList subclasses are used in Torque as an interface to
 44/// handle spatial queries. SceneObject::buildPolyList() takes an implementor of
 45/// AbstractPolyList (such as ConcretePolyList, ClippedPolyList, etc.) and a
 46/// bounding volume. The function runs geometry data from all the objects in the
 47/// bounding volume through the passed PolyList.
 48///
 49/// This interface only provides a method to get data INTO your implementation. Different
 50/// subclasses provide different interfaces to get data back out, depending on their
 51/// specific quirks.
 52///
 53/// The physics engine now uses convex hulls for collision detection.
 54///
 55/// @see Convex
 56class AbstractPolyList
 57{
 58protected:
 59   // User set state
 60   SceneObject* mCurrObject;
 61
 62   MatrixF  mBaseMatrix;               // Base transform
 63   MatrixF  mTransformMatrix;          // Current object transform
 64   MatrixF  mMatrix;                   // Base * current transform
 65   Point3F  mScale;
 66
 67   PlaneTransformer mPlaneTransformer;
 68
 69   bool     mInterestNormalRegistered;
 70   Point3F  mInterestNormal;
 71
 72public:
 73   AbstractPolyList();
 74   virtual ~AbstractPolyList();
 75
 76   /// @name Common Interface
 77   /// @{
 78   void setBaseTransform(const MatrixF& mat);
 79
 80   /// Sets the transform applying to the current stream of
 81   /// vertices.
 82   ///
 83   /// @param  mat   Transformation of the object. (in)
 84   /// @param  scale Scaling of the object. (in)
 85   void setTransform(const MatrixF* mat, const Point3F& scale);
 86
 87   /// Gets the transform applying to the current stream of
 88   /// vertices.
 89   ///
 90   /// @param  mat   Transformation of the object. (out)
 91   /// @param  scale Scaling of the object. (out)
 92   void getTransform(MatrixF* mat, Point3F * scale);
 93
 94   /// This is called by the object which is currently feeding us
 95   /// vertices, to tell us who it is.
 96   void setObject(SceneObject*);
 97
 98   /// Add a box via the query interface (below). This wraps some calls
 99   /// to addPoint and addPlane.
100   void addBox(const Box3F &box, BaseMatInstance* material = NULL);
101
102   void doConstruct();
103   /// @}
104
105   /// @name Query Interface
106   ///
107   /// It is through this interface that geometry data is fed to the
108   /// PolyList. The order of calls are:
109   ///   - begin()
110   ///   - One or more calls to vertex() and one call to plane(), in any order.
111   ///   - end()
112   ///
113   /// @code
114   ///   // Example code that adds data to a PolyList.
115   ///   // See AbstractPolyList::addBox() for the function this was taken from.
116   ///
117   ///   // First, we add points... (note that we use base to track the start of adding.)
118   ///   U32 base = addPoint(pos);
119   ///   pos.y += dy; addPoint(pos);
120   ///   pos.x += dx; addPoint(pos);
121   ///   pos.y -= dy; addPoint(pos);
122   ///   pos.z += dz; addPoint(pos);
123   ///   pos.x -= dx; addPoint(pos);
124   ///   pos.y += dy; addPoint(pos);
125   ///   pos.x += dx; addPoint(pos);
126   ///
127   ///   // Now we add our surfaces. (there are six, as we are adding a cube here)
128   ///   for (S32 i = 0; i < 6; i++) {
129   ///      // Begin a surface
130   ///      begin(0,i);
131   ///
132   ///      // Calculate the vertex ids; we have a lookup table to tell us offset from base.
133   ///      // In your own code, you might use a different method.
134   ///      S32 v1 = base + PolyFace[i][0];
135   ///      S32 v2 = base + PolyFace[i][1];
136   ///      S32 v3 = base + PolyFace[i][2];
137   ///      S32 v4 = base + PolyFace[i][3];
138   ///
139   ///      // Reference the four vertices that make up this surface.
140   ///      vertex(v1);
141   ///      vertex(v2);
142   ///      vertex(v3);
143   ///      vertex(v4);
144   ///
145   ///      // Indicate the plane of this surface.
146   ///      plane(v1,v2,v3);
147   ///
148   ///      // End the surface.
149   ///      end();
150   ///   }
151   /// @endcode
152   /// @{
153
154   /// Are we empty of data?
155   virtual bool isEmpty() const = 0;
156
157   /// Adds a point to the poly list, and returns
158   /// an ID number for that point.
159   virtual U32  addPoint(const Point3F& p) = 0;
160
161   /// Adds a point and normal to the poly list, and returns
162   /// an ID number for them.  Normals are ignored for polylists
163   /// that do not support them.
164   virtual U32  addPointAndNormal(const Point3F& p, const Point3F& normal) { return addPoint( p ); }
165
166   /// Adds a plane to the poly list, and returns
167   /// an ID number for that point.
168   virtual U32  addPlane(const PlaneF& plane) = 0;
169
170   /// Start a surface.
171   ///
172   /// @param  material    A material ID for this surface.
173   /// @param  surfaceKey  A key value to associate with this surface.
174   virtual void begin(BaseMatInstance* material,U32 surfaceKey) = 0;
175
176   /// Indicate the plane of the surface.
177   virtual void plane(U32 v1,U32 v2,U32 v3) = 0;
178   /// Indicate the plane of the surface.
179   virtual void plane(const PlaneF& p) = 0;
180   /// Indicate the plane of the surface.
181   virtual void plane(const U32 index) = 0;
182
183   /// Reference a vertex which is in this surface.
184   virtual void vertex(U32 vi) = 0;
185
186   /// Mark the end of a surface.
187   virtual void end() = 0;
188
189   /// Return list transform and bounds in list space.
190   ///
191   /// @returns False if no data is available.
192   virtual bool getMapping(MatrixF *, Box3F *);
193   /// @}
194
195   /// @name Interest
196   ///
197   /// This is a mechanism to let you specify interest in a specific normal.
198   /// If you set a normal you're interested in, then any planes facing "away"
199   /// from that normal are culled from the results.
200   ///
201   /// This is handy if you're using this to do a physics check, as you're not
202   /// interested in polygons facing away from you (since you don't collide with
203   /// the backsides/insides of things).
204   /// @{
205
206   void setInterestNormal(const Point3F& normal);
207   void clearInterestNormal()                        { mInterestNormalRegistered = false; }
208
209
210   virtual bool isInterestedInPlane(const PlaneF& plane);
211   virtual bool isInterestedInPlane(const U32 index);
212
213   /// @}
214
215  protected:
216   /// A helper function to convert a plane index to a PlaneF structure.
217   virtual const PlaneF& getIndexedPlane(const U32 index) = 0;
218};
219
220
221inline AbstractPolyList::AbstractPolyList()
222{
223   doConstruct();
224}
225
226inline void AbstractPolyList::doConstruct()
227{
228   mCurrObject = NULL;
229   mBaseMatrix.identity();
230   mMatrix.identity();
231   mScale.set(1, 1, 1);
232
233   mPlaneTransformer.setIdentity();
234
235   mInterestNormalRegistered = false;
236}
237
238inline void AbstractPolyList::setBaseTransform(const MatrixF& mat)
239{
240   mBaseMatrix = mat;
241}
242
243inline void AbstractPolyList::setTransform(const MatrixF* mat, const Point3F& scale)
244{
245   mMatrix = mBaseMatrix;
246   mTransformMatrix = *mat;
247   mMatrix.mul(*mat);
248   mScale = scale;
249
250   mPlaneTransformer.set(mMatrix, mScale);
251}
252
253inline void AbstractPolyList::getTransform(MatrixF* mat, Point3F * scale)
254{
255   *mat   = mTransformMatrix;
256   *scale = mScale;
257}
258
259inline void AbstractPolyList::setObject(SceneObject* obj)
260{
261   mCurrObject = obj;
262}
263
264
265#endif // _ABSTRACTPOLYLIST_H_
266