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 | |
---|
24 | SHELL_COMMAND(activate, FogEffect, activateFog); |
---|
25 | SHELL_COMMAND(deactivate, FogEffect, deactivateFog); |
---|
26 | SHELL_COMMAND(fadein, FogEffect, fadeInFog); |
---|
27 | SHELL_COMMAND(fadeout, FogEffect, fadeOutFog); |
---|
28 | |
---|
29 | // TODO: Fix fades |
---|
30 | |
---|
31 | using namespace std; |
---|
32 | |
---|
33 | CREATE_FACTORY(FogEffect, CL_FOG_EFFECT); |
---|
34 | |
---|
35 | FogEffect::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 | |
---|
49 | FogEffect::~FogEffect() |
---|
50 | { |
---|
51 | this->deactivate(); |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | void 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 | |
---|
73 | bool 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 | |
---|
91 | bool 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 | glHint(GL_FOG_HINT, GL_DONT_CARE); |
---|
101 | glFogf(GL_FOG_START, this->fogStart); |
---|
102 | glFogf(GL_FOG_END, this->fogEnd); |
---|
103 | |
---|
104 | // glFogf(GL_FOG_DENSITY, this->fogDensity); |
---|
105 | |
---|
106 | glEnable(GL_FOG); |
---|
107 | |
---|
108 | return 0; |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | bool FogEffect::deactivate() |
---|
113 | { |
---|
114 | PRINTF(0)("Deactivating FogEffect\n"); |
---|
115 | |
---|
116 | this->fogFadeInActivate = false; |
---|
117 | this->fogFadeOutActivate = false; |
---|
118 | this->fogActivate = false; |
---|
119 | |
---|
120 | glDisable(GL_FOG); |
---|
121 | |
---|
122 | return 0; |
---|
123 | } |
---|
124 | |
---|
125 | void FogEffect::draw() const { |
---|
126 | |
---|
127 | // If Fog Fade In |
---|
128 | if ( this->fogFadeInActivate ) { |
---|
129 | glFogf(GL_FOG_DENSITY, this->fogFadeInDensity); |
---|
130 | // PRINTF(0)( "this->fogFadeInDensity: %f\n", this->fogFadeInDensity); |
---|
131 | } |
---|
132 | // If Fog Fade Out |
---|
133 | else if ( this->fogFadeOutActivate ) |
---|
134 | glFogf(GL_FOG_DENSITY, this->fogFadeOutDensity); |
---|
135 | |
---|
136 | // Normal Fog activate |
---|
137 | else |
---|
138 | glFogf(GL_FOG_DENSITY, this->fogDensity); |
---|
139 | |
---|
140 | } |
---|
141 | |
---|
142 | void FogEffect::tick(float dt) |
---|
143 | { |
---|
144 | if (!this->fogActivate) |
---|
145 | return; |
---|
146 | |
---|
147 | if ( this->fogFadeInActivate ) { |
---|
148 | this->localTimer += dt; |
---|
149 | float progress = this->localTimer / this->fogFadeInDuration; |
---|
150 | this->fogFadeInDensity = progress * this->fogDensity; |
---|
151 | |
---|
152 | if ( this->localTimer >= this->fogFadeOutDuration ) |
---|
153 | this->fogFadeInActivate = false; |
---|
154 | } |
---|
155 | else if ( this->fogFadeOutActivate ) { |
---|
156 | this->localTimer += dt; |
---|
157 | float progress = this->localTimer / this->fogFadeInDuration; |
---|
158 | this->fogFadeOutDensity = 1 - progress * this->fogDensity; |
---|
159 | |
---|
160 | if ( this->localTimer >= this->fogFadeOutDuration ) |
---|
161 | this->deactivate(); |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | void FogEffect::fadeInFog() { |
---|
166 | |
---|
167 | // If Fog is already fading out, stop it |
---|
168 | this->fogFadeOutActivate = false; |
---|
169 | |
---|
170 | // If Fog is already on, turn it off first |
---|
171 | if (this->fogActivate) |
---|
172 | this->deactivate(); |
---|
173 | |
---|
174 | // If no manual FadeIn value was set, set a default value |
---|
175 | if (!this->fogFadeInDuration > 0) |
---|
176 | this->fogFadeInDuration = 20; |
---|
177 | |
---|
178 | // Reset local timer |
---|
179 | this->localTimer = 0; |
---|
180 | |
---|
181 | // set FogFadeIn activate |
---|
182 | this->fogFadeInActivate = true; |
---|
183 | |
---|
184 | // Activate Fog |
---|
185 | this->activate(); |
---|
186 | |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | void FogEffect::fadeOutFog() { |
---|
191 | |
---|
192 | // If Fog is already fading in, stop it |
---|
193 | this->fogFadeInActivate = false; |
---|
194 | |
---|
195 | // If Fog is off, turn it on first |
---|
196 | if (!this->fogActivate) |
---|
197 | this->activate(); |
---|
198 | |
---|
199 | // If no manual FadeOut value was set, set a default value |
---|
200 | if (!this->fogFadeOutDuration > 0) |
---|
201 | this->fogFadeOutDuration = 20; |
---|
202 | |
---|
203 | // set FogFadeOut activate |
---|
204 | this->fogFadeOutActivate = true; |
---|
205 | |
---|
206 | // Reset local timer |
---|
207 | this->localTimer = 0; |
---|
208 | |
---|
209 | } |
---|
210 | |
---|
211 | |
---|
212 | GLint FogEffect::stringToFogMode(const std::string& mode) |
---|
213 | { |
---|
214 | if(mode == "GL_LINEAR") |
---|
215 | return GL_LINEAR; |
---|
216 | else if(mode == "GL_EXP") |
---|
217 | return GL_EXP; |
---|
218 | else if(mode == "GL_EXP2" ) |
---|
219 | return GL_EXP2; |
---|
220 | else |
---|
221 | return -1; |
---|
222 | } |
---|
223 | |
---|
224 | |
---|