Project

General

Profile

SO3Engine
SO3Texture.cpp
Go to the documentation of this file.
1
10#include "SO3Renderer/SO3Root.h"
11#include <OgreBitwise.h>
12
13namespace SO3
14{
15
16STexture::STexture(SScene* scene, const std::string& groupName, const std::string& textureName, const std::string& path, const int& w, const int& h) : SData(textureName)
17{
18 mScene = scene;
19 mGroupName = groupName;
20 O3TexturePtr.reset();
21 mWidth = w;
22 mHeight = h;
23 mBuffer.data = 0;
24
25 try
26 {
27 O3TexturePtr = mScene->O3TextureManager->getByName(textureName, mGroupName);
28 }
29 catch (Ogre::Exception &)
30 {
31 }
32
33 if (!O3TexturePtr)
34 {
35 if(path.empty())
36 {
37 const Ogre::RenderSystemCapabilities* caps = SRoot::getSingleton().GetOgreRenderSystem()->getCapabilities();
38
39 if (caps && (!caps->hasCapability(Ogre::RSC_NON_POWER_OF_2_TEXTURES) || caps->getNonPOW2TexturesLimited()))
40 {
41 mWidth = Ogre::Bitwise::firstPO2From(static_cast <unsigned short>(mWidth / 2));
42 mHeight = Ogre::Bitwise::firstPO2From(static_cast <unsigned short>(mHeight / 2));
43 }
44
45 try
46 {
47 Ogre::PixelFormat format = Ogre::PF_A8R8G8B8;
49 Ogre::TexturePtr texptr = mScene->O3TextureManager->createManual(textureName, mGroupName, Ogre::TEX_TYPE_2D, mWidth, mHeight, Ogre::MIP_DEFAULT, format, Ogre::TU_STATIC_WRITE_ONLY, this, false, 0);
50 texptr->load();
51 O3TexturePtr = texptr;
52 }
53 catch (Ogre::Exception &e)
54 {
55 Ogre::LogManager::getSingleton().getDefaultLog()->logMessage("An exception has occurred: "+ e.getDescription());
56 O3TexturePtr.reset();
57 }
58 }
59 else
60 {
61 try
62 {
63 Ogre::TexturePtr texptr = mScene->O3TextureManager->load(path, mGroupName);
64 O3TexturePtr = texptr;
65 /*
66 Ogre::Image im;
67 im.load(path, mGroupName);
68#ifdef _WIN32
69 O3TexturePtr = mScene->O3TextureManager->loadImage(textureName, mGroupName, im, Ogre::TEX_TYPE_2D);
70#else
71 O3TexturePtr = mScene->O3TextureManager->loadImage(textureName, mGroupName, im, Ogre::TEX_TYPE_2D, 0);
72#endif
73 */
74 }
75 catch( Ogre::Exception &e )
76 {
77 Ogre::LogManager::getSingleton().getDefaultLog()->logMessage("An exception has occurred: " + e.getDescription());
78 O3TexturePtr.reset();
79 }
80 }
81 }
82}
83
84STexture::STexture(SScene* scene, const std::string& groupName, const std::string& textureName, Ogre::Image image) : SData(textureName)
85{
86 mScene = scene;
87 mGroupName = groupName;
88 O3TexturePtr.reset();
89 mWidth = image.getWidth();
90 mHeight = image.getHeight();
91 mBuffer.data = 0;
92
93 try
94 {
95 O3TexturePtr = mScene->O3TextureManager->getByName(textureName, mGroupName);
96 }
97 catch (Ogre::Exception &)
98 {
99 }
100
101 if (!O3TexturePtr)
102 {
103 try
104 {
105 O3TexturePtr = mScene->O3TextureManager->loadImage(textureName, mGroupName, image);
106 }
107 catch (Ogre::Exception &e)
108 {
109 Ogre::LogManager::getSingleton().getDefaultLog()->logMessage("An exception has occurred: " + e.getDescription());
110 O3TexturePtr.reset();
111 }
112 }
113}
114
115STexture::STexture() : SData("")
116{
117 // Forbiden (private)
118 O3TexturePtr.reset();
119 mBuffer.data = 0;
120}
121
123{
124 if (O3TexturePtr)
125 {
126 if (O3TexturePtr.get()->isManuallyLoaded())
127 {
128 O3TexturePtr->unload();
129 //O3TexturePtr->setManuallyLoaded(false);
130 try
131 {
132 Ogre::TextureManager::getSingletonPtr()->remove(O3TexturePtr->getHandle());
133 }
134 catch(Ogre::Exception&)
135 {
136 //not found
137 }
138
139 O3TexturePtr.reset();
140 O3TexturePtr.setNull();
141 }
142 }
143 if (mBuffer.data)
144 SAFE_FREE(mBuffer.data);
145}
146
148{
149 return mScene;
150}
151
153{
154 return mGroupName;
155}
156
158{
159 return O3TexturePtr;
160}
161
162void STexture::BlitAlphaTexture(PtrObjBitmap Bcolor, PtrObjBitmap Balpha)
163{
164 if (mBuffer.data)
165 SAFE_FREE(mBuffer.data);
166
167 std::size_t textureNumMipmap = 0;
168
169 if (O3TexturePtr->getNumMipmaps() != Ogre::MIP_UNLIMITED)
170 textureNumMipmap = O3TexturePtr->getNumMipmaps();
171
172 // Get the texture pixel buffer
173 Ogre::HardwarePixelBufferSharedPtr pixelBuffer = O3TexturePtr->getBuffer(0, 0);
174
175 // Lock the pixel buffer and get a pixel box
176 pixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
177
178 // copy last bitmap state
179 const Ogre::PixelBox& largePixelBox = pixelBuffer->getCurrentLock();
180 ConversionTools::ScolBitmapGetRGBA(Bcolor, Balpha, largePixelBox);
181 mBuffer = Ogre::PixelBox(largePixelBox.getWidth(), largePixelBox.getHeight(), largePixelBox.getDepth(), largePixelBox.format);
182 mBuffer.data = (uchar*)malloc(mBuffer.getConsecutiveSize());
183 Ogre::Image::scale(largePixelBox, mBuffer);
184
185 // make mip maps
186 for (size_t iMipmap = 1; iMipmap < textureNumMipmap; iMipmap++)
187 {
188 // Get the texture pixel buffer for the sub mipmap level
189 Ogre::HardwarePixelBufferSharedPtr pixelBufferMipmap = O3TexturePtr->getBuffer(0, iMipmap);
190
191 // Lock the pixel buffer and get a pixel box
192 pixelBufferMipmap->lock(Ogre::HardwareBuffer::HBL_DISCARD);
193 const Ogre::PixelBox& smallPixelBox = pixelBufferMipmap->getCurrentLock();
194
195 ConversionTools::ScolBitmapGetRGBA(Bcolor, Balpha, smallPixelBox);
196
197 // Unlock the mipmap pixel buffer
198 pixelBufferMipmap->unlock();
199 }
200
201 // Unlock the pixel buffer
202 pixelBuffer->unlock();
203}
204
205void STexture::BlitTexture(PtrObjBitmap B)
206{
207 if (mBuffer.data)
208 SAFE_FREE(mBuffer.data);
209
210 std::size_t textureNumMipmap = 0;
211
212 if (O3TexturePtr->getNumMipmaps() != Ogre::MIP_UNLIMITED)
213 textureNumMipmap = O3TexturePtr->getNumMipmaps();
214
215 // Get the texture pixel buffer
216 Ogre::HardwarePixelBufferSharedPtr pixelBuffer = O3TexturePtr->getBuffer(0, 0);
217
218 // Lock the pixel buffer and get a pixel box
219 pixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
220
221 const Ogre::PixelBox& largePixelBox = pixelBuffer->getCurrentLock();
222 ConversionTools::ScolBitmapGetRGBA(B, 0, largePixelBox);
223 mBuffer = Ogre::PixelBox(largePixelBox.getWidth(), largePixelBox.getHeight(), largePixelBox.getDepth(), largePixelBox.format);
224 mBuffer.data = (uchar*)malloc(mBuffer.getConsecutiveSize());
225 Ogre::Image::scale(largePixelBox, mBuffer);
226
227 // make mip maps
228 for (size_t iMipmap = 1; iMipmap < textureNumMipmap; iMipmap++)
229 {
230 // Get the texture pixel buffer for the sub mipmap level
231 Ogre::HardwarePixelBufferSharedPtr pixelBufferMipmap = O3TexturePtr->getBuffer(0, iMipmap);
232
233 // Lock the pixel buffer and get a pixel box
234 pixelBufferMipmap->lock(Ogre::HardwareBuffer::HBL_DISCARD);
235 const Ogre::PixelBox& smallPixelBox = pixelBufferMipmap->getCurrentLock();
236 ConversionTools::ScolBitmapGetRGBA(B, 0, smallPixelBox);
237
238 // Unlock the mipmap pixel buffer
239 pixelBufferMipmap->unlock();
240 }
241
242 // Unlock the pixel buffer
243 pixelBuffer->unlock();
244}
245
246void STexture::loadResource(Ogre::Resource* resource)
247{
248 if (mBuffer.data)
249 {
250 std::size_t textureNumMipmap = O3TexturePtr->getNumMipmaps();
251
252 // Get the texture pixel buffer
253 Ogre::HardwarePixelBufferSharedPtr pixelBuffer = O3TexturePtr->getBuffer(0, 0);
254
255 // Lock the pixel buffer and get a pixel box
256 pixelBuffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
257
258 const Ogre::PixelBox& largePixelBox = pixelBuffer->getCurrentLock();
259 Ogre::Image::scale(mBuffer, largePixelBox);
260
261 // make mip maps
262 for (size_t iMipmap = 1; iMipmap < textureNumMipmap; iMipmap++)
263 {
264 // Get the texture pixel buffer for the sub mipmap level
265 Ogre::HardwarePixelBufferSharedPtr pixelBufferMipmap = O3TexturePtr->getBuffer(0, iMipmap);
266
267 // Lock the pixel buffer and get a pixel box
268 pixelBufferMipmap->lock(Ogre::HardwareBuffer::HBL_DISCARD);
269 const Ogre::PixelBox& smallPixelBox = pixelBufferMipmap->getCurrentLock();
270 Ogre::Image::scale(mBuffer, smallPixelBox);
271
272 // Unlock the mipmap pixel buffer
273 pixelBufferMipmap->unlock();
274 }
275
276 // Unlock the pixel buffer
277 pixelBuffer->unlock();
278 }
279}
280
281}
SCOL_EXPORT int cbmachine w
Definition SO3SCOL.cpp:5150
static void ScolBitmapGetRGBA(PtrObjBitmap scolBitmap, PtrObjBitmap alphaBitmap, const Ogre::PixelBox &pixelbox)
bool GetRttPixelFormat(Ogre::PixelFormat &format, bool alpha=false, bool floattex=false)
Definition SO3Root.cpp:650
Ogre::RenderSystem * GetOgreRenderSystem()
Definition SO3Root.cpp:865
static SRoot & getSingleton()
Definition SO3Root.cpp:116
Ogre::TextureManager * O3TextureManager
Definition SO3Scene.h:131
SScene * mScene
Definition SO3Texture.h:46
SScene * GetScene()
std::string GetGroupName()
void BlitTexture(PtrObjBitmap B)
std::string mGroupName
Definition SO3Texture.h:47
void BlitAlphaTexture(PtrObjBitmap Bcolor, PtrObjBitmap Balpha)
Ogre::TexturePtr getOgreTexturePointer()
Ogre::TexturePtr O3TexturePtr
Definition SO3Texture.h:45