Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/shaders/src/orxonox/graphics/LensFlare.cc @ 9448

Last change on this file since 9448 was 9448, checked in by davidsa, 11 years ago

Added Hardware Occlusion Query Capabilities for use in dynamic flare effects, this is still a pretty static implemenation, meaning it will fail with mutltiple HOQ objects on screen, also needs some tidying up and documentation at a later point

File size: 5.0 KB
Line 
1 /*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *      Reto Grieder (physics)
25 *   Co-authors:
26 *      ...
27 *
28 */
29
30/**
31  @file LensFlare.cc
32  @brief Implementation of the LensFlare class.
33*/
34
35#include "LensFlare.h"
36
37#include "core/XMLPort.h"
38#include "graphics/Billboard.h"
39#include "CameraManager.h"
40#include "RenderQueueListener.h"
41
42#include <OgreSphere.h>
43#include <OgreRenderWindow.h>
44
45namespace orxonox
46{
47    CreateFactory(LensFlare);
48   
49    LensFlare::LensFlare(BaseObject* creator) : StaticEntity(creator), scale_(1.0f)
50    {
51        RegisterObject(LensFlare);
52       
53        this->createBillboards();
54       
55        this->registerVariables();
56    }
57
58    LensFlare::~LensFlare()
59    {
60    }
61
62    void LensFlare::XMLPort(Element& xmlelement, XMLPort::Mode mode)
63    {
64        SUPER(LensFlare, XMLPort, xmlelement, mode);
65        XMLPortParam(LensFlare, "scale", setScale, getScale, xmlelement, mode).defaultValues(1.0f);
66    }
67   
68    void LensFlare::registerVariables()
69    {
70        registerVariable(this->scale_, VariableDirection::ToClient, new NetworkCallback<LensFlare>(this, &LensFlare::updateBillboardPositions));
71    }
72   
73    void LensFlare::createBillboards()
74    {
75        this->occlusionBillboard_ = new Billboard(this);
76        this->occlusionBillboard_->setMaterial("lensflare/hoq");
77        this->occlusionBillboard_->setPosition(this->getPosition());
78        this->occlusionBillboard_->setVisible(false);
79        this->occlusionBillboard_->setRenderQueueGroup(RENDER_QUEUE_HOQ);
80        this->attach(this->occlusionBillboard_);
81       
82        Billboard* burst = new Billboard(this);
83        burst->setMaterial("lensflare/burst");
84        burst->setPosition(this->getPosition());
85        burst->setVisible(false);
86        this->attach(burst);
87    }
88   
89    void LensFlare::updateBillboardPositions()
90    {
91        Ogre::Camera* camera=CameraManager::getInstance().getActiveCamera()->getOgreCamera(); //get active Ogre Camera Instance, so we can check whether the light source is visible
92        bool lightIsVisible=camera->isVisible(this->getPosition()); //is the light source visible from our point of view?
93        this->cameraDistance_=camera->getPosition().distance(this->getPosition());
94        unsigned int dimension=this->cameraDistance_*this->scale_;
95       
96        this->occlusionBillboard_->setPosition(this->getPosition());
97        this->occlusionBillboard_->setVisible(lightIsVisible);
98        this->occlusionBillboard_->setDefaultDimensions(dimension,dimension);
99       
100        Billboard* burst=static_cast<Billboard*>(getAttachedObject(1));
101        burst->setPosition(this->getPosition());
102        burst->setVisible(lightIsVisible);
103        burst->setDefaultDimensions(dimension,dimension);
104    }
105
106    void LensFlare::tick(float dt)
107    {
108        if(this->isVisible())
109        {
110            updateBillboardPositions();
111            if(this->occlusionBillboard_->isVisible()) {
112                unsigned int dimension=this->cameraDistance_*this->scale_;
113                Ogre::Sphere* sphere=new Ogre::Sphere(this->getPosition(),dimension*0.25);
114                Ogre::Camera* camera=CameraManager::getInstance().getActiveCamera()->getOgreCamera();
115                float left, right, top, bottom;
116                camera->projectSphere(*sphere,&left,&top,&right,&bottom);//approximate maximum pixel count of billboard with a sphere
117                delete sphere;
118               
119                Ogre::RenderWindow* window = GraphicsManager::getInstance().getRenderWindow();
120                float maxCount=(right-left)*(top-bottom)*window->getWidth()*window->getHeight()*0.25;
121                float pixelCount=this->getScene()->getRenderQueueListener()->getPixelCount();//get pixel count
122                float ratio=pixelCount/maxCount;
123                //orxout() << "maxCount: " << maxCount << " HOQ: " << pixelCount << " ratio: " << ratio << std::endl;
124                ColourValue* colour = new ColourValue(1.0f,1.0f,1.0f,ratio); //adjust alpha of billboard
125               
126                Billboard* burst=static_cast<Billboard*>(getAttachedObject(1));
127                burst->setColour(*colour);
128                delete colour;
129            }
130        }
131    }
132
133    void LensFlare::changedVisibility()
134    {
135    }
136}
Note: See TracBrowser for help on using the repository browser.