Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/environments/mapped_water.cc @ 10152

Last change on this file since 10152 was 10114, checked in by patrick, 18 years ago

merged network back to trunk

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