Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/weather_effects/fog_effect.cc @ 10594

Last change on this file since 10594 was 10114, checked in by patrick, 18 years ago

merged network back to trunk

File size: 7.2 KB
Line 
1/*
2  orxonox - the future of 3D-vertical-scrollers
3
4  Copyright (C) 2004 orx
5
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.
10
11### File Specific:
12  main-programmer: hdavid, amaechler
13*/
14
15#include "fog_effect.h"
16
17#include "util/loading/load_param.h"
18#include "util/loading/factory.h"
19
20#include "shell_command.h"
21#include "script_class.h"
22#include "cloud_effect.h"
23#include "debug.h"
24
25ObjectListDefinition(FogEffect);
26// Define shell commands
27//SHELL_COMMAND(activate, FogEffect, activateFog);
28//SHELL_COMMAND(deactivate, FogEffect, deactivateFog);
29SHELL_COMMAND(fadein, FogEffect, fadeInFog);
30SHELL_COMMAND(fadeout, FogEffect, fadeOutFog);
31
32
33
34CREATE_SCRIPTABLE_CLASS(FogEffect,
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))
39                       );
40
41CREATE_FACTORY(FogEffect);
42
43/**
44 * @brief standard constructor
45 */
46FogEffect::FogEffect(const TiXmlElement* root) {
47  this->registerObject(this, FogEffect::_objectList);
48    // Initialize values
49    this->init();
50
51    // Load XML params
52    if (root != NULL)
53        this->loadParams(root);
54
55    // Activate fog, if chosen to be activated by default
56    if (this->fogActivate)
57        this->activate();
58}
59
60/**
61 * @brief standard destructor
62 */
63FogEffect::~FogEffect() {
64    this->deactivate();
65}
66
67/**
68 * @brief initalizes the fog effect with default values
69 */
70void 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);
77
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;
88
89  this->cloudColor = Vector(0.2f, 0.3f, 0.3f);
90  this->skyColor = Vector(0.2f, 0.3f, 0.3f);
91}
92
93/**
94 * @brief loads the fog effect parameters.
95 * @param root: the XML-Element to load the data from
96 */
97void FogEffect::loadParams(const TiXmlElement* root) {
98    WeatherEffect::loadParams(root);
99
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);
108
109    LOAD_PARAM_START_CYCLE(root, element);
110    {
111      LoadParam_CYCLE(element, "option", this, FogEffect, setFogOption).describe("sets a fog option: activate");
112    }
113    LOAD_PARAM_END_CYCLE(element);
114}
115
116/**
117 * @brief activates the fog effect
118 */
119void FogEffect::activate() {
120    PRINTF(3)( "Activating FogEffect\n");
121
122    // init fogGL
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);
130
131    this->fogActivate = true;
132
133    glEnable(GL_FOG);
134
135    // Store cloud- and sky color before the snow
136    this->oldCloudColor = CloudEffect::cloudColor;
137    this->oldSkyColor   = CloudEffect::skyColor;
138
139    // Change the colors
140    CloudEffect::changeCloudColor(this->cloudColor, this->fogFadeInDuration);
141    CloudEffect::changeSkyColor(this->skyColor, this->fogFadeInDuration);
142}
143
144/**
145 * @brief deactivates the fog effect
146 */
147void FogEffect::deactivate() {
148    PRINTF(3)("Deactivating FogEffect\n");
149
150    this->fogFadeInActivate = false;
151    this->fogFadeOutActivate = false;
152    this->fogActivate = false;
153
154    glDisable(GL_FOG);
155}
156
157/**
158 * @brief draws the fog effect
159 */
160void FogEffect::draw() const {
161
162    if (!this->fogActivate)
163        return;
164
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    }
172}
173
174/**
175 * @brief ticks the fog effect
176 * @param dt: tick float
177 */
178void FogEffect::tick(float dt) {
179
180    if (!this->fogActivate)
181        return;
182
183    // If fog is fading in
184    if ( this->fogFadeInActivate ) {
185        this->localTimer += dt;
186
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 )
193            this->fogFadeInActivate = false;
194    }
195
196    // If fog is fading out
197    if ( this->fogFadeOutActivate ) {
198        this->localTimer += dt;
199
200        if ( this->fogMode == GL_LINEAR)
201          this->fogFadeEnd = 2000 * ( this->localTimer / this->fogFadeOutDuration ) + this->fogEnd;
202        else
203            this->fogFadeDensity = 1 - (( this->localTimer / this->fogFadeOutDuration ) * this->fogDensity);
204
205        if ( this->localTimer >= this->fogFadeOutDuration )
206            this->deactivate();
207    }
208}
209
210/**
211 * @brief fades the fog in
212 */
213void FogEffect::fadeInFog() {
214
215    // If Fog is already fading out, stop it
216    this->fogFadeOutActivate = false;
217
218    // If Fog is already on, turn it off first
219    if (this->fogActivate)
220        this->deactivate();
221
222    // If no manual FadeIn value was set, set a default value
223    if (!this->fogFadeInDuration > 0)
224        this->fogFadeInDuration = 10;
225
226    // Reset local timer
227    this->localTimer = 0;
228
229    // Activate Fog
230    this->activate();
231
232    // set FogFadeIn activate
233    this->fogFadeInActivate = true;
234}
235
236/**
237 * @brief fades the fog out
238 */
239void FogEffect::fadeOutFog() {
240
241    // If Fog is already fading in, stop it
242    this->fogFadeInActivate = false;
243
244
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)
251        this->fogFadeOutDuration = 10;
252
253    // set FogFadeOut activate
254    this->fogFadeOutActivate = true;
255
256    // Reset local timer
257    this->localTimer = 0;
258
259    // Restore the old cloud- and sky color
260    CloudEffect::changeCloudColor(this->oldCloudColor, this->fogFadeOutDuration);
261    CloudEffect::changeSkyColor(this->oldSkyColor, this->fogFadeOutDuration);
262}
263
Note: See TracBrowser for help on using the repository browser.