Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

branches/water: light!

File size: 11.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
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  //! todo: rename texture to reflection texture
37  // set up refleciton texture
38  // FIXME mat.setDiffuseMap(this->texture, 0); doesnt work,
39  mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 0);
40  // load refraction texture
41 // mat.setDiffuseMap("pictures/sky-replace.jpg", 1);
42  // load normal map
43  mat.setDiffuseMap("pictures/normalmap.bmp", GL_TEXTURE_2D, 2);
44  // load dudv map
45  mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 3);
46  // set up depth texture
47  mat.setDiffuseMap("pictures/sky-replace.jpg", 4);
48
49  // set the size of the refraction and reflection textures
50
51
52
53  /// MAKE THE MAPPING TEXTURE.
54  // THIS IS A HACK AND SHOULD BE IN TEXTURE SOMEWHERE.
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  // Set the texture quality
76  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
77  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
78  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
79  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
80
81  // Since we stored the texture space with OpenGL, we can delete the image data
82  delete [] pTextureReflection;
83  delete [] pTextureRefraction;
84
85
86  //shader = new Shader( "/home/stefalie/svn/orxonox/data/trunk/shaders/water.vert", "/home/stefalie/svn/orxonox/data/trunk/shaders/mapped_water.frag");
87  shader = new Shader( ResourceManager::getInstance()->getDataDir() + "/shaders/mapped_water.vert", ResourceManager::getInstance()->getDataDir() +"/shaders/mapped_water.frag");
88
89  this->move = 0.0f;
90  this->g_WaterUV = 35.0f;
91  this->kNormalMapScale = 0.25f;
92  this->g_WaterFlow = 0.0015f;
93}
94
95MappedWater::~MappedWater()
96{
97  //delete shader;
98}
99
100void MappedWater::loadParams(const TiXmlElement* root)
101{
102  WorldEntity::loadParams(root);
103
104  LoadParam(root, "waterHeight", this, MappedWater, setHeight);
105}
106
107
108void MappedWater::draw() const
109{
110  glPushMatrix();
111  glTranslatef(0,this->waterHeight,0);
112
113  //HACK
114 
115  //glEnable(GL_LIGHTING);
116
117
118  //mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 1);
119  //mat.setTransparency(1.0);
120  //mat.setDiffuse(1.0, 0, .1);
121
122
123  // HACK
124
125  //glActiveTexture(GL_TEXTURE0);
126  //glBindTexture(GL_TEXTURE_2D, this->texture);
127  mat.unselect();
128  mat.select();
129  //glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0));
130
131
132  // Shader init
133  this->shader->activateShader();
134  GLint uniform;
135  Shader::Uniform* uni;
136
137  // Set the variable "reflection" to correspond to the first texture unit
138  uni = new Shader::Uniform (shader, "reflection");
139  uni->set(0);
140  //uniform = glGetUniformLocationARB(shader->getProgram(), "reflection");
141  //glUniform1iARB(uniform, 0); //second paramter is the texture unit
142
143  // Set the variable "refraction" to correspond to the second texture unit
144 // uni = new Shader::Uniform (shader, "refraction");
145 // uni->set(1);
146  //uniform = glGetUniformLocationARB(shader->getProgram(), "refraction");
147  //glUniform1iARB(uniform, 1);
148
149  // Set the variable "normalMap" to correspond to the third texture unit
150  uni = new Shader::Uniform (shader, "normalMap");
151  uni->set(2);
152  //uniform = glGetUniformLocationARB(shader->getProgram(), "normalMap");
153  //glUniform1iARB(uniform, 2);
154
155  // Set the variable "dudvMap" to correspond to the fourth texture unit
156  uni = new Shader::Uniform (shader, "dudvMap");
157  uni->set(3);
158  //uniform = glGetUniformLocationARB(shader->getProgram(), "dudvMap");
159  //glUniform1iARB(uniform, 3);
160
161  // Set the variable "depthMap" to correspond to the fifth texture unit
162 // uni = new Shader::Uniform (shader, "depthMap");
163 // uni->set(4);
164  //uniform = glGetUniformLocationARB(shader->getProgram(), "depthMap");
165  //glUniform1iARB(uniform, 4);
166  // FIXME we dont have a depthMap yet :-(
167
168  // Give the variable "waterColor" a blue color
169  uniform = glGetUniformLocationARB(shader->getProgram(), "waterColor");
170  glUniform4fARB(uniform, 0.1f, 0.2f, 0.4f, 1.0f);
171
172  // FIXME set camera and light information
173  Vector lightPos(100.0f, 600.0f, 100.0f);
174  Vector vPosition = State::getCameraNode()->getAbsCoor();
175  // Store the camera position in a variable
176  //CVector3 vPosition = g_Camera.Position();
177  //Vector vPosition(50.0f, 50.0f, 50.0f);
178
179  // Give the variable "lightPos" our hard coded light position
180  uniform = glGetUniformLocationARB(shader->getProgram(), "lightPos");
181  glUniform4fARB(uniform, lightPos.x, lightPos.y, lightPos.z, 1.0f);
182
183  // Give the variable "cameraPos" our camera position
184  uniform = glGetUniformLocationARB(shader->getProgram(), "cameraPos");
185  glUniform4fARB(uniform, vPosition.x, vPosition.y, vPosition.z, 1.0f);
186
187
188
189
190
191
192  glBegin(GL_QUADS);
193  // The back left vertice for the water
194  glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0f, g_WaterUV);            // Reflection texture
195  glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0f, refrUV - move);        // Refraction texture
196  glMultiTexCoord2fARB(GL_TEXTURE2_ARB, 0.0f, normalUV + move2);     // Normal map texture
197  glMultiTexCoord2fARB(GL_TEXTURE3_ARB, 0, 0);                       // DUDV map texture
198  glMultiTexCoord2fARB(GL_TEXTURE4_ARB, 0, 0);                       // Depth texture
199  glVertex3f(0.0f, waterHeight, 0.0f);
200
201  // The front left vertice for the water
202  glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0f, 0.0f);                  // Reflection texture
203  glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0f, 0.0f - move);           // Refraction texture
204  glMultiTexCoord2fARB(GL_TEXTURE2_ARB, 0.0f, 0.0f + move2);          // Normal map texture
205  glMultiTexCoord2fARB(GL_TEXTURE3_ARB, 0, 0);                        // DUDV map texture
206  glMultiTexCoord2fARB(GL_TEXTURE4_ARB, 0, 0);                        // Depth texture
207  glVertex3f(0.0f, waterHeight, 1000.0f);
208
209  // The front right vertice for the water
210  glMultiTexCoord2fARB(GL_TEXTURE0_ARB, g_WaterUV, 0.0f);             // Reflection texture
211  glMultiTexCoord2fARB(GL_TEXTURE1_ARB, refrUV, 0.0f - move);         // Refraction texture
212  glMultiTexCoord2fARB(GL_TEXTURE2_ARB, normalUV, 0.0f + move2);      // Normal map texture
213  glMultiTexCoord2fARB(GL_TEXTURE3_ARB, 0, 0);                        // DUDV map texture
214  glMultiTexCoord2fARB(GL_TEXTURE4_ARB, 0, 0);                        // Depth texture
215  glVertex3f(1000.0f, waterHeight, 1000.0f);
216
217  // The back right vertice for the water
218  glMultiTexCoord2fARB(GL_TEXTURE0_ARB, g_WaterUV, g_WaterUV);        // Reflection texture
219  glMultiTexCoord2fARB(GL_TEXTURE1_ARB, refrUV, refrUV - move);       // Refraction texture
220  glMultiTexCoord2fARB(GL_TEXTURE2_ARB, normalUV, normalUV + move2);  // Normal map texture
221  glMultiTexCoord2fARB(GL_TEXTURE3_ARB, 0, 0);                        // DUDV map texture
222  glMultiTexCoord2fARB(GL_TEXTURE4_ARB, 0, 0);                        // Depth texture
223  glVertex3f(1000.0f, waterHeight, 0.0f);
224  glEnd();
225
226  this->shader->deactivateShader();
227
228
229  Material::unselect();
230
231  glPopMatrix();
232}
233
234void MappedWater::tick(float dt)
235{
236  this->move += this->g_WaterFlow = 0.004;
237  this->move2 = this->move * this->kNormalMapScale;
238  this->refrUV = this->g_WaterUV;
239  this->normalUV = this->g_WaterUV * this->kNormalMapScale;
240}
241
242void MappedWater::setHeight(float height)
243{
244  this->waterHeight = height;
245}
246
247void MappedWater::activateReflection()
248{
249  glPushAttrib(GL_VIEWPORT_BIT);
250 
251
252  //glLoadIdentity();
253  glViewport(0,0, textureSize, textureSize);
254
255  glPushMatrix();
256  // Clear the color and depth bits, reset the matrix and position our camera.
257
258  //g_Camera.Look();
259
260
261  // If our camera is above the water we will render the scene flipped upside down.
262  // In order to line up the reflection nicely with the world we have to translate
263  // the world to the position of our reflected surface, multiplied by two.
264  //if(g_Camera.Position().y > waterHeight)
265  //{
266  // Translate the world, then flip it upside down
267  glTranslatef(0.0f, this->waterHeight*2.0f, 0.0f);
268  glScalef(1.0, -1.0, 1.0);
269
270  // Since the world is updside down we need to change the culling to FRONT
271  glCullFace(GL_FRONT);
272
273  // Set our plane equation and turn clipping on
274  double plane[4] = {0.0, 1.0, 0.0, -waterHeight};
275  //glEnable(GL_CLIP_PLANE0);
276  //glClipPlane(GL_CLIP_PLANE0, plane);
277
278  // Render the world upside down and clipped (only render the top flipped).
279  // If we don't turn OFF caustics for the reflection texture we get horrible
280  // artifacts in the water.  That is why we set bRenderCaustics to FALSE.
281  //RenderWorld(false);
282
283  // Turn clipping off
284  // glDisable(GL_CLIP_PLANE0);
285
286  // Restore back-face culling
287  //  glCullFace(GL_BACK);
288  //}
289  /*else
290  {
291      // If the camera is below the water we don't want to flip the world,
292      // but just render it clipped so only the top is drawn.
293      double plane[4] = {0.0, 1.0, 0.0, waterHeight};
294      glEnable(GL_CLIP_PLANE0);
295      glClipPlane(GL_CLIP_PLANE0, plane);
296      RenderWorld(true);
297      glDisable(GL_CLIP_PLANE0);
298  }*/
299}
300
301
302void MappedWater::deactivateReflection()
303{
304//  glBindTexture(GL_TEXTURE_2D, texture.getTexture()); //is done by mat.select();
305  //glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0));
306
307  //mat.setDiffuseMap(&texture, 0);
308  mat.select();
309  glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
310  //glDisable(GL_CLIP_PLANE0);
311
312  glPopMatrix();
313
314  glPopAttrib();
315}
316
317void MappedWater::activateRefraction()
318{
319}
320
321void MappedWater::deactivateRefraction()
322{
323}
Note: See TracBrowser for help on using the repository browser.