[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 | |
---|
[7193] | 21 | #include "util/loading/load_param.h" |
---|
| 22 | #include "util/loading/factory.h" |
---|
[6776] | 23 | |
---|
[8495] | 24 | #include "glincl.h" |
---|
[6779] | 25 | #include "texture.h" |
---|
[6776] | 26 | |
---|
[6786] | 27 | #include "light.h" |
---|
[6787] | 28 | #include "state.h" |
---|
[6786] | 29 | |
---|
[7810] | 30 | #include "render2D/image_plane.h" |
---|
[6776] | 31 | |
---|
[6884] | 32 | #include "light.h" |
---|
[6889] | 33 | #include "camera.h" |
---|
[6884] | 34 | |
---|
[6976] | 35 | |
---|
[6776] | 36 | |
---|
[6976] | 37 | |
---|
[9406] | 38 | |
---|
[6776] | 39 | CREATE_FACTORY(LenseFlare, CL_LENSE_FLARE); |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | /** |
---|
[7843] | 43 | * @brief default constructor |
---|
[6776] | 44 | * @param root The XML-element to load the LenseFlare from |
---|
| 45 | */ |
---|
[8495] | 46 | LenseFlare::LenseFlare(const TiXmlElement* root) { |
---|
| 47 | this->setClassID(CL_LENSE_FLARE, "LenseFlare"); |
---|
[6787] | 48 | |
---|
[8495] | 49 | /* length image scale */ |
---|
| 50 | this->flareMatrix[0] = 1.0f; |
---|
| 51 | this->flareMatrix[1] = 1.0f; |
---|
| 52 | this->flareMatrix[2] = 0.5f; |
---|
| 53 | this->flareMatrix[3] = 0.5f; |
---|
| 54 | this->flareMatrix[4] = 0.33f; |
---|
| 55 | this->flareMatrix[5] = 0.25f; |
---|
| 56 | this->flareMatrix[6] = 0.125f; |
---|
| 57 | this->flareMatrix[7] = 1.0f; |
---|
| 58 | this->flareMatrix[8] = -0.5f; |
---|
| 59 | this->flareMatrix[9] = 0.5f; |
---|
| 60 | this->flareMatrix[10] = -0.25f; |
---|
| 61 | this->flareMatrix[11] = 0.15f; |
---|
| 62 | this->flareMatrix[12] = -1.82f; |
---|
| 63 | this->flareMatrix[13] = 0.25f; |
---|
[6884] | 64 | |
---|
[8495] | 65 | this->lightSource = (LightManager::getInstance())->getLight(0); |
---|
[9006] | 66 | PRINTF(4)("light is: %p\n", this->lightSource); |
---|
[6980] | 67 | |
---|
[8495] | 68 | if (root != NULL) { |
---|
| 69 | this->loadParams(root); |
---|
| 70 | this->activate(); |
---|
| 71 | } |
---|
[7015] | 72 | |
---|
[8495] | 73 | this->bVisible = true; |
---|
| 74 | this->setSourceVisibility(false); |
---|
[7015] | 75 | |
---|
[6776] | 76 | } |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | /** |
---|
| 80 | * destroys a LenseFlare |
---|
| 81 | */ |
---|
[8495] | 82 | LenseFlare::~LenseFlare() { |
---|
| 83 | std::vector<ImagePlane*>::iterator it; |
---|
| 84 | for( it = flares.begin(); it != flares.end(); it++) |
---|
| 85 | delete (*it); |
---|
[6783] | 86 | } |
---|
[6776] | 87 | |
---|
| 88 | |
---|
| 89 | /** |
---|
| 90 | * @param root The XML-element to load the LenseFlare from |
---|
| 91 | */ |
---|
[8495] | 92 | void LenseFlare::loadParams(const TiXmlElement* root) { |
---|
| 93 | GraphicsEffect::loadParams(root); |
---|
[6776] | 94 | |
---|
[8495] | 95 | LOAD_PARAM_START_CYCLE(root, element); |
---|
| 96 | { |
---|
| 97 | LoadParam_CYCLE(element, "add-flare-texture", this, LenseFlare, addFlare) |
---|
[6779] | 98 | .describe("adds a lensflare texture to the engine"); |
---|
[8495] | 99 | } |
---|
| 100 | LOAD_PARAM_END_CYCLE(element); |
---|
[6776] | 101 | } |
---|
| 102 | |
---|
[7009] | 103 | |
---|
[6776] | 104 | /** |
---|
| 105 | * initializes the fog effect |
---|
| 106 | */ |
---|
[8495] | 107 | void LenseFlare::init() {} |
---|
[6776] | 108 | |
---|
| 109 | |
---|
| 110 | /** |
---|
| 111 | * activates the fog effect |
---|
| 112 | */ |
---|
[8495] | 113 | void LenseFlare::activate() { |
---|
| 114 | this->bActivated = true; |
---|
[6776] | 115 | } |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | * deactivates the fog effect |
---|
| 120 | */ |
---|
[8495] | 121 | void LenseFlare::deactivate() { |
---|
| 122 | this->bActivated = false; |
---|
[6776] | 123 | } |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | /** |
---|
[7843] | 127 | * @brief converts a gl mode char to a GLint |
---|
[6776] | 128 | * @param mode the mode character |
---|
| 129 | */ |
---|
[8495] | 130 | GLint LenseFlare::stringToFogMode(const std::string& mode) { |
---|
| 131 | return 0; |
---|
[8316] | 132 | } |
---|
[6776] | 133 | |
---|
[6779] | 134 | |
---|
| 135 | /** |
---|
[7843] | 136 | * @brief adds a texture flare |
---|
[6779] | 137 | * @param textureName the name of the flare texture |
---|
[6783] | 138 | * |
---|
[6785] | 139 | * 1st: Texture of the Sun/Light source itself |
---|
| 140 | * 2nd: Texture of the fist halo |
---|
| 141 | * 3rd: Texture of small burst |
---|
| 142 | * 4th: Texture of the second halo |
---|
| 143 | * 5th: Texutre of the second burst |
---|
| 144 | * 6th: Texture of the third halo |
---|
| 145 | * 7th: Texture of the third burst |
---|
[6779] | 146 | */ |
---|
[8495] | 147 | void LenseFlare::addFlare(const std::string& textureName) { |
---|
| 148 | if( this->flares.size() > LF_MAX_FLARES) { |
---|
| 149 | PRINTF(2)("You tried to add more than %i lense flares, ignoring\n", LF_MAX_FLARES); |
---|
| 150 | return; |
---|
| 151 | } |
---|
[6787] | 152 | |
---|
[8495] | 153 | ImagePlane* bb = new ImagePlane(NULL); |
---|
| 154 | if (this->flares.empty()) |
---|
| 155 | bb->setLayer(E2D_LAYER_BELOW_ALL); |
---|
| 156 | bb->setTexture(textureName); |
---|
| 157 | bb->setSize(50, 50); |
---|
| 158 | this->flares.push_back(bb); |
---|
| 159 | bb->setVisibility(true); |
---|
[7844] | 160 | |
---|
[8495] | 161 | PRINTF(4)("Added a Lenseflare ImagePlane with texture %s\n", textureName.c_str()); |
---|
[6785] | 162 | |
---|
[8495] | 163 | // the first flare belongs to the light source |
---|
| 164 | if( this->flares.size() == 1 && this->lightSource != NULL) { |
---|
| 165 | bb->setBindNode(static_cast<PNode*>(this->lightSource)); |
---|
| 166 | } |
---|
[7843] | 167 | |
---|
[8495] | 168 | PRINTF(4)("Finished adding\n"); |
---|
[6782] | 169 | } |
---|
[6779] | 170 | |
---|
| 171 | |
---|
[8495] | 172 | void LenseFlare::setSourceVisibility(bool visibility) { |
---|
| 173 | if (this->bVisible == visibility) |
---|
| 174 | return; |
---|
[6887] | 175 | |
---|
[8495] | 176 | std::vector<ImagePlane*>::const_iterator it; |
---|
[7844] | 177 | for(it = ++flares.begin(); it != flares.end(); it++) |
---|
[8495] | 178 | (*it)->setVisibility(visibility); |
---|
| 179 | this->bVisible = visibility; |
---|
[6886] | 180 | } |
---|
[6785] | 181 | |
---|
[6885] | 182 | |
---|
[6782] | 183 | /** |
---|
[6785] | 184 | * tick the effect |
---|
| 185 | */ |
---|
[8495] | 186 | void LenseFlare::tick(float dt) { |
---|
| 187 | if( unlikely(!this->bActivated || this->flares.size() == 0)) |
---|
| 188 | return; |
---|
[6884] | 189 | |
---|
[8495] | 190 | // refetch light source information if needed |
---|
| 191 | if( unlikely( this->lightSource == NULL)) { |
---|
| 192 | this->lightSource = (LightManager::getInstance())->getLight(0); |
---|
| 193 | if( this->flares.size() > 0) |
---|
| 194 | this->flares[0]->setBindNode(static_cast<PNode*>(this->lightSource)); |
---|
| 195 | } |
---|
[6884] | 196 | |
---|
[6889] | 197 | //set the frustum plane |
---|
[8495] | 198 | if (!flares.empty()) |
---|
| 199 | this->setSourceVisibility(this->flares[0]->isVisible()); |
---|
[6885] | 200 | |
---|
[6889] | 201 | |
---|
[8495] | 202 | // always update the screen center, it could be, that the window is resized |
---|
| 203 | this->screenCenter = Vector2D(State::getResX()/2.0f, State::getResY()/2.0f); |
---|
[7015] | 204 | |
---|
[8495] | 205 | // flare vector is the direction from the center to the light source |
---|
| 206 | this->flareVector = this->flares[0]->getAbsCoor2D() - this->screenCenter; |
---|
| 207 | this->distance = this->flareVector.len(); |
---|
| 208 | this->flareVector.normalize(); |
---|
[6785] | 209 | |
---|
[8495] | 210 | // now calculate the new coordinates of the billboards |
---|
| 211 | std::vector<ImagePlane*>::iterator it; |
---|
| 212 | int i; |
---|
| 213 | for( it = flares.begin(), i = 0; it != flares.end(); it++, i++) { |
---|
| 214 | // set the new position |
---|
| 215 | if( i == 0) |
---|
| 216 | continue; |
---|
[6889] | 217 | |
---|
[8495] | 218 | (*it)->setAbsCoor2D( this->screenCenter + this->flareVector * this->flareMatrix[i * 2] * this->distance); |
---|
| 219 | (*it)->setSize2D(50.0f * this->flareMatrix[i * 2 + 1], 50.0f * this->flareMatrix[i * 2 + 1]); |
---|
| 220 | PRINTF(5)("Tick flare %i @ (%f, %f)\n", i, (*it)->getAbsCoor2D().x, (*it)->getAbsCoor2D().y); |
---|
| 221 | } |
---|
[6785] | 222 | } |
---|
| 223 | |
---|
| 224 | |
---|
| 225 | /** |
---|
[6782] | 226 | * draws the LenseFlares |
---|
| 227 | */ |
---|
[8495] | 228 | void LenseFlare::draw() const { |
---|
| 229 | if( !this->bActivated) |
---|
| 230 | return; |
---|
[6779] | 231 | } |
---|