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