Torque3D Documentation / _generateds / recastPolyList.cpp

recastPolyList.cpp

Engine/source/navigation/recastPolyList.cpp

More...

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2013 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 "recastPolyList.h"
 25#include "platform/platform.h"
 26
 27#include "gfx/gfxDevice.h"
 28#include "gfx/primBuilder.h"
 29#include "gfx/gfxStateBlock.h"
 30
 31RecastPolyList::RecastPolyList()
 32{
 33   nverts = 0;
 34   verts = NULL;
 35   vertcap = 0;
 36
 37   ntris = 0;
 38   tris = NULL;
 39   tricap = 0;
 40   vidx = 0;
 41}
 42
 43RecastPolyList::~RecastPolyList()
 44{
 45   clear();
 46}
 47
 48void RecastPolyList::clear()
 49{
 50   nverts = 0;
 51   delete[] verts;
 52   verts = NULL;
 53   vertcap = 0;
 54
 55   ntris = 0;
 56   delete[] tris;
 57   tris = NULL;
 58   tricap = 0;
 59}
 60
 61bool RecastPolyList::isEmpty() const
 62{
 63   return getTriCount() == 0;
 64}
 65
 66U32 RecastPolyList::addPoint(const Point3F &p)
 67{
 68   // If we've reached the vertex cap, double the array size.
 69   if(nverts == vertcap)
 70   {
 71      // vertcap starts at 64, otherwise it doubles.
 72      if(vertcap == 0) vertcap = 16;
 73      else vertcap *= 2;
 74      // Allocate new vertex storage.
 75      F32 *newverts = new F32[vertcap*3];
 76
 77      dMemcpy(newverts, verts, nverts*3 * sizeof(F32));
 78      dFree(verts);
 79      verts = newverts;
 80   }
 81   Point3F v = p;
 82   v.convolve(mScale);
 83   mMatrix.mulP(v);
 84   // Insert the new vertex.
 85   verts[nverts*3] = v.x;
 86   verts[nverts*3+1] = v.z;
 87   verts[nverts*3+2] = -v.y;
 88   // Return nverts before incrementing it.
 89   return nverts++;
 90}
 91
 92U32 RecastPolyList::addPlane(const PlaneF &plane)
 93{
 94   planes.increment();
 95   mPlaneTransformer.transform(plane, planes.last());
 96   return planes.size() - 1;
 97}
 98
 99void RecastPolyList::begin(BaseMatInstance *material, U32 surfaceKey)
100{
101   vidx = 0;
102   // If we've reached the tri cap, grow the array.
103   if(ntris == tricap)
104   {
105      if(tricap == 0) tricap = 16;
106      else tricap *= 2;
107      // Allocate new vertex storage.
108      S32 *newtris = new S32[tricap*3];
109
110      dMemcpy(newtris, tris, ntris*3 * sizeof(S32));
111      dFree(tris);
112      tris = newtris;
113   }
114}
115
116void RecastPolyList::plane(U32 v1, U32 v2, U32 v3)
117{
118}
119
120void RecastPolyList::plane(const PlaneF& p)
121{
122}
123
124void RecastPolyList::plane(const U32 index)
125{
126}
127
128void RecastPolyList::vertex(U32 vi)
129{
130   if(vidx == 3)
131      return;
132   tris[ntris*3+2-vidx] = vi;
133   vidx++;
134}
135
136void RecastPolyList::end()
137{
138   ntris++;
139}
140
141U32 RecastPolyList::getVertCount() const
142{
143   return nverts;
144}
145
146const F32 *RecastPolyList::getVerts() const
147{
148   return verts;
149}
150
151U32 RecastPolyList::getTriCount() const
152{
153   return ntris;
154}
155
156const S32 *RecastPolyList::getTris() const
157{
158   return tris;
159}
160
161void RecastPolyList::renderWire() const
162{
163   GFXStateBlockDesc desc;
164   desc.setCullMode(GFXCullNone);
165   desc.setZReadWrite(false, false);
166   //desc.setBlend(true);
167   GFXStateBlockRef sb = GFX->createStateBlock(desc);
168   GFX->setStateBlock(sb);
169
170   PrimBuild::color3i(255, 0, 255);
171
172   for(U32 t = 0; t < getTriCount(); t++)
173   {
174      PrimBuild::begin(GFXLineStrip, 4);
175
176      PrimBuild::vertex3f(verts[tris[t*3]*3],   -verts[tris[t*3]*3+2],   verts[tris[t*3]*3+1]);
177      PrimBuild::vertex3f(verts[tris[t*3+1]*3], -verts[tris[t*3+1]*3+2], verts[tris[t*3+1]*3+1]);
178      PrimBuild::vertex3f(verts[tris[t*3+2]*3], -verts[tris[t*3+2]*3+2], verts[tris[t*3+2]*3+1]);
179      PrimBuild::vertex3f(verts[tris[t*3]*3],   -verts[tris[t*3]*3+2],   verts[tris[t*3]*3+1]);
180
181      PrimBuild::end();
182   }
183}
184