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