source: GTP/trunk/Lib/Illum/IBRBillboardCloudTrees/OGRE/src/LBBCEntityTextureAtlasViewMode.cpp @ 731

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