Torque3D Documentation / _generateds / primBuilder.cpp

primBuilder.cpp

Engine/source/gfx/primBuilder.cpp

More...

Namespaces:

namespace

Primitive Builder.

Public Defines

Detailed Description

Public Defines

INIT_VERTEX_SIZE(x) 
VERTEX_BOUNDS_CHECK() 
VERTEX_SIZE_CHECK() 
  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#include "primBuilder.h"
 24#include "gfxDevice.h"
 25#include "console/console.h"
 26
 27
 28//*****************************************************************************
 29// Primitive Builder
 30//*****************************************************************************
 31namespace PrimBuild
 32{
 33Vector<GFXVertexPCT> mTempVertBuff;
 34GFXVertexBufferHandle<GFXVertexPCT> mVertBuff;
 35GFXPrimitiveType  mType;
 36U32               mCurVertIndex;
 37ColorI            mCurColor( 255, 255, 255 );
 38Point2F           mCurTexCoord;
 39const ColorI      _colWhite( 255, 255, 255, 255 );
 40
 41#ifdef TORQUE_DEBUG
 42U32 mMaxVerts;
 43
 44#define INIT_VERTEX_SIZE(x) mMaxVerts = x;
 45#define VERTEX_BOUNDS_CHECK() AssertFatal( mCurVertIndex < mMaxVerts, "PrimBuilder encountered an out of bounds vertex! Break and debug!" );
 46
 47// This next check shouldn't really be used a lot unless you are tracking down
 48// a specific bug. -pw
 49#define VERTEX_SIZE_CHECK() AssertFatal( mCurVertIndex <= mMaxVerts, "PrimBuilder allocated more verts than you used! Break and debug or rendering artifacts could occur." );
 50
 51#else
 52
 53#define INIT_VERTEX_SIZE(x)
 54#define VERTEX_BOUNDS_CHECK()
 55#define VERTEX_SIZE_CHECK()
 56
 57#endif
 58
 59//-----------------------------------------------------------------------------
 60// begin
 61//-----------------------------------------------------------------------------
 62void begin( GFXPrimitiveType type, U32 maxVerts )
 63{
 64   AssertFatal( type >= GFXPT_FIRST && type < GFXPT_COUNT, "PrimBuilder::end() - Bad primitive type!" );
 65
 66   mType = type;
 67   mCurVertIndex = 0;
 68   INIT_VERTEX_SIZE( maxVerts );
 69   mTempVertBuff.setSize( maxVerts );
 70}
 71
 72void beginToBuffer( GFXPrimitiveType type, U32 maxVerts )
 73{
 74   AssertFatal( type >= GFXPT_FIRST && type < GFXPT_COUNT, "PrimBuilder::end() - Bad primitive type!" );
 75
 76   mType = type;
 77   mCurVertIndex = 0;
 78   INIT_VERTEX_SIZE( maxVerts );
 79   mTempVertBuff.setSize( maxVerts );
 80}
 81
 82//-----------------------------------------------------------------------------
 83// end
 84//-----------------------------------------------------------------------------
 85GFXVertexBuffer * endToBuffer( U32 &numPrims )
 86{
 87   mVertBuff.set(GFX, mTempVertBuff.size(), GFXBufferTypeVolatile);
 88   GFXVertexPCT *verts = mVertBuff.lock();
 89   dMemcpy( verts, mTempVertBuff.address(), mTempVertBuff.size() * sizeof(GFXVertexPCT) );
 90   mVertBuff.unlock();
 91
 92   VERTEX_SIZE_CHECK();
 93
 94   switch( mType )
 95   {
 96      case GFXPointList:
 97      {
 98         numPrims = mCurVertIndex;
 99         break;
100      }
101
102      case GFXLineList:
103      {
104         numPrims = mCurVertIndex / 2;
105         break;
106      }
107
108      case GFXLineStrip:
109      {
110         numPrims = mCurVertIndex - 1;
111         break;
112      }
113
114      case GFXTriangleList:
115      {
116         numPrims = mCurVertIndex / 3;
117         break;
118      }
119
120      case GFXTriangleStrip:
121      {
122         numPrims = mCurVertIndex - 2;
123         break;
124      }
125      case GFXPT_COUNT:
126         // handle warning
127         break;
128   }
129
130   return mVertBuff;
131}
132
133void end( bool useGenericShaders )
134{
135   if ( mCurVertIndex == 0 ) 
136      return; 
137
138   VERTEX_SIZE_CHECK();
139
140   U32 vertStride = 1;
141   U32 stripStart = 0;
142
143   AssertFatal( mType >= GFXPT_FIRST && mType < GFXPT_COUNT, "PrimBuilder::end() - Bad primitive type!" );
144               
145   switch( mType )
146   {
147      default:
148      case GFXPointList:
149      {
150         vertStride = 1;
151         break;
152      }
153
154      case GFXLineList:
155      {
156         vertStride = 2;
157         break;
158      }
159
160      case GFXTriangleList:
161      {
162         vertStride = 3;
163         break;
164      }
165
166      case GFXLineStrip:
167      {
168         stripStart = 1;
169         vertStride = 1;
170         break;
171      }
172
173      case GFXTriangleStrip:
174      {
175         stripStart = 2;
176         vertStride = 1;
177         break;
178      }
179   }
180
181    if ( useGenericShaders )
182    {
183        GFXStateBlock *currentBlock = GFX->getStateBlock();
184        if (currentBlock && currentBlock->getDesc().samplersDefined)
185        {
186            if (currentBlock->getDesc().vertexColorEnable)
187                GFX->setupGenericShaders( GFXDevice::GSModColorTexture );
188            else
189                GFX->setupGenericShaders( GFXDevice::GSTexture );
190        }
191        else
192            GFX->setupGenericShaders( GFXDevice::GSColor );
193    }
194
195   const GFXVertexPCT *srcVerts = mTempVertBuff.address();
196   U32 numVerts = mCurVertIndex;
197   
198   // Make sure we don't have a dirty prim buffer left.
199   GFX->setPrimitiveBuffer( NULL );
200
201   if ( stripStart > 0 )
202   {
203      // TODO: Fix this to allow > GFX_MAX_DYNAMIC_VERTS!
204
205      U32 copyVerts = getMin( (U32)GFX_MAX_DYNAMIC_VERTS, numVerts );
206      mVertBuff.set( GFX, copyVerts, GFXBufferTypeVolatile );
207
208      GFXVertexPCT *verts = mVertBuff.lock();
209      dMemcpy( verts, srcVerts, copyVerts * sizeof( GFXVertexPCT ) );
210      mVertBuff.unlock();
211
212      U32 numPrims = ( copyVerts / vertStride ) - stripStart;
213      GFX->setVertexBuffer( mVertBuff );
214      GFX->drawPrimitive( mType, 0, numPrims );
215   }
216   else
217   {
218      while ( numVerts > 0 )
219      {
220         U32 copyVerts = getMin( (U32)GFX_MAX_DYNAMIC_VERTS, numVerts );
221         copyVerts -= copyVerts % vertStride;
222
223         mVertBuff.set( GFX, copyVerts, GFXBufferTypeVolatile );
224
225         GFXVertexPCT *verts = mVertBuff.lock();
226         dMemcpy( verts, srcVerts, copyVerts * sizeof( GFXVertexPCT ) );
227         mVertBuff.unlock();
228
229         U32 numPrims = copyVerts / vertStride;
230         GFX->setVertexBuffer( mVertBuff );
231         GFX->drawPrimitive( mType, 0, numPrims );
232
233         srcVerts += copyVerts;
234         numVerts -= copyVerts;
235      }
236   }
237}
238
239//-----------------------------------------------------------------------------
240// vertex2f
241//-----------------------------------------------------------------------------
242void vertex2f( F32 x, F32 y )
243{
244   VERTEX_BOUNDS_CHECK();
245   GFXVertexPCT *vert = &mTempVertBuff[mCurVertIndex++];
246
247   vert->point.x = x;
248   vert->point.y = y;
249   vert->point.z = 0.0;
250   vert->color = mCurColor;
251   vert->texCoord = mCurTexCoord;
252}
253
254//-----------------------------------------------------------------------------
255// vertex3f
256//-----------------------------------------------------------------------------
257void vertex3f( F32 x, F32 y, F32 z )
258{
259   VERTEX_BOUNDS_CHECK();
260   GFXVertexPCT *vert = &mTempVertBuff[mCurVertIndex++];
261
262   vert->point.x = x;
263   vert->point.y = y;
264   vert->point.z = z;
265   vert->color = mCurColor;
266   vert->texCoord = mCurTexCoord;
267}
268
269//-----------------------------------------------------------------------------
270// vertex3fv
271//-----------------------------------------------------------------------------
272void vertex3fv( const F32 *data )
273{
274   VERTEX_BOUNDS_CHECK();
275   GFXVertexPCT *vert = &mTempVertBuff[mCurVertIndex++];
276
277   vert->point.set( data[0], data[1], data[2] );
278   vert->color = mCurColor;
279   vert->texCoord = mCurTexCoord;
280}
281
282//-----------------------------------------------------------------------------
283// vertex2fv
284//-----------------------------------------------------------------------------
285void vertex2fv( const F32 *data )
286{
287   VERTEX_BOUNDS_CHECK();
288   GFXVertexPCT *vert = &mTempVertBuff[mCurVertIndex++];
289
290   vert->point.set( data[0], data[1], 0.f );
291   vert->color = mCurColor;
292   vert->texCoord = mCurTexCoord;
293}
294
295
296
297//-----------------------------------------------------------------------------
298// color
299//-----------------------------------------------------------------------------
300void color( const ColorI &inColor )
301{
302   mCurColor = inColor;
303}
304
305void color( const LinearColorF &inColor )
306{
307   mCurColor = LinearColorF(inColor).toColorI();
308}
309
310void color3i( U8 red, U8 green, U8 blue )
311{
312   mCurColor.set( red, green, blue );
313}
314
315void color4i( U8 red, U8 green, U8 blue, U8 alpha )
316{
317   mCurColor.set( red, green, blue, alpha );
318}
319
320void color3f( F32 red, F32 green, F32 blue )
321{
322   mCurColor.set( U8( red * 255 ), U8( green * 255 ), U8( blue * 255 ) );
323}
324
325void color4f( F32 red, F32 green, F32 blue, F32 alpha )
326{
327   mCurColor.set( U8( red * 255 ), U8( green * 255 ), U8( blue * 255 ), U8( alpha * 255 ) );
328}
329
330
331//-----------------------------------------------------------------------------
332// texCoord
333//-----------------------------------------------------------------------------
334void texCoord2f( F32 x, F32 y )
335{
336   mCurTexCoord.set( x, y );
337}
338
339void shutdown()
340{
341   mVertBuff = NULL;
342}
343
344}  // namespace PrimBuild
345