2
0

renderShapeExample.cpp 7.9 KB

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