renderShapeExample.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. addField( "shapeFile", TypeStringFilename, Offset( mShapeFile, RenderShapeExample ),
  66. "The path to the DTS 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. stream->write( mShapeFile );
  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. stream->read( &mShapeFile );
  141. if ( isProperlyAdded() )
  142. createShape();
  143. }
  144. }
  145. //-----------------------------------------------------------------------------
  146. // Object Rendering
  147. //-----------------------------------------------------------------------------
  148. void RenderShapeExample::createShape()
  149. {
  150. if ( mShapeFile.isEmpty() )
  151. return;
  152. // If this is the same shape then no reason to update it
  153. if ( mShapeInstance && mShapeFile.equal( mShape.getPath().getFullPath(), String::NoCase ) )
  154. return;
  155. // Clean up our previous shape
  156. if ( mShapeInstance )
  157. SAFE_DELETE( mShapeInstance );
  158. mShape = NULL;
  159. // Attempt to get the resource from the ResourceManager
  160. mShape = ResourceManager::get().load( mShapeFile );
  161. if ( !mShape )
  162. {
  163. Con::errorf( "RenderShapeExample::createShape() - Unable to load shape: %s", mShapeFile.c_str() );
  164. return;
  165. }
  166. // Attempt to preload the Materials for this shape
  167. if ( isClientObject() &&
  168. !mShape->preloadMaterialList( mShape.getPath() ) &&
  169. NetConnection::filesWereDownloaded() )
  170. {
  171. mShape = NULL;
  172. return;
  173. }
  174. // Update the bounding box
  175. mObjBox = mShape->bounds;
  176. resetWorldBox();
  177. setRenderTransform(mObjToWorld);
  178. // Create the TSShapeInstance
  179. mShapeInstance = new TSShapeInstance( mShape, isClientObject() );
  180. }
  181. void RenderShapeExample::prepRenderImage( SceneRenderState *state )
  182. {
  183. // Make sure we have a TSShapeInstance
  184. if ( !mShapeInstance )
  185. return;
  186. // Calculate the distance of this object from the camera
  187. Point3F cameraOffset;
  188. getRenderTransform().getColumn( 3, &cameraOffset );
  189. cameraOffset -= state->getDiffuseCameraPosition();
  190. F32 dist = cameraOffset.len();
  191. if ( dist < 0.01f )
  192. dist = 0.01f;
  193. // Set up the LOD for the shape
  194. F32 invScale = ( 1.0f / getMax( getMax( mObjScale.x, mObjScale.y ), mObjScale.z ) );
  195. mShapeInstance->setDetailFromDistance( state, dist * invScale );
  196. // Make sure we have a valid level of detail
  197. if ( mShapeInstance->getCurrentDetail() < 0 )
  198. return;
  199. // GFXTransformSaver is a handy helper class that restores
  200. // the current GFX matrices to their original values when
  201. // it goes out of scope at the end of the function
  202. GFXTransformSaver saver;
  203. // Set up our TS render state
  204. TSRenderState rdata;
  205. rdata.setSceneState( state );
  206. rdata.setFadeOverride( 1.0f );
  207. // We might have some forward lit materials
  208. // so pass down a query to gather lights.
  209. LightQuery query;
  210. query.init( getWorldSphere() );
  211. rdata.setLightQuery( &query );
  212. // Set the world matrix to the objects render transform
  213. MatrixF mat = getRenderTransform();
  214. mat.scale( mObjScale );
  215. GFX->setWorldMatrix( mat );
  216. // Animate the the shape
  217. mShapeInstance->animate();
  218. // Allow the shape to submit the RenderInst(s) for itself
  219. mShapeInstance->render( rdata );
  220. }