| [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 |  | 
|---|
 | 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 |  | 
|---|
| [6976] | 35 |  | 
|---|
| [6776] | 36 | using namespace std; | 
|---|
 | 37 |  | 
|---|
| [6976] | 38 |  | 
|---|
| [6776] | 39 | CREATE_FACTORY(LenseFlare, CL_LENSE_FLARE); | 
|---|
 | 40 |  | 
|---|
 | 41 |  | 
|---|
 | 42 | /** | 
|---|
 | 43 |  *  default constructor | 
|---|
 | 44 |  * @param root The XML-element to load the LenseFlare from | 
|---|
 | 45 |  */ | 
|---|
 | 46 |  LenseFlare::LenseFlare(const TiXmlElement* root) | 
|---|
 | 47 | { | 
|---|
| [6980] | 48 |   this->setClassID(CL_LENSE_FLARE, "LenseFlare"); | 
|---|
| [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); | 
|---|
| [6980] | 61 |   PRINTF(0)("light is: %p\n", this->lightSource); | 
|---|
 | 62 |  | 
|---|
| [7015] | 63 |   this->isVisible = true; | 
|---|
 | 64 |  | 
|---|
| [6980] | 65 |   if (root != NULL) | 
|---|
 | 66 |   { | 
|---|
 | 67 |     this->loadParams(root); | 
|---|
 | 68 |     this->activate(); | 
|---|
 | 69 |   } | 
|---|
| [7015] | 70 |  | 
|---|
 | 71 |   this->setSourceVisibility(false); | 
|---|
 | 72 |   this->isVisible = true; | 
|---|
 | 73 |  | 
|---|
| [6776] | 74 | } | 
|---|
 | 75 |  | 
|---|
 | 76 |  | 
|---|
 | 77 | /** | 
|---|
 | 78 |  *  destroys a LenseFlare | 
|---|
 | 79 |  */ | 
|---|
 | 80 | LenseFlare::~LenseFlare() | 
|---|
| [6783] | 81 | { | 
|---|
 | 82 |   std::vector<Billboard*>::iterator it; | 
|---|
 | 83 |   for( it = flares.begin(); it != flares.end(); it++) | 
|---|
 | 84 |     delete (*it); | 
|---|
 | 85 | } | 
|---|
| [6776] | 86 |  | 
|---|
 | 87 |  | 
|---|
 | 88 | /** | 
|---|
 | 89 |  * @param root The XML-element to load the LenseFlare from | 
|---|
 | 90 |  */ | 
|---|
 | 91 | void LenseFlare::loadParams(const TiXmlElement* root) | 
|---|
 | 92 | { | 
|---|
 | 93 |   GraphicsEffect::loadParams(root); | 
|---|
 | 94 |  | 
|---|
| [6980] | 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"); | 
|---|
| [6980] | 99 |   } | 
|---|
 | 100 |   LOAD_PARAM_END_CYCLE(element); | 
|---|
| [6776] | 101 | } | 
|---|
 | 102 |  | 
|---|
| [7009] | 103 |  | 
|---|
| [6776] | 104 | /** | 
|---|
 | 105 |  * initializes the fog effect | 
|---|
 | 106 |  */ | 
|---|
 | 107 | bool LenseFlare::init() | 
|---|
| [6782] | 108 | {} | 
|---|
| [6776] | 109 |  | 
|---|
 | 110 |  | 
|---|
 | 111 | /** | 
|---|
 | 112 |  * activates the fog effect | 
|---|
 | 113 |  */ | 
|---|
 | 114 | bool LenseFlare::activate() | 
|---|
 | 115 | { | 
|---|
| [6782] | 116 |   this->bActivated = true; | 
|---|
| [6776] | 117 | } | 
|---|
 | 118 |  | 
|---|
 | 119 |  | 
|---|
 | 120 | /** | 
|---|
 | 121 |  * deactivates the fog effect | 
|---|
 | 122 |  */ | 
|---|
 | 123 | bool LenseFlare::deactivate() | 
|---|
 | 124 | { | 
|---|
| [6782] | 125 |   this->bActivated = false; | 
|---|
| [6776] | 126 | } | 
|---|
 | 127 |  | 
|---|
 | 128 |  | 
|---|
 | 129 | /** | 
|---|
 | 130 |  * converts a gl mode char to a GLint | 
|---|
 | 131 |  * @param mode the mode character | 
|---|
 | 132 |  */ | 
|---|
| [7221] | 133 | GLint LenseFlare::stringToFogMode(const std::string& mode) | 
|---|
| [6776] | 134 | {} | 
|---|
 | 135 |  | 
|---|
| [6779] | 136 |  | 
|---|
 | 137 | /** | 
|---|
 | 138 |  * adds a texture flare | 
|---|
 | 139 |  * @param textureName the name of the flare texture | 
|---|
| [6783] | 140 |  * | 
|---|
| [6785] | 141 |  *  1st: Texture of the Sun/Light source itself | 
|---|
 | 142 |  *  2nd: Texture of the fist halo | 
|---|
 | 143 |  *  3rd: Texture of small burst | 
|---|
 | 144 |  *  4th: Texture of the second halo | 
|---|
 | 145 |  *  5th: Texutre of the second burst | 
|---|
 | 146 |  *  6th: Texture of the third halo | 
|---|
 | 147 |  *  7th: Texture of the third burst | 
|---|
| [6779] | 148 |  */ | 
|---|
| [7221] | 149 | void LenseFlare::addFlare(const std::string& textureName) | 
|---|
| [6779] | 150 | { | 
|---|
| [6884] | 151 |   if( this->flares.size() > LF_MAX_FLARES) | 
|---|
| [6787] | 152 |   { | 
|---|
 | 153 |     PRINTF(2)("You tried to add more than %i lense flares, ignoring\n", LF_MAX_FLARES); | 
|---|
 | 154 |     return; | 
|---|
 | 155 |   } | 
|---|
 | 156 |  | 
|---|
| [6782] | 157 |   Billboard* bb = new Billboard(NULL); | 
|---|
 | 158 |   bb->setTexture(textureName); | 
|---|
| [6885] | 159 |   bb->setSize(50, 50); | 
|---|
| [6783] | 160 |   this->flares.push_back(bb); | 
|---|
| [7221] | 161 |   PRINTF(4)("Added a Lenseflare Billboard with texture %s\n", textureName.c_str()); | 
|---|
| [6785] | 162 |  | 
|---|
 | 163 |   // the first flare belongs to the light source | 
|---|
 | 164 |   if( this->flares.size() == 1 && this->lightSource != NULL) | 
|---|
 | 165 |   { | 
|---|
| [6786] | 166 |     bb->setBindNode(static_cast<PNode*>(this->lightSource)); | 
|---|
| [6889] | 167 |     bb->setVisibility(true); | 
|---|
| [6785] | 168 |   } | 
|---|
| [7221] | 169 |   PRINTF(4)("Finished adding\n"); | 
|---|
| [6782] | 170 | } | 
|---|
| [6779] | 171 |  | 
|---|
 | 172 |  | 
|---|
| [7015] | 173 | void LenseFlare::setSourceVisibility(bool visibility) | 
|---|
| [6886] | 174 | { | 
|---|
| [7015] | 175 |   if (this->isVisible == visibility) | 
|---|
 | 176 |     return; | 
|---|
| [6887] | 177 |  | 
|---|
| [6889] | 178 |   float dist = this->frustumPlane.distancePoint(this->lightSource->getAbsCoor()); | 
|---|
| [7015] | 179 |   PRINTF(0)("dist: %f\n", dist); | 
|---|
 | 180 |   std::vector<Billboard*>::const_iterator it; | 
|---|
 | 181 |     for(it = flares.begin(); it != flares.end(); it++) | 
|---|
 | 182 |       (*it)->setVisibility(visibility); | 
|---|
 | 183 |   this->isVisible = visibility; | 
|---|
| [6886] | 184 | } | 
|---|
| [6785] | 185 |  | 
|---|
| [6885] | 186 |  | 
|---|
| [6782] | 187 | /** | 
|---|
| [6785] | 188 |  * tick the effect | 
|---|
 | 189 |  */ | 
|---|
 | 190 | void LenseFlare::tick(float dt) | 
|---|
 | 191 | { | 
|---|
| [6787] | 192 |   if( unlikely(!this->bActivated || this->flares.size() == 0)) | 
|---|
 | 193 |     return; | 
|---|
| [6884] | 194 |  | 
|---|
| [6885] | 195 |   // refetch light source information if needed | 
|---|
| [6884] | 196 |   if( unlikely( this->lightSource == NULL)) | 
|---|
| [6885] | 197 |   { | 
|---|
| [6884] | 198 |     this->lightSource = (LightManager::getInstance())->getLight(0); | 
|---|
| [6885] | 199 |     if( this->flares.size() > 0) | 
|---|
 | 200 |       this->flares[0]->setBindNode(static_cast<PNode*>(this->lightSource)); | 
|---|
 | 201 |   } | 
|---|
| [6884] | 202 |  | 
|---|
| [6889] | 203 |     //set the frustum plane | 
|---|
| [6885] | 204 |  | 
|---|
| [7015] | 205 |   this->frustumPlane = State::getCamera()->getViewFrustum(); | 
|---|
| [6889] | 206 |  | 
|---|
| [7015] | 207 |   if (State::getCamera()->distance(lightSource) < 0) | 
|---|
 | 208 |     this->setSourceVisibility(false); | 
|---|
 | 209 |   else | 
|---|
 | 210 |     this->setSourceVisibility(true); | 
|---|
 | 211 |  | 
|---|
 | 212 |  | 
|---|
| [6787] | 213 |   // always update the screen center, it could be, that the window is resized | 
|---|
| [7316] | 214 |   this->screenCenter = Vector2D(State::getResX()/2.0f, State::getResY()/2.0f); | 
|---|
| [7015] | 215 |  | 
|---|
| [6787] | 216 |   // flare vector is the direction from the center to the light source | 
|---|
| [6888] | 217 |   this->flareVector = this->flares[0]->getAbsCoor2D() - this->screenCenter; | 
|---|
| [6787] | 218 |   this->distance = this->flareVector.len(); | 
|---|
 | 219 |   this->flareVector.normalize(); | 
|---|
| [6785] | 220 |  | 
|---|
| [6787] | 221 |   // now calculate the new coordinates of the billboards | 
|---|
 | 222 |   std::vector<Billboard*>::iterator it; | 
|---|
 | 223 |   int i; | 
|---|
 | 224 |   for( it = flares.begin(), i = 0; it != flares.end(); it++, i++) | 
|---|
 | 225 |   { | 
|---|
 | 226 |     // set the new position | 
|---|
| [6885] | 227 |     if( i == 0) | 
|---|
 | 228 |       continue; | 
|---|
| [6889] | 229 |  | 
|---|
| [6885] | 230 |     (*it)->setAbsCoor2D( this->screenCenter + this->flareVector * this->flareMatrix[i * 2] * this->distance); | 
|---|
| [6888] | 231 |     (*it)->setSize2D(50.0f * this->flareMatrix[i * 2 + 1], 50.0f * this->flareMatrix[i * 2 + 1]); | 
|---|
| [6887] | 232 |     PRINTF(5)("Tick flare %i @ (%f, %f)\n", i, (*it)->getAbsCoor2D().x, (*it)->getAbsCoor2D().y); | 
|---|
| [6787] | 233 |     // tick them | 
|---|
 | 234 |     (*it)->tick(dt); | 
|---|
 | 235 |   } | 
|---|
| [6785] | 236 | } | 
|---|
 | 237 |  | 
|---|
 | 238 |  | 
|---|
 | 239 | /** | 
|---|
| [6782] | 240 |  * draws the LenseFlares | 
|---|
 | 241 |  */ | 
|---|
 | 242 | void LenseFlare::draw() const | 
|---|
 | 243 | { | 
|---|
 | 244 |   if( !this->bActivated) | 
|---|
 | 245 |     return; | 
|---|
| [6779] | 246 | } | 
|---|