PixelLightAPI  .
Texture.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: Texture.h                                      *
00003  *
00004  *  Copyright (C) 2002-2011 The PixelLight Team (http://www.pixellight.org/)
00005  *
00006  *  This file is part of PixelLight.
00007  *
00008  *  PixelLight is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU Lesser General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  PixelLight is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  *  GNU Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public License
00019  *  along with PixelLight. If not, see <http://www.gnu.org/licenses/>.
00020 \*********************************************************/
00021 
00022 
00023 #ifndef __PLRENDERER_TEXTURE_H__
00024 #define __PLRENDERER_TEXTURE_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Includes                                              ]
00030 //[-------------------------------------------------------]
00031 #include <PLCore/Container/Resource.h>
00032 #include <PLMath/Vector3i.h>
00033 #include "PLRenderer/PLRenderer.h"
00034 
00035 
00036 //[-------------------------------------------------------]
00037 //[ Namespace                                             ]
00038 //[-------------------------------------------------------]
00039 namespace PLRenderer {
00040 
00041 
00042 //[-------------------------------------------------------]
00043 //[ Forward declarations                                  ]
00044 //[-------------------------------------------------------]
00045 class TextureBuffer;
00046 class TextureManager;
00047 class ResourceHandler;
00048 
00049 
00050 //[-------------------------------------------------------]
00051 //[ Classes                                               ]
00052 //[-------------------------------------------------------]
00053 /**
00054 *  @brief
00055 *    Texture resource class
00056 *
00057 *  @remarks
00058 *    This generic texture class encapsulates the concrete renderer textures for easier and
00059 *    universal usage. 'Texture animation' is also 'just a texture'. This class can load the data
00060 *    automatically using internally the 'Image'-class - so all image formats the image class
00061 *    supports can also be used as textures.
00062 *
00063 *    The texture class has own special texture compression information standing 'above' the real
00064 *    renderer texture compression. A good example for the usage of this is 'normal map compression'.
00065 *    Because normal maps don't look good when using classic compression, it's better do don't
00066 *    compress them - or to use some tricks so we don't loose to much quality. So, the compression
00067 *    information just indicates for instance the usage like 'DXT5_xGxR' were a texture is compressed
00068 *    as 'DXT5', but the data is stored in a slightly other way. A scene renderer implementation can
00069 *    use this proved information to tell the shader programs about the special usage of the data.
00070 *    When loading a texture, by default the texture data is used as provided. If a 'tga' image is
00071 *    used, no GPU compression is used. If a 'dds' image provides 'fitting' compressed data, this
00072 *    data is directly send to the GPU.
00073 *
00074 *    To have some control over the loading and usage of a texture, we provide a XML based file format
00075 *    called 'plt'. If you for instance load in a texture called 'Data/Textures/Soldier_normal.dds', we look whether or
00076 *    not there's a file called 'Soldier_normal.plt' within the same directory. If so, we take the information
00077 *    this file contains into account. A 'plt' file for a compressed normal map may look like this:
00078 *        <?xml version="1.0"?>
00079 *        <Texture Version="1">
00080 *            <General Compression="DXT5_xGxR" />
00081 *        </Texture>
00082 *    We can automatically detect whether or not a texture is compressed, but we CAN'T find out automatically
00083 *    HOW the data is organized internally. In the sample above 'DXT5_xGxR' tells us that 'swizzled DXT5' is
00084 *    used. It's the job of a scene renderer or of YOU as programmer to take this information into account.
00085 */
00086 class Texture : public PLCore::Resource<Texture> {
00087 
00088 
00089     //[-------------------------------------------------------]
00090     //[ Friends                                               ]
00091     //[-------------------------------------------------------]
00092     friend class TextureManager;
00093 
00094 
00095     //[-------------------------------------------------------]
00096     //[ Public definitions                                    ]
00097     //[-------------------------------------------------------]
00098     public:
00099         /**
00100         *  @brief
00101         *    Texture compression formats
00102         */
00103         enum ECompressionFormat {
00104             Default         = 0,    /**< Data is used as provided (for instance 'tga' is not compressed, 'dds' may be compressed) */
00105             DXT1            = 1,    /**< DXT1 compression (known as BC1 in DirectX 10, RGB compression: 8:1, 8 bytes per block) */
00106             DXT3            = 2,    /**< DXT3 compression (known as BC2 in DirectX 10, RGBA compression: 4:1, 16 bytes per block) */
00107             DXT5            = 3,    /**< DXT5 compression (known as BC3 in DirectX 10, RGBA compression: 4:1, 16 bytes per block) */
00108             DXT5_xGxR       = 4,    /**< Swizzled DXT5 (same as above!) format for normal map compression (red stored in alpha, blue unused) */
00109             LATC1           = 5,    /**< 1 component texture compression (also known as 3DC+/ATI1N, known as BC4 in DirectX 10, 8 bytes per block) */
00110             LATC2           = 6,    /**< 2 component texture compression (luminance & alpha compression 4:1 -> normal map compression, also known as 3DC/ATI2N, known as BC5 in DirectX 10, 16 bytes per block) */
00111             LATC2_XYSwizzle = 7,    /**< Alternate XY swizzle LATC2 (same as above!) format for normal map compression (texture can be used just like DXT5_xGxR) */
00112             None            = 8     /**< No compression is used */
00113         };
00114 
00115 
00116     //[-------------------------------------------------------]
00117     //[ Public functions                                      ]
00118     //[-------------------------------------------------------]
00119     public:
00120         /**
00121         *  @brief
00122         *    Destructor
00123         */
00124         PLRENDERER_API virtual ~Texture();
00125 
00126         /**
00127         *  @brief
00128         *    Returns the texture manager this texture is in
00129         *
00130         *  @return
00131         *     The texture manager this texture is in
00132         */
00133         inline TextureManager &GetTextureManager() const;
00134 
00135         /**
00136         *  @brief
00137         *    Returns the texture buffer
00138         *
00139         *  @return
00140         *    The texture buffer, there you can e.g. upload other texture data, can be a null pointer
00141         */
00142         PLRENDERER_API TextureBuffer *GetTextureBuffer() const;
00143 
00144         /**
00145         *  @brief
00146         *    Sets the texture buffer
00147         *
00148         *  @param[in] pTextureBuffer
00149         *    Texture buffer to set, can be a null pointer
00150         */
00151         PLRENDERER_API void SetTextureBuffer(TextureBuffer *pTextureBuffer = nullptr);
00152 
00153         /**
00154         *  @brief
00155         *    Returns the texture compression hint
00156         *
00157         *  @return
00158         *    The texture compression hint
00159         *
00160         *  @remarks
00161         *    The texture compression hint JUST indicates the format or it's special usage.
00162         *    It must not match the real internal format of the used renderer texture.
00163         */
00164         inline ECompressionFormat GetCompressionHint() const;
00165 
00166         /**
00167         *  @brief
00168         *    Sets the texture compression hint
00169         *
00170         *  @param[in] nFormat
00171         *    The texture compression hint
00172         *
00173         *  @see
00174         *    - GetCompressionHint()
00175         */
00176         inline void SetCompressionHint(ECompressionFormat nFormat = Default);
00177 
00178         /**
00179         *  @brief
00180         *    Returns the original texture size
00181         *
00182         *  @return
00183         *    The original texture size
00184         *
00185         *  @remarks
00186         *    If a texture dimension can't be used by the GPU, the texture is scaled automatically
00187         *    during loading. Use the 'GetOriginalSize()'-function to request the original
00188         *    texture dimension. Use GetResource() to get the internal renderer texture where
00189         *    you can request more information about the texture on the GPU.
00190         */
00191         inline const PLMath::Vector3i &GetOriginalSize() const;
00192 
00193         /**
00194         *  @brief
00195         *    Makes the texture to the current renderer texture
00196         *
00197         *  @param[in] nStage
00198         *    Which texture stage?
00199         *
00200         *  @return
00201         *    'true' if all went fine, else 'false'
00202         */
00203         PLRENDERER_API bool Bind(PLCore::uint32 nStage = 0) const;
00204 
00205 
00206     //[-------------------------------------------------------]
00207     //[ Public virtual Texture functions                      ]
00208     //[-------------------------------------------------------]
00209     public:
00210         /**
00211         *  @brief
00212         *    Returns whether or not this texture is animated
00213         *
00214         *  @return
00215         *    'true' if this texture is animated, else 'false'
00216         *
00217         *  @remarks
00218         *    If this is an animated texture (tani filename extension) you can cast to 'TextureAni'
00219         *    to get the texture animation data.
00220         */
00221         PLRENDERER_API virtual bool IsAnimated() const;
00222 
00223 
00224     //[-------------------------------------------------------]
00225     //[ Protected functions                                   ]
00226     //[-------------------------------------------------------]
00227     protected:
00228         /**
00229         *  @brief
00230         *    Constructor
00231         *
00232         *  @param[in] cManager
00233         *    Texture manager using this resource
00234         *  @param[in] sName
00235         *    Resource name to set
00236         */
00237         Texture(TextureManager &cManager, const PLCore::String &sName);
00238 
00239         /**
00240         *  @brief
00241         *    Returns a correct texture size
00242         *
00243         *  @param[in] nSize
00244         *    Size to correct
00245         *  @param[in] nMinSize
00246         *    Minimum valid size
00247         *  @param[in] nMaxSize
00248         *    Maximum valid size
00249         *  @param[in] bRectangle
00250         *    Is this a rectangle texture?
00251         *
00252         *  @return
00253         *    The correct texture size
00254         */
00255         PLCore::uint32 GetCorrectTextureSize(PLCore::uint32 nSize, PLCore::uint32 nMinSize, PLCore::uint32 nMaxSize, bool bRectangle) const;
00256 
00257         /**
00258         *  @brief
00259         *    Destroys the used texture buffer
00260         */
00261         void DestroyTextureBuffer();
00262 
00263 
00264     //[-------------------------------------------------------]
00265     //[ Private data                                          ]
00266     //[-------------------------------------------------------]
00267     private:
00268         ResourceHandler    *m_pTextureBufferHandler;    /**< Renderer texture buffer resource handler (NEVER a null pointer!) */
00269         bool                m_bShareTextureBuffer;      /**< If 'true', do not delete the texture buffer by yourself! */
00270         ECompressionFormat  m_nCompressionHint;         /**< Compression hint */
00271         PLMath::Vector3i    m_vOriginalSize;            /**< Original texture size */
00272 
00273 
00274     //[-------------------------------------------------------]
00275     //[ Public virtual PLCore::Loadable functions             ]
00276     //[-------------------------------------------------------]
00277     public:
00278         PLRENDERER_API virtual bool LoadByFilename(const PLCore::String &sFilename, const PLCore::String &sParams = "", const PLCore::String &sMethod = "") override;
00279         PLRENDERER_API virtual bool SaveByFilename(const PLCore::String &sFilename, const PLCore::String &sParams = "", const PLCore::String &sMethod = "") override;
00280         PLRENDERER_API virtual bool SaveByFile(PLCore::File &cFile, const PLCore::String &sParams = "", const PLCore::String &sMethod = "") override;
00281         PLRENDERER_API virtual bool Unload() override;
00282         PLRENDERER_API virtual PLCore::String GetLoadableTypeName() const override;
00283 
00284 
00285 };
00286 
00287 
00288 //[-------------------------------------------------------]
00289 //[ Namespace                                             ]
00290 //[-------------------------------------------------------]
00291 } // PLRenderer
00292 
00293 
00294 //[-------------------------------------------------------]
00295 //[ Implementation                                        ]
00296 //[-------------------------------------------------------]
00297 #include "PLRenderer/Texture/Texture.inl"
00298 
00299 
00300 #endif // __PLRENDERER_TEXTURE_H__


PixelLight PixelLight 0.9.10-R1
Copyright (C) 2002-2011 by The PixelLight Team
Last modified Fri Dec 23 2011 15:51:04
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported