Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain.older/src/world_entities/environments/mapped_water.cc @ 10635

Last change on this file since 10635 was 9110, checked in by bensch, 18 years ago

orxonox/trunk: merged the Presentation back

File size: 30.0 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  PRINTF(0)("\nMappedWater XML Code:\n<MappedWater>\n  <waterpos>%f, %f, %f</waterpos>\n  <watersize>%f, %f</watersize>\n  <wateruv>%f</wateruv>\n  <waterflow>%f</waterflow>\n  <lightpos>%f, %f, %f</lightpos>\n  <waterangle>%f</waterangle>\n  <normalmapscale>%f</normalmapscale>\n  <shinesize>%f</waterpos>\n  <shinestrength>%f</shinestrength>\n  <reflstrength>%f</reflstrength>\n  <refraction>%f</refraction>\n  <watercolor>%f, %f, %f</watercolor>\n</MappedWater>\n", this->waterVerts[0], this->waterHeight, this->waterVerts[1], this->xWidth, this->zWidth, this->waterUV, this->waterFlow, this->lightPos.x, this->lightPos.y, this->lightPos.z, this->waterAngle, this->kNormalMapScale, this->shineSize, this->shineStrength, this->reflStrength, this->refraction, this->waterColor.x, this->waterColor.y, this->waterColor.z);
340}
341
342/**
343 * @brief starts the slider gui that lets you edit all water parameters
344 */
345void MappedWater::toggleGui()
346{
347  if (this->box == NULL)
348  {
349    this->box = new OrxGui::GLGuiBox(OrxGui::Vertical);
350    {
351      OrxGui::GLGuiBox* waterColorBox = new OrxGui::GLGuiBox(OrxGui::Horizontal);
352      {
353        OrxGui::GLGuiText* waterColorText = new OrxGui::GLGuiText();
354        waterColorText->setText("WaterColor");
355        waterColorBox->pack(waterColorText);
356
357        OrxGui::GLGuiSlider* waterColorR = new OrxGui::GLGuiSlider();
358        waterColorR->setRange(0, 1.0f);
359        waterColorR->setValue(this->waterColor.x);
360        waterColorR->setStep(0.1f);
361        waterColorR->connect(SIGNAL(waterColorR, valueChanged), this, SLOT(MappedWater, resetWaterColorR));
362        waterColorBox->pack(waterColorR);
363
364        OrxGui::GLGuiSlider* waterColorG = new OrxGui::GLGuiSlider();
365        waterColorG->setRange(0, 1.0f);
366        waterColorG->setStep(0.1f);
367        waterColorG->setValue(this->waterColor.y);
368        waterColorG->connect(SIGNAL(waterColorG, valueChanged), this, SLOT(MappedWater, resetWaterColorG));
369        waterColorBox->pack(waterColorG);
370
371        OrxGui::GLGuiSlider* waterColorB = new OrxGui::GLGuiSlider();
372        waterColorB->setRange(0, 1.0f);
373        waterColorB->setStep(0.1f);
374        waterColorB->setValue(this->waterColor.z);
375        waterColorB->connect(SIGNAL(waterColorB, valueChanged), this, SLOT(MappedWater, resetWaterColorB));
376        waterColorBox->pack(waterColorB);
377      }
378      this->box->pack(waterColorBox);
379
380      OrxGui::GLGuiBox* waterUVBox = new OrxGui::GLGuiBox(OrxGui::Horizontal);
381      {
382        OrxGui::GLGuiText* waterUVText = new OrxGui::GLGuiText();
383        waterUVText->setText("WaterUV");
384        waterUVBox->pack(waterUVText);
385
386        OrxGui::GLGuiSlider* waterUV = new OrxGui::GLGuiSlider();
387        waterUV->setRange(1, 20);
388        waterUV->setValue(this->waterUV);
389        waterUV->setStep(1);
390        waterUV->connect(SIGNAL(waterUV, valueChanged), this, SLOT(MappedWater, setWaterUV));
391        waterUVBox->pack(waterUV);
392      }
393      this->box->pack(waterUVBox);
394
395      OrxGui::GLGuiBox* waterFlowBox = new OrxGui::GLGuiBox(OrxGui::Horizontal);
396      {
397        OrxGui::GLGuiText* waterFlowText = new OrxGui::GLGuiText();
398        waterFlowText->setText("WaterFlow");
399        waterFlowBox->pack(waterFlowText);
400
401        OrxGui::GLGuiSlider* waterFlow = new OrxGui::GLGuiSlider();
402        waterFlow->setRange(0.01f, 2);
403        waterFlow->setValue(this->waterFlow);
404        waterFlow->setStep(0.02f);
405        waterFlow->connect(SIGNAL(waterFlow, valueChanged), this, SLOT(MappedWater, setWaterFlow));
406        waterFlowBox->pack(waterFlow);
407      }
408      this->box->pack(waterFlowBox);
409
410      OrxGui::GLGuiBox* shineSizeBox = new OrxGui::GLGuiBox(OrxGui::Horizontal);
411      {
412        OrxGui::GLGuiText* shineSizeText = new OrxGui::GLGuiText();
413        shineSizeText->setText("ShineSize");
414        shineSizeBox->pack(shineSizeText);
415
416        OrxGui::GLGuiSlider* shineSize = new OrxGui::GLGuiSlider();
417        shineSize->setRange(1, 128);
418        shineSize->setValue(this->shineSize);
419        shineSize->setStep(1);
420        shineSize->connect(SIGNAL(shineSize, valueChanged), this, SLOT(MappedWater, resetShineSize));
421        shineSizeBox->pack(shineSize);
422      }
423      this->box->pack(shineSizeBox);
424
425      OrxGui::GLGuiBox* shineStrengthBox = new OrxGui::GLGuiBox(OrxGui::Horizontal);
426      {
427        OrxGui::GLGuiText* shineStrengthText = new OrxGui::GLGuiText();
428        shineStrengthText->setText("ShineStrength");
429        shineStrengthBox->pack(shineStrengthText);
430
431        OrxGui::GLGuiSlider* shineStrength = new OrxGui::GLGuiSlider();
432        shineStrength->setRange(0, 1);
433        shineStrength->setValue(this->shineStrength);
434        shineStrength->setStep(0.1f);
435        shineStrength->connect(SIGNAL(shineStrength, valueChanged), this, SLOT(MappedWater, resetShineStrength));
436        shineStrengthBox->pack(shineStrength);
437      }
438      this->box->pack(shineStrengthBox);
439
440      OrxGui::GLGuiBox* reflStrengthBox = new OrxGui::GLGuiBox(OrxGui::Horizontal);
441      {
442        OrxGui::GLGuiText* reflStrengthText = new OrxGui::GLGuiText();
443        reflStrengthText->setText("ReflStrength");
444        reflStrengthBox->pack(reflStrengthText);
445
446        OrxGui::GLGuiSlider* reflStrength = new OrxGui::GLGuiSlider();
447        reflStrength->setRange(0, 1);
448        reflStrength->setValue(this->reflStrength);
449        reflStrength->setStep(0.1f);
450        reflStrength->connect(SIGNAL(reflStrength, valueChanged), this, SLOT(MappedWater, resetReflStrength));
451        reflStrengthBox->pack(reflStrength);
452      }
453      this->box->pack(reflStrengthBox);
454
455      OrxGui::GLGuiBox* refractionBox = new OrxGui::GLGuiBox(OrxGui::Horizontal);
456      {
457        OrxGui::GLGuiText* refractionText = new OrxGui::GLGuiText();
458        refractionText->setText("Refraction");
459        refractionBox->pack(refractionText);
460
461        OrxGui::GLGuiSlider* refraction = new OrxGui::GLGuiSlider();
462        refraction->setRange(0.001f, 0.1f);
463        refraction->setValue(this->refraction);
464        refraction->setStep(0.004f);
465        refraction->connect(SIGNAL(refraction, valueChanged), this, SLOT(MappedWater, resetRefraction));
466        refractionBox->pack(refraction);
467      }
468      this->box->pack(refractionBox);
469
470      OrxGui::GLGuiBox* lightPosBox = new OrxGui::GLGuiBox(OrxGui::Horizontal);
471      {
472        OrxGui::GLGuiText* lightPosText = new OrxGui::GLGuiText();
473        lightPosText->setText("LightPos");
474        lightPosBox->pack(lightPosText);
475
476        OrxGui::GLGuiSlider* lightPosX = new OrxGui::GLGuiSlider();
477        lightPosX->setRange(-600, 600);
478        lightPosX->setValue(this->lightPos.x);
479        lightPosX->setStep(15);
480        lightPosX->connect(SIGNAL(lightPosX, valueChanged), this, SLOT(MappedWater, resetLightPosX));
481        lightPosBox->pack(lightPosX);
482
483        OrxGui::GLGuiSlider* lightPosY = new OrxGui::GLGuiSlider();
484        lightPosY->setRange(-600, 600);
485        lightPosY->setStep(15);
486        lightPosY->setValue(this->lightPos.y);
487        lightPosY->connect(SIGNAL(lightPosY, valueChanged), this, SLOT(MappedWater, resetLightPosY));
488        lightPosBox->pack(lightPosY);
489
490        OrxGui::GLGuiSlider* lightPosZ = new OrxGui::GLGuiSlider();
491        lightPosZ->setRange(-600, 600);
492        lightPosZ->setStep(15);
493        lightPosZ->setValue(this->lightPos.z);
494        lightPosZ->connect(SIGNAL(lightPosZ, valueChanged), this, SLOT(MappedWater, resetLightPosZ));
495        lightPosBox->pack(lightPosZ);
496      }
497      this->box->pack(lightPosBox);
498
499      OrxGui::GLGuiBox* waterHeightBox = new OrxGui::GLGuiBox(OrxGui::Horizontal);
500      {
501        OrxGui::GLGuiText* waterHeightText = new OrxGui::GLGuiText();
502        waterHeightText->setText("WaterHeight");
503        waterHeightBox->pack(waterHeightText);
504
505        OrxGui::GLGuiSlider* waterHeight = new OrxGui::GLGuiSlider();
506        waterHeight->setRange(-500, 500);
507        waterHeight->setValue(this->waterHeight);
508        waterHeight->setStep(10);
509        waterHeight->connect(SIGNAL(waterHeight, valueChanged), this, SLOT(MappedWater, setWaterHeight));
510        waterHeightBox->pack(waterHeight);
511      }
512      this->box->pack(waterHeightBox);
513    }
514
515    this->box->showAll();
516    this->box->setAbsCoor2D(300, 40);
517    OrxGui::GLGuiHandler::getInstance()->activate();
518    OrxGui::GLGuiHandler::getInstance()->activateCursor();
519  }
520  else
521  {
522    OrxGui::GLGuiHandler::getInstance()->deactivate();
523    OrxGui::GLGuiHandler::getInstance()->deactivateCursor();
524    delete this->box;
525    this->box = NULL;
526  }
527}
528
529/**
530 * @brief activates the water shader and draws a quad with four textures on it
531 */
532void MappedWater::draw() const
533{
534  glMatrixMode(GL_MODELVIEW);
535  glPushMatrix();
536
537  // don't use glRotate or glTranslate here... the shader won't work anymore
538
539  mat.select();
540
541  this->shader->activateShader();
542
543  // reset the camera uniform to the current cam position
544  Vector pos = State::getCameraNode()->getAbsCoor();
545  cam_uni->set(pos.x, pos.y, pos.z, 1.0f);
546
547  glDisable(GL_BLEND);
548
549  glBegin(GL_QUADS);
550  // The back left vertice for the water
551  glMultiTexCoord2f(GL_TEXTURE0, 0, waterUV);                   // Reflection texture
552  glMultiTexCoord2f(GL_TEXTURE1, 0, waterUV - move);            // Refraction texture
553  glMultiTexCoord2f(GL_TEXTURE2, 0, normalUV + move2);          // Normal map texture
554  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                         // DUDV map texture
555  glVertex3f(this->waterVerts[0], this->waterHeight, this->waterVerts[1]);
556
557  // The front left vertice for the water
558  glMultiTexCoord2f(GL_TEXTURE0, 0, 0);                         // Reflection texture
559  glMultiTexCoord2f(GL_TEXTURE1, 0, -move);                     // Refraction texture
560  glMultiTexCoord2f(GL_TEXTURE2, 0, move2);                     // Normal map texture
561  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                         // DUDV map texture
562  glVertex3f(this->waterVerts[2], this->waterHeight, this->waterVerts[3]);
563
564  // The front right vertice for the water
565  glMultiTexCoord2f(GL_TEXTURE0, waterUV, 0);                   // Reflection texture
566  glMultiTexCoord2f(GL_TEXTURE1, waterUV, -move);               // Refraction texture
567  glMultiTexCoord2f(GL_TEXTURE2, normalUV, move2);              // Normal map texture
568  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                         // DUDV map texture
569  glVertex3f(this->waterVerts[4], this->waterHeight, this->waterVerts[5]);
570
571  // The back right vertice for the water
572  glMultiTexCoord2f(GL_TEXTURE0, waterUV, waterUV);             // Reflection texture
573  glMultiTexCoord2f(GL_TEXTURE1, waterUV, waterUV - move);      // Refraction texture
574  glMultiTexCoord2f(GL_TEXTURE2, normalUV, normalUV + move2);   // Normal map texture
575  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                         // DUDV map texture
576  glVertex3f(this->waterVerts[6], this->waterHeight, this->waterVerts[7]);
577  glEnd();
578
579  this->shader->deactivateShader();
580
581  mat.unselect();
582
583  glPopMatrix();
584}
585
586/**
587 * @brief tick tack, calculates the flow of the water
588 */
589void MappedWater::tick(float dt)
590{
591  // makes the water flow
592  this->move += this->waterFlow * dt;
593  this->move2 = this->move * this->kNormalMapScale;
594
595  // fading TODO fix this so it isnt hacky anymore
596  if(bFadeWaterUV)
597  {
598    this->waterUVFader = new tAnimation<MappedWater>(this, &MappedWater::setWaterUV);
599    this->waterUVFader->setInfinity(ANIM_INF_CONSTANT);
600
601    this->waterUVFader->addKeyFrame(this->waterUV, this->waterUVFadeTime, ANIM_LINEAR);
602    this->waterUVFader->addKeyFrame(this->newWaterUV, 0, ANIM_LINEAR);
603
604    bFadeWaterUV = false;
605    this->waterUVFader->replay();
606  }
607
608  if(bFadeWaterFlow)
609  {
610    this->waterFlowFader = new tAnimation<MappedWater>(this, &MappedWater::setWaterFlow);
611    this->waterFlowFader->setInfinity(ANIM_INF_CONSTANT);
612
613    this->waterFlowFader->addKeyFrame(this->waterFlow, this->waterFlowFadeTime, ANIM_LINEAR);
614    this->waterFlowFader->addKeyFrame(this->newWaterFlow, 0, ANIM_LINEAR);
615
616    bFadeWaterFlow = false;
617    this->waterFlowFader->replay();
618  }
619
620  if(bFadeShineSize)
621  {
622    this->shineSizeFader = new tAnimation<MappedWater>(this, &MappedWater::resetShineSize);
623    this->shineSizeFader->setInfinity(ANIM_INF_CONSTANT);
624
625    this->shineSizeFader->addKeyFrame(this->shineSize, this->shineSizeFadeTime, ANIM_LINEAR);
626    this->shineSizeFader->addKeyFrame(this->newShineSize, 0, ANIM_LINEAR);
627
628    bFadeShineSize = false;
629    this->shineSizeFader->replay();
630  }
631
632  if(bFadeShineStrength)
633  {
634    this->shineStrengthFader = new tAnimation<MappedWater>(this, &MappedWater::resetShineStrength);
635    this->shineStrengthFader->setInfinity(ANIM_INF_CONSTANT);
636
637    this->shineStrengthFader->addKeyFrame(this->shineStrength, this->shineStrengthFadeTime, ANIM_LINEAR);
638    this->shineStrengthFader->addKeyFrame(this->newShineStrength, 0, ANIM_LINEAR);
639
640    bFadeShineStrength = false;
641    this->shineStrengthFader->replay();
642  }
643
644  if(bFadeReflStrength)
645  {
646    this->reflStrengthFader = new tAnimation<MappedWater>(this, &MappedWater::resetReflStrength);
647    this->reflStrengthFader->setInfinity(ANIM_INF_CONSTANT);
648
649    this->reflStrengthFader->addKeyFrame(this->reflStrength, this->reflStrengthFadeTime, ANIM_LINEAR);
650    this->reflStrengthFader->addKeyFrame(this->newReflStrength, 0, ANIM_LINEAR);
651
652    bFadeReflStrength = false;
653    this->reflStrengthFader->replay();
654  }
655
656  if(bFadeRefraction)
657  {
658    this->refractionFader = new tAnimation<MappedWater>(this, &MappedWater::resetRefraction);
659    this->refractionFader->setInfinity(ANIM_INF_CONSTANT);
660
661    this->refractionFader->addKeyFrame(this->refraction, this->refractionFadeTime, ANIM_LINEAR);
662    this->refractionFader->addKeyFrame(this->newRefraction, 0, ANIM_LINEAR);
663
664    bFadeRefraction = false;
665    this->refractionFader->replay();
666  }
667
668  if(bFadeWaterHeight)
669  {
670    this->waterHeightFader = new tAnimation<MappedWater>(this, &MappedWater::setWaterHeight);
671    this->waterHeightFader->setInfinity(ANIM_INF_CONSTANT);
672
673    this->waterHeightFader->addKeyFrame(this->waterHeight, this->waterHeightFadeTime, ANIM_LINEAR);
674    this->waterHeightFader->addKeyFrame(this->newWaterHeight, 0, ANIM_LINEAR);
675
676    bFadeWaterHeight = false;
677    this->waterHeightFader->replay();
678  }
679
680  if(bFadeWaterColor)
681  {
682    this->waterColorRFader = new tAnimation<MappedWater>(this, &MappedWater::resetWaterColorR);
683    this->waterColorRFader->setInfinity(ANIM_INF_CONSTANT);
684
685    this->waterColorRFader->addKeyFrame(this->waterColor.x, this->waterColorFadeTime, ANIM_LINEAR);
686    this->waterColorRFader->addKeyFrame(this->newWaterColor.x, 0, ANIM_LINEAR);
687
688    this->waterColorRFader->replay();
689
690    this->waterColorGFader = new tAnimation<MappedWater>(this, &MappedWater::resetWaterColorG);
691    this->waterColorGFader->setInfinity(ANIM_INF_CONSTANT);
692
693    this->waterColorGFader->addKeyFrame(this->waterColor.y, this->waterColorFadeTime, ANIM_LINEAR);
694    this->waterColorGFader->addKeyFrame(this->newWaterColor.y, 0, ANIM_LINEAR);
695
696    this->waterColorGFader->replay();
697
698    this->waterColorBFader = new tAnimation<MappedWater>(this, &MappedWater::resetWaterColorB);
699    this->waterColorBFader->setInfinity(ANIM_INF_CONSTANT);
700
701    this->waterColorBFader->addKeyFrame(this->waterColor.z, this->waterColorFadeTime, ANIM_LINEAR);
702    this->waterColorBFader->addKeyFrame(this->newWaterColor.z, 0, ANIM_LINEAR);
703
704    this->waterColorBFader->replay();
705
706    bFadeWaterColor = false;
707  }
708}
709
710/**
711 * @brief prepares everything to render the reflection texutre
712 */
713void MappedWater::activateReflection()
714{
715  // To create the reflection texture we just need to set the view port
716  // to our texture map size, then render the current scene our camera
717  // is looking at to the already allocated texture unit.  Since this
718  // is a reflection of the top of the water surface we use clipping
719  // planes to only render the top of the world as a reflection.  If
720  // we are below the water we don't flip the reflection but just use
721  // the current view of the top as we are seeing through the water.
722  // When you look through water at the surface it isn't really reflected,
723  // only when looking down from above the water on the surface.
724
725  // save viewport matrix and change the viewport size
726  glPushAttrib(GL_VIEWPORT_BIT);
727  glViewport(0,0, textureSize, textureSize);
728
729  glMatrixMode(GL_MODELVIEW);
730  glPushMatrix();
731
732  // If our camera is above the water we will render the scene flipped upside down.
733  // In order to line up the reflection nicely with the world we have to translate
734  // the world to the position of our reflected surface, multiplied by two.
735  glEnable(GL_CLIP_PLANE0);
736  Vector pos = State::getCameraNode()->getAbsCoor();
737
738  if(pos.y > waterHeight)
739  {
740    // Translate the world, then flip it upside down
741    glTranslatef(0, waterHeight * 2, 0);
742    glScalef(1, -1, 1);
743
744    // Since the world is updside down we need to change the culling to FRONT
745    glCullFace(GL_FRONT);
746
747    // Set our plane equation and turn clipping on
748    double plane[4] = {0, 1, 0, -waterHeight};
749    glClipPlane(GL_CLIP_PLANE0, plane);
750  }
751  else
752  {
753    // If the camera is below the water we don't want to flip the world,
754    // but just render it clipped so only the top is drawn.
755    double plane[4] = {0, 1, 0, waterHeight};
756    glClipPlane(GL_CLIP_PLANE0, plane);
757  }
758}
759
760/**
761 * @brief ends the reflection and saves the graphic buffer into a texture
762 */
763void MappedWater::deactivateReflection()
764{
765  glDisable(GL_CLIP_PLANE0);
766  glCullFace(GL_BACK);
767
768  // Create the texture and store it on the video card
769  mat.renderToTexture(0, GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
770
771  glPopMatrix();
772  glPopAttrib();
773}
774
775/**
776 * @brief prepares everything to render the refraction texutre
777 */
778void MappedWater::activateRefraction()
779{
780  // To create the refraction and depth textures we do the same thing
781  // we did for the reflection texture, except we don't need to turn
782  // the world upside down.  We want to find the depth of the water,
783  // not the depth of the sky and above water terrain.
784
785  // save viewport matrix and change the viewport size
786  glPushAttrib(GL_VIEWPORT_BIT);
787  glViewport(0,0, textureSize, textureSize);
788
789  glMatrixMode(GL_MODELVIEW);
790  glPushMatrix();
791
792  // If our camera is above the water we will render only the parts that
793  // are under the water.  If the camera is below the water we render
794  // only the stuff above the water.  Like the reflection texture, we
795  // incorporate clipping planes to only render what we need.
796
797  // If the camera is above water, render the data below the water
798  glEnable(GL_CLIP_PLANE0);
799  Vector pos = State::getCameraNode()->getAbsCoor();
800  if(pos.y > waterHeight)
801  {
802    double plane[4] = {0, -1, 0, waterHeight};
803    glClipPlane(GL_CLIP_PLANE0, plane);
804  }
805  // If the camera is below the water, only render the data above the water
806  else
807  {
808    glCullFace(GL_FRONT);
809    double plane[4] = {0, 1, 0, -waterHeight};
810    glClipPlane(GL_CLIP_PLANE0, plane);
811  }
812}
813
814/**
815 * @brief ends the refraction and saves the graphic buffer into a texture
816 */
817void MappedWater::deactivateRefraction()
818{
819  glDisable(GL_CLIP_PLANE0);
820  glCullFace(GL_BACK);
821
822  // Create the texture and store it on the video card
823  mat.renderToTexture(1, GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
824
825  glPopMatrix();
826  glPopAttrib();
827}
Note: See TracBrowser for help on using the repository browser.