source: GTP/trunk/Lib/Illum/IBRBillboardCloudTrees/OGRE/src/IMGTexture.cpp @ 751

Revision 751, 6.6 KB checked in by igarcia, 19 years ago (diff)
Line 
1
2#include "IMGTexture.h"
3
4namespace IMG
5{
6
7Texture::Texture(): references(0) // initialize references to 0
8{
9        mTexture = 0;
10        mWidth = 0;
11        mHeight = 0;
12        mMaterial = 0;
13        mCalculatedAvgColour = false;
14        mRectangle2D = new Ogre::Rectangle2D(true);
15        mRectangle2D->setBoundingBox(Ogre::AxisAlignedBox(-100000.0 * Ogre::Vector3::UNIT_SCALE, 100000.0 * Ogre::Vector3::UNIT_SCALE));
16        mRectangle2D->setRenderQueueGroup(Ogre::RENDER_QUEUE_OVERLAY);
17}
18
19Texture::~Texture()
20{
21        // Gametools -- BUG: 06/04/2006
22        //if (mRectangle2D)
23        //{
24        //      delete mRectangle2D;
25        //}
26
27        //if (mMaterial)
28        //{
29        //      delete mMaterial;
30        //}
31}
32
33void Texture::debug()
34{
35        unsigned char* pBufferBox = new unsigned char[mTexture->getBuffer(0, 0)->getSizeInBytes()];
36
37    // Read pixels     
38        Ogre::PixelBox pBox(mWidth, mHeight, 1, mPixelFormat, pBufferBox);
39
40        mTexture->getBuffer(0, 0)->blitToMemory(pBox);
41
42        if (mPixelFormat == Ogre::PF_FLOAT32_RGBA)
43        {
44                imdebug("rgba w=%d h=%d %p b=32f",pBox.getWidth(), pBox.getHeight(), pBox.data);
45        }
46
47        if (mPixelFormat == Ogre::PF_A8R8G8B8)
48        {
49                //unsigned char* pBufferDest = new unsigned char[pBox.getWidth()*pBox.getHeight()*4];   
50                unsigned char* pBufferDest = new unsigned char[pBox.getWidth()*pBox.getHeight()*4];     
51                Ogre::PixelBox pDest(mWidth, mHeight, 1, Ogre::PF_BYTE_RGBA, pBufferDest);
52                Ogre::PixelUtil::bulkPixelConversion(pBox.data, mPixelFormat, pDest.data, Ogre::PF_BYTE_RGBA, pBox.getWidth()*pBox.getHeight());
53               
54                imdebug("rgba w=%d h=%d %p ", pDest.getWidth(), pDest.getHeight(), pDest.data);
55               
56                delete [] pBufferDest;
57        }
58
59        delete [] pBufferBox;
60}
61
62void Texture::setRenderTargetListener(Ogre::RenderTargetListener *renderTargetListener)
63{
64        mTexture->getBuffer(0, 0)->getRenderTarget()->addListener(renderTargetListener);
65}
66
67Ogre::Rectangle2D* Texture::getRectangle2D()
68{
69        return mRectangle2D;
70}
71
72void Texture::setCorners(Ogre::Real left, Ogre::Real top, Ogre::Real right, Ogre::Real bottom)
73{
74        mRectangle2D->setCorners(left, top, right, bottom);
75}
76
77void Texture::bind(Ogre::String textureName)
78{               
79        if (!Ogre::TextureManager::getSingleton().getByName(textureName).isNull())
80        {
81                mTexture = (Ogre::Texture*) Ogre::TextureManager::getSingleton().getByName(textureName).getPointer();
82                mWidth = mTexture->getSrcWidth();
83                mHeight = mTexture->getSrcHeight();
84        }
85        else
86        {
87                Ogre::TexturePtr tPtr = Ogre::TextureManager::getSingleton().load(textureName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
88
89                mTexture = (Ogre::Texture*) tPtr.getPointer();
90                mWidth = mTexture->getSrcWidth();
91                mHeight = mTexture->getSrcHeight();
92        }
93}
94
95void Texture::setBackgroundColour(Ogre::ColourValue colour)
96{
97        mBackgroundColour = colour;
98}
99
100Ogre::ColourValue Texture::getBackgroundColour()
101{
102        return mBackgroundColour;
103}
104
105Ogre::ColourValue Texture::getAvgColour()
106{
107        if (mCalculatedAvgColour)
108        {
109                return mAvgColour;
110        }
111        else
112        {
113                calculateAvgColour();
114                return mAvgColour;
115        }
116}
117
118void Texture::calculateAvgColour()
119{
120        unsigned int cR = 0;
121        unsigned int cG = 0;
122        unsigned int cB = 0;
123        unsigned int cA = 0;
124
125        Ogre::Image img;
126        img.load(mTexture->getName(), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
127        unsigned char* data = static_cast<unsigned char*>(img.getData());
128
129        unsigned int nColorpixels = 1;
130
131        for (unsigned int i = 0; i < mTexture->getWidth() * mTexture->getHeight() * 4; i=i+4)
132        {
133                cA = (unsigned short int)data[i+3];
134                if (cA != 0)
135                {
136                        cB = cB + data[i+2];
137                        cG = cG + data[i+1];
138                        cR = cR + data[i];                             
139
140                        nColorpixels++;
141                        //Ogre::LogManager::getSingleton().logMessage("(" + Ogre::StringConverter::toString(data[i]) + "," + Ogre::StringConverter::toString(data[i+1]) + "," + Ogre::StringConverter::toString(data[i+2]) + "," + Ogre::StringConverter::toString(data[i+3]) + ")");
142                }                                       
143        }
144
145        cR = cR / nColorpixels;
146        cG = cG / nColorpixels;
147        cB = cB / nColorpixels;
148
149        mAvgColour = Ogre::ColourValue(cR/255.0, cG/255.0, cB/255.0, 0.0);
150        mCalculatedAvgColour = true;
151}
152
153void Texture::load(Ogre::String folderName, Ogre::String fileName)
154{
155        Ogre::TextureManager::getSingleton().load(fileName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
156        mTexture = (Ogre::Texture*)Ogre::TextureManager::getSingleton().getByName(fileName).getPointer();
157        mWidth = mTexture->getSrcWidth();
158        mHeight = mTexture->getSrcHeight();
159}
160
161void Texture::save(Ogre::String folderName, Ogre::String filename)
162{
163    Ogre::HardwarePixelBufferSharedPtr readbuffer;
164    readbuffer = mTexture->getBuffer(0, 0);
165        unsigned char *tex = new unsigned char[readbuffer->getSizeInBytes()];
166        Ogre::PixelBox *pBox = new Ogre::PixelBox(mTexture->getWidth(), mTexture->getHeight(), mTexture->getDepth(), mTexture->getFormat(), tex);
167    readbuffer->blitToMemory((*pBox));
168   
169    Ogre::Image img;
170    img = img.loadDynamicImage(tex, mTexture->getWidth(), mTexture->getHeight(), mTexture->getDepth(), mTexture->getFormat(), true, 1, 0);             
171        img.resize(mWidth, mHeight);
172    img.save(folderName + filename);
173        delete pBox;
174}
175
176void Texture::update()
177{
178        mTexture->getBuffer(0, 0)->getRenderTarget()->update();
179}
180
181void Texture::setWidth(unsigned int width)
182{
183        mWidth = width;
184}
185
186unsigned int Texture::getWidth()
187{
188        return mWidth;
189}
190
191void Texture::setHeight(unsigned int height)
192{
193        mHeight = height;
194}
195
196unsigned int Texture::getHeight()
197{
198        return mHeight;
199}
200
201void Texture::setTextureName(Ogre::String textureName)
202{
203        mTextureName = textureName;
204}
205
206Ogre::String Texture::getTextureName()
207{
208        return mTextureName;
209}
210
211void Texture::setMaterial(Ogre::Material *material)
212{
213        mMaterial = material;
214        mRectangle2D->setMaterial(mMaterial->getName());
215}
216
217Ogre::Material* Texture::getMaterial()
218{
219        return mMaterial;
220}
221
222void Texture::create(Ogre::String fileName, unsigned int width, unsigned int height, Ogre::PixelFormat pixelFormat, Ogre::Camera *camera, Ogre::ColourValue backgroundColour)
223{
224        mCamera = camera;
225        mWidth = width;
226        mHeight = height;
227        mPixelFormat = pixelFormat;
228        mTexture = Ogre::TextureManager::getSingleton().createManual(fileName, "General", Ogre::TEX_TYPE_2D, mWidth, mHeight, 0, mPixelFormat, Ogre::TU_RENDERTARGET).getPointer();
229        mTexture->getBuffer(0, 0)->getRenderTarget();
230        Ogre::RenderTexture *rt = mTexture->getBuffer()->getRenderTarget();
231        rt->setAutoUpdated(false);
232        Ogre::Viewport *v = rt->addViewport(mCamera);
233        v->setClearEveryFrame(true);
234        v->setBackgroundColour(backgroundColour);
235        v->setOverlaysEnabled(false);
236        v->setDimensions(0,0,1.0,1.0); 
237        Ogre::Root::getSingleton().getRenderSystem()->clearFrameBuffer(Ogre::FBT_COLOUR, Ogre::ColourValue(1.0f,1.0f,1.0f,0.0f) );
238}
239
240void Texture::setCamera(Ogre::Camera *camera)
241{
242        mCamera = camera;
243}
244
245
246void Texture::setFormat(Ogre::PixelFormat pixelFormat)
247{
248        mPixelFormat = pixelFormat;
249}
250
251}
Note: See TracBrowser for help on using the repository browser.