Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/world_entities/environments/mapped_water.cc @ 9172

Last change on this file since 9172 was 9170, checked in by stefalie, 18 years ago

changed slider values for the water

File size: 30.2 KB
Line 
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
27CREATE_FACTORY(MappedWater, CL_MAPPED_WATER);
28
29SHELL_COMMAND(gui, MappedWater, toggleGui);
30SHELL_COMMAND(output, MappedWater, saveParams);
31
32CREATE_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 */
47MappedWater::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 */
75MappedWater::~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 */
90void 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 */
127void 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 */
162void 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 */
205void 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 */
223void 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 */
238void 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 */
253void 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 */
268void 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 */
283void 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 */
300void 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 */
315void 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 */
336void 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 */
360void 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(-500, 500);
522        waterHeight->setValue(this->waterHeight);
523        waterHeight->setStep(10);
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 */
547void 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  glBegin(GL_QUADS);
565  // The back left vertice for the water
566  glMultiTexCoord2f(GL_TEXTURE0, 0, waterUV);                   // Reflection texture
567  glMultiTexCoord2f(GL_TEXTURE1, 0, waterUV - move);            // Refraction texture
568  glMultiTexCoord2f(GL_TEXTURE2, 0, normalUV + move2);          // Normal map texture
569  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                         // DUDV map texture
570  glVertex3f(this->waterVerts[0], this->waterHeight, this->waterVerts[1]);
571
572  // The front left vertice for the water
573  glMultiTexCoord2f(GL_TEXTURE0, 0, 0);                         // Reflection texture
574  glMultiTexCoord2f(GL_TEXTURE1, 0, -move);                     // Refraction texture
575  glMultiTexCoord2f(GL_TEXTURE2, 0, move2);                     // Normal map texture
576  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                         // DUDV map texture
577  glVertex3f(this->waterVerts[2], this->waterHeight, this->waterVerts[3]);
578
579  // The front right vertice for the water
580  glMultiTexCoord2f(GL_TEXTURE0, waterUV, 0);                   // Reflection texture
581  glMultiTexCoord2f(GL_TEXTURE1, waterUV, -move);               // Refraction texture
582  glMultiTexCoord2f(GL_TEXTURE2, normalUV, move2);              // Normal map texture
583  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                         // DUDV map texture
584  glVertex3f(this->waterVerts[4], this->waterHeight, this->waterVerts[5]);
585
586  // The back right vertice for the water
587  glMultiTexCoord2f(GL_TEXTURE0, waterUV, waterUV);             // Reflection texture
588  glMultiTexCoord2f(GL_TEXTURE1, waterUV, waterUV - move);      // Refraction texture
589  glMultiTexCoord2f(GL_TEXTURE2, normalUV, normalUV + move2);   // Normal map texture
590  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                         // DUDV map texture
591  glVertex3f(this->waterVerts[6], this->waterHeight, this->waterVerts[7]);
592  glEnd();
593
594  this->shader->deactivateShader();
595
596  mat.unselect();
597
598  glPopMatrix();
599}
600
601/**
602 * @brief tick tack, calculates the flow of the water
603 */
604void MappedWater::tick(float dt)
605{
606  // makes the water flow
607  this->move += this->waterFlow * dt;
608  this->move2 = this->move * this->kNormalMapScale;
609
610  // fading TODO fix this so it isnt hacky anymore
611  if(bFadeWaterUV)
612  {
613    this->waterUVFader = new tAnimation<MappedWater>(this, &MappedWater::setWaterUV);
614    this->waterUVFader->setInfinity(ANIM_INF_CONSTANT);
615
616    this->waterUVFader->addKeyFrame(this->waterUV, this->waterUVFadeTime, ANIM_LINEAR);
617    this->waterUVFader->addKeyFrame(this->newWaterUV, 0, ANIM_LINEAR);
618
619    bFadeWaterUV = false;
620    this->waterUVFader->replay();
621  }
622
623  if(bFadeWaterFlow)
624  {
625    this->waterFlowFader = new tAnimation<MappedWater>(this, &MappedWater::setWaterFlow);
626    this->waterFlowFader->setInfinity(ANIM_INF_CONSTANT);
627
628    this->waterFlowFader->addKeyFrame(this->waterFlow, this->waterFlowFadeTime, ANIM_LINEAR);
629    this->waterFlowFader->addKeyFrame(this->newWaterFlow, 0, ANIM_LINEAR);
630
631    bFadeWaterFlow = false;
632    this->waterFlowFader->replay();
633  }
634
635  if(bFadeShineSize)
636  {
637    this->shineSizeFader = new tAnimation<MappedWater>(this, &MappedWater::resetShineSize);
638    this->shineSizeFader->setInfinity(ANIM_INF_CONSTANT);
639
640    this->shineSizeFader->addKeyFrame(this->shineSize, this->shineSizeFadeTime, ANIM_LINEAR);
641    this->shineSizeFader->addKeyFrame(this->newShineSize, 0, ANIM_LINEAR);
642
643    bFadeShineSize = false;
644    this->shineSizeFader->replay();
645  }
646
647  if(bFadeShineStrength)
648  {
649    this->shineStrengthFader = new tAnimation<MappedWater>(this, &MappedWater::resetShineStrength);
650    this->shineStrengthFader->setInfinity(ANIM_INF_CONSTANT);
651
652    this->shineStrengthFader->addKeyFrame(this->shineStrength, this->shineStrengthFadeTime, ANIM_LINEAR);
653    this->shineStrengthFader->addKeyFrame(this->newShineStrength, 0, ANIM_LINEAR);
654
655    bFadeShineStrength = false;
656    this->shineStrengthFader->replay();
657  }
658
659  if(bFadeReflStrength)
660  {
661    this->reflStrengthFader = new tAnimation<MappedWater>(this, &MappedWater::resetReflStrength);
662    this->reflStrengthFader->setInfinity(ANIM_INF_CONSTANT);
663
664    this->reflStrengthFader->addKeyFrame(this->reflStrength, this->reflStrengthFadeTime, ANIM_LINEAR);
665    this->reflStrengthFader->addKeyFrame(this->newReflStrength, 0, ANIM_LINEAR);
666
667    bFadeReflStrength = false;
668    this->reflStrengthFader->replay();
669  }
670
671  if(bFadeRefraction)
672  {
673    this->refractionFader = new tAnimation<MappedWater>(this, &MappedWater::resetRefraction);
674    this->refractionFader->setInfinity(ANIM_INF_CONSTANT);
675
676    this->refractionFader->addKeyFrame(this->refraction, this->refractionFadeTime, ANIM_LINEAR);
677    this->refractionFader->addKeyFrame(this->newRefraction, 0, ANIM_LINEAR);
678
679    bFadeRefraction = false;
680    this->refractionFader->replay();
681  }
682
683  if(bFadeWaterHeight)
684  {
685    this->waterHeightFader = new tAnimation<MappedWater>(this, &MappedWater::setWaterHeight);
686    this->waterHeightFader->setInfinity(ANIM_INF_CONSTANT);
687
688    this->waterHeightFader->addKeyFrame(this->waterHeight, this->waterHeightFadeTime, ANIM_LINEAR);
689    this->waterHeightFader->addKeyFrame(this->newWaterHeight, 0, ANIM_LINEAR);
690
691    bFadeWaterHeight = false;
692    this->waterHeightFader->replay();
693  }
694
695  if(bFadeWaterColor)
696  {
697    this->waterColorRFader = new tAnimation<MappedWater>(this, &MappedWater::resetWaterColorR);
698    this->waterColorRFader->setInfinity(ANIM_INF_CONSTANT);
699
700    this->waterColorRFader->addKeyFrame(this->waterColor.x, this->waterColorFadeTime, ANIM_LINEAR);
701    this->waterColorRFader->addKeyFrame(this->newWaterColor.x, 0, ANIM_LINEAR);
702
703    this->waterColorRFader->replay();
704
705    this->waterColorGFader = new tAnimation<MappedWater>(this, &MappedWater::resetWaterColorG);
706    this->waterColorGFader->setInfinity(ANIM_INF_CONSTANT);
707
708    this->waterColorGFader->addKeyFrame(this->waterColor.y, this->waterColorFadeTime, ANIM_LINEAR);
709    this->waterColorGFader->addKeyFrame(this->newWaterColor.y, 0, ANIM_LINEAR);
710
711    this->waterColorGFader->replay();
712
713    this->waterColorBFader = new tAnimation<MappedWater>(this, &MappedWater::resetWaterColorB);
714    this->waterColorBFader->setInfinity(ANIM_INF_CONSTANT);
715
716    this->waterColorBFader->addKeyFrame(this->waterColor.z, this->waterColorFadeTime, ANIM_LINEAR);
717    this->waterColorBFader->addKeyFrame(this->newWaterColor.z, 0, ANIM_LINEAR);
718
719    this->waterColorBFader->replay();
720
721    bFadeWaterColor = false;
722  }
723}
724
725/**
726 * @brief prepares everything to render the reflection texutre
727 */
728void MappedWater::activateReflection()
729{
730  // To create the reflection texture we just need to set the view port
731  // to our texture map size, then render the current scene our camera
732  // is looking at to the already allocated texture unit.  Since this
733  // is a reflection of the top of the water surface we use clipping
734  // planes to only render the top of the world as a reflection.  If
735  // we are below the water we don't flip the reflection but just use
736  // the current view of the top as we are seeing through the water.
737  // When you look through water at the surface it isn't really reflected,
738  // only when looking down from above the water on the surface.
739
740  // save viewport matrix and change the viewport size
741  glPushAttrib(GL_VIEWPORT_BIT);
742  glViewport(0,0, textureSize, textureSize);
743
744  glMatrixMode(GL_MODELVIEW);
745  glPushMatrix();
746
747  // If our camera is above the water we will render the scene flipped upside down.
748  // In order to line up the reflection nicely with the world we have to translate
749  // the world to the position of our reflected surface, multiplied by two.
750  glEnable(GL_CLIP_PLANE0);
751  Vector pos = State::getCameraNode()->getAbsCoor();
752
753  if(pos.y > waterHeight)
754  {
755    // Translate the world, then flip it upside down
756    glTranslatef(0, waterHeight * 2, 0);
757    glScalef(1, -1, 1);
758
759    // Since the world is updside down we need to change the culling to FRONT
760    glCullFace(GL_FRONT);
761
762    // Set our plane equation and turn clipping on
763    double plane[4] = {0, 1, 0, -waterHeight};
764    glClipPlane(GL_CLIP_PLANE0, plane);
765  }
766  else
767  {
768    // If the camera is below the water we don't want to flip the world,
769    // but just render it clipped so only the top is drawn.
770    double plane[4] = {0, 1, 0, waterHeight};
771    glClipPlane(GL_CLIP_PLANE0, plane);
772  }
773}
774
775/**
776 * @brief ends the reflection and saves the graphic buffer into a texture
777 */
778void MappedWater::deactivateReflection()
779{
780  glDisable(GL_CLIP_PLANE0);
781  glCullFace(GL_BACK);
782
783  // Create the texture and store it on the video card
784  mat.renderToTexture(0, GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
785
786  glPopMatrix();
787  glPopAttrib();
788}
789
790/**
791 * @brief prepares everything to render the refraction texutre
792 */
793void MappedWater::activateRefraction()
794{
795  // To create the refraction and depth textures we do the same thing
796  // we did for the reflection texture, except we don't need to turn
797  // the world upside down.  We want to find the depth of the water,
798  // not the depth of the sky and above water terrain.
799
800  // save viewport matrix and change the viewport size
801  glPushAttrib(GL_VIEWPORT_BIT);
802  glViewport(0,0, textureSize, textureSize);
803
804  glMatrixMode(GL_MODELVIEW);
805  glPushMatrix();
806
807  // If our camera is above the water we will render only the parts that
808  // are under the water.  If the camera is below the water we render
809  // only the stuff above the water.  Like the reflection texture, we
810  // incorporate clipping planes to only render what we need.
811
812  // If the camera is above water, render the data below the water
813  glEnable(GL_CLIP_PLANE0);
814  Vector pos = State::getCameraNode()->getAbsCoor();
815  if(pos.y > waterHeight)
816  {
817    double plane[4] = {0, -1, 0, waterHeight};
818    glClipPlane(GL_CLIP_PLANE0, plane);
819  }
820  // If the camera is below the water, only render the data above the water
821  else
822  {
823    glCullFace(GL_FRONT);
824    double plane[4] = {0, 1, 0, -waterHeight};
825    glClipPlane(GL_CLIP_PLANE0, plane);
826  }
827}
828
829/**
830 * @brief ends the refraction and saves the graphic buffer into a texture
831 */
832void MappedWater::deactivateRefraction()
833{
834  glDisable(GL_CLIP_PLANE0);
835  glCullFace(GL_BACK);
836
837  // Create the texture and store it on the video card
838  mat.renderToTexture(1, GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
839
840  glPopMatrix();
841  glPopAttrib();
842}
Note: See TracBrowser for help on using the repository browser.