Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/atmospheric_engine/src/lib/graphics/effects/fog_effect.cc @ 8704

Last change on this file since 8704 was 8704, checked in by amaechler, 18 years ago
File size: 4.9 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
22SHELL_COMMAND(activate, FogEffect, activateFog);
23SHELL_COMMAND(deactivate, FogEffect, deactivateFog);
24SHELL_COMMAND(fadein, FogEffect, fadeInFog);
25SHELL_COMMAND(fadeout, FogEffect, fadeOutFog);
26
27// TODO: Fix fades
28
29using namespace std;
30
31CREATE_FACTORY(FogEffect, CL_FOG_EFFECT);
32
33FogEffect::FogEffect(const TiXmlElement* root) {
34    this->setClassID(CL_FOG_EFFECT, "FogEffect");
35
36    this->init();
37
38    if (root != NULL)
39        this->loadParams(root);
40
41    if (this->fogActivate)
42        this->activate();
43}
44
45
46FogEffect::~FogEffect() {
47    this->deactivate();
48}
49
50
51void FogEffect::loadParams(const TiXmlElement* root) {
52    WeatherEffect::loadParams(root);
53
54    LoadParam(root, "mode", this, FogEffect, setFogMode);
55    LoadParam(root, "density", this, FogEffect, setFogDensity);
56    LoadParam(root, "range", this, FogEffect, setFogRange);
57    LoadParam(root, "color", this, FogEffect, setFogColor);
58    LoadParam(root, "fadeinduration", this, FogEffect, setFogFadeIn);
59    LoadParam(root, "fadeoutduration", this, FogEffect, setFogFadeOut);
60
61    LOAD_PARAM_START_CYCLE(root, element);
62    {
63        LoadParam_CYCLE(element, "option", this, FogEffect, setFogOption);
64    }
65    LOAD_PARAM_END_CYCLE(element);
66}
67
68void FogEffect::init() {
69    // default values
70    this->fogMode = GL_LINEAR;
71    this->fogDensity = 0.005;
72    this->fogStart = 0;
73    this->fogEnd = 200;
74    this->colorVector = Vector(0.3, 0.3, 0.3);
75
76    // init variables
77    this->fogFadeInDuration = 0;
78    this->fogFadeOutDuration = 0;
79    this->fogFadeDensity = 0;
80    this->localTimer = 0;
81    this->fogActivate = false;
82    this->fogFadeInActivate = false;
83    this->fogFadeOutActivate = false;
84
85}
86
87
88void FogEffect::activate() {
89    PRINTF(0)( "Activating FogEffect\n");
90
91    // init fogGL
92    GLfloat fogColor[4] = { colorVector.x, colorVector.y, colorVector.z, 1.0};
93    glFogi(GL_FOG_MODE, this->fogMode);
94    glFogfv(GL_FOG_COLOR, fogColor);
95    glHint(GL_FOG_HINT, GL_DONT_CARE);
96    glFogf(GL_FOG_DENSITY, this->fogDensity);
97    glFogf(GL_FOG_START, this->fogStart);
98    glFogf(GL_FOG_END, this->fogEnd);
99
100    this->fogActivate = true;
101
102    glEnable(GL_FOG);
103
104}
105
106
107void FogEffect::deactivate() {
108    PRINTF(0)("Deactivating FogEffect\n");
109
110    this->fogFadeInActivate = false;
111    this->fogFadeOutActivate = false;
112    this->fogActivate = false;
113
114    glDisable(GL_FOG);
115
116}
117
118void FogEffect::draw() const {
119
120    if (!this->fogActivate)
121        return;
122
123    // If Fog Fade
124    if ( this->fogFadeInActivate || this->fogFadeOutActivate ) {
125      glDisable(GL_FOG);
126
127      GLfloat fogColor[4] = { colorVector.x, colorVector.y, colorVector.z, 1.0};
128      glFogi(GL_FOG_MODE, this->fogMode);
129      glFogfv(GL_FOG_COLOR, fogColor);
130      glHint(GL_FOG_HINT, GL_DONT_CARE);
131      glFogf(GL_FOG_DENSITY, this->fogFadeDensity);
132      glFogf(GL_FOG_START, this->fogStart);
133      glFogf(GL_FOG_END, this->fogEnd);
134
135      glEnable(GL_FOG);
136    }
137}
138
139void FogEffect::tick(float dt) {
140
141    if (!this->fogActivate)
142        return;
143
144    if ( this->fogFadeInActivate ) {
145        this->localTimer += dt;
146        this->fogFadeDensity = ( this->localTimer / this->fogFadeInDuration ) * this->fogDensity;
147
148        if ( this->localTimer >= this->fogFadeOutDuration )
149            this->fogFadeInActivate = false;
150    }
151
152    if ( this->fogFadeOutActivate ) {
153        this->localTimer += dt;
154        this->fogFadeDensity = 1 - (( this->localTimer / this->fogFadeInDuration ) * this->fogDensity);
155
156        if ( this->localTimer >= this->fogFadeOutDuration )
157            this->deactivate();
158    }
159}
160
161void FogEffect::fadeInFog() {
162
163    // If Fog is already fading out, stop it
164    this->fogFadeOutActivate = false;
165
166    // If Fog is already on, turn it off first
167    if (this->fogActivate)
168        this->deactivate();
169
170    // If no manual FadeIn value was set, set a default value
171    if (!this->fogFadeInDuration > 0)
172        this->fogFadeInDuration = 20;
173
174    // Reset local timer
175    this->localTimer = 0;
176
177    // set FogFadeIn activate
178    this->fogFadeInActivate = true;
179
180    // Activate Fog
181    this->activate();
182
183}
184
185
186void FogEffect::fadeOutFog() {
187
188    // If Fog is already fading in, stop it
189    this->fogFadeInActivate = false;
190
191    // If Fog is off, turn it on first
192    if (!this->fogActivate)
193        this->activate();
194
195    // If no manual FadeOut value was set, set a default value
196    if (!this->fogFadeOutDuration > 0)
197        this->fogFadeOutDuration = 20;
198
199    // set FogFadeOut activate
200    this->fogFadeOutActivate = true;
201
202    // Reset local timer
203    this->localTimer = 0;
204
205}
206
Note: See TracBrowser for help on using the repository browser.