source: GTP/trunk/Lib/Illum/IBRBillboardCloudTrees/OGRE/src/LBBCBillboardCloudDiffuseColorTextureViewMode.cpp @ 821

Revision 821, 16.3 KB checked in by igarcia, 18 years ago (diff)
Line 
1
2#include "LBBCBillboardCloudDiffuseColorTextureViewMode.h"
3#include "LBBCLeaves.h"
4#include "LBBCManager.h"
5
6namespace LBBC {
7
8BillboardCloudDiffuseColorTextureViewMode::BillboardCloudDiffuseColorTextureViewMode(Ogre::RenderWindow* win, unsigned int ogreFrameListenerModeHandle, bool useBufferedInputKeys, bool useBufferedInputMouse)
9:OBA::OgreFrameListenerMode(win, ogreFrameListenerModeHandle, useBufferedInputKeys, useBufferedInputMouse)
10{       
11        mFrame = 0;
12        mCurrentBillboardGroup = 0;
13        mCurrentBillboard = 0;
14        showDebugOverlay(false);
15        mDebugBillboardGeneration = false;
16        mDebugTextureAtlasGeneration = false;
17}
18
19BillboardCloudDiffuseColorTextureViewMode::~BillboardCloudDiffuseColorTextureViewMode()
20{
21
22}
23
24
25void BillboardCloudDiffuseColorTextureViewMode::setBillboardCloudGroupedTextureAtlasDebug(bool debugTextureAtlasGeneration)
26{
27        mDebugTextureAtlasGeneration = debugTextureAtlasGeneration;
28}
29
30void BillboardCloudDiffuseColorTextureViewMode::configureBillboardOrthogonalAlignedCamera(unsigned int iBillboard)
31{
32        BBC::BillboardPtr billboardPtr = ((LBBC::LBBCManager*)LBBC::LBBCManager::getSingletonPtr())->getLeaves()->getBillboardCloud()->getBillboard(iBillboard);
33        Ogre::Vector3 vTopLeft = billboardPtr->getBillboardClusterData()->getQuadTopLeftCorner();
34        Ogre::Vector3 vTopRight = billboardPtr->getBillboardClusterData()->getQuadTopRightCorner();
35        Ogre::Vector3 vBottomLeft = billboardPtr->getBillboardClusterData()->getQuadBottomLeftCorner();
36        Ogre::Vector3 vBottomRight = billboardPtr->getBillboardClusterData()->getQuadBottomRightCorner();
37
38        Ogre::Vector3 vMidPoint = vTopLeft.midPoint(vBottomRight);
39        Ogre::Vector3 vWidth(vTopRight - vTopLeft);
40        Ogre::Vector3 vHeight(vBottomLeft - vTopLeft);
41
42        Ogre::Real w = vWidth.length();
43        Ogre::Real h = vHeight.length();
44        Ogre::Real aspect = w / h;
45        Ogre::Radian fovy = Ogre::Radian(Ogre::Degree(90.0));
46        Ogre::Real nearPlane =  h / Ogre::Math::Tan(fovy/2.0,false);
47        Ogre::Real farPlane = 100000;
48
49        Ogre::Vector3 vUp = vTopLeft - vBottomLeft;
50        vUp.normalise();
51
52        Ogre::Vector3 vI(vTopRight-vTopLeft);
53        vI.normalise();
54
55        Ogre::Vector3 vJ(vBottomLeft-vTopLeft);
56        vJ.normalise();
57
58        Ogre::Vector3 vDir = -vI.crossProduct(vJ);
59        vDir.normalise();
60
61        Ogre::Vector3 vRight = vDir.crossProduct(vUp);
62        vRight.normalise();     
63
64        Ogre::Quaternion qOrientation(vRight, vUp, -vDir);
65       
66        mCamera->setNearClipDistance(nearPlane);
67        mCamera->setOrientation(qOrientation);
68        mCamera->setPosition(vMidPoint - (vDir * (nearPlane*nearPlane)));
69
70        if (Ogre::Root::getSingleton().getRenderSystem()->getName() != "Direct3D9 Rendering SubSystem")
71        {
72                Ogre::Radian thetaY (fovy / 2.0f);
73                Ogre::Real tanThetaY = Ogre::Math::Tan(thetaY);
74
75                Ogre::Real tanThetaX = tanThetaY * aspect;
76
77                Ogre::Real half_w,half_h;
78                half_w = w / 2.0;
79                half_h = h / 2.0;
80
81                Ogre::Real iw = 1.0 / half_w;
82                Ogre::Real ih = 1.0 / half_h;
83                Ogre::Real q;
84                if (farPlane == 0)
85                {
86                        q = 0;
87                }
88                else
89                {
90                        q = 2.0 / (farPlane - nearPlane);
91                }
92                mCustomProjMatrix = Ogre::Matrix4::ZERO;
93                mCustomProjMatrix[0][0] = iw;
94                mCustomProjMatrix[1][1] = ih;
95                mCustomProjMatrix[2][2] = -q;
96                mCustomProjMatrix[2][3] = - (farPlane + nearPlane)/(farPlane - nearPlane);
97                mCustomProjMatrix[3][3] = 1;   
98        }
99        else
100        {
101                Ogre::Radian thetaY (fovy / 2.0f);
102                Ogre::Real tanThetaY = Ogre::Math::Tan(thetaY);
103
104                //Real thetaX = thetaY * aspect;
105                Ogre::Real tanThetaX = tanThetaY * aspect; //Math::Tan(thetaX);
106                Ogre::Real half_w = w / 2;
107                Ogre::Real half_h = h / 2;
108                Ogre::Real iw = 1.0 / half_w;
109                Ogre::Real ih = 1.0 / half_h;
110                Ogre::Real q;
111                if (farPlane == 0)
112                {
113                        q = 0;
114                }
115                else
116                {
117                        q = 1.0 / (farPlane - nearPlane);
118                }
119
120                mCustomProjMatrix = Ogre::Matrix4::ZERO;
121                mCustomProjMatrix[0][0] = iw;
122                mCustomProjMatrix[1][1] = ih;
123                mCustomProjMatrix[2][2] = q;
124                mCustomProjMatrix[2][3] = -nearPlane / (farPlane - nearPlane);
125                mCustomProjMatrix[3][3] = 1;
126
127                mCustomProjMatrix[2][2] = -mCustomProjMatrix[2][2];
128        }
129}
130
131void BillboardCloudDiffuseColorTextureViewMode::saveTextureAtlas()
132{
133        if (mBitRange == 32)
134        {
135                mDestPixelFormat = Ogre::PF_FLOAT32_RGBA;
136        }
137        else if (mBitRange == 16)
138        {
139                mDestPixelFormat = Ogre::PF_FLOAT16_RGBA;
140        }
141        else  // mBitRange == 8
142        {
143                mDestPixelFormat = Ogre::PF_BYTE_RGBA;
144        }
145
146        Ogre::String textureAtlasFileName = BBC::Util::getBaseName(mTextureAtlasName) + Ogre::StringConverter::toString(mCurrentBillboardGroup) + "." + BBC::Util::getExtensionName(mTextureAtlasName);
147        mTextureAtlas->save(mTextureAtlasFolder, textureAtlasFileName, mDestPixelFormat);
148}
149
150void BillboardCloudDiffuseColorTextureViewMode::disableEntityClusterCustomOrthogonalCameraMaterial(unsigned int iEntityClusterGrouped)
151{
152        //mEntityClustersGrouped->getSubEntity(iEntityClusterGrouped)->getMaterial()->getTechnique(0)->getPass(0)->setVertexProgram("");
153        //mEntityClustersGrouped->getSubEntity(iEntityClusterGrouped)->getMaterial()->getTechnique(0)->getPass(0)->setFragmentProgram("");
154        //mEntityClustersGrouped->getSubEntity(iEntityClusterGrouped)->getMaterial()->reload();
155}
156
157void BillboardCloudDiffuseColorTextureViewMode::enableEntityClusterCustomOrthogonalCameraMaterial(unsigned int iEntityClusterGrouped)
158{
159        //mEntityClustersGrouped->getSubEntity(iEntityClusterGrouped)->getMaterial()->getTechnique(0)->getPass(0)->setVertexProgram("diffuseTextureScreenAligned_VP20");
160        mEntityClustersGrouped->getSubEntity(iEntityClusterGrouped)->getMaterial()->getTechnique(0)->getPass(0)->setFragmentProgram("diffuseTextureScreenAligned_FP20");
161
162        //Ogre::GpuProgramParametersSharedPtr vertParams;
163        //Ogre::GpuProgramParametersSharedPtr fragParams;
164        //vertParams = mEntityClustersGrouped->getSubEntity(iEntityClusterGrouped)->getMaterial()->getTechnique(0)->getPass(0)->getVertexProgramParameters();
165        //fragParams = mEntityClustersGrouped->getSubEntity(iEntityClusterGrouped)->getMaterial()->getTechnique(0)->getPass(0)->getFragmentProgramParameters();
166
167        //vertParams->setNamedConstant("projectionmatrix", mCamera->getProjectionMatrixWithRSDepth());
168        //vertParams->setNamedAutoConstant("worldmatrix", Ogre::GpuProgramParameters::ACT_WORLD_MATRIX);
169        //vertParams->setNamedAutoConstant("viewmatrix", Ogre::GpuProgramParameters::ACT_VIEW_MATRIX);
170        mEntityClustersGrouped->getSubEntity(iEntityClusterGrouped)->getMaterial()->reload();
171}
172
173void BillboardCloudDiffuseColorTextureViewMode::configureTexture()
174{
175        mTextureAtlas->getRectangle2D()->setVisible(false);
176        mEntityClustersGrouped->getSubEntity((mCurrentTexture-1) % mEntityClustersGrouped->getNumSubEntities())->setVisible(false);
177        mEntityClustersGrouped->getSubEntity(mCurrentTexture % mEntityClustersGrouped->getNumSubEntities())->setVisible(true);
178
179        configureBillboardOrthogonalAlignedCamera(mCurrentTexture % mBillboardCloudSplitted->getNumSubEntities());
180        mCamera->setCustomProjectionMatrix(true,mCustomProjMatrix);
181
182        if (mDebugTextureAtlasGeneration)
183        {
184                mClusterTexture->getMaterial()->getTechnique(0)->getPass(0)->setAmbient(Ogre::Math::RangeRandom(0.0,1.0), Ogre::Math::RangeRandom(0.0,1.0), Ogre::Math::RangeRandom(0.0,1.0));
185                mClusterTexture->getMaterial()->getTechnique(0)->getPass(0)->setDiffuse(Ogre::Math::RangeRandom(0.0,1.0), Ogre::Math::RangeRandom(0.0,1.0), Ogre::Math::RangeRandom(0.0,1.0), 1.0);
186        }
187}
188
189void BillboardCloudDiffuseColorTextureViewMode::generateTexture()
190{
191        configureTexture();
192
193        enableEntityClusterCustomOrthogonalCameraMaterial(mCurrentTexture % mEntityClustersGrouped->getNumSubEntities());
194       
195        mClusterTexture->update();             
196
197        disableEntityClusterCustomOrthogonalCameraMaterial(mCurrentTexture % mEntityClustersGrouped->getNumSubEntities());
198}
199
200void BillboardCloudDiffuseColorTextureViewMode::initializeEntityClustersGrouped()
201{
202        if (mFrame == 1)
203        {
204                for (unsigned int iSubEntity = 0; iSubEntity < mEntityClustersGrouped->getNumSubEntities(); iSubEntity++)
205                {
206                        mEntityClustersGrouped->getSubEntity(iSubEntity)->setVisible(false);
207                }
208        }
209}
210
211void BillboardCloudDiffuseColorTextureViewMode::initializeBillboardCloudSplitted()
212{
213        if (mFrame == 1)
214        {
215                for (unsigned int iSubEntity = 0; iSubEntity < mBillboardCloudSplitted->getNumSubEntities(); iSubEntity++)
216                {
217                        mBillboardCloudSplitted->getSubEntity(iSubEntity)->setVisible(false);
218                }
219        }
220}
221
222bool BillboardCloudDiffuseColorTextureViewMode::processUnbufferedKeyInput(const Ogre::FrameEvent& evt)
223{
224        if (mInputDevice->isKeyDown(Ogre::KC_SPACE))
225    {
226        mCurrentTexture++;
227    }
228
229        return OgreFrameListenerMode::processUnbufferedKeyInput(evt);
230}
231
232void BillboardCloudDiffuseColorTextureViewMode::configureTextureAtlas()
233{
234        mEntityClustersGrouped->getSubEntity(mCurrentTexture % mEntityClustersGrouped->getNumSubEntities())->setVisible(false);
235
236        mClusterTexture->getRectangle2D()->setVisible(true);
237        mTextureAtlas->getRectangle2D()->setVisible(true);
238
239        Ogre::Vector2 uvMapMax = ((LBBC::LBBCManager*)LBBC::LBBCManager::getSingletonPtr())->getLeaves()->getBillboardCloud()->getBillboard(mCurrentTexture % mEntityClustersGrouped->getNumSubEntities())->getBillboardClusterData()->getBillboardUVMapMax(0);
240        Ogre::Vector2 uvMapMin = ((LBBC::LBBCManager*)LBBC::LBBCManager::getSingletonPtr())->getLeaves()->getBillboardCloud()->getBillboard(mCurrentTexture % mEntityClustersGrouped->getNumSubEntities())->getBillboardClusterData()->getBillboardUVMapMin(0);
241
242        mClusterTexture->setCorners(uvMapMin[0], uvMapMax[1], uvMapMax[0], uvMapMin[1]);
243}
244
245void BillboardCloudDiffuseColorTextureViewMode::generateTextureAtlas()
246{       
247        configureTextureAtlas();
248
249        mTextureAtlas->update();               
250        //mTextureAtlas->debug();
251       
252        mClusterTexture->getRectangle2D()->setVisible(false);
253}
254
255bool BillboardCloudDiffuseColorTextureViewMode::frameStarted(const Ogre::FrameEvent& evt, Ogre::InputReader *inputReader)
256{
257        showDebugOverlay(false);
258        initializeEntityClustersGrouped();
259    initializeBillboardCloudSplitted();
260
261        if (mFrame > 1)
262        {               
263                if (!mDebugBillboardGeneration)
264                {
265                        mWindow->getViewport(0)->setBackgroundColour(Ogre::ColourValue(1.0, 1.0, 1.0, 1.0));
266
267                        if (mCurrentTexture < mNumTextures)
268                        {
269                                generateTexture();
270
271                                generateTextureAtlas();
272
273                                mCurrentTexture++;
274                                mCurrentBillboard++;
275
276                                unsigned int numBillboardsCurrentGroups = ((LBBC::LBBCManager*)LBBC::LBBCManager::getSingletonPtr())->getLeaves()->getBillboardCloud()->getBillboardGroup(mCurrentBillboardGroup)->getNumBillboards();
277
278                                if (mCurrentBillboard == numBillboardsCurrentGroups)
279                                {
280                                        saveTextureAtlas();
281                                       
282                                        unsigned int numBillboardGroups = ((LBBC::LBBCManager*)LBBC::LBBCManager::getSingletonPtr())->getLeaves()->getBillboardCloud()->getNumBillboardGroups();
283
284                                        if (mCurrentBillboardGroup < numBillboardGroups)
285                                        {
286                                                mCurrentBillboard = 0;
287                                                mCurrentBillboardGroup++;                                               
288
289                                                mTextureAtlas->getRectangle2D()->setVisible(false);
290                                                mTextureAtlas->update();
291                                                //mTextureAtlas->debug();
292                                        }
293                                }
294                        }
295
296                        if (mCurrentTexture == mNumTextures)
297                        {                               
298                                mDebugBillboardGeneration = true;
299
300                                mWindow->getViewport(0)->setBackgroundColour(Ogre::ColourValue(0.0, 0.0, 0.0, 1.0));
301
302                                mTextureAtlas->getRectangle2D()->setVisible(false);
303
304                                mCurrentTexture = 0;
305                        }
306                }
307                else
308                {
309                        mWindow->getViewport(0)->setBackgroundColour(Ogre::ColourValue(0.0, 0.0, 0.0, 1.0));
310
311                        configureBillboard();
312
313                        configureTexture();
314
315                        mCurrentTexture = mCurrentTexture % mNumTextures;
316                }
317        }
318
319        mFrame++;
320
321        return OgreFrameListenerMode::frameStarted(evt, inputReader);   
322}
323
324void BillboardCloudDiffuseColorTextureViewMode::configureBillboard()
325{
326        mBillboardCloudSplitted->getSubEntity((mCurrentTexture-1) % mEntityClustersGrouped->getNumSubEntities())->setVisible(false);
327        mBillboardCloudSplitted->getSubEntity(mCurrentTexture % mEntityClustersGrouped->getNumSubEntities())->setVisible(true);
328}
329
330void BillboardCloudDiffuseColorTextureViewMode::setTextureAtlasSize(unsigned int size)
331{
332        mTextureAtlasSize = size;
333}
334
335void BillboardCloudDiffuseColorTextureViewMode::setEntityClustersGroupedName(Ogre::String entityClustersGroupedName)
336{
337        mEntityClustersGroupedName = entityClustersGroupedName;
338}
339
340void BillboardCloudDiffuseColorTextureViewMode::setBillboardCloudSplittedName(Ogre::String billboardCloudSplittedName)
341{
342        mBillboardCloudSplittedName = billboardCloudSplittedName;
343}
344
345void BillboardCloudDiffuseColorTextureViewMode::setTextureAtlasBitRange(unsigned int bitRange)
346{
347        mBitRange = bitRange;
348}
349
350void BillboardCloudDiffuseColorTextureViewMode::setTextureSize(unsigned int size)
351{
352        mTextureSize = size;
353}
354
355void BillboardCloudDiffuseColorTextureViewMode::setTextureAtlasFolder(Ogre::String textureAtlasFolder)
356{
357        mTextureAtlasFolder = textureAtlasFolder;
358}
359
360void BillboardCloudDiffuseColorTextureViewMode::setTextureAtlasName(Ogre::String textureAtlasName)
361{
362        mTextureAtlasName = textureAtlasName;
363}
364
365void BillboardCloudDiffuseColorTextureViewMode::setTextureName(Ogre::String textureName)
366{
367        mTextureName = textureName;
368}
369
370void BillboardCloudDiffuseColorTextureViewMode::createScene()
371{
372        if (mBitRange == 32)
373        {
374                mSrcPixelFormat = Ogre::PF_FLOAT32_RGBA;
375        }
376        else if (mBitRange == 16)
377        {
378                mSrcPixelFormat = Ogre::PF_FLOAT16_RGBA;
379        }
380        else  // mBitRange == 8
381        {
382                mSrcPixelFormat = Ogre::PF_A8R8G8B8;
383        }
384
385        LBBC::Leaves *mLeaves = ((LBBC::LBBCManager*)LBBC::LBBCManager::getSingletonPtr())->getLeaves();
386        Ogre::Vector2 uvMapMax = mLeaves->getBillboardCloud()->getBillboard(0)->getBillboardClusterData()->getBillboardUVMapMax(0);
387        Ogre::Vector2 uvMapMin = mLeaves->getBillboardCloud()->getBillboard(0)->getBillboardClusterData()->getBillboardUVMapMin(0);
388
389        IMG::TexturePtr texturePtr ( new IMG::Texture() );
390        texturePtr->bind(mTextureName);
391        Ogre::ColourValue avgColour = texturePtr->getAvgColour();
392        mWindow->getViewport(0)->setBackgroundColour(Ogre::ColourValue(1.0, 1.0, 1.0, 1.0));
393
394        mEntityClustersGroupedSceneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
395        mBillboardCloudSplittedSceneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
396
397        mEntityClustersGrouped = mSceneMgr->createEntity(mEntityClustersGroupedName, mEntityClustersGroupedName);
398    mEntityClustersGroupedSceneNode->attachObject(mEntityClustersGrouped);
399
400        mBillboardCloudSplitted = mSceneMgr->createEntity(mBillboardCloudSplittedName, mBillboardCloudSplittedName);
401    mBillboardCloudSplittedSceneNode->attachObject(mBillboardCloudSplitted);
402
403        mTextureAtlas = IMG::TextureAtlasPtr( new IMG::TextureAtlas() );
404        mTextureAtlas->create("TextureAtlasDiffuseColor", mTextureAtlasSize, mTextureAtlasSize, mSrcPixelFormat, mCamera, avgColour);
405        mTextureAtlas->setTextureAtlasSceneNode(mEntityClustersGroupedSceneNode);
406        Ogre::MaterialPtr materialPtr1 = Ogre::MaterialManager::getSingleton().create("DiffuseColorTextureAtlasMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);             
407        materialPtr1->getTechnique(0)->getPass(0)->setAlphaRejectSettings(Ogre::CMPF_GREATER_EQUAL, 10);
408        materialPtr1->getTechnique(0)->getPass(0)->createTextureUnitState("transparent.png");
409        materialPtr1->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureFiltering(Ogre::FO_NONE, Ogre::FO_NONE, Ogre::FO_NONE);                   
410        materialPtr1->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
411        materialPtr1->getTechnique(0)->getPass(0)->setLightingEnabled(false);
412        mTextureAtlas->setMaterial(materialPtr1.getPointer());
413        mTextureAtlas->setCorners(-1.0, 1.0, 1.0, -1.0);       
414
415        mClusterTexture = IMG::TexturePtr( new IMG::Texture() );
416        mClusterTexture->create("ClusterTextureDiffuseColor", mTextureSize, mTextureSize, mSrcPixelFormat, mCamera, avgColour);
417        Ogre::MaterialPtr materialPtr = Ogre::MaterialManager::getSingleton().create("DiffuseColorTextureMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);                   
418        if (!mDebugTextureAtlasGeneration)
419        {
420                materialPtr->getTechnique(0)->getPass(0)->setAlphaRejectSettings(Ogre::CMPF_GREATER_EQUAL, 10);
421                materialPtr->getTechnique(0)->getPass(0)->createTextureUnitState("ClusterTextureDiffuseColor", 0);
422                materialPtr->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureFiltering(Ogre::FO_NONE, Ogre::FO_NONE, Ogre::FO_NONE);                     
423                materialPtr->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);         
424                materialPtr->getTechnique(0)->getPass(0)->setLightingEnabled(false);
425        }
426        mClusterTexture->setMaterial(materialPtr.getPointer());
427
428        mTextureAtlas->addTexture(mClusterTexture);
429
430        mNumTextures = mEntityClustersGrouped->getNumSubEntities();
431        mCurrentTexture = 0;
432}
433
434void BillboardCloudDiffuseColorTextureViewMode::destroyScene()
435{
436       
437}
438
439}
Note: See TracBrowser for help on using the repository browser.