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