[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: |
---|
[11075] | 23 | * David 'davidsa' Salvisberg |
---|
[9445] | 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | @file LensFlare.cc |
---|
| 31 | @brief Implementation of the LensFlare class. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include "LensFlare.h" |
---|
| 35 | |
---|
| 36 | #include "core/XMLPort.h" |
---|
| 37 | #include "graphics/Billboard.h" |
---|
| 38 | #include "CameraManager.h" |
---|
[9448] | 39 | #include "RenderQueueListener.h" |
---|
[9445] | 40 | |
---|
[9448] | 41 | #include <OgreSphere.h> |
---|
| 42 | #include <OgreRenderWindow.h> |
---|
| 43 | |
---|
[9445] | 44 | namespace orxonox |
---|
| 45 | { |
---|
[11073] | 46 | RegisterClass(LensFlare); |
---|
[9445] | 47 | |
---|
[11078] | 48 | LensFlare::LensFlare(Context* context) : StaticEntity(context), scale_(1.0f), fadeOnViewBorder_(true), fadeResolution_(7), fadeExponent_(2.0f) |
---|
[9445] | 49 | { |
---|
| 50 | RegisterObject(LensFlare); |
---|
| 51 | |
---|
[11078] | 52 | this->lensConfiguration_.emplace_back("lensflare/burst", 1.0f, 1.0f, 1.0f); //main burst |
---|
| 53 | this->lensConfiguration_.emplace_back("lensflare/burst", 0.7f, 1.2f, 1.05f); //secondary burst |
---|
| 54 | this->lensConfiguration_.emplace_back("lensflare/bursthalo", 0.7f, 0.9f, 1.0f); //halo around main burst |
---|
| 55 | this->lensConfiguration_.emplace_back("lensflare/ring", 0.1f, 2.5f, 0.9f); //all the different distanced lenses |
---|
| 56 | this->lensConfiguration_.emplace_back("lensflare/iris", 0.1f, 0.2f, 0.5f); |
---|
| 57 | this->lensConfiguration_.emplace_back("lensflare/halo5", 0.1f, 0.3f, 0.45f); |
---|
| 58 | this->lensConfiguration_.emplace_back("lensflare/halo5", 0.4f, 0.2f, 0.35f); |
---|
| 59 | this->lensConfiguration_.emplace_back("lensflare/iris", 0.1f, 0.4f, 0.25f); |
---|
| 60 | this->lensConfiguration_.emplace_back("lensflare/halo4", 0.05f, 0.2f, 0.2f); |
---|
[9500] | 61 | |
---|
[9445] | 62 | this->createBillboards(); |
---|
| 63 | |
---|
| 64 | this->registerVariables(); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | LensFlare::~LensFlare() |
---|
| 68 | { |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | void LensFlare::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 72 | { |
---|
| 73 | SUPER(LensFlare, XMLPort, xmlelement, mode); |
---|
[9448] | 74 | XMLPortParam(LensFlare, "scale", setScale, getScale, xmlelement, mode).defaultValues(1.0f); |
---|
[9461] | 75 | XMLPortParam(LensFlare, "fadeOnViewBorder", setFadeOnViewBorder, isFadeOnViewBorder, xmlelement, mode).defaultValues(true); |
---|
| 76 | XMLPortParam(LensFlare, "fadeResolution", setFadeResolution, getFadeResolution, xmlelement, mode).defaultValues(7); |
---|
| 77 | XMLPortParam(LensFlare, "fadeExponent", setFadeExponent, getFadeExponent, xmlelement, mode).defaultValues(2.0f); |
---|
[9500] | 78 | XMLPortParam(LensFlare, "colour", setColour, getColour, xmlelement, mode); |
---|
[9445] | 79 | } |
---|
| 80 | |
---|
| 81 | void LensFlare::registerVariables() |
---|
| 82 | { |
---|
[9461] | 83 | registerVariable(this->scale_, VariableDirection::ToClient); |
---|
| 84 | registerVariable(this->fadeOnViewBorder_, VariableDirection::ToClient); |
---|
| 85 | registerVariable(this->fadeResolution_, VariableDirection::ToClient); |
---|
[9445] | 86 | } |
---|
[9461] | 87 | |
---|
| 88 | /** |
---|
| 89 | @brief |
---|
| 90 | This function creates all the billboards needed for the flare effect |
---|
| 91 | */ |
---|
[9445] | 92 | void LensFlare::createBillboards() |
---|
| 93 | { |
---|
[11073] | 94 | this->occlusionBillboard_ = new Billboard(this->getContext()); |
---|
[9448] | 95 | this->occlusionBillboard_->setMaterial("lensflare/hoq"); |
---|
| 96 | this->occlusionBillboard_->setVisible(false); |
---|
[9461] | 97 | this->occlusionBillboard_->disableFrustumCulling(); |
---|
[9448] | 98 | this->occlusionBillboard_->setRenderQueueGroup(RENDER_QUEUE_HOQ); |
---|
| 99 | this->attach(this->occlusionBillboard_); |
---|
| 100 | |
---|
[11078] | 101 | for (const LensFlare::Lens& lens : lensConfiguration_) |
---|
| 102 | { |
---|
| 103 | Billboard* lensPart = new Billboard(this->getContext()); |
---|
| 104 | lensPart->setMaterial(lens.material_); |
---|
[9500] | 105 | lensPart->disableFrustumCulling(); |
---|
| 106 | lensPart->setVisible(true); |
---|
| 107 | this->attach(lensPart); |
---|
[11079] | 108 | this->billboards_.push_back(lensPart); |
---|
[9500] | 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 | */ |
---|
[11078] | 122 | void LensFlare::updateBillboardStates(const Vector3& viewDirection, float dimension, bool lightIsVisible) |
---|
[9461] | 123 | { |
---|
[11078] | 124 | this->occlusionBillboard_->setDefaultDimensions(dimension * 0.5f, dimension * 0.5f); |
---|
[9500] | 125 | this->occlusionBillboard_->setVisible(lightIsVisible); |
---|
[11078] | 126 | |
---|
| 127 | int i = 0; |
---|
[11079] | 128 | for (Billboard* billboard : this->billboards_) |
---|
[11078] | 129 | { |
---|
| 130 | const LensFlare::Lens& lens = lensConfiguration_.at(i); |
---|
| 131 | billboard->setPosition(-viewDirection * (1.0f - lens.position_)); |
---|
[9461] | 132 | billboard->setVisible(lightIsVisible); |
---|
[11078] | 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 | { |
---|
[11078] | 146 | ColourValue cur = this->colour_; |
---|
| 147 | |
---|
| 148 | int i = 0; |
---|
[11079] | 149 | for (Billboard* billboard : this->billboards_) |
---|
[11078] | 150 | { |
---|
| 151 | cur.a = alpha * lensConfiguration_.at(i).alpha_; |
---|
| 152 | billboard->setColour(cur); |
---|
[9466] | 153 | i++; |
---|
[9461] | 154 | } |
---|
| 155 | } |
---|
[9445] | 156 | |
---|
[9461] | 157 | /** |
---|
| 158 | @brief |
---|
| 159 | 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 |
---|
| 160 | @param dimension |
---|
| 161 | the current dimension of the main billboard, we're always using square billboards |
---|
| 162 | @return |
---|
| 163 | the absolute amount of point samples that are currently captured by the camera of the view port |
---|
| 164 | */ |
---|
[9466] | 165 | unsigned int LensFlare::getPointCount(float dimension) const |
---|
[9445] | 166 | { |
---|
[11078] | 167 | Ogre::Camera* camera = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); |
---|
[9491] | 168 | Vector3 position = this->getWorldPosition(); |
---|
[9466] | 169 | Vector3 nX = camera->getDerivedOrientation().xAxis().normalisedCopy(); |
---|
| 170 | Vector3 nY = camera->getDerivedOrientation().yAxis().normalisedCopy(); |
---|
[11078] | 171 | int halfRes = fadeResolution_ / 2; |
---|
| 172 | float resDim = dimension / fadeResolution_; |
---|
| 173 | unsigned int count = 0; |
---|
| 174 | for (int i = -halfRes; i <= halfRes; i++) |
---|
[9461] | 175 | { |
---|
[11078] | 176 | for (int j = -halfRes; j <= halfRes; j++) |
---|
[9461] | 177 | { |
---|
[11078] | 178 | Vector3 point = position + (i*resDim) * nX + (j*resDim) * nY;//generate point samples |
---|
| 179 | if (camera->isVisible(point)) |
---|
[9461] | 180 | { |
---|
| 181 | count++; |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | return count; |
---|
[9445] | 186 | } |
---|
| 187 | |
---|
| 188 | void LensFlare::tick(float dt) |
---|
| 189 | { |
---|
| 190 | if(this->isVisible()) |
---|
| 191 | { |
---|
[11078] | 192 | // get the current distance of the lensflare center from the camera |
---|
| 193 | Ogre::Camera* camera = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); //get active Ogre Camera Instance, so we can check whether the light source is visible |
---|
| 194 | float cameraDistance = camera->getDerivedPosition().distance(this->getPosition()); |
---|
| 195 | float dimension = cameraDistance * this->scale_; |
---|
| 196 | if (!this->fadeOnViewBorder_) |
---|
[9461] | 197 | { |
---|
[11078] | 198 | this->fadeResolution_ = 3;//this is so we can still determine when the billboard has left the screen |
---|
[9461] | 199 | } |
---|
[11078] | 200 | unsigned int pointCount = this->getPointCount(dimension * 0.25f * this->scale_); |
---|
| 201 | Vector3 viewDirection = this->getWorldPosition() - camera->getDerivedPosition() - camera->getDerivedDirection() * cameraDistance; |
---|
| 202 | this->updateBillboardStates(viewDirection, dimension, pointCount > 0); |
---|
| 203 | if (pointCount > 0) |
---|
| 204 | { |
---|
| 205 | Ogre::Sphere sphere(this->getPosition(), dimension * 0.25f * this->scale_); |
---|
[9448] | 206 | float left, right, top, bottom; |
---|
[11078] | 207 | camera->projectSphere(sphere, &left, &top, &right, &bottom);//approximate maximum pixel count of billboard with a sphere |
---|
[9448] | 208 | |
---|
| 209 | Ogre::RenderWindow* window = GraphicsManager::getInstance().getRenderWindow(); |
---|
[11078] | 210 | float maxCount = (right - left) * (top - bottom) * window->getWidth() * window->getHeight() * 0.25f; |
---|
| 211 | unsigned int pixelCount = this->getScene()->getRenderQueueListener()->getPixelCount();//get pixel count |
---|
| 212 | float ratio = (maxCount < 0.0f) ? 0.0f : (1.0f * pixelCount / maxCount);//prevent underflow and division by zero |
---|
| 213 | float borderRatio = 1.0f; |
---|
| 214 | if (this->fadeOnViewBorder_) |
---|
[9461] | 215 | { |
---|
[11078] | 216 | borderRatio = ((float) pointCount) / (((float) fadeResolution_) * ((float) fadeResolution_));//ratio for the border fade |
---|
[9461] | 217 | } |
---|
| 218 | //update alpha values of all billboards except the HOQ billboard |
---|
[11078] | 219 | this->updateBillboardAlphas(std::min(1.0f, std::pow(std::min(ratio, borderRatio), this->fadeExponent_))); |
---|
[9448] | 220 | } |
---|
[9445] | 221 | } |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | void LensFlare::changedVisibility() |
---|
| 225 | { |
---|
[9461] | 226 | |
---|
[9445] | 227 | } |
---|
[11078] | 228 | } |
---|