renderShapeExample.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. INIT_ASSET(Shape);
  55. mShapeInstance = NULL;
  56. }
  57. RenderShapeExample::~RenderShapeExample()
  58. {
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Object Editing
  62. //-----------------------------------------------------------------------------
  63. void RenderShapeExample::initPersistFields()
  64. {
  65. addGroup( "Rendering" );
  66. INITPERSISTFIELD_SHAPEASSET(Shape, RenderShapeExample, "The path to the shape file.")
  67. endGroup( "Rendering" );
  68. // SceneObject already handles exposing the transform
  69. Parent::initPersistFields();
  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(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(conn, Shape);
  141. if ( isProperlyAdded() )
  142. createShape();
  143. }
  144. }
  145. //-----------------------------------------------------------------------------
  146. // Object Rendering
  147. //-----------------------------------------------------------------------------
  148. void RenderShapeExample::createShape()
  149. {
  150. if ( getShape() == StringTable->EmptyString() )
  151. return;
  152. // If this is the same shape then no reason to update it
  153. if ( mShapeInstance && getShape() == StringTable->insert(mShape.getPath().getFullPath().c_str()) )
  154. return;
  155. // Clean up our previous shape
  156. if ( mShapeInstance )
  157. SAFE_DELETE( mShapeInstance );
  158. // Attempt to preload the Materials for this shape
  159. if ( isClientObject() &&
  160. !mShape->preloadMaterialList( mShape.getPath() ) &&
  161. NetConnection::filesWereDownloaded() )
  162. {
  163. return;
  164. }
  165. // Update the bounding box
  166. mObjBox = mShape->mBounds;
  167. resetWorldBox();
  168. setRenderTransform(mObjToWorld);
  169. // Create the TSShapeInstance
  170. mShapeInstance = new TSShapeInstance( mShape, isClientObject() );
  171. }
  172. void RenderShapeExample::prepRenderImage( SceneRenderState *state )
  173. {
  174. // Make sure we have a TSShapeInstance
  175. if ( !mShapeInstance )
  176. return;
  177. // Calculate the distance of this object from the camera
  178. Point3F cameraOffset;
  179. getRenderTransform().getColumn( 3, &cameraOffset );
  180. cameraOffset -= state->getDiffuseCameraPosition();
  181. F32 dist = cameraOffset.len();
  182. if ( dist < 0.01f )
  183. dist = 0.01f;
  184. // Set up the LOD for the shape
  185. F32 invScale = ( 1.0f / getMax( getMax( mObjScale.x, mObjScale.y ), mObjScale.z ) );
  186. mShapeInstance->setDetailFromDistance( state, dist * invScale );
  187. // Make sure we have a valid level of detail
  188. if ( mShapeInstance->getCurrentDetail() < 0 )
  189. return;
  190. // GFXTransformSaver is a handy helper class that restores
  191. // the current GFX matrices to their original values when
  192. // it goes out of scope at the end of the function
  193. GFXTransformSaver saver;
  194. // Set up our TS render state
  195. TSRenderState rdata;
  196. rdata.setSceneState( state );
  197. rdata.setFadeOverride( 1.0f );
  198. // We might have some forward lit materials
  199. // so pass down a query to gather lights.
  200. LightQuery query;
  201. query.init( getWorldSphere() );
  202. rdata.setLightQuery( &query );
  203. // Set the world matrix to the objects render transform
  204. MatrixF mat = getRenderTransform();
  205. mat.scale( mObjScale );
  206. GFX->setWorldMatrix( mat );
  207. // Animate the the shape
  208. mShapeInstance->animate();
  209. // Allow the shape to submit the RenderInst(s) for itself
  210. mShapeInstance->render( rdata );
  211. }