Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8774 was 8729, checked in by stefalie, 19 years ago

water: quite everything works…

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