Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

branches/water: …

File size: 10.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  /// Initialization of the shaders
95  this->shader->activateShader();
96  // Set the variable "reflection" to correspond to the first texture unit
97  Shader::Uniform(shader, "reflection").set(0);
98
99  // Set the variable "refraction" to correspond to the second texture unit
100  //this->uni = new Shader::Uniform (shader, "refraction");
101  //this->uni->set(1);
102
103  // Set the variable "normalMap" to correspond to the third texture unit
104  Shader::Uniform(shader, "normalMap").set(2);
105
106  // Set the variable "dudvMap" to correspond to the fourth texture unit
107  Shader::Uniform(shader, "dudvMap").set(3);
108
109  // Set the variable "depthMap" to correspond to the fifth texture unit
110  //this->uni = new Shader::Uniform (shader, "depthMap");
111  //this->uni->set(4);
112  this->shader->activateShader();
113}
114
115MappedWater::~MappedWater()
116{
117  //delete shader;
118}
119
120void MappedWater::loadParams(const TiXmlElement* root)
121{
122  WorldEntity::loadParams(root);
123
124  LoadParam(root, "waterHeight", this, MappedWater, setHeight);
125}
126
127
128void MappedWater::draw() const
129{
130  glPushMatrix();
131  glTranslatef(0,this->waterHeight,0);
132
133  mat.unselect();
134  mat.select();
135
136
137
138  // Shader init
139  this->shader->activateShader();
140  GLint uniform;
141
142
143
144
145
146  // Give the variable "waterColor" a blue color
147  uniform = glGetUniformLocationARB(shader->getProgram(), "waterColor");
148  glUniform4fARB(uniform, 0.1f, 0.2f, 0.4f, 1.0f);
149
150  // FIXME set camera and light information
151  Vector lightPos(100.0f, 600.0f, 100.0f);
152  Vector vPosition = State::getCameraNode()->getAbsCoor();
153  // Store the camera position in a variable
154  //CVector3 vPosition = g_Camera.Position();
155  //Vector vPosition(50.0f, 50.0f, 50.0f);
156
157  // Give the variable "lightPos" our hard coded light position
158  uniform = glGetUniformLocationARB(shader->getProgram(), "lightPos");
159  glUniform4fARB(uniform, lightPos.x, lightPos.y, lightPos.z, 1.0f);
160
161  // Give the variable "cameraPos" our camera position
162  uniform = glGetUniformLocationARB(shader->getProgram(), "cameraPos");
163  glUniform4fARB(uniform, vPosition.x, vPosition.y, vPosition.z, 1.0f);
164
165
166
167
168
169
170  glBegin(GL_QUADS);
171  // The back left vertice for the water
172  glMultiTexCoord2f(GL_TEXTURE0, 0.0f, g_WaterUV);            // Reflection texture
173  glMultiTexCoord2f(GL_TEXTURE1, 0.0f, refrUV - move);        // Refraction texture
174  glMultiTexCoord2f(GL_TEXTURE2, 0.0f, normalUV + move2);     // Normal map texture
175  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                       // DUDV map texture
176  glMultiTexCoord2f(GL_TEXTURE4, 0, 0);                       // Depth texture
177  glVertex3f(0.0f, waterHeight, 0.0f);
178
179  // The front left vertice for the water
180  glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 0.0f);                  // Reflection texture
181  glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 0.0f - move);           // Refraction texture
182  glMultiTexCoord2f(GL_TEXTURE2, 0.0f, 0.0f + 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, waterHeight, 1000.0f);
186
187  // The front right vertice for the water
188  glMultiTexCoord2f(GL_TEXTURE0, g_WaterUV, 0.0f);             // Reflection texture
189  glMultiTexCoord2f(GL_TEXTURE1, refrUV, 0.0f - move);         // Refraction texture
190  glMultiTexCoord2f(GL_TEXTURE2, normalUV, 0.0f + move2);      // Normal map texture
191  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                        // DUDV map texture
192  glMultiTexCoord2f(GL_TEXTURE4, 0, 0);                        // Depth texture
193  glVertex3f(1000.0f, waterHeight, 1000.0f);
194
195  // The back right vertice for the water
196  glMultiTexCoord2f(GL_TEXTURE0, g_WaterUV, g_WaterUV);        // Reflection texture
197  glMultiTexCoord2f(GL_TEXTURE1, refrUV, refrUV - move);       // Refraction texture
198  glMultiTexCoord2f(GL_TEXTURE2, normalUV, normalUV + move2);  // Normal map texture
199  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);                        // DUDV map texture
200  glMultiTexCoord2f(GL_TEXTURE4, 0, 0);                        // Depth texture
201  glVertex3f(1000.0f, waterHeight, 0.0f);
202  glEnd();
203
204  this->shader->deactivateShader();
205
206  mat.unselect();
207
208  glPopMatrix();
209}
210
211void MappedWater::tick(float dt)
212{
213  // makes the water flow
214  this->move += this->g_WaterFlow;
215  this->move2 = this->move * this->kNormalMapScale;
216  this->refrUV = this->g_WaterUV;
217  this->normalUV = this->g_WaterUV * this->kNormalMapScale;
218}
219
220void MappedWater::setHeight(float height)
221{
222  this->waterHeight = height;
223}
224
225void MappedWater::activateReflection()
226{
227  glPushAttrib(GL_VIEWPORT_BIT);
228
229
230  //glLoadIdentity();
231  glViewport(0,0, textureSize, textureSize);
232
233  glPushMatrix();
234  // Clear the color and depth bits, reset the matrix and position our camera.
235
236  //g_Camera.Look();
237
238
239  // If our camera is above the water we will render the scene flipped upside down.
240  // In order to line up the reflection nicely with the world we have to translate
241  // the world to the position of our reflected surface, multiplied by two.
242  //if(g_Camera.Position().y > waterHeight)
243  //{
244  // Translate the world, then flip it upside down
245  glTranslatef(0.0f, this->waterHeight*2.0f, 0.0f);
246  glScalef(1.0, -1.0, 1.0);
247
248  // Since the world is updside down we need to change the culling to FRONT
249  //glCullFace(GL_FRONT);
250
251  // Set our plane equation and turn clipping on
252  //double plane[4] = {0.0, 1.0, 0.0, -waterHeight};
253  //glEnable(GL_CLIP_PLANE0);
254  //glClipPlane(GL_CLIP_PLANE0, plane);
255
256  // Render the world upside down and clipped (only render the top flipped).
257  // If we don't turn OFF caustics for the reflection texture we get horrible
258  // artifacts in the water.  That is why we set bRenderCaustics to FALSE.
259  //RenderWorld(false);
260
261  // Turn clipping off
262  // glDisable(GL_CLIP_PLANE0);
263
264  // Restore back-face culling
265  //  glCullFace(GL_BACK);
266  //}
267  /*else
268  {
269      // If the camera is below the water we don't want to flip the world,
270      // but just render it clipped so only the top is drawn.
271      double plane[4] = {0.0, 1.0, 0.0, waterHeight};
272      glEnable(GL_CLIP_PLANE0);
273      glClipPlane(GL_CLIP_PLANE0, plane);
274      RenderWorld(true);
275      glDisable(GL_CLIP_PLANE0);
276  }*/
277}
278
279
280void MappedWater::deactivateReflection()
281{
282//  glBindTexture(GL_TEXTURE_2D, texture.getTexture()); //is done by mat.select();
283  //glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0));
284
285  //mat.setDiffuseMap(&texture, 0);
286
287  mat.select();
288  glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
289  //glDisable(GL_CLIP_PLANE0);
290
291  glPopMatrix();
292
293  glPopAttrib();
294}
295
296void MappedWater::activateRefraction()
297{
298}
299
300void MappedWater::deactivateRefraction()
301{
302}
Note: See TracBrowser for help on using the repository browser.