Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8429 was 8373, checked in by amaechler, 19 years ago

branches/atmospheric_engine

File size: 4.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{
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 (this->fogFadeInDuration > 0 && this->localTimer < this->fogFadeInDuration)
126                glFogf(GL_FOG_DENSITY, this->fogFadeDensity);
127        //else
128        //      glFogf(GL_FOG_DENSITY, this->fogDensity);
129
130}
131void FogEffect::tick(float dt)
132{
133        if (!this->fogActivate)
134                return;
135               
136        if (this->fogFadeInDuration > 0 && this->localTimer < this->fogFadeInDuration) {
137                this->localTimer += dt;
138                float progress = this->localTimer / this->fogFadeInDuration;
139                this->fogFadeDensity = progress * this->fogDensity;
140        }
141        else if (this->fogFadeOutDuration > 0 && this->localTimer < this->fogFadeOutDuration) {
142                this->localTimer += dt;
143                float progress = this->localTimer / this->fogFadeInDuration;
144                this->fogFadeDensity = 1 - progress * this->fogDensity;
145        }
146}
147
148void FogEffect::fadeInFog() {
149
150        if (this->fogActivate)
151                this->deactivate();
152
153        if (!this->fogFadeInDuration > 0)
154                this->fogFadeInDuration = 20;
155
156        this->localTimer = 0;
157        this->activate();
158
159}
160
161
162void FogEffect::fadeOutFog() {
163
164        if (this->fogActivate)
165                this->deactivate();
166
167        if (!this->fogFadeOutDuration > 0)
168                this->fogFadeOutDuration = 20;
169
170        this->localTimer = 0;
171        this->activate();
172}
173
174
175GLint FogEffect::stringToFogMode(const std::string& mode)
176{
177        if(mode == "GL_LINEAR")
178                return GL_LINEAR;
179        else if(mode == "GL_EXP")
180                return GL_EXP;
181        else if(mode == "GL_EXP2" )
182                return GL_EXP2;
183        else
184                return -1;
185}
186
187
Note: See TracBrowser for help on using the repository browser.