sceneSpace.cpp

Engine/source/scene/sceneSpace.cpp

More...

Public Variables

bool

For frame signal.

Detailed Description

Public Variables

bool gEditingMission 

For frame signal.

  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 "scene/sceneSpace.h"
 26
 27#include "core/stream/bitStream.h"
 28#include "console/engineAPI.h"
 29#include "math/mathIO.h"
 30#include "math/mOrientedBox.h"
 31#include "gfx/primBuilder.h"
 32#include "gfx/gfxDrawUtil.h"
 33#include "gfx/gfxTransformSaver.h"
 34#include "renderInstance/renderPassManager.h"
 35#include "materials/materialDefinition.h"
 36#include "materials/baseMatInstance.h"
 37#include "scene/sceneRenderState.h"
 38
 39
 40extern bool gEditingMission;
 41
 42
 43//-----------------------------------------------------------------------------
 44
 45SceneSpace::SceneSpace()
 46   : mEditorRenderMaterial( NULL )
 47{
 48   mNetFlags.set( Ghostable | ScopeAlways );
 49   mTypeMask |= StaticObjectType;
 50
 51   // Except when their rendering is otherwise suppressed, we do not want
 52   // spaces to get culled away when in the editor.
 53   mObjectFlags |= DisableCullingInEditorFlag;
 54}
 55
 56//-----------------------------------------------------------------------------
 57
 58SceneSpace::~SceneSpace()
 59{
 60   SAFE_DELETE( mEditorRenderMaterial );
 61}
 62
 63//-----------------------------------------------------------------------------
 64
 65bool SceneSpace::onAdd()
 66{
 67   if( !Parent::onAdd() )
 68      return false;
 69
 70   addToScene();
 71   return true;
 72}
 73
 74//-----------------------------------------------------------------------------
 75
 76void SceneSpace::onRemove()
 77{
 78   removeFromScene();
 79   Parent::onRemove();
 80}
 81
 82//-----------------------------------------------------------------------------
 83
 84void SceneSpace::setTransform(const MatrixF & mat)
 85{  
 86   Parent::setTransform( mat );
 87
 88   if( isServerObject() )
 89      setMaskBits( TransformMask );
 90}
 91
 92//-----------------------------------------------------------------------------
 93
 94void SceneSpace::onEditorEnable()
 95{
 96   // If we haven't created a material for editor rendering yet,
 97   // try so now.
 98
 99   if( isClientObject() && !mEditorRenderMaterial )
100      mEditorRenderMaterial = _createEditorRenderMaterial();
101}
102
103//-----------------------------------------------------------------------------
104
105void SceneSpace::onEditorDisable()
106{
107   SAFE_DELETE( mEditorRenderMaterial );
108}
109
110//-----------------------------------------------------------------------------
111
112BaseMatInstance* SceneSpace::_createEditorRenderMaterial()
113{
114   String materialName = String::ToString( "Editor%sMaterial", getClassName() );
115   
116   Material* material;
117   if( !Sim::findObject( materialName, material ) )
118      return NULL;
119
120   return material->createMatInstance();
121}
122
123//-----------------------------------------------------------------------------
124
125void SceneSpace::prepRenderImage( SceneRenderState* state )
126{
127   if( !gEditingMission )
128      return;
129
130   if( !state->isDiffusePass() )
131      return;
132
133   ObjectRenderInst* ri = state->getRenderPass()->allocInst< ObjectRenderInst >();
134   ri->renderDelegate.bind( this, &SceneSpace::_renderObject );
135   ri->type = RenderPassManager::RIT_Editor;
136   ri->defaultKey = 0;
137   ri->defaultKey2 = 0;
138   state->getRenderPass()->addInst( ri );
139}
140
141//-----------------------------------------------------------------------------
142
143void SceneSpace::_renderObject( ObjectRenderInst* ri, SceneRenderState* state, BaseMatInstance* overrideMat )
144{
145   if( overrideMat )
146      return;
147
148   if( !mEditorRenderMaterial )
149   {
150      // We have no material for rendering so just render
151      // a plain box.
152
153      GFXTransformSaver saver;
154
155      MatrixF mat = getRenderTransform();
156      mat.scale( getScale() );
157
158      GFX->multWorld( mat );
159
160      GFXStateBlockDesc desc;
161      desc.setZReadWrite( true, false );
162      desc.setBlend( true );
163      desc.setCullMode( GFXCullNone );
164
165      GFXDrawUtil *drawer = GFX->getDrawUtil();
166      drawer->drawCube( desc, mObjBox, _getDefaultEditorSolidColor() );
167
168      // Render black wireframe.
169
170      desc.setFillModeWireframe();
171      drawer->drawCube( desc, mObjBox, _getDefaultEditorWireframeColor() );
172   }
173   else
174   {
175      //RDTODO
176   }
177}
178
179//-----------------------------------------------------------------------------
180
181U32 SceneSpace::packUpdate( NetConnection* connection, U32 mask, BitStream* stream )
182{
183   U32 retMask = Parent::packUpdate( connection, mask, stream );
184
185   if( stream->writeFlag( mask & TransformMask ) ) 
186   {
187      mathWrite( *stream, mObjToWorld );
188      mathWrite( *stream, mObjScale );
189   }
190
191   return retMask;
192}
193
194//-----------------------------------------------------------------------------
195
196void SceneSpace::unpackUpdate( NetConnection* connection, BitStream* stream )
197{
198   Parent::unpackUpdate( connection, stream );
199
200   if( stream->readFlag() )  // TransformMask
201   {
202      mathRead( *stream, &mObjToWorld );
203      mathRead( *stream, &mObjScale );
204
205      setTransform( mObjToWorld );
206   }
207}
208