sceneSpace.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "scene/sceneSpace.h"
  24. #include "core/stream/bitStream.h"
  25. #include "console/engineAPI.h"
  26. #include "math/mathIO.h"
  27. #include "math/mOrientedBox.h"
  28. #include "gfx/primBuilder.h"
  29. #include "gfx/gfxDrawUtil.h"
  30. #include "gfx/gfxTransformSaver.h"
  31. #include "renderInstance/renderPassManager.h"
  32. #include "materials/materialDefinition.h"
  33. #include "materials/baseMatInstance.h"
  34. #include "scene/sceneRenderState.h"
  35. extern bool gEditingMission;
  36. //-----------------------------------------------------------------------------
  37. SceneSpace::SceneSpace()
  38. : mEditorRenderMaterial( NULL )
  39. {
  40. mNetFlags.set( Ghostable | ScopeAlways );
  41. mTypeMask |= StaticObjectType;
  42. // Except when their rendering is otherwise suppressed, we do not want
  43. // spaces to get culled away when in the editor.
  44. mObjectFlags |= DisableCullingInEditorFlag;
  45. }
  46. //-----------------------------------------------------------------------------
  47. SceneSpace::~SceneSpace()
  48. {
  49. SAFE_DELETE( mEditorRenderMaterial );
  50. }
  51. //-----------------------------------------------------------------------------
  52. bool SceneSpace::onAdd()
  53. {
  54. if( !Parent::onAdd() )
  55. return false;
  56. addToScene();
  57. return true;
  58. }
  59. //-----------------------------------------------------------------------------
  60. void SceneSpace::onRemove()
  61. {
  62. removeFromScene();
  63. Parent::onRemove();
  64. }
  65. //-----------------------------------------------------------------------------
  66. void SceneSpace::setTransform(const MatrixF & mat)
  67. {
  68. Parent::setTransform( mat );
  69. if( isServerObject() )
  70. setMaskBits( TransformMask );
  71. }
  72. //-----------------------------------------------------------------------------
  73. void SceneSpace::onEditorEnable()
  74. {
  75. // If we haven't created a material for editor rendering yet,
  76. // try so now.
  77. if( isClientObject() && !mEditorRenderMaterial )
  78. mEditorRenderMaterial = _createEditorRenderMaterial();
  79. }
  80. //-----------------------------------------------------------------------------
  81. void SceneSpace::onEditorDisable()
  82. {
  83. SAFE_DELETE( mEditorRenderMaterial );
  84. }
  85. //-----------------------------------------------------------------------------
  86. BaseMatInstance* SceneSpace::_createEditorRenderMaterial()
  87. {
  88. String materialName = String::ToString( "Editor%sMaterial", getClassName() );
  89. Material* material;
  90. if( !Sim::findObject( materialName, material ) )
  91. return NULL;
  92. return material->createMatInstance();
  93. }
  94. //-----------------------------------------------------------------------------
  95. void SceneSpace::prepRenderImage( SceneRenderState* state )
  96. {
  97. if( !gEditingMission )
  98. return;
  99. if( !state->isDiffusePass() )
  100. return;
  101. ObjectRenderInst* ri = state->getRenderPass()->allocInst< ObjectRenderInst >();
  102. ri->renderDelegate.bind( this, &SceneSpace::_renderObject );
  103. ri->type = RenderPassManager::RIT_Editor;
  104. ri->defaultKey = 0;
  105. ri->defaultKey2 = 0;
  106. state->getRenderPass()->addInst( ri );
  107. }
  108. //-----------------------------------------------------------------------------
  109. void SceneSpace::_renderObject( ObjectRenderInst* ri, SceneRenderState* state, BaseMatInstance* overrideMat )
  110. {
  111. if( overrideMat )
  112. return;
  113. if( !mEditorRenderMaterial )
  114. {
  115. // We have no material for rendering so just render
  116. // a plain box.
  117. GFXTransformSaver saver;
  118. MatrixF mat = getRenderTransform();
  119. mat.scale( getScale() );
  120. GFX->multWorld( mat );
  121. GFXStateBlockDesc desc;
  122. desc.setZReadWrite( true, false );
  123. desc.setBlend( true );
  124. desc.setCullMode( GFXCullNone );
  125. GFXDrawUtil *drawer = GFX->getDrawUtil();
  126. drawer->drawCube( desc, mObjBox, _getDefaultEditorSolidColor() );
  127. // Render black wireframe.
  128. desc.setFillModeWireframe();
  129. drawer->drawCube( desc, mObjBox, _getDefaultEditorWireframeColor() );
  130. }
  131. else
  132. {
  133. //RDTODO
  134. }
  135. }
  136. //-----------------------------------------------------------------------------
  137. U32 SceneSpace::packUpdate( NetConnection* connection, U32 mask, BitStream* stream )
  138. {
  139. U32 retMask = Parent::packUpdate( connection, mask, stream );
  140. if( stream->writeFlag( mask & TransformMask ) )
  141. {
  142. mathWrite( *stream, mObjToWorld );
  143. mathWrite( *stream, mObjScale );
  144. }
  145. return retMask;
  146. }
  147. //-----------------------------------------------------------------------------
  148. void SceneSpace::unpackUpdate( NetConnection* connection, BitStream* stream )
  149. {
  150. Parent::unpackUpdate( connection, stream );
  151. if( stream->readFlag() ) // TransformMask
  152. {
  153. mathRead( *stream, &mObjToWorld );
  154. mathRead( *stream, &mObjScale );
  155. setTransform( mObjToWorld );
  156. }
  157. }