1 | |
---|
2 | #include "IMGTexture.h" |
---|
3 | |
---|
4 | namespace IMG |
---|
5 | { |
---|
6 | |
---|
7 | Texture::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 | |
---|
19 | Texture::~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 | |
---|
33 | void Texture::setRenderTargetListener(Ogre::RenderTargetListener *renderTargetListener) |
---|
34 | { |
---|
35 | mTexture->getBuffer(0, 0)->getRenderTarget()->addListener(renderTargetListener); |
---|
36 | } |
---|
37 | |
---|
38 | Ogre::Rectangle2D* Texture::getRectangle2D() |
---|
39 | { |
---|
40 | return mRectangle2D; |
---|
41 | } |
---|
42 | |
---|
43 | void Texture::setCorners(Ogre::Real left, Ogre::Real top, Ogre::Real right, Ogre::Real bottom) |
---|
44 | { |
---|
45 | mRectangle2D->setCorners(left, top, right, bottom); |
---|
46 | } |
---|
47 | |
---|
48 | void Texture::bind(Ogre::String textureName) |
---|
49 | { |
---|
50 | if (!Ogre::TextureManager::getSingleton().getByName(textureName).isNull()) |
---|
51 | { |
---|
52 | mTexture = (Ogre::Texture*) Ogre::TextureManager::getSingleton().getByName(textureName).getPointer(); |
---|
53 | mWidth = mTexture->getSrcWidth(); |
---|
54 | mHeight = mTexture->getSrcHeight(); |
---|
55 | } |
---|
56 | else |
---|
57 | { |
---|
58 | Ogre::TexturePtr tPtr = Ogre::TextureManager::getSingleton().load(textureName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); |
---|
59 | |
---|
60 | mTexture = (Ogre::Texture*) tPtr.getPointer(); |
---|
61 | mWidth = mTexture->getSrcWidth(); |
---|
62 | mHeight = mTexture->getSrcHeight(); |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | void Texture::setBackgroundColour(Ogre::ColourValue colour) |
---|
67 | { |
---|
68 | mBackgroundColour = colour; |
---|
69 | } |
---|
70 | |
---|
71 | Ogre::ColourValue Texture::getBackgroundColour() |
---|
72 | { |
---|
73 | return mBackgroundColour; |
---|
74 | } |
---|
75 | |
---|
76 | Ogre::ColourValue Texture::getAvgColour() |
---|
77 | { |
---|
78 | if (mCalculatedAvgColour) |
---|
79 | { |
---|
80 | return mAvgColour; |
---|
81 | } |
---|
82 | else |
---|
83 | { |
---|
84 | calculateAvgColour(); |
---|
85 | return mAvgColour; |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | void Texture::calculateAvgColour() |
---|
90 | { |
---|
91 | unsigned int cR = 0;
|
---|
92 | unsigned int cG = 0;
|
---|
93 | unsigned int cB = 0;
|
---|
94 | unsigned int cA = 0;
|
---|
95 | |
---|
96 | Ogre::Image img;
|
---|
97 | img.load(mTexture->getName(), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
|
---|
98 | unsigned char* data = static_cast<unsigned char*>(img.getData());
|
---|
99 |
|
---|
100 | unsigned int nColorpixels = 1;
|
---|
101 |
|
---|
102 | for (unsigned int i = 0; i < mTexture->getWidth() * mTexture->getHeight() * 4; i=i+4)
|
---|
103 | {
|
---|
104 | cA = (unsigned short int)data[i+3];
|
---|
105 | if (cA != 0)
|
---|
106 | {
|
---|
107 | cB = cB + data[i+2];
|
---|
108 | cG = cG + data[i+1];
|
---|
109 | cR = cR + data[i];
|
---|
110 |
|
---|
111 | nColorpixels++;
|
---|
112 | //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]) + ")");
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | cR = cR / nColorpixels;
|
---|
117 | cG = cG / nColorpixels;
|
---|
118 | cB = cB / nColorpixels;
|
---|
119 |
|
---|
120 | mAvgColour = Ogre::ColourValue(cR/255.0, cG/255.0, cB/255.0, 0.0); |
---|
121 | mCalculatedAvgColour = true; |
---|
122 | } |
---|
123 | |
---|
124 | void Texture::load(Ogre::String folderName, Ogre::String fileName) |
---|
125 | { |
---|
126 | Ogre::TextureManager::getSingleton().load(fileName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); |
---|
127 | mTexture = (Ogre::Texture*)Ogre::TextureManager::getSingleton().getByName(fileName).getPointer(); |
---|
128 | mWidth = mTexture->getSrcWidth(); |
---|
129 | mHeight = mTexture->getSrcHeight(); |
---|
130 | } |
---|
131 | |
---|
132 | void Texture::save(Ogre::String folderName, Ogre::String filename) |
---|
133 | { |
---|
134 | Ogre::HardwarePixelBufferSharedPtr readbuffer; |
---|
135 | readbuffer = mTexture->getBuffer(0, 0); |
---|
136 | unsigned char *tex = new unsigned char[readbuffer->getSizeInBytes()]; |
---|
137 | Ogre::PixelBox *pBox = new Ogre::PixelBox(mTexture->getWidth(), mTexture->getHeight(), mTexture->getDepth(), mTexture->getFormat(), tex); |
---|
138 | readbuffer->blitToMemory((*pBox)); |
---|
139 | |
---|
140 | Ogre::Image img; |
---|
141 | img = img.loadDynamicImage(tex, mTexture->getWidth(), mTexture->getHeight(), mTexture->getDepth(), mTexture->getFormat(), true, 1, 0); |
---|
142 | img.resize(mWidth, mHeight); |
---|
143 | img.save(folderName + filename); |
---|
144 | delete pBox; |
---|
145 | } |
---|
146 | |
---|
147 | void Texture::update() |
---|
148 | { |
---|
149 | mTexture->getBuffer(0, 0)->getRenderTarget()->update(); |
---|
150 | } |
---|
151 | |
---|
152 | void Texture::setWidth(unsigned int width) |
---|
153 | { |
---|
154 | mWidth = width; |
---|
155 | } |
---|
156 | |
---|
157 | unsigned int Texture::getWidth() |
---|
158 | { |
---|
159 | return mWidth; |
---|
160 | } |
---|
161 | |
---|
162 | void Texture::setHeight(unsigned int height) |
---|
163 | { |
---|
164 | mHeight = height; |
---|
165 | } |
---|
166 | |
---|
167 | unsigned int Texture::getHeight() |
---|
168 | { |
---|
169 | return mHeight; |
---|
170 | } |
---|
171 | |
---|
172 | void Texture::setTextureName(Ogre::String textureName) |
---|
173 | { |
---|
174 | mTextureName = textureName; |
---|
175 | } |
---|
176 | |
---|
177 | Ogre::String Texture::getTextureName() |
---|
178 | { |
---|
179 | return mTextureName; |
---|
180 | } |
---|
181 | |
---|
182 | void Texture::setMaterial(Ogre::Material *material) |
---|
183 | { |
---|
184 | mMaterial = material; |
---|
185 | mRectangle2D->setMaterial(mMaterial->getName()); |
---|
186 | } |
---|
187 | |
---|
188 | Ogre::Material* Texture::getMaterial() |
---|
189 | { |
---|
190 | return mMaterial; |
---|
191 | } |
---|
192 | |
---|
193 | void Texture::create(Ogre::String fileName, unsigned int width, unsigned int height, Ogre::PixelFormat pixelFormat, Ogre::Camera *camera) |
---|
194 | { |
---|
195 | mCamera = camera; |
---|
196 | mWidth = width; |
---|
197 | mHeight = height; |
---|
198 | mPixelFormat = pixelFormat; |
---|
199 | mTexture = Ogre::TextureManager::getSingleton().createManual(fileName, "General", Ogre::TEX_TYPE_2D, mWidth, mHeight, 0, mPixelFormat, Ogre::TU_RENDERTARGET).getPointer();
|
---|
200 | mTexture->getBuffer(0, 0)->getRenderTarget();
|
---|
201 | Ogre::RenderTexture *rt = mTexture->getBuffer()->getRenderTarget();
|
---|
202 | rt->setAutoUpdated(false);
|
---|
203 | Ogre::Viewport *v = rt->addViewport(mCamera);
|
---|
204 | v->setClearEveryFrame(true);
|
---|
205 | v->setBackgroundColour(Ogre::ColourValue(1.0f, 1.0f, 1.0f, 0.0f));
|
---|
206 | v->setOverlaysEnabled(false);
|
---|
207 | v->setDimensions(0,0,1.0,1.0); |
---|
208 | Ogre::Root::getSingleton().getRenderSystem()->clearFrameBuffer(Ogre::FBT_COLOUR, Ogre::ColourValue(1.0f,1.0f,1.0f,0.0f) ); |
---|
209 | } |
---|
210 | |
---|
211 | void Texture::setCamera(Ogre::Camera *camera) |
---|
212 | { |
---|
213 | mCamera = camera; |
---|
214 | } |
---|
215 | |
---|
216 | |
---|
217 | void Texture::setFormat(Ogre::PixelFormat pixelFormat) |
---|
218 | { |
---|
219 | mPixelFormat = pixelFormat; |
---|
220 | } |
---|
221 | |
---|
222 | } |
---|