renderShapeExample.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 "T3D/examples/renderShapeExample.h"
  23. #include "math/mathIO.h"
  24. #include "sim/netConnection.h"
  25. #include "scene/sceneRenderState.h"
  26. #include "console/consoleTypes.h"
  27. #include "core/resourceManager.h"
  28. #include "core/stream/bitStream.h"
  29. #include "gfx/gfxTransformSaver.h"
  30. #include "renderInstance/renderPassManager.h"
  31. #include "lighting/lightQuery.h"
  32. IMPLEMENT_CO_NETOBJECT_V1(RenderShapeExample);
  33. ConsoleDocClass( RenderShapeExample,
  34. "@brief An example scene object which renders a DTS.\n\n"
  35. "This class implements a basic SceneObject that can exist in the world at a "
  36. "3D position and render itself. There are several valid ways to render an "
  37. "object in Torque. This class makes use of the 'TS' (three space) shape "
  38. "system. TS manages loading the various mesh formats supported by Torque as "
  39. "well was rendering those meshes (including LOD and animation...though this "
  40. "example doesn't include any animation over time).\n\n"
  41. "See the C++ code for implementation details.\n\n"
  42. "@ingroup Examples\n" );
  43. //-----------------------------------------------------------------------------
  44. // Object setup and teardown
  45. //-----------------------------------------------------------------------------
  46. RenderShapeExample::RenderShapeExample()
  47. {
  48. // Flag this object so that it will always
  49. // be sent across the network to clients
  50. mNetFlags.set( Ghostable | ScopeAlways );
  51. // Set it as a "static" object.
  52. mTypeMask |= StaticObjectType | StaticShapeObjectType;
  53. // Make sure to initialize our TSShapeInstance to NULL
  54. mShapeInstance = NULL;
  55. }
  56. RenderShapeExample::~RenderShapeExample()
  57. {
  58. }
  59. //-----------------------------------------------------------------------------
  60. // Object Editing
  61. //-----------------------------------------------------------------------------
  62. void RenderShapeExample::initPersistFields()
  63. {
  64. docsURL;
  65. Parent::initPersistFields();
  66. addGroup( "Shapes" );
  67. INITPERSISTFIELD_SHAPEASSET_REFACTOR(Shape, RenderShapeExample, "The path to the shape file.")
  68. endGroup( "Shapes" );
  69. // SceneObject already handles exposing the transform
  70. }
  71. void RenderShapeExample::inspectPostApply()
  72. {
  73. Parent::inspectPostApply();
  74. // Flag the network mask to send the updates
  75. // to the client object
  76. setMaskBits( UpdateMask );
  77. }
  78. bool RenderShapeExample::onAdd()
  79. {
  80. if ( !Parent::onAdd() )
  81. return false;
  82. // Set up a 1x1x1 bounding box
  83. mObjBox.set( Point3F( -0.5f, -0.5f, -0.5f ),
  84. Point3F( 0.5f, 0.5f, 0.5f ) );
  85. resetWorldBox();
  86. // Add this object to the scene
  87. addToScene();
  88. // Setup the shape.
  89. createShape();
  90. return true;
  91. }
  92. void RenderShapeExample::onRemove()
  93. {
  94. // Remove this object from the scene
  95. removeFromScene();
  96. // Remove our TSShapeInstance
  97. if ( mShapeInstance )
  98. SAFE_DELETE( mShapeInstance );
  99. Parent::onRemove();
  100. }
  101. void RenderShapeExample::setTransform(const MatrixF & mat)
  102. {
  103. // Let SceneObject handle all of the matrix manipulation
  104. Parent::setTransform( mat );
  105. // Dirty our network mask so that the new transform gets
  106. // transmitted to the client object
  107. setMaskBits( TransformMask );
  108. }
  109. U32 RenderShapeExample::packUpdate( NetConnection *conn, U32 mask, BitStream *stream )
  110. {
  111. // Allow the Parent to get a crack at writing its info
  112. U32 retMask = Parent::packUpdate( conn, mask, stream );
  113. // Write our transform information
  114. if ( stream->writeFlag( mask & TransformMask ) )
  115. {
  116. mathWrite(*stream, getTransform());
  117. mathWrite(*stream, getScale());
  118. }
  119. // Write out any of the updated editable properties
  120. if ( stream->writeFlag( mask & UpdateMask ) )
  121. {
  122. PACK_ASSET_REFACTOR(conn, Shape);
  123. // Allow the server object a chance to handle a new shape
  124. createShape();
  125. }
  126. return retMask;
  127. }
  128. void RenderShapeExample::unpackUpdate(NetConnection *conn, BitStream *stream)
  129. {
  130. // Let the Parent read any info it sent
  131. Parent::unpackUpdate(conn, stream);
  132. if ( stream->readFlag() ) // TransformMask
  133. {
  134. mathRead(*stream, &mObjToWorld);
  135. mathRead(*stream, &mObjScale);
  136. setTransform( mObjToWorld );
  137. }
  138. if ( stream->readFlag() ) // UpdateMask
  139. {
  140. UNPACK_ASSET_REFACTOR(conn, Shape);
  141. if ( isProperlyAdded() )
  142. createShape();
  143. }
  144. }
  145. //-----------------------------------------------------------------------------
  146. // Object Rendering
  147. //-----------------------------------------------------------------------------
  148. void RenderShapeExample::createShape()
  149. {
  150. if ( mShapeAsset.isNull() )
  151. return;
  152. // Clean up our previous shape
  153. if ( mShapeInstance )
  154. SAFE_DELETE( mShapeInstance );
  155. // Attempt to preload the Materials for this shape
  156. if ( isClientObject() &&
  157. !getShape()->preloadMaterialList(getShape().getPath() ) &&
  158. NetConnection::filesWereDownloaded() )
  159. {
  160. return;
  161. }
  162. // Update the bounding box
  163. mObjBox = getShape()->mBounds;
  164. resetWorldBox();
  165. setRenderTransform(mObjToWorld);
  166. // Create the TSShapeInstance
  167. mShapeInstance = new TSShapeInstance(getShape(), isClientObject() );
  168. }
  169. void RenderShapeExample::prepRenderImage( SceneRenderState *state )
  170. {
  171. // Make sure we have a TSShapeInstance
  172. if ( !mShapeInstance )
  173. return;
  174. // Calculate the distance of this object from the camera
  175. Point3F cameraOffset;
  176. getRenderTransform().getColumn( 3, &cameraOffset );
  177. cameraOffset -= state->getDiffuseCameraPosition();
  178. F32 dist = cameraOffset.len();
  179. if ( dist < 0.01f )
  180. dist = 0.01f;
  181. // Set up the LOD for the shape
  182. F32 invScale = ( 1.0f / getMax( getMax( mObjScale.x, mObjScale.y ), mObjScale.z ) );
  183. mShapeInstance->setDetailFromDistance( state, dist * invScale );
  184. // Make sure we have a valid level of detail
  185. if ( mShapeInstance->getCurrentDetail() < 0 )
  186. return;
  187. // GFXTransformSaver is a handy helper class that restores
  188. // the current GFX matrices to their original values when
  189. // it goes out of scope at the end of the function
  190. GFXTransformSaver saver;
  191. // Set up our TS render state
  192. TSRenderState rdata;
  193. rdata.setSceneState( state );
  194. rdata.setFadeOverride( 1.0f );
  195. // We might have some forward lit materials
  196. // so pass down a query to gather lights.
  197. LightQuery query;
  198. query.init( getWorldSphere() );
  199. rdata.setLightQuery( &query );
  200. // Set the world matrix to the objects render transform
  201. MatrixF mat = getRenderTransform();
  202. mat.scale( mObjScale );
  203. GFX->setWorldMatrix( mat );
  204. // Animate the the shape
  205. mShapeInstance->animate();
  206. // Allow the shape to submit the RenderInst(s) for itself
  207. mShapeInstance->render( rdata );
  208. }