Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/lib/graphics/effects/fog_effect.cc @ 9201

Last change on this file since 9201 was 9182, checked in by amaechler, 18 years ago

fixes

File size: 6.4 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
23// Define shell commands
24SHELL_COMMAND(activate, FogEffect, activateFog);
25SHELL_COMMAND(deactivate, FogEffect, deactivateFog);
26SHELL_COMMAND(fadein, FogEffect, fadeInFog);
27SHELL_COMMAND(fadeout, FogEffect, fadeOutFog);
28
29using namespace std;
30
31CREATE_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
38CREATE_FACTORY(FogEffect, CL_FOG_EFFECT);
39
40/**
41 * @brief standard constructor
42 */
43FogEffect::FogEffect(const TiXmlElement* root) {
44    this->setClassID(CL_FOG_EFFECT, "FogEffect");
45
46    // Initialize values
47    this->init();
48
49    // Load XML params
50    if (root != NULL)
51        this->loadParams(root);
52
53    // Activate fog, if chosen to be activated by default
54    if (this->fogActivate)
55        this->activate();
56}
57
58/**
59 * @brief standard destructor
60 */
61FogEffect::~FogEffect() {
62    this->deactivate();
63}
64
65/**
66 * @brief initalizes the fog effect with default values
67 */
68void 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);
75
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 */
92void FogEffect::loadParams(const TiXmlElement* root) {
93    WeatherEffect::loadParams(root);
94
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");;
101
102    LOAD_PARAM_START_CYCLE(root, element);
103    {
104      LoadParam_CYCLE(element, "option", this, FogEffect, setFogOption).describe("sets a fog option: activate");;
105    }
106    LOAD_PARAM_END_CYCLE(element);
107}
108
109/**
110 * @brief activates the fog effect
111 */
112void FogEffect::activate() {
113    PRINTF(3)( "Activating FogEffect\n");
114
115    // init fogGL
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);
123
124    this->fogActivate = true;
125
126    glEnable(GL_FOG);
127}
128
129/**
130 * @brief deactivates the fog effect
131 */
132void FogEffect::deactivate() {
133    PRINTF(3)("Deactivating FogEffect\n");
134
135    this->fogFadeInActivate = false;
136    this->fogFadeOutActivate = false;
137    this->fogActivate = false;
138
139    glDisable(GL_FOG);
140}
141
142/**
143 * @brief draws the fog effect
144 */
145void FogEffect::draw() const {
146
147    if (!this->fogActivate)
148        return;
149
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    }
157}
158
159/**
160 * @brief ticks the fog effect
161 * @param dt: tick float
162 */
163void FogEffect::tick(float dt) {
164
165    if (!this->fogActivate)
166        return;
167
168    // If fog is fading in
169    if ( this->fogFadeInActivate ) {
170        this->localTimer += dt;
171
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 )
178            this->fogFadeInActivate = false;
179    }
180
181    // If fog is fading out
182    if ( this->fogFadeOutActivate ) {
183        this->localTimer += dt;
184
185        if ( this->fogMode == GL_LINEAR)
186          this->fogFadeEnd = 2000 * ( this->localTimer / this->fogFadeOutDuration ) + this->fogEnd;
187        else
188            this->fogFadeDensity = 1 - (( this->localTimer / this->fogFadeOutDuration ) * this->fogDensity);
189
190        if ( this->localTimer >= this->fogFadeOutDuration )
191            this->deactivate();
192    }
193}
194
195/**
196 * @brief fades the fog in
197 */
198void FogEffect::fadeInFog() {
199
200    // If Fog is already fading out, stop it
201    this->fogFadeOutActivate = false;
202
203    // If Fog is already on, turn it off first
204    if (this->fogActivate)
205        this->deactivate();
206
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
217    // set FogFadeIn activate
218    this->fogFadeInActivate = true;
219}
220
221/**
222 * @brief fades the fog out
223 */
224void 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;
242}
243
Note: See TracBrowser for help on using the repository browser.