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: Stefan Lienard |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #include "mapped_water.h" |
---|
17 | #include "util/loading/load_param.h" |
---|
18 | #include "util/loading/factory.h" |
---|
19 | #include "util/loading/resource_manager.h" |
---|
20 | #include "state.h" |
---|
21 | #include "t_animation.h" |
---|
22 | #include <cmath> |
---|
23 | #include "glgui.h" |
---|
24 | #include "shell_command.h" |
---|
25 | #include "script_class.h" |
---|
26 | |
---|
27 | CREATE_FACTORY(MappedWater, CL_MAPPED_WATER); |
---|
28 | |
---|
29 | SHELL_COMMAND(gui, MappedWater, toggleGui); |
---|
30 | SHELL_COMMAND(output, MappedWater, saveParams); |
---|
31 | |
---|
32 | CREATE_SCRIPTABLE_CLASS(MappedWater, CL_MAPPED_WATER, |
---|
33 | addMethod("waterUV", ExecutorLua2<MappedWater, float, float>(&MappedWater::fadeWaterUV)) |
---|
34 | ->addMethod("waterFlow", ExecutorLua2<MappedWater, float, float>(&MappedWater::fadeWaterFlow)) |
---|
35 | ->addMethod("shineSize", ExecutorLua2<MappedWater, float, float>(&MappedWater::fadeShineSize)) |
---|
36 | ->addMethod("shineStrength", ExecutorLua2<MappedWater, float, float>(&MappedWater::fadeShineStrength)) |
---|
37 | ->addMethod("reflStrength", ExecutorLua2<MappedWater, float, float>(&MappedWater::fadeReflStrength)) |
---|
38 | ->addMethod("refraction", ExecutorLua2<MappedWater, float, float>(&MappedWater::fadeRefraction)) |
---|
39 | ->addMethod("waterHeight", ExecutorLua2<MappedWater, float, float>(&MappedWater::fadeWaterHeight)) |
---|
40 | ->addMethod("waterColor", ExecutorLua4<MappedWater, float, float, float, float>(&MappedWater::fadeWaterColor))); |
---|
41 | |
---|
42 | |
---|
43 | /** |
---|
44 | * @brief constructor |
---|
45 | * @param root xml data |
---|
46 | */ |
---|
47 | MappedWater::MappedWater(const TiXmlElement* root) |
---|
48 | { |
---|
49 | this->setClassID(CL_MAPPED_WATER, "MappedWater"); |
---|
50 | this->toList(OM_ENVIRON); |
---|
51 | |
---|
52 | /// sets start values and parameters |
---|
53 | this->initParams(); |
---|
54 | // now the standard values were loaded, if the values are specified in the .oxw file |
---|
55 | // loadParams will overwrite the standard values with the specified values |
---|
56 | if (root != NULL) |
---|
57 | this->loadParams(root); |
---|
58 | |
---|
59 | /// initialization of the textures |
---|
60 | this->initTextures(); |
---|
61 | |
---|
62 | /// initialization of the shaders |
---|
63 | this->initShaders(); |
---|
64 | |
---|
65 | /// calculation of the 4 verts of the water quad |
---|
66 | this->calcVerts(); |
---|
67 | |
---|
68 | // init gui |
---|
69 | this->box = NULL; |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * @brief deltes shader and the uniform used by the camera |
---|
74 | */ |
---|
75 | MappedWater::~MappedWater() |
---|
76 | { |
---|
77 | delete shader; |
---|
78 | delete cam_uni; |
---|
79 | delete color_uni; |
---|
80 | delete light_uni; |
---|
81 | delete shineSize_uni; |
---|
82 | delete shineStrength_uni; |
---|
83 | delete reflStrength_uni; |
---|
84 | delete refr_uni; |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * @brief initialization of loadable parameters, sets start standard values |
---|
89 | */ |
---|
90 | void MappedWater::initParams() |
---|
91 | { |
---|
92 | // those standardvalues will be overwritten if they're also set in the oxw file |
---|
93 | this->setWaterPos(0, 0, 0); |
---|
94 | this->setWaterSize(100, 100); |
---|
95 | this->setWaterUV(9); |
---|
96 | this->setWaterFlow(0.08f); |
---|
97 | this->setLightPos(0, 10, 0); |
---|
98 | this->setWaterAngle(0); |
---|
99 | this->setNormalMapScale(0.25f); |
---|
100 | this->setWaterColor(0.1f, 0.2f, 0.4f); |
---|
101 | this->setShineSize(128); |
---|
102 | this->setShineStrength(0.7f); |
---|
103 | this->setReflStrength(1.0f); |
---|
104 | this->setRefraction(0.009f); |
---|
105 | |
---|
106 | // initialization of the texture coords, speeds etc... |
---|
107 | // normalUV wont change anymore |
---|
108 | this->normalUV = this->waterUV * this->kNormalMapScale; |
---|
109 | // move and move2 are used for the reflection and refraction, the values are changed in tick() |
---|
110 | this->move = 0; |
---|
111 | this->move2 = this->move * this->kNormalMapScale; |
---|
112 | |
---|
113 | // initalize fading bools |
---|
114 | this->bFadeWaterHeight = false; |
---|
115 | this->bFadeWaterUV = false; |
---|
116 | this->bFadeWaterFlow = false; |
---|
117 | this->bFadeShineSize = false; |
---|
118 | this->bFadeShineStrength = false; |
---|
119 | this->bFadeReflStrength = false; |
---|
120 | this->bFadeRefraction = false; |
---|
121 | this->bFadeWaterColor = false; |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * @brief initialization of the textures |
---|
126 | */ |
---|
127 | void MappedWater::initTextures() |
---|
128 | { |
---|
129 | // sets parameters for the textures |
---|
130 | this->textureSize = 512; |
---|
131 | unsigned int channels = 32; |
---|
132 | GLenum type = GL_RGBA; |
---|
133 | |
---|
134 | // set up refleciton texture |
---|
135 | Texture reflTex(GL_TEXTURE_2D, this->textureSize, this->textureSize, channels, type); |
---|
136 | mat.setDiffuseMap(reflTex, 0); |
---|
137 | // set up refraction texture |
---|
138 | Texture refrTex(GL_TEXTURE_2D, this->textureSize, this->textureSize, channels, type); |
---|
139 | mat.setDiffuseMap(refrTex, 1); |
---|
140 | // load normal map |
---|
141 | mat.setDiffuseMap("pictures/water_normalmap.bmp", GL_TEXTURE_2D, 2); |
---|
142 | // load dudv map |
---|
143 | mat.setDiffuseMap("pictures/water_dudvmap.bmp", GL_TEXTURE_2D, 3); |
---|
144 | |
---|
145 | // sets texture parameters for reflection texture |
---|
146 | glBindTexture(GL_TEXTURE_2D, this->mat.diffuseTextureID(0)); |
---|
147 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); |
---|
148 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); |
---|
149 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
---|
150 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
---|
151 | // sets texture parameters for refraction texture |
---|
152 | glBindTexture(GL_TEXTURE_2D, this->mat.diffuseTextureID(1)); |
---|
153 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); |
---|
154 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); |
---|
155 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
---|
156 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * @brief initialization of the shaders |
---|
161 | */ |
---|
162 | void MappedWater::initShaders() |
---|
163 | { |
---|
164 | // load shader files |
---|
165 | shader = new Shader( ResourceManager::getInstance()->getDataDir() + "/shaders/mapped_water.vert", ResourceManager::getInstance()->getDataDir() +"/shaders/mapped_water.frag"); |
---|
166 | |
---|
167 | this->shader->activateShader(); |
---|
168 | // Set the variable "reflection" to correspond to the first texture unit |
---|
169 | Shader::Uniform(shader, "reflection").set(0); |
---|
170 | // Set the variable "refraction" to correspond to the second texture unit |
---|
171 | Shader::Uniform(shader, "refraction").set(1); |
---|
172 | // Set the variable "normalMap" to correspond to the third texture unit |
---|
173 | Shader::Uniform(shader, "normalMap").set(2); |
---|
174 | // Set the variable "dudvMap" to correspond to the fourth texture unit |
---|
175 | Shader::Uniform(shader, "dudvMap").set(3); |
---|
176 | // Set the variable "depthMap" to correspond to the fifth texture unit |
---|
177 | Shader::Uniform(shader, "depthMap").set(1); |
---|
178 | // Give the variable "waterColor" a blue color |
---|
179 | color_uni = new Shader::Uniform(shader, "waterColor"); |
---|
180 | color_uni->set(waterColor.x, waterColor.y, waterColor.z, 1.0f); |
---|
181 | // Give the variable "lightPos" our hard coded light position |
---|
182 | light_uni = new Shader::Uniform(shader, "lightPos"); |
---|
183 | light_uni->set(lightPos.x, lightPos.y, lightPos.z, 1.0f); |
---|
184 | // Set the variable "shine" |
---|
185 | shineSize_uni = new Shader::Uniform(shader, "kShine"); |
---|
186 | shineSize_uni->set(this->shineSize); |
---|
187 | // Set the variable "shineStrength" |
---|
188 | shineStrength_uni = new Shader::Uniform(shader, "shineStrength"); |
---|
189 | shineStrength_uni->set(this->shineStrength); |
---|
190 | // Set the variable "reflStrength" |
---|
191 | reflStrength_uni = new Shader::Uniform(shader, "reflStrength"); |
---|
192 | reflStrength_uni->set(this->reflStrength); |
---|
193 | // Set the variable "refraction" |
---|
194 | refr_uni = new Shader::Uniform(shader, "kRefraction"); |
---|
195 | refr_uni->set(this->refraction); |
---|
196 | // uniform for the camera position |
---|
197 | cam_uni = new Shader::Uniform(shader, "cameraPos"); |
---|
198 | |
---|
199 | this->shader->deactivateShader(); |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * @brief calculates the 4 verts of the water quad |
---|
204 | */ |
---|
205 | void MappedWater::calcVerts() |
---|
206 | { |
---|
207 | float deg2radtemp = this->waterAngle / 180 * PI; |
---|
208 | |
---|
209 | this->waterVerts[2] = this->waterVerts[0] + cos(deg2radtemp) * this->xWidth; |
---|
210 | this->waterVerts[3] = this->waterVerts[1] + sin(deg2radtemp) * this->xWidth; |
---|
211 | this->waterVerts[4] = this->waterVerts[0] + cos(deg2radtemp) * this->xWidth - sin(deg2radtemp) * this->zWidth; |
---|
212 | this->waterVerts[5] = this->waterVerts[1] + sin(deg2radtemp) * this->xWidth + cos(deg2radtemp) * this->zWidth; |
---|
213 | this->waterVerts[6] = this->waterVerts[0] - sin(deg2radtemp) * this->zWidth; |
---|
214 | this->waterVerts[7] = this->waterVerts[1] + cos(deg2radtemp) * this->zWidth; |
---|
215 | } |
---|
216 | |
---|
217 | /** |
---|
218 | * @brief resets the waterColor in the Shader |
---|
219 | * @param r new value for red |
---|
220 | * @param g new value for green |
---|
221 | * @param b new value for blue |
---|
222 | */ |
---|
223 | void MappedWater::resetWaterColor(float r, float g, float b) |
---|
224 | { |
---|
225 | this->shader->activateShader(); |
---|
226 | this->waterColor = Vector(r, g, b); |
---|
227 | |
---|
228 | // Give the variable "waterColor" a color |
---|
229 | color_uni->set(waterColor.x, waterColor.y, waterColor.z, 1.0f); |
---|
230 | |
---|
231 | this->shader->deactivateShader(); |
---|
232 | } |
---|
233 | |
---|
234 | /** |
---|
235 | * @brief resets the shininess in the Shader |
---|
236 | * @param shine new value for the shininess |
---|
237 | */ |
---|
238 | void MappedWater::resetShineSize(float shine) |
---|
239 | { |
---|
240 | this->shader->activateShader(); |
---|
241 | this->shineSize = shine; |
---|
242 | |
---|
243 | // Set the variable "shine" |
---|
244 | shineSize_uni->set(this->shineSize); |
---|
245 | |
---|
246 | this->shader->deactivateShader(); |
---|
247 | } |
---|
248 | |
---|
249 | /** |
---|
250 | * @brief resets the strength of the specular reflection in the Shader |
---|
251 | * @param strength new value for the strength of the specular reflection |
---|
252 | */ |
---|
253 | void MappedWater::resetShineStrength(float strength) |
---|
254 | { |
---|
255 | this->shader->activateShader(); |
---|
256 | this->shineStrength = strength; |
---|
257 | |
---|
258 | // Set the variable "shine" |
---|
259 | shineStrength_uni->set(this->shineStrength); |
---|
260 | |
---|
261 | this->shader->deactivateShader(); |
---|
262 | } |
---|
263 | |
---|
264 | /** |
---|
265 | * @brief resets the strength of the reflection in the Shader |
---|
266 | * @param strength new value for the strength of the reflection |
---|
267 | */ |
---|
268 | void MappedWater::resetReflStrength(float strength) |
---|
269 | { |
---|
270 | this->shader->activateShader(); |
---|
271 | this->reflStrength = strength; |
---|
272 | |
---|
273 | // Set the variable "shine" |
---|
274 | reflStrength_uni->set(this->reflStrength); |
---|
275 | |
---|
276 | this->shader->deactivateShader(); |
---|
277 | } |
---|
278 | |
---|
279 | /** |
---|
280 | * @brief resets the refraction in the Shader |
---|
281 | * @param refraction new value for the refraction |
---|
282 | */ |
---|
283 | void MappedWater::resetRefraction(float refraction) |
---|
284 | { |
---|
285 | this->shader->activateShader(); |
---|
286 | this->refraction = refraction; |
---|
287 | |
---|
288 | // Set the variable "shine" |
---|
289 | refr_uni->set(this->refraction); |
---|
290 | |
---|
291 | this->shader->deactivateShader(); |
---|
292 | } |
---|
293 | |
---|
294 | /** |
---|
295 | * @brief resets the lightPos in the Shader |
---|
296 | * @param x new x value |
---|
297 | * @param y new y value |
---|
298 | * @param z new z value |
---|
299 | */ |
---|
300 | void MappedWater::resetLightPos(float x, float y, float z) |
---|
301 | { |
---|
302 | this->shader->activateShader(); |
---|
303 | this->lightPos = Vector(x, y, z); |
---|
304 | |
---|
305 | // Give the variable "lightPos" our hard coded light position |
---|
306 | light_uni->set(lightPos.x, lightPos.y, lightPos.z, 1.0f); |
---|
307 | |
---|
308 | this->shader->deactivateShader(); |
---|
309 | } |
---|
310 | |
---|
311 | /** |
---|
312 | * @brief ends the refraction and saves the graphic buffer into a texture |
---|
313 | * @param root xml data |
---|
314 | */ |
---|
315 | void MappedWater::loadParams(const TiXmlElement* root) |
---|
316 | { |
---|
317 | WorldEntity::loadParams(root); |
---|
318 | |
---|
319 | LoadParam(root, "waterpos", this, MappedWater, setWaterPos); |
---|
320 | LoadParam(root, "watersize", this, MappedWater, setWaterSize); |
---|
321 | LoadParam(root, "wateruv", this, MappedWater, setWaterUV); |
---|
322 | LoadParam(root, "waterflow", this, MappedWater, setWaterFlow); |
---|
323 | LoadParam(root, "lightpos", this, MappedWater, setLightPos); |
---|
324 | LoadParam(root, "waterangle", this, MappedWater, setWaterAngle); |
---|
325 | LoadParam(root, "normalmapscale", this, MappedWater, setNormalMapScale); |
---|
326 | LoadParam(root, "shinesize", this, MappedWater, setShineSize); |
---|
327 | LoadParam(root, "shinestrength", this, MappedWater, setShineStrength); |
---|
328 | LoadParam(root, "reflstrength", this, MappedWater, setReflStrength); |
---|
329 | LoadParam(root, "refraction", this, MappedWater, setRefraction); |
---|
330 | LoadParam(root, "watercolor", this, MappedWater, setWaterColor); |
---|
331 | } |
---|
332 | |
---|
333 | /** |
---|
334 | * @brief prints the xml code of the water params |
---|
335 | */ |
---|
336 | void MappedWater::saveParams() |
---|
337 | { |
---|
338 | // it's not too nice, but it works fine |
---|
339 | PRINT(0)("\nMappedWater XML Code:\n<MappedWater>\n"); |
---|
340 | |
---|
341 | PRINT(0)(" <waterpos>%f, %f, %f</waterpos>\n", this->waterVerts[0], this->waterHeight, this->waterVerts[1]); |
---|
342 | PRINT(0)(" <watersize>%f, %f</watersize>\n", this->xWidth, this->zWidth); |
---|
343 | PRINT(0)(" <wateruv>%f</wateruv>\n", this->waterUV); |
---|
344 | PRINT(0)(" <waterflow>%f</waterflow>\n", this->waterFlow); |
---|
345 | PRINT(0)(" <lightpos>%f, %f, %f</lightpos>\n", this->lightPos.x, this->lightPos.y, this->lightPos.z); |
---|
346 | PRINT(0)(" <waterangle>%f</waterangle>\n", this->waterAngle); |
---|
347 | PRINT(0)(" <normalmapscale>%f</normalmapscale>\n", this->kNormalMapScale); |
---|
348 | PRINT(0)(" <shinesize>%f</shinesize>\n", this->shineSize); |
---|
349 | PRINT(0)(" <shinestrength>%f</shinestrength>\n", this->shineStrength); |
---|
350 | PRINT(0)(" <reflstrength>%f</reflstrength>\n", this->reflStrength); |
---|
351 | PRINT(0)(" <refraction>%f</refraction>\n", this->refraction); |
---|
352 | PRINT(0)(" <watercolor>%f, %f, %f</watercolor>\n", this->waterColor.x, this->waterColor.y, this->waterColor.z); |
---|
353 | |
---|
354 | PRINT(0)("</MappedWater>\n"); |
---|
355 | } |
---|
356 | |
---|
357 | /** |
---|
358 | * @brief starts the slider gui that lets you edit all water parameters |
---|
359 | */ |
---|
360 | void MappedWater::toggleGui() |
---|
361 | { |
---|
362 | if (this->box == NULL) |
---|
363 | { |
---|
364 | this->box = new OrxGui::GLGuiBox(OrxGui::Vertical); |
---|
365 | { |
---|
366 | OrxGui::GLGuiBox* waterColorBox = new OrxGui::GLGuiBox(OrxGui::Horizontal); |
---|
367 | { |
---|
368 | OrxGui::GLGuiText* waterColorText = new OrxGui::GLGuiText(); |
---|
369 | waterColorText->setText("WaterColor"); |
---|
370 | waterColorBox->pack(waterColorText); |
---|
371 | |
---|
372 | OrxGui::GLGuiSlider* waterColorR = new OrxGui::GLGuiSlider(); |
---|
373 | waterColorR->setRange(0, 1.0f); |
---|
374 | waterColorR->setValue(this->waterColor.x); |
---|
375 | waterColorR->setStep(0.1f); |
---|
376 | waterColorR->connect(SIGNAL(waterColorR, valueChanged), this, SLOT(MappedWater, resetWaterColorR)); |
---|
377 | waterColorBox->pack(waterColorR); |
---|
378 | |
---|
379 | OrxGui::GLGuiSlider* waterColorG = new OrxGui::GLGuiSlider(); |
---|
380 | waterColorG->setRange(0, 1.0f); |
---|
381 | waterColorG->setStep(0.1f); |
---|
382 | waterColorG->setValue(this->waterColor.y); |
---|
383 | waterColorG->connect(SIGNAL(waterColorG, valueChanged), this, SLOT(MappedWater, resetWaterColorG)); |
---|
384 | waterColorBox->pack(waterColorG); |
---|
385 | |
---|
386 | OrxGui::GLGuiSlider* waterColorB = new OrxGui::GLGuiSlider(); |
---|
387 | waterColorB->setRange(0, 1.0f); |
---|
388 | waterColorB->setStep(0.1f); |
---|
389 | waterColorB->setValue(this->waterColor.z); |
---|
390 | waterColorB->connect(SIGNAL(waterColorB, valueChanged), this, SLOT(MappedWater, resetWaterColorB)); |
---|
391 | waterColorBox->pack(waterColorB); |
---|
392 | } |
---|
393 | this->box->pack(waterColorBox); |
---|
394 | |
---|
395 | OrxGui::GLGuiBox* waterUVBox = new OrxGui::GLGuiBox(OrxGui::Horizontal); |
---|
396 | { |
---|
397 | OrxGui::GLGuiText* waterUVText = new OrxGui::GLGuiText(); |
---|
398 | waterUVText->setText("WaterUV"); |
---|
399 | waterUVBox->pack(waterUVText); |
---|
400 | |
---|
401 | OrxGui::GLGuiSlider* waterUV = new OrxGui::GLGuiSlider(); |
---|
402 | waterUV->setRange(1, 20); |
---|
403 | waterUV->setValue(this->waterUV); |
---|
404 | waterUV->setStep(1); |
---|
405 | waterUV->connect(SIGNAL(waterUV, valueChanged), this, SLOT(MappedWater, setWaterUV)); |
---|
406 | waterUVBox->pack(waterUV); |
---|
407 | } |
---|
408 | this->box->pack(waterUVBox); |
---|
409 | |
---|
410 | OrxGui::GLGuiBox* waterFlowBox = new OrxGui::GLGuiBox(OrxGui::Horizontal); |
---|
411 | { |
---|
412 | OrxGui::GLGuiText* waterFlowText = new OrxGui::GLGuiText(); |
---|
413 | waterFlowText->setText("WaterFlow"); |
---|
414 | waterFlowBox->pack(waterFlowText); |
---|
415 | |
---|
416 | OrxGui::GLGuiSlider* waterFlow = new OrxGui::GLGuiSlider(); |
---|
417 | waterFlow->setRange(0.01f, 2); |
---|
418 | waterFlow->setValue(this->waterFlow); |
---|
419 | waterFlow->setStep(0.02f); |
---|
420 | waterFlow->connect(SIGNAL(waterFlow, valueChanged), this, SLOT(MappedWater, setWaterFlow)); |
---|
421 | waterFlowBox->pack(waterFlow); |
---|
422 | } |
---|
423 | this->box->pack(waterFlowBox); |
---|
424 | |
---|
425 | OrxGui::GLGuiBox* shineSizeBox = new OrxGui::GLGuiBox(OrxGui::Horizontal); |
---|
426 | { |
---|
427 | OrxGui::GLGuiText* shineSizeText = new OrxGui::GLGuiText(); |
---|
428 | shineSizeText->setText("ShineSize"); |
---|
429 | shineSizeBox->pack(shineSizeText); |
---|
430 | |
---|
431 | OrxGui::GLGuiSlider* shineSize = new OrxGui::GLGuiSlider(); |
---|
432 | shineSize->setRange(1, 128); |
---|
433 | shineSize->setValue(this->shineSize); |
---|
434 | shineSize->setStep(1); |
---|
435 | shineSize->connect(SIGNAL(shineSize, valueChanged), this, SLOT(MappedWater, resetShineSize)); |
---|
436 | shineSizeBox->pack(shineSize); |
---|
437 | } |
---|
438 | this->box->pack(shineSizeBox); |
---|
439 | |
---|
440 | OrxGui::GLGuiBox* shineStrengthBox = new OrxGui::GLGuiBox(OrxGui::Horizontal); |
---|
441 | { |
---|
442 | OrxGui::GLGuiText* shineStrengthText = new OrxGui::GLGuiText(); |
---|
443 | shineStrengthText->setText("ShineStrength"); |
---|
444 | shineStrengthBox->pack(shineStrengthText); |
---|
445 | |
---|
446 | OrxGui::GLGuiSlider* shineStrength = new OrxGui::GLGuiSlider(); |
---|
447 | shineStrength->setRange(0, 1); |
---|
448 | shineStrength->setValue(this->shineStrength); |
---|
449 | shineStrength->setStep(0.1f); |
---|
450 | shineStrength->connect(SIGNAL(shineStrength, valueChanged), this, SLOT(MappedWater, resetShineStrength)); |
---|
451 | shineStrengthBox->pack(shineStrength); |
---|
452 | } |
---|
453 | this->box->pack(shineStrengthBox); |
---|
454 | |
---|
455 | OrxGui::GLGuiBox* reflStrengthBox = new OrxGui::GLGuiBox(OrxGui::Horizontal); |
---|
456 | { |
---|
457 | OrxGui::GLGuiText* reflStrengthText = new OrxGui::GLGuiText(); |
---|
458 | reflStrengthText->setText("ReflStrength"); |
---|
459 | reflStrengthBox->pack(reflStrengthText); |
---|
460 | |
---|
461 | OrxGui::GLGuiSlider* reflStrength = new OrxGui::GLGuiSlider(); |
---|
462 | reflStrength->setRange(0, 1); |
---|
463 | reflStrength->setValue(this->reflStrength); |
---|
464 | reflStrength->setStep(0.1f); |
---|
465 | reflStrength->connect(SIGNAL(reflStrength, valueChanged), this, SLOT(MappedWater, resetReflStrength)); |
---|
466 | reflStrengthBox->pack(reflStrength); |
---|
467 | } |
---|
468 | this->box->pack(reflStrengthBox); |
---|
469 | |
---|
470 | OrxGui::GLGuiBox* refractionBox = new OrxGui::GLGuiBox(OrxGui::Horizontal); |
---|
471 | { |
---|
472 | OrxGui::GLGuiText* refractionText = new OrxGui::GLGuiText(); |
---|
473 | refractionText->setText("Refraction"); |
---|
474 | refractionBox->pack(refractionText); |
---|
475 | |
---|
476 | OrxGui::GLGuiSlider* refraction = new OrxGui::GLGuiSlider(); |
---|
477 | refraction->setRange(0.001f, 0.1f); |
---|
478 | refraction->setValue(this->refraction); |
---|
479 | refraction->setStep(0.004f); |
---|
480 | refraction->connect(SIGNAL(refraction, valueChanged), this, SLOT(MappedWater, resetRefraction)); |
---|
481 | refractionBox->pack(refraction); |
---|
482 | } |
---|
483 | this->box->pack(refractionBox); |
---|
484 | |
---|
485 | OrxGui::GLGuiBox* lightPosBox = new OrxGui::GLGuiBox(OrxGui::Horizontal); |
---|
486 | { |
---|
487 | OrxGui::GLGuiText* lightPosText = new OrxGui::GLGuiText(); |
---|
488 | lightPosText->setText("LightPos"); |
---|
489 | lightPosBox->pack(lightPosText); |
---|
490 | |
---|
491 | OrxGui::GLGuiSlider* lightPosX = new OrxGui::GLGuiSlider(); |
---|
492 | lightPosX->setRange(-4000, 4000); |
---|
493 | lightPosX->setValue(this->lightPos.x); |
---|
494 | lightPosX->setStep(15); |
---|
495 | lightPosX->connect(SIGNAL(lightPosX, valueChanged), this, SLOT(MappedWater, resetLightPosX)); |
---|
496 | lightPosBox->pack(lightPosX); |
---|
497 | |
---|
498 | OrxGui::GLGuiSlider* lightPosY = new OrxGui::GLGuiSlider(); |
---|
499 | lightPosY->setRange(-4000, 4000); |
---|
500 | lightPosY->setStep(15); |
---|
501 | lightPosY->setValue(this->lightPos.y); |
---|
502 | lightPosY->connect(SIGNAL(lightPosY, valueChanged), this, SLOT(MappedWater, resetLightPosY)); |
---|
503 | lightPosBox->pack(lightPosY); |
---|
504 | |
---|
505 | OrxGui::GLGuiSlider* lightPosZ = new OrxGui::GLGuiSlider(); |
---|
506 | lightPosZ->setRange(-4000, 4000); |
---|
507 | lightPosZ->setStep(15); |
---|
508 | lightPosZ->setValue(this->lightPos.z); |
---|
509 | lightPosZ->connect(SIGNAL(lightPosZ, valueChanged), this, SLOT(MappedWater, resetLightPosZ)); |
---|
510 | lightPosBox->pack(lightPosZ); |
---|
511 | } |
---|
512 | this->box->pack(lightPosBox); |
---|
513 | |
---|
514 | OrxGui::GLGuiBox* waterHeightBox = new OrxGui::GLGuiBox(OrxGui::Horizontal); |
---|
515 | { |
---|
516 | OrxGui::GLGuiText* waterHeightText = new OrxGui::GLGuiText(); |
---|
517 | waterHeightText->setText("WaterHeight"); |
---|
518 | waterHeightBox->pack(waterHeightText); |
---|
519 | |
---|
520 | OrxGui::GLGuiSlider* waterHeight = new OrxGui::GLGuiSlider(); |
---|
521 | waterHeight->setRange(100, 370); |
---|
522 | waterHeight->setValue(this->waterHeight); |
---|
523 | waterHeight->setStep(4); |
---|
524 | waterHeight->connect(SIGNAL(waterHeight, valueChanged), this, SLOT(MappedWater, setWaterHeight)); |
---|
525 | waterHeightBox->pack(waterHeight); |
---|
526 | } |
---|
527 | this->box->pack(waterHeightBox); |
---|
528 | } |
---|
529 | |
---|
530 | this->box->showAll(); |
---|
531 | this->box->setAbsCoor2D(300, 40); |
---|
532 | OrxGui::GLGuiHandler::getInstance()->activate(); |
---|
533 | OrxGui::GLGuiHandler::getInstance()->activateCursor(); |
---|
534 | } |
---|
535 | else |
---|
536 | { |
---|
537 | OrxGui::GLGuiHandler::getInstance()->deactivate(); |
---|
538 | OrxGui::GLGuiHandler::getInstance()->deactivateCursor(); |
---|
539 | delete this->box; |
---|
540 | this->box = NULL; |
---|
541 | } |
---|
542 | } |
---|
543 | |
---|
544 | /** |
---|
545 | * @brief activates the water shader and draws a quad with four textures on it |
---|
546 | */ |
---|
547 | void MappedWater::draw() const |
---|
548 | { |
---|
549 | glMatrixMode(GL_MODELVIEW); |
---|
550 | glPushMatrix(); |
---|
551 | |
---|
552 | // don't use glRotate or glTranslate here... the shader won't work anymore |
---|
553 | |
---|
554 | mat.select(); |
---|
555 | |
---|
556 | this->shader->activateShader(); |
---|
557 | |
---|
558 | // reset the camera uniform to the current cam position |
---|
559 | Vector pos = State::getCameraNode()->getAbsCoor(); |
---|
560 | cam_uni->set(pos.x, pos.y, pos.z, 1.0f); |
---|
561 | |
---|
562 | glDisable(GL_BLEND); |
---|
563 | |
---|
564 | // TODO change texture coords, so water doesnt look distorted when xWidth != zWidth |
---|
565 | glBegin(GL_QUADS); |
---|
566 | // The back left vertice for the water |
---|
567 | glMultiTexCoord2f(GL_TEXTURE0, 0, waterUV); // Reflection texture |
---|
568 | glMultiTexCoord2f(GL_TEXTURE1, 0, waterUV - move); // Refraction texture |
---|
569 | glMultiTexCoord2f(GL_TEXTURE2, 0, normalUV + move2); // Normal map texture |
---|
570 | glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture |
---|
571 | glVertex3f(this->waterVerts[0], this->waterHeight, this->waterVerts[1]); |
---|
572 | |
---|
573 | // The front left vertice for the water |
---|
574 | glMultiTexCoord2f(GL_TEXTURE0, 0, 0); // Reflection texture |
---|
575 | glMultiTexCoord2f(GL_TEXTURE1, 0, -move); // Refraction texture |
---|
576 | glMultiTexCoord2f(GL_TEXTURE2, 0, move2); // Normal map texture |
---|
577 | glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture |
---|
578 | glVertex3f(this->waterVerts[2], this->waterHeight, this->waterVerts[3]); |
---|
579 | |
---|
580 | // The front right vertice for the water |
---|
581 | glMultiTexCoord2f(GL_TEXTURE0, waterUV, 0); // Reflection texture |
---|
582 | glMultiTexCoord2f(GL_TEXTURE1, waterUV, -move); // Refraction texture |
---|
583 | glMultiTexCoord2f(GL_TEXTURE2, normalUV, move2); // Normal map texture |
---|
584 | glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture |
---|
585 | glVertex3f(this->waterVerts[4], this->waterHeight, this->waterVerts[5]); |
---|
586 | |
---|
587 | // The back right vertice for the water |
---|
588 | glMultiTexCoord2f(GL_TEXTURE0, waterUV, waterUV); // Reflection texture |
---|
589 | glMultiTexCoord2f(GL_TEXTURE1, waterUV, waterUV - move); // Refraction texture |
---|
590 | glMultiTexCoord2f(GL_TEXTURE2, normalUV, normalUV + move2); // Normal map texture |
---|
591 | glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture |
---|
592 | glVertex3f(this->waterVerts[6], this->waterHeight, this->waterVerts[7]); |
---|
593 | glEnd(); |
---|
594 | |
---|
595 | this->shader->deactivateShader(); |
---|
596 | |
---|
597 | mat.unselect(); |
---|
598 | |
---|
599 | glPopMatrix(); |
---|
600 | } |
---|
601 | |
---|
602 | /** |
---|
603 | * @brief tick tack, calculates the flow of the water |
---|
604 | */ |
---|
605 | void MappedWater::tick(float dt) |
---|
606 | { |
---|
607 | // makes the water flow |
---|
608 | this->move += this->waterFlow * dt; |
---|
609 | this->move2 = this->move * this->kNormalMapScale; |
---|
610 | |
---|
611 | // fading TODO fix this so it isnt hacky anymore |
---|
612 | if(bFadeWaterUV) |
---|
613 | { |
---|
614 | this->waterUVFader = new tAnimation<MappedWater>(this, &MappedWater::setWaterUV); |
---|
615 | this->waterUVFader->setInfinity(ANIM_INF_CONSTANT); |
---|
616 | |
---|
617 | this->waterUVFader->addKeyFrame(this->waterUV, this->waterUVFadeTime, ANIM_LINEAR); |
---|
618 | this->waterUVFader->addKeyFrame(this->newWaterUV, 0, ANIM_LINEAR); |
---|
619 | |
---|
620 | bFadeWaterUV = false; |
---|
621 | this->waterUVFader->replay(); |
---|
622 | } |
---|
623 | |
---|
624 | if(bFadeWaterFlow) |
---|
625 | { |
---|
626 | this->waterFlowFader = new tAnimation<MappedWater>(this, &MappedWater::setWaterFlow); |
---|
627 | this->waterFlowFader->setInfinity(ANIM_INF_CONSTANT); |
---|
628 | |
---|
629 | this->waterFlowFader->addKeyFrame(this->waterFlow, this->waterFlowFadeTime, ANIM_LINEAR); |
---|
630 | this->waterFlowFader->addKeyFrame(this->newWaterFlow, 0, ANIM_LINEAR); |
---|
631 | |
---|
632 | bFadeWaterFlow = false; |
---|
633 | this->waterFlowFader->replay(); |
---|
634 | } |
---|
635 | |
---|
636 | if(bFadeShineSize) |
---|
637 | { |
---|
638 | this->shineSizeFader = new tAnimation<MappedWater>(this, &MappedWater::resetShineSize); |
---|
639 | this->shineSizeFader->setInfinity(ANIM_INF_CONSTANT); |
---|
640 | |
---|
641 | this->shineSizeFader->addKeyFrame(this->shineSize, this->shineSizeFadeTime, ANIM_LINEAR); |
---|
642 | this->shineSizeFader->addKeyFrame(this->newShineSize, 0, ANIM_LINEAR); |
---|
643 | |
---|
644 | bFadeShineSize = false; |
---|
645 | this->shineSizeFader->replay(); |
---|
646 | } |
---|
647 | |
---|
648 | if(bFadeShineStrength) |
---|
649 | { |
---|
650 | this->shineStrengthFader = new tAnimation<MappedWater>(this, &MappedWater::resetShineStrength); |
---|
651 | this->shineStrengthFader->setInfinity(ANIM_INF_CONSTANT); |
---|
652 | |
---|
653 | this->shineStrengthFader->addKeyFrame(this->shineStrength, this->shineStrengthFadeTime, ANIM_LINEAR); |
---|
654 | this->shineStrengthFader->addKeyFrame(this->newShineStrength, 0, ANIM_LINEAR); |
---|
655 | |
---|
656 | bFadeShineStrength = false; |
---|
657 | this->shineStrengthFader->replay(); |
---|
658 | } |
---|
659 | |
---|
660 | if(bFadeReflStrength) |
---|
661 | { |
---|
662 | this->reflStrengthFader = new tAnimation<MappedWater>(this, &MappedWater::resetReflStrength); |
---|
663 | this->reflStrengthFader->setInfinity(ANIM_INF_CONSTANT); |
---|
664 | |
---|
665 | this->reflStrengthFader->addKeyFrame(this->reflStrength, this->reflStrengthFadeTime, ANIM_LINEAR); |
---|
666 | this->reflStrengthFader->addKeyFrame(this->newReflStrength, 0, ANIM_LINEAR); |
---|
667 | |
---|
668 | bFadeReflStrength = false; |
---|
669 | this->reflStrengthFader->replay(); |
---|
670 | } |
---|
671 | |
---|
672 | if(bFadeRefraction) |
---|
673 | { |
---|
674 | this->refractionFader = new tAnimation<MappedWater>(this, &MappedWater::resetRefraction); |
---|
675 | this->refractionFader->setInfinity(ANIM_INF_CONSTANT); |
---|
676 | |
---|
677 | this->refractionFader->addKeyFrame(this->refraction, this->refractionFadeTime, ANIM_LINEAR); |
---|
678 | this->refractionFader->addKeyFrame(this->newRefraction, 0, ANIM_LINEAR); |
---|
679 | |
---|
680 | bFadeRefraction = false; |
---|
681 | this->refractionFader->replay(); |
---|
682 | } |
---|
683 | |
---|
684 | if(bFadeWaterHeight) |
---|
685 | { |
---|
686 | this->waterHeightFader = new tAnimation<MappedWater>(this, &MappedWater::setWaterHeight); |
---|
687 | this->waterHeightFader->setInfinity(ANIM_INF_CONSTANT); |
---|
688 | |
---|
689 | this->waterHeightFader->addKeyFrame(this->waterHeight, this->waterHeightFadeTime, ANIM_LINEAR); |
---|
690 | this->waterHeightFader->addKeyFrame(this->newWaterHeight, 0, ANIM_LINEAR); |
---|
691 | |
---|
692 | bFadeWaterHeight = false; |
---|
693 | this->waterHeightFader->replay(); |
---|
694 | } |
---|
695 | |
---|
696 | if(bFadeWaterColor) |
---|
697 | { |
---|
698 | this->waterColorRFader = new tAnimation<MappedWater>(this, &MappedWater::resetWaterColorR); |
---|
699 | this->waterColorRFader->setInfinity(ANIM_INF_CONSTANT); |
---|
700 | |
---|
701 | this->waterColorRFader->addKeyFrame(this->waterColor.x, this->waterColorFadeTime, ANIM_LINEAR); |
---|
702 | this->waterColorRFader->addKeyFrame(this->newWaterColor.x, 0, ANIM_LINEAR); |
---|
703 | |
---|
704 | this->waterColorRFader->replay(); |
---|
705 | |
---|
706 | this->waterColorGFader = new tAnimation<MappedWater>(this, &MappedWater::resetWaterColorG); |
---|
707 | this->waterColorGFader->setInfinity(ANIM_INF_CONSTANT); |
---|
708 | |
---|
709 | this->waterColorGFader->addKeyFrame(this->waterColor.y, this->waterColorFadeTime, ANIM_LINEAR); |
---|
710 | this->waterColorGFader->addKeyFrame(this->newWaterColor.y, 0, ANIM_LINEAR); |
---|
711 | |
---|
712 | this->waterColorGFader->replay(); |
---|
713 | |
---|
714 | this->waterColorBFader = new tAnimation<MappedWater>(this, &MappedWater::resetWaterColorB); |
---|
715 | this->waterColorBFader->setInfinity(ANIM_INF_CONSTANT); |
---|
716 | |
---|
717 | this->waterColorBFader->addKeyFrame(this->waterColor.z, this->waterColorFadeTime, ANIM_LINEAR); |
---|
718 | this->waterColorBFader->addKeyFrame(this->newWaterColor.z, 0, ANIM_LINEAR); |
---|
719 | |
---|
720 | this->waterColorBFader->replay(); |
---|
721 | |
---|
722 | bFadeWaterColor = false; |
---|
723 | } |
---|
724 | } |
---|
725 | |
---|
726 | /** |
---|
727 | * @brief prepares everything to render the reflection texutre |
---|
728 | */ |
---|
729 | void MappedWater::activateReflection() |
---|
730 | { |
---|
731 | // To create the reflection texture we just need to set the view port |
---|
732 | // to our texture map size, then render the current scene our camera |
---|
733 | // is looking at to the already allocated texture unit. Since this |
---|
734 | // is a reflection of the top of the water surface we use clipping |
---|
735 | // planes to only render the top of the world as a reflection. If |
---|
736 | // we are below the water we don't flip the reflection but just use |
---|
737 | // the current view of the top as we are seeing through the water. |
---|
738 | // When you look through water at the surface it isn't really reflected, |
---|
739 | // only when looking down from above the water on the surface. |
---|
740 | |
---|
741 | // save viewport matrix and change the viewport size |
---|
742 | glPushAttrib(GL_VIEWPORT_BIT); |
---|
743 | glViewport(0,0, textureSize, textureSize); |
---|
744 | |
---|
745 | glMatrixMode(GL_MODELVIEW); |
---|
746 | glPushMatrix(); |
---|
747 | |
---|
748 | // If our camera is above the water we will render the scene flipped upside down. |
---|
749 | // In order to line up the reflection nicely with the world we have to translate |
---|
750 | // the world to the position of our reflected surface, multiplied by two. |
---|
751 | glEnable(GL_CLIP_PLANE0); |
---|
752 | Vector pos = State::getCameraNode()->getAbsCoor(); |
---|
753 | |
---|
754 | if(pos.y > waterHeight) |
---|
755 | { |
---|
756 | // Translate the world, then flip it upside down |
---|
757 | glTranslatef(0, waterHeight * 2, 0); |
---|
758 | glScalef(1, -1, 1); |
---|
759 | |
---|
760 | // Since the world is updside down we need to change the culling to FRONT |
---|
761 | glCullFace(GL_FRONT); |
---|
762 | |
---|
763 | // Set our plane equation and turn clipping on |
---|
764 | double plane[4] = {0, 1, 0, -waterHeight}; |
---|
765 | glClipPlane(GL_CLIP_PLANE0, plane); |
---|
766 | } |
---|
767 | else |
---|
768 | { |
---|
769 | // If the camera is below the water we don't want to flip the world, |
---|
770 | // but just render it clipped so only the top is drawn. |
---|
771 | double plane[4] = {0, 1, 0, waterHeight}; |
---|
772 | glClipPlane(GL_CLIP_PLANE0, plane); |
---|
773 | } |
---|
774 | } |
---|
775 | |
---|
776 | /** |
---|
777 | * @brief ends the reflection and saves the graphic buffer into a texture |
---|
778 | */ |
---|
779 | void MappedWater::deactivateReflection() |
---|
780 | { |
---|
781 | glDisable(GL_CLIP_PLANE0); |
---|
782 | glCullFace(GL_BACK); |
---|
783 | |
---|
784 | // Create the texture and store it on the video card |
---|
785 | mat.renderToTexture(0, GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize); |
---|
786 | |
---|
787 | glPopMatrix(); |
---|
788 | glPopAttrib(); |
---|
789 | } |
---|
790 | |
---|
791 | /** |
---|
792 | * @brief prepares everything to render the refraction texutre |
---|
793 | */ |
---|
794 | void MappedWater::activateRefraction() |
---|
795 | { |
---|
796 | // To create the refraction and depth textures we do the same thing |
---|
797 | // we did for the reflection texture, except we don't need to turn |
---|
798 | // the world upside down. We want to find the depth of the water, |
---|
799 | // not the depth of the sky and above water terrain. |
---|
800 | |
---|
801 | // save viewport matrix and change the viewport size |
---|
802 | glPushAttrib(GL_VIEWPORT_BIT); |
---|
803 | glViewport(0,0, textureSize, textureSize); |
---|
804 | |
---|
805 | glMatrixMode(GL_MODELVIEW); |
---|
806 | glPushMatrix(); |
---|
807 | |
---|
808 | // If our camera is above the water we will render only the parts that |
---|
809 | // are under the water. If the camera is below the water we render |
---|
810 | // only the stuff above the water. Like the reflection texture, we |
---|
811 | // incorporate clipping planes to only render what we need. |
---|
812 | |
---|
813 | // If the camera is above water, render the data below the water |
---|
814 | glEnable(GL_CLIP_PLANE0); |
---|
815 | Vector pos = State::getCameraNode()->getAbsCoor(); |
---|
816 | if(pos.y > waterHeight) |
---|
817 | { |
---|
818 | double plane[4] = {0, -1, 0, waterHeight}; |
---|
819 | glClipPlane(GL_CLIP_PLANE0, plane); |
---|
820 | } |
---|
821 | // If the camera is below the water, only render the data above the water |
---|
822 | else |
---|
823 | { |
---|
824 | glCullFace(GL_FRONT); |
---|
825 | double plane[4] = {0, 1, 0, -waterHeight}; |
---|
826 | glClipPlane(GL_CLIP_PLANE0, plane); |
---|
827 | } |
---|
828 | } |
---|
829 | |
---|
830 | /** |
---|
831 | * @brief ends the refraction and saves the graphic buffer into a texture |
---|
832 | */ |
---|
833 | void MappedWater::deactivateRefraction() |
---|
834 | { |
---|
835 | glDisable(GL_CLIP_PLANE0); |
---|
836 | glCullFace(GL_BACK); |
---|
837 | |
---|
838 | // Create the texture and store it on the video card |
---|
839 | mat.renderToTexture(1, GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize); |
---|
840 | |
---|
841 | glPopMatrix(); |
---|
842 | glPopAttrib(); |
---|
843 | } |
---|