Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7814 was 7814, checked in by stefalie, 19 years ago

branches/water: textures works now, but its at the wrong place

File size: 8.7 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
21
22
23
24CREATE_FACTORY(MappedWater, CL_MAPPED_WATER);
25
26
27MappedWater::MappedWater(const TiXmlElement* root)
28    : texture(GL_TEXTURE_2D)
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(&this->refractionTexture, 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(&this->depthTexture, 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* pTexture = new unsigned int [this->textureSize * this->textureSize * channels];
59  memset(pTexture, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int));
60  // Register the texture with OpenGL and bind it to the texture ID
61  mat.select();
62  glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0));
63  printf("========= CREATE %d\n", this->texture.getTexture());
64
65  // Create the texture and store it on the video card
66  glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTexture);
67
68  //gluBuild2DMipmaps(GL_TEXTURE_2D, channels, this->textureSize, this->textureSize, type,  GL_UNSIGNED_INT, pTexture);
69
70  // Set the texture quality
71  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
72  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
73  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
74  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
75
76  // Since we stored the texture space with OpenGL, we can delete the image data
77  delete [] pTexture;
78
79
80  //shader = new Shader( "/home/stefalie/svn/orxonox/data/trunk/shaders/water.vert", "/home/stefalie/svn/orxonox/data/trunk/shaders/mapped_water.frag");
81  shader = new Shader( ResourceManager::getInstance()->getDataDir() + "/shaders/mapped_water.vert", ResourceManager::getInstance()->getDataDir() +"/shaders/mapped_water.frag");
82}
83
84MappedWater::~MappedWater()
85{
86  //delete shader;
87}
88
89void MappedWater::loadParams(const TiXmlElement* root)
90{
91  WorldEntity::loadParams(root);
92
93  LoadParam(root, "waterHeight", this, MappedWater, setHeight);
94}
95
96
97void MappedWater::draw() const
98{
99  glPushMatrix();
100  glTranslatef(0,this->waterHeight,0);
101
102  //HACK
103  glTranslatef(550,0,100);
104  //glEnable(GL_LIGHTING);
105
106
107  //mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 1);
108  //mat.setTransparency(1.0);
109  //mat.setDiffuse(1.0, 0, .1);
110
111
112  // HACK
113
114  glDisable(GL_BLEND);
115  //glActiveTexture(GL_TEXTURE0);
116  //glBindTexture(GL_TEXTURE_2D, this->texture);
117
118  mat.select();
119  glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0));
120
121
122/*  // Shader init
123  //this->shader->activateShader();
124  GLint uniform;
125
126  // Set the variable "reflection" to correspond to the first texture unit
127  uniform = glGetUniformLocationARB(shader->getProgram(), "reflection");
128  glUniform1iARB(uniform, 0); //second paramter is the texture unit
129
130  // Set the variable "refraction" to correspond to the second texture unit
131  //uniform = glGetUniformLocationARB(shader->getProgram(), "refraction");
132  //glUniform1iARB(uniform, 0);
133  // FIXME glUniform1iARB(uniform, 1);
134
135  // Set the variable "normalMap" to correspond to the third texture unit
136  //uniform = glGetUniformLocationARB(shader->getProgram(), "normalMap");
137  //glUniform1iARB(uniform, 2);
138
139  // Set the variable "dudvMap" to correspond to the fourth texture unit
140  uniform = glGetUniformLocationARB(shader->getProgram(), "dudvMap");
141  glUniform1iARB(uniform, 3);
142
143  // Set the variable "depthMap" to correspond to the fifth texture unit
144  //uniform = glGetUniformLocationARB(shader->getProgram(), "depthMap");
145  //glUniform1iARB(uniform, 4);
146  // FIXME we dont have a depthMap yet :-(
147
148  // Give the variable "waterColor" a blue color
149  uniform = glGetUniformLocationARB(shader->getProgram(), "waterColor");
150  glUniform4fARB(uniform, 0.1f, 0.2f, 0.4f, 1.0f);
151
152  // FIXME set camera and light information
153  Vector lightPos(100.0f, 150.0f, 100.0f);
154
155  // Store the camera position in a variable
156  //CVector3 vPosition = g_Camera.Position();
157  Vector vPosition(50.0f, 50.0f, 50.0f);
158
159  // Give the variable "lightPos" our hard coded light position
160  uniform = glGetUniformLocationARB(shader->getProgram(), "lightPos");
161  glUniform4fARB(uniform, lightPos.x, lightPos.y, lightPos.z, 1.0f);
162
163  // Give the variable "cameraPos" our camera position
164  uniform = glGetUniformLocationARB(shader->getProgram(), "cameraPos");
165  glUniform4fARB(uniform, vPosition.x, vPosition.y, vPosition.z, 1.0f);
166*/
167  glBegin(GL_QUADS);
168  glNormal3f(0, 1, 0);
169  glMultiTexCoord2f(GL_TEXTURE0, 0, 0);
170  glMultiTexCoord2f(GL_TEXTURE1, 0, 0);
171  glMultiTexCoord2f(GL_TEXTURE2, 0, 0);
172  glMultiTexCoord2f(GL_TEXTURE3, 0, 0);
173  glMultiTexCoord2f(GL_TEXTURE4, 0, 0);
174  glVertex3f(-500, 0, 500);
175
176  glMultiTexCoord2f(GL_TEXTURE0, 1, 0);
177  glMultiTexCoord2f(GL_TEXTURE1, 1, 0);
178  glMultiTexCoord2f(GL_TEXTURE2, 1, 0);
179  glMultiTexCoord2f(GL_TEXTURE3, 1, 0);
180  glMultiTexCoord2f(GL_TEXTURE4, 1, 0);
181  glVertex3f(500, 0, 500);
182
183  glMultiTexCoord2f(GL_TEXTURE0, 1, 1);
184  glMultiTexCoord2f(GL_TEXTURE1, 1, 1);
185  glMultiTexCoord2f(GL_TEXTURE2, 1, 1);
186  glMultiTexCoord2f(GL_TEXTURE3, 1, 1);
187  glMultiTexCoord2f(GL_TEXTURE4, 1, 1);
188  glVertex3f(500, 0, -500);
189
190  glMultiTexCoord2f(GL_TEXTURE0, 0, 1);
191  glMultiTexCoord2f(GL_TEXTURE1, 0, 1);
192  glMultiTexCoord2f(GL_TEXTURE2, 0, 1);
193  glMultiTexCoord2f(GL_TEXTURE3, 0, 1);
194  glMultiTexCoord2f(GL_TEXTURE4, 0, 1);
195  glVertex3f(-500, 0, -500);
196  glEnd();
197  //this->shader->deactivateShader();
198
199  glPopMatrix();
200}
201
202void MappedWater::tick(float dt)
203{
204}
205
206void MappedWater::setHeight(float height)
207{
208  this->waterHeight = height;
209}
210
211void MappedWater::activateReflection()
212{
213  glPushAttrib(GL_VIEWPORT_BIT);
214 
215
216  //glLoadIdentity();
217  glViewport(0,0, textureSize, textureSize);
218
219  glPushMatrix();
220  // Clear the color and depth bits, reset the matrix and position our camera.
221
222  //g_Camera.Look();
223
224
225  // If our camera is above the water we will render the scene flipped upside down.
226  // In order to line up the reflection nicely with the world we have to translate
227  // the world to the position of our reflected surface, multiplied by two.
228  //if(g_Camera.Position().y > waterHeight)
229  //{
230  // Translate the world, then flip it upside down
231  glTranslatef(0.0f, this->waterHeight*2.0f, 0.0f);
232  glScalef(1.0, -1.0, 1.0);
233
234  // Since the world is updside down we need to change the culling to FRONT
235  glCullFace(GL_FRONT);
236
237  // Set our plane equation and turn clipping on
238  double plane[4] = {0.0, 1.0, 0.0, -waterHeight};
239  //glEnable(GL_CLIP_PLANE0);
240  //glClipPlane(GL_CLIP_PLANE0, plane);
241
242  // Render the world upside down and clipped (only render the top flipped).
243  // If we don't turn OFF caustics for the reflection texture we get horrible
244  // artifacts in the water.  That is why we set bRenderCaustics to FALSE.
245  //RenderWorld(false);
246
247  // Turn clipping off
248  // glDisable(GL_CLIP_PLANE0);
249
250  // Restore back-face culling
251  //  glCullFace(GL_BACK);
252  //}
253  /*else
254  {
255      // If the camera is below the water we don't want to flip the world,
256      // but just render it clipped so only the top is drawn.
257      double plane[4] = {0.0, 1.0, 0.0, waterHeight};
258      glEnable(GL_CLIP_PLANE0);
259      glClipPlane(GL_CLIP_PLANE0, plane);
260      RenderWorld(true);
261      glDisable(GL_CLIP_PLANE0);
262  }*/
263}
264
265
266void MappedWater::deactivateReflection()
267{
268//  glBindTexture(GL_TEXTURE_2D, texture.getTexture()); //is done by mat.select();
269  //glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0));
270
271  //mat.setDiffuseMap(&texture, 0);
272  mat.select();
273  glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
274  //glDisable(GL_CLIP_PLANE0);
275
276  glPopMatrix();
277
278  glPopAttrib();
279 
280}
281
282void MappedWater::activateRefraction()
283{
284}
285
286void MappedWater::deactivateRefraction()
287{
288}
Note: See TracBrowser for help on using the repository browser.