Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/water/src/world_entities/environments/mapped_water.cc @ 8788

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

water: param reset functions work, the fade functions are next

File size: 13.6 KB
RevLine 
[7796]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"
[8037]20#include "state.h"
[8729]21#include "math.h"
[7796]22
23
24CREATE_FACTORY(MappedWater, CL_MAPPED_WATER);
25
[8719]26/**
27 * @brief constructor
28 * @param root xml data
29 */
[7796]30MappedWater::MappedWater(const TiXmlElement* root)
31{
32  this->setClassID(CL_MAPPED_WATER, "MappedWater");
33  this->toList(OM_ENVIRON);
34
[8719]35  /// sets start values and parameters
36  this->initParams();
37  // now the standard values were loaded, if the values are specified in the .oxw file
38  // loadParams will overwrite the standard values with the specified values
[7796]39  if (root != NULL)
40    this->loadParams(root);
41
[8719]42  /// initialization of the textures
43  this->initTextures();
[8037]44
[8719]45  /// initialization of the shaders
46  this->initShaders();
[8788]47 
48  tempcounter = 0;
[8719]49}
50
51/**
52 * @brief deltes shader and the uniform used by the camera
53 */
54MappedWater::~MappedWater()
55{
56  delete shader;
57  delete cam_uni;
[8729]58  delete color_uni;
59  delete light_uni;
60  delete shine_uni;
[8719]61}
62
63/**
64 * @brief initialization of loadable parameters, sets start standard values
65 */
66void MappedWater::initParams()
67{
68  // those standardvalues will be overwritten if they're also set in the oxw file
69  this->setWaterPos(0, 0, 0);
70  this->setWaterSize(100, 100);
71  this->setWaterUV(9);
72  this->setWaterFlow(0.08);
73  this->setLightPos(0, 10, 0);
74  this->setWaterAngle(0);
75  this->setNormalMapScale(0.25f);
[8729]76  this->setWaterColor(0.1f, 0.2f, 0.4f);
77  this->setShininess(128);
[8719]78
79  // initialization of the texture coords, speeds etc...
80  // normalUV wont change anymore
81  this->normalUV = this->waterUV * this->kNormalMapScale;
82  // move and move2 are used for the reflection and refraction, the values are changed in tick()
83  this->move = 0;
84  this->move2 = this->move * this->kNormalMapScale;
[8729]85
86
[8719]87}
88
89/**
90 * @brief initialization of the textures
91 */
92void MappedWater::initTextures()
93{
94  // sets parameters for the textures
95  this->textureSize = 512;
96  unsigned int channels = 32;
97  GLenum type = GL_RGBA;
98
[7796]99  // set up refleciton texture
[8719]100  Texture reflTex(GL_TEXTURE_2D, this->textureSize, this->textureSize, channels, type);
101  mat.setDiffuseMap(reflTex, 0);
102  // set up refraction texture
103  Texture refrTex(GL_TEXTURE_2D, this->textureSize, this->textureSize, channels, type);
104  mat.setDiffuseMap(refrTex, 1);
[7796]105  // load normal map
[8312]106  mat.setDiffuseMap("pictures/normalmap.bmp", GL_TEXTURE_2D, 2);
[7796]107  // load dudv map
[8312]108  mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 3);
[7796]109
[8719]110  // sets texture parameters for reflection texture
[8619]111  glBindTexture(GL_TEXTURE_2D, this->mat.diffuseTextureID(0));
[8312]112  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
113  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
114  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
115  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
[8719]116  // sets texture parameters for refraction texture
[8619]117  glBindTexture(GL_TEXTURE_2D, this->mat.diffuseTextureID(1));
[7796]118  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
119  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
120  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
121  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
[8719]122}
[7796]123
[8719]124/**
125 * @brief initialization of the shaders
126 */
127void MappedWater::initShaders()
128{
[8037]129  // load shader files
[8312]130  shader = new Shader( ResourceManager::getInstance()->getDataDir() + "/shaders/mapped_water.vert", ResourceManager::getInstance()->getDataDir() +"/shaders/mapped_water.frag");
[8037]131
132  this->shader->activateShader();
133  // Set the variable "reflection" to correspond to the first texture unit
134  Shader::Uniform(shader, "reflection").set(0);
135  // Set the variable "refraction" to correspond to the second texture unit
136  Shader::Uniform(shader, "refraction").set(1);
137  // Set the variable "normalMap" to correspond to the third texture unit
138  Shader::Uniform(shader, "normalMap").set(2);
139  // Set the variable "dudvMap" to correspond to the fourth texture unit
140  Shader::Uniform(shader, "dudvMap").set(3);
141  // Set the variable "depthMap" to correspond to the fifth texture unit
[8312]142  Shader::Uniform(shader, "depthMap").set(2);
[8037]143  // Give the variable "waterColor" a blue color
[8729]144  color_uni = new Shader::Uniform(shader, "waterColor");
145  color_uni->set(waterColor.x, waterColor.y, waterColor.z, 1.0f);
[8037]146  // Give the variable "lightPos" our hard coded light position
[8729]147  light_uni = new Shader::Uniform(shader, "lightPos");
148  light_uni->set(lightPos.x, lightPos.y, lightPos.z, 1.0f);
149  // Set the variable "shine"
150  shine_uni = new Shader::Uniform(shader, "kShine");
151  shine_uni->set(this->shininess);
[8037]152  // uniform for the camera position
153  cam_uni = new Shader::Uniform(shader, "cameraPos");
154
[8312]155  this->shader->deactivateShader();
[7796]156}
157
[8719]158/**
[8788]159 * @brief resets the waterColor in the Shader
160 * @param r new value for red
161 * @param g new value for green
162 * @param b new value for blue
163 */
164void MappedWater::resetWaterColor(float r, float g, float b)
165{
166  this->shader->activateShader();
167  this->waterColor = Vector(r, g, b);
168
169  // Give the variable "waterColor" a color
170  color_uni->set(waterColor.x, waterColor.y, waterColor.z, 1.0f);
171
172  this->shader->deactivateShader();
173};
174
175/**
176 * @brief resets the shininess in the Shader
177 * @param shine new value for the shininess
178 */
179void MappedWater::resetShininess(float shine)
180{
181  this->shader->activateShader();
182  this->shininess = shine;
183
184  // Set the variable "shine"
185  shine_uni->set(this->shininess);
186
187  this->shader->deactivateShader();
188};
189
190/**
191 * @brief resets the lightPos in the Shader
192 * @param x new x value
193 * @param y new y value
194 * @param z new z value
195 */
196void MappedWater::resetLightPos(float x, float y, float z)
197{
198  this->shader->activateShader();
199  this->lightPos = Vector(x, y, z);
200
201  // Give the variable "lightPos" our hard coded light position
202  light_uni->set(lightPos.x, lightPos.y, lightPos.z, 1.0f);
203
204  this->shader->deactivateShader();
205};
206
207/**
[8719]208 * @brief ends the refraction and saves the graphic buffer into a texture
209 * @param root xml data
210 */
[7796]211void MappedWater::loadParams(const TiXmlElement* root)
212{
213  WorldEntity::loadParams(root);
214
[8719]215  LoadParam(root, "waterpos", this, MappedWater, setWaterPos);
216  LoadParam(root, "watersize", this, MappedWater, setWaterSize);
217  LoadParam(root, "lightpos", this, MappedWater, setLightPos);
218  LoadParam(root, "wateruv", this, MappedWater, setWaterUV);
219  LoadParam(root, "waterflow", this, MappedWater, setWaterFlow);
220  LoadParam(root, "normalmapscale", this, MappedWater, setNormalMapScale);
221  LoadParam(root, "waterangle", this, MappedWater, setWaterAngle);
[8729]222  LoadParam(root, "watercolor", this, MappedWater, setWaterColor);
223  LoadParam(root, "shininess", this, MappedWater, setShininess);
[7796]224}
225
[8719]226/**
227 * @brief activates the water shader and draws a quad with four textures on it
228 */
[7796]229void MappedWater::draw() const
230{
[8312]231  glMatrixMode(GL_MODELVIEW);
232
[7796]233  glPushMatrix();
[8719]234  // don't use a glTranslate here, the reflection point won't be at the right place anymore
235  glRotatef(this->waterAngle, 0, 1, 0);
[7796]236
237  mat.select();
238
[8312]239  this->shader->activateShader();
[7796]240
[8037]241  // reset the camera uniform to the current cam position
242  Vector pos = State::getCameraNode()->getAbsCoor();
[8312]243  cam_uni->set(pos.x, pos.y, pos.z, 1.0f);
[7796]244
[8719]245  glDisable(GL_BLEND);
[8312]246
[8037]247  glBegin(GL_QUADS);
248  // The back left vertice for the water
[8719]249  glMultiTexCoord2f(GL_TEXTURE0, 0, waterUV);            // Reflection texture
250  glMultiTexCoord2f(GL_TEXTURE1, 0, waterUV - move);        // Refraction texture
251  glMultiTexCoord2f(GL_TEXTURE2, 0, normalUV + move2);     // Normal map texture
[8037]252  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                       // DUDV map texture
[8719]253  glVertex3f(this->waterPos.x, this->waterPos.y, this->waterPos.z);
[7796]254
[8037]255  // The front left vertice for the water
[8719]256  glMultiTexCoord2f(GL_TEXTURE0, 0, 0);                  // Reflection texture
257  glMultiTexCoord2f(GL_TEXTURE1, 0, -move);           // Refraction texture
258  glMultiTexCoord2f(GL_TEXTURE2, 0, move2);          // Normal map texture
[8037]259  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                        // DUDV map texture
[8719]260  glVertex3f(this->waterPos.x, this->waterPos.y, this->waterPos.z + this->zWidth);
[7796]261
[8037]262  // The front right vertice for the water
[8719]263  glMultiTexCoord2f(GL_TEXTURE0, waterUV, 0);             // Reflection texture
264  glMultiTexCoord2f(GL_TEXTURE1, waterUV, -move);         // Refraction texture
265  glMultiTexCoord2f(GL_TEXTURE2, normalUV, move2);      // Normal map texture
[8037]266  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                        // DUDV map texture
[8719]267  glVertex3f(this->waterPos.x + this->xWidth, this->waterPos.y, this->waterPos.z + this->zWidth);
[7796]268
[8037]269  // The back right vertice for the water
[8719]270  glMultiTexCoord2f(GL_TEXTURE0, waterUV, waterUV);        // Reflection texture
271  glMultiTexCoord2f(GL_TEXTURE1, waterUV, waterUV - move);       // Refraction texture
[8037]272  glMultiTexCoord2f(GL_TEXTURE2, normalUV, normalUV + move2);  // Normal map texture
273  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                        // DUDV map texture
[8719]274  glVertex3f(this->waterPos.x + this->xWidth, this->waterPos.y, this->waterPos.z);
[8037]275  glEnd();
[7796]276
[8312]277  this->shader->deactivateShader();
[7796]278
[8037]279  mat.unselect();
[7796]280
281  glPopMatrix();
282}
283
[8719]284/**
285 * @brief tick tack, calculates the flow of the water
286 */
[7796]287void MappedWater::tick(float dt)
288{
[8037]289  // makes the water flow
[8719]290  this->move += this->waterFlow * dt;
[8037]291  this->move2 = this->move * this->kNormalMapScale;
[8788]292 
293
294  if(this->tempcounter == 200) {
295    this->resetWaterColor(1, 0, 0);
296    resetShininess(1);
297    resetLightPos(0, 100, 0);
298    PRINTF(0)("test waterchangecolor ");
299    }
300  tempcounter++;
301   
[7796]302}
303
[8719]304/**
305 * @brief prepares everything to render the reflection texutre
306 */
307void MappedWater::activateReflection()
[7796]308{
[8719]309  // To create the reflection texture we just need to set the view port
310  // to our texture map size, then render the current scene our camera
311  // is looking at to the already allocated texture unit.  Since this
312  // is a reflection of the top of the water surface we use clipping
313  // planes to only render the top of the world as a reflection.  If
314  // we are below the water we don't flip the reflection but just use
315  // the current view of the top as we are seeing through the water.
316  // When you look through water at the surface it isn't really reflected,
317  // only when looking down from above the water on the surface.
[7796]318
[8037]319  // save viewport matrix and change the viewport size
[7796]320  glPushAttrib(GL_VIEWPORT_BIT);
321  glViewport(0,0, textureSize, textureSize);
322
[8312]323  glMatrixMode(GL_MODELVIEW);
[8037]324  glPushMatrix();
[7796]325
326  // If our camera is above the water we will render the scene flipped upside down.
327  // In order to line up the reflection nicely with the world we have to translate
328  // the world to the position of our reflected surface, multiplied by two.
[8037]329  glEnable(GL_CLIP_PLANE0);
330  Vector pos = State::getCameraNode()->getAbsCoor();
[8719]331
332  if(pos.y > waterPos.y)
[8037]333  {
334    // Translate the world, then flip it upside down
[8719]335    glTranslatef(0, waterPos.y*2, 0);
336    glScalef(1, -1, 1);
[7796]337
[8037]338    // Since the world is updside down we need to change the culling to FRONT
339    glCullFace(GL_FRONT);
[7796]340
[8037]341    // Set our plane equation and turn clipping on
[8719]342    double plane[4] = {0, 1, 0, -waterPos.y};
[8037]343    glClipPlane(GL_CLIP_PLANE0, plane);
344  }
345  else
[7796]346  {
[8037]347    // If the camera is below the water we don't want to flip the world,
348    // but just render it clipped so only the top is drawn.
[8719]349    double plane[4] = {0, 1, 0, waterPos.y};
[8037]350    glClipPlane(GL_CLIP_PLANE0, plane);
351  }
[7796]352}
353
[8719]354/**
355 * @brief ends the reflection and saves the graphic buffer into a texture
356 */
[7796]357void MappedWater::deactivateReflection()
358{
[8037]359  glDisable(GL_CLIP_PLANE0);
360  glCullFace(GL_BACK);
[7796]361
[8312]362  // Create the texture and store it on the video card
[8719]363  mat.renderToTexture(0, GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
[8312]364
[8037]365  glPopMatrix();
[7796]366  glPopAttrib();
367}
368
[8719]369/**
370 * @brief prepares everything to render the refraction texutre
371 */
[7796]372void MappedWater::activateRefraction()
[8719]373{
[8037]374  // To create the refraction and depth textures we do the same thing
375  // we did for the reflection texture, except we don't need to turn
376  // the world upside down.  We want to find the depth of the water,
377  // not the depth of the sky and above water terrain.
378
379  // save viewport matrix and change the viewport size
380  glPushAttrib(GL_VIEWPORT_BIT);
381  glViewport(0,0, textureSize, textureSize);
382
[8312]383  glMatrixMode(GL_MODELVIEW);
[8037]384  glPushMatrix();
385
386  // If our camera is above the water we will render only the parts that
387  // are under the water.  If the camera is below the water we render
388  // only the stuff above the water.  Like the reflection texture, we
389  // incorporate clipping planes to only render what we need.
390
391  // If the camera is above water, render the data below the water
392  glEnable(GL_CLIP_PLANE0);
393  Vector pos = State::getCameraNode()->getAbsCoor();
[8719]394  if(pos.y > waterPos.y)
[8037]395  {
[8719]396    double plane[4] = {0, -1, 0, waterPos.y};
[8037]397    glClipPlane(GL_CLIP_PLANE0, plane);
398  }
399  // If the camera is below the water, only render the data above the water
400  else
401  {
402    glCullFace(GL_FRONT);
[8719]403    double plane[4] = {0, 1, 0, -waterPos.y};
[8037]404    glClipPlane(GL_CLIP_PLANE0, plane);
405  }
[7796]406}
407
[8719]408/**
409 * @brief ends the refraction and saves the graphic buffer into a texture
410 */
[7796]411void MappedWater::deactivateRefraction()
412{
[8037]413  glDisable(GL_CLIP_PLANE0);
414  glCullFace(GL_BACK);
415
[8312]416  // Create the texture and store it on the video card
[8719]417  mat.renderToTexture(1, GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
[8312]418
419  glPopMatrix();
420  glPopAttrib();
[7796]421}
Note: See TracBrowser for help on using the repository browser.