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 | // Define shell commands |
---|
23 | SHELL_COMMAND(activate, FogEffect, activateFog); |
---|
24 | SHELL_COMMAND(deactivate, FogEffect, deactivateFog); |
---|
25 | SHELL_COMMAND(fadein, FogEffect, fadeInFog); |
---|
26 | SHELL_COMMAND(fadeout, FogEffect, fadeOutFog); |
---|
27 | |
---|
28 | using namespace std; |
---|
29 | |
---|
30 | CREATE_FACTORY(FogEffect, CL_FOG_EFFECT); |
---|
31 | |
---|
32 | /** |
---|
33 | * @brief standard constructor |
---|
34 | */ |
---|
35 | FogEffect::FogEffect(const TiXmlElement* root) { |
---|
36 | this->setClassID(CL_FOG_EFFECT, "FogEffect"); |
---|
37 | |
---|
38 | // Initialize values |
---|
39 | this->init(); |
---|
40 | |
---|
41 | // Load XML params |
---|
42 | if (root != NULL) |
---|
43 | this->loadParams(root); |
---|
44 | |
---|
45 | // Activate fog, if chosen to be activated by default |
---|
46 | if (this->fogActivate) |
---|
47 | this->activate(); |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | * @brief standard destructor |
---|
52 | */ |
---|
53 | FogEffect::~FogEffect() { |
---|
54 | this->deactivate(); |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * @brief initalizes the fog effect with default values |
---|
59 | */ |
---|
60 | void FogEffect::init() { |
---|
61 | // default values |
---|
62 | this->fogMode = GL_LINEAR; |
---|
63 | this->fogDensity = 0.005; |
---|
64 | this->fogStart = 0; |
---|
65 | this->fogEnd = 300; |
---|
66 | this->colorVector = Vector(0.6, 0.0, 0.0); |
---|
67 | |
---|
68 | // init variables |
---|
69 | this->fogFadeInDuration = 0; |
---|
70 | this->fogFadeOutDuration = 0; |
---|
71 | this->fogFadeDensity = 0; |
---|
72 | this->fogFadeEnd = 0; |
---|
73 | |
---|
74 | this->localTimer = 0; |
---|
75 | this->fogActivate = false; |
---|
76 | this->fogFadeInActivate = false; |
---|
77 | this->fogFadeOutActivate = false; |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * @brief loads the fog effect parameters. |
---|
82 | * @param root: the XML-Element to load the data from |
---|
83 | */ |
---|
84 | void FogEffect::loadParams(const TiXmlElement* root) { |
---|
85 | WeatherEffect::loadParams(root); |
---|
86 | |
---|
87 | LoadParam(root, "mode", this, FogEffect, setFogMode).describe("fog mode (linear, exponential)");; |
---|
88 | LoadParam(root, "density", this, FogEffect, setFogDensity).describe("fog density if exp. fog");; |
---|
89 | LoadParam(root, "range", this, FogEffect, setFogRange).describe("fog range: start, end");; |
---|
90 | LoadParam(root, "color", this, FogEffect, setFogColor).describe("fog color: r,g,b");; |
---|
91 | LoadParam(root, "fadeinduration", this, FogEffect, setFogFadeIn).describe("duration of the fade in");; |
---|
92 | LoadParam(root, "fadeoutduration", this, FogEffect, setFogFadeOut).describe("duration of the fade out");; |
---|
93 | |
---|
94 | LOAD_PARAM_START_CYCLE(root, element); |
---|
95 | { |
---|
96 | LoadParam_CYCLE(element, "option", this, FogEffect, setFogOption).describe("sets a fog option: activate");; |
---|
97 | } |
---|
98 | LOAD_PARAM_END_CYCLE(element); |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | * @brief activates the fog effect |
---|
103 | */ |
---|
104 | void FogEffect::activate() { |
---|
105 | PRINTF(3)( "Activating FogEffect\n"); |
---|
106 | |
---|
107 | // init fogGL |
---|
108 | GLfloat fogColor[4] = { colorVector.x, colorVector.y, colorVector.z, 1.0}; |
---|
109 | glFogi(GL_FOG_MODE, this->fogMode); |
---|
110 | glFogfv(GL_FOG_COLOR, fogColor); |
---|
111 | glHint(GL_FOG_HINT, GL_DONT_CARE); |
---|
112 | glFogf(GL_FOG_DENSITY, this->fogDensity); |
---|
113 | glFogf(GL_FOG_START, this->fogStart); |
---|
114 | glFogf(GL_FOG_END, this->fogEnd); |
---|
115 | |
---|
116 | this->fogActivate = true; |
---|
117 | |
---|
118 | glEnable(GL_FOG); |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * @brief deactivates the fog effect |
---|
123 | */ |
---|
124 | void FogEffect::deactivate() { |
---|
125 | PRINTF(3)("Deactivating FogEffect\n"); |
---|
126 | |
---|
127 | this->fogFadeInActivate = false; |
---|
128 | this->fogFadeOutActivate = false; |
---|
129 | this->fogActivate = false; |
---|
130 | |
---|
131 | glDisable(GL_FOG); |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * @brief draws the fog effect |
---|
136 | */ |
---|
137 | void FogEffect::draw() const { |
---|
138 | |
---|
139 | if (!this->fogActivate) |
---|
140 | return; |
---|
141 | |
---|
142 | // If fog is fading |
---|
143 | if ( this->fogFadeInActivate || this->fogFadeOutActivate ) { |
---|
144 | if ( this->fogMode == GL_LINEAR) |
---|
145 | glFogf(GL_FOG_END, this->fogFadeEnd); |
---|
146 | else |
---|
147 | glFogf(GL_FOG_DENSITY, this->fogFadeDensity); |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * @brief ticks the fog effect |
---|
153 | * @param dt: tick float |
---|
154 | */ |
---|
155 | void FogEffect::tick(float dt) { |
---|
156 | |
---|
157 | if (!this->fogActivate) |
---|
158 | return; |
---|
159 | |
---|
160 | // If fog is fading in |
---|
161 | if ( this->fogFadeInActivate ) { |
---|
162 | this->localTimer += dt; |
---|
163 | |
---|
164 | if ( this->fogMode == GL_LINEAR) |
---|
165 | this->fogFadeEnd = 2000 * (1 - ( this->localTimer / this->fogFadeInDuration )) + this->fogEnd; |
---|
166 | else |
---|
167 | this->fogFadeDensity = ( this->localTimer / this->fogFadeInDuration ) * this->fogDensity; |
---|
168 | |
---|
169 | if ( this->localTimer >= this->fogFadeInDuration ) |
---|
170 | this->fogFadeInActivate = false; |
---|
171 | } |
---|
172 | |
---|
173 | // If fog is fading out |
---|
174 | if ( this->fogFadeOutActivate ) { |
---|
175 | this->localTimer += dt; |
---|
176 | |
---|
177 | if ( this->fogMode == GL_LINEAR) |
---|
178 | this->fogFadeEnd = 2000 * ( this->localTimer / this->fogFadeInDuration ) + this->fogEnd; |
---|
179 | else |
---|
180 | this->fogFadeDensity = 1 - (( this->localTimer / this->fogFadeInDuration ) * this->fogDensity); |
---|
181 | |
---|
182 | if ( this->localTimer >= this->fogFadeOutDuration ) |
---|
183 | this->deactivate(); |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | * @brief fades the fog in |
---|
189 | */ |
---|
190 | void FogEffect::fadeInFog() { |
---|
191 | |
---|
192 | // If Fog is already fading out, stop it |
---|
193 | this->fogFadeOutActivate = false; |
---|
194 | |
---|
195 | // If Fog is already on, turn it off first |
---|
196 | if (this->fogActivate) |
---|
197 | this->deactivate(); |
---|
198 | |
---|
199 | // If no manual FadeIn value was set, set a default value |
---|
200 | if (!this->fogFadeInDuration > 0) |
---|
201 | this->fogFadeInDuration = 20; |
---|
202 | |
---|
203 | // Reset local timer |
---|
204 | this->localTimer = 0; |
---|
205 | |
---|
206 | // Activate Fog |
---|
207 | this->activate(); |
---|
208 | |
---|
209 | // set FogFadeIn activate |
---|
210 | this->fogFadeInActivate = true; |
---|
211 | } |
---|
212 | |
---|
213 | /** |
---|
214 | * @brief fades the fog out |
---|
215 | */ |
---|
216 | void FogEffect::fadeOutFog() { |
---|
217 | |
---|
218 | // If Fog is already fading in, stop it |
---|
219 | this->fogFadeInActivate = false; |
---|
220 | |
---|
221 | // If Fog is off, turn it on first |
---|
222 | if (!this->fogActivate) |
---|
223 | this->activate(); |
---|
224 | |
---|
225 | // If no manual FadeOut value was set, set a default value |
---|
226 | if (!this->fogFadeOutDuration > 0) |
---|
227 | this->fogFadeOutDuration = 20; |
---|
228 | |
---|
229 | // set FogFadeOut activate |
---|
230 | this->fogFadeOutActivate = true; |
---|
231 | |
---|
232 | // Reset local timer |
---|
233 | this->localTimer = 0; |
---|
234 | } |
---|
235 | |
---|