Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

water: waterheight bug is fixxed, but the mighty reflection is still gooone?, furthermore: i dont understand why we need a refraction renderpass… it looks the same regardless if its activated or not, we could save 5fps ;)

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