Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

branches/atmospheric_engine: better default values

File size: 3.6 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(startfogging, FogEffect, startFogging);
27
28using namespace std;
29
30CREATE_FACTORY(FogEffect, CL_FOG_EFFECT);
31
32FogEffect::FogEffect(const TiXmlElement* root)
33{
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{
48        this->deactivate();
49}
50
51
52void FogEffect::loadParams(const TiXmlElement* root)
53{
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
70bool FogEffect::init()
71{
72        //default values
73        this->fogActivate = false;
74        this->fogFadeInDuration = 0;
75        this->fogFadeOutDuration = 0;
76        this->localTimer = 0;
77       
78        this->fogMode = GL_LINEAR;
79        this->fogDensity = 0.05;
80        this->fogStart = 0;
81        this->fogEnd = 500;
82        this->colorVector = Vector(0.3, 0.3, 0.3);
83
84        return 0;
85}
86
87
88bool FogEffect::activate()
89{
90        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);
91
92        this->fogActivate = true;
93       
94        GLfloat fogColor[4] = { colorVector.x, colorVector.y, colorVector.z, 1.0};
95        glFogi(GL_FOG_MODE, this->fogMode);
96        glFogfv(GL_FOG_COLOR, fogColor);
97        glFogf(GL_FOG_DENSITY, this->fogDensity);
98        glHint(GL_FOG_HINT, GL_DONT_CARE);
99        glFogf(GL_FOG_START, this->fogStart);
100        glFogf(GL_FOG_END, this->fogEnd);
101       
102        glEnable(GL_FOG);
103        // glClearColor(0.5, 0.5, 0.5, 1.0);
104
105        return 0;
106}
107
108
109bool FogEffect::deactivate()
110{
111        PRINTF(0)("Deactivating FogEffect\n");
112
113        this->fogActivate = false;
114       
115        glDisable(GL_FOG);
116
117        return 0;
118}
119
120void FogEffect::draw() const {
121
122        if (this->fogFadeInDuration > 0 && this->localTimer < this->fogFadeInDuration)
123                glFogf(GL_FOG_DENSITY, this->fogFadeDensity);
124        //else
125        //      glFogf(GL_FOG_DENSITY, this->fogDensity);
126
127}
128void FogEffect::tick(float dt)
129{
130        if (!this->fogActivate)
131                return;
132               
133
134        if (this->fogFadeInDuration > 0 && this->localTimer < this->fogFadeInDuration) {
135                this->localTimer += dt;
136                float progress = this->localTimer / this->fogFadeInDuration;
137                this->fogFadeDensity = progress * this->fogDensity;
138        }
139}
140
141void FogEffect::startFogging() {
142
143        if (this->fogActivate)
144                this->deactivate();
145
146        if (!this->fogFadeInDuration > 0)
147                this->fogFadeInDuration = 20;
148
149        this->localTimer = 0;
150        this->activate();
151
152}
153
154
155GLint FogEffect::stringToFogMode(const std::string& mode)
156{
157        if(mode == "GL_LINEAR")
158                return GL_LINEAR;
159        else if(mode == "GL_EXP")
160                return GL_EXP;
161        else if(mode == "GL_EXP2" )
162                return GL_EXP2;
163        else
164                return -1;
165}
166
167
Note: See TracBrowser for help on using the repository browser.