Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

290 lines
8.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "movieobjects/dmetexture.h"
  7. #include "datamodel/dmelementfactoryhelper.h"
  8. #include "movieobjects_interfaces.h"
  9. #include "materialsystem/imaterial.h"
  10. #include "materialsystem/imaterialsystem.h"
  11. //-----------------------------------------------------------------------------
  12. // Expose this class to the scene database
  13. //-----------------------------------------------------------------------------
  14. IMPLEMENT_ELEMENT_FACTORY( DmeBaseTexture, CDmeBaseTexture );
  15. //-----------------------------------------------------------------------------
  16. // Constructor, destructor
  17. //-----------------------------------------------------------------------------
  18. void CDmeBaseTexture::OnConstruction()
  19. {
  20. m_pVTFTexture = CreateVTFTexture();
  21. m_bClampS.Init( this, "clampS" );
  22. m_bClampT.Init( this, "clampT" );
  23. m_bClampU.Init( this, "clampU" );
  24. m_bNoLod.Init( this, "noMipmapLOD" );
  25. m_bNiceFiltered.Init( this, "niceFiltered" );
  26. m_bNormalMap.Init( this, "normalMap" );
  27. m_flBumpScale.Init( this, "bumpScale" );
  28. m_nCompressType.Init( this, "compressType" );
  29. m_nFilterType.Init( this, "filterType" );
  30. m_nMipmapType.Init( this, "mipmapType" );
  31. }
  32. void CDmeBaseTexture::OnDestruction()
  33. {
  34. if ( m_pVTFTexture )
  35. {
  36. DestroyVTFTexture( m_pVTFTexture );
  37. m_pVTFTexture = NULL;
  38. }
  39. }
  40. //-----------------------------------------------------------------------------
  41. // accessor for cached ITexture
  42. //-----------------------------------------------------------------------------
  43. ITexture *CDmeBaseTexture::GetCachedTexture()
  44. {
  45. return m_Texture;
  46. }
  47. //-----------------------------------------------------------------------------
  48. // Computes texture flags
  49. //-----------------------------------------------------------------------------
  50. int CDmeBaseTexture::CalcTextureFlags( int nDepth ) const
  51. {
  52. int nFlags = 0;
  53. if ( m_bClampS )
  54. {
  55. nFlags |= TEXTUREFLAGS_CLAMPS;
  56. }
  57. if ( m_bClampT )
  58. {
  59. nFlags |= TEXTUREFLAGS_CLAMPT;
  60. }
  61. if ( m_bClampU )
  62. {
  63. nFlags |= TEXTUREFLAGS_CLAMPU;
  64. }
  65. if ( m_bNoLod )
  66. {
  67. nFlags |= TEXTUREFLAGS_NOLOD;
  68. }
  69. if ( m_bNiceFiltered )
  70. {
  71. nFlags |= TEXTUREFLAGS_NICEFILTERED;
  72. }
  73. if ( m_bNormalMap )
  74. {
  75. nFlags |= TEXTUREFLAGS_NORMAL;
  76. }
  77. if ( m_bNormalMap )
  78. {
  79. nFlags |= TEXTUREFLAGS_NORMAL;
  80. }
  81. if ( m_bNoDebugOverride )
  82. {
  83. nFlags |= TEXTUREFLAGS_NODEBUGOVERRIDE;
  84. }
  85. switch ( m_nCompressType )
  86. {
  87. case DMETEXTURE_COMPRESS_DEFAULT:
  88. case DMETEXTURE_COMPRESS_DXT1:
  89. break;
  90. case DMETEXTURE_COMPRESS_NONE:
  91. nFlags |= TEXTUREFLAGS_NOCOMPRESS;
  92. break;
  93. case DMETEXTURE_COMPRESS_DXT5:
  94. nFlags |= TEXTUREFLAGS_HINT_DXT5;
  95. break;
  96. }
  97. switch ( m_nFilterType )
  98. {
  99. case DMETEXTURE_FILTER_DEFAULT:
  100. case DMETEXTURE_FILTER_BILINEAR:
  101. break;
  102. case DMETEXTURE_FILTER_ANISOTROPIC:
  103. nFlags |= TEXTUREFLAGS_ANISOTROPIC;
  104. break;
  105. case DMETEXTURE_FILTER_TRILINEAR:
  106. nFlags |= TEXTUREFLAGS_TRILINEAR;
  107. break;
  108. case DMETEXTURE_FILTER_POINT:
  109. nFlags |= TEXTUREFLAGS_POINTSAMPLE;
  110. break;
  111. }
  112. switch ( m_nMipmapType )
  113. {
  114. case DMETEXTURE_MIPMAP_DEFAULT:
  115. case DMETEXTURE_MIPMAP_ALL_LEVELS:
  116. break;
  117. case DMETEXTURE_MIPMAP_NONE:
  118. nFlags |= TEXTUREFLAGS_NOMIP;
  119. break;
  120. }
  121. if ( nDepth > 1 )
  122. {
  123. // FIXME: Volume textures don't currently support DXT compression
  124. nFlags &= ~TEXTUREFLAGS_HINT_DXT5;
  125. nFlags |= TEXTUREFLAGS_NOCOMPRESS;
  126. // FIXME: Volume textures don't currently support NICE filtering
  127. nFlags &= ~TEXTUREFLAGS_NICEFILTERED;
  128. }
  129. return nFlags;
  130. }
  131. //-----------------------------------------------------------------------------
  132. // Computes the desired texture format based on flags
  133. //-----------------------------------------------------------------------------
  134. ImageFormat CDmeBaseTexture::ComputeDesiredImageFormat( ImageFormat srcFormat, int nWidth, int nHeight, int nDepth, int nFlags )
  135. {
  136. // HDRFIXME: Need to figure out what format to use here.
  137. if ( srcFormat == IMAGE_FORMAT_RGB323232F )
  138. return IMAGE_FORMAT_RGBA16161616F;
  139. /*
  140. if( bDUDVTarget)
  141. {
  142. if ( bCopyAlphaToLuminance && ( nFlags & ( TEXTUREFLAGS_ONEBITALPHA | TEXTUREFLAGS_EIGHTBITALPHA ) ) )
  143. return IMAGE_FORMAT_UVLX8888;
  144. return IMAGE_FORMAT_UV88;
  145. }
  146. */
  147. // can't compress textures that are smaller than 4x4
  148. if ( (nFlags & TEXTUREFLAGS_NOCOMPRESS) || (nFlags & TEXTUREFLAGS_PROCEDURAL) ||
  149. ( nWidth < 4 ) || ( nHeight < 4 ) || ( nDepth > 1 ) )
  150. {
  151. if ( nFlags & ( TEXTUREFLAGS_ONEBITALPHA | TEXTUREFLAGS_EIGHTBITALPHA ) )
  152. return IMAGE_FORMAT_BGRA8888;
  153. return IMAGE_FORMAT_BGR888;
  154. }
  155. if( nFlags & TEXTUREFLAGS_HINT_DXT5 )
  156. return IMAGE_FORMAT_DXT5;
  157. // compressed with alpha blending
  158. if ( nFlags & TEXTUREFLAGS_EIGHTBITALPHA )
  159. return IMAGE_FORMAT_DXT5;
  160. if ( nFlags & TEXTUREFLAGS_ONEBITALPHA )
  161. return IMAGE_FORMAT_DXT5; // IMAGE_FORMAT_DXT1_ONEBITALPHA
  162. return IMAGE_FORMAT_DXT1;
  163. }
  164. //-----------------------------------------------------------------------------
  165. //
  166. // Normal texture
  167. //
  168. //-----------------------------------------------------------------------------
  169. //-----------------------------------------------------------------------------
  170. // Expose this class to the scene database
  171. //-----------------------------------------------------------------------------
  172. IMPLEMENT_ELEMENT_FACTORY( DmeTexture, CDmeTexture );
  173. //-----------------------------------------------------------------------------
  174. // Constructor, destructor
  175. //-----------------------------------------------------------------------------
  176. void CDmeTexture::OnConstruction()
  177. {
  178. m_Images.Init( this, "images" );
  179. }
  180. void CDmeTexture::OnDestruction()
  181. {
  182. }
  183. //-----------------------------------------------------------------------------
  184. // Build a VTF representing the
  185. //-----------------------------------------------------------------------------
  186. //-----------------------------------------------------------------------------
  187. // resolve
  188. //-----------------------------------------------------------------------------
  189. void CDmeTexture::Resolve()
  190. {
  191. // FIXME: Could change this to not shutdown if only the bits have changed
  192. m_Texture.Shutdown();
  193. int nFrameCount = m_Images.Count();
  194. if ( nFrameCount == 0 )
  195. return;
  196. // Use the size of the 0th image
  197. CDmeImage *pImage = m_Images[0];
  198. int nWidth = pImage->m_Width;
  199. int nHeight = pImage->m_Height;
  200. int nDepth = pImage->m_Depth;
  201. ImageFormat srcFormat = pImage->Format();
  202. // FIXME: How should this work exactly?
  203. int nFlags = CalcTextureFlags( nDepth );
  204. m_pVTFTexture->Init( nWidth, nHeight, nDepth, srcFormat, nFlags, nFrameCount );
  205. ImageFormat format = ComputeDesiredImageFormat( pImage->Format(), nWidth, nHeight, nDepth, nFlags );
  206. // m_Texture.InitProceduralTexture( GetName(), "DmeTexture", nWidth, nHeight, nDepth, nFrameCount, format, nFlags );
  207. // m_Texture->SetTextureRegenerator( this );
  208. // Fill in the texture bits
  209. }
  210. /*
  211. //-----------------------------------------------------------------------------
  212. // Fill in the texture bits
  213. //-----------------------------------------------------------------------------
  214. void CDmeTexture::RegenerateTextureBits( ITexture *pTexture, IVTFTexture *pVTFTexture, Rect_t *pRect )
  215. {
  216. }
  217. */
  218. //-----------------------------------------------------------------------------
  219. //
  220. // Cube texture
  221. //
  222. //-----------------------------------------------------------------------------
  223. //-----------------------------------------------------------------------------
  224. // Expose this class to the scene database
  225. //-----------------------------------------------------------------------------
  226. IMPLEMENT_ELEMENT_FACTORY( DmeCubeTexture, CDmeCubeTexture );
  227. //-----------------------------------------------------------------------------
  228. // Constructor, destructor
  229. //-----------------------------------------------------------------------------
  230. void CDmeCubeTexture::OnConstruction()
  231. {
  232. m_ImagesPosX.Init( this, "imagesPosX" );
  233. m_ImagesNegX.Init( this, "imagesNegX" );
  234. m_ImagesPosY.Init( this, "imagesPosY" );
  235. m_ImagesNegY.Init( this, "imagesNegY" );
  236. m_ImagesPosZ.Init( this, "imagesPosZ" );
  237. m_ImagesNegZ.Init( this, "imagesNegZ" );
  238. }
  239. void CDmeCubeTexture::OnDestruction()
  240. {
  241. }