1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2013 Torus Knot Software Ltd |
---|
8 | |
---|
9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
10 | of this software and associated documentation files (the "Software"), to deal |
---|
11 | in the Software without restriction, including without limitation the rights |
---|
12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
13 | copies of the Software, and to permit persons to whom the Software is |
---|
14 | furnished to do so, subject to the following conditions: |
---|
15 | |
---|
16 | The above copyright notice and this permission notice shall be included in |
---|
17 | all copies or substantial portions of the Software. |
---|
18 | |
---|
19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
25 | THE SOFTWARE. |
---|
26 | ----------------------------------------------------------------------------- |
---|
27 | */ |
---|
28 | #ifndef _Texture_H__ |
---|
29 | #define _Texture_H__ |
---|
30 | |
---|
31 | #include "OgrePrerequisites.h" |
---|
32 | #include "OgreHardwareBuffer.h" |
---|
33 | #include "OgreResource.h" |
---|
34 | #include "OgreImage.h" |
---|
35 | #include "OgreHeaderPrefix.h" |
---|
36 | |
---|
37 | namespace Ogre { |
---|
38 | |
---|
39 | /** \addtogroup Core |
---|
40 | * @{ |
---|
41 | */ |
---|
42 | /** \addtogroup Resources |
---|
43 | * @{ |
---|
44 | */ |
---|
45 | /** Enum identifying the texture usage |
---|
46 | */ |
---|
47 | enum TextureUsage |
---|
48 | { |
---|
49 | /// @copydoc HardwareBuffer::Usage |
---|
50 | TU_STATIC = HardwareBuffer::HBU_STATIC, |
---|
51 | TU_DYNAMIC = HardwareBuffer::HBU_DYNAMIC, |
---|
52 | TU_WRITE_ONLY = HardwareBuffer::HBU_WRITE_ONLY, |
---|
53 | TU_STATIC_WRITE_ONLY = HardwareBuffer::HBU_STATIC_WRITE_ONLY, |
---|
54 | TU_DYNAMIC_WRITE_ONLY = HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY, |
---|
55 | TU_DYNAMIC_WRITE_ONLY_DISCARDABLE = HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE, |
---|
56 | /// Mipmaps will be automatically generated for this texture |
---|
57 | TU_AUTOMIPMAP = 16, |
---|
58 | /** This texture will be a render target, i.e. used as a target for render to texture |
---|
59 | setting this flag will ignore all other texture usages except TU_AUTOMIPMAP */ |
---|
60 | TU_RENDERTARGET = 32, |
---|
61 | /// Default to automatic mipmap generation static textures |
---|
62 | TU_DEFAULT = TU_AUTOMIPMAP | TU_STATIC_WRITE_ONLY |
---|
63 | }; |
---|
64 | |
---|
65 | /** Enum identifying the texture type |
---|
66 | */ |
---|
67 | enum TextureType |
---|
68 | { |
---|
69 | /// 1D texture, used in combination with 1D texture coordinates |
---|
70 | TEX_TYPE_1D = 1, |
---|
71 | /// 2D texture, used in combination with 2D texture coordinates (default) |
---|
72 | TEX_TYPE_2D = 2, |
---|
73 | /// 3D volume texture, used in combination with 3D texture coordinates |
---|
74 | TEX_TYPE_3D = 3, |
---|
75 | /// 3D cube map, used in combination with 3D texture coordinates |
---|
76 | TEX_TYPE_CUBE_MAP = 4, |
---|
77 | /// 2D texture array |
---|
78 | TEX_TYPE_2D_ARRAY = 5, |
---|
79 | /// 2D non-square texture, used in combination with 2D texture coordinates |
---|
80 | TEX_TYPE_2D_RECT = 6 |
---|
81 | }; |
---|
82 | |
---|
83 | /** Enum identifying special mipmap numbers |
---|
84 | */ |
---|
85 | enum TextureMipmap |
---|
86 | { |
---|
87 | /// Generate mipmaps up to 1x1 |
---|
88 | MIP_UNLIMITED = 0x7FFFFFFF, |
---|
89 | /// Use TextureManager default |
---|
90 | MIP_DEFAULT = -1 |
---|
91 | }; |
---|
92 | |
---|
93 | /** Abstract class representing a Texture resource. |
---|
94 | @remarks |
---|
95 | The actual concrete subclass which will exist for a texture |
---|
96 | is dependent on the rendering system in use (Direct3D, OpenGL etc). |
---|
97 | This class represents the commonalities, and is the one 'used' |
---|
98 | by programmers even though the real implementation could be |
---|
99 | different in reality. Texture objects are created through |
---|
100 | the 'create' method of the TextureManager concrete subclass. |
---|
101 | */ |
---|
102 | class _OgreExport Texture : public Resource |
---|
103 | { |
---|
104 | public: |
---|
105 | Texture(ResourceManager* creator, const String& name, ResourceHandle handle, |
---|
106 | const String& group, bool isManual = false, ManualResourceLoader* loader = 0); |
---|
107 | |
---|
108 | virtual ~Texture() {} |
---|
109 | |
---|
110 | /** Sets the type of texture; can only be changed before load() |
---|
111 | */ |
---|
112 | virtual void setTextureType(TextureType ttype ) { mTextureType = ttype; } |
---|
113 | |
---|
114 | /** Gets the type of texture |
---|
115 | */ |
---|
116 | virtual TextureType getTextureType(void) const { return mTextureType; } |
---|
117 | |
---|
118 | /** Gets the number of mipmaps to be used for this texture. |
---|
119 | */ |
---|
120 | virtual uint8 getNumMipmaps(void) const {return mNumMipmaps;} |
---|
121 | |
---|
122 | /** Sets the number of mipmaps to be used for this texture. |
---|
123 | @note |
---|
124 | Must be set before calling any 'load' method. |
---|
125 | */ |
---|
126 | virtual void setNumMipmaps(uint8 num) {mNumRequestedMipmaps = mNumMipmaps = num;} |
---|
127 | |
---|
128 | /** Are mipmaps hardware generated? |
---|
129 | @remarks |
---|
130 | Will only be accurate after texture load, or createInternalResources |
---|
131 | */ |
---|
132 | virtual bool getMipmapsHardwareGenerated(void) const { return mMipmapsHardwareGenerated; } |
---|
133 | |
---|
134 | /** Returns the gamma adjustment factor applied to this texture on loading. |
---|
135 | */ |
---|
136 | virtual float getGamma(void) const { return mGamma; } |
---|
137 | |
---|
138 | /** Sets the gamma adjustment factor applied to this texture on loading the |
---|
139 | data. |
---|
140 | @note |
---|
141 | Must be called before any 'load' method. This gamma factor will |
---|
142 | be premultiplied in and may reduce the precision of your textures. |
---|
143 | You can use setHardwareGamma if supported to apply gamma on |
---|
144 | sampling the texture instead. |
---|
145 | */ |
---|
146 | virtual void setGamma(float g) { mGamma = g; } |
---|
147 | |
---|
148 | /** Sets whether this texture will be set up so that on sampling it, |
---|
149 | hardware gamma correction is applied. |
---|
150 | @remarks |
---|
151 | 24-bit textures are often saved in gamma colour space; this preserves |
---|
152 | precision in the 'darks'. However, if you're performing blending on |
---|
153 | the sampled colours, you really want to be doing it in linear space. |
---|
154 | One way is to apply a gamma correction value on loading (see setGamma), |
---|
155 | but this means you lose precision in those dark colours. An alternative |
---|
156 | is to get the hardware to do the gamma correction when reading the |
---|
157 | texture and converting it to a floating point value for the rest of |
---|
158 | the pipeline. This option allows you to do that; it's only supported |
---|
159 | in relatively recent hardware (others will ignore it) but can improve |
---|
160 | the quality of colour reproduction. |
---|
161 | @note |
---|
162 | Must be called before any 'load' method since it may affect the |
---|
163 | construction of the underlying hardware resources. |
---|
164 | Also note this only useful on textures using 8-bit colour channels. |
---|
165 | */ |
---|
166 | virtual void setHardwareGammaEnabled(bool enabled) { mHwGamma = enabled; } |
---|
167 | |
---|
168 | /** Gets whether this texture will be set up so that on sampling it, |
---|
169 | hardware gamma correction is applied. |
---|
170 | */ |
---|
171 | virtual bool isHardwareGammaEnabled() const { return mHwGamma; } |
---|
172 | |
---|
173 | /** Set the level of multisample AA to be used if this texture is a |
---|
174 | rendertarget. |
---|
175 | @note This option will be ignored if TU_RENDERTARGET is not part of the |
---|
176 | usage options on this texture, or if the hardware does not support it. |
---|
177 | @param fsaa The number of samples |
---|
178 | @param fsaaHint Any hinting text (@see Root::createRenderWindow) |
---|
179 | */ |
---|
180 | virtual void setFSAA(uint fsaa, const String& fsaaHint) { mFSAA = fsaa; mFSAAHint = fsaaHint; } |
---|
181 | |
---|
182 | /** Get the level of multisample AA to be used if this texture is a |
---|
183 | rendertarget. |
---|
184 | */ |
---|
185 | virtual uint getFSAA() const { return mFSAA; } |
---|
186 | |
---|
187 | /** Get the multisample AA hint if this texture is a rendertarget. |
---|
188 | */ |
---|
189 | virtual const String& getFSAAHint() const { return mFSAAHint; } |
---|
190 | |
---|
191 | /** Returns the height of the texture. |
---|
192 | */ |
---|
193 | virtual uint32 getHeight(void) const { return mHeight; } |
---|
194 | |
---|
195 | /** Returns the width of the texture. |
---|
196 | */ |
---|
197 | virtual uint32 getWidth(void) const { return mWidth; } |
---|
198 | |
---|
199 | /** Returns the depth of the texture (only applicable for 3D textures). |
---|
200 | */ |
---|
201 | virtual uint32 getDepth(void) const { return mDepth; } |
---|
202 | |
---|
203 | /** Returns the height of the original input texture (may differ due to hardware requirements). |
---|
204 | */ |
---|
205 | virtual uint32 getSrcHeight(void) const { return mSrcHeight; } |
---|
206 | |
---|
207 | /** Returns the width of the original input texture (may differ due to hardware requirements). |
---|
208 | */ |
---|
209 | virtual uint32 getSrcWidth(void) const { return mSrcWidth; } |
---|
210 | |
---|
211 | /** Returns the original depth of the input texture (only applicable for 3D textures). |
---|
212 | */ |
---|
213 | virtual uint32 getSrcDepth(void) const { return mSrcDepth; } |
---|
214 | |
---|
215 | /** Set the height of the texture; can only do this before load(); |
---|
216 | */ |
---|
217 | virtual void setHeight(uint32 h) { mHeight = mSrcHeight = h; } |
---|
218 | |
---|
219 | /** Set the width of the texture; can only do this before load(); |
---|
220 | */ |
---|
221 | virtual void setWidth(uint32 w) { mWidth = mSrcWidth = w; } |
---|
222 | |
---|
223 | /** Set the depth of the texture (only applicable for 3D textures); |
---|
224 | can only do this before load(); |
---|
225 | */ |
---|
226 | virtual void setDepth(uint32 d) { mDepth = mSrcDepth = d; } |
---|
227 | |
---|
228 | /** Returns the TextureUsage identifier for this Texture |
---|
229 | */ |
---|
230 | virtual int getUsage() const |
---|
231 | { |
---|
232 | return mUsage; |
---|
233 | } |
---|
234 | |
---|
235 | /** Sets the TextureUsage identifier for this Texture; only useful before load() |
---|
236 | |
---|
237 | @param u is a combination of TU_STATIC, TU_DYNAMIC, TU_WRITE_ONLY |
---|
238 | TU_AUTOMIPMAP and TU_RENDERTARGET (see TextureUsage enum). You are |
---|
239 | strongly advised to use HBU_STATIC_WRITE_ONLY wherever possible, if you need to |
---|
240 | update regularly, consider HBU_DYNAMIC_WRITE_ONLY. |
---|
241 | */ |
---|
242 | virtual void setUsage(int u) { mUsage = u; } |
---|
243 | |
---|
244 | /** Creates the internal texture resources for this texture. |
---|
245 | @remarks |
---|
246 | This method creates the internal texture resources (pixel buffers, |
---|
247 | texture surfaces etc) required to begin using this texture. You do |
---|
248 | not need to call this method directly unless you are manually creating |
---|
249 | a texture, in which case something must call it, after having set the |
---|
250 | size and format of the texture (e.g. the ManualResourceLoader might |
---|
251 | be the best one to call it). If you are not defining a manual texture, |
---|
252 | or if you use one of the self-contained load...() methods, then it will be |
---|
253 | called for you. |
---|
254 | */ |
---|
255 | virtual void createInternalResources(void); |
---|
256 | |
---|
257 | /** Frees internal texture resources for this texture. |
---|
258 | */ |
---|
259 | virtual void freeInternalResources(void); |
---|
260 | |
---|
261 | /** Copies (and maybe scales to fit) the contents of this texture to |
---|
262 | another texture. */ |
---|
263 | virtual void copyToTexture( TexturePtr& target ); |
---|
264 | |
---|
265 | /** Loads the data from an image. |
---|
266 | @note Important: only call this from outside the load() routine of a |
---|
267 | Resource. Don't call it within (including ManualResourceLoader) - use |
---|
268 | _loadImages() instead. This method is designed to be external, |
---|
269 | performs locking and checks the load status before loading. |
---|
270 | */ |
---|
271 | virtual void loadImage( const Image &img ); |
---|
272 | |
---|
273 | /** Loads the data from a raw stream. |
---|
274 | @note Important: only call this from outside the load() routine of a |
---|
275 | Resource. Don't call it within (including ManualResourceLoader) - use |
---|
276 | _loadImages() instead. This method is designed to be external, |
---|
277 | performs locking and checks the load status before loading. |
---|
278 | @param stream Data stream containing the raw pixel data |
---|
279 | @param uWidth Width of the image |
---|
280 | @param uHeight Height of the image |
---|
281 | @param eFormat The format of the pixel data |
---|
282 | */ |
---|
283 | virtual void loadRawData( DataStreamPtr& stream, |
---|
284 | ushort uWidth, ushort uHeight, PixelFormat eFormat); |
---|
285 | |
---|
286 | /** Internal method to load the texture from a set of images. |
---|
287 | @note Do NOT call this method unless you are inside the load() routine |
---|
288 | already, e.g. a ManualResourceLoader. It is not threadsafe and does |
---|
289 | not check or update resource loading status. |
---|
290 | */ |
---|
291 | virtual void _loadImages( const ConstImagePtrList& images ); |
---|
292 | |
---|
293 | /** Returns the pixel format for the texture surface. */ |
---|
294 | virtual PixelFormat getFormat() const |
---|
295 | { |
---|
296 | return mFormat; |
---|
297 | } |
---|
298 | |
---|
299 | /** Returns the desired pixel format for the texture surface. */ |
---|
300 | virtual PixelFormat getDesiredFormat(void) const |
---|
301 | { |
---|
302 | return mDesiredFormat; |
---|
303 | } |
---|
304 | |
---|
305 | /** Returns the pixel format of the original input texture (may differ due to |
---|
306 | hardware requirements and pixel format conversion). |
---|
307 | */ |
---|
308 | virtual PixelFormat getSrcFormat(void) const |
---|
309 | { |
---|
310 | return mSrcFormat; |
---|
311 | } |
---|
312 | |
---|
313 | /** Sets the pixel format for the texture surface; can only be set before load(). */ |
---|
314 | virtual void setFormat(PixelFormat pf); |
---|
315 | |
---|
316 | /** Returns true if the texture has an alpha layer. */ |
---|
317 | virtual bool hasAlpha(void) const; |
---|
318 | |
---|
319 | /** Sets desired bit depth for integer pixel format textures. |
---|
320 | @note |
---|
321 | Available values: 0, 16 and 32, where 0 (the default) means keep original format |
---|
322 | as it is. This value is number of bits for the pixel. |
---|
323 | */ |
---|
324 | virtual void setDesiredIntegerBitDepth(ushort bits); |
---|
325 | |
---|
326 | /** gets desired bit depth for integer pixel format textures. |
---|
327 | */ |
---|
328 | virtual ushort getDesiredIntegerBitDepth(void) const; |
---|
329 | |
---|
330 | /** Sets desired bit depth for float pixel format textures. |
---|
331 | @note |
---|
332 | Available values: 0, 16 and 32, where 0 (the default) means keep original format |
---|
333 | as it is. This value is number of bits for a channel of the pixel. |
---|
334 | */ |
---|
335 | virtual void setDesiredFloatBitDepth(ushort bits); |
---|
336 | |
---|
337 | /** gets desired bit depth for float pixel format textures. |
---|
338 | */ |
---|
339 | virtual ushort getDesiredFloatBitDepth(void) const; |
---|
340 | |
---|
341 | /** Sets desired bit depth for integer and float pixel format. |
---|
342 | */ |
---|
343 | virtual void setDesiredBitDepths(ushort integerBits, ushort floatBits); |
---|
344 | |
---|
345 | /** Sets whether luminace pixel format will treated as alpha format when load this texture. |
---|
346 | */ |
---|
347 | virtual void setTreatLuminanceAsAlpha(bool asAlpha); |
---|
348 | |
---|
349 | /** Gets whether luminace pixel format will treated as alpha format when load this texture. |
---|
350 | */ |
---|
351 | virtual bool getTreatLuminanceAsAlpha(void) const; |
---|
352 | |
---|
353 | /** Return the number of faces this texture has. This will be 6 for a cubemap |
---|
354 | texture and 1 for a 1D, 2D or 3D one. |
---|
355 | */ |
---|
356 | virtual size_t getNumFaces() const; |
---|
357 | |
---|
358 | /** Return hardware pixel buffer for a surface. This buffer can then |
---|
359 | be used to copy data from and to a particular level of the texture. |
---|
360 | @param face Face number, in case of a cubemap texture. Must be 0 |
---|
361 | for other types of textures. |
---|
362 | For cubemaps, this is one of |
---|
363 | +X (0), -X (1), +Y (2), -Y (3), +Z (4), -Z (5) |
---|
364 | @param mipmap Mipmap level. This goes from 0 for the first, largest |
---|
365 | mipmap level to getNumMipmaps()-1 for the smallest. |
---|
366 | @return A shared pointer to a hardware pixel buffer |
---|
367 | @remarks The buffer is invalidated when the resource is unloaded or destroyed. |
---|
368 | Do not use it after the lifetime of the containing texture. |
---|
369 | */ |
---|
370 | virtual HardwarePixelBufferSharedPtr getBuffer(size_t face=0, size_t mipmap=0) = 0; |
---|
371 | |
---|
372 | |
---|
373 | /** Populate an Image with the contents of this texture. |
---|
374 | @param destImage The target image (contents will be overwritten) |
---|
375 | @param includeMipMaps Whether to embed mipmaps in the image |
---|
376 | */ |
---|
377 | virtual void convertToImage(Image& destImage, bool includeMipMaps = false); |
---|
378 | |
---|
379 | /** Retrieve a platform or API-specific piece of information from this texture. |
---|
380 | This method of retrieving information should only be used if you know what you're doing. |
---|
381 | @param name The name of the attribute to retrieve |
---|
382 | @param pData Pointer to memory matching the type of data you want to retrieve. |
---|
383 | */ |
---|
384 | virtual void getCustomAttribute(const String& name, void* pData) {} |
---|
385 | |
---|
386 | |
---|
387 | |
---|
388 | protected: |
---|
389 | uint32 mHeight; |
---|
390 | uint32 mWidth; |
---|
391 | uint32 mDepth; |
---|
392 | |
---|
393 | uint8 mNumRequestedMipmaps; |
---|
394 | uint8 mNumMipmaps; |
---|
395 | bool mMipmapsHardwareGenerated; |
---|
396 | float mGamma; |
---|
397 | bool mHwGamma; |
---|
398 | uint mFSAA; |
---|
399 | String mFSAAHint; |
---|
400 | |
---|
401 | TextureType mTextureType; |
---|
402 | PixelFormat mFormat; |
---|
403 | int mUsage; /// Bit field, so this can't be TextureUsage |
---|
404 | |
---|
405 | PixelFormat mSrcFormat; |
---|
406 | uint32 mSrcWidth, mSrcHeight, mSrcDepth; |
---|
407 | |
---|
408 | PixelFormat mDesiredFormat; |
---|
409 | unsigned short mDesiredIntegerBitDepth; |
---|
410 | unsigned short mDesiredFloatBitDepth; |
---|
411 | bool mTreatLuminanceAsAlpha; |
---|
412 | |
---|
413 | bool mInternalResourcesCreated; |
---|
414 | |
---|
415 | /// @copydoc Resource::calculateSize |
---|
416 | size_t calculateSize(void) const; |
---|
417 | |
---|
418 | |
---|
419 | /** Implementation of creating internal texture resources |
---|
420 | */ |
---|
421 | virtual void createInternalResourcesImpl(void) = 0; |
---|
422 | |
---|
423 | /** Implementation of freeing internal texture resources |
---|
424 | */ |
---|
425 | virtual void freeInternalResourcesImpl(void) = 0; |
---|
426 | |
---|
427 | /** Default implementation of unload which calls freeInternalResources */ |
---|
428 | void unloadImpl(void); |
---|
429 | |
---|
430 | /** Identify the source file type as a string, either from the extension |
---|
431 | or from a magic number. |
---|
432 | */ |
---|
433 | String getSourceFileType() const; |
---|
434 | |
---|
435 | }; |
---|
436 | /** @} */ |
---|
437 | /** @} */ |
---|
438 | |
---|
439 | } |
---|
440 | |
---|
441 | #include "OgreHeaderSuffix.h" |
---|
442 | |
---|
443 | #endif |
---|