[657] | 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-2005 The OGRE Team
|
---|
| 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 | */
|
---|
| 25 | #include "OgreStableHeaders.h"
|
---|
| 26 | #include "OgreLogManager.h"
|
---|
| 27 | #include "OgreHardwarePixelBuffer.h"
|
---|
| 28 | #include "OgreImage.h"
|
---|
| 29 | #include "OgreTexture.h"
|
---|
| 30 | #include "OgreException.h"
|
---|
| 31 | #include "OgreResourceManager.h"
|
---|
| 32 |
|
---|
| 33 | namespace Ogre {
|
---|
| 34 | //--------------------------------------------------------------------------
|
---|
| 35 | Texture::Texture(ResourceManager* creator, const String& name,
|
---|
| 36 | ResourceHandle handle, const String& group, bool isManual,
|
---|
| 37 | ManualResourceLoader* loader)
|
---|
| 38 | : Resource(creator, name, handle, group, isManual, loader),
|
---|
| 39 | // init defaults; can be overridden before load()
|
---|
| 40 | mHeight(512),
|
---|
| 41 | mWidth(512),
|
---|
| 42 | mDepth(1),
|
---|
| 43 | mNumRequestedMipmaps(0),
|
---|
| 44 | mNumMipmaps(0),
|
---|
| 45 | mMipmapsHardwareGenerated(false),
|
---|
| 46 | mGamma(1.0f),
|
---|
| 47 | mTextureType(TEX_TYPE_2D),
|
---|
| 48 | mFormat(PF_A8R8G8B8),
|
---|
| 49 | mUsage(TU_DEFAULT),
|
---|
| 50 | // mSrcBpp inited later on
|
---|
| 51 | mSrcWidth(0),
|
---|
| 52 | mSrcHeight(0),
|
---|
| 53 | mSrcDepth(0), mInternalResourcesCreated(false)
|
---|
| 54 | // mFinalBpp inited later on by enable32bit
|
---|
| 55 | // mHasAlpha inited later on
|
---|
| 56 | {
|
---|
| 57 |
|
---|
| 58 | enable32Bit(false);
|
---|
| 59 |
|
---|
| 60 | if (createParamDictionary("Texture"))
|
---|
| 61 | {
|
---|
| 62 | // Define the parameters that have to be present to load
|
---|
| 63 | // from a generic source; actually there are none, since when
|
---|
| 64 | // predeclaring, you use a texture file which includes all the
|
---|
| 65 | // information required.
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 |
|
---|
| 69 | }
|
---|
| 70 | //-------------------------------------------------------------------------- //--------------------------------------------------------------------------
|
---|
| 71 | void Texture::loadRawData( DataStreamPtr& stream,
|
---|
| 72 | ushort uWidth, ushort uHeight, PixelFormat eFormat)
|
---|
| 73 | {
|
---|
| 74 | Image img;
|
---|
| 75 | img.loadRawData(stream, uWidth, uHeight, eFormat);
|
---|
| 76 | loadImage(img);
|
---|
| 77 | }
|
---|
| 78 | //--------------------------------------------------------------------------
|
---|
| 79 | void Texture::setFormat(PixelFormat pf)
|
---|
| 80 | {
|
---|
| 81 | mFormat = pf;
|
---|
| 82 | // This should probably change with new texture access methods, but
|
---|
| 83 | // no changes made for now
|
---|
| 84 | mSrcBpp = PixelUtil::getNumElemBytes(mFormat);
|
---|
| 85 | mHasAlpha = PixelUtil::getFlags(mFormat) & PFF_HASALPHA;
|
---|
| 86 | }
|
---|
| 87 | //--------------------------------------------------------------------------
|
---|
| 88 | size_t Texture::calculateSize(void) const
|
---|
| 89 | {
|
---|
| 90 | return getNumFaces() * PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
|
---|
| 91 | }
|
---|
| 92 | //--------------------------------------------------------------------------
|
---|
| 93 | size_t Texture::getNumFaces(void) const
|
---|
| 94 | {
|
---|
| 95 | return getTextureType() == TEX_TYPE_CUBE_MAP ? 6 : 1;
|
---|
| 96 | }
|
---|
| 97 | //--------------------------------------------------------------------------
|
---|
| 98 | void Texture::_loadImages( const std::vector<const Image*>& images )
|
---|
| 99 | {
|
---|
| 100 | if(images.size() < 1)
|
---|
| 101 | OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Cannot load empty vector of images",
|
---|
| 102 | "Texture::loadImages");
|
---|
| 103 |
|
---|
| 104 | if( mIsLoaded )
|
---|
| 105 | {
|
---|
| 106 | LogManager::getSingleton().logMessage(
|
---|
| 107 | LML_NORMAL, "Texture: "+mName+": Unloading Image");
|
---|
| 108 | unload();
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | // Set desired texture size and properties from images[0]
|
---|
| 112 | mSrcWidth = mWidth = images[0]->getWidth();
|
---|
| 113 | mSrcHeight = mHeight = images[0]->getHeight();
|
---|
| 114 | mSrcDepth = mDepth = images[0]->getDepth();
|
---|
| 115 | mFormat = images[0]->getFormat();
|
---|
| 116 | mSrcBpp = PixelUtil::getNumElemBits(mFormat);
|
---|
| 117 | mHasAlpha = PixelUtil::hasAlpha(mFormat);
|
---|
| 118 |
|
---|
| 119 | if (mFinalBpp == 16)
|
---|
| 120 | {
|
---|
| 121 | // Drop down texture internal format
|
---|
| 122 | switch (mFormat)
|
---|
| 123 | {
|
---|
| 124 | case PF_R8G8B8:
|
---|
| 125 | case PF_X8R8G8B8:
|
---|
| 126 | mFormat = PF_R5G6B5;
|
---|
| 127 | break;
|
---|
| 128 |
|
---|
| 129 | case PF_B8G8R8:
|
---|
| 130 | case PF_X8B8G8R8:
|
---|
| 131 | mFormat = PF_B5G6R5;
|
---|
| 132 | break;
|
---|
| 133 |
|
---|
| 134 | case PF_A8R8G8B8:
|
---|
| 135 | case PF_R8G8B8A8:
|
---|
| 136 | case PF_A8B8G8R8:
|
---|
| 137 | case PF_B8G8R8A8:
|
---|
| 138 | mFormat = PF_A4R4G4B4;
|
---|
| 139 | break;
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | // The custom mipmaps in the image have priority over everything
|
---|
| 144 | size_t imageMips = images[0]->getNumMipmaps();
|
---|
| 145 |
|
---|
| 146 | if(imageMips > 0) {
|
---|
| 147 | mNumMipmaps = images[0]->getNumMipmaps();
|
---|
| 148 | // Disable flag for auto mip generation
|
---|
| 149 | mUsage &= ~TU_AUTOMIPMAP;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | // Create the texture
|
---|
| 153 | createInternalResources();
|
---|
| 154 | // Check if we're loading one image with multiple faces
|
---|
| 155 | // or a vector of images representing the faces
|
---|
| 156 | size_t faces;
|
---|
| 157 | bool multiImage; // Load from multiple images?
|
---|
| 158 | if(images.size() > 1)
|
---|
| 159 | {
|
---|
| 160 | faces = images.size();
|
---|
| 161 | multiImage = true;
|
---|
| 162 | }
|
---|
| 163 | else
|
---|
| 164 | {
|
---|
| 165 | faces = images[0]->getNumFaces();
|
---|
| 166 | multiImage = false;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | // Check wether number of faces in images exceeds number of faces
|
---|
| 170 | // in this texture. If so, clamp it.
|
---|
| 171 | if(faces > getNumFaces())
|
---|
| 172 | faces = getNumFaces();
|
---|
| 173 |
|
---|
| 174 | // Say what we're doing
|
---|
| 175 | StringUtil::StrStreamType str;
|
---|
| 176 | str << "Texture: " << mName << ": Loading " << faces << " faces"
|
---|
| 177 | << "(" << PixelUtil::getFormatName(images[0]->getFormat()) << "," <<
|
---|
| 178 | images[0]->getWidth() << "x" << images[0]->getHeight() << "x" << images[0]->getDepth() <<
|
---|
| 179 | ") with ";
|
---|
| 180 | if (!(mMipmapsHardwareGenerated && mNumMipmaps == 0))
|
---|
| 181 | str << mNumMipmaps;
|
---|
| 182 | if(mUsage & TU_AUTOMIPMAP)
|
---|
| 183 | {
|
---|
| 184 | if (mMipmapsHardwareGenerated)
|
---|
| 185 | str << " hardware";
|
---|
| 186 |
|
---|
| 187 | str << " generated mipmaps";
|
---|
| 188 | }
|
---|
| 189 | else
|
---|
| 190 | {
|
---|
| 191 | str << " custom mipmaps";
|
---|
| 192 | }
|
---|
| 193 | if(multiImage)
|
---|
| 194 | str << " from multiple Images.";
|
---|
| 195 | else
|
---|
| 196 | str << " from Image.";
|
---|
| 197 | // Scoped
|
---|
| 198 | {
|
---|
| 199 | // Print data about first destination surface
|
---|
| 200 | HardwarePixelBufferSharedPtr buf = getBuffer(0, 0);
|
---|
| 201 | str << " Internal format is " << PixelUtil::getFormatName(buf->getFormat()) <<
|
---|
| 202 | "," << buf->getWidth() << "x" << buf->getHeight() << "x" << buf->getDepth() << ".";
|
---|
| 203 | }
|
---|
| 204 | LogManager::getSingleton().logMessage(
|
---|
| 205 | LML_NORMAL, str.str());
|
---|
| 206 |
|
---|
| 207 | // Main loading loop
|
---|
| 208 | // imageMips == 0 if the image has no custom mipmaps, otherwise contains the number of custom mips
|
---|
| 209 | for(size_t mip = 0; mip<=imageMips; ++mip)
|
---|
| 210 | {
|
---|
| 211 | for(size_t i = 0; i < faces; ++i)
|
---|
| 212 | {
|
---|
| 213 | PixelBox src;
|
---|
| 214 | if(multiImage)
|
---|
| 215 | {
|
---|
| 216 | // Load from multiple images
|
---|
| 217 | src = images[i]->getPixelBox(0, mip);
|
---|
| 218 | }
|
---|
| 219 | else
|
---|
| 220 | {
|
---|
| 221 | // Load from faces of images[0]
|
---|
| 222 | src = images[0]->getPixelBox(i, mip);
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | if(mGamma != 1.0f) {
|
---|
| 226 | // Apply gamma correction
|
---|
| 227 | // Do not overwrite original image but do gamma correction in temporary buffer
|
---|
| 228 | MemoryDataStreamPtr buf; // for scoped deletion of conversion buffer
|
---|
| 229 | buf.bind(new MemoryDataStream(
|
---|
| 230 | PixelUtil::getMemorySize(
|
---|
| 231 | src.getWidth(), src.getHeight(), src.getDepth(), src.format)));
|
---|
| 232 |
|
---|
| 233 | PixelBox corrected = PixelBox(src.getWidth(), src.getHeight(), src.getDepth(), src.format, buf->getPtr());
|
---|
| 234 | PixelUtil::bulkPixelConversion(src, corrected);
|
---|
| 235 |
|
---|
| 236 | Image::applyGamma(static_cast<uint8*>(corrected.data), mGamma, corrected.getConsecutiveSize(),
|
---|
| 237 | PixelUtil::getNumElemBits(src.format));
|
---|
| 238 |
|
---|
| 239 | // Destination: entire texture. blitFromMemory does the scaling to
|
---|
| 240 | // a power of two for us when needed
|
---|
| 241 | getBuffer(i, mip)->blitFromMemory(corrected);
|
---|
| 242 | }
|
---|
| 243 | else
|
---|
| 244 | {
|
---|
| 245 | // Destination: entire texture. blitFromMemory does the scaling to
|
---|
| 246 | // a power of two for us when needed
|
---|
| 247 | getBuffer(i, mip)->blitFromMemory(src);
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 | // Update size (the final size, not including temp space)
|
---|
| 253 | mSize = getNumFaces() * PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
|
---|
| 254 |
|
---|
| 255 | mIsLoaded = true;
|
---|
| 256 | }
|
---|
| 257 | //-----------------------------------------------------------------------------
|
---|
| 258 | void Texture::createInternalResources(void)
|
---|
| 259 | {
|
---|
| 260 | if (!mInternalResourcesCreated)
|
---|
| 261 | {
|
---|
| 262 | createInternalResourcesImpl();
|
---|
| 263 | mInternalResourcesCreated = true;
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
| 266 | //-----------------------------------------------------------------------------
|
---|
| 267 | void Texture::freeInternalResources(void)
|
---|
| 268 | {
|
---|
| 269 | if (mInternalResourcesCreated)
|
---|
| 270 | {
|
---|
| 271 | freeInternalResourcesImpl();
|
---|
| 272 | mInternalResourcesCreated = false;
|
---|
| 273 | }
|
---|
| 274 | }
|
---|
| 275 | //-----------------------------------------------------------------------------
|
---|
| 276 | void Texture::unloadImpl(void)
|
---|
| 277 | {
|
---|
| 278 | freeInternalResources();
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 |
|
---|
| 282 | }
|
---|