Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

water: some cleanup and some cleandown :-)

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