source: GTP/trunk/Lib/Illum/IBRBillboardCloudTrees/OGRE/src/LBBCBillboardCloudIndirectTextureViewMode.cpp @ 900

Revision 900, 16.0 KB checked in by igarcia, 19 years ago (diff)
Line 
1
2#include "LBBCBillboardCloudIndirectTextureViewMode.h"
3#include "LBBCLeaves.h"
4#include "LBBCManager.h"
5
6namespace LBBC {
7
8BillboardCloudIndirectTextureViewMode::BillboardCloudIndirectTextureViewMode(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
19BillboardCloudIndirectTextureViewMode::~BillboardCloudIndirectTextureViewMode()
20{
21
22}
23
24
25void BillboardCloudIndirectTextureViewMode::setBillboardCloudGroupedTextureAtlasDebug(bool debugTextureAtlasGeneration)
26{
27        mDebugTextureAtlasGeneration = debugTextureAtlasGeneration;
28}
29
30void BillboardCloudIndirectTextureViewMode::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 BillboardCloudIndirectTextureViewMode::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 BillboardCloudIndirectTextureViewMode::configureTexture()
151{
152        mTextureAtlas->getRectangle2D()->setVisible(false);
153
154        //mClusterTexture->getRectangle2D()->setVisible(true);
155        //mClusterTexture->getMaterial()->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName("customNormalMap128x128.png");
156        //mClusterTexture->setCorners(-1.0, 1.0, 1.0, -1.0);
157
158        mBillboardCloudPointClusters->getSubEntity((mCurrentTexture-1) % mBillboardCloudPointClusters->getNumSubEntities())->setVisible(false);
159        mBillboardCloudPointClusters->getSubEntity(mCurrentTexture % mBillboardCloudPointClusters->getNumSubEntities())->setVisible(true);
160
161        configureBillboardOrthogonalAlignedCamera(mCurrentTexture % mBillboardCloudSplitted->getNumSubEntities());
162        mCamera->setCustomProjectionMatrix(true,mCustomProjMatrix);
163
164        Ogre::GpuProgramParametersSharedPtr fragParams;
165       
166        if (mDebugTextureAtlasGeneration)
167        {
168                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));
169                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);
170        }
171}
172
173void BillboardCloudIndirectTextureViewMode::generateTexture()
174{
175        configureTexture();
176       
177        mClusterTexture->update();     
178        //mClusterTexture->debug();
179}
180
181void BillboardCloudIndirectTextureViewMode::setEntityClustersGroupedName(Ogre::String entityClustersGroupedName)
182{
183        mEntityClustersGroupedName = entityClustersGroupedName;
184}
185
186void BillboardCloudIndirectTextureViewMode::initializeEntityClustersGrouped()
187{
188        if (mFrame == 1)
189        {
190                for (unsigned int iSubEntity = 0; iSubEntity < mBillboardCloudPointClusters->getNumSubEntities(); iSubEntity++)
191                {
192                        mEntityClustersGrouped->getSubEntity(iSubEntity)->setVisible(false);
193                }
194        }
195}
196
197void BillboardCloudIndirectTextureViewMode::initializeBillboardCloudPointClusters()
198{
199        if (mFrame == 1)
200        {
201                for (unsigned int iSubEntity = 0; iSubEntity < mBillboardCloudPointClusters->getNumSubEntities(); iSubEntity++)
202                {
203                        mBillboardCloudPointClusters->getSubEntity(iSubEntity)->setVisible(false);
204                }
205        }
206}
207
208void BillboardCloudIndirectTextureViewMode::initializeBillboardCloudSplitted()
209{
210        if (mFrame == 1)
211        {               
212                ((LBBC::LBBCManager*)LBBC::LBBCManager::getSingletonPtr())->loadBillboardCloudGroupedIndirectTexturingXML();
213                for (unsigned int iSubEntity = 0; iSubEntity < mBillboardCloudSplitted->getNumSubEntities(); iSubEntity++)
214                {
215                        mBillboardCloudSplitted->getSubEntity(iSubEntity)->setVisible(false);
216                }
217        }
218}
219
220bool BillboardCloudIndirectTextureViewMode::processUnbufferedKeyInput(const Ogre::FrameEvent& evt)
221{
222        if (mInputDevice->isKeyDown(Ogre::KC_SPACE))
223    {
224        mCurrentTexture++;
225    }
226
227        return OgreFrameListenerMode::processUnbufferedKeyInput(evt);
228}
229
230void BillboardCloudIndirectTextureViewMode::configureTextureAtlas()
231{
232        mBillboardCloudPointClusters->getSubEntity(mCurrentTexture % mBillboardCloudPointClusters->getNumSubEntities())->setVisible(false);
233
234        mTextureAtlas->getRectangle2D()->setVisible(true);
235
236        Ogre::Vector2 uvMapMax = ((LBBC::LBBCManager*)LBBC::LBBCManager::getSingletonPtr())->getLeaves()->getBillboardCloud()->getBillboard(mCurrentTexture % mBillboardCloudPointClusters->getNumSubEntities())->getBillboardClusterData()->getBillboardUVMapMax(0);
237        Ogre::Vector2 uvMapMin = ((LBBC::LBBCManager*)LBBC::LBBCManager::getSingletonPtr())->getLeaves()->getBillboardCloud()->getBillboard(mCurrentTexture % mBillboardCloudPointClusters->getNumSubEntities())->getBillboardClusterData()->getBillboardUVMapMin(0);
238
239        mClusterTexture->setCorners(uvMapMin[0], uvMapMax[1], uvMapMax[0], uvMapMin[1]);
240        mClusterTexture->getRectangle2D()->setVisible(true);
241        //mClusterTexture->getMaterial()->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(mClusterTexture->getTextureName());
242}
243
244void BillboardCloudIndirectTextureViewMode::generateTextureAtlas()
245{       
246        configureTextureAtlas();
247
248        mTextureAtlas->update();               
249        //mTextureAtlas->debug();
250       
251        mClusterTexture->getRectangle2D()->setVisible(false);
252}
253
254bool BillboardCloudIndirectTextureViewMode::frameStarted(const Ogre::FrameEvent& evt, Ogre::InputReader *inputReader)
255{
256        showDebugOverlay(false);
257        initializeEntityClustersGrouped();
258    initializeBillboardCloudSplitted();
259        initializeBillboardCloudPointClusters();
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 BillboardCloudIndirectTextureViewMode::configureBillboard()
325{
326        mEntityClustersGrouped->getSubEntity((mCurrentTexture-1) % mEntityClustersGrouped->getNumSubEntities())->setVisible(false);
327        mEntityClustersGrouped->getSubEntity(mCurrentTexture % mEntityClustersGrouped->getNumSubEntities())->setVisible(true);
328        mBillboardCloudSplitted->getSubEntity((mCurrentTexture-1) % mBillboardCloudPointClusters->getNumSubEntities())->setVisible(false);
329        mBillboardCloudSplitted->getSubEntity(mCurrentTexture % mBillboardCloudPointClusters->getNumSubEntities())->setVisible(true);
330}
331
332void BillboardCloudIndirectTextureViewMode::setTextureAtlasSize(unsigned int size)
333{
334        mTextureAtlasSize = size;
335}
336
337void BillboardCloudIndirectTextureViewMode::setBillboardCloudPointClustersName(Ogre::String billboardCloudPointClustersName)
338{
339        mBillboardCloudPointClustersName = billboardCloudPointClustersName;
340}
341
342void BillboardCloudIndirectTextureViewMode::setBillboardCloudSplittedName(Ogre::String billboardCloudSplittedName)
343{
344        mBillboardCloudSplittedName = billboardCloudSplittedName;
345}
346
347void BillboardCloudIndirectTextureViewMode::setTextureAtlasBitRange(unsigned int bitRange)
348{
349        mBitRange = bitRange;
350}
351
352void BillboardCloudIndirectTextureViewMode::setTextureSize(unsigned int size)
353{
354        mTextureSize = size;
355}
356
357void BillboardCloudIndirectTextureViewMode::setTextureAtlasFolder(Ogre::String textureAtlasFolder)
358{
359        mTextureAtlasFolder = textureAtlasFolder;
360}
361
362void BillboardCloudIndirectTextureViewMode::setTextureAtlasName(Ogre::String textureAtlasName)
363{
364        mTextureAtlasName = textureAtlasName;
365}
366
367void BillboardCloudIndirectTextureViewMode::createScene()
368{
369        if (mBitRange == 32)
370        {
371                mSrcPixelFormat = Ogre::PF_FLOAT32_RGBA;
372        }
373        else if (mBitRange == 16)
374        {
375                mSrcPixelFormat = Ogre::PF_FLOAT16_RGBA;
376        }
377        else  // mBitRange == 8
378        {
379                mSrcPixelFormat = Ogre::PF_A8R8G8B8;
380        }
381
382        mWindow->getViewport(0)->setBackgroundColour(Ogre::ColourValue(1.0, 1.0, 1.0, 1.0));
383
384        mBillboardCloudPointClustersSceneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
385        mBillboardCloudSplittedSceneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
386
387        mBillboardCloudPointClusters = mSceneMgr->createEntity(mBillboardCloudPointClustersName, mBillboardCloudPointClustersName);
388    mBillboardCloudPointClustersSceneNode->attachObject(mBillboardCloudPointClusters);
389
390        mBillboardCloudSplitted = mSceneMgr->createEntity(mBillboardCloudSplittedName, mBillboardCloudSplittedName);
391    mBillboardCloudSplittedSceneNode->attachObject(mBillboardCloudSplitted);
392
393        mEntityClustersGroupedSceneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
394        mEntityClustersGrouped = mSceneMgr->createEntity(mEntityClustersGroupedName, mEntityClustersGroupedName);
395    mEntityClustersGroupedSceneNode->attachObject(mEntityClustersGrouped);
396
397        mTextureAtlas = IMG::TextureAtlasPtr( new IMG::TextureAtlas() );
398        mTextureAtlas->create("IndirectTextureAtlas", mTextureAtlasSize, mTextureAtlasSize, mSrcPixelFormat, mCamera, Ogre::ColourValue(0.0, 0.0, 0.0, 0.0));
399        mTextureAtlas->setTextureAtlasSceneNode(mBillboardCloudPointClustersSceneNode);
400        Ogre::MaterialPtr materialPtr1 = Ogre::MaterialManager::getSingleton().create("IndirectTextureAtlasMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);         
401        materialPtr1->getTechnique(0)->getPass(0)->setAlphaRejectSettings(Ogre::CMPF_GREATER_EQUAL, 10);
402        materialPtr1->getTechnique(0)->getPass(0)->createTextureUnitState("transparent.png");
403        materialPtr1->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureFiltering(Ogre::FO_NONE, Ogre::FO_NONE, Ogre::FO_NONE);                   
404        materialPtr1->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
405        materialPtr1->getTechnique(0)->getPass(0)->setLightingEnabled(false);
406        mTextureAtlas->setMaterial(materialPtr1.getPointer());
407        mTextureAtlas->setCorners(-1.0, 1.0, 1.0, -1.0);       
408
409        mClusterTexture = IMG::TexturePtr( new IMG::Texture() );
410        mClusterTexture->create("ClusterIndirectTexture", mTextureSize, mTextureSize, mSrcPixelFormat, mCamera, Ogre::ColourValue(0.0, 0.0, 0.0, 0.0));
411        Ogre::MaterialPtr materialPtr = Ogre::MaterialManager::getSingleton().create("IndirectTextureMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);                       
412        if (!mDebugTextureAtlasGeneration)
413        {
414                materialPtr->getTechnique(0)->getPass(0)->createTextureUnitState("customNormalMap128x128.png", 0);
415                materialPtr->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureFiltering(Ogre::FO_NONE, Ogre::FO_NONE, Ogre::FO_NONE);                     
416                materialPtr->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);         
417
418                materialPtr->getTechnique(0)->createPass();
419                materialPtr->getTechnique(0)->getPass(1)->setSceneBlending(Ogre::SBT_ADD);
420                materialPtr->getTechnique(0)->getPass(1)->setDepthWriteEnabled(false);
421
422                materialPtr->getTechnique(0)->getPass(1)->setAlphaRejectSettings(Ogre::CMPF_GREATER_EQUAL, 10);
423                materialPtr->getTechnique(0)->getPass(1)->createTextureUnitState("ClusterIndirectTexture", 0);
424                materialPtr->getTechnique(0)->getPass(1)->getTextureUnitState(0)->setTextureFiltering(Ogre::FO_NONE, Ogre::FO_NONE, Ogre::FO_NONE);                     
425                materialPtr->getTechnique(0)->getPass(1)->getTextureUnitState(0)->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);         
426
427                materialPtr->getTechnique(0)->getPass(0)->setLightingEnabled(false);
428        }
429
430        mClusterTexture->setMaterial(materialPtr.getPointer());
431
432        mTextureAtlas->addTexture(mClusterTexture);
433
434        mNumTextures = mBillboardCloudPointClusters->getNumSubEntities();
435        mCurrentTexture = 0;
436}
437
438void BillboardCloudIndirectTextureViewMode::destroyScene()
439{
440       
441}
442
443}
Note: See TracBrowser for help on using the repository browser.