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