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