renderShapeExample.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. docsURL;
  66. Parent::initPersistFields();
  67. addGroup( "Shapes" );
  68. INITPERSISTFIELD_SHAPEASSET(Shape, RenderShapeExample, "The path to the shape file.")
  69. endGroup( "Shapes" );
  70. // SceneObject already handles exposing the transform
  71. }
  72. void RenderShapeExample::inspectPostApply()
  73. {
  74. Parent::inspectPostApply();
  75. // Flag the network mask to send the updates
  76. // to the client object
  77. setMaskBits( UpdateMask );
  78. }
  79. bool RenderShapeExample::onAdd()
  80. {
  81. if ( !Parent::onAdd() )
  82. return false;
  83. // Set up a 1x1x1 bounding box
  84. mObjBox.set( Point3F( -0.5f, -0.5f, -0.5f ),
  85. Point3F( 0.5f, 0.5f, 0.5f ) );
  86. resetWorldBox();
  87. // Add this object to the scene
  88. addToScene();
  89. // Setup the shape.
  90. createShape();
  91. return true;
  92. }
  93. void RenderShapeExample::onRemove()
  94. {
  95. // Remove this object from the scene
  96. removeFromScene();
  97. // Remove our TSShapeInstance
  98. if ( mShapeInstance )
  99. SAFE_DELETE( mShapeInstance );
  100. Parent::onRemove();
  101. }
  102. void RenderShapeExample::setTransform(const MatrixF & mat)
  103. {
  104. // Let SceneObject handle all of the matrix manipulation
  105. Parent::setTransform( mat );
  106. // Dirty our network mask so that the new transform gets
  107. // transmitted to the client object
  108. setMaskBits( TransformMask );
  109. }
  110. U32 RenderShapeExample::packUpdate( NetConnection *conn, U32 mask, BitStream *stream )
  111. {
  112. // Allow the Parent to get a crack at writing its info
  113. U32 retMask = Parent::packUpdate( conn, mask, stream );
  114. // Write our transform information
  115. if ( stream->writeFlag( mask & TransformMask ) )
  116. {
  117. mathWrite(*stream, getTransform());
  118. mathWrite(*stream, getScale());
  119. }
  120. // Write out any of the updated editable properties
  121. if ( stream->writeFlag( mask & UpdateMask ) )
  122. {
  123. PACK_ASSET(conn, Shape);
  124. // Allow the server object a chance to handle a new shape
  125. createShape();
  126. }
  127. return retMask;
  128. }
  129. void RenderShapeExample::unpackUpdate(NetConnection *conn, BitStream *stream)
  130. {
  131. // Let the Parent read any info it sent
  132. Parent::unpackUpdate(conn, stream);
  133. if ( stream->readFlag() ) // TransformMask
  134. {
  135. mathRead(*stream, &mObjToWorld);
  136. mathRead(*stream, &mObjScale);
  137. setTransform( mObjToWorld );
  138. }
  139. if ( stream->readFlag() ) // UpdateMask
  140. {
  141. UNPACK_ASSET(conn, Shape);
  142. if ( isProperlyAdded() )
  143. createShape();
  144. }
  145. }
  146. //-----------------------------------------------------------------------------
  147. // Object Rendering
  148. //-----------------------------------------------------------------------------
  149. void RenderShapeExample::createShape()
  150. {
  151. if ( getShape() == StringTable->EmptyString() )
  152. return;
  153. // If this is the same shape then no reason to update it
  154. if ( mShapeInstance && getShape() == StringTable->insert(mShape.getPath().getFullPath().c_str()) )
  155. return;
  156. // Clean up our previous shape
  157. if ( mShapeInstance )
  158. SAFE_DELETE( mShapeInstance );
  159. // Attempt to preload the Materials for this shape
  160. if ( isClientObject() &&
  161. !mShape->preloadMaterialList( mShape.getPath() ) &&
  162. NetConnection::filesWereDownloaded() )
  163. {
  164. return;
  165. }
  166. // Update the bounding box
  167. mObjBox = mShape->mBounds;
  168. resetWorldBox();
  169. setRenderTransform(mObjToWorld);
  170. // Create the TSShapeInstance
  171. mShapeInstance = new TSShapeInstance( mShape, isClientObject() );
  172. }
  173. void RenderShapeExample::prepRenderImage( SceneRenderState *state )
  174. {
  175. // Make sure we have a TSShapeInstance
  176. if ( !mShapeInstance )
  177. return;
  178. // Calculate the distance of this object from the camera
  179. Point3F cameraOffset;
  180. getRenderTransform().getColumn( 3, &cameraOffset );
  181. cameraOffset -= state->getDiffuseCameraPosition();
  182. F32 dist = cameraOffset.len();
  183. if ( dist < 0.01f )
  184. dist = 0.01f;
  185. // Set up the LOD for the shape
  186. F32 invScale = ( 1.0f / getMax( getMax( mObjScale.x, mObjScale.y ), mObjScale.z ) );
  187. mShapeInstance->setDetailFromDistance( state, dist * invScale );
  188. // Make sure we have a valid level of detail
  189. if ( mShapeInstance->getCurrentDetail() < 0 )
  190. return;
  191. // GFXTransformSaver is a handy helper class that restores
  192. // the current GFX matrices to their original values when
  193. // it goes out of scope at the end of the function
  194. GFXTransformSaver saver;
  195. // Set up our TS render state
  196. TSRenderState rdata;
  197. rdata.setSceneState( state );
  198. rdata.setFadeOverride( 1.0f );
  199. // We might have some forward lit materials
  200. // so pass down a query to gather lights.
  201. LightQuery query;
  202. query.init( getWorldSphere() );
  203. rdata.setLightQuery( &query );
  204. // Set the world matrix to the objects render transform
  205. MatrixF mat = getRenderTransform();
  206. mat.scale( mObjScale );
  207. GFX->setWorldMatrix( mat );
  208. // Animate the the shape
  209. mShapeInstance->animate();
  210. // Allow the shape to submit the RenderInst(s) for itself
  211. mShapeInstance->render( rdata );
  212. }