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

Revision 778, 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        initializeEntityClustersGrouped();
258    initializeBillboardCloudSplitted();
259
260        if (mFrame > 1)
261        {               
262                if (!mDebugBillboardGeneration)
263                {
264                        mWindow->getViewport(0)->setBackgroundColour(Ogre::ColourValue(1.0, 1.0, 1.0, 1.0));
265
266                        if (mCurrentTexture < mNumTextures)
267                        {
268                                generateTexture();
269
270                                generateTextureAtlas();
271
272                                mCurrentTexture++;
273                                mCurrentBillboard++;
274
275                                unsigned int numBillboardsCurrentGroups = ((LBBC::LBBCManager*)LBBC::LBBCManager::getSingletonPtr())->getLeaves()->getBillboardCloud()->getBillboardGroup(mCurrentBillboardGroup)->getNumBillboards();
276
277                                if (mCurrentBillboard == numBillboardsCurrentGroups)
278                                {
279                                        saveTextureAtlas();
280                                       
281                                        unsigned int numBillboardGroups = ((LBBC::LBBCManager*)LBBC::LBBCManager::getSingletonPtr())->getLeaves()->getBillboardCloud()->getNumBillboardGroups();
282
283                                        if (mCurrentBillboardGroup < numBillboardGroups)
284                                        {
285                                                mCurrentBillboard = 0;
286                                                mCurrentBillboardGroup++;                                               
287
288                                                mTextureAtlas->getRectangle2D()->setVisible(false);
289                                                mTextureAtlas->update();
290                                                //mTextureAtlas->debug();
291                                        }
292                                }
293                        }
294
295                        if (mCurrentTexture == mNumTextures)
296                        {                               
297                                mDebugBillboardGeneration = true;
298
299                                mWindow->getViewport(0)->setBackgroundColour(Ogre::ColourValue(0.0, 0.0, 0.0, 1.0));
300
301                                mTextureAtlas->getRectangle2D()->setVisible(false);
302
303                                mCurrentTexture = 0;
304                        }
305                }
306                else
307                {
308                        mWindow->getViewport(0)->setBackgroundColour(Ogre::ColourValue(0.0, 0.0, 0.0, 1.0));
309
310                        configureBillboard();
311
312                        configureTexture();
313
314                        mCurrentTexture = mCurrentTexture % mNumTextures;
315                }
316        }
317
318        mFrame++;
319
320        return OgreFrameListenerMode::frameStarted(evt, inputReader);   
321}
322
323void BillboardCloudDiffuseColorTextureViewMode::configureBillboard()
324{
325        mBillboardCloudSplitted->getSubEntity((mCurrentTexture-1) % mEntityClustersGrouped->getNumSubEntities())->setVisible(false);
326        mBillboardCloudSplitted->getSubEntity(mCurrentTexture % mEntityClustersGrouped->getNumSubEntities())->setVisible(true);
327}
328
329void BillboardCloudDiffuseColorTextureViewMode::setTextureAtlasSize(unsigned int size)
330{
331        mTextureAtlasSize = size;
332}
333
334void BillboardCloudDiffuseColorTextureViewMode::setEntityClustersGroupedName(Ogre::String entityClustersGroupedName)
335{
336        mEntityClustersGroupedName = entityClustersGroupedName;
337}
338
339void BillboardCloudDiffuseColorTextureViewMode::setBillboardCloudSplittedName(Ogre::String billboardCloudSplittedName)
340{
341        mBillboardCloudSplittedName = billboardCloudSplittedName;
342}
343
344void BillboardCloudDiffuseColorTextureViewMode::setTextureAtlasBitRange(unsigned int bitRange)
345{
346        mBitRange = bitRange;
347}
348
349void BillboardCloudDiffuseColorTextureViewMode::setTextureSize(unsigned int size)
350{
351        mTextureSize = size;
352}
353
354void BillboardCloudDiffuseColorTextureViewMode::setTextureAtlasFolder(Ogre::String textureAtlasFolder)
355{
356        mTextureAtlasFolder = textureAtlasFolder;
357}
358
359void BillboardCloudDiffuseColorTextureViewMode::setTextureAtlasName(Ogre::String textureAtlasName)
360{
361        mTextureAtlasName = textureAtlasName;
362}
363
364void BillboardCloudDiffuseColorTextureViewMode::setTextureName(Ogre::String textureName)
365{
366        mTextureName = textureName;
367}
368
369void BillboardCloudDiffuseColorTextureViewMode::createScene()
370{
371        if (mBitRange == 32)
372        {
373                mSrcPixelFormat = Ogre::PF_FLOAT32_RGBA;
374        }
375        else if (mBitRange == 16)
376        {
377                mSrcPixelFormat = Ogre::PF_FLOAT16_RGBA;
378        }
379        else  // mBitRange == 8
380        {
381                mSrcPixelFormat = Ogre::PF_A8R8G8B8;
382        }
383
384        LBBC::Leaves *mLeaves = ((LBBC::LBBCManager*)LBBC::LBBCManager::getSingletonPtr())->getLeaves();
385        Ogre::Vector2 uvMapMax = mLeaves->getBillboardCloud()->getBillboard(0)->getBillboardClusterData()->getBillboardUVMapMax(0);
386        Ogre::Vector2 uvMapMin = mLeaves->getBillboardCloud()->getBillboard(0)->getBillboardClusterData()->getBillboardUVMapMin(0);
387
388        IMG::TexturePtr texturePtr ( new IMG::Texture() );
389        texturePtr->bind(mTextureName);
390        Ogre::ColourValue avgColour = texturePtr->getAvgColour();
391        mWindow->getViewport(0)->setBackgroundColour(Ogre::ColourValue(1.0, 1.0, 1.0, 1.0));
392
393        mEntityClustersGroupedSceneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
394        mBillboardCloudSplittedSceneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
395
396        mEntityClustersGrouped = mSceneMgr->createEntity(mEntityClustersGroupedName, mEntityClustersGroupedName);
397    mEntityClustersGroupedSceneNode->attachObject(mEntityClustersGrouped);
398
399        mBillboardCloudSplitted = mSceneMgr->createEntity(mBillboardCloudSplittedName, mBillboardCloudSplittedName);
400    mBillboardCloudSplittedSceneNode->attachObject(mBillboardCloudSplitted);
401
402        mTextureAtlas = IMG::TextureAtlasPtr( new IMG::TextureAtlas() );
403        mTextureAtlas->create("TextureAtlasDiffuseColor", mTextureAtlasSize, mTextureAtlasSize, mSrcPixelFormat, mCamera, avgColour);
404        mTextureAtlas->setTextureAtlasSceneNode(mEntityClustersGroupedSceneNode);
405        Ogre::MaterialPtr materialPtr1 = Ogre::MaterialManager::getSingleton().create("DiffuseColorTextureAtlasMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);             
406        materialPtr1->getTechnique(0)->getPass(0)->setAlphaRejectSettings(Ogre::CMPF_GREATER_EQUAL, 10);
407        materialPtr1->getTechnique(0)->getPass(0)->createTextureUnitState("transparent.png");
408        materialPtr1->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureFiltering(Ogre::FO_NONE, Ogre::FO_NONE, Ogre::FO_NONE);                   
409        materialPtr1->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
410        materialPtr1->getTechnique(0)->getPass(0)->setLightingEnabled(false);
411        mTextureAtlas->setMaterial(materialPtr1.getPointer());
412        mTextureAtlas->setCorners(-1.0, 1.0, 1.0, -1.0);       
413
414        mClusterTexture = IMG::TexturePtr( new IMG::Texture() );
415        mClusterTexture->create("ClusterTextureDiffuseColor", mTextureSize, mTextureSize, mSrcPixelFormat, mCamera, avgColour);
416        Ogre::MaterialPtr materialPtr = Ogre::MaterialManager::getSingleton().create("DiffuseColorTextureMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);                   
417        if (!mDebugTextureAtlasGeneration)
418        {
419                materialPtr->getTechnique(0)->getPass(0)->setAlphaRejectSettings(Ogre::CMPF_GREATER_EQUAL, 10);
420                materialPtr->getTechnique(0)->getPass(0)->createTextureUnitState("ClusterTextureDiffuseColor", 0);
421                materialPtr->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureFiltering(Ogre::FO_NONE, Ogre::FO_NONE, Ogre::FO_NONE);                     
422                materialPtr->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);         
423                materialPtr->getTechnique(0)->getPass(0)->setLightingEnabled(false);
424        }
425        mClusterTexture->setMaterial(materialPtr.getPointer());
426
427        mTextureAtlas->addTexture(mClusterTexture);
428
429        mNumTextures = mEntityClustersGrouped->getNumSubEntities();
430        mCurrentTexture = 0;
431}
432
433void BillboardCloudDiffuseColorTextureViewMode::destroyScene()
434{
435       
436}
437
438}
Note: See TracBrowser for help on using the repository browser.