[9445] | 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" |
---|
[9448] | 40 | #include "RenderQueueListener.h" |
---|
[9445] | 41 | |
---|
[9448] | 42 | #include <OgreSphere.h> |
---|
| 43 | #include <OgreRenderWindow.h> |
---|
| 44 | |
---|
[9445] | 45 | namespace orxonox |
---|
| 46 | { |
---|
| 47 | CreateFactory(LensFlare); |
---|
| 48 | |
---|
[9466] | 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)) |
---|
[9445] | 50 | { |
---|
| 51 | RegisterObject(LensFlare); |
---|
| 52 | |
---|
[9500] | 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 |
---|
[9510] | 55 | this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/burst"),0.7f,1.2f,1.05f)); //secondary burst |
---|
[9500] | 56 | this->lensConfiguration_->push_back(new LensFlare::Lens(new std::string("lensflare/bursthalo"),0.7f,0.9f,1.0f)); //halo around main burst |
---|
[9510] | 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)); |
---|
[9500] | 63 | |
---|
[9445] | 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); |
---|
[9448] | 76 | XMLPortParam(LensFlare, "scale", setScale, getScale, xmlelement, mode).defaultValues(1.0f); |
---|
[9461] | 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); |
---|
[9500] | 80 | XMLPortParam(LensFlare, "colour", setColour, getColour, xmlelement, mode); |
---|
[9445] | 81 | } |
---|
| 82 | |
---|
| 83 | void LensFlare::registerVariables() |
---|
| 84 | { |
---|
[9461] | 85 | registerVariable(this->scale_, VariableDirection::ToClient); |
---|
| 86 | registerVariable(this->fadeOnViewBorder_, VariableDirection::ToClient); |
---|
| 87 | registerVariable(this->fadeResolution_, VariableDirection::ToClient); |
---|
[9445] | 88 | } |
---|
[9461] | 89 | |
---|
| 90 | /** |
---|
| 91 | @brief |
---|
| 92 | This function creates all the billboards needed for the flare effect |
---|
| 93 | */ |
---|
[9445] | 94 | void LensFlare::createBillboards() |
---|
| 95 | { |
---|
[9448] | 96 | this->occlusionBillboard_ = new Billboard(this); |
---|
| 97 | this->occlusionBillboard_->setMaterial("lensflare/hoq"); |
---|
| 98 | this->occlusionBillboard_->setVisible(false); |
---|
[9461] | 99 | this->occlusionBillboard_->disableFrustumCulling(); |
---|
[9448] | 100 | this->occlusionBillboard_->setRenderQueueGroup(RENDER_QUEUE_HOQ); |
---|
| 101 | this->attach(this->occlusionBillboard_); |
---|
| 102 | |
---|
[9500] | 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 | } |
---|
[9445] | 110 | } |
---|
[9461] | 111 | |
---|
| 112 | /** |
---|
| 113 | @brief |
---|
| 114 | This function updates the states of all the billboards, i.e. their positions, visibilty and dimensions |
---|
[9464] | 115 | @param viewDirection |
---|
| 116 | normalised vector pointing from the current camera to the point light center |
---|
[9461] | 117 | @param dimension |
---|
| 118 | the current dimension of the main billboard, we're always using square billboards |
---|
[9464] | 119 | @param lightIsVisible |
---|
| 120 | is the (point-)light source currently visible |
---|
[9461] | 121 | */ |
---|
[9466] | 122 | void LensFlare::updateBillboardStates(Vector3& viewDirection, float dimension, bool lightIsVisible) |
---|
[9461] | 123 | { |
---|
[9500] | 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++) { |
---|
[9461] | 129 | Billboard* billboard=static_cast<Billboard*>(*it); |
---|
[9500] | 130 | LensFlare::Lens* lens=lensConfiguration_->at(i); |
---|
| 131 | billboard->setPosition(-viewDirection*(1.0f-lens->position_)); |
---|
[9461] | 132 | billboard->setVisible(lightIsVisible); |
---|
[9500] | 133 | billboard->setDefaultDimensions(dimension*lens->scale_,dimension*lens->scale_); |
---|
[9464] | 134 | i++; |
---|
[9461] | 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 | { |
---|
[9466] | 146 | this->colour_->a=alpha; |
---|
[9461] | 147 | std::set<WorldEntity*>::const_iterator it = this->getAttachedObjects().begin(); |
---|
| 148 | it++; |
---|
[9466] | 149 | for(int i=0;it!=this->getAttachedObjects().end(); it++) { |
---|
[9500] | 150 | ColourValue* cur=new ColourValue(0,0,0,0); |
---|
| 151 | (*cur)+= *(this->colour_); |
---|
| 152 | cur->a*=lensConfiguration_->at(i)->alpha_; |
---|
[9461] | 153 | Billboard* billboard=static_cast<Billboard*>(*it); |
---|
[9500] | 154 | billboard->setColour(*cur); |
---|
[9466] | 155 | i++; |
---|
[9461] | 156 | } |
---|
| 157 | } |
---|
[9445] | 158 | |
---|
[9461] | 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 | */ |
---|
[9466] | 167 | unsigned int LensFlare::getPointCount(float dimension) const |
---|
[9445] | 168 | { |
---|
[9461] | 169 | Ogre::Camera* camera=CameraManager::getInstance().getActiveCamera()->getOgreCamera(); |
---|
[9491] | 170 | Vector3 position = this->getWorldPosition(); |
---|
[9466] | 171 | Vector3 nX = camera->getDerivedOrientation().xAxis().normalisedCopy(); |
---|
| 172 | Vector3 nY = camera->getDerivedOrientation().yAxis().normalisedCopy(); |
---|
[9461] | 173 | int halfRes=fadeResolution_/2; |
---|
[9466] | 174 | float resDim=dimension/fadeResolution_; |
---|
[9461] | 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; |
---|
[9445] | 188 | } |
---|
| 189 | |
---|
| 190 | void LensFlare::tick(float dt) |
---|
| 191 | { |
---|
| 192 | if(this->isVisible()) |
---|
| 193 | { |
---|
[9461] | 194 | Ogre::Camera* camera=CameraManager::getInstance().getActiveCamera()->getOgreCamera(); //get active Ogre Camera Instance, so we can check whether the light source is visible |
---|
[9491] | 195 | this->cameraDistance_=camera->getDerivedPosition().distance(this->getPosition()); |
---|
[9466] | 196 | float dimension=this->cameraDistance_*this->scale_; |
---|
[9461] | 197 | if(!this->fadeOnViewBorder_) |
---|
| 198 | { |
---|
| 199 | this->fadeResolution_=3;//this is so we can still determine when the billboard has left the screen |
---|
| 200 | } |
---|
[9491] | 201 | unsigned int pointCount=this->getPointCount(dimension*0.25f*this->scale_); |
---|
| 202 | Vector3 viewDirection=this->getWorldPosition()-camera->getDerivedPosition()-camera->getDerivedDirection()*this->cameraDistance_; |
---|
[9464] | 203 | updateBillboardStates(viewDirection,dimension,pointCount>0); |
---|
[9461] | 204 | if(pointCount>0) { |
---|
[9491] | 205 | Ogre::Sphere* sphere=new Ogre::Sphere(this->getPosition(),dimension*0.25f*this->scale_); |
---|
[9448] | 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(); |
---|
[9466] | 211 | float maxCount=(right-left)*(top-bottom)*window->getWidth()*window->getHeight()*0.25f; |
---|
[9448] | 212 | float pixelCount=this->getScene()->getRenderQueueListener()->getPixelCount();//get pixel count |
---|
[9466] | 213 | float ratio=(maxCount<0.0f)?0.0f:(pixelCount/maxCount);//prevent underflow and division by zero |
---|
[9461] | 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 |
---|
[9464] | 220 | this->updateBillboardAlphas(std::min(1.0f,std::pow(std::min(ratio,borderRatio),this->fadeExponent_))); |
---|
[9448] | 221 | } |
---|
[9445] | 222 | } |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | void LensFlare::changedVisibility() |
---|
| 226 | { |
---|
[9461] | 227 | |
---|
[9445] | 228 | } |
---|
| 229 | } |
---|