Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/effects/fog_effect.cc @ 8347

Last change on this file since 8347 was 8316, checked in by bensch, 18 years ago

trunk: fixed most -Wall warnings… but there are still many missing :/

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