Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8902 was 8792, checked in by patrick, 18 years ago

merged water branche back to trunk

File size: 13.6 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 "math.h"
22
23
24CREATE_FACTORY(MappedWater, CL_MAPPED_WATER);
25
26/**
27 * @brief constructor
28 * @param root xml data
29 */
30MappedWater::MappedWater(const TiXmlElement* root)
31{
32  this->setClassID(CL_MAPPED_WATER, "MappedWater");
33  this->toList(OM_ENVIRON);
34
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
39  if (root != NULL)
40    this->loadParams(root);
41
42  /// initialization of the textures
43  this->initTextures();
44
45  /// initialization of the shaders
46  this->initShaders();
47 
48  tempcounter = 0;
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;
58  delete color_uni;
59  delete light_uni;
60  delete shine_uni;
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);
76  this->setWaterColor(0.1f, 0.2f, 0.4f);
77  this->setShininess(128);
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;
85
86
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
99  // set up refleciton texture
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);
105  // load normal map
106  mat.setDiffuseMap("pictures/normalmap.bmp", GL_TEXTURE_2D, 2);
107  // load dudv map
108  mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 3);
109
110  // sets texture parameters for reflection texture
111  glBindTexture(GL_TEXTURE_2D, this->mat.diffuseTextureID(0));
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);
116  // sets texture parameters for refraction texture
117  glBindTexture(GL_TEXTURE_2D, this->mat.diffuseTextureID(1));
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);
122}
123
124/**
125 * @brief initialization of the shaders
126 */
127void MappedWater::initShaders()
128{
129  // load shader files
130  shader = new Shader( ResourceManager::getInstance()->getDataDir() + "/shaders/mapped_water.vert", ResourceManager::getInstance()->getDataDir() +"/shaders/mapped_water.frag");
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
142  Shader::Uniform(shader, "depthMap").set(2);
143  // Give the variable "waterColor" a blue color
144  color_uni = new Shader::Uniform(shader, "waterColor");
145  color_uni->set(waterColor.x, waterColor.y, waterColor.z, 1.0f);
146  // Give the variable "lightPos" our hard coded light position
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);
152  // uniform for the camera position
153  cam_uni = new Shader::Uniform(shader, "cameraPos");
154
155  this->shader->deactivateShader();
156}
157
158/**
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/**
208 * @brief ends the refraction and saves the graphic buffer into a texture
209 * @param root xml data
210 */
211void MappedWater::loadParams(const TiXmlElement* root)
212{
213  WorldEntity::loadParams(root);
214
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);
222  LoadParam(root, "watercolor", this, MappedWater, setWaterColor);
223  LoadParam(root, "shininess", this, MappedWater, setShininess);
224}
225
226/**
227 * @brief activates the water shader and draws a quad with four textures on it
228 */
229void MappedWater::draw() const
230{
231  glMatrixMode(GL_MODELVIEW);
232
233  glPushMatrix();
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);
236
237  mat.select();
238
239  this->shader->activateShader();
240
241  // reset the camera uniform to the current cam position
242  Vector pos = State::getCameraNode()->getAbsCoor();
243  cam_uni->set(pos.x, pos.y, pos.z, 1.0f);
244
245  glDisable(GL_BLEND);
246
247  glBegin(GL_QUADS);
248  // The back left vertice for the water
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
252  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                       // DUDV map texture
253  glVertex3f(this->waterPos.x, this->waterPos.y, this->waterPos.z);
254
255  // The front left vertice for the water
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
259  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                        // DUDV map texture
260  glVertex3f(this->waterPos.x, this->waterPos.y, this->waterPos.z + this->zWidth);
261
262  // The front right vertice for the water
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
266  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                        // DUDV map texture
267  glVertex3f(this->waterPos.x + this->xWidth, this->waterPos.y, this->waterPos.z + this->zWidth);
268
269  // The back right vertice for the water
270  glMultiTexCoord2f(GL_TEXTURE0, waterUV, waterUV);        // Reflection texture
271  glMultiTexCoord2f(GL_TEXTURE1, waterUV, waterUV - move);       // Refraction texture
272  glMultiTexCoord2f(GL_TEXTURE2, normalUV, normalUV + move2);  // Normal map texture
273  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                        // DUDV map texture
274  glVertex3f(this->waterPos.x + this->xWidth, this->waterPos.y, this->waterPos.z);
275  glEnd();
276
277  this->shader->deactivateShader();
278
279  mat.unselect();
280
281  glPopMatrix();
282}
283
284/**
285 * @brief tick tack, calculates the flow of the water
286 */
287void MappedWater::tick(float dt)
288{
289  // makes the water flow
290  this->move += this->waterFlow * dt;
291  this->move2 = this->move * this->kNormalMapScale;
292 
293
294//   if(this->tempcounter == 200) {
295//     this->resetWaterColor(1, 0, 0);
296//     resetShininess(1);
297//     resetLightPos(0, 50, 0);
298//     PRINTF(0)("test waterchangecolor ");
299//     }
300//   tempcounter++;
301   
302}
303
304/**
305 * @brief prepares everything to render the reflection texutre
306 */
307void MappedWater::activateReflection()
308{
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.
318
319  // save viewport matrix and change the viewport size
320  glPushAttrib(GL_VIEWPORT_BIT);
321  glViewport(0,0, textureSize, textureSize);
322
323  glMatrixMode(GL_MODELVIEW);
324  glPushMatrix();
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.
329  glEnable(GL_CLIP_PLANE0);
330  Vector pos = State::getCameraNode()->getAbsCoor();
331
332  if(pos.y > waterPos.y)
333  {
334    // Translate the world, then flip it upside down
335    glTranslatef(0, waterPos.y*2, 0);
336    glScalef(1, -1, 1);
337
338    // Since the world is updside down we need to change the culling to FRONT
339    glCullFace(GL_FRONT);
340
341    // Set our plane equation and turn clipping on
342    double plane[4] = {0, 1, 0, -waterPos.y};
343    glClipPlane(GL_CLIP_PLANE0, plane);
344  }
345  else
346  {
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.
349    double plane[4] = {0, 1, 0, waterPos.y};
350    glClipPlane(GL_CLIP_PLANE0, plane);
351  }
352}
353
354/**
355 * @brief ends the reflection and saves the graphic buffer into a texture
356 */
357void MappedWater::deactivateReflection()
358{
359  glDisable(GL_CLIP_PLANE0);
360  glCullFace(GL_BACK);
361
362  // Create the texture and store it on the video card
363  mat.renderToTexture(0, GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
364
365  glPopMatrix();
366  glPopAttrib();
367}
368
369/**
370 * @brief prepares everything to render the refraction texutre
371 */
372void MappedWater::activateRefraction()
373{
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
383  glMatrixMode(GL_MODELVIEW);
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();
394  if(pos.y > waterPos.y)
395  {
396    double plane[4] = {0, -1, 0, waterPos.y};
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);
403    double plane[4] = {0, 1, 0, -waterPos.y};
404    glClipPlane(GL_CLIP_PLANE0, plane);
405  }
406}
407
408/**
409 * @brief ends the refraction and saves the graphic buffer into a texture
410 */
411void MappedWater::deactivateRefraction()
412{
413  glDisable(GL_CLIP_PLANE0);
414  glCullFace(GL_BACK);
415
416  // Create the texture and store it on the video card
417  mat.renderToTexture(1, GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
418
419  glPopMatrix();
420  glPopAttrib();
421}
Note: See TracBrowser for help on using the repository browser.