| [6741] | 1 | /* | 
|---|
| [8495] | 2 | orxonox - the future of 3D-vertical-scrollers | 
|---|
| [6741] | 3 |  | 
|---|
| [8495] | 4 | Copyright (C) 2004 orx | 
|---|
| [6741] | 5 |  | 
|---|
| [8495] | 6 | This program is free software; you can redistribute it and/or modify | 
|---|
|  | 7 | it under the terms of the GNU General Public License as published by | 
|---|
|  | 8 | the Free Software Foundation; either version 2, or (at your option) | 
|---|
|  | 9 | any later version. | 
|---|
| [6741] | 10 |  | 
|---|
|  | 11 | ### File Specific: | 
|---|
| [8495] | 12 | main-programmer: hdavid, amaechler | 
|---|
| [6741] | 13 | */ | 
|---|
|  | 14 |  | 
|---|
|  | 15 | #include "fog_effect.h" | 
|---|
|  | 16 |  | 
|---|
| [7193] | 17 | #include "util/loading/load_param.h" | 
|---|
|  | 18 | #include "util/loading/factory.h" | 
|---|
| [6741] | 19 |  | 
|---|
| [8255] | 20 | #include "shell_command.h" | 
|---|
| [9112] | 21 | #include "script_class.h" | 
|---|
| [9235] | 22 | #include "cloud_effect.h" | 
|---|
| [6772] | 23 |  | 
|---|
| [8793] | 24 | // Define shell commands | 
|---|
| [9235] | 25 | //SHELL_COMMAND(activate, FogEffect, activateFog); | 
|---|
|  | 26 | //SHELL_COMMAND(deactivate, FogEffect, deactivateFog); | 
|---|
| [8495] | 27 | SHELL_COMMAND(fadein, FogEffect, fadeInFog); | 
|---|
|  | 28 | SHELL_COMMAND(fadeout, FogEffect, fadeOutFog); | 
|---|
| [8255] | 29 |  | 
|---|
| [6741] | 30 |  | 
|---|
| [9406] | 31 |  | 
|---|
| [9112] | 32 | CREATE_SCRIPTABLE_CLASS(FogEffect, CL_FOG_EFFECT, | 
|---|
|  | 33 | addMethod("fadeIn", ExecutorLua0<FogEffect>(&FogEffect::fadeInFog)) | 
|---|
|  | 34 | ->addMethod("fadeOut", ExecutorLua0<FogEffect>(&FogEffect::fadeOutFog)) | 
|---|
|  | 35 | ->addMethod("activate", ExecutorLua0<FogEffect>(&FogEffect::activate)) | 
|---|
|  | 36 | ->addMethod("deactivate", ExecutorLua0<FogEffect>(&FogEffect::deactivate)) | 
|---|
|  | 37 | ); | 
|---|
|  | 38 |  | 
|---|
| [6772] | 39 | CREATE_FACTORY(FogEffect, CL_FOG_EFFECT); | 
|---|
| [6741] | 40 |  | 
|---|
| [8793] | 41 | /** | 
|---|
|  | 42 | * @brief standard constructor | 
|---|
|  | 43 | */ | 
|---|
| [8495] | 44 | FogEffect::FogEffect(const TiXmlElement* root) { | 
|---|
|  | 45 | this->setClassID(CL_FOG_EFFECT, "FogEffect"); | 
|---|
| [6741] | 46 |  | 
|---|
| [8793] | 47 | // Initialize values | 
|---|
| [8495] | 48 | this->init(); | 
|---|
| [6772] | 49 |  | 
|---|
| [8793] | 50 | // Load XML params | 
|---|
| [8495] | 51 | if (root != NULL) | 
|---|
|  | 52 | this->loadParams(root); | 
|---|
| [7107] | 53 |  | 
|---|
| [8793] | 54 | // Activate fog, if chosen to be activated by default | 
|---|
| [8495] | 55 | if (this->fogActivate) | 
|---|
|  | 56 | this->activate(); | 
|---|
| [6741] | 57 | } | 
|---|
|  | 58 |  | 
|---|
| [8793] | 59 | /** | 
|---|
|  | 60 | * @brief standard destructor | 
|---|
|  | 61 | */ | 
|---|
| [8495] | 62 | FogEffect::~FogEffect() { | 
|---|
|  | 63 | this->deactivate(); | 
|---|
| [6980] | 64 | } | 
|---|
| [6741] | 65 |  | 
|---|
| [8793] | 66 | /** | 
|---|
|  | 67 | * @brief initalizes the fog effect with default values | 
|---|
|  | 68 | */ | 
|---|
|  | 69 | void FogEffect::init() { | 
|---|
|  | 70 | // default values | 
|---|
|  | 71 | this->fogMode = GL_LINEAR; | 
|---|
|  | 72 | this->fogDensity = 0.005; | 
|---|
|  | 73 | this->fogStart = 0; | 
|---|
|  | 74 | this->fogEnd = 300; | 
|---|
|  | 75 | this->colorVector = Vector(0.6, 0.0, 0.0); | 
|---|
| [6741] | 76 |  | 
|---|
| [8793] | 77 | // init variables | 
|---|
|  | 78 | this->fogFadeInDuration = 0; | 
|---|
|  | 79 | this->fogFadeOutDuration = 0; | 
|---|
|  | 80 | this->fogFadeDensity = 0; | 
|---|
|  | 81 | this->fogFadeEnd = 0; | 
|---|
|  | 82 |  | 
|---|
|  | 83 | this->localTimer = 0; | 
|---|
|  | 84 | this->fogActivate = false; | 
|---|
|  | 85 | this->fogFadeInActivate = false; | 
|---|
|  | 86 | this->fogFadeOutActivate = false; | 
|---|
| [9235] | 87 |  | 
|---|
|  | 88 | this->cloudColor = Vector(0.2f, 0.3f, 0.3f); | 
|---|
|  | 89 | this->skyColor = Vector(0.2f, 0.3f, 0.3f); | 
|---|
| [8793] | 90 | } | 
|---|
|  | 91 |  | 
|---|
|  | 92 | /** | 
|---|
|  | 93 | * @brief loads the fog effect parameters. | 
|---|
|  | 94 | * @param root: the XML-Element to load the data from | 
|---|
|  | 95 | */ | 
|---|
| [8495] | 96 | void FogEffect::loadParams(const TiXmlElement* root) { | 
|---|
|  | 97 | WeatherEffect::loadParams(root); | 
|---|
| [6741] | 98 |  | 
|---|
| [9235] | 99 | LoadParam(root, "mode", this, FogEffect, setFogMode).describe("fog mode (linear, exponential)"); | 
|---|
|  | 100 | LoadParam(root, "density", this, FogEffect, setFogDensity).describe("fog density if exp. fog"); | 
|---|
|  | 101 | LoadParam(root, "range", this, FogEffect, setFogRange).describe("fog range: start, end"); | 
|---|
|  | 102 | LoadParam(root, "color", this, FogEffect, setFogColor).describe("fog color: r,g,b"); | 
|---|
|  | 103 | LoadParam(root, "fadeinduration", this, FogEffect, setFogFadeIn).describe("duration of the fade in"); | 
|---|
|  | 104 | LoadParam(root, "fadeoutduration", this, FogEffect, setFogFadeOut).describe("duration of the fade out"); | 
|---|
|  | 105 | LoadParam(root, "cloudcolor", this, FogEffect, setCloudColor); | 
|---|
|  | 106 | LoadParam(root, "skycolor", this, FogEffect, setSkyColor); | 
|---|
|  | 107 |  | 
|---|
| [8495] | 108 | LOAD_PARAM_START_CYCLE(root, element); | 
|---|
|  | 109 | { | 
|---|
| [9235] | 110 | LoadParam_CYCLE(element, "option", this, FogEffect, setFogOption).describe("sets a fog option: activate"); | 
|---|
| [8495] | 111 | } | 
|---|
|  | 112 | LOAD_PARAM_END_CYCLE(element); | 
|---|
| [7810] | 113 | } | 
|---|
| [6772] | 114 |  | 
|---|
| [8793] | 115 | /** | 
|---|
|  | 116 | * @brief activates the fog effect | 
|---|
|  | 117 | */ | 
|---|
| [8495] | 118 | void FogEffect::activate() { | 
|---|
| [9006] | 119 | PRINTF(3)( "Activating FogEffect\n"); | 
|---|
| [6772] | 120 |  | 
|---|
| [8793] | 121 | // init fogGL | 
|---|
| [8495] | 122 | GLfloat fogColor[4] = { colorVector.x, colorVector.y, colorVector.z, 1.0}; | 
|---|
|  | 123 | glFogi(GL_FOG_MODE, this->fogMode); | 
|---|
|  | 124 | glFogfv(GL_FOG_COLOR, fogColor); | 
|---|
|  | 125 | glHint(GL_FOG_HINT, GL_DONT_CARE); | 
|---|
|  | 126 | glFogf(GL_FOG_DENSITY, this->fogDensity); | 
|---|
|  | 127 | glFogf(GL_FOG_START, this->fogStart); | 
|---|
|  | 128 | glFogf(GL_FOG_END, this->fogEnd); | 
|---|
| [8316] | 129 |  | 
|---|
| [8793] | 130 | this->fogActivate = true; | 
|---|
|  | 131 |  | 
|---|
| [8495] | 132 | glEnable(GL_FOG); | 
|---|
| [9235] | 133 |  | 
|---|
|  | 134 | // Store cloud- and sky color before the snow | 
|---|
|  | 135 | this->oldCloudColor = CloudEffect::cloudColor; | 
|---|
|  | 136 | this->oldSkyColor   = CloudEffect::skyColor; | 
|---|
|  | 137 |  | 
|---|
|  | 138 | // Change the colors | 
|---|
|  | 139 | CloudEffect::changeCloudColor(this->cloudColor, this->fogFadeInDuration); | 
|---|
|  | 140 | CloudEffect::changeSkyColor(this->skyColor, this->fogFadeInDuration); | 
|---|
| [6752] | 141 | } | 
|---|
|  | 142 |  | 
|---|
| [8793] | 143 | /** | 
|---|
|  | 144 | * @brief deactivates the fog effect | 
|---|
|  | 145 | */ | 
|---|
| [8495] | 146 | void FogEffect::deactivate() { | 
|---|
| [9006] | 147 | PRINTF(3)("Deactivating FogEffect\n"); | 
|---|
| [8255] | 148 |  | 
|---|
| [8495] | 149 | this->fogFadeInActivate = false; | 
|---|
|  | 150 | this->fogFadeOutActivate = false; | 
|---|
|  | 151 | this->fogActivate = false; | 
|---|
| [8316] | 152 |  | 
|---|
| [8495] | 153 | glDisable(GL_FOG); | 
|---|
| [6772] | 154 | } | 
|---|
|  | 155 |  | 
|---|
| [8793] | 156 | /** | 
|---|
|  | 157 | * @brief draws the fog effect | 
|---|
|  | 158 | */ | 
|---|
| [8255] | 159 | void FogEffect::draw() const { | 
|---|
| [6772] | 160 |  | 
|---|
| [8495] | 161 | if (!this->fogActivate) | 
|---|
|  | 162 | return; | 
|---|
| [8255] | 163 |  | 
|---|
| [8793] | 164 | // If fog is fading | 
|---|
|  | 165 | if ( this->fogFadeInActivate || this->fogFadeOutActivate ) { | 
|---|
|  | 166 | if ( this->fogMode == GL_LINEAR) | 
|---|
|  | 167 | glFogf(GL_FOG_END, this->fogFadeEnd); | 
|---|
|  | 168 | else | 
|---|
|  | 169 | glFogf(GL_FOG_DENSITY, this->fogFadeDensity); | 
|---|
|  | 170 | } | 
|---|
| [8255] | 171 | } | 
|---|
|  | 172 |  | 
|---|
| [8793] | 173 | /** | 
|---|
|  | 174 | * @brief ticks the fog effect | 
|---|
|  | 175 | * @param dt: tick float | 
|---|
|  | 176 | */ | 
|---|
| [8495] | 177 | void FogEffect::tick(float dt) { | 
|---|
| [8793] | 178 |  | 
|---|
| [8495] | 179 | if (!this->fogActivate) | 
|---|
|  | 180 | return; | 
|---|
| [8316] | 181 |  | 
|---|
| [8793] | 182 | // If fog is fading in | 
|---|
| [8495] | 183 | if ( this->fogFadeInActivate ) { | 
|---|
|  | 184 | this->localTimer += dt; | 
|---|
|  | 185 |  | 
|---|
| [8793] | 186 | if ( this->fogMode == GL_LINEAR) | 
|---|
|  | 187 | this->fogFadeEnd = 2000 * (1 - ( this->localTimer / this->fogFadeInDuration )) + this->fogEnd; | 
|---|
|  | 188 | else | 
|---|
|  | 189 | this->fogFadeDensity = ( this->localTimer / this->fogFadeInDuration ) * this->fogDensity; | 
|---|
|  | 190 |  | 
|---|
|  | 191 | if ( this->localTimer >= this->fogFadeInDuration ) | 
|---|
| [8495] | 192 | this->fogFadeInActivate = false; | 
|---|
|  | 193 | } | 
|---|
|  | 194 |  | 
|---|
| [8793] | 195 | // If fog is fading out | 
|---|
| [8495] | 196 | if ( this->fogFadeOutActivate ) { | 
|---|
|  | 197 | this->localTimer += dt; | 
|---|
|  | 198 |  | 
|---|
| [8793] | 199 | if ( this->fogMode == GL_LINEAR) | 
|---|
| [9235] | 200 | this->fogFadeEnd = 2000 * ( this->localTimer / this->fogFadeOutDuration ) + this->fogEnd; | 
|---|
| [8793] | 201 | else | 
|---|
| [9235] | 202 | this->fogFadeDensity = 1 - (( this->localTimer / this->fogFadeOutDuration ) * this->fogDensity); | 
|---|
| [8793] | 203 |  | 
|---|
| [8495] | 204 | if ( this->localTimer >= this->fogFadeOutDuration ) | 
|---|
|  | 205 | this->deactivate(); | 
|---|
|  | 206 | } | 
|---|
| [8255] | 207 | } | 
|---|
|  | 208 |  | 
|---|
| [8793] | 209 | /** | 
|---|
|  | 210 | * @brief fades the fog in | 
|---|
|  | 211 | */ | 
|---|
| [8495] | 212 | void FogEffect::fadeInFog() { | 
|---|
| [8255] | 213 |  | 
|---|
| [8495] | 214 | // If Fog is already fading out, stop it | 
|---|
|  | 215 | this->fogFadeOutActivate = false; | 
|---|
| [8255] | 216 |  | 
|---|
| [8495] | 217 | // If Fog is already on, turn it off first | 
|---|
|  | 218 | if (this->fogActivate) | 
|---|
|  | 219 | this->deactivate(); | 
|---|
| [8255] | 220 |  | 
|---|
| [8495] | 221 | // If no manual FadeIn value was set, set a default value | 
|---|
|  | 222 | if (!this->fogFadeInDuration > 0) | 
|---|
| [9235] | 223 | this->fogFadeInDuration = 10; | 
|---|
| [8495] | 224 |  | 
|---|
|  | 225 | // Reset local timer | 
|---|
|  | 226 | this->localTimer = 0; | 
|---|
|  | 227 |  | 
|---|
|  | 228 | // Activate Fog | 
|---|
|  | 229 | this->activate(); | 
|---|
|  | 230 |  | 
|---|
| [8793] | 231 | // set FogFadeIn activate | 
|---|
|  | 232 | this->fogFadeInActivate = true; | 
|---|
| [8255] | 233 | } | 
|---|
|  | 234 |  | 
|---|
| [8793] | 235 | /** | 
|---|
|  | 236 | * @brief fades the fog out | 
|---|
|  | 237 | */ | 
|---|
| [8495] | 238 | void FogEffect::fadeOutFog() { | 
|---|
|  | 239 |  | 
|---|
|  | 240 | // If Fog is already fading in, stop it | 
|---|
|  | 241 | this->fogFadeInActivate = false; | 
|---|
|  | 242 |  | 
|---|
| [9235] | 243 |  | 
|---|
| [8495] | 244 | // If Fog is off, turn it on first | 
|---|
|  | 245 | if (!this->fogActivate) | 
|---|
|  | 246 | this->activate(); | 
|---|
|  | 247 |  | 
|---|
|  | 248 | // If no manual FadeOut value was set, set a default value | 
|---|
|  | 249 | if (!this->fogFadeOutDuration > 0) | 
|---|
| [9235] | 250 | this->fogFadeOutDuration = 10; | 
|---|
| [8495] | 251 |  | 
|---|
|  | 252 | // set FogFadeOut activate | 
|---|
|  | 253 | this->fogFadeOutActivate = true; | 
|---|
|  | 254 |  | 
|---|
|  | 255 | // Reset local timer | 
|---|
|  | 256 | this->localTimer = 0; | 
|---|
| [9235] | 257 |  | 
|---|
|  | 258 | // Restore the old cloud- and sky color | 
|---|
|  | 259 | CloudEffect::changeCloudColor(this->oldCloudColor, this->fogFadeOutDuration); | 
|---|
|  | 260 | CloudEffect::changeSkyColor(this->oldSkyColor, this->fogFadeOutDuration); | 
|---|
| [6772] | 261 | } | 
|---|
|  | 262 |  | 
|---|