tsLastDetail.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. /// The shape detail level to capture into
  83. /// the imposters.
  84. S32 mDl;
  85. /// The bounding radius of the shape
  86. /// used to size the billboard.
  87. F32 mRadius;
  88. /// The center offset for the bounding
  89. /// sphere used to render the imposter.
  90. Point3F mCenter;
  91. /// The square dimensions of each
  92. /// captured imposter image.
  93. S32 mDim;
  94. /// The number steps around the equator of
  95. /// the globe at which we capture an imposter.
  96. U32 mNumEquatorSteps;
  97. /// The number of steps to go from equator to
  98. /// each polar region (0 means equator only) at
  99. /// which we capture an imposter.
  100. U32 mNumPolarSteps;
  101. /// The angle in radians of sub-polar regions.
  102. F32 mPolarAngle;
  103. /// If true we captures polar images in the
  104. /// imposter texture.
  105. bool mIncludePoles;
  106. /// The combined imposter state and corner data vertex
  107. /// format used for rendering with multiple streams.
  108. GFXVertexFormat mImposterVertDecl;
  109. /// The material for this imposter.
  110. SimObjectPtr<Material> mMaterial;
  111. /// The material instance used to render this imposter.
  112. BaseMatInstance *mMatInstance;
  113. /// This is a global list of all the TSLastDetail
  114. /// objects in the system.
  115. static Vector<TSLastDetail*> smLastDetails;
  116. /// The maximum texture size for a billboard texture.
  117. static const U32 smMaxTexSize = 2048;
  118. /// This update actually regenerates the imposter images.
  119. void _update();
  120. ///
  121. void _validateDim();
  122. /// Helper which returns the imposter diffuse map path.
  123. String _getDiffuseMapPath() const { return mCachePath + ".imposter.dds"; }
  124. /// Helper which returns the imposter normal map path.
  125. String _getNormalMapPath() const { return mCachePath + ".imposter_normals.dds"; }
  126. public:
  127. TSLastDetail( TSShape *shape,
  128. const String &cachePath,
  129. U32 numEquatorSteps,
  130. U32 numPolarSteps,
  131. F32 polarAngle,
  132. bool includePoles,
  133. S32 dl,
  134. S32 dim );
  135. ~TSLastDetail();
  136. /// Global preference for rendering imposters to shadows.
  137. static bool smCanShadow;
  138. /// Calls update on all TSLastDetail objects in the system.
  139. /// @see update()
  140. static void updateImposterImages( bool forceUpdate = false );
  141. /// Loads the imposter images by reading them from the disk
  142. /// or generating them if the TSShape is more recient than the
  143. /// cached imposter textures.
  144. ///
  145. /// This should not be called from within any rendering code.
  146. ///
  147. /// @param forceUpdate If true the disk cache is invalidated and
  148. /// new imposter images are rendered.
  149. ///
  150. void update( bool forceUpdate = false );
  151. /// Internal function called from TSShapeInstance to
  152. /// submit an imposter render instance.
  153. void render( const TSRenderState &rdata, F32 alpha );
  154. /// Returns the material instance used to render this imposter.
  155. BaseMatInstance* getMatInstance() const { return mMatInstance; }
  156. /// Helper function which deletes the cached imposter
  157. /// texture files from disk.
  158. void deleteImposterCacheTextures();
  159. /// Returns the radius.
  160. /// @see mRadius
  161. F32 getRadius() const { return mRadius; }
  162. };
  163. #endif // _TSLASTDETAIL_H_