Torque3D Documentation / _generateds / concretePolyList.cpp

concretePolyList.cpp

Engine/source/collision/concretePolyList.cpp

More...

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#include "platform/platform.h"
 25#include "collision/concretePolyList.h"
 26
 27#include "math/mMath.h"
 28#include "console/console.h"
 29#include "gfx/gfxDevice.h"
 30#include "gfx/primBuilder.h"
 31#include "gfx/gfxStateBlock.h"
 32
 33
 34//----------------------------------------------------------------------------
 35
 36ConcretePolyList::ConcretePolyList()
 37{
 38   VECTOR_SET_ASSOCIATION(mPolyList);
 39   VECTOR_SET_ASSOCIATION(mVertexList);
 40   VECTOR_SET_ASSOCIATION(mIndexList);
 41   VECTOR_SET_ASSOCIATION(mPolyPlaneList);
 42
 43   mIndexList.reserve(100);
 44}
 45
 46ConcretePolyList::~ConcretePolyList()
 47{
 48
 49}
 50
 51
 52//----------------------------------------------------------------------------
 53void ConcretePolyList::clear()
 54{
 55   // Only clears internal data
 56   mPolyList.clear();
 57   mVertexList.clear();
 58   mIndexList.clear();
 59   mPolyPlaneList.clear();
 60}
 61
 62//----------------------------------------------------------------------------
 63
 64U32 ConcretePolyList::addPoint(const Point3F& p)
 65{
 66   mVertexList.increment();
 67   Point3F& v = mVertexList.last();
 68   v.x = p.x * mScale.x;
 69   v.y = p.y * mScale.y;
 70   v.z = p.z * mScale.z;
 71   mMatrix.mulP(v);
 72
 73   return mVertexList.size() - 1;
 74}
 75
 76
 77U32 ConcretePolyList::addPlane(const PlaneF& plane)
 78{
 79   mPolyPlaneList.increment();
 80   mPlaneTransformer.transform(plane, mPolyPlaneList.last());
 81
 82   return mPolyPlaneList.size() - 1;
 83}
 84
 85
 86//----------------------------------------------------------------------------
 87
 88void ConcretePolyList::begin(BaseMatInstance* material,U32 surfaceKey)
 89{
 90   mPolyList.increment();
 91   Poly& poly = mPolyList.last();
 92   poly.object = mCurrObject;
 93   poly.material = material;
 94   poly.vertexStart = mIndexList.size();
 95   poly.surfaceKey = surfaceKey;
 96}
 97
 98
 99//----------------------------------------------------------------------------
100
101void ConcretePolyList::plane(U32 v1,U32 v2,U32 v3)
102{
103   mPolyList.last().plane.set(mVertexList[v1],
104      mVertexList[v2],mVertexList[v3]);
105}
106
107void ConcretePolyList::plane(const PlaneF& p)
108{
109   mPlaneTransformer.transform(p, mPolyList.last().plane); 
110}
111
112void ConcretePolyList::plane(const U32 index)
113{
114   AssertFatal(index < mPolyPlaneList.size(), "Out of bounds index!");
115   mPolyList.last().plane = mPolyPlaneList[index];
116}
117
118const PlaneF& ConcretePolyList::getIndexedPlane(const U32 index)
119{
120   AssertFatal(index < mPolyPlaneList.size(), "Out of bounds index!");
121   return mPolyPlaneList[index];
122}
123
124
125//----------------------------------------------------------------------------
126
127void ConcretePolyList::vertex(U32 vi)
128{
129   mIndexList.push_back(vi);
130}
131
132
133//----------------------------------------------------------------------------
134
135bool ConcretePolyList::isEmpty() const
136{
137   return mPolyList.empty();
138}
139
140void ConcretePolyList::end()
141{
142   Poly& poly = mPolyList.last();
143   poly.vertexCount = mIndexList.size() - poly.vertexStart;
144}
145
146void ConcretePolyList::render()
147{
148   GFXStateBlockDesc solidZDisable;
149   solidZDisable.setCullMode( GFXCullNone );
150   solidZDisable.setZReadWrite( false, false );
151   GFXStateBlockRef sb = GFX->createStateBlock( solidZDisable );
152   GFX->setStateBlock( sb );
153
154   PrimBuild::color3i( 255, 0, 255 );
155
156   Poly *p;
157   Point3F *pnt;
158
159   for ( p = mPolyList.begin(); p < mPolyList.end(); p++ )
160   {
161      PrimBuild::begin( GFXLineStrip, p->vertexCount + 1 );      
162
163      for ( U32 i = 0; i < p->vertexCount; i++ )
164      {
165         pnt = &mVertexList[mIndexList[p->vertexStart + i]];
166         PrimBuild::vertex3fv( pnt );
167      }
168
169      pnt = &mVertexList[mIndexList[p->vertexStart]];
170      PrimBuild::vertex3fv( pnt );
171
172      PrimBuild::end();
173   }   
174}
175
176void ConcretePolyList::triangulate()
177{
178   PROFILE_SCOPE( ConcretePolyList_Triangulate );
179
180   // Build into a new polylist and index list.
181   //
182   // TODO: There are potential performance issues
183   // here as we're not reserving enough space for
184   // new generated triangles.
185   //
186   // We need to either over estimate and shrink or 
187   // better yet fix vector to internally grow in 
188   // large chunks.
189   //
190   PolyList polyList;
191   polyList.reserve( mPolyList.size() );
192   IndexList indexList;
193   indexList.reserve( mIndexList.size() );
194   
195   U32 j, numTriangles;
196
197   //
198   PolyList::const_iterator polyIter = mPolyList.begin();
199   for ( ; polyIter != mPolyList.end(); polyIter++ )
200   {
201      const Poly &poly = *polyIter;
202
203      // How many triangles in this poly?
204      numTriangles = poly.vertexCount - 2;        
205
206      // Build out the triangles.
207      for ( j = 0; j < numTriangles; j++ )
208      {
209         polyList.increment();
210         
211         Poly &triangle = polyList.last();
212         triangle = poly;
213         triangle.vertexCount = 3;
214         triangle.vertexStart = indexList.size();
215
216         indexList.push_back( mIndexList[ poly.vertexStart ] );
217         indexList.push_back( mIndexList[ poly.vertexStart + 1 + j ] );
218         indexList.push_back( mIndexList[ poly.vertexStart + 2 + j ] );
219      }
220   } 
221
222   mPolyList = polyList;
223   mIndexList = indexList;
224}
225