Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Tweaked the lens configuration to achieve a pretty lens flare effect

File size: 10.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), colour_(new ColourValue(1.0f,0.9f,0.9f))
50    {
51        RegisterObject(LensFlare);
52       
53        this->lensConfiguration_=new std::vector<LensFlare::Lens*>();
54        this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/burst"),1.0f,1.0f,1.0f)); //main burst
55        this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/burst"),0.7f,1.2f,1.05f)); //secondary burst
56        this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/bursthalo"),0.7f,0.9f,1.0f)); //halo around main burst
57        this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/ring"),0.1f,2.5f,0.9f)); //all the different distanced lenses
58        this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/iris"),0.1f,0.2f,0.5f));
59        this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/halo5"),0.1f,0.3f,0.45f));
60        this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/halo5"),0.4f,0.2f,0.35f));
61        this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/iris"),0.1f,0.4f,0.25f));
62        this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/halo4"),0.05f,0.2f,0.2f));
63       
64        this->createBillboards();
65       
66        this->registerVariables();
67    }
68
69    LensFlare::~LensFlare()
70    {
71    }
72
73    void LensFlare::XMLPort(Element& xmlelement, XMLPort::Mode mode)
74    {
75        SUPER(LensFlare, XMLPort, xmlelement, mode);
76        XMLPortParam(LensFlare, "scale", setScale, getScale, xmlelement, mode).defaultValues(1.0f);
77        XMLPortParam(LensFlare, "fadeOnViewBorder", setFadeOnViewBorder, isFadeOnViewBorder, xmlelement, mode).defaultValues(true);
78        XMLPortParam(LensFlare, "fadeResolution", setFadeResolution, getFadeResolution, xmlelement, mode).defaultValues(7);
79        XMLPortParam(LensFlare, "fadeExponent", setFadeExponent, getFadeExponent, xmlelement, mode).defaultValues(2.0f);
80        XMLPortParam(LensFlare, "colour", setColour, getColour, xmlelement, mode);
81    }
82   
83    void LensFlare::registerVariables()
84    {
85        registerVariable(this->scale_, VariableDirection::ToClient);
86        registerVariable(this->fadeOnViewBorder_, VariableDirection::ToClient);
87        registerVariable(this->fadeResolution_, VariableDirection::ToClient);
88    }
89
90    /**
91    @brief
92        This function creates all the billboards needed for the flare effect
93    */
94    void LensFlare::createBillboards()
95    {
96        this->occlusionBillboard_ = new Billboard(this);
97        this->occlusionBillboard_->setMaterial("lensflare/hoq");
98        this->occlusionBillboard_->setVisible(false);
99        this->occlusionBillboard_->disableFrustumCulling();
100        this->occlusionBillboard_->setRenderQueueGroup(RENDER_QUEUE_HOQ);
101        this->attach(this->occlusionBillboard_);
102       
103        for(std::vector<LensFlare::Lens*>::iterator it = lensConfiguration_->begin(); it != lensConfiguration_->end(); ++it) {
104            Billboard* lensPart=new Billboard(this);
105            lensPart->setMaterial(*(*it)->material_);
106            lensPart->disableFrustumCulling();
107            lensPart->setVisible(true);
108            this->attach(lensPart);
109        }
110    }
111
112    /**
113    @brief
114        This function updates the states of all the billboards, i.e. their positions, visibilty and dimensions
115    @param viewDirection
116        normalised vector pointing from the current camera to the point light center
117    @param dimension
118        the current dimension of the main billboard, we're always using square billboards
119    @param lightIsVisible
120        is the (point-)light source currently visible
121    */
122    void LensFlare::updateBillboardStates(Vector3& viewDirection, float dimension, bool lightIsVisible)
123    { 
124        this->occlusionBillboard_->setDefaultDimensions(dimension*0.5f,dimension*0.5f);
125        this->occlusionBillboard_->setVisible(lightIsVisible);
126        std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin();
127        it++;
128        for(int i=0; it != this->getAttachedObjects().end(); it++) {
129            Billboard* billboard=static_cast<Billboard*>(*it);
130            LensFlare::Lens* lens=lensConfiguration_->at(i);
131            billboard->setPosition(-viewDirection*(1.0f-lens->position_));
132            billboard->setVisible(lightIsVisible);
133            billboard->setDefaultDimensions(dimension*lens->scale_,dimension*lens->scale_);
134            i++;
135        }
136    }
137
138    /**
139    @brief
140        This function updates the alpha values for all billboards except for the one used for Hardware Occlusion Querying
141    @param alpha
142        the new alpha value all visible billboards should use
143    */
144    void LensFlare::updateBillboardAlphas(float alpha)
145    {
146        this->colour_->a=alpha;
147        std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin();
148        it++;
149        for(int i=0;it!=this->getAttachedObjects().end(); it++) {
150            ColourValue* cur=new ColourValue(0,0,0,0);
151            (*cur)+= *(this->colour_);
152            cur->a*=lensConfiguration_->at(i)->alpha_;
153            Billboard* billboard=static_cast<Billboard*>(*it);
154            billboard->setColour(*cur);
155            i++;
156        }
157    }
158   
159    /**
160    @brief
161        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
162    @param dimension
163        the current dimension of the main billboard, we're always using square billboards
164    @return
165        the absolute amount of point samples that are currently captured by the camera of the view port
166    */
167    unsigned int LensFlare::getPointCount(float dimension) const
168    {
169        Ogre::Camera* camera=CameraManager::getInstance().getActiveCamera()->getOgreCamera();
170        Vector3 position = this->getWorldPosition();
171        Vector3 nX = camera->getDerivedOrientation().xAxis().normalisedCopy();
172        Vector3 nY = camera->getDerivedOrientation().yAxis().normalisedCopy();
173        int halfRes=fadeResolution_/2;
174        float resDim=dimension/fadeResolution_;
175        unsigned int count=0;
176        for(int i=-halfRes;i<=halfRes;i++)
177        {
178            for(int j=-halfRes;j<=halfRes;j++)
179            {
180                Vector3 point=position+(i*resDim)*nX+(j*resDim)*nY;//generate point samples
181                if(camera->isVisible(point))
182                {
183                    count++;
184                }
185            }
186        }
187        return count;
188    }
189
190    void LensFlare::tick(float dt)
191    {
192        if(this->isVisible())
193        {
194            Ogre::Camera* camera=CameraManager::getInstance().getActiveCamera()->getOgreCamera(); //get active Ogre Camera Instance, so we can check whether the light source is visible
195            this->cameraDistance_=camera->getDerivedPosition().distance(this->getPosition());
196            float dimension=this->cameraDistance_*this->scale_;
197            if(!this->fadeOnViewBorder_)
198            {
199                this->fadeResolution_=3;//this is so we can still determine when the billboard has left the screen
200            }
201            unsigned int pointCount=this->getPointCount(dimension*0.25f*this->scale_);
202            Vector3 viewDirection=this->getWorldPosition()-camera->getDerivedPosition()-camera->getDerivedDirection()*this->cameraDistance_;
203            updateBillboardStates(viewDirection,dimension,pointCount>0);
204            if(pointCount>0) {
205                Ogre::Sphere* sphere=new Ogre::Sphere(this->getPosition(),dimension*0.25f*this->scale_);
206                float left, right, top, bottom;
207                camera->projectSphere(*sphere,&left,&top,&right,&bottom);//approximate maximum pixel count of billboard with a sphere
208                delete sphere;
209               
210                Ogre::RenderWindow* window = GraphicsManager::getInstance().getRenderWindow();
211                float maxCount=(right-left)*(top-bottom)*window->getWidth()*window->getHeight()*0.25f;
212                float pixelCount=this->getScene()->getRenderQueueListener()->getPixelCount();//get pixel count
213                float ratio=(maxCount<0.0f)?0.0f:(pixelCount/maxCount);//prevent underflow and division by zero
214                float borderRatio=1.0f;
215                if(this->fadeOnViewBorder_)
216                {
217                    borderRatio=((float) pointCount)/(((float) fadeResolution_)*((float) fadeResolution_));//ratio for the border fade
218                }
219                //update alpha values of all billboards except the HOQ billboard
220                this->updateBillboardAlphas(std::min(1.0f,std::pow(std::min(ratio,borderRatio),this->fadeExponent_)));
221            }
222        }
223    }
224
225    void LensFlare::changedVisibility()
226    {
227     
228    }
229}
Note: See TracBrowser for help on using the repository browser.