1 | |
---|
2 | #include "LBBCEntityTextureAtlasViewMode.h" |
---|
3 | |
---|
4 | namespace LBBC |
---|
5 | { |
---|
6 | |
---|
7 | EntityTextureAtlasViewMode::EntityTextureAtlasViewMode(Ogre::RenderWindow* win, unsigned int ogreFrameListenerModeHandle, bool useBufferedInputKeys, bool useBufferedInputMouse) |
---|
8 | :OBA::OgreFrameListenerMode(win, ogreFrameListenerModeHandle, useBufferedInputKeys, useBufferedInputMouse) |
---|
9 | { |
---|
10 | mTextureAtlasGenerated = false; |
---|
11 | mFrame = 0; |
---|
12 | showDebugOverlay(false); |
---|
13 | } |
---|
14 | |
---|
15 | EntityTextureAtlasViewMode::~EntityTextureAtlasViewMode() |
---|
16 | { |
---|
17 | // Gametools -- BUG: 06/04/2006 |
---|
18 | //destroyScene(); |
---|
19 | } |
---|
20 | |
---|
21 | void EntityTextureAtlasViewMode::setTextureAtlasFolder(Ogre::String textureAtlasFolder) |
---|
22 | { |
---|
23 | mTextureAtlasFolder = textureAtlasFolder; |
---|
24 | } |
---|
25 | |
---|
26 | void EntityTextureAtlasViewMode::setTextureName(Ogre::String textureName)
|
---|
27 | {
|
---|
28 | mTextureName = textureName;
|
---|
29 | }
|
---|
30 |
|
---|
31 | void EntityTextureAtlasViewMode::setTextureAtlasName(Ogre::String textureAtlasName)
|
---|
32 | {
|
---|
33 | mTextureAtlasName = textureAtlasName;
|
---|
34 | }
|
---|
35 |
|
---|
36 | void EntityTextureAtlasViewMode::setTextureAtlasSize(unsigned int size) |
---|
37 | { |
---|
38 | mTextureAtlasSize = size; |
---|
39 | } |
---|
40 | |
---|
41 | void EntityTextureAtlasViewMode::setTextureAtlasNumSamples(unsigned int numSamples) |
---|
42 | { |
---|
43 | mNumSamples = numSamples; |
---|
44 | } |
---|
45 | |
---|
46 | void EntityTextureAtlasViewMode::setTextureAtlasBitRange(unsigned int bitRange) |
---|
47 | { |
---|
48 | mBitRange = bitRange; |
---|
49 | } |
---|
50 | |
---|
51 | bool EntityTextureAtlasViewMode::frameStarted(const Ogre::FrameEvent& evt, Ogre::InputReader *inputReader) |
---|
52 | { |
---|
53 | mWindow->getViewport(0)->setBackgroundColour(Ogre::ColourValue(1.0, 1.0, 1.0, 1.0)); |
---|
54 | |
---|
55 | if (mFrame == 1) |
---|
56 | { |
---|
57 | mTextureAtlas->getTextureAtlasSceneNode()->setVisible(false); |
---|
58 | mTextureAtlas->update(); |
---|
59 | mTextureAtlas->getTextureAtlasSceneNode()->setVisible(true); |
---|
60 | } |
---|
61 | |
---|
62 | if (mFrame == 2) |
---|
63 | { |
---|
64 | if (!mTextureAtlasGenerated) |
---|
65 | {
|
---|
66 | if (mBitRange == 32)
|
---|
67 | {
|
---|
68 | mDestPixelFormat = Ogre::PF_FLOAT32_RGBA;
|
---|
69 | }
|
---|
70 | else if (mBitRange == 16)
|
---|
71 | {
|
---|
72 | mDestPixelFormat = Ogre::PF_FLOAT16_RGBA;
|
---|
73 | }
|
---|
74 | else // mBitRange == 8
|
---|
75 | {
|
---|
76 | mDestPixelFormat = Ogre::PF_BYTE_RGBA;
|
---|
77 | }
|
---|
78 | |
---|
79 | mTextureAtlas->update(); |
---|
80 | //mTextureAtlas->debug(); |
---|
81 | mTextureAtlas->save(mTextureAtlasFolder, mTextureAtlasName, mDestPixelFormat); |
---|
82 | mTextureAtlasGenerated = true; |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | mFrame++; |
---|
87 | |
---|
88 | return OgreFrameListenerMode::frameStarted(evt, inputReader); |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | void EntityTextureAtlasViewMode::createScene() |
---|
93 | { |
---|
94 | IMG::TexturePtr texturePtr ( new IMG::Texture() );
|
---|
95 | texturePtr->bind(mTextureName);
|
---|
96 | Ogre::ColourValue avgColour = texturePtr->getAvgColour(); |
---|
97 | mWindow->getViewport(0)->setBackgroundColour(Ogre::ColourValue(1.0, 1.0, 1.0, 1.0)); |
---|
98 | |
---|
99 | mTextureAtlasNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
|
---|
100 | mTextureAtlas = IMG::TextureAtlasPtr( new IMG::TextureAtlas() );
|
---|
101 |
|
---|
102 | if (mBitRange == 32)
|
---|
103 | {
|
---|
104 | mSrcPixelFormat = Ogre::PF_FLOAT32_RGBA;
|
---|
105 | }
|
---|
106 | else if (mBitRange == 16)
|
---|
107 | {
|
---|
108 | mSrcPixelFormat = Ogre::PF_FLOAT16_RGBA;
|
---|
109 | }
|
---|
110 | else // mBitRange == 8
|
---|
111 | {
|
---|
112 | mSrcPixelFormat = Ogre::PF_A8R8G8B8;
|
---|
113 | }
|
---|
114 |
|
---|
115 | mTextureAtlas->create(mTextureAtlasName, mTextureAtlasSize, mTextureAtlasSize, mSrcPixelFormat, mCamera, avgColour);
|
---|
116 | mTextureAtlas->setTextureAtlasSceneNode(mTextureAtlasNode);
|
---|
117 |
|
---|
118 | unsigned int numSubTextures = mNumSamples;
|
---|
119 | Ogre::Vector2 maxTexCoords(1.0, 1.0);
|
---|
120 | unsigned int irow = 1;
|
---|
121 | unsigned int icol = 1;
|
---|
122 | unsigned int nrow = Ogre::Math::Sqrt(BBC::Util::nextPowerOf2(numSubTextures));
|
---|
123 | Ogre::Real step = 2.0 / nrow;
|
---|
124 | Ogre::Real degreeStep = 360.0 / numSubTextures;
|
---|
125 | Ogre::Real degree = 0.0;
|
---|
126 |
|
---|
127 | Ogre::MaterialPtr materialPtr = Ogre::MaterialManager::getSingleton().create("TextureAtlas" + Ogre::StringConverter::toString(numSubTextures), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
|
---|
128 | materialPtr->getTechnique(0)->getPass(0)->setAlphaRejectSettings(Ogre::CMPF_GREATER_EQUAL, 10);
|
---|
129 | materialPtr->getTechnique(0)->getPass(0)->createTextureUnitState("transparent.png"); |
---|
130 | materialPtr->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureFiltering(Ogre::FO_NONE, Ogre::FO_NONE, Ogre::FO_NONE); |
---|
131 | materialPtr->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
|
---|
132 | materialPtr->getTechnique(0)->getPass(0)->setLightingEnabled(false);
|
---|
133 | mTextureAtlas->setMaterial(materialPtr.getPointer());
|
---|
134 | mTextureAtlas->setCorners(-1.0, 1.0, 1.0, -1.0);
|
---|
135 |
|
---|
136 | for (unsigned int iSubTexture = 0; iSubTexture < numSubTextures; iSubTexture++)
|
---|
137 | {
|
---|
138 | IMG::TexturePtr texturePtr = IMG::TexturePtr( new IMG::Texture() );
|
---|
139 | Ogre::MaterialPtr materialPtr = Ogre::MaterialManager::getSingleton().create("TextureAtlas" + Ogre::StringConverter::toString(iSubTexture), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
|
---|
140 | //materialPtr->getTechnique(0)->getPass(0)->setAmbient(Ogre::Math::RangeRandom(1.0, 0.0), 1.0, 0.0);
|
---|
141 | //materialPtr->getTechnique(0)->getPass(0)->setDiffuse(1.0, 1.0, 1.0, 1.0);
|
---|
142 | materialPtr->getTechnique(0)->getPass(0)->setLightingEnabled(false);
|
---|
143 | materialPtr->getTechnique(0)->getPass(0)->setAlphaRejectSettings(Ogre::CMPF_GREATER_EQUAL, 10);
|
---|
144 | materialPtr->getTechnique(0)->getPass(0)->createTextureUnitState(mTextureName, 0);
|
---|
145 | materialPtr->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureFiltering(Ogre::FO_NONE, Ogre::FO_NONE, Ogre::FO_NONE);
|
---|
146 | materialPtr->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
|
---|
147 | materialPtr->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureRotate(Ogre::Radian(Ogre::Degree(degree)));
|
---|
148 |
|
---|
149 | texturePtr->setMaterial(materialPtr.getPointer());
|
---|
150 | texturePtr->setCorners(maxTexCoords[0] - step, maxTexCoords[1], maxTexCoords[0], maxTexCoords[1] - step);
|
---|
151 |
|
---|
152 | mTextureAtlas->addTexture(texturePtr);
|
---|
153 |
|
---|
154 | if (irow == nrow)
|
---|
155 | {
|
---|
156 | irow = 1;
|
---|
157 | icol++;
|
---|
158 | maxTexCoords = Ogre::Vector2(1.0, maxTexCoords[1] - step);
|
---|
159 | }
|
---|
160 | else
|
---|
161 | {
|
---|
162 | irow++;
|
---|
163 | maxTexCoords = Ogre::Vector2(maxTexCoords[0] - step, maxTexCoords[1]);
|
---|
164 | }
|
---|
165 |
|
---|
166 | degree = degree + degreeStep;
|
---|
167 | }
|
---|
168 | } |
---|
169 | |
---|
170 | void EntityTextureAtlasViewMode::destroyScene() |
---|
171 | { |
---|
172 | mTextureAtlasNode->detachAllObjects(); |
---|
173 | } |
---|
174 | |
---|
175 | } |
---|