1 | /*------------------------------------------------------------------------- |
---|
2 | This source file is a part of OGRE |
---|
3 | (Object-oriented Graphics Rendering Engine) |
---|
4 | |
---|
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 library is free software; you can redistribute it and/or modify it |
---|
11 | under the terms of the GNU Lesser General Public License (LGPL) as |
---|
12 | published by the Free Software Foundation; either version 2.1 of the |
---|
13 | License, or (at your option) any later version. |
---|
14 | |
---|
15 | This library is distributed in the hope that it will be useful, but |
---|
16 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
17 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
---|
18 | License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU Lesser General Public License |
---|
21 | along with this library; if not, write to the Free Software Foundation, |
---|
22 | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or go to |
---|
23 | http://www.gnu.org/copyleft/lesser.txt |
---|
24 | |
---|
25 | You may alternatively use this source under the terms of a specific version of |
---|
26 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
27 | Torus Knot Software Ltd. |
---|
28 | -------------------------------------------------------------------------*/ |
---|
29 | #include "OgreStableHeaders.h" |
---|
30 | #include "OgreShadowTextureManager.h" |
---|
31 | #include "OgreResourceGroupManager.h" |
---|
32 | #include "OgreTextureManager.h" |
---|
33 | #include "OgreStringConverter.h" |
---|
34 | #include "OgreHardwarePixelBuffer.h" |
---|
35 | |
---|
36 | namespace Ogre |
---|
37 | { |
---|
38 | //----------------------------------------------------------------------- |
---|
39 | bool operator== ( const ShadowTextureConfig& lhs, const ShadowTextureConfig& rhs ) |
---|
40 | { |
---|
41 | if ( lhs.width != rhs.width || |
---|
42 | lhs.height != rhs.height || |
---|
43 | lhs.format != rhs.format ) |
---|
44 | { |
---|
45 | return false; |
---|
46 | } |
---|
47 | |
---|
48 | return true; |
---|
49 | } |
---|
50 | //----------------------------------------------------------------------- |
---|
51 | bool operator!= ( const ShadowTextureConfig& lhs, const ShadowTextureConfig& rhs ) |
---|
52 | { |
---|
53 | return !( lhs == rhs ); |
---|
54 | } |
---|
55 | //----------------------------------------------------------------------- |
---|
56 | template<> ShadowTextureManager* Singleton<ShadowTextureManager>::ms_Singleton = 0; |
---|
57 | ShadowTextureManager* ShadowTextureManager::getSingletonPtr(void) |
---|
58 | { |
---|
59 | return ms_Singleton; |
---|
60 | } |
---|
61 | ShadowTextureManager& ShadowTextureManager::getSingleton(void) |
---|
62 | { |
---|
63 | assert( ms_Singleton ); return ( *ms_Singleton ); |
---|
64 | } |
---|
65 | //--------------------------------------------------------------------- |
---|
66 | ShadowTextureManager::ShadowTextureManager() |
---|
67 | : mCount(0) |
---|
68 | { |
---|
69 | |
---|
70 | } |
---|
71 | //--------------------------------------------------------------------- |
---|
72 | ShadowTextureManager::~ShadowTextureManager() |
---|
73 | { |
---|
74 | clear(); |
---|
75 | } |
---|
76 | //--------------------------------------------------------------------- |
---|
77 | void ShadowTextureManager::getShadowTextures(const ShadowTextureConfigList& config, |
---|
78 | ShadowTextureList& listToPopulate) |
---|
79 | { |
---|
80 | listToPopulate.clear(); |
---|
81 | |
---|
82 | std::set<Texture*> usedTextures; |
---|
83 | |
---|
84 | for (ShadowTextureConfigList::const_iterator c = config.begin(); c != config.end(); ++c) |
---|
85 | { |
---|
86 | const ShadowTextureConfig& config = *c; |
---|
87 | bool found = false; |
---|
88 | for (ShadowTextureList::iterator t = mTextureList.begin(); t != mTextureList.end(); ++t) |
---|
89 | { |
---|
90 | const TexturePtr& tex = *t; |
---|
91 | // Skip if already used this one |
---|
92 | if (usedTextures.find(tex.getPointer()) != usedTextures.end()) |
---|
93 | continue; |
---|
94 | |
---|
95 | if (config.width == tex->getWidth() && config.height == tex->getHeight() |
---|
96 | && config.format == tex->getFormat()) |
---|
97 | { |
---|
98 | // Ok, a match |
---|
99 | listToPopulate.push_back(tex); |
---|
100 | usedTextures.insert(tex.getPointer()); |
---|
101 | found = true; |
---|
102 | break; |
---|
103 | } |
---|
104 | } |
---|
105 | if (!found) |
---|
106 | { |
---|
107 | // Create a new texture |
---|
108 | static const String baseName = "Ogre/ShadowTexture"; |
---|
109 | String targName = baseName + StringConverter::toString(mCount++); |
---|
110 | TexturePtr shadowTex = TextureManager::getSingleton().createManual( |
---|
111 | targName, |
---|
112 | ResourceGroupManager::INTERNAL_RESOURCE_GROUP_NAME, |
---|
113 | TEX_TYPE_2D, config.width, config.height, 0, config.format, |
---|
114 | TU_RENDERTARGET); |
---|
115 | // Ensure texture loaded |
---|
116 | shadowTex->load(); |
---|
117 | listToPopulate.push_back(shadowTex); |
---|
118 | usedTextures.insert(shadowTex.getPointer()); |
---|
119 | mTextureList.push_back(shadowTex); |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | } |
---|
124 | //--------------------------------------------------------------------- |
---|
125 | TexturePtr ShadowTextureManager::getNullShadowTexture(PixelFormat format) |
---|
126 | { |
---|
127 | for (ShadowTextureList::iterator t = mNullTextureList.begin(); t != mNullTextureList.end(); ++t) |
---|
128 | { |
---|
129 | const TexturePtr& tex = *t; |
---|
130 | |
---|
131 | if (format == tex->getFormat()) |
---|
132 | { |
---|
133 | // Ok, a match |
---|
134 | return tex; |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | // not found, create a new one |
---|
139 | // A 1x1 texture of the correct format, not a render target |
---|
140 | static const String baseName = "Ogre/ShadowTextureNull"; |
---|
141 | String targName = baseName + StringConverter::toString(mCount++); |
---|
142 | TexturePtr shadowTex = TextureManager::getSingleton().createManual( |
---|
143 | targName, |
---|
144 | ResourceGroupManager::INTERNAL_RESOURCE_GROUP_NAME, |
---|
145 | TEX_TYPE_2D, 1, 1, 0, format); |
---|
146 | mNullTextureList.push_back(shadowTex); |
---|
147 | |
---|
148 | // lock & populate the texture based on format |
---|
149 | shadowTex->getBuffer()->lock(HardwareBuffer::HBL_DISCARD); |
---|
150 | const PixelBox& box = shadowTex->getBuffer()->getCurrentLock(); |
---|
151 | |
---|
152 | // set high-values across all bytes of the format |
---|
153 | memset(box.data, 0xFFFF, PixelUtil::getNumElemBytes(format)); |
---|
154 | |
---|
155 | shadowTex->getBuffer()->unlock(); |
---|
156 | |
---|
157 | return shadowTex; |
---|
158 | |
---|
159 | } |
---|
160 | //--------------------------------------------------------------------- |
---|
161 | void ShadowTextureManager::clearUnused() |
---|
162 | { |
---|
163 | for (ShadowTextureList::iterator i = mTextureList.begin(); i != mTextureList.end(); ) |
---|
164 | { |
---|
165 | // Unreferenced if only this reference and the resource system |
---|
166 | // Any cached shadow textures should be re-bound each frame dropping |
---|
167 | // any old references |
---|
168 | if ((*i).useCount() == ResourceGroupManager::RESOURCE_SYSTEM_NUM_REFERENCE_COUNTS + 1) |
---|
169 | { |
---|
170 | TextureManager::getSingleton().remove((*i)->getHandle()); |
---|
171 | i = mTextureList.erase(i); |
---|
172 | } |
---|
173 | else |
---|
174 | { |
---|
175 | ++i; |
---|
176 | } |
---|
177 | } |
---|
178 | for (ShadowTextureList::iterator i = mNullTextureList.begin(); i != mNullTextureList.end(); ) |
---|
179 | { |
---|
180 | // Unreferenced if only this reference and the resource system |
---|
181 | // Any cached shadow textures should be re-bound each frame dropping |
---|
182 | // any old references |
---|
183 | if ((*i).useCount() == ResourceGroupManager::RESOURCE_SYSTEM_NUM_REFERENCE_COUNTS + 1) |
---|
184 | { |
---|
185 | TextureManager::getSingleton().remove((*i)->getHandle()); |
---|
186 | i = mNullTextureList.erase(i); |
---|
187 | } |
---|
188 | else |
---|
189 | { |
---|
190 | ++i; |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | } |
---|
195 | //--------------------------------------------------------------------- |
---|
196 | void ShadowTextureManager::clear() |
---|
197 | { |
---|
198 | for (ShadowTextureList::iterator i = mTextureList.begin(); i != mTextureList.end(); ++i) |
---|
199 | { |
---|
200 | TextureManager::getSingleton().remove((*i)->getHandle()); |
---|
201 | } |
---|
202 | mTextureList.clear(); |
---|
203 | |
---|
204 | } |
---|
205 | //--------------------------------------------------------------------- |
---|
206 | |
---|
207 | } |
---|
208 | |
---|