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