Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/effects/fog_effect.cc @ 9370

Last change on this file since 9370 was 9235, checked in by bensch, 18 years ago

merged the presentation back

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
24// Define shell commands
25//SHELL_COMMAND(activate, FogEffect, activateFog);
26//SHELL_COMMAND(deactivate, FogEffect, deactivateFog);
27SHELL_COMMAND(fadein, FogEffect, fadeInFog);
28SHELL_COMMAND(fadeout, FogEffect, fadeOutFog);
29
30using namespace std;
31
32CREATE_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
39CREATE_FACTORY(FogEffect, CL_FOG_EFFECT);
40
41/**
42 * @brief standard constructor
43 */
44FogEffect::FogEffect(const TiXmlElement* root) {
45    this->setClassID(CL_FOG_EFFECT, "FogEffect");
46
47    // Initialize values
48    this->init();
49
50    // Load XML params
51    if (root != NULL)
52        this->loadParams(root);
53
54    // Activate fog, if chosen to be activated by default
55    if (this->fogActivate)
56        this->activate();
57}
58
59/**
60 * @brief standard destructor
61 */
62FogEffect::~FogEffect() {
63    this->deactivate();
64}
65
66/**
67 * @brief initalizes the fog effect with default values
68 */
69void 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);
76
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;
87 
88  this->cloudColor = Vector(0.2f, 0.3f, 0.3f);
89  this->skyColor = Vector(0.2f, 0.3f, 0.3f);
90}
91
92/**
93 * @brief loads the fog effect parameters.
94 * @param root: the XML-Element to load the data from
95 */
96void FogEffect::loadParams(const TiXmlElement* root) {
97    WeatherEffect::loadParams(root);
98
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   
108    LOAD_PARAM_START_CYCLE(root, element);
109    {
110      LoadParam_CYCLE(element, "option", this, FogEffect, setFogOption).describe("sets a fog option: activate");
111    }
112    LOAD_PARAM_END_CYCLE(element);
113}
114
115/**
116 * @brief activates the fog effect
117 */
118void FogEffect::activate() {
119    PRINTF(3)( "Activating FogEffect\n");
120
121    // init fogGL
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);
129
130    this->fogActivate = true;
131
132    glEnable(GL_FOG);
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);
141}
142
143/**
144 * @brief deactivates the fog effect
145 */
146void FogEffect::deactivate() {
147    PRINTF(3)("Deactivating FogEffect\n");
148
149    this->fogFadeInActivate = false;
150    this->fogFadeOutActivate = false;
151    this->fogActivate = false;
152
153    glDisable(GL_FOG);
154}
155
156/**
157 * @brief draws the fog effect
158 */
159void FogEffect::draw() const {
160
161    if (!this->fogActivate)
162        return;
163
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    }
171}
172
173/**
174 * @brief ticks the fog effect
175 * @param dt: tick float
176 */
177void FogEffect::tick(float dt) {
178
179    if (!this->fogActivate)
180        return;
181
182    // If fog is fading in
183    if ( this->fogFadeInActivate ) {
184        this->localTimer += dt;
185
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 )
192            this->fogFadeInActivate = false;
193    }
194
195    // If fog is fading out
196    if ( this->fogFadeOutActivate ) {
197        this->localTimer += dt;
198
199        if ( this->fogMode == GL_LINEAR)
200          this->fogFadeEnd = 2000 * ( this->localTimer / this->fogFadeOutDuration ) + this->fogEnd;
201        else
202            this->fogFadeDensity = 1 - (( this->localTimer / this->fogFadeOutDuration ) * this->fogDensity);
203
204        if ( this->localTimer >= this->fogFadeOutDuration )
205            this->deactivate();
206    }
207}
208
209/**
210 * @brief fades the fog in
211 */
212void FogEffect::fadeInFog() {
213
214    // If Fog is already fading out, stop it
215    this->fogFadeOutActivate = false;
216
217    // If Fog is already on, turn it off first
218    if (this->fogActivate)
219        this->deactivate();
220
221    // If no manual FadeIn value was set, set a default value
222    if (!this->fogFadeInDuration > 0)
223        this->fogFadeInDuration = 10;
224
225    // Reset local timer
226    this->localTimer = 0;
227
228    // Activate Fog
229    this->activate();
230
231    // set FogFadeIn activate
232    this->fogFadeInActivate = true;
233}
234
235/**
236 * @brief fades the fog out
237 */
238void FogEffect::fadeOutFog() {
239
240    // If Fog is already fading in, stop it
241    this->fogFadeInActivate = false;
242
243   
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)
250        this->fogFadeOutDuration = 10;
251
252    // set FogFadeOut activate
253    this->fogFadeOutActivate = true;
254
255    // Reset local timer
256    this->localTimer = 0;
257
258    // Restore the old cloud- and sky color
259    CloudEffect::changeCloudColor(this->oldCloudColor, this->fogFadeOutDuration);
260    CloudEffect::changeSkyColor(this->oldSkyColor, this->fogFadeOutDuration);
261}
262
Note: See TracBrowser for help on using the repository browser.