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

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