tsLastDetail.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #ifndef _TSLASTDETAIL_H_
  23. #define _TSLASTDETAIL_H_
  24. #ifndef _MATHTYPES_H_
  25. #include "math/mathTypes.h"
  26. #endif
  27. #ifndef _MPOINT3_H_
  28. #include "math/mPoint3.h"
  29. #endif
  30. #ifndef _MMATRIX_H_
  31. #include "math/mMatrix.h"
  32. #endif
  33. #ifndef _TVECTOR_H_
  34. #include "core/util/tVector.h"
  35. #endif
  36. #ifndef __RESOURCE_H__
  37. #include "core/resource.h"
  38. #endif
  39. #ifndef _TSRENDERDATA_H_
  40. #include "ts/tsRenderState.h"
  41. #endif
  42. #ifndef _GFXVERTEXFORMAT_H_
  43. #include "gfx/gfxVertexFormat.h"
  44. #endif
  45. #ifndef _SIM_H_
  46. #include "console/simObject.h"
  47. #endif
  48. class TSShape;
  49. class TSRenderState;
  50. class SceneRenderState;
  51. class Material;
  52. class BaseMatInstance;
  53. /// The imposter state vertex format.
  54. GFXDeclareVertexFormat( ImposterState )
  55. {
  56. /// .xyz = imposter center
  57. /// .w = billboard corner... damn SM 2.0
  58. Point3F center;
  59. F32 corner;
  60. /// .x = scaled half size
  61. /// .y = alpha fade out
  62. F32 halfSize;
  63. F32 alpha;
  64. /// The rotation encoded as the up
  65. /// and right vectors... cross FTW.
  66. Point3F upVec;
  67. Point3F rightVec;
  68. };
  69. /// This neat little class renders the object to a texture so that when the object
  70. /// is far away, it can be drawn as a billboard instead of a mesh. This happens
  71. /// when the model is first loaded as to keep the realtime render as fast as possible.
  72. /// It also renders the model from a few different perspectives so that it would actually
  73. /// pass as a model instead of a silly old billboard. In other words, this is an imposter.
  74. class TSLastDetail
  75. {
  76. protected:
  77. /// The shape which we're impostering.
  78. TSShape *mShape;
  79. /// This is the path of the object, which is
  80. /// where we'll be storing our cache for rendered imposters.
  81. String mCachePath;
  82. String mDiffusePath;
  83. String mNormalPath;
  84. /// The shape detail level to capture into
  85. /// the imposters.
  86. S32 mDl;
  87. /// The bounding radius of the shape
  88. /// used to size the billboard.
  89. F32 mRadius;
  90. /// The center offset for the bounding
  91. /// sphere used to render the imposter.
  92. Point3F mCenter;
  93. /// The square dimensions of each
  94. /// captured imposter image.
  95. S32 mDim;
  96. /// The number steps around the equator of
  97. /// the globe at which we capture an imposter.
  98. U32 mNumEquatorSteps;
  99. /// The number of steps to go from equator to
  100. /// each polar region (0 means equator only) at
  101. /// which we capture an imposter.
  102. U32 mNumPolarSteps;
  103. /// The angle in radians of sub-polar regions.
  104. F32 mPolarAngle;
  105. /// If true we captures polar images in the
  106. /// imposter texture.
  107. bool mIncludePoles;
  108. /// The combined imposter state and corner data vertex
  109. /// format used for rendering with multiple streams.
  110. GFXVertexFormat mImposterVertDecl;
  111. /// The material for this imposter.
  112. SimObjectPtr<Material> mMaterial;
  113. /// The material instance used to render this imposter.
  114. BaseMatInstance *mMatInstance;
  115. /// This is a global list of all the TSLastDetail
  116. /// objects in the system.
  117. static Vector<TSLastDetail*> smLastDetails;
  118. /// The maximum texture size for a billboard texture.
  119. static const U32 smMaxTexSize = 2048;
  120. /// This update actually regenerates the imposter images.
  121. void _update();
  122. ///
  123. void _validateDim();
  124. /// Helper which returns the imposter diffuse map path.
  125. String _getDiffuseMapPath() const { return mDiffusePath; }
  126. /// Helper which returns the imposter normal map path.
  127. String _getNormalMapPath() const { return mNormalPath; }
  128. public:
  129. TSLastDetail( TSShape *shape,
  130. const String &cachePath,
  131. U32 numEquatorSteps,
  132. U32 numPolarSteps,
  133. F32 polarAngle,
  134. bool includePoles,
  135. S32 dl,
  136. S32 dim );
  137. TSLastDetail(TSShape* shape,
  138. const String& cachePath,
  139. const String& diffusePath,
  140. const String& normalPath,
  141. U32 numEquatorSteps,
  142. U32 numPolarSteps,
  143. F32 polarAngle,
  144. bool includePoles,
  145. S32 dl, S32 dim);
  146. ~TSLastDetail();
  147. /// Global preference for rendering imposters to shadows.
  148. static bool smCanShadow;
  149. /// Calls update on all TSLastDetail objects in the system.
  150. /// @see update()
  151. static void updateImposterImages( bool forceUpdate = false );
  152. /// Loads the imposter images by reading them from the disk
  153. /// or generating them if the TSShape is more recient than the
  154. /// cached imposter textures.
  155. ///
  156. /// This should not be called from within any rendering code.
  157. ///
  158. /// @param forceUpdate If true the disk cache is invalidated and
  159. /// new imposter images are rendered.
  160. ///
  161. void update( bool forceUpdate = false );
  162. /// Internal function called from TSShapeInstance to
  163. /// submit an imposter render instance.
  164. void render( const TSRenderState &rdata, F32 alpha );
  165. /// Returns the material instance used to render this imposter.
  166. BaseMatInstance* getMatInstance() const { return mMatInstance; }
  167. /// Helper function which deletes the cached imposter
  168. /// texture files from disk.
  169. void deleteImposterCacheTextures();
  170. /// Returns the radius.
  171. /// @see mRadius
  172. F32 getRadius() const { return mRadius; }
  173. };
  174. #endif // _TSLASTDETAIL_H_