Texture.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. #ifndef SE_INCL_TEXTURE_H
  13. #define SE_INCL_TEXTURE_H
  14. #ifdef PRAGMA_ONCE
  15. #pragma once
  16. #endif
  17. #include <Engine/Base/Lists.h>
  18. #include <Engine/Base/Anim.h>
  19. #include <Engine/Graphics/GfxLibrary.h>
  20. #define BYTES_PER_TEXEL 4 // all textures in engine are 4 bytes per pixel
  21. // td_ulFlags bits
  22. // (on disk and in memory)
  23. #define TEX_ALPHACHANNEL (1UL<<0) // texture has alpha channel (for old version support)
  24. #define TEX_32BIT (1UL<<1) // texture needs to be in 32-bit quality uploaded if can
  25. // (only in memory)
  26. #define TEX_STATIC (1UL<<5) // remain loaded after being bound (i.e. uploaded - for base textures)
  27. #define TEX_CONSTANT (1UL<<6) // cannot be changed (no mip-map disposing, no LOD biasing, no colorizing, nothing!)
  28. #define TEX_TRANSPARENT (1UL<<7) // only one bit of alpha channel is enough (internal format GL_RGB5_A1)
  29. #define TEX_EQUALIZED (1UL<<8) // texture has 128-gray last mipmap (i.e. can be discarded in shade mode)
  30. #define TEX_GRAY (1UL<<9) // grayscale texture
  31. #define TEX_WHITE (1UL<<10) // completely white texture (believe me, there are some cases)
  32. #define TEX_KEEPCOLOR (1UL<<11) // don't (de)saturate (for heightmaps and such!)
  33. #define TEX_SINGLEMIPMAP (1UL<<18) // set if last uploading was in single-mipmap
  34. #define TEX_PROBED (1UL<<19) // set if last binding was as probe-texture
  35. // (flags that shows if texture mipmaps has been changed)
  36. #define TEX_DISPOSED (1UL<<20) // largest mip-map(s) has been left-out
  37. #define TEX_DITHERED (1UL<<21) // dithering has been applied on this texture
  38. #define TEX_FILTERED (1UL<<22) // flitering has been applied on this texture
  39. #define TEX_SATURATED (1UL<<23) // saturation has been adjusted on this texture
  40. #define TEX_COLORIZED (1UL<<24) // mipmaps has been colorized on this texture
  41. #define TEX_WASOLD (1UL<<30) // loaded from old format (version 3)
  42. /*
  43. * Bitmap data for a class of texture objects
  44. */
  45. class ENGINE_API CTextureData : public CAnimData {
  46. public:
  47. // implementation:
  48. ULONG td_ulFlags; // see defines
  49. MEX td_mexWidth, td_mexHeight; // texture dimensions
  50. INDEX td_iFirstMipLevel; // the highest quality mip level
  51. INDEX td_ctFineMipLevels; // number of bilineary created mip levels
  52. SLONG td_slFrameSize; // sum of sizes of all mip-maps for one frame
  53. INDEX td_ctFrames; // number of different frames
  54. class CTexParams td_tpLocal; // local texture parameters
  55. ULONG td_ulInternalFormat; // format in which texture will be uploaded
  56. CTimerValue td_tvLastDrawn; // timer for probing
  57. ULONG td_ulProbeObject;
  58. union {
  59. ULONG td_ulObject;
  60. ULONG *td_pulObjects;
  61. };
  62. ULONG *td_pulFrames; // all frames with their mip-maps and private palettes
  63. UBYTE *td_pubBuffer1, *td_pubBuffer2; // buffers for effect textures
  64. PIX td_pixBufferWidth, td_pixBufferHeight; // effect buffer dimensions
  65. class CTextureData *td_ptdBaseTexture; // base texure for effects (if any)
  66. class CTextureEffectGlobal *td_ptegEffect; // all data for effect textures
  67. INDEX td_iRenderFrame; // frame number currently rendering (for profiling)
  68. // constructor and destructor
  69. CTextureData();
  70. ~CTextureData();
  71. // reference counting (override from CAnimData)
  72. void RemReference_internal(void);
  73. // converts global mip level to the corresponding one of texture
  74. INDEX ClampMipLevel( FLOAT fMipFactor) const;
  75. // gets values from some of texture data members
  76. inline MEX GetWidth(void) const { return td_mexWidth; };
  77. inline MEX GetHeight(void) const { return td_mexHeight; };
  78. inline PIX GetPixWidth(void) const { return td_mexWidth >>td_iFirstMipLevel; };
  79. inline PIX GetPixHeight(void) const { return td_mexHeight>>td_iFirstMipLevel; };
  80. inline ULONG GetFlags(void) const { return td_ulFlags; };
  81. inline ULONG GetNoOfMips(void) const { return GetNoOfMipmaps( GetPixWidth(), GetPixHeight()); };
  82. inline ULONG GetNoOfFineMips(void) const { return td_ctFineMipLevels; };
  83. // mark that texture has been used
  84. inline void MarkDrawn(void) { td_tvLastDrawn = _pTimer->GetHighPrecisionTimer(); };
  85. // get string description of texture size, mips and parameters
  86. CTString GetDescription(void);
  87. // sets new texture mex width and changes height remaining texture's aspect ratio
  88. inline void ChangeSize( MEX mexNewWidth) {
  89. td_mexHeight = MEX( ((FLOAT)mexNewWidth)/td_mexWidth * td_mexHeight);
  90. td_mexWidth = mexNewWidth;
  91. };
  92. // check if texture frame(s) has been somehow altered (dithering, filtering, saturation, colorizing...)
  93. inline BOOL IsModified(void) {
  94. return td_ulFlags & (TEX_DISPOSED|TEX_DITHERED|TEX_FILTERED|TEX_SATURATED|TEX_COLORIZED);
  95. };
  96. // export finest mipmap of one texture's frame to imageinfo
  97. void Export_t( class CImageInfo &iiExportedImage, INDEX iFrame);
  98. // set texture frame as current for accelerator (this will upload texture that needs or wants uploading)
  99. void SetAsCurrent( INDEX iFrameNo=0, BOOL bForceUpload=FALSE);
  100. // creates new effect texture with one frame
  101. void CreateEffectTexture( PIX pixWidth, PIX pixHeight, MEX mexWidth,
  102. CTextureData *ptdBaseTexture, ULONG ulGlobalEffect);
  103. // creates new texture with one frame
  104. void Create_t( const CImageInfo *pII, MEX mexWanted, INDEX ctFineMips, BOOL bForce32bit);
  105. // adds one frame to created texture
  106. void AddFrame_t( const CImageInfo *pII);
  107. // remove texture from gfx API (driver)
  108. void Unbind(void);
  109. // free memory allocated for texture
  110. void Clear(void);
  111. // read texture from file
  112. void Read_t( CTStream *inFile);
  113. // write texture to file
  114. void Write_t( CTStream *outFile);
  115. // force texture to be re-loaded (if needed) in corresponding manner
  116. void Force( ULONG ulTexFlags);
  117. // get texel from texture's largest mip-map
  118. COLOR GetTexel( MEX mexU, MEX mexV);
  119. // copy (and eventually convert to floats) one row from texture to an array (iChannel is 1=R,2=G,3=B,4=A)
  120. void FetchRow( PIX pixRow, void *pfDst, INDEX iChannel=4, BOOL bConvertToFloat=TRUE);
  121. // get pointer to one row of texture
  122. ULONG *GetRowPointer( PIX pixRow);
  123. // overridden from CSerial:
  124. // check if this kind of objects is auto-freed
  125. virtual BOOL IsAutoFreed(void);
  126. // get amount of memory used by this object
  127. virtual SLONG GetUsedMemory(void);
  128. };
  129. /*
  130. * An instance of a texture object
  131. */
  132. class ENGINE_API CTextureObject : public CAnimObject {
  133. // implementation:
  134. public:
  135. // interface:
  136. public:
  137. CTextureObject(void);
  138. // copy from another object of same class
  139. void Copy(CTextureObject &toOther);
  140. MEX GetWidth(void) const;
  141. MEX GetHeight(void) const;
  142. ULONG GetFlags(void) const;
  143. void Read_t( CTStream *istrFile); // throw char * // read and
  144. void Write_t( CTStream *ostrFile); // throw char * // write functions
  145. // obtain texture and set it for this object
  146. void SetData_t(const CTFileName &fnmTexture); // throw char *
  147. // get filename of texture or empty string if no texture
  148. const CTFileName &GetName(void);
  149. };
  150. ENGINE_API extern void CreateTexture_t( const CTFileName &inFileName,
  151. MEX inMex, INDEX inMipmaps, BOOL bForce32bit);
  152. ENGINE_API extern void CreateTexture_t( const CTFileName &inFileName, const CTFileName &outFileName,
  153. MEX inMex, INDEX inMipmaps, BOOL bForce32bit);
  154. ENGINE_API extern void ProcessScript_t( const CTFileName &inFileName);
  155. /*
  156. * Render-to-texture class
  157. */
  158. /*
  159. class ENGINE_API CRenderTexture
  160. {
  161. // implementation:
  162. public:
  163. CListNode rt_lnInGfx // for linking in list of all renderable textures
  164. ULONG rt_ulFlags; // see defines (only alpha and 32bit, for now)
  165. PIX tt_pixWidth, rt_pixHeight; // texture dimensions
  166. ULONG td_ulInternalFormat; // format in which texture will be uploaded
  167. ULONG *rt_pulImage; // image in memory (no mipmaps for now!)
  168. class CTexParams td_tpLocal; // local texture parameters
  169. // interface:
  170. public:
  171. CRenderTexture(void);
  172. ~CRenderTexture(void);
  173. // prepare
  174. BOOL Init( PIX pixWidth, PIX pixHeight, BOOL b32bit, BOOL bAlpha=FALSE);
  175. // reset (i.e. prepare again - after display mode switch and stuff like that)
  176. void Reset(void);
  177. // set texture as current for accelerator
  178. void SetAsCurrent(void);
  179. // set texture as target for rendering
  180. void SetAsTarget(void);
  181. };
  182. */
  183. #endif /* include-once check. */