source: OGRE/trunk/ogrenew/OgreMain/src/OgreTextureManager.cpp @ 692

Revision 692, 6.5 KB checked in by mattausch, 19 years ago (diff)

adding ogre 1.2 and dependencies

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#include "OgreStableHeaders.h"
26#include "OgreTextureManager.h"
27#include "OgreException.h"
28#include "OgrePixelFormat.h"
29
30namespace Ogre {
31    //-----------------------------------------------------------------------
32    template<> TextureManager* Singleton<TextureManager>::ms_Singleton = 0;
33    TextureManager* TextureManager::getSingletonPtr(void)
34    {
35        return ms_Singleton;
36    }
37    TextureManager& TextureManager::getSingleton(void)
38    { 
39        assert( ms_Singleton );  return ( *ms_Singleton ); 
40    }
41    TextureManager::TextureManager(bool enable32Bit)
42         : mIs32Bit(enable32Bit), mDefaultNumMipmaps(MIP_UNLIMITED)
43    {
44        mResourceType = "Texture";
45        mLoadOrder = 75.0f;
46
47        // Subclasses should register (when this is fully constructed)
48    }
49    //-----------------------------------------------------------------------
50    TextureManager::~TextureManager()
51    {
52        // subclasses should unregister with resource group manager
53
54    }
55    //-----------------------------------------------------------------------
56    TexturePtr TextureManager::load(const String &name, const String& group,
57        TextureType texType, int numMipmaps, Real gamma, bool isAlpha)
58    {
59        TexturePtr tex = getByName(name);
60
61        if(tex.isNull())
62        {
63            tex = create(name, group);
64            tex->setTextureType(texType);
65            tex->setNumMipmaps((numMipmaps == -1)? mDefaultNumMipmaps :
66                                static_cast<size_t>(numMipmaps));
67            tex->setGamma(gamma);
68            if (isAlpha)
69                tex->setFormat(PF_A8);
70            tex->enable32Bit(mIs32Bit);
71        }
72        tex->load();
73
74        return tex;
75    }
76
77    //-----------------------------------------------------------------------
78    TexturePtr TextureManager::loadImage( const String &name, const String& group,
79        const Image &img, TextureType texType, int numMipmaps, Real gamma, bool isAlpha )
80    {
81        TexturePtr tex = create(name, group, true);
82
83        tex->setTextureType(texType);
84        tex->setNumMipmaps((numMipmaps == -1)? mDefaultNumMipmaps :
85                        static_cast<size_t>(numMipmaps));
86        tex->setGamma(gamma);
87        if (isAlpha)
88            tex->setFormat(PF_A8);
89        tex->enable32Bit(mIs32Bit);
90        tex->loadImage(img);
91
92        return tex;
93    }
94    //-----------------------------------------------------------------------
95    TexturePtr TextureManager::loadRawData(const String &name, const String& group,
96        DataStreamPtr& stream, ushort uWidth, ushort uHeight,
97        PixelFormat format, TextureType texType,
98        int numMipmaps, Real gamma)
99        {
100        TexturePtr tex = create(name, group, true);
101
102        tex->setTextureType(texType);
103        tex->setNumMipmaps((numMipmaps == -1)? mDefaultNumMipmaps :
104                        static_cast<size_t>(numMipmaps));
105        tex->setGamma(gamma);
106        tex->enable32Bit(mIs32Bit);
107                tex->loadRawData(stream, uWidth, uHeight, format);
108               
109        return tex;
110        }
111    //-----------------------------------------------------------------------
112    TexturePtr TextureManager::createManual(const String & name, const String& group,
113        TextureType texType, uint width, uint height, uint depth, int numMipmaps,
114        PixelFormat format, int usage, ManualResourceLoader* loader)
115    {
116        TexturePtr ret = create(name, group, true, loader);
117        ret->setTextureType(texType);
118        ret->setWidth(width);
119        ret->setHeight(height);
120                ret->setDepth(depth);
121        ret->setNumMipmaps((numMipmaps == -1)? mDefaultNumMipmaps :
122                        static_cast<size_t>(numMipmaps));
123        ret->setFormat(format);
124        ret->setUsage(usage);
125        ret->enable32Bit(mIs32Bit);
126                ret->createInternalResources();
127                return ret;
128    }
129    //-----------------------------------------------------------------------
130    void TextureManager::enable32BitTextures( bool setting )
131    {
132        mIs32Bit = setting;
133
134        // Iterate throught all textures
135        for( ResourceMap::iterator it = mResources.begin(); it != mResources.end(); ++it )
136        {
137            Texture* texture = static_cast<Texture*>(it->second.get());
138            // Reload loaded and reloadable texture only
139            if (texture->isLoaded() && texture->isReloadable())
140            {
141                texture->unload();
142                texture->enable32Bit(setting);
143                texture->load();
144            }
145            else
146            {
147                texture->enable32Bit(setting);
148            }
149        }
150    }
151    //-----------------------------------------------------------------------
152    void TextureManager::setDefaultNumMipmaps( size_t num )
153    {
154        mDefaultNumMipmaps = num;
155    }
156    //-----------------------------------------------------------------------
157        bool TextureManager::isFormatSupported(TextureType ttype, PixelFormat format, int usage)
158        {
159                return getNativeFormat(ttype, format, usage) == format;
160        }
161    //-----------------------------------------------------------------------
162        bool TextureManager::isEquivalentFormatSupported(TextureType ttype, PixelFormat format, int usage)
163        {
164                PixelFormat supportedFormat = getNativeFormat(ttype, format, usage);
165
166                // Assume that same or greater number of bits means quality not degraded
167                return PixelUtil::getNumElemBits(supportedFormat) >= PixelUtil::getNumElemBits(format);
168               
169        }
170}
Note: See TracBrowser for help on using the repository browser.