Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9461 was 9461, checked in by davidsa, 12 years ago

Updated orxonox::Billboard so you can disable FrustumCulling. Also improved orxonox::LensFlare and added documentation.

File size: 8.1 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), fadeOnViewBorder_(true), fadeResolution_(7), fadeExponent_(2.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        XMLPortParam(LensFlare, "fadeOnViewBorder", setFadeOnViewBorder, isFadeOnViewBorder, xmlelement, mode).defaultValues(true);
67        XMLPortParam(LensFlare, "fadeResolution", setFadeResolution, getFadeResolution, xmlelement, mode).defaultValues(7);
68        XMLPortParam(LensFlare, "fadeExponent", setFadeExponent, getFadeExponent, xmlelement, mode).defaultValues(2.0f);
69    }
70   
71    void LensFlare::registerVariables()
72    {
73        registerVariable(this->scale_, VariableDirection::ToClient);
74        registerVariable(this->fadeOnViewBorder_, VariableDirection::ToClient);
75        registerVariable(this->fadeResolution_, VariableDirection::ToClient);
76    }
77
78    /**
79    @brief
80        This function creates all the billboards needed for the flare effect
81    */
82    void LensFlare::createBillboards()
83    {
84        //TODO: add more billboards, possibly do some cleaning up, by using a loop
85        this->occlusionBillboard_ = new Billboard(this);
86        this->occlusionBillboard_->setMaterial("lensflare/hoq");
87        this->occlusionBillboard_->setPosition(this->getPosition());
88        this->occlusionBillboard_->setVisible(false);
89        this->occlusionBillboard_->disableFrustumCulling();
90        this->occlusionBillboard_->setRenderQueueGroup(RENDER_QUEUE_HOQ);
91        this->attach(this->occlusionBillboard_);
92       
93        Billboard* burst = new Billboard(this);
94        burst->setMaterial("lensflare/burst");
95        burst->setPosition(this->getPosition());
96        burst->disableFrustumCulling();
97        burst->setVisible(true);
98        this->attach(burst);
99    }
100
101    /**
102    @brief
103        This function updates the states of all the billboards, i.e. their positions, visibilty and dimensions
104    @param dimension
105        the current dimension of the main billboard, we're always using square billboards
106    */
107    void LensFlare::updateBillboardStates(unsigned int dimension, bool lightIsVisible)
108    { 
109        //TODO: position and dimensions need to be calculated for everything but the main burst of the flare
110        for(std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin(); it != this->getAttachedObjects().end(); it++) {
111            Billboard* billboard=static_cast<Billboard*>(*it);
112            billboard->setPosition(this->getPosition());
113            billboard->setVisible(lightIsVisible);
114            billboard->setDefaultDimensions(dimension,dimension);
115        }
116    }
117
118    /**
119    @brief
120        This function updates the alpha values for all billboards except for the one used for Hardware Occlusion Querying
121    @param alpha
122        the new alpha value all visible billboards should use
123    */
124    void LensFlare::updateBillboardAlphas(float alpha)
125    {
126        ColourValue* colour = new ColourValue(1.0f,1.0f,1.0f,alpha);
127        std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin();
128        it++;
129        for(;it!=this->getAttachedObjects().end(); it++) {
130            Billboard* billboard=static_cast<Billboard*>(*it);
131            billboard->setColour(*colour);
132        }
133        delete colour;
134    }
135   
136    /**
137    @brief
138        This function generates point samples of the main burst billboard according to the fadeResolution and returns how many of them are in the view port
139    @param dimension
140        the current dimension of the main billboard, we're always using square billboards
141    @return
142        the absolute amount of point samples that are currently captured by the camera of the view port
143    */
144    unsigned int LensFlare::getPointCount(unsigned int dimension) const
145    {
146        Ogre::Camera* camera=CameraManager::getInstance().getActiveCamera()->getOgreCamera();
147        Vector3 position = this->getPosition();
148        Vector3 nX = camera->getOrientation().xAxis().normalisedCopy();
149        Vector3 nY = camera->getOrientation().yAxis().normalisedCopy();
150        int halfRes=fadeResolution_/2;
151        int resDim=dimension/fadeResolution_;
152        unsigned int count=0;
153        for(int i=-halfRes;i<=halfRes;i++)
154        {
155            for(int j=-halfRes;j<=halfRes;j++)
156            {
157                Vector3 point=position+(i*resDim)*nX+(j*resDim)*nY;//generate point samples
158                if(camera->isVisible(point))
159                {
160                    count++;
161                }
162            }
163        }
164        return count;
165    }
166
167    void LensFlare::tick(float dt)
168    {
169        if(this->isVisible())
170        {
171            Ogre::Camera* camera=CameraManager::getInstance().getActiveCamera()->getOgreCamera(); //get active Ogre Camera Instance, so we can check whether the light source is visible
172            this->cameraDistance_=camera->getPosition().distance(this->getPosition());
173            unsigned int dimension=this->cameraDistance_*this->scale_;
174            if(!this->fadeOnViewBorder_)
175            {
176                this->fadeResolution_=3;//this is so we can still determine when the billboard has left the screen
177            }
178            unsigned int pointCount=this->getPointCount(dimension);
179            updateBillboardStates(dimension,pointCount>0);
180            if(pointCount>0) {
181                Ogre::Sphere* sphere=new Ogre::Sphere(this->getPosition(),dimension*0.25);
182                float left, right, top, bottom;
183                camera->projectSphere(*sphere,&left,&top,&right,&bottom);//approximate maximum pixel count of billboard with a sphere
184                delete sphere;
185               
186                Ogre::RenderWindow* window = GraphicsManager::getInstance().getRenderWindow();
187                float maxCount=(right-left)*(top-bottom)*window->getWidth()*window->getHeight()*0.25;
188                float pixelCount=this->getScene()->getRenderQueueListener()->getPixelCount();//get pixel count
189                float ratio=(maxCount==0)?0:(pixelCount/maxCount);//prevent division by zero
190                float borderRatio=1.0f;
191                if(this->fadeOnViewBorder_)
192                {
193                    borderRatio=((float) pointCount)/(((float) fadeResolution_)*((float) fadeResolution_));//ratio for the border fade
194                }
195               
196                //update alpha values of all billboards except the HOQ billboard
197                this->updateBillboardAlphas(std::min(1.0f,std::pow(std::min(ratio,borderRatio),2.0f)));
198            }
199        }
200    }
201
202    void LensFlare::changedVisibility()
203    {
204     
205    }
206}
Note: See TracBrowser for help on using the repository browser.