Advertisement
Guest User

Untitled

a guest
Jan 4th, 2021
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.77 KB | None | 0 0
  1. //===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7.  
  8. #ifndef VTF_H
  9. #define VTF_H
  10.  
  11. #ifdef _WIN32
  12. #pragma once
  13. #endif
  14.  
  15. #include "bitmap/imageformat.h"
  16.  
  17. // #define XTF_FILE_FORMAT_ONLY to just include the vtf / xtf structures, and none of the code declaration
  18. #ifndef XTF_FILE_FORMAT_ONLY
  19.  
  20. //-----------------------------------------------------------------------------
  21. // Forward declarations
  22. //-----------------------------------------------------------------------------
  23. class CUtlBuffer;
  24. class Vector;
  25. struct Rect_t;
  26.  
  27.  
  28. //-----------------------------------------------------------------------------
  29. // Texture flags
  30. //-----------------------------------------------------------------------------
  31. enum
  32. {
  33. // flags from the *.txt config file
  34. TEXTUREFLAGS_POINTSAMPLE = 0x00000001,
  35. TEXTUREFLAGS_TRILINEAR = 0x00000002,
  36. TEXTUREFLAGS_CLAMPS = 0x00000004,
  37. TEXTUREFLAGS_CLAMPT = 0x00000008,
  38. TEXTUREFLAGS_ANISOTROPIC = 0x00000010,
  39. TEXTUREFLAGS_HINT_DXT5 = 0x00000020,
  40. TEXTUREFLAGS_NOCOMPRESS = 0x00000040,
  41. TEXTUREFLAGS_NORMAL = 0x00000080,
  42. TEXTUREFLAGS_NOMIP = 0x00000100,
  43. TEXTUREFLAGS_NOLOD = 0x00000200,
  44. TEXTUREFLAGS_MINMIP = 0x00000400,
  45. TEXTUREFLAGS_PROCEDURAL = 0x00000800,
  46.  
  47. // These are automatically generated by vtex from the texture data.
  48. TEXTUREFLAGS_ONEBITALPHA = 0x00001000,
  49. TEXTUREFLAGS_EIGHTBITALPHA = 0x00002000,
  50.  
  51. // newer flags from the *.txt config file
  52. TEXTUREFLAGS_ENVMAP = 0x00004000,
  53. TEXTUREFLAGS_RENDERTARGET = 0x00008000,
  54. TEXTUREFLAGS_DEPTHRENDERTARGET = 0x00010000,
  55. TEXTUREFLAGS_NODEBUGOVERRIDE = 0x00020000,
  56. TEXTUREFLAGS_SINGLECOPY = 0x00040000,
  57. TEXTUREFLAGS_ONEOVERMIPLEVELINALPHA = 0x00080000,
  58. TEXTUREFLAGS_PREMULTCOLORBYONEOVERMIPLEVEL = 0x00100000,
  59. TEXTUREFLAGS_NORMALTODUDV = 0x00200000,
  60. TEXTUREFLAGS_ALPHATESTMIPGENERATION = 0x00400000,
  61.  
  62. TEXTUREFLAGS_NODEPTHBUFFER = 0x00800000,
  63.  
  64. TEXTUREFLAGS_NICEFILTERED = 0x01000000,
  65.  
  66. TEXTUREFLAGS_CLAMPU = 0x02000000,
  67.  
  68. // xbox extensions
  69. TEXTUREFLAGS_PRESWIZZLED = 0x04000000,
  70. TEXTUREFLAGS_CACHEABLE = 0x08000000,
  71. TEXTUREFLAGS_UNFILTERABLE_OK = 0x10000000,
  72.  
  73. TEXTUREFLAGS_LASTFLAG = 0x10000000,
  74. };
  75.  
  76.  
  77. //-----------------------------------------------------------------------------
  78. // Cubemap face indices
  79. //-----------------------------------------------------------------------------
  80. enum CubeMapFaceIndex_t
  81. {
  82. CUBEMAP_FACE_RIGHT = 0,
  83. CUBEMAP_FACE_LEFT,
  84. CUBEMAP_FACE_BACK, // NOTE: This face is in the +y direction?!?!?
  85. CUBEMAP_FACE_FRONT, // NOTE: This face is in the -y direction!?!?
  86. CUBEMAP_FACE_UP,
  87. CUBEMAP_FACE_DOWN,
  88.  
  89. // This is the fallback for low-end
  90. CUBEMAP_FACE_SPHEREMAP,
  91.  
  92. // NOTE: Cubemaps have *7* faces; the 7th is the fallback spheremap
  93. CUBEMAP_FACE_COUNT
  94. };
  95.  
  96.  
  97. //-----------------------------------------------------------------------------
  98. // Enumeration used for spheremap generation
  99. //-----------------------------------------------------------------------------
  100. enum LookDir_t
  101. {
  102. LOOK_DOWN_X = 0,
  103. LOOK_DOWN_NEGX,
  104. LOOK_DOWN_Y,
  105. LOOK_DOWN_NEGY,
  106. LOOK_DOWN_Z,
  107. LOOK_DOWN_NEGZ,
  108. };
  109.  
  110.  
  111. //-----------------------------------------------------------------------------
  112. // Use this image format if you want to perform tool operations on the texture
  113. //-----------------------------------------------------------------------------
  114. #define IMAGE_FORMAT_DEFAULT ((ImageFormat)-2)
  115.  
  116.  
  117. //-----------------------------------------------------------------------------
  118. // Interface to get at various bits of a VTF texture
  119. //-----------------------------------------------------------------------------
  120. class IVTFTexture
  121. {
  122. public:
  123. // Initializes the texture and allocates space for the bits
  124. // In most cases, you shouldn't force the mip count.
  125. virtual bool Init( int nWidth, int nHeight, int nDepth, ImageFormat fmt, int nFlags, int iFrameCount, int nForceMipCount = -1 ) = 0;
  126.  
  127. // Methods to set other texture fields
  128. virtual void SetBumpScale( float flScale ) = 0;
  129. virtual void SetReflectivity( const Vector &vecReflectivity ) = 0;
  130.  
  131. // Methods to initialize the low-res image
  132. virtual void InitLowResImage( int nWidth, int nHeight, ImageFormat fmt ) = 0;
  133.  
  134. // When unserializing, we can skip a certain number of mip levels,
  135. // and we also can just load everything but the image data
  136. // NOTE: If you load only the buffer header, you'll need to use the
  137. // VTFBufferHeaderSize() method below to only read that much from the file
  138. // NOTE: If you skip mip levels, the height + width of the texture will
  139. // change to reflect the size of the largest read in mip level
  140. #ifndef _XBOX
  141. virtual bool Unserialize( CUtlBuffer &buf, bool bBufferHeaderOnly = false, int nSkipMipLevels = 0 ) = 0;
  142. virtual bool Serialize( CUtlBuffer &buf ) = 0;
  143. #else
  144. virtual bool Unserialize( CUtlBuffer &buf, bool bBufferHeaderOnly, bool bStaticDataOnly ) = 0;
  145. #endif
  146.  
  147. // These are methods to help with optimization:
  148. // Once the header is read in, they indicate where to start reading
  149. // other data (measured from file start), and how many bytes to read....
  150. virtual void LowResFileInfo( int *pStartLocation, int *pSizeInBytes) const = 0;
  151. virtual void ImageFileInfo( int nFrame, int nFace, int nMip, int *pStartLocation, int *pSizeInBytes) const = 0;
  152. #ifndef _XBOX
  153. virtual int FileSize( int nMipSkipCount = 0 ) const = 0;
  154. #else
  155. virtual int FileSize( bool bStaticOnly ) const = 0;
  156. virtual int FileImageDataOffset( void ) const = 0;
  157. virtual int FileImageDataLength( int numFrames ) const = 0;
  158. #endif
  159.  
  160. // Attributes...
  161. virtual int Width() const = 0;
  162. virtual int Height() const = 0;
  163. virtual int Depth() const = 0;
  164. virtual int MipCount() const = 0;
  165.  
  166. // returns the size of one row of a particular mip level
  167. virtual int RowSizeInBytes( int nMipLevel ) const = 0;
  168.  
  169. // returns the size of one face of a particular mip level
  170. virtual int FaceSizeInBytes( int nMipLevel ) const = 0;
  171.  
  172. virtual ImageFormat Format() const = 0;
  173. virtual int FaceCount() const = 0;
  174. virtual int FrameCount() const = 0;
  175. virtual int Flags() const = 0;
  176.  
  177. virtual float BumpScale() const = 0;
  178.  
  179. virtual int LowResWidth() const = 0;
  180. virtual int LowResHeight() const = 0;
  181. virtual ImageFormat LowResFormat() const = 0;
  182.  
  183. #ifdef _XBOX
  184. virtual int FallbackWidth() const = 0;
  185. virtual int FallbackHeight() const = 0;
  186. virtual int FallbackMipCount() const = 0;
  187.  
  188. virtual int MappingWidth() const = 0;
  189. virtual int MappingHeight() const = 0;
  190. virtual int MipSkipCount() const = 0;
  191. #endif
  192.  
  193. // NOTE: reflectivity[0] = blue, [1] = greem, [2] = red
  194. virtual const Vector &Reflectivity() const = 0;
  195.  
  196. virtual bool IsCubeMap() const = 0;
  197. virtual bool IsNormalMap() const = 0;
  198. virtual bool IsVolumeTexture() const = 0;
  199. #ifdef _XBOX
  200. virtual bool IsPreSwizzled() const = 0;
  201. #endif
  202.  
  203. // Computes the dimensions of a particular mip level
  204. virtual void ComputeMipLevelDimensions( int iMipLevel, int *pMipWidth, int *pMipHeight, int *pMipDepth ) const = 0;
  205.  
  206. #ifdef _XBOX
  207. virtual void ComputeFallbackMipDimensions( int iMipLevel, int *pMipWidth, int *pMipHeight ) const = 0;
  208. #endif
  209. // Computes the size (in bytes) of a single mipmap of a single face of a single frame
  210. virtual int ComputeMipSize( int iMipLevel ) const = 0;
  211.  
  212. // Computes the size of a subrect (specified at the top mip level) at a particular lower mip level
  213. virtual void ComputeMipLevelSubRect( Rect_t* pSrcRect, int nMipLevel, Rect_t *pSubRect ) const = 0;
  214.  
  215. // Computes the size (in bytes) of a single face of a single frame
  216. // All mip levels starting at the specified mip level are included
  217. virtual int ComputeFaceSize( int iStartingMipLevel = 0 ) const = 0;
  218.  
  219. // Computes the total size (in bytes) of all faces, all frames
  220. virtual int ComputeTotalSize() const = 0;
  221.  
  222. // Returns the base address of the image data
  223. virtual unsigned char *ImageData() = 0;
  224.  
  225. // Returns a pointer to the data associated with a particular frame, face, and mip level
  226. virtual unsigned char *ImageData( int iFrame, int iFace, int iMipLevel ) = 0;
  227.  
  228. // Returns a pointer to the data associated with a particular frame, face, mip level, and offset
  229. virtual unsigned char *ImageData( int iFrame, int iFace, int iMipLevel, int x, int y, int z = 0 ) = 0;
  230.  
  231. #ifdef _XBOX
  232. // Returns a pointer to the palette data associated with a particular frame
  233. virtual unsigned char *PaletteData( int iFrame ) = 0;
  234.  
  235. // Returns a pointer to the fallback data associated with a particular frame, face, and mip level
  236. virtual unsigned char *FallbackImageData( int iFrame, int iFace, int iMipLevel ) = 0;
  237. #endif
  238. // Returns the base address of the low-res image data
  239. virtual unsigned char *LowResImageData() = 0;
  240.  
  241. // Converts the textures image format. Use IMAGE_FORMAT_DEFAULT
  242. // if you want to be able to use various tool functions below
  243. virtual void ConvertImageFormat( ImageFormat fmt, bool bNormalToDUDV ) = 0;
  244.  
  245. // NOTE: The following methods only work on textures using the
  246. // IMAGE_FORMAT_DEFAULT!
  247.  
  248. // Generate spheremap based on the current cube faces (only works for cubemaps)
  249. // The look dir indicates the direction of the center of the sphere
  250. // NOTE: Only call this *after* cube faces have been correctly
  251. // oriented (using FixCubemapFaceOrientation)
  252. #ifndef _XBOX
  253. virtual void GenerateSpheremap( LookDir_t lookDir = LOOK_DOWN_Z ) = 0;
  254.  
  255. // Generate spheremap based on the current cube faces (only works for cubemaps)
  256. // The look dir indicates the direction of the center of the sphere
  257. // NOTE: Only call this *after* cube faces have been correctly
  258. // oriented (using FixCubemapFaceOrientation)
  259. virtual void GenerateHemisphereMap( unsigned char *pSphereMapBitsRGBA, int targetWidth,
  260. int targetHeight, LookDir_t lookDir, int iFrame ) = 0;
  261.  
  262. // Fixes the cubemap faces orientation from our standard to the
  263. // standard the material system needs.
  264. virtual void FixCubemapFaceOrientation( ) = 0;
  265.  
  266. // Generates mipmaps from the base mip levels
  267. virtual void GenerateMipmaps() = 0;
  268.  
  269. // Put 1/miplevel (1..n) into alpha.
  270. virtual void PutOneOverMipLevelInAlpha() = 0;
  271.  
  272. // Computes the reflectivity
  273. virtual void ComputeReflectivity( ) = 0;
  274.  
  275. // Computes the alpha flags
  276. virtual void ComputeAlphaFlags() = 0;
  277.  
  278. // Generate the low-res image bits
  279. virtual bool ConstructLowResImage() = 0;
  280.  
  281. // Gets the texture all internally consistent assuming you've loaded
  282. // mip 0 of all faces of all frames
  283. virtual void PostProcess(bool bGenerateSpheremap, LookDir_t lookDir = LOOK_DOWN_Z, bool bAllowFixCubemapOrientation = true) = 0;
  284.  
  285. // Blends adjacent pixels on cubemap borders, since the card doesn't do it. If the texture
  286. // is S3TC compressed, then it has to do it AFTER the texture has been compressed to prevent
  287. // artifacts along the edges.
  288. //
  289. // If bSkybox is true, it assumes the faces are oriented in the way the engine draws the skybox
  290. // (which happens to be different from the way cubemaps have their faces).
  291. virtual void MatchCubeMapBorders( int iStage, ImageFormat finalFormat, bool bSkybox ) = 0;
  292.  
  293. // Sets threshhold values for alphatest mipmapping
  294. virtual void SetAlphaTestThreshholds( float flBase, float flHighFreq ) = 0;
  295. #endif
  296.  
  297. #ifdef _XBOX
  298. virtual void ReleaseMemory() = 0;
  299. #endif
  300. };
  301.  
  302.  
  303. //-----------------------------------------------------------------------------
  304. // Class factory
  305. //-----------------------------------------------------------------------------
  306. IVTFTexture *CreateVTFTexture();
  307. void DestroyVTFTexture( IVTFTexture *pTexture );
  308.  
  309.  
  310. //-----------------------------------------------------------------------------
  311. // Allows us to only load in the first little bit of the VTF file to get info
  312. // Clients should read this much into a UtlBuffer and then pass it in to
  313. // Unserialize
  314. //-----------------------------------------------------------------------------
  315. int VTFFileHeaderSize( int nMajorVersion = -1, int nMinorVersion = -1 );
  316. #include "vector.h"
  317.  
  318. #endif // XTF_FILE_FORMAT_ONLY
  319.  
  320. //-----------------------------------------------------------------------------
  321. // Disk format for VTF files
  322. //
  323. // NOTE: After the header is the low-res image data
  324. // Then follows image data, which is sorted in the following manner
  325. //
  326. // for each mip level (starting with 1x1, and getting larger)
  327. // for each animation frame
  328. // for each face
  329. // store the image data for the face
  330. //
  331. // NOTE: In memory, we store the data in the following manner:
  332. // for each animation frame
  333. // for each face
  334. // for each mip level (starting with the largest, and getting smaller)
  335. // store the image data for the face
  336. //
  337. // This is done because the various image manipulation function we have
  338. // expect this format
  339. //-----------------------------------------------------------------------------
  340.  
  341. #pragma pack(1)
  342.  
  343. // version number for the disk texture cache
  344. #define VTF_MAJOR_VERSION 7
  345. #define VTF_MINOR_VERSION 2
  346.  
  347. struct VTFFileBaseHeader_t
  348. {
  349. char fileTypeString[4]; // "VTF" Valve texture file
  350. int version[2]; // version[0].version[1]
  351. int headerSize;
  352. };
  353.  
  354. struct VTFFileHeaderV7_1_t : public VTFFileBaseHeader_t
  355. {
  356. unsigned short width;
  357. unsigned short height;
  358. unsigned int flags;
  359. unsigned short numFrames;
  360. unsigned short startFrame;
  361. VectorAligned reflectivity; // This is a linear value, right? Average of all frames?
  362. float bumpScale;
  363. ImageFormat imageFormat;
  364. unsigned char numMipLevels;
  365. ImageFormat lowResImageFormat;
  366. unsigned char lowResImageWidth;
  367. unsigned char lowResImageHeight;
  368. };
  369.  
  370. struct VTFFileHeader_t : public VTFFileHeaderV7_1_t
  371. {
  372. unsigned short depth;
  373. };
  374.  
  375.  
  376. #define XTF_MAJOR_VERSION 5
  377. #define XTF_MINOR_VERSION 0
  378.  
  379. struct XTFFileHeader_t : public VTFFileBaseHeader_t
  380. {
  381. unsigned int flags;
  382. unsigned short width; // actual width
  383. unsigned short height; // actual height
  384. unsigned short depth; // always 1
  385. unsigned short numFrames;
  386. unsigned short preloadDataSize; // exact size of preload data
  387. unsigned short imageDataOffset; // aligned to sector size
  388. Vector reflectivity; // Resides on 16 byte boundary!
  389. float bumpScale;
  390. ImageFormat imageFormat;
  391. unsigned char lowResImageWidth;
  392. unsigned char lowResImageHeight;
  393. unsigned char fallbackImageWidth;
  394. unsigned char fallbackImageHeight;
  395. unsigned char mipSkipCount; // used to resconstruct mapping dimensions
  396. unsigned char pad; // for alignment
  397. };
  398.  
  399. #pragma pack()
  400.  
  401. #endif // VTF_H
  402.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement