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.005; |
---|
72 | this->fogStart = 0; |
---|
73 | this->fogEnd = 200; |
---|
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->fogFadeEnd = 0; |
---|
81 | |
---|
82 | this->localTimer = 0; |
---|
83 | this->fogActivate = false; |
---|
84 | this->fogFadeInActivate = false; |
---|
85 | this->fogFadeOutActivate = false; |
---|
86 | |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | void FogEffect::activate() { |
---|
91 | PRINTF(0)( "Activating FogEffect\n"); |
---|
92 | |
---|
93 | // init fogGL |
---|
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 | glHint(GL_FOG_HINT, GL_DONT_CARE); |
---|
98 | glFogf(GL_FOG_DENSITY, this->fogDensity); |
---|
99 | glFogf(GL_FOG_START, this->fogStart); |
---|
100 | glFogf(GL_FOG_END, this->fogEnd); |
---|
101 | |
---|
102 | this->fogActivate = true; |
---|
103 | |
---|
104 | glEnable(GL_FOG); |
---|
105 | |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | void FogEffect::deactivate() { |
---|
110 | PRINTF(0)("Deactivating FogEffect\n"); |
---|
111 | |
---|
112 | this->fogFadeInActivate = false; |
---|
113 | this->fogFadeOutActivate = false; |
---|
114 | this->fogActivate = false; |
---|
115 | |
---|
116 | glDisable(GL_FOG); |
---|
117 | |
---|
118 | } |
---|
119 | |
---|
120 | void FogEffect::draw() const { |
---|
121 | |
---|
122 | if (!this->fogActivate) |
---|
123 | return; |
---|
124 | |
---|
125 | // If Fog Fade |
---|
126 | if ( this->fogFadeInActivate || this->fogFadeOutActivate ) { |
---|
127 | if ( this->fogMode == GL_LINEAR) |
---|
128 | glFogf(GL_FOG_END, this->fogFadeEnd); |
---|
129 | else |
---|
130 | glFogf(GL_FOG_DENSITY, this->fogFadeDensity); |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | void FogEffect::tick(float dt) { |
---|
135 | |
---|
136 | if (!this->fogActivate) |
---|
137 | return; |
---|
138 | |
---|
139 | if ( this->fogFadeInActivate ) { |
---|
140 | this->localTimer += dt; |
---|
141 | |
---|
142 | if ( this->fogMode == GL_LINEAR) |
---|
143 | this->fogFadeEnd = 2000 * (1 - ( this->localTimer / this->fogFadeInDuration )) + this->fogEnd; |
---|
144 | else |
---|
145 | this->fogFadeDensity = ( this->localTimer / this->fogFadeInDuration ) * this->fogDensity; |
---|
146 | |
---|
147 | if ( this->localTimer >= this->fogFadeInDuration ) |
---|
148 | this->fogFadeInActivate = false; |
---|
149 | } |
---|
150 | |
---|
151 | if ( this->fogFadeOutActivate ) { |
---|
152 | this->localTimer += dt; |
---|
153 | |
---|
154 | if ( this->fogMode == GL_LINEAR) |
---|
155 | this->fogFadeEnd = 2000 * ( this->localTimer / this->fogFadeInDuration ) + this->fogEnd; |
---|
156 | else |
---|
157 | this->fogFadeDensity = 1 - (( this->localTimer / this->fogFadeInDuration ) * this->fogDensity); |
---|
158 | |
---|
159 | if ( this->localTimer >= this->fogFadeOutDuration ) |
---|
160 | this->deactivate(); |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | void FogEffect::fadeInFog() { |
---|
165 | |
---|
166 | // If Fog is already fading out, stop it |
---|
167 | this->fogFadeOutActivate = false; |
---|
168 | |
---|
169 | // If Fog is already on, turn it off first |
---|
170 | if (this->fogActivate) |
---|
171 | this->deactivate(); |
---|
172 | |
---|
173 | // If no manual FadeIn value was set, set a default value |
---|
174 | if (!this->fogFadeInDuration > 0) |
---|
175 | this->fogFadeInDuration = 20; |
---|
176 | |
---|
177 | // Reset local timer |
---|
178 | this->localTimer = 0; |
---|
179 | |
---|
180 | // Activate Fog |
---|
181 | this->activate(); |
---|
182 | |
---|
183 | // set FogFadeIn activate |
---|
184 | this->fogFadeInActivate = true; |
---|
185 | |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | void FogEffect::fadeOutFog() { |
---|
190 | |
---|
191 | // If Fog is already fading in, stop it |
---|
192 | this->fogFadeInActivate = false; |
---|
193 | |
---|
194 | // If Fog is off, turn it on first |
---|
195 | if (!this->fogActivate) |
---|
196 | this->activate(); |
---|
197 | |
---|
198 | // If no manual FadeOut value was set, set a default value |
---|
199 | if (!this->fogFadeOutDuration > 0) |
---|
200 | this->fogFadeOutDuration = 20; |
---|
201 | |
---|
202 | // set FogFadeOut activate |
---|
203 | this->fogFadeOutActivate = true; |
---|
204 | |
---|
205 | // Reset local timer |
---|
206 | this->localTimer = 0; |
---|
207 | |
---|
208 | } |
---|
209 | |
---|