[6776] | 1 | |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | orxonox - the future of 3D-vertical-scrollers |
---|
| 5 | |
---|
| 6 | Copyright (C) 2004 orx |
---|
| 7 | |
---|
| 8 | This program is free software; you can redistribute it and/or modify |
---|
| 9 | it under the terms of the GNU General Public License as published by |
---|
| 10 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 11 | any later version. |
---|
| 12 | |
---|
| 13 | ### File Specific: |
---|
| 14 | main-programmer: Patrick Boenzli |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS |
---|
| 18 | |
---|
| 19 | #include "lense_flare.h" |
---|
| 20 | |
---|
| 21 | #include "load_param.h" |
---|
| 22 | #include "factory.h" |
---|
| 23 | |
---|
| 24 | #include "glincl.h" |
---|
[6779] | 25 | #include "texture.h" |
---|
[6776] | 26 | |
---|
[6786] | 27 | #include "light.h" |
---|
[6787] | 28 | #include "state.h" |
---|
[6786] | 29 | |
---|
[6782] | 30 | #include "render2D/billboard.h" |
---|
[6776] | 31 | |
---|
[6884] | 32 | #include "light.h" |
---|
[6889] | 33 | #include "camera.h" |
---|
[6884] | 34 | |
---|
[6776] | 35 | using namespace std; |
---|
| 36 | |
---|
| 37 | CREATE_FACTORY(LenseFlare, CL_LENSE_FLARE); |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * default constructor |
---|
| 43 | * @param root The XML-element to load the LenseFlare from |
---|
| 44 | */ |
---|
| 45 | LenseFlare::LenseFlare(const TiXmlElement* root) |
---|
| 46 | { |
---|
| 47 | if (root != NULL) |
---|
| 48 | this->loadParams(root); |
---|
[6787] | 49 | |
---|
| 50 | this->flareMatrix = new float[14]; |
---|
| 51 | /* length image scale */ |
---|
| 52 | this->flareMatrix[0] = 1.0f; this->flareMatrix[1] = 1.0f; |
---|
| 53 | this->flareMatrix[2] = 0.5f; this->flareMatrix[3] = 0.5f; |
---|
| 54 | this->flareMatrix[4] = 0.33f; this->flareMatrix[5] = 0.25f; |
---|
| 55 | this->flareMatrix[6] = 0.125f; this->flareMatrix[7] = 1.0f; |
---|
| 56 | this->flareMatrix[8] = -0.5f; this->flareMatrix[9] = 0.5f; |
---|
[6888] | 57 | this->flareMatrix[10] = -0.25f; this->flareMatrix[11] = 0.15f; |
---|
| 58 | this->flareMatrix[12] = -1.82f; this->flareMatrix[13] = 0.25f; |
---|
[6884] | 59 | |
---|
| 60 | this->lightSource = (LightManager::getInstance())->getLight(0); |
---|
[6776] | 61 | } |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | /** |
---|
| 65 | * destroys a LenseFlare |
---|
| 66 | */ |
---|
| 67 | LenseFlare::~LenseFlare() |
---|
[6783] | 68 | { |
---|
| 69 | std::vector<Billboard*>::iterator it; |
---|
| 70 | for( it = flares.begin(); it != flares.end(); it++) |
---|
| 71 | delete (*it); |
---|
| 72 | } |
---|
[6776] | 73 | |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * @param root The XML-element to load the LenseFlare from |
---|
| 77 | */ |
---|
| 78 | void LenseFlare::loadParams(const TiXmlElement* root) |
---|
| 79 | { |
---|
| 80 | GraphicsEffect::loadParams(root); |
---|
| 81 | |
---|
[6779] | 82 | LoadParam(root, "add-flare-texture", this, LenseFlare, addFlare) |
---|
| 83 | .describe("adds a lensflare texture to the engine"); |
---|
[6776] | 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * initializes the fog effect |
---|
| 89 | */ |
---|
| 90 | bool LenseFlare::init() |
---|
[6782] | 91 | {} |
---|
[6776] | 92 | |
---|
| 93 | |
---|
| 94 | /** |
---|
| 95 | * activates the fog effect |
---|
| 96 | */ |
---|
| 97 | bool LenseFlare::activate() |
---|
| 98 | { |
---|
[6782] | 99 | this->bActivated = true; |
---|
[6776] | 100 | } |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | * deactivates the fog effect |
---|
| 105 | */ |
---|
| 106 | bool LenseFlare::deactivate() |
---|
| 107 | { |
---|
[6782] | 108 | this->bActivated = false; |
---|
[6776] | 109 | } |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | * converts a gl mode char to a GLint |
---|
| 114 | * @param mode the mode character |
---|
| 115 | */ |
---|
| 116 | GLint LenseFlare::charToFogMode(const char* mode) |
---|
| 117 | {} |
---|
| 118 | |
---|
[6779] | 119 | |
---|
| 120 | /** |
---|
| 121 | * adds a texture flare |
---|
| 122 | * @param textureName the name of the flare texture |
---|
[6783] | 123 | * |
---|
[6785] | 124 | * 1st: Texture of the Sun/Light source itself |
---|
| 125 | * 2nd: Texture of the fist halo |
---|
| 126 | * 3rd: Texture of small burst |
---|
| 127 | * 4th: Texture of the second halo |
---|
| 128 | * 5th: Texutre of the second burst |
---|
| 129 | * 6th: Texture of the third halo |
---|
| 130 | * 7th: Texture of the third burst |
---|
[6779] | 131 | */ |
---|
[6782] | 132 | void LenseFlare::addFlare(const char* textureName) |
---|
[6779] | 133 | { |
---|
[6884] | 134 | if( this->flares.size() > LF_MAX_FLARES) |
---|
[6787] | 135 | { |
---|
| 136 | PRINTF(2)("You tried to add more than %i lense flares, ignoring\n", LF_MAX_FLARES); |
---|
| 137 | return; |
---|
| 138 | } |
---|
| 139 | |
---|
[6782] | 140 | Billboard* bb = new Billboard(NULL); |
---|
| 141 | bb->setTexture(textureName); |
---|
[6885] | 142 | bb->setSize(50, 50); |
---|
[6783] | 143 | this->flares.push_back(bb); |
---|
[6787] | 144 | PRINTF(0)("Added a Lenseflare Billboard with texture %s\n", textureName); |
---|
[6785] | 145 | |
---|
| 146 | // the first flare belongs to the light source |
---|
| 147 | if( this->flares.size() == 1 && this->lightSource != NULL) |
---|
| 148 | { |
---|
[6786] | 149 | bb->setBindNode(static_cast<PNode*>(this->lightSource)); |
---|
[6889] | 150 | bb->setVisibility(true); |
---|
[6785] | 151 | } |
---|
[6782] | 152 | } |
---|
[6779] | 153 | |
---|
| 154 | |
---|
[6885] | 155 | bool LenseFlare::sourceVisible() const |
---|
[6886] | 156 | { |
---|
[6887] | 157 | |
---|
[6889] | 158 | float dist = this->frustumPlane.distancePoint(this->lightSource->getAbsCoor()); |
---|
| 159 | if( dist < 0.0f) |
---|
| 160 | { |
---|
| 161 | std::vector<Billboard*>::const_iterator it = flares.begin(); it++; |
---|
| 162 | for(; it != flares.end(); it++) |
---|
| 163 | (*it)->setVisibility(true); |
---|
| 164 | return true; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | std::vector<Billboard*>::const_iterator it = flares.begin(); it++; |
---|
| 168 | for(; it != flares.end(); it++) |
---|
| 169 | (*it)->setVisibility(false); |
---|
| 170 | |
---|
| 171 | return false; |
---|
[6886] | 172 | } |
---|
[6785] | 173 | |
---|
[6885] | 174 | |
---|
[6782] | 175 | /** |
---|
[6785] | 176 | * tick the effect |
---|
| 177 | */ |
---|
| 178 | void LenseFlare::tick(float dt) |
---|
| 179 | { |
---|
[6787] | 180 | if( unlikely(!this->bActivated || this->flares.size() == 0)) |
---|
| 181 | return; |
---|
[6884] | 182 | |
---|
[6885] | 183 | // refetch light source information if needed |
---|
[6884] | 184 | if( unlikely( this->lightSource == NULL)) |
---|
[6885] | 185 | { |
---|
[6884] | 186 | this->lightSource = (LightManager::getInstance())->getLight(0); |
---|
[6885] | 187 | if( this->flares.size() > 0) |
---|
| 188 | this->flares[0]->setBindNode(static_cast<PNode*>(this->lightSource)); |
---|
| 189 | } |
---|
[6884] | 190 | |
---|
[6889] | 191 | //set the frustum plane |
---|
| 192 | Vector psTarget = (State::getCameraTarget())->getAbsCoor(); |
---|
| 193 | Vector psCamera = (State::getCamera())->getAbsCoor(); |
---|
| 194 | Vector psCameraDir = psCamera - psTarget; |
---|
| 195 | //PRINTF(0)("camera Dir: %f %f %f, camera: %f %f %f\n", psCameraDir.x, psCameraDir.y, psCameraDir.z, psCamera.x, psCamera.y, psCamera.z); |
---|
| 196 | this->frustumPlane = Plane(psCameraDir, Vector(-70.0,0.0,0.0)); |
---|
[6885] | 197 | |
---|
[6889] | 198 | |
---|
[6787] | 199 | // always update the screen center, it could be, that the window is resized |
---|
| 200 | this->screenCenter = Vector(State::getResX()/2.0f, State::getResY()/2.0f, 0.0f); |
---|
| 201 | // flare vector is the direction from the center to the light source |
---|
[6888] | 202 | this->flareVector = this->flares[0]->getAbsCoor2D() - this->screenCenter; |
---|
| 203 | this->flareVector.z = 0.0f; |
---|
[6787] | 204 | this->distance = this->flareVector.len(); |
---|
| 205 | this->flareVector.normalize(); |
---|
[6785] | 206 | |
---|
[6787] | 207 | // now calculate the new coordinates of the billboards |
---|
| 208 | std::vector<Billboard*>::iterator it; |
---|
| 209 | int i; |
---|
| 210 | for( it = flares.begin(), i = 0; it != flares.end(); it++, i++) |
---|
| 211 | { |
---|
| 212 | // set the new position |
---|
[6885] | 213 | if( i == 0) |
---|
| 214 | continue; |
---|
[6889] | 215 | |
---|
[6885] | 216 | (*it)->setAbsCoor2D( this->screenCenter + this->flareVector * this->flareMatrix[i * 2] * this->distance); |
---|
[6888] | 217 | (*it)->setSize2D(50.0f * this->flareMatrix[i * 2 + 1], 50.0f * this->flareMatrix[i * 2 + 1]); |
---|
[6887] | 218 | PRINTF(5)("Tick flare %i @ (%f, %f)\n", i, (*it)->getAbsCoor2D().x, (*it)->getAbsCoor2D().y); |
---|
[6787] | 219 | // tick them |
---|
| 220 | (*it)->tick(dt); |
---|
| 221 | } |
---|
[6785] | 222 | } |
---|
| 223 | |
---|
| 224 | |
---|
| 225 | /** |
---|
[6782] | 226 | * draws the LenseFlares |
---|
| 227 | */ |
---|
| 228 | void LenseFlare::draw() const |
---|
| 229 | { |
---|
| 230 | if( !this->bActivated) |
---|
| 231 | return; |
---|
| 232 | |
---|
[6889] | 233 | this->sourceVisible(); |
---|
[6779] | 234 | } |
---|