Index: GTP/trunk/Lib/Illum/IBRBillboardCloudTrees/OGRE/src/IMGTextureAtlas.cpp
===================================================================
--- GTP/trunk/Lib/Illum/IBRBillboardCloudTrees/OGRE/src/IMGTextureAtlas.cpp	(revision 721)
+++ GTP/trunk/Lib/Illum/IBRBillboardCloudTrees/OGRE/src/IMGTextureAtlas.cpp	(revision 731)
@@ -4,47 +4,146 @@
 namespace IMG {
 
-    TextureAtlas::TextureAtlas()
+TextureAtlas::TextureAtlas(): references(0) // initialize references to 0
+{
+	mRectangle2D->setBoundingBox(Ogre::AxisAlignedBox(-100000.0 * Ogre::Vector3::UNIT_SCALE, 100000.0 * Ogre::Vector3::UNIT_SCALE));
+	mRectangle2D->setRenderQueueGroup(Ogre::RENDER_QUEUE_BACKGROUND);
+}
+
+TextureAtlas::~TextureAtlas()
+{
+	mTextureList.clear();
+}
+
+unsigned int TextureAtlas::getReference()
+{
+	return references;
+}
+
+void TextureAtlas::setTextureAtlasName(Ogre::String textureAtlasName)
+{
+	mTextureAtlasName = textureAtlasName;
+}
+
+Ogre::String TextureAtlas::getTextureAtlasName()
+{
+	return mTextureAtlasName;
+}
+
+void TextureAtlas::addTexture(TexturePtr texture)
+{
+	mTextureList.push_back(texture);
+	mTextureAtlasSceneNode->attachObject(texture->getRectangle2D());
+}
+
+TexturePtr TextureAtlas::getTexture(unsigned int iTexture)
+{
+	return mTextureList[iTexture];
+}
+
+void TextureAtlas::removeTexture(unsigned int iTexture)
+{
+	mTextureList.erase(mTextureList.begin()+iTexture);
+}
+
+unsigned int TextureAtlas::getNumTextures()
+{
+	return mTextureList.size();
+}
+
+void TextureAtlas::setTextureAtlasSceneNode(Ogre::SceneNode *textureAtlasSceneNode)
+{
+	mTextureAtlasSceneNode = textureAtlasSceneNode;	
+	mTextureAtlasSceneNode->attachObject(mRectangle2D);
+}
+
+Ogre::SceneNode* TextureAtlas::getTextureAtlasSceneNode()
+{
+	return mTextureAtlasSceneNode;
+}
+
+void TextureAtlas::setCamera(Ogre::Camera *camera)
+{
+	mCamera = camera;
+}
+
+void TextureAtlas::update()
+{
+	mTexture->getBuffer(0, 0)->getRenderTarget()->update();
+}
+
+void TextureAtlas::save(Ogre::String folderName, Ogre::String fileName, Ogre::PixelFormat pixelFormat)
+{	
+	unsigned char* pBufferBox = new unsigned char[mTexture->getBuffer(0, 0)->getSizeInBytes()];
+	unsigned char* pBufferDest = new unsigned char[mTexture->getBuffer(0, 0)->getSizeInBytes()];
+
+    // Read pixels	
+	Ogre::PixelBox pBox(mWidth, mHeight, 1, mPixelFormat, pBufferBox);
+	Ogre::PixelBox pDest(mWidth, mHeight, 1, pixelFormat, pBufferDest);	
+	mTexture->getBuffer(0, 0)->blitToMemory(pBox);
+	Ogre::PixelUtil::bulkPixelConversion(pBox.data,mPixelFormat,pDest.data,pixelFormat,pBox.getWidth()*pBox.getHeight());
+
+	//if (pixelFormat == Ogre::PF_FLOAT32_RGBA)
+	//{
+	//	imdebug("rgba w=%d h=%d %p b=32f",pDest.getWidth(),pDest.getHeight(),pDest.data);
+	//}
+
+	// copyToMemory
+	Ogre::ImageCodec::ImageData *imgData = new Ogre::ImageCodec::ImageData();
+    
+    imgData->width = mWidth;
+    imgData->height = mHeight;
+	imgData->depth = 1;	
+    imgData->format = pixelFormat;
+
+	// Wrap buffer in a chunk
+	Ogre::MemoryDataStreamPtr stream(new Ogre::MemoryDataStream(pBufferDest, mTexture->getBuffer(0, 0)->getSizeInBytes(), false));
+
+    // Get codec 
+    size_t pos = fileName.find_last_of(".");
+    Ogre::String extension;
+    if( pos == Ogre::String::npos )
 	{
+        OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS, "Unable to determine image type for '" + fileName + "' - invalid extension.", "GLRenderTexture::writeContentsToFile" );
 	}
 
-    virtual TextureAtlas::~TextureAtlas()
+    while( pos != fileName.length() - 1 )
 	{
+        extension += fileName[++pos];
 	}
 
-    void TextureAtlas::setRenderSourceName(Ogre::String renderSourceName)
-	{
-		mRenderSourceName = renderSourceName;
-	}
+    // Get the codec
+    Ogre::Codec * pCodec = Ogre::Codec::getCodec(extension);
 
-    Ogre::String TextureAtlas::getRenderSourceName()
-	{
-		return mRenderSourceName;
-	}
+    // Write out
+    Ogre::Codec::CodecDataPtr codecDataPtr(imgData);
 
-	void TextureAtlas::setRenderSourceName(Ogre::String renderSourceName)
-	{
-		mRenderSourceName = renderSourceName;
-	}
+	pCodec->codeToFile(stream,folderName + fileName, codecDataPtr);
 
-	void TextureAtlas::addTexture(Texture *texture)
-	{
-		mTextureList.push_back(texture);
-	}
+	delete [] pBufferDest;
+	delete [] pBufferBox;
+}
 
-	void TextureAtlas::getTexture(unsigned int iTexture)
-	{
-		return mTextureList[iTexture];
-	}
+void TextureAtlas::setFormat(Ogre::PixelFormat pixelFormat)
+{
+	mPixelFormat = pixelFormat;
+}
 
-	void TextureAtlas::removeTexture(unsigned int iTexture)
-	{
-		Texture *texture = mTextureList[iTexture];
-		mTextureList.erase(mTextureList.begin()+iTexture);
-		delete texture;
-	}
+void TextureAtlas::create(Ogre::String fileName, unsigned int width, unsigned int height, Ogre::PixelFormat pixelFormat, Ogre::Camera *camera)
+{
+	mCamera = camera;
+	mWidth = width;
+	mHeight = height;
+	mPixelFormat = pixelFormat;
+	mTexture = Ogre::TextureManager::getSingleton().createManual(fileName, "General", Ogre::TEX_TYPE_2D, mWidth, mHeight, 0, mPixelFormat, Ogre::TU_RENDERTARGET).getPointer();
+	mTexture->getBuffer(0, 0)->getRenderTarget();
+	Ogre::RenderTexture *rt = mTexture->getBuffer()->getRenderTarget();
+	rt->setAutoUpdated(false);
+	Ogre::Viewport *v = rt->addViewport(mCamera);
+	v->setClearEveryFrame(true);
+	v->setBackgroundColour(Ogre::ColourValue(1.0f, 1.0f, 1.0f, 0.0f));
+	v->setOverlaysEnabled(false);
+	v->setDimensions(0,0,1.0,1.0);	
+	Ogre::Root::getSingleton().getRenderSystem()->clearFrameBuffer(Ogre::FBT_COLOUR, Ogre::ColourValue(1.0f,1.0f,1.0f,0.0f) );
+}
 
-	unsigned int TextureAtlas::getNumTextures()
-	{
-		return mTextureList.size();
-	}
 }
