Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8474 was 8471, checked in by amaechler, 18 years ago

atmospheric: fog - weird fade problems, not solved

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