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