1 | |
---|
2 | #include "IMGTextureAtlas.h" |
---|
3 | |
---|
4 | namespace IMG { |
---|
5 | |
---|
6 | TextureAtlas::TextureAtlas(): references(0) // initialize references to 0 |
---|
7 | { |
---|
8 | mRectangle2D->setBoundingBox(Ogre::AxisAlignedBox(-100000.0 * Ogre::Vector3::UNIT_SCALE, 100000.0 * Ogre::Vector3::UNIT_SCALE)); |
---|
9 | mRectangle2D->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND); |
---|
10 | mCurrentTextureAtlas = TEXTURE_ATLAS_PING; |
---|
11 | mFirstUpdate = true; |
---|
12 | } |
---|
13 | |
---|
14 | TextureAtlas::~TextureAtlas() |
---|
15 | { |
---|
16 | mTextureList.clear(); |
---|
17 | } |
---|
18 | |
---|
19 | unsigned int TextureAtlas::getReference() |
---|
20 | { |
---|
21 | return references; |
---|
22 | } |
---|
23 | |
---|
24 | void TextureAtlas::update() |
---|
25 | { |
---|
26 | if (mCurrentTextureAtlas == TEXTURE_ATLAS_PING) |
---|
27 | { |
---|
28 | if (!mFirstUpdate) |
---|
29 | { |
---|
30 | mMaterial->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(mTextureAtlasPong->getName()); |
---|
31 | mMaterial->reload(); |
---|
32 | mTexture->getBuffer(0, 0)->getRenderTarget()->update(); |
---|
33 | } |
---|
34 | |
---|
35 | if (mFirstUpdate) |
---|
36 | { |
---|
37 | mFirstUpdate = false; |
---|
38 | mTexture->getBuffer(0, 0)->getRenderTarget()->update(); |
---|
39 | } |
---|
40 | } |
---|
41 | else |
---|
42 | { |
---|
43 | mMaterial->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(mTexture->getName()); |
---|
44 | mMaterial->reload(); |
---|
45 | mTextureAtlasPong->getBuffer(0, 0)->getRenderTarget()->update(); |
---|
46 | } |
---|
47 | |
---|
48 | mCurrentTextureAtlas++; |
---|
49 | mCurrentTextureAtlas = mCurrentTextureAtlas % 2; |
---|
50 | } |
---|
51 | |
---|
52 | void TextureAtlas::setTextureAtlasName(Ogre::String textureAtlasName) |
---|
53 | { |
---|
54 | mTextureAtlasName = textureAtlasName; |
---|
55 | } |
---|
56 | |
---|
57 | Ogre::String TextureAtlas::getTextureAtlasName() |
---|
58 | { |
---|
59 | return mTextureAtlasName; |
---|
60 | } |
---|
61 | |
---|
62 | void TextureAtlas::addTexture(TexturePtr texture) |
---|
63 | { |
---|
64 | mTextureList.push_back(texture); |
---|
65 | mTextureAtlasSceneNode->attachObject(texture->getRectangle2D()); |
---|
66 | } |
---|
67 | |
---|
68 | TexturePtr TextureAtlas::getTexture(unsigned int iTexture) |
---|
69 | { |
---|
70 | return mTextureList[iTexture]; |
---|
71 | } |
---|
72 | |
---|
73 | void TextureAtlas::removeTexture(unsigned int iTexture) |
---|
74 | { |
---|
75 | mTextureList.erase(mTextureList.begin()+iTexture); |
---|
76 | } |
---|
77 | |
---|
78 | unsigned int TextureAtlas::getNumTextures() |
---|
79 | { |
---|
80 | return mTextureList.size(); |
---|
81 | } |
---|
82 | |
---|
83 | void TextureAtlas::setTextureAtlasSceneNode(Ogre::SceneNode *textureAtlasSceneNode) |
---|
84 | { |
---|
85 | mTextureAtlasSceneNode = textureAtlasSceneNode; |
---|
86 | mTextureAtlasSceneNode->attachObject(mRectangle2D); |
---|
87 | } |
---|
88 | |
---|
89 | void TextureAtlas::create(Ogre::String fileName, unsigned int width, unsigned int height, Ogre::PixelFormat pixelFormat, Ogre::Camera *camera, Ogre::ColourValue backgroundColour) |
---|
90 | { |
---|
91 | mCamera = camera; |
---|
92 | mWidth = width; |
---|
93 | mHeight = height; |
---|
94 | mPixelFormat = pixelFormat; |
---|
95 | |
---|
96 | {
|
---|
97 | mTexture = Ogre::TextureManager::getSingleton().createManual(fileName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D, mWidth, mHeight, 0, mPixelFormat, Ogre::TU_RENDERTARGET).getPointer();
|
---|
98 | mTexture->getBuffer(0, 0)->getRenderTarget();
|
---|
99 | Ogre::RenderTexture *rt = mTexture->getBuffer()->getRenderTarget();
|
---|
100 | rt->setAutoUpdated(false);
|
---|
101 | Ogre::Viewport *v = rt->addViewport(mCamera);
|
---|
102 | v->setClearEveryFrame(true);
|
---|
103 | v->setBackgroundColour(backgroundColour);
|
---|
104 | v->setOverlaysEnabled(false);
|
---|
105 | v->setDimensions(0,0,1.0,1.0); |
---|
106 | } |
---|
107 | |
---|
108 | { |
---|
109 | mTextureAtlasPong = Ogre::TextureManager::getSingleton().createManual("PONG" + fileName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D, mWidth, mHeight, 0, mPixelFormat, Ogre::TU_RENDERTARGET).getPointer();
|
---|
110 | mTextureAtlasPong->getBuffer(0, 0)->getRenderTarget();
|
---|
111 | Ogre::RenderTexture *rt = mTextureAtlasPong->getBuffer()->getRenderTarget();
|
---|
112 | rt->setAutoUpdated(false);
|
---|
113 | Ogre::Viewport *v = rt->addViewport(mCamera);
|
---|
114 | v->setClearEveryFrame(true);
|
---|
115 | v->setBackgroundColour(backgroundColour);
|
---|
116 | v->setOverlaysEnabled(false);
|
---|
117 | v->setDimensions(0,0,1.0,1.0); |
---|
118 | } |
---|
119 | Ogre::Root::getSingleton().getRenderSystem()->clearFrameBuffer(Ogre::FBT_COLOUR, Ogre::ColourValue(1.0f,1.0f,1.0f,0.0f) ); |
---|
120 | } |
---|
121 | |
---|
122 | Ogre::SceneNode* TextureAtlas::getTextureAtlasSceneNode() |
---|
123 | { |
---|
124 | return mTextureAtlasSceneNode; |
---|
125 | } |
---|
126 | |
---|
127 | void TextureAtlas::debug() |
---|
128 | { |
---|
129 | Ogre::Texture *texture; |
---|
130 | if (mCurrentTextureAtlas == TEXTURE_ATLAS_PONG) |
---|
131 | { |
---|
132 | texture = mTexture; |
---|
133 | } |
---|
134 | else |
---|
135 | { |
---|
136 | texture = mTextureAtlasPong; |
---|
137 | } |
---|
138 | |
---|
139 | unsigned char* pBufferBox = new unsigned char[texture->getBuffer(0, 0)->getSizeInBytes()];
|
---|
140 |
|
---|
141 | // Read pixels
|
---|
142 | Ogre::PixelBox pBox(mWidth, mHeight, 1, mPixelFormat, pBufferBox);
|
---|
143 |
|
---|
144 | texture->getBuffer(0, 0)->blitToMemory(pBox);
|
---|
145 |
|
---|
146 | if (mPixelFormat == Ogre::PF_FLOAT32_RGBA)
|
---|
147 | {
|
---|
148 | imdebug("rgba w=%d h=%d %p b=32f",pBox.getWidth(), pBox.getHeight(), pBox.data);
|
---|
149 | } |
---|
150 | |
---|
151 | if (mPixelFormat == Ogre::PF_A8R8G8B8)
|
---|
152 | {
|
---|
153 | //unsigned char* pBufferDest = new unsigned char[pBox.getWidth()*pBox.getHeight()*4];
|
---|
154 | unsigned char* pBufferDest = new unsigned char[pBox.getWidth()*pBox.getHeight()*4];
|
---|
155 | Ogre::PixelBox pDest(mWidth, mHeight, 1, Ogre::PF_BYTE_RGBA, pBufferDest);
|
---|
156 | Ogre::PixelUtil::bulkPixelConversion(pBox.data, mPixelFormat, pDest.data, Ogre::PF_BYTE_RGBA, pBox.getWidth()*pBox.getHeight());
|
---|
157 |
|
---|
158 | imdebug("rgba w=%d h=%d %p ", pDest.getWidth(), pDest.getHeight(), pDest.data);
|
---|
159 |
|
---|
160 | delete [] pBufferDest;
|
---|
161 | } |
---|
162 | |
---|
163 | delete [] pBufferBox; |
---|
164 | } |
---|
165 | |
---|
166 | void TextureAtlas::save(Ogre::String folderName, Ogre::String fileName, Ogre::PixelFormat pixelFormat) |
---|
167 | {
|
---|
168 | Ogre::Texture *texture; |
---|
169 | |
---|
170 | if (mCurrentTextureAtlas == TEXTURE_ATLAS_PONG) |
---|
171 | { |
---|
172 | texture = mTexture; |
---|
173 | } |
---|
174 | else |
---|
175 | { |
---|
176 | texture = mTextureAtlasPong; |
---|
177 | } |
---|
178 | |
---|
179 | unsigned char* pBufferBox = new unsigned char[texture->getBuffer(0, 0)->getSizeInBytes()];
|
---|
180 | unsigned char* pBufferDest = new unsigned char[texture->getBuffer(0, 0)->getSizeInBytes()];
|
---|
181 |
|
---|
182 | // Read pixels
|
---|
183 | Ogre::PixelBox pBox(mWidth, mHeight, 1, mPixelFormat, pBufferBox);
|
---|
184 | Ogre::PixelBox pDest(mWidth, mHeight, 1, pixelFormat, pBufferDest);
|
---|
185 | texture->getBuffer(0, 0)->blitToMemory(pBox);
|
---|
186 | Ogre::PixelUtil::bulkPixelConversion(pBox.data,mPixelFormat,pDest.data,pixelFormat,pBox.getWidth()*pBox.getHeight());
|
---|
187 |
|
---|
188 | // copyToMemory
|
---|
189 | Ogre::ImageCodec::ImageData *imgData = new Ogre::ImageCodec::ImageData();
|
---|
190 |
|
---|
191 | imgData->width = mWidth;
|
---|
192 | imgData->height = mHeight;
|
---|
193 | imgData->depth = 1;
|
---|
194 | imgData->format = pixelFormat;
|
---|
195 |
|
---|
196 | // Wrap buffer in a chunk
|
---|
197 | Ogre::MemoryDataStreamPtr stream(new Ogre::MemoryDataStream(pBufferDest, texture->getBuffer(0, 0)->getSizeInBytes(), false));
|
---|
198 |
|
---|
199 | // Get codec
|
---|
200 | size_t pos = fileName.find_last_of(".");
|
---|
201 | Ogre::String extension;
|
---|
202 | if( pos == Ogre::String::npos )
|
---|
203 | {
|
---|
204 | OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS, "Unable to determine image type for '" + fileName + "' - invalid extension.", "GLRenderTexture::writeContentsToFile" );
|
---|
205 | }
|
---|
206 |
|
---|
207 | while( pos != fileName.length() - 1 )
|
---|
208 | {
|
---|
209 | extension += fileName[++pos];
|
---|
210 | }
|
---|
211 |
|
---|
212 | // Get the codec
|
---|
213 | Ogre::Codec * pCodec = Ogre::Codec::getCodec(extension);
|
---|
214 |
|
---|
215 | // Write out
|
---|
216 | Ogre::Codec::CodecDataPtr codecDataPtr(imgData);
|
---|
217 |
|
---|
218 | pCodec->codeToFile(stream,folderName + fileName, codecDataPtr);
|
---|
219 |
|
---|
220 | delete [] pBufferDest; |
---|
221 | delete [] pBufferBox; |
---|
222 | } |
---|
223 | |
---|
224 | } |
---|