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