[7796] | 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" |
---|
[7823] | 20 | #include "state.h" |
---|
[7796] | 21 | |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | CREATE_FACTORY(MappedWater, CL_MAPPED_WATER); |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | MappedWater::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 |
---|
[7814] | 38 | // FIXME mat.setDiffuseMap(this->texture, 0); doesnt work, |
---|
[7796] | 39 | mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 0); |
---|
| 40 | // load refraction texture |
---|
[7823] | 41 | mat.setDiffuseMap("pictures/sky-replace.jpg", 1); |
---|
[7796] | 42 | // load normal map |
---|
[7823] | 43 | mat.setDiffuseMap("pictures/normalmap.bmp", GL_TEXTURE_2D, 2); |
---|
[7796] | 44 | // load dudv map |
---|
[7823] | 45 | mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 3); |
---|
[7796] | 46 | // set up depth texture |
---|
[7823] | 47 | mat.setDiffuseMap("pictures/sky-replace.jpg", 4); |
---|
[7796] | 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; |
---|
[7823] | 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)); |
---|
[7796] | 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 |
---|
[7823] | 67 | glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureReflection); |
---|
[7796] | 68 | |
---|
[7814] | 69 | //gluBuild2DMipmaps(GL_TEXTURE_2D, channels, this->textureSize, this->textureSize, type, GL_UNSIGNED_INT, pTexture); |
---|
| 70 | |
---|
[7823] | 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 | |
---|
[7796] | 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 |
---|
[7823] | 82 | delete [] pTextureReflection; |
---|
| 83 | delete [] pTextureRefraction; |
---|
[7796] | 84 | |
---|
[7814] | 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"); |
---|
[7823] | 88 | |
---|
| 89 | this->move = 0.0f; |
---|
| 90 | this->g_WaterUV = 35.0f; |
---|
| 91 | this->kNormalMapScale = 0.25f; |
---|
| 92 | this->g_WaterFlow = 0.0015f; |
---|
[7796] | 93 | } |
---|
| 94 | |
---|
| 95 | MappedWater::~MappedWater() |
---|
| 96 | { |
---|
| 97 | //delete shader; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | void MappedWater::loadParams(const TiXmlElement* root) |
---|
| 101 | { |
---|
| 102 | WorldEntity::loadParams(root); |
---|
| 103 | |
---|
| 104 | LoadParam(root, "waterHeight", this, MappedWater, setHeight); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | void MappedWater::draw() const |
---|
| 109 | { |
---|
| 110 | glPushMatrix(); |
---|
| 111 | glTranslatef(0,this->waterHeight,0); |
---|
| 112 | |
---|
[7814] | 113 | //HACK |
---|
[7817] | 114 | |
---|
[7796] | 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 | |
---|
[7814] | 125 | glDisable(GL_BLEND); |
---|
[7796] | 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 | |
---|
[7814] | 132 | |
---|
[7816] | 133 | // Shader init |
---|
[7823] | 134 | this->shader->activateShader(); |
---|
| 135 | GLint uniform; |
---|
[7796] | 136 | |
---|
| 137 | // Set the variable "reflection" to correspond to the first texture unit |
---|
[7823] | 138 | uniform = glGetUniformLocationARB(shader->getProgram(), "reflection"); |
---|
| 139 | glUniform1iARB(uniform, 0); //second paramter is the texture unit |
---|
[7796] | 140 | |
---|
| 141 | // Set the variable "refraction" to correspond to the second texture unit |
---|
[7823] | 142 | uniform = glGetUniformLocationARB(shader->getProgram(), "refraction"); |
---|
| 143 | glUniform1iARB(uniform, 1); |
---|
[7796] | 144 | |
---|
| 145 | // Set the variable "normalMap" to correspond to the third texture unit |
---|
[7823] | 146 | uniform = glGetUniformLocationARB(shader->getProgram(), "normalMap"); |
---|
| 147 | glUniform1iARB(uniform, 2); |
---|
[7796] | 148 | |
---|
| 149 | // Set the variable "dudvMap" to correspond to the fourth texture unit |
---|
[7823] | 150 | uniform = glGetUniformLocationARB(shader->getProgram(), "dudvMap"); |
---|
| 151 | glUniform1iARB(uniform, 3); |
---|
[7796] | 152 | |
---|
| 153 | // Set the variable "depthMap" to correspond to the fifth texture unit |
---|
[7823] | 154 | uniform = glGetUniformLocationARB(shader->getProgram(), "depthMap"); |
---|
| 155 | glUniform1iARB(uniform, 4); |
---|
[7796] | 156 | // FIXME we dont have a depthMap yet :-( |
---|
| 157 | |
---|
| 158 | // Give the variable "waterColor" a blue color |
---|
[7823] | 159 | uniform = glGetUniformLocationARB(shader->getProgram(), "waterColor"); |
---|
| 160 | glUniform4fARB(uniform, 0.1f, 0.2f, 0.4f, 1.0f); |
---|
[7796] | 161 | |
---|
| 162 | // FIXME set camera and light information |
---|
[7823] | 163 | Vector lightPos(100.0f, 600.0f, 100.0f); |
---|
| 164 | Vector vPosition = State::getCameraNode()->getAbsCoor(); |
---|
[7796] | 165 | // Store the camera position in a variable |
---|
| 166 | //CVector3 vPosition = g_Camera.Position(); |
---|
[7816] | 167 | //Vector vPosition(50.0f, 50.0f, 50.0f); |
---|
[7796] | 168 | |
---|
| 169 | // Give the variable "lightPos" our hard coded light position |
---|
[7823] | 170 | uniform = glGetUniformLocationARB(shader->getProgram(), "lightPos"); |
---|
| 171 | glUniform4fARB(uniform, lightPos.x, lightPos.y, lightPos.z, 1.0f); |
---|
[7796] | 172 | |
---|
| 173 | // Give the variable "cameraPos" our camera position |
---|
[7823] | 174 | uniform = glGetUniformLocationARB(shader->getProgram(), "cameraPos"); |
---|
| 175 | glUniform4fARB(uniform, vPosition.x, vPosition.y, vPosition.z, 1.0f); |
---|
[7816] | 176 | |
---|
[7823] | 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 | |
---|
[7796] | 184 | glBegin(GL_QUADS); |
---|
[7823] | 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); |
---|
[7796] | 192 | |
---|
[7823] | 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); |
---|
[7796] | 200 | |
---|
[7823] | 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); |
---|
[7796] | 208 | |
---|
[7823] | 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); |
---|
[7796] | 216 | glEnd(); |
---|
| 217 | |
---|
[7816] | 218 | this->shader->deactivateShader(); |
---|
| 219 | |
---|
[7823] | 220 | |
---|
[7796] | 221 | glPopMatrix(); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | void MappedWater::tick(float dt) |
---|
| 225 | { |
---|
[7823] | 226 | this->move += this->g_WaterFlow; |
---|
[7796] | 227 | } |
---|
| 228 | |
---|
| 229 | void MappedWater::setHeight(float height) |
---|
| 230 | { |
---|
| 231 | this->waterHeight = height; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | void MappedWater::activateReflection() |
---|
| 235 | { |
---|
| 236 | glPushAttrib(GL_VIEWPORT_BIT); |
---|
[7814] | 237 | |
---|
[7796] | 238 | |
---|
| 239 | //glLoadIdentity(); |
---|
| 240 | glViewport(0,0, textureSize, textureSize); |
---|
| 241 | |
---|
[7814] | 242 | glPushMatrix(); |
---|
[7796] | 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 |
---|
[7814] | 254 | glTranslatef(0.0f, this->waterHeight*2.0f, 0.0f); |
---|
| 255 | glScalef(1.0, -1.0, 1.0); |
---|
[7796] | 256 | |
---|
| 257 | // Since the world is updside down we need to change the culling to FRONT |
---|
[7814] | 258 | glCullFace(GL_FRONT); |
---|
[7796] | 259 | |
---|
| 260 | // Set our plane equation and turn clipping on |
---|
[7814] | 261 | double plane[4] = {0.0, 1.0, 0.0, -waterHeight}; |
---|
| 262 | //glEnable(GL_CLIP_PLANE0); |
---|
| 263 | //glClipPlane(GL_CLIP_PLANE0, plane); |
---|
[7796] | 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 | |
---|
| 289 | void MappedWater::deactivateReflection() |
---|
| 290 | { |
---|
| 291 | // glBindTexture(GL_TEXTURE_2D, texture.getTexture()); //is done by mat.select(); |
---|
[7814] | 292 | //glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0)); |
---|
[7796] | 293 | |
---|
| 294 | //mat.setDiffuseMap(&texture, 0); |
---|
[7814] | 295 | mat.select(); |
---|
[7796] | 296 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize); |
---|
[7814] | 297 | //glDisable(GL_CLIP_PLANE0); |
---|
[7796] | 298 | |
---|
[7814] | 299 | glPopMatrix(); |
---|
[7796] | 300 | |
---|
| 301 | glPopAttrib(); |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | void MappedWater::activateRefraction() |
---|
| 305 | { |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | void MappedWater::deactivateRefraction() |
---|
| 309 | { |
---|
| 310 | } |
---|