Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

branches/water: the texture mooooves

File size: 10.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
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  glDisable(GL_BLEND);
126  //glActiveTexture(GL_TEXTURE0);
127  //glBindTexture(GL_TEXTURE_2D, this->texture);
128
129  mat.select();
130  glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0));
131
132
133  // Shader init
134  this->shader->activateShader();
135  GLint uniform;
136
137  // Set the variable "reflection" to correspond to the first texture unit
138  uniform = glGetUniformLocationARB(shader->getProgram(), "reflection");
139  glUniform1iARB(uniform, 0); //second paramter is the texture unit
140
141  // Set the variable "refraction" to correspond to the second texture unit
142  uniform = glGetUniformLocationARB(shader->getProgram(), "refraction");
143  glUniform1iARB(uniform, 1);
144
145  // Set the variable "normalMap" to correspond to the third texture unit
146  uniform = glGetUniformLocationARB(shader->getProgram(), "normalMap");
147  glUniform1iARB(uniform, 2);
148
149  // Set the variable "dudvMap" to correspond to the fourth texture unit
150  uniform = glGetUniformLocationARB(shader->getProgram(), "dudvMap");
151  glUniform1iARB(uniform, 3);
152
153  // Set the variable "depthMap" to correspond to the fifth texture unit
154  uniform = glGetUniformLocationARB(shader->getProgram(), "depthMap");
155  glUniform1iARB(uniform, 4);
156  // FIXME we dont have a depthMap yet :-(
157
158  // Give the variable "waterColor" a blue color
159  uniform = glGetUniformLocationARB(shader->getProgram(), "waterColor");
160  glUniform4fARB(uniform, 0.1f, 0.2f, 0.4f, 1.0f);
161
162  // FIXME set camera and light information
163  Vector lightPos(100.0f, 600.0f, 100.0f);
164  Vector vPosition = State::getCameraNode()->getAbsCoor();
165  // Store the camera position in a variable
166  //CVector3 vPosition = g_Camera.Position();
167  //Vector vPosition(50.0f, 50.0f, 50.0f);
168
169  // Give the variable "lightPos" our hard coded light position
170  uniform = glGetUniformLocationARB(shader->getProgram(), "lightPos");
171  glUniform4fARB(uniform, lightPos.x, lightPos.y, lightPos.z, 1.0f);
172
173  // Give the variable "cameraPos" our camera position
174  uniform = glGetUniformLocationARB(shader->getProgram(), "cameraPos");
175  glUniform4fARB(uniform, vPosition.x, vPosition.y, vPosition.z, 1.0f);
176
177
178
179  float move2 = this->move * this->kNormalMapScale;
180  float refrUV = this->g_WaterUV;
181  float normalUV = this->g_WaterUV * this->kNormalMapScale;
182
183
184  glBegin(GL_QUADS);
185  // The back left vertice for the water
186  glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0f, g_WaterUV);            // Reflection texture
187  glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0f, refrUV - move);        // Refraction texture
188  glMultiTexCoord2fARB(GL_TEXTURE2_ARB, 0.0f, normalUV + move2);     // Normal map texture
189  glMultiTexCoord2fARB(GL_TEXTURE3_ARB, 0, 0);                       // DUDV map texture
190  glMultiTexCoord2fARB(GL_TEXTURE4_ARB, 0, 0);                       // Depth texture
191  glVertex3f(0.0f, waterHeight, 0.0f);
192
193  // The front left vertice for the water
194  glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0f, 0.0f);                  // Reflection texture
195  glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0f, 0.0f - move);           // Refraction texture
196  glMultiTexCoord2fARB(GL_TEXTURE2_ARB, 0.0f, 0.0f + 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, 1000.0f);
200
201  // The front right vertice for the water
202  glMultiTexCoord2fARB(GL_TEXTURE0_ARB, g_WaterUV, 0.0f);             // Reflection texture
203  glMultiTexCoord2fARB(GL_TEXTURE1_ARB, refrUV, 0.0f - move);         // Refraction texture
204  glMultiTexCoord2fARB(GL_TEXTURE2_ARB, normalUV, 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(1000.0f, waterHeight, 1000.0f);
208
209  // The back right vertice for the water
210  glMultiTexCoord2fARB(GL_TEXTURE0_ARB, g_WaterUV, g_WaterUV);        // Reflection texture
211  glMultiTexCoord2fARB(GL_TEXTURE1_ARB, refrUV, refrUV - move);       // Refraction texture
212  glMultiTexCoord2fARB(GL_TEXTURE2_ARB, normalUV, normalUV + 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, 0.0f);
216  glEnd();
217
218  this->shader->deactivateShader();
219
220
221  glPopMatrix();
222}
223
224void MappedWater::tick(float dt)
225{
226  this->move += this->g_WaterFlow;
227}
228
229void MappedWater::setHeight(float height)
230{
231  this->waterHeight = height;
232}
233
234void MappedWater::activateReflection()
235{
236  glPushAttrib(GL_VIEWPORT_BIT);
237 
238
239  //glLoadIdentity();
240  glViewport(0,0, textureSize, textureSize);
241
242  glPushMatrix();
243  // Clear the color and depth bits, reset the matrix and position our camera.
244
245  //g_Camera.Look();
246
247
248  // If our camera is above the water we will render the scene flipped upside down.
249  // In order to line up the reflection nicely with the world we have to translate
250  // the world to the position of our reflected surface, multiplied by two.
251  //if(g_Camera.Position().y > waterHeight)
252  //{
253  // Translate the world, then flip it upside down
254  glTranslatef(0.0f, this->waterHeight*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, -waterHeight};
262  //glEnable(GL_CLIP_PLANE0);
263  //glClipPlane(GL_CLIP_PLANE0, plane);
264
265  // Render the world upside down and clipped (only render the top flipped).
266  // If we don't turn OFF caustics for the reflection texture we get horrible
267  // artifacts in the water.  That is why we set bRenderCaustics to FALSE.
268  //RenderWorld(false);
269
270  // Turn clipping off
271  // glDisable(GL_CLIP_PLANE0);
272
273  // Restore back-face culling
274  //  glCullFace(GL_BACK);
275  //}
276  /*else
277  {
278      // If the camera is below the water we don't want to flip the world,
279      // but just render it clipped so only the top is drawn.
280      double plane[4] = {0.0, 1.0, 0.0, waterHeight};
281      glEnable(GL_CLIP_PLANE0);
282      glClipPlane(GL_CLIP_PLANE0, plane);
283      RenderWorld(true);
284      glDisable(GL_CLIP_PLANE0);
285  }*/
286}
287
288
289void MappedWater::deactivateReflection()
290{
291//  glBindTexture(GL_TEXTURE_2D, texture.getTexture()); //is done by mat.select();
292  //glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0));
293
294  //mat.setDiffuseMap(&texture, 0);
295  mat.select();
296  glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
297  //glDisable(GL_CLIP_PLANE0);
298
299  glPopMatrix();
300
301  glPopAttrib();
302}
303
304void MappedWater::activateRefraction()
305{
306}
307
308void MappedWater::deactivateRefraction()
309{
310}
Note: See TracBrowser for help on using the repository browser.