Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8441 was 8434, checked in by amaechler, 19 years ago

atmospheric_engine: fade work

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