Torque3D Documentation / _generateds / renderTerrainMgr.cpp

renderTerrainMgr.cpp

Engine/source/renderInstance/renderTerrainMgr.cpp

More...

Public Functions

ConsoleDocClass(RenderTerrainMgr , "@brief A <a href="/coding/file/editortool_8cpp/#editortool_8cpp_1a4cb041169a32ea3d4cacadbb955e06b4">render</a> bin <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> terrain mesh <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">rendering.\n\n</a>" "This bin renders terrain <a href="/coding/file/editortool_8cpp/#editortool_8cpp_1a4cb041169a32ea3d4cacadbb955e06b4">render</a> instances from <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> TerrainBlock. " "Normally <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> mesh would <a href="/coding/file/editortool_8cpp/#editortool_8cpp_1a4cb041169a32ea3d4cacadbb955e06b4">render</a> via the RenderMeshMgr, but terrain uses " "<a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> <a href="/coding/class/classterrainmaterial/">TerrainMaterial</a> designed <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> multi-layered surfaces which this " "bin can <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">processs.\n\n</a>" " @ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">RenderBin\n</a>" )

Detailed Description

Public Functions

ConsoleDocClass(RenderTerrainMgr , "@brief A <a href="/coding/file/editortool_8cpp/#editortool_8cpp_1a4cb041169a32ea3d4cacadbb955e06b4">render</a> bin <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> terrain mesh <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">rendering.\n\n</a>" "This bin renders terrain <a href="/coding/file/editortool_8cpp/#editortool_8cpp_1a4cb041169a32ea3d4cacadbb955e06b4">render</a> instances from <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> TerrainBlock. " "Normally <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> mesh would <a href="/coding/file/editortool_8cpp/#editortool_8cpp_1a4cb041169a32ea3d4cacadbb955e06b4">render</a> via the RenderMeshMgr, but terrain uses " "<a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> <a href="/coding/class/classterrainmaterial/">TerrainMaterial</a> designed <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> multi-layered surfaces which this " "bin can <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">processs.\n\n</a>" " @ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">RenderBin\n</a>" )

IMPLEMENT_CONOBJECT(RenderTerrainMgr )

  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 "renderInstance/renderTerrainMgr.h"
 26
 27#include "platform/profiler.h"
 28#include "scene/sceneManager.h"
 29#include "gfx/gfxTransformSaver.h"
 30#include "gfx/gfxDrawUtil.h"
 31#include "gfx/gfxDebugEvent.h"
 32#include "materials/shaderData.h"
 33#include "materials/matInstance.h"
 34#include "scene/sceneRenderState.h"
 35#include "console/consoleTypes.h"
 36#include "terrain/terrCell.h"
 37#include "terrain/terrCellMaterial.h"
 38#include "math/util/matrixSet.h"
 39#include "materials/materialManager.h"
 40
 41bool RenderTerrainMgr::smRenderWireframe = false;
 42
 43S32 RenderTerrainMgr::smCellsRendered = 0;
 44S32 RenderTerrainMgr::smOverrideCells = 0;
 45S32 RenderTerrainMgr::smDrawCalls = 0;
 46
 47
 48IMPLEMENT_CONOBJECT(RenderTerrainMgr);
 49
 50
 51ConsoleDocClass( RenderTerrainMgr, 
 52   "@brief A render bin for terrain mesh rendering.\n\n"
 53   "This bin renders terrain render instances from a TerrainBlock. "
 54   "Normally a mesh would render via the RenderMeshMgr, but terrain uses "
 55   "a TerrainMaterial designed for multi-layered surfaces which this "
 56   "bin can processs.\n\n"
 57   "@ingroup RenderBin\n" );
 58
 59RenderTerrainMgr::RenderTerrainMgr()
 60   :  RenderBinManager( RenderPassManager::RIT_Terrain, 1.0f, 1.0f )
 61{
 62}
 63
 64RenderTerrainMgr::RenderTerrainMgr( F32 renderOrder, F32 processAddOrder )
 65   :  RenderBinManager( RenderPassManager::RIT_Terrain, renderOrder, processAddOrder )
 66{
 67}
 68
 69RenderTerrainMgr::~RenderTerrainMgr()
 70{
 71}
 72
 73void RenderTerrainMgr::initPersistFields()
 74{
 75   Con::addVariable( "RenderTerrainMgr::renderWireframe", TypeBool, &smRenderWireframe,
 76      "Used to enable wireframe rendering on terrain for debugging.\n"
 77      "@ingroup RenderBin\n" );
 78
 79   // For stats.
 80   GFXDevice::getDeviceEventSignal().notify( &RenderTerrainMgr::_clearStats );
 81   Con::addVariable( "$TerrainBlock::cellsRendered", TypeS32, &smCellsRendered, "@internal" );
 82   Con::addVariable( "$TerrainBlock::overrideCells", TypeS32, &smOverrideCells, "@internal" );
 83   Con::addVariable( "$TerrainBlock::drawCalls", TypeS32, &smDrawCalls, "@internal" );
 84
 85   Parent::initPersistFields();
 86}
 87
 88bool RenderTerrainMgr::_clearStats( GFXDevice::GFXDeviceEventType type )
 89{
 90   if ( type == GFXDevice::deStartOfFrame )
 91   {
 92      smCellsRendered = 0;
 93      smOverrideCells = 0;
 94      smDrawCalls = 0;
 95   }
 96
 97   return true;
 98}
 99
100void RenderTerrainMgr::internalAddElement( RenderInst *inst_ )
101{
102   TerrainRenderInst *inst = static_cast<TerrainRenderInst*>( inst_ );
103   mInstVector.push_back( inst );
104}
105
106void RenderTerrainMgr::sort()
107{
108   // TODO: We could probably sort this in some
109   // manner to improve terrain rendering perf.
110}
111
112void RenderTerrainMgr::clear()
113{
114   mInstVector.clear();
115}
116
117void RenderTerrainMgr::render( SceneRenderState *state )
118{
119   if ( mInstVector.empty() )
120      return;
121
122   // Check if bin is disabled in advanced lighting.
123   if ( MATMGR->getDeferredEnabled() && mBasicOnly )
124      return;
125
126   PROFILE_SCOPE( RenderTerrainMgr_Render );
127
128   GFXTransformSaver saver;
129
130   // Prepare the common scene graph data.
131   SceneData sgData;
132   sgData.init( state );
133   sgData.wireframe |= smRenderWireframe;
134
135   // Restore transforms
136   MatrixSet &matrixSet = getRenderPass()->getMatrixSet();
137   matrixSet.restoreSceneViewProjection();
138
139   GFXDEBUGEVENT_SCOPE( RenderTerrainMgr_Render, ColorI::GREEN );
140
141   const MatrixF worldViewXfm = matrixSet.getWorldToCamera();
142   const MatrixF &projXfm = matrixSet.getCameraToScreen();
143
144   // HACKTASTIC!
145   //
146   // If this is a shadow pass then render the terrain
147   // with the first material we get.
148   //
149   // In the near future terrains will operate using regular
150   // MatInstance materials like any other mesh in which case
151   // this will all be replaced by a normal mesh render bin.
152   //
153   if ( state->isShadowPass() )
154   {      
155      PROFILE_SCOPE( RenderTerrainMgr_Render_Shadow );
156
157      Vector<TerrainRenderInst*>::iterator inst = mInstVector.begin();
158      BaseMatInstance *overideMat = (*inst)->mat;
159      while ( overideMat && overideMat->setupPass( state, sgData ) )
160      {
161         for ( ; inst != mInstVector.end(); inst++ )
162         {
163            smOverrideCells++;
164
165            GFX->setPrimitiveBuffer( (*inst)->primBuff );
166            GFX->setVertexBuffer( (*inst)->vertBuff );
167
168            matrixSet.setWorld(*(*inst)->objectToWorldXfm);
169
170            overideMat->setSceneInfo( state, sgData );
171            overideMat->setTransforms( matrixSet, state );
172
173            GFX->drawPrimitive( (*inst)->prim );
174         }
175      }
176
177      return;
178   }
179
180   // Do the detail map passes.
181   Vector<TerrainRenderInst*>::iterator inst = mInstVector.begin();
182   for ( ; inst != mInstVector.end(); inst++ )
183   {
184      TerrainCellMaterial *mat = (*inst)->cellMat;
185
186      GFX->setPrimitiveBuffer( (*inst)->primBuff );
187      GFX->setVertexBuffer( (*inst)->vertBuff );
188
189      ++smCellsRendered;
190
191      mat->setTransformAndEye(   *(*inst)->objectToWorldXfm,
192                                 worldViewXfm,
193                                 projXfm,
194                                 state->getFarPlane() );
195
196      sgData.objTrans = (*inst)->objectToWorldXfm;
197      dMemcpy( sgData.lights, (*inst)->lights, sizeof( sgData.lights ) );
198
199      while ( mat->setupPass( state, sgData ) )
200      {
201         ++smDrawCalls;
202         GFX->drawPrimitive( (*inst)->prim );
203      }
204   }
205}
206
207