[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 |
---|
[7982] | 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 |
---|
[7983] | 45 | mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 3); |
---|
[7796] | 46 | // set up depth texture |
---|
[7983] | 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)); |
---|
[7983] | 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 |
---|
[7983] | 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); |
---|
[7823] | 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; |
---|
[7983] | 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; |
---|
[7983] | 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(); |
---|
[7796] | 113 | } |
---|
| 114 | |
---|
| 115 | MappedWater::~MappedWater() |
---|
| 116 | { |
---|
| 117 | //delete shader; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | void MappedWater::loadParams(const TiXmlElement* root) |
---|
| 121 | { |
---|
| 122 | WorldEntity::loadParams(root); |
---|
| 123 | |
---|
| 124 | LoadParam(root, "waterHeight", this, MappedWater, setHeight); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | |
---|
| 128 | void MappedWater::draw() const |
---|
| 129 | { |
---|
| 130 | glPushMatrix(); |
---|
| 131 | glTranslatef(0,this->waterHeight,0); |
---|
| 132 | |
---|
[7982] | 133 | mat.unselect(); |
---|
[7796] | 134 | mat.select(); |
---|
| 135 | |
---|
[7814] | 136 | |
---|
[7983] | 137 | |
---|
[7816] | 138 | // Shader init |
---|
[7823] | 139 | this->shader->activateShader(); |
---|
| 140 | GLint uniform; |
---|
[7796] | 141 | |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | // Give the variable "waterColor" a blue color |
---|
[7823] | 147 | uniform = glGetUniformLocationARB(shader->getProgram(), "waterColor"); |
---|
| 148 | glUniform4fARB(uniform, 0.1f, 0.2f, 0.4f, 1.0f); |
---|
[7796] | 149 | |
---|
| 150 | // FIXME set camera and light information |
---|
[7823] | 151 | Vector lightPos(100.0f, 600.0f, 100.0f); |
---|
| 152 | Vector vPosition = State::getCameraNode()->getAbsCoor(); |
---|
[7796] | 153 | // Store the camera position in a variable |
---|
| 154 | //CVector3 vPosition = g_Camera.Position(); |
---|
[7816] | 155 | //Vector vPosition(50.0f, 50.0f, 50.0f); |
---|
[7796] | 156 | |
---|
| 157 | // Give the variable "lightPos" our hard coded light position |
---|
[7823] | 158 | uniform = glGetUniformLocationARB(shader->getProgram(), "lightPos"); |
---|
| 159 | glUniform4fARB(uniform, lightPos.x, lightPos.y, lightPos.z, 1.0f); |
---|
[7796] | 160 | |
---|
| 161 | // Give the variable "cameraPos" our camera position |
---|
[7823] | 162 | uniform = glGetUniformLocationARB(shader->getProgram(), "cameraPos"); |
---|
| 163 | glUniform4fARB(uniform, vPosition.x, vPosition.y, vPosition.z, 1.0f); |
---|
[7816] | 164 | |
---|
[7823] | 165 | |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | |
---|
[7982] | 169 | |
---|
[7796] | 170 | glBegin(GL_QUADS); |
---|
[7823] | 171 | // The back left vertice for the water |
---|
[7983] | 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 |
---|
[7823] | 177 | glVertex3f(0.0f, waterHeight, 0.0f); |
---|
[7796] | 178 | |
---|
[7823] | 179 | // The front left vertice for the water |
---|
[7983] | 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 |
---|
[7823] | 185 | glVertex3f(0.0f, waterHeight, 1000.0f); |
---|
[7796] | 186 | |
---|
[7823] | 187 | // The front right vertice for the water |
---|
[7983] | 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 |
---|
[7823] | 193 | glVertex3f(1000.0f, waterHeight, 1000.0f); |
---|
[7796] | 194 | |
---|
[7823] | 195 | // The back right vertice for the water |
---|
[7983] | 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 |
---|
[7823] | 201 | glVertex3f(1000.0f, waterHeight, 0.0f); |
---|
[7796] | 202 | glEnd(); |
---|
| 203 | |
---|
[7816] | 204 | this->shader->deactivateShader(); |
---|
| 205 | |
---|
[7983] | 206 | mat.unselect(); |
---|
[7823] | 207 | |
---|
[7796] | 208 | glPopMatrix(); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | void MappedWater::tick(float dt) |
---|
| 212 | { |
---|
[7983] | 213 | // makes the water flow |
---|
| 214 | this->move += this->g_WaterFlow; |
---|
[7982] | 215 | this->move2 = this->move * this->kNormalMapScale; |
---|
| 216 | this->refrUV = this->g_WaterUV; |
---|
| 217 | this->normalUV = this->g_WaterUV * this->kNormalMapScale; |
---|
[7796] | 218 | } |
---|
| 219 | |
---|
| 220 | void MappedWater::setHeight(float height) |
---|
| 221 | { |
---|
| 222 | this->waterHeight = height; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | void MappedWater::activateReflection() |
---|
| 226 | { |
---|
| 227 | glPushAttrib(GL_VIEWPORT_BIT); |
---|
| 228 | |
---|
[7983] | 229 | |
---|
[7796] | 230 | //glLoadIdentity(); |
---|
| 231 | glViewport(0,0, textureSize, textureSize); |
---|
| 232 | |
---|
[7814] | 233 | glPushMatrix(); |
---|
[7796] | 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 |
---|
[7814] | 245 | glTranslatef(0.0f, this->waterHeight*2.0f, 0.0f); |
---|
| 246 | glScalef(1.0, -1.0, 1.0); |
---|
[7796] | 247 | |
---|
| 248 | // Since the world is updside down we need to change the culling to FRONT |
---|
[7983] | 249 | //glCullFace(GL_FRONT); |
---|
[7796] | 250 | |
---|
| 251 | // Set our plane equation and turn clipping on |
---|
[7983] | 252 | //double plane[4] = {0.0, 1.0, 0.0, -waterHeight}; |
---|
[7814] | 253 | //glEnable(GL_CLIP_PLANE0); |
---|
| 254 | //glClipPlane(GL_CLIP_PLANE0, plane); |
---|
[7796] | 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 | |
---|
| 280 | void MappedWater::deactivateReflection() |
---|
| 281 | { |
---|
| 282 | // glBindTexture(GL_TEXTURE_2D, texture.getTexture()); //is done by mat.select(); |
---|
[7814] | 283 | //glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0)); |
---|
[7796] | 284 | |
---|
| 285 | //mat.setDiffuseMap(&texture, 0); |
---|
[7983] | 286 | |
---|
[7814] | 287 | mat.select(); |
---|
[7796] | 288 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize); |
---|
[7814] | 289 | //glDisable(GL_CLIP_PLANE0); |
---|
[7796] | 290 | |
---|
[7814] | 291 | glPopMatrix(); |
---|
[7796] | 292 | |
---|
| 293 | glPopAttrib(); |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | void MappedWater::activateRefraction() |
---|
| 297 | { |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | void MappedWater::deactivateRefraction() |
---|
| 301 | { |
---|
| 302 | } |
---|