Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/mountain_lake/src/world_entities/environments/mapped_water.cc @ 8919

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

mountain_lake: waterColor fading compiles, but doesnt work yet

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