Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

water: fog :-(

File size: 14.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
22
23CREATE_FACTORY(MappedWater, CL_MAPPED_WATER);
24
25
26MappedWater::MappedWater(const TiXmlElement* root)
27{
28  this->setClassID(CL_MAPPED_WATER, "MappedWater");
29  this->toList(OM_ENVIRON);
30
31  if (root != NULL)
32    this->loadParams(root);
33
34  //PRINTF(0)("MaxTextureUnits: %i\n", Material::getMaxTextureUnits());
35
36  // TODO rename texture to reflection texture
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->g_WaterUV = 35.0f;
108  this->kNormalMapScale = 0.25f;
109  this->g_WaterFlow = 0.0015f;
110
111  /// initialization of the shaders
112  // load shader files
113  shader = new Shader( ResourceManager::getInstance()->getDataDir() + "/shaders/mapped_water.vert", ResourceManager::getInstance()->getDataDir() +"/shaders/mapped_water.frag");
114
115  this->shader->activateShader();
116  // Set the variable "reflection" to correspond to the first texture unit
117  Shader::Uniform(shader, "reflection").set(0);
118  // Set the variable "refraction" to correspond to the second texture unit
119  Shader::Uniform(shader, "refraction").set(1);
120  // Set the variable "normalMap" to correspond to the third texture unit
121  Shader::Uniform(shader, "normalMap").set(2);
122  // Set the variable "dudvMap" to correspond to the fourth texture unit
123  Shader::Uniform(shader, "dudvMap").set(3);
124  // Set the variable "depthMap" to correspond to the fifth texture unit
125  Shader::Uniform(shader, "depthMap").set(2);
126  // Give the variable "waterColor" a blue color
127  Shader::Uniform(shader, "waterColor").set(0.1f, 0.2f, 0.4f, 1.0f);
128  // Give the variable "lightPos" our hard coded light position
129  Shader::Uniform(shader, "lightPos").set(lightPos.x, lightPos.y, lightPos.z, 1.0f);
130  // uniform for the camera position
131  cam_uni = new Shader::Uniform(shader, "cameraPos");
132
133  this->shader->deactivateShader();
134 
135 
136  this->fog = new FogEffect();
137  fog->setFogColor(0.1f, 0.2f, 0.4f);
138  fog->setFogRange(0.0f, 500.0f);
139  fog->setFogDensity(0.03f);
140}
141
142MappedWater::~MappedWater()
143{
144  delete shader;
145  delete cam_uni;
146  delete fog;
147}
148
149void MappedWater::loadParams(const TiXmlElement* root)
150{
151  WorldEntity::loadParams(root);
152
153  LoadParam(root, "waterPos", this, MappedWater, setWaterPos);
154  LoadParam(root, "lightPos", this, MappedWater, setLightPosition);
155}
156
157
158void MappedWater::draw() const
159{
160  glMatrixMode(GL_MODELVIEW);
161
162  glPushMatrix();
163  glTranslatef(this->waterPos.x,this->waterPos.y,this->waterPos.z);
164
165  /////????//mat.unselect();
166  mat.select();
167
168  this->shader->activateShader();
169
170  // reset the camera uniform to the current cam position
171  Vector pos = State::getCameraNode()->getAbsCoor();
172  cam_uni->set(pos.x, pos.y, pos.z, 1.0f);
173
174  //glDisable(GL_BLEND);
175
176  glBegin(GL_QUADS);
177  // The back left vertice for the water
178 // glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 0);//g_WaterUV);            // Reflection texture
179 // glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 0);//refrUV - move);        // Refraction texture
180  glMultiTexCoord2f(GL_TEXTURE0, 0.0f, g_WaterUV);            // Reflection texture
181  glMultiTexCoord2f(GL_TEXTURE1, 0.0f, refrUV - move);        // Refraction texture
182  glMultiTexCoord2f(GL_TEXTURE2, 0.0f, normalUV + move2);     // Normal map texture
183  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                       // DUDV map texture
184  //glMultiTexCoord2f(GL_TEXTURE4, 0, 0);                       // Depth texture
185  glVertex3f(0.0f, waterPos.y, 0.0f);
186
187  // The front left vertice for the water
188 // glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 1);//0.0f);                  // Reflection texture
189 // glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 1);//0.0f - move);           // Refraction texture
190  glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 0.0f);                  // Reflection texture
191  glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 0.0f - move);           // Refraction texture
192  glMultiTexCoord2f(GL_TEXTURE2, 0.0f, 0.0f + move2);          // Normal map texture
193  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                        // DUDV map texture
194  //glMultiTexCoord2f(GL_TEXTURE4, 0, 0);                        // Depth texture
195  glVertex3f(0.0f, waterPos.y, 1000.0f);
196
197  // The front right vertice for the water
198//  glMultiTexCoord2f(GL_TEXTURE0, 1,1); //g_WaterUV, 0.0f);             // Reflection texture
199 // glMultiTexCoord2f(GL_TEXTURE1, 1,1);//refrUV, 0.0f - move);         // Refraction texture
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  //glMultiTexCoord2f(GL_TEXTURE4, 0, 0);                        // Depth texture
205  glVertex3f(1000.0f, waterPos.y, 1000.0f);
206
207  // The back right vertice for the water
208 // glMultiTexCoord2f(GL_TEXTURE0, 1,0);//g_WaterUV, g_WaterUV);        // Reflection texture
209 // glMultiTexCoord2f(GL_TEXTURE1, 1,0);//refrUV, refrUV - move);       // Refraction texture
210  glMultiTexCoord2f(GL_TEXTURE0, g_WaterUV, g_WaterUV);        // Reflection texture
211  glMultiTexCoord2f(GL_TEXTURE1, refrUV, refrUV - move);       // Refraction texture
212  glMultiTexCoord2f(GL_TEXTURE2, normalUV, normalUV + move2);  // Normal map texture
213  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                        // DUDV map texture
214  //glMultiTexCoord2f(GL_TEXTURE4, 0, 0);                        // Depth texture
215  glVertex3f(1000.0f, waterPos.y, 0.0f);
216  glEnd();
217
218  this->shader->deactivateShader();
219
220  mat.unselect();
221
222  if(pos.y < this->waterPos.y)
223  {
224    // draw some fog
225    this->fog->activate();
226  }
227  else
228  {
229    this->fog->deactivate();
230  }
231
232  glPopMatrix();
233}
234
235void MappedWater::tick(float dt)
236{
237  // makes the water flow
238  this->move += this->g_WaterFlow;
239  this->move2 = this->move * this->kNormalMapScale;
240  this->refrUV = this->g_WaterUV;
241  this->normalUV = this->g_WaterUV * this->kNormalMapScale; 
242}
243
244void MappedWater::activateReflection()
245{
246  // save viewport matrix and change the viewport size
247  glPushAttrib(GL_VIEWPORT_BIT);
248  glViewport(0,0, textureSize, textureSize);
249
250  glMatrixMode(GL_MODELVIEW);
251  glPushMatrix();
252
253  // If our camera is above the water we will render the scene flipped upside down.
254  // In order to line up the reflection nicely with the world we have to translate
255  // the world to the position of our reflected surface, multiplied by two.
256  glEnable(GL_CLIP_PLANE0);
257  Vector pos = State::getCameraNode()->getAbsCoor();
258  //pos.debug();
259  //PRINTF(0)("waterheight: %f\n", waterHeight);
260  if(pos.y > waterPos.y)
261  {
262    // Translate the world, then flip it upside down
263    glTranslatef(0.0f, waterPos.y*2.0f, 0.0f);
264    glScalef(1.0, -1.0, 1.0);
265
266    // Since the world is updside down we need to change the culling to FRONT
267    glCullFace(GL_FRONT);
268
269    // Set our plane equation and turn clipping on
270    double plane[4] = {0.0, 1.0, 0.0, -waterPos.y};
271    glClipPlane(GL_CLIP_PLANE0, plane);
272  }
273  else
274  {
275    // If the camera is below the water we don't want to flip the world,
276    // but just render it clipped so only the top is drawn.
277    double plane[4] = {0.0, 1.0, 0.0, waterPos.y};
278    glClipPlane(GL_CLIP_PLANE0, plane);
279  }
280}
281
282
283void MappedWater::deactivateReflection()
284{
285  glDisable(GL_CLIP_PLANE0);
286  glCullFace(GL_BACK);
287
288  //mat.select();
289  /////glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
290  ///mat.renderToTexture(0, GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
291  ///
292
293  glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0));
294
295  // Create the texture and store it on the video card
296  glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
297
298
299  glPopMatrix();
300  glPopAttrib();
301}
302
303void MappedWater::activateRefraction()
304{/*
305  // save viewport matrix and change the viewport size
306  glPushAttrib(GL_VIEWPORT_BIT);
307  glViewport(0,0, textureSize, textureSize);
308
309  glMatrixMode(GL_MODELVIEW);
310  glPushMatrix();
311
312  // If our camera is above the water we will render the scene flipped upside down.
313  // In order to line up the reflection nicely with the world we have to translate
314  // the world to the position of our reflected surface, multiplied by two.
315  glEnable(GL_CLIP_PLANE0);
316  Vector pos = State::getCameraNode()->getAbsCoor();
317  //pos.debug();
318  //PRINTF(0)("waterheight: %f\n", waterHeight);
319  if(pos.y > waterHeight)
320  {
321    // Translate the world, then flip it upside down
322    glTranslatef(0.0f, waterHeight*2.0f, 0.0f);
323    glScalef(1.0, -1.0, 1.0);
324
325    // Since the world is updside down we need to change the culling to FRONT
326    glCullFace(GL_FRONT);
327
328    // Set our plane equation and turn clipping on
329    double plane[4] = {0.0, 1.0, 0.0, -waterHeight};
330    glClipPlane(GL_CLIP_PLANE0, plane);
331  }
332  else
333  {
334    // If the camera is below the water we don't want to flip the world,
335    // but just render it clipped so only the top is drawn.
336    double plane[4] = {0.0, 1.0, 0.0, waterHeight};
337    glClipPlane(GL_CLIP_PLANE0, plane);
338  }
339*/
340
341  // To create the refraction and depth textures we do the same thing
342  // we did for the reflection texture, except we don't need to turn
343  // the world upside down.  We want to find the depth of the water,
344  // not the depth of the sky and above water terrain.
345
346  // save viewport matrix and change the viewport size
347  glPushAttrib(GL_VIEWPORT_BIT);
348  glViewport(0,0, textureSize, textureSize);
349
350  glMatrixMode(GL_MODELVIEW);
351  glPushMatrix();
352
353  // If our camera is above the water we will render only the parts that
354  // are under the water.  If the camera is below the water we render
355  // only the stuff above the water.  Like the reflection texture, we
356  // incorporate clipping planes to only render what we need.
357
358  // If the camera is above water, render the data below the water
359  glEnable(GL_CLIP_PLANE0);
360  Vector pos = State::getCameraNode()->getAbsCoor();
361  if(pos.y > waterPos.y)
362  {
363    double plane[4] = {0.0, -1.0, 0.0, waterPos.y}; 
364    glClipPlane(GL_CLIP_PLANE0, plane);
365
366  }
367  // If the camera is below the water, only render the data above the water
368  else
369  {
370    glCullFace(GL_FRONT);
371    double plane[4] = {0.0, 1.0, 0.0, -waterPos.y}; 
372    glClipPlane(GL_CLIP_PLANE0, plane);
373  }
374}
375
376void MappedWater::deactivateRefraction()
377{
378  glDisable(GL_CLIP_PLANE0);
379  glCullFace(GL_BACK);
380
381
382  glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(1));
383
384  // Create the texture and store it on the video card
385  glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
386
387  glPopMatrix();
388  glPopAttrib();
389
390  // Bind the current scene to our refraction texture
391//  glBindTexture(GL_TEXTURE_2D, g_Texture[REFRACTION_ID]);
392//  glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
393
394  // Bind the current scene to our depth texture
395//  glBindTexture(GL_TEXTURE_2D, g_Texture[DEPTH_ID]);
396//  glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, textureSize, textureSize, 0);
397
398}
Note: See TracBrowser for help on using the repository browser.