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 | Ogre::Rectangle2D* Texture::getRectangle2D() |
---|
34 | { |
---|
35 | return mRectangle2D; |
---|
36 | } |
---|
37 | |
---|
38 | void Texture::setCorners(Ogre::Real left, Ogre::Real top, Ogre::Real right, Ogre::Real bottom) |
---|
39 | { |
---|
40 | mRectangle2D->setCorners(left, top, right, bottom); |
---|
41 | } |
---|
42 | |
---|
43 | void Texture::bind(Ogre::String textureName) |
---|
44 | { |
---|
45 | if (!Ogre::TextureManager::getSingleton().getByName(textureName).isNull()) |
---|
46 | { |
---|
47 | mTexture = (Ogre::Texture*) Ogre::TextureManager::getSingleton().getByName(textureName).getPointer(); |
---|
48 | mWidth = mTexture->getSrcWidth(); |
---|
49 | mHeight = mTexture->getSrcHeight(); |
---|
50 | } |
---|
51 | else |
---|
52 | { |
---|
53 | Ogre::TexturePtr tPtr = Ogre::TextureManager::getSingleton().load(textureName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); |
---|
54 | |
---|
55 | mTexture = (Ogre::Texture*) tPtr.getPointer(); |
---|
56 | mWidth = mTexture->getSrcWidth(); |
---|
57 | mHeight = mTexture->getSrcHeight(); |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | void Texture::setBackgroundColour(Ogre::ColourValue colour) |
---|
62 | { |
---|
63 | mBackgroundColour = colour; |
---|
64 | } |
---|
65 | |
---|
66 | Ogre::ColourValue Texture::getBackgroundColour() |
---|
67 | { |
---|
68 | return mBackgroundColour; |
---|
69 | } |
---|
70 | |
---|
71 | Ogre::ColourValue Texture::getAvgColour() |
---|
72 | { |
---|
73 | if (mCalculatedAvgColour) |
---|
74 | { |
---|
75 | return mAvgColour; |
---|
76 | } |
---|
77 | else |
---|
78 | { |
---|
79 | calculateAvgColour(); |
---|
80 | return mAvgColour; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | void Texture::calculateAvgColour() |
---|
85 | { |
---|
86 | unsigned int cR = 0;
|
---|
87 | unsigned int cG = 0;
|
---|
88 | unsigned int cB = 0;
|
---|
89 | unsigned int cA = 0;
|
---|
90 | |
---|
91 | Ogre::Image img;
|
---|
92 | img.load(mTexture->getName(), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
|
---|
93 | unsigned char* data = static_cast<unsigned char*>(img.getData());
|
---|
94 |
|
---|
95 | unsigned int nColorpixels = 1;
|
---|
96 |
|
---|
97 | for (unsigned int i = 0; i < mTexture->getWidth() * mTexture->getHeight() * 4; i=i+4)
|
---|
98 | {
|
---|
99 | cA = (unsigned short int)data[i+3];
|
---|
100 | if (cA != 0)
|
---|
101 | {
|
---|
102 | cB = cB + data[i+2];
|
---|
103 | cG = cG + data[i+1];
|
---|
104 | cR = cR + data[i];
|
---|
105 |
|
---|
106 | nColorpixels++;
|
---|
107 | //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]) + ")");
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | cR = cR / nColorpixels;
|
---|
112 | cG = cG / nColorpixels;
|
---|
113 | cB = cB / nColorpixels;
|
---|
114 |
|
---|
115 | mAvgColour = Ogre::ColourValue(cR/255.0, cG/255.0, cB/255.0, 0.0); |
---|
116 | mCalculatedAvgColour = true; |
---|
117 | } |
---|
118 | |
---|
119 | void Texture::load(Ogre::String folderName, Ogre::String fileName) |
---|
120 | { |
---|
121 | Ogre::TextureManager::getSingleton().load(fileName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); |
---|
122 | mTexture = (Ogre::Texture*)Ogre::TextureManager::getSingleton().getByName(fileName).getPointer(); |
---|
123 | mWidth = mTexture->getSrcWidth(); |
---|
124 | mHeight = mTexture->getSrcHeight(); |
---|
125 | } |
---|
126 | |
---|
127 | void Texture::save(Ogre::String folderName, Ogre::String filename) |
---|
128 | { |
---|
129 | Ogre::HardwarePixelBufferSharedPtr readbuffer; |
---|
130 | readbuffer = mTexture->getBuffer(0, 0); |
---|
131 | unsigned char *tex = new unsigned char[readbuffer->getSizeInBytes()]; |
---|
132 | Ogre::PixelBox *pBox = new Ogre::PixelBox(mTexture->getWidth(), mTexture->getHeight(), mTexture->getDepth(), mTexture->getFormat(), tex); |
---|
133 | readbuffer->blitToMemory((*pBox)); |
---|
134 | |
---|
135 | Ogre::Image img; |
---|
136 | img = img.loadDynamicImage(tex, mTexture->getWidth(), mTexture->getHeight(), mTexture->getDepth(), mTexture->getFormat(), true, 1, 0); |
---|
137 | img.resize(mWidth, mHeight); |
---|
138 | img.save(folderName + filename); |
---|
139 | delete pBox; |
---|
140 | } |
---|
141 | |
---|
142 | void Texture::rotate(Ogre::Real rRotation) |
---|
143 | { |
---|
144 | //mTexture->setTextureRotate( Degree( -rRotation ) ); |
---|
145 | } |
---|
146 | |
---|
147 | void Texture::setWidth(unsigned int width) |
---|
148 | { |
---|
149 | mWidth = width; |
---|
150 | } |
---|
151 | |
---|
152 | unsigned int Texture::getWidth() |
---|
153 | { |
---|
154 | return mWidth; |
---|
155 | } |
---|
156 | |
---|
157 | void Texture::setHeight(unsigned int height) |
---|
158 | { |
---|
159 | mHeight = height; |
---|
160 | } |
---|
161 | |
---|
162 | unsigned int Texture::getHeight() |
---|
163 | { |
---|
164 | return mHeight; |
---|
165 | } |
---|
166 | |
---|
167 | void Texture::setTextureName(Ogre::String textureName) |
---|
168 | { |
---|
169 | mTextureName = textureName; |
---|
170 | } |
---|
171 | |
---|
172 | Ogre::String Texture::getTextureName() |
---|
173 | { |
---|
174 | return mTextureName; |
---|
175 | } |
---|
176 | |
---|
177 | void Texture::setMaterial(Ogre::Material *material) |
---|
178 | { |
---|
179 | mMaterial = material; |
---|
180 | mRectangle2D->setMaterial(mMaterial->getName()); |
---|
181 | } |
---|
182 | |
---|
183 | Ogre::Material* Texture::getMaterial() |
---|
184 | { |
---|
185 | return mMaterial; |
---|
186 | } |
---|
187 | |
---|
188 | } |
---|