Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

FIX: fixed light intensity being affected in a weird way by the distance to the light source, and weird scaling artifacts

File size: 9.7 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), colour_(new ColourValue(1.0f,0.9f,0.9f))
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_->setVisible(false);
88        this->occlusionBillboard_->disableFrustumCulling();
89        this->occlusionBillboard_->setRenderQueueGroup(RENDER_QUEUE_HOQ);
90        this->attach(this->occlusionBillboard_);
91       
92        Billboard* burst = new Billboard(this);
93        burst->setMaterial("lensflare/burst");
94        burst->disableFrustumCulling();
95        burst->setVisible(true);
96        this->attach(burst);
97       
98        Billboard* bursthalo = new Billboard(this);
99        bursthalo->setMaterial("lensflare/bursthalo");
100        bursthalo->disableFrustumCulling();
101        bursthalo->setVisible(true);
102        this->attach(bursthalo);
103       
104        bursthalo = new Billboard(this);
105        bursthalo->setMaterial("lensflare/halo1");
106        bursthalo->disableFrustumCulling();
107        bursthalo->setVisible(true);
108        this->attach(bursthalo);
109       
110        bursthalo = new Billboard(this);
111        bursthalo->setMaterial("lensflare/halo2");
112        bursthalo->disableFrustumCulling();
113        bursthalo->setVisible(true);
114        this->attach(bursthalo);
115       
116        bursthalo = new Billboard(this);
117        bursthalo->setMaterial("lensflare/halo3");
118        bursthalo->disableFrustumCulling();
119        bursthalo->setVisible(true);
120        this->attach(bursthalo);
121    }
122
123    /**
124    @brief
125        This function updates the states of all the billboards, i.e. their positions, visibilty and dimensions
126    @param viewDirection
127        normalised vector pointing from the current camera to the point light center
128    @param dimension
129        the current dimension of the main billboard, we're always using square billboards
130    @param lightIsVisible
131        is the (point-)light source currently visible
132    */
133    void LensFlare::updateBillboardStates(Vector3& viewDirection, float dimension, bool lightIsVisible)
134    { 
135        //TODO: develop a more sane method for determining positions and scale factors of the flare components
136        //A good solution would probably be to introduce a data structure which stores a lens flare configuration
137        int i=0;
138        float step=0.0f;
139        for(std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin(); it != this->getAttachedObjects().end(); it++) {
140            Billboard* billboard=static_cast<Billboard*>(*it);
141            billboard->setPosition(-viewDirection*step);
142            billboard->setVisible(lightIsVisible);
143            billboard->setDefaultDimensions((i==0?0.5f:1.0f)*(i>2?0.25f:1.0f)*dimension*std::pow((1.0f-step),-1.0f),(i==0?0.5f:1.0f)*(i>2?0.25f:1.0f)*dimension*std::pow((1.0f-step),-1.0f));
144            step=0.25f*(i>2?(i-2):0);
145            i++;
146        }
147    }
148
149    /**
150    @brief
151        This function updates the alpha values for all billboards except for the one used for Hardware Occlusion Querying
152    @param alpha
153        the new alpha value all visible billboards should use
154    */
155    void LensFlare::updateBillboardAlphas(float alpha)
156    {
157        this->colour_->a=alpha;
158        std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin();
159        it++;
160        for(int i=0;it!=this->getAttachedObjects().end(); it++) {
161            if(i==2)
162            {
163                this->colour_->a*=0.33f;
164            }
165            Billboard* billboard=static_cast<Billboard*>(*it);
166            billboard->setColour(*(this->colour_));
167            i++;
168        }
169    }
170   
171    /**
172    @brief
173        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
174    @param dimension
175        the current dimension of the main billboard, we're always using square billboards
176    @return
177        the absolute amount of point samples that are currently captured by the camera of the view port
178    */
179    unsigned int LensFlare::getPointCount(float dimension) const
180    {
181        Ogre::Camera* camera=CameraManager::getInstance().getActiveCamera()->getOgreCamera();
182        Vector3 position = this->getWorldPosition();
183        Vector3 nX = camera->getDerivedOrientation().xAxis().normalisedCopy();
184        Vector3 nY = camera->getDerivedOrientation().yAxis().normalisedCopy();
185        int halfRes=fadeResolution_/2;
186        float resDim=dimension/fadeResolution_;
187        unsigned int count=0;
188        for(int i=-halfRes;i<=halfRes;i++)
189        {
190            for(int j=-halfRes;j<=halfRes;j++)
191            {
192                Vector3 point=position+(i*resDim)*nX+(j*resDim)*nY;//generate point samples
193                if(camera->isVisible(point))
194                {
195                    count++;
196                }
197            }
198        }
199        return count;
200    }
201
202    void LensFlare::tick(float dt)
203    {
204        if(this->isVisible())
205        {
206            Ogre::Camera* camera=CameraManager::getInstance().getActiveCamera()->getOgreCamera(); //get active Ogre Camera Instance, so we can check whether the light source is visible
207            this->cameraDistance_=camera->getDerivedPosition().distance(this->getPosition());
208            float dimension=this->cameraDistance_*this->scale_;
209            if(!this->fadeOnViewBorder_)
210            {
211                this->fadeResolution_=3;//this is so we can still determine when the billboard has left the screen
212            }
213            unsigned int pointCount=this->getPointCount(dimension*0.25f*this->scale_);
214            Vector3 viewDirection=this->getWorldPosition()-camera->getDerivedPosition()-camera->getDerivedDirection()*this->cameraDistance_;
215            updateBillboardStates(viewDirection,dimension,pointCount>0);
216            if(pointCount>0) {
217                Ogre::Sphere* sphere=new Ogre::Sphere(this->getPosition(),dimension*0.25f*this->scale_);
218                float left, right, top, bottom;
219                camera->projectSphere(*sphere,&left,&top,&right,&bottom);//approximate maximum pixel count of billboard with a sphere
220                delete sphere;
221               
222                Ogre::RenderWindow* window = GraphicsManager::getInstance().getRenderWindow();
223                float maxCount=(right-left)*(top-bottom)*window->getWidth()*window->getHeight()*0.25f;
224                float pixelCount=this->getScene()->getRenderQueueListener()->getPixelCount();//get pixel count
225                float ratio=(maxCount<0.0f)?0.0f:(pixelCount/maxCount);//prevent underflow and division by zero
226                float borderRatio=1.0f;
227                if(this->fadeOnViewBorder_)
228                {
229                    borderRatio=((float) pointCount)/(((float) fadeResolution_)*((float) fadeResolution_));//ratio for the border fade
230                }
231                //update alpha values of all billboards except the HOQ billboard
232                this->updateBillboardAlphas(std::min(1.0f,std::pow(std::min(ratio,borderRatio),this->fadeExponent_)));
233            }
234        }
235    }
236
237    void LensFlare::changedVisibility()
238    {
239     
240    }
241}
Note: See TracBrowser for help on using the repository browser.