Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

water: works fine so far…

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