Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

water: general cleanup

File size: 13.0 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  /// loads the textures
38  // set up refleciton texture
39  //mat.setDiffuseMap(this->texture, 0);
40  mat.setDiffuseMap("pictures/refl.bmp", GL_TEXTURE_2D, 0);
41  // load refraction texture
42  mat.setDiffuseMap("pictures/refr.bmp", GL_TEXTURE_2D, 1);
43  // load normal map
44  mat.setDiffuseMap("pictures/normalmap.bmp", GL_TEXTURE_2D, 2);
45  // load dudv map
46  mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 3);
47  // set up depth texture
48  //mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 4);
49
50
51
52  /// MAKE THE MAPPING TEXTURE.
53  // THIS IS A HACK AND SHOULD BE IN TEXTURE SOMEWHERE
54  // set the size of the refraction and reflection textures
55  this->textureSize = 512;
56  //unsigned int channels = 32;
57  //GLenum type = GL_RGBA;
58  //unsigned int* pTextureReflection = new unsigned int [this->textureSize * this->textureSize * channels];
59  //memset(pTextureReflection, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int));
60  //unsigned int* pTextureRefraction = new unsigned int [this->textureSize * this->textureSize * channels];
61  //memset(pTextureRefraction, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int));
62  // Register the texture with OpenGL and bind it to the texture ID
63  //mat.select();
64  //glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0));
65
66  // Create the texture and store it on the video card
67  //glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureReflection);
68
69  //gluBuild2DMipmaps(GL_TEXTURE_2D, channels, this->textureSize, this->textureSize, type,  GL_UNSIGNED_INT, pTexture);
70
71  //the same for the refraction
72  //glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(1));
73  //glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureRefraction);
74
75  unsigned int channels = 32;
76  GLenum type = GL_RGBA;
77  unsigned int* pTextureReflection = new unsigned int [this->textureSize * this->textureSize * channels];
78  memset(pTextureReflection, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int));
79  //mat.select();
80  glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0));
81  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
82  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
83  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
84  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
85  glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureReflection);
86
87
88  unsigned int* pTextureRefraction = new unsigned int [this->textureSize * this->textureSize * channels];
89  memset(pTextureRefraction, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int));
90  glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(1));
91  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
92  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
93  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
94  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
95  glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureRefraction);
96
97  // Set the texture quality
98
99
100  // Since we stored the texture space with OpenGL, we can delete the image data
101  //delete [] pTextureReflection;
102  //delete [] pTextureRefraction;
103
104
105  /// initialization of the texture coords, speeds etc...
106  this->move = 0.0f;
107  this->kNormalMapScale = 0.25f;
108
109
110  /// initialization of the shaders
111  // load shader files
112  shader = new Shader( ResourceManager::getInstance()->getDataDir() + "/shaders/mapped_water.vert", ResourceManager::getInstance()->getDataDir() +"/shaders/mapped_water.frag");
113
114  this->shader->activateShader();
115  // Set the variable "reflection" to correspond to the first texture unit
116  Shader::Uniform(shader, "reflection").set(0);
117  // Set the variable "refraction" to correspond to the second texture unit
118  Shader::Uniform(shader, "refraction").set(1);
119  // Set the variable "normalMap" to correspond to the third texture unit
120  Shader::Uniform(shader, "normalMap").set(2);
121  // Set the variable "dudvMap" to correspond to the fourth texture unit
122  Shader::Uniform(shader, "dudvMap").set(3);
123  // Set the variable "depthMap" to correspond to the fifth texture unit
124  Shader::Uniform(shader, "depthMap").set(2);
125  // Give the variable "waterColor" a blue color
126  Shader::Uniform(shader, "waterColor").set(0.1f, 0.2f, 0.4f, 1.0f);
127  // Give the variable "lightPos" our hard coded light position
128  Shader::Uniform(shader, "lightPos").set(lightPos.x, lightPos.y, lightPos.z, 1.0f);
129  // uniform for the camera position
130  cam_uni = new Shader::Uniform(shader, "cameraPos");
131
132  this->shader->deactivateShader();
133
134  /// fog, doesnt work the way i want it to work
135  /*this->fog = new FogEffect();
136  fog->setFogColor(0.1f, 0.2f, 0.4f);
137  fog->setFogRange(0.0f, 500.0f);
138  fog->setFogDensity(0.03f);*/
139}
140
141/**
142 * @brief deltes shader and the uniform used by the camera
143 */
144MappedWater::~MappedWater()
145{
146  delete shader;
147  delete cam_uni;
148  delete fog;
149}
150
151/**
152 * @brief ends the refraction and saves the graphic buffer into a texture
153 * @param root xml data
154 */
155void MappedWater::loadParams(const TiXmlElement* root)
156{
157  WorldEntity::loadParams(root);
158
159  LoadParam(root, "waterpos", this, MappedWater, setWaterPos);
160  LoadParam(root, "watersize", this, MappedWater, setWaterSize);
161  LoadParam(root, "lightpos", this, MappedWater, setLightPosition);
162  LoadParam(root, "wateruv", this, MappedWater, setWaterUV);
163  LoadParam(root, "waterflow", this, MappedWater, setWaterFlow);
164}
165
166/**
167 * @brief activates the water shader and draws a quad with four textures on it
168 */
169void MappedWater::draw() const
170{
171  glMatrixMode(GL_MODELVIEW);
172
173  glPushMatrix();
174  glTranslatef(this->waterPos.x,this->waterPos.y,this->waterPos.z);
175
176  mat.select();
177
178  this->shader->activateShader();
179
180  // reset the camera uniform to the current cam position
181  Vector pos = State::getCameraNode()->getAbsCoor();
182  cam_uni->set(pos.x, pos.y, pos.z, 1.0f);
183
184  glBegin(GL_QUADS);
185  // The back left vertice for the water
186  glMultiTexCoord2f(GL_TEXTURE0, 0.0f, g_WaterUV);            // Reflection texture
187  glMultiTexCoord2f(GL_TEXTURE1, 0.0f, refrUV - move);        // Refraction texture
188  glMultiTexCoord2f(GL_TEXTURE2, 0.0f, normalUV + move2);     // Normal map texture
189  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                       // DUDV map texture
190  glVertex3f(0.0f, this->waterPos.y, 0.0f);
191
192  // The front left vertice for the water
193  glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 0.0f);                  // Reflection texture
194  glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 0.0f - move);           // Refraction texture
195  glMultiTexCoord2f(GL_TEXTURE2, 0.0f, 0.0f + move2);          // Normal map texture
196  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                        // DUDV map texture
197  glVertex3f(0.0f, this->waterPos.y, this->zWidth);
198
199  // The front right vertice for the water
200  glMultiTexCoord2f(GL_TEXTURE0, g_WaterUV, 0.0f);             // Reflection texture
201  glMultiTexCoord2f(GL_TEXTURE1, refrUV, 0.0f - move);         // Refraction texture
202  glMultiTexCoord2f(GL_TEXTURE2, normalUV, 0.0f + move2);      // Normal map texture
203  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                        // DUDV map texture
204  glVertex3f(this->xWidth, this->waterPos.y, this->zWidth);
205
206  // The back right vertice for the water
207  glMultiTexCoord2f(GL_TEXTURE0, g_WaterUV, g_WaterUV);        // Reflection texture
208  glMultiTexCoord2f(GL_TEXTURE1, refrUV, refrUV - move);       // Refraction texture
209  glMultiTexCoord2f(GL_TEXTURE2, normalUV, normalUV + move2);  // Normal map texture
210  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                        // DUDV map texture
211  glVertex3f(this->xWidth, this->waterPos.y, 0.0f);
212  glEnd();
213
214  this->shader->deactivateShader();
215
216  mat.unselect();
217
218  /*if(pos.y < this->waterPos.y)
219  {
220    // draw some fog
221    this->fog->activate();
222  }
223  else
224  {
225    this->fog->deactivate();
226  }*/
227
228  glPopMatrix();
229}
230
231/**
232 * @brief tick tack, calculates the flow of the water
233 */
234void MappedWater::tick(float dt)
235{
236  // makes the water flow
237  this->move += this->g_WaterFlow;
238  this->move2 = this->move * this->kNormalMapScale;
239  this->refrUV = this->g_WaterUV;
240  this->normalUV = this->g_WaterUV * 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.0f, waterPos.y*2.0f, 0.0f);
275    glScalef(1.0, -1.0, 1.0);
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.0, 1.0, 0.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.0, 1.0, 0.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  glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0));
302
303  // Create the texture and store it on the video card
304  glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
305
306  glPopMatrix();
307  glPopAttrib();
308}
309
310/**
311 * @brief prepares everything to render the refraction texutre
312 */
313void MappedWater::activateRefraction()
314{
315  // To create the refraction and depth textures we do the same thing
316  // we did for the reflection texture, except we don't need to turn
317  // the world upside down.  We want to find the depth of the water,
318  // not the depth of the sky and above water terrain.
319
320  // save viewport matrix and change the viewport size
321  glPushAttrib(GL_VIEWPORT_BIT);
322  glViewport(0,0, textureSize, textureSize);
323
324  glMatrixMode(GL_MODELVIEW);
325  glPushMatrix();
326
327  // If our camera is above the water we will render only the parts that
328  // are under the water.  If the camera is below the water we render
329  // only the stuff above the water.  Like the reflection texture, we
330  // incorporate clipping planes to only render what we need.
331
332  // If the camera is above water, render the data below the water
333  glEnable(GL_CLIP_PLANE0);
334  Vector pos = State::getCameraNode()->getAbsCoor();
335  if(pos.y > waterPos.y)
336  {
337    double plane[4] = {0.0, -1.0, 0.0, waterPos.y}; 
338    glClipPlane(GL_CLIP_PLANE0, plane);
339
340  }
341  // If the camera is below the water, only render the data above the water
342  else
343  {
344    glCullFace(GL_FRONT);
345    double plane[4] = {0.0, 1.0, 0.0, -waterPos.y}; 
346    glClipPlane(GL_CLIP_PLANE0, plane);
347  }
348}
349
350/**
351 * @brief ends the refraction and saves the graphic buffer into a texture
352 */
353void MappedWater::deactivateRefraction()
354{
355  glDisable(GL_CLIP_PLANE0);
356  glCullFace(GL_BACK);
357
358  glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(1));
359
360  // Create the texture and store it on the video card
361  glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
362
363  glPopMatrix();
364  glPopAttrib();
365}
Note: See TracBrowser for help on using the repository browser.