| 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-2006 Torus Knot Software Ltd |
|---|
| 8 | Also see acknowledgements in Readme.html |
|---|
| 9 | |
|---|
| 10 | This program is free software; you can redistribute it and/or modify it under |
|---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
|---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 13 | version. |
|---|
| 14 | |
|---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
|---|
| 18 | |
|---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
|---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
|---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
|---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
|---|
| 23 | |
|---|
| 24 | You may alternatively use this source under the terms of a specific version of |
|---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from |
|---|
| 26 | Torus Knot Software Ltd. |
|---|
| 27 | ----------------------------------------------------------------------------- |
|---|
| 28 | */ |
|---|
| 29 | #include "OgreStableHeaders.h" |
|---|
| 30 | #include "OgreTextureManager.h" |
|---|
| 31 | #include "OgreException.h" |
|---|
| 32 | #include "OgrePixelFormat.h" |
|---|
| 33 | |
|---|
| 34 | namespace Ogre { |
|---|
| 35 | //----------------------------------------------------------------------- |
|---|
| 36 | template<> TextureManager* Singleton<TextureManager>::ms_Singleton = 0; |
|---|
| 37 | TextureManager* TextureManager::getSingletonPtr(void) |
|---|
| 38 | { |
|---|
| 39 | return ms_Singleton; |
|---|
| 40 | } |
|---|
| 41 | TextureManager& TextureManager::getSingleton(void) |
|---|
| 42 | { |
|---|
| 43 | assert( ms_Singleton ); return ( *ms_Singleton ); |
|---|
| 44 | } |
|---|
| 45 | //----------------------------------------------------------------------- |
|---|
| 46 | TextureManager::TextureManager(void) |
|---|
| 47 | : mPreferredIntegerBitDepth(0) |
|---|
| 48 | , mPreferredFloatBitDepth(0) |
|---|
| 49 | , mDefaultNumMipmaps(MIP_UNLIMITED) |
|---|
| 50 | { |
|---|
| 51 | mResourceType = "Texture"; |
|---|
| 52 | mLoadOrder = 75.0f; |
|---|
| 53 | |
|---|
| 54 | // Subclasses should register (when this is fully constructed) |
|---|
| 55 | } |
|---|
| 56 | //----------------------------------------------------------------------- |
|---|
| 57 | TextureManager::~TextureManager() |
|---|
| 58 | { |
|---|
| 59 | // subclasses should unregister with resource group manager |
|---|
| 60 | |
|---|
| 61 | } |
|---|
| 62 | //----------------------------------------------------------------------- |
|---|
| 63 | TexturePtr TextureManager::load(const String &name, const String& group, |
|---|
| 64 | TextureType texType, int numMipmaps, Real gamma, bool isAlpha, PixelFormat desiredFormat) |
|---|
| 65 | { |
|---|
| 66 | ResourceCreateOrRetrieveResult res = createOrRetrieve(name, group); |
|---|
| 67 | TexturePtr tex = res.first; |
|---|
| 68 | // Was it created? |
|---|
| 69 | if(res.second) |
|---|
| 70 | { |
|---|
| 71 | tex->setTextureType(texType); |
|---|
| 72 | tex->setNumMipmaps((numMipmaps == MIP_DEFAULT)? mDefaultNumMipmaps : |
|---|
| 73 | static_cast<size_t>(numMipmaps)); |
|---|
| 74 | tex->setGamma(gamma); |
|---|
| 75 | tex->setTreatLuminanceAsAlpha(isAlpha); |
|---|
| 76 | tex->setFormat(desiredFormat); |
|---|
| 77 | } |
|---|
| 78 | tex->load(); |
|---|
| 79 | |
|---|
| 80 | return tex; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | //----------------------------------------------------------------------- |
|---|
| 84 | TexturePtr TextureManager::loadImage( const String &name, const String& group, |
|---|
| 85 | const Image &img, TextureType texType, int numMipmaps, Real gamma, bool isAlpha, PixelFormat desiredFormat) |
|---|
| 86 | { |
|---|
| 87 | TexturePtr tex = create(name, group, true); |
|---|
| 88 | |
|---|
| 89 | tex->setTextureType(texType); |
|---|
| 90 | tex->setNumMipmaps((numMipmaps == MIP_DEFAULT)? mDefaultNumMipmaps : |
|---|
| 91 | static_cast<size_t>(numMipmaps)); |
|---|
| 92 | tex->setGamma(gamma); |
|---|
| 93 | tex->setTreatLuminanceAsAlpha(isAlpha); |
|---|
| 94 | tex->setFormat(desiredFormat); |
|---|
| 95 | tex->loadImage(img); |
|---|
| 96 | |
|---|
| 97 | return tex; |
|---|
| 98 | } |
|---|
| 99 | //----------------------------------------------------------------------- |
|---|
| 100 | TexturePtr TextureManager::loadRawData(const String &name, const String& group, |
|---|
| 101 | DataStreamPtr& stream, ushort uWidth, ushort uHeight, |
|---|
| 102 | PixelFormat format, TextureType texType, |
|---|
| 103 | int numMipmaps, Real gamma) |
|---|
| 104 | { |
|---|
| 105 | TexturePtr tex = create(name, group, true); |
|---|
| 106 | |
|---|
| 107 | tex->setTextureType(texType); |
|---|
| 108 | tex->setNumMipmaps((numMipmaps == MIP_DEFAULT)? mDefaultNumMipmaps : |
|---|
| 109 | static_cast<size_t>(numMipmaps)); |
|---|
| 110 | tex->setGamma(gamma); |
|---|
| 111 | tex->loadRawData(stream, uWidth, uHeight, format); |
|---|
| 112 | |
|---|
| 113 | return tex; |
|---|
| 114 | } |
|---|
| 115 | //----------------------------------------------------------------------- |
|---|
| 116 | TexturePtr TextureManager::createManual(const String & name, const String& group, |
|---|
| 117 | TextureType texType, uint width, uint height, uint depth, int numMipmaps, |
|---|
| 118 | PixelFormat format, int usage, ManualResourceLoader* loader) |
|---|
| 119 | { |
|---|
| 120 | TexturePtr ret = create(name, group, true, loader); |
|---|
| 121 | ret->setTextureType(texType); |
|---|
| 122 | ret->setWidth(width); |
|---|
| 123 | ret->setHeight(height); |
|---|
| 124 | ret->setDepth(depth); |
|---|
| 125 | ret->setNumMipmaps((numMipmaps == MIP_DEFAULT)? mDefaultNumMipmaps : |
|---|
| 126 | static_cast<size_t>(numMipmaps)); |
|---|
| 127 | ret->setFormat(format); |
|---|
| 128 | ret->setUsage(usage); |
|---|
| 129 | ret->createInternalResources(); |
|---|
| 130 | return ret; |
|---|
| 131 | } |
|---|
| 132 | //----------------------------------------------------------------------- |
|---|
| 133 | void TextureManager::setPreferredIntegerBitDepth(ushort bits, bool reloadTextures) |
|---|
| 134 | { |
|---|
| 135 | mPreferredIntegerBitDepth = bits; |
|---|
| 136 | |
|---|
| 137 | if (reloadTextures) |
|---|
| 138 | { |
|---|
| 139 | // Iterate throught all textures |
|---|
| 140 | for (ResourceMap::iterator it = mResources.begin(); it != mResources.end(); ++it) |
|---|
| 141 | { |
|---|
| 142 | Texture* texture = static_cast<Texture*>(it->second.get()); |
|---|
| 143 | // Reload loaded and reloadable texture only |
|---|
| 144 | if (texture->isLoaded() && texture->isReloadable()) |
|---|
| 145 | { |
|---|
| 146 | texture->unload(); |
|---|
| 147 | texture->setDesiredIntegerBitDepth(bits); |
|---|
| 148 | texture->load(); |
|---|
| 149 | } |
|---|
| 150 | else |
|---|
| 151 | { |
|---|
| 152 | texture->setDesiredIntegerBitDepth(bits); |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | //----------------------------------------------------------------------- |
|---|
| 158 | ushort TextureManager::getPreferredIntegerBitDepth(void) const |
|---|
| 159 | { |
|---|
| 160 | return mPreferredIntegerBitDepth; |
|---|
| 161 | } |
|---|
| 162 | //----------------------------------------------------------------------- |
|---|
| 163 | void TextureManager::setPreferredFloatBitDepth(ushort bits, bool reloadTextures) |
|---|
| 164 | { |
|---|
| 165 | mPreferredFloatBitDepth = bits; |
|---|
| 166 | |
|---|
| 167 | if (reloadTextures) |
|---|
| 168 | { |
|---|
| 169 | // Iterate throught all textures |
|---|
| 170 | for (ResourceMap::iterator it = mResources.begin(); it != mResources.end(); ++it) |
|---|
| 171 | { |
|---|
| 172 | Texture* texture = static_cast<Texture*>(it->second.get()); |
|---|
| 173 | // Reload loaded and reloadable texture only |
|---|
| 174 | if (texture->isLoaded() && texture->isReloadable()) |
|---|
| 175 | { |
|---|
| 176 | texture->unload(); |
|---|
| 177 | texture->setDesiredFloatBitDepth(bits); |
|---|
| 178 | texture->load(); |
|---|
| 179 | } |
|---|
| 180 | else |
|---|
| 181 | { |
|---|
| 182 | texture->setDesiredFloatBitDepth(bits); |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | } |
|---|
| 186 | } |
|---|
| 187 | //----------------------------------------------------------------------- |
|---|
| 188 | ushort TextureManager::getPreferredFloatBitDepth(void) const |
|---|
| 189 | { |
|---|
| 190 | return mPreferredFloatBitDepth; |
|---|
| 191 | } |
|---|
| 192 | //----------------------------------------------------------------------- |
|---|
| 193 | void TextureManager::setPreferredBitDepths(ushort integerBits, ushort floatBits, bool reloadTextures) |
|---|
| 194 | { |
|---|
| 195 | mPreferredIntegerBitDepth = integerBits; |
|---|
| 196 | mPreferredFloatBitDepth = floatBits; |
|---|
| 197 | |
|---|
| 198 | if (reloadTextures) |
|---|
| 199 | { |
|---|
| 200 | // Iterate throught all textures |
|---|
| 201 | for (ResourceMap::iterator it = mResources.begin(); it != mResources.end(); ++it) |
|---|
| 202 | { |
|---|
| 203 | Texture* texture = static_cast<Texture*>(it->second.get()); |
|---|
| 204 | // Reload loaded and reloadable texture only |
|---|
| 205 | if (texture->isLoaded() && texture->isReloadable()) |
|---|
| 206 | { |
|---|
| 207 | texture->unload(); |
|---|
| 208 | texture->setDesiredBitDepths(integerBits, floatBits); |
|---|
| 209 | texture->load(); |
|---|
| 210 | } |
|---|
| 211 | else |
|---|
| 212 | { |
|---|
| 213 | texture->setDesiredBitDepths(integerBits, floatBits); |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | //----------------------------------------------------------------------- |
|---|
| 219 | void TextureManager::setDefaultNumMipmaps( size_t num ) |
|---|
| 220 | { |
|---|
| 221 | mDefaultNumMipmaps = num; |
|---|
| 222 | } |
|---|
| 223 | //----------------------------------------------------------------------- |
|---|
| 224 | bool TextureManager::isFormatSupported(TextureType ttype, PixelFormat format, int usage) |
|---|
| 225 | { |
|---|
| 226 | return getNativeFormat(ttype, format, usage) == format; |
|---|
| 227 | } |
|---|
| 228 | //----------------------------------------------------------------------- |
|---|
| 229 | bool TextureManager::isEquivalentFormatSupported(TextureType ttype, PixelFormat format, int usage) |
|---|
| 230 | { |
|---|
| 231 | PixelFormat supportedFormat = getNativeFormat(ttype, format, usage); |
|---|
| 232 | |
|---|
| 233 | // Assume that same or greater number of bits means quality not degraded |
|---|
| 234 | return PixelUtil::getNumElemBits(supportedFormat) >= PixelUtil::getNumElemBits(format); |
|---|
| 235 | |
|---|
| 236 | } |
|---|
| 237 | } |
|---|