| 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 | CREATE_FACTORY(MappedWater, CL_MAPPED_WATER); |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * @brief constructor |
|---|
| 27 | * @param root xml data |
|---|
| 28 | */ |
|---|
| 29 | MappedWater::MappedWater(const TiXmlElement* root) |
|---|
| 30 | { |
|---|
| 31 | this->setClassID(CL_MAPPED_WATER, "MappedWater"); |
|---|
| 32 | this->toList(OM_ENVIRON); |
|---|
| 33 | |
|---|
| 34 | if (root != NULL) |
|---|
| 35 | this->loadParams(root); |
|---|
| 36 | |
|---|
| 37 | /// loads the textures |
|---|
| 38 | // sets parameters for the textures |
|---|
| 39 | this->textureSize = 512; |
|---|
| 40 | unsigned int channels = 32; |
|---|
| 41 | GLenum type = GL_RGBA; |
|---|
| 42 | // set up refleciton texture |
|---|
| 43 | Texture reflTex(GL_TEXTURE_2D, this->textureSize, this->textureSize, channels, type); |
|---|
| 44 | mat.setDiffuseMap(reflTex, 0); |
|---|
| 45 | //mat.setDiffuseMap("pictures/refl.bmp", GL_TEXTURE_2D, 0); |
|---|
| 46 | // load refraction texture |
|---|
| 47 | Texture refrTex(GL_TEXTURE_2D, this->textureSize, this->textureSize, channels, type); |
|---|
| 48 | mat.setDiffuseMap(refrTex, 1); |
|---|
| 49 | //mat.setDiffuseMap("pictures/refr.bmp", GL_TEXTURE_2D, 1); |
|---|
| 50 | // load normal map |
|---|
| 51 | mat.setDiffuseMap("pictures/normalmap.bmp", GL_TEXTURE_2D, 2); |
|---|
| 52 | // load dudv map |
|---|
| 53 | mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 3); |
|---|
| 54 | // set up depth texture |
|---|
| 55 | //mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 4); |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | // set the size of the refraction and reflection textures |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | // initialization of the texture coords, speeds etc... |
|---|
| 62 | this->move = 0.0f; |
|---|
| 63 | this->kNormalMapScale = 0.25f; |
|---|
| 64 | |
|---|
| 65 | //unsigned int channels = 32; |
|---|
| 66 | //GLenum type = GL_RGBA; |
|---|
| 67 | //unsigned int* pTextureReflection = new unsigned int [this->textureSize * this->textureSize * channels]; |
|---|
| 68 | //memset(pTextureReflection, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int)); |
|---|
| 69 | //unsigned int* pTextureRefraction = new unsigned int [this->textureSize * this->textureSize * channels]; |
|---|
| 70 | //memset(pTextureRefraction, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int)); |
|---|
| 71 | // Register the texture with OpenGL and bind it to the texture ID |
|---|
| 72 | //mat.select(); |
|---|
| 73 | //glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0)); |
|---|
| 74 | |
|---|
| 75 | // Create the texture and store it on the video card |
|---|
| 76 | //glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureReflection); |
|---|
| 77 | |
|---|
| 78 | //gluBuild2DMipmaps(GL_TEXTURE_2D, channels, this->textureSize, this->textureSize, type, GL_UNSIGNED_INT, pTexture); |
|---|
| 79 | |
|---|
| 80 | //the same for the refraction |
|---|
| 81 | //glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(1)); |
|---|
| 82 | //glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureRefraction); |
|---|
| 83 | /* |
|---|
| 84 | unsigned int channels = 32; |
|---|
| 85 | GLenum type = GL_RGBA; |
|---|
| 86 | unsigned int* pTextureReflection = new unsigned int [this->textureSize * this->textureSize * channels]; |
|---|
| 87 | memset(pTextureReflection, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int)); |
|---|
| 88 | //mat.select();*/ |
|---|
| 89 | glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0)); |
|---|
| 90 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); |
|---|
| 91 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); |
|---|
| 92 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
|---|
| 93 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
|---|
| 94 | //glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureReflection); |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | //unsigned int* pTextureRefraction = new unsigned int [this->textureSize * this->textureSize * channels]; |
|---|
| 98 | //memset(pTextureRefraction, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int)); |
|---|
| 99 | glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(1)); |
|---|
| 100 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); |
|---|
| 101 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); |
|---|
| 102 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
|---|
| 103 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
|---|
| 104 | //glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureRefraction); |
|---|
| 105 | |
|---|
| 106 | // Set the texture quality |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | // Since we stored the texture space with OpenGL, we can delete the image data |
|---|
| 110 | //delete [] pTextureReflection; |
|---|
| 111 | //delete [] pTextureRefraction; |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | /// initialization of the shaders |
|---|
| 115 | this->initShaders(); |
|---|
| 116 | |
|---|
| 117 | /// fog, doesnt work the way i want it to work |
|---|
| 118 | /*this->fog = new FogEffect(); |
|---|
| 119 | fog->setFogColor(0.1f, 0.2f, 0.4f); |
|---|
| 120 | fog->setFogRange(0.0f, 500.0f); |
|---|
| 121 | fog->setFogDensity(0.03f);*/ |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | /** |
|---|
| 125 | * @brief deltes shader and the uniform used by the camera |
|---|
| 126 | */ |
|---|
| 127 | MappedWater::~MappedWater() |
|---|
| 128 | { |
|---|
| 129 | delete shader; |
|---|
| 130 | delete cam_uni; |
|---|
| 131 | //delete fog; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /** |
|---|
| 135 | * @brief initialization of the shaders |
|---|
| 136 | */ |
|---|
| 137 | void MappedWater::initShaders() |
|---|
| 138 | { |
|---|
| 139 | // load shader files |
|---|
| 140 | shader = new Shader( ResourceManager::getInstance()->getDataDir() + "/shaders/mapped_water.vert", ResourceManager::getInstance()->getDataDir() +"/shaders/mapped_water.frag"); |
|---|
| 141 | |
|---|
| 142 | this->shader->activateShader(); |
|---|
| 143 | // Set the variable "reflection" to correspond to the first texture unit |
|---|
| 144 | Shader::Uniform(shader, "reflection").set(0); |
|---|
| 145 | // Set the variable "refraction" to correspond to the second texture unit |
|---|
| 146 | Shader::Uniform(shader, "refraction").set(1); |
|---|
| 147 | // Set the variable "normalMap" to correspond to the third texture unit |
|---|
| 148 | Shader::Uniform(shader, "normalMap").set(2); |
|---|
| 149 | // Set the variable "dudvMap" to correspond to the fourth texture unit |
|---|
| 150 | Shader::Uniform(shader, "dudvMap").set(3); |
|---|
| 151 | // Set the variable "depthMap" to correspond to the fifth texture unit |
|---|
| 152 | Shader::Uniform(shader, "depthMap").set(2); |
|---|
| 153 | // Give the variable "waterColor" a blue color |
|---|
| 154 | Shader::Uniform(shader, "waterColor").set(0.1f, 0.2f, 0.4f, 1.0f); |
|---|
| 155 | // Give the variable "lightPos" our hard coded light position |
|---|
| 156 | Shader::Uniform(shader, "lightPos").set(lightPos.x, lightPos.y, lightPos.z, 1.0f); |
|---|
| 157 | // uniform for the camera position |
|---|
| 158 | cam_uni = new Shader::Uniform(shader, "cameraPos"); |
|---|
| 159 | |
|---|
| 160 | this->shader->deactivateShader(); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | /** |
|---|
| 164 | * @brief ends the refraction and saves the graphic buffer into a texture |
|---|
| 165 | * @param root xml data |
|---|
| 166 | */ |
|---|
| 167 | void MappedWater::loadParams(const TiXmlElement* root) |
|---|
| 168 | { |
|---|
| 169 | WorldEntity::loadParams(root); |
|---|
| 170 | |
|---|
| 171 | LoadParam(root, "waterpos", this, MappedWater, setWaterPos); |
|---|
| 172 | LoadParam(root, "watersize", this, MappedWater, setWaterSize); |
|---|
| 173 | LoadParam(root, "lightpos", this, MappedWater, setLightPosition); |
|---|
| 174 | LoadParam(root, "wateruv", this, MappedWater, setWaterUV); |
|---|
| 175 | LoadParam(root, "waterflow", this, MappedWater, setWaterFlow); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | /** |
|---|
| 179 | * @brief activates the water shader and draws a quad with four textures on it |
|---|
| 180 | */ |
|---|
| 181 | void MappedWater::draw() const |
|---|
| 182 | { |
|---|
| 183 | glMatrixMode(GL_MODELVIEW); |
|---|
| 184 | |
|---|
| 185 | glPushMatrix(); |
|---|
| 186 | glTranslatef(this->waterPos.x ,0 ,this->waterPos.z); |
|---|
| 187 | |
|---|
| 188 | mat.select(); |
|---|
| 189 | |
|---|
| 190 | this->shader->activateShader(); |
|---|
| 191 | |
|---|
| 192 | // reset the camera uniform to the current cam position |
|---|
| 193 | Vector pos = State::getCameraNode()->getAbsCoor(); |
|---|
| 194 | cam_uni->set(pos.x, pos.y, pos.z, 1.0f); |
|---|
| 195 | |
|---|
| 196 | glDisable(GL_BLEND); |
|---|
| 197 | |
|---|
| 198 | glBegin(GL_QUADS); |
|---|
| 199 | // The back left vertice for the water |
|---|
| 200 | glMultiTexCoord2f(GL_TEXTURE0, 0.0f, g_WaterUV); // Reflection texture |
|---|
| 201 | glMultiTexCoord2f(GL_TEXTURE1, 0.0f, refrUV - move); // Refraction texture |
|---|
| 202 | glMultiTexCoord2f(GL_TEXTURE2, 0.0f, normalUV + move2); // Normal map texture |
|---|
| 203 | glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture |
|---|
| 204 | glVertex3f(0.0f, this->waterPos.y, 0.0f); |
|---|
| 205 | |
|---|
| 206 | // The front left vertice for the water |
|---|
| 207 | glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 0.0f); // Reflection texture |
|---|
| 208 | glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 0.0f - move); // Refraction texture |
|---|
| 209 | glMultiTexCoord2f(GL_TEXTURE2, 0.0f, 0.0f + move2); // Normal map texture |
|---|
| 210 | glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture |
|---|
| 211 | glVertex3f(0.0f, this->waterPos.y, this->zWidth); |
|---|
| 212 | |
|---|
| 213 | // The front right vertice for the water |
|---|
| 214 | glMultiTexCoord2f(GL_TEXTURE0, g_WaterUV, 0.0f); // Reflection texture |
|---|
| 215 | glMultiTexCoord2f(GL_TEXTURE1, refrUV, 0.0f - move); // Refraction texture |
|---|
| 216 | glMultiTexCoord2f(GL_TEXTURE2, normalUV, 0.0f + move2); // Normal map texture |
|---|
| 217 | glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture |
|---|
| 218 | glVertex3f(this->xWidth, this->waterPos.y, this->zWidth); |
|---|
| 219 | |
|---|
| 220 | // The back right vertice for the water |
|---|
| 221 | glMultiTexCoord2f(GL_TEXTURE0, g_WaterUV, g_WaterUV); // Reflection texture |
|---|
| 222 | glMultiTexCoord2f(GL_TEXTURE1, refrUV, refrUV - move); // Refraction texture |
|---|
| 223 | glMultiTexCoord2f(GL_TEXTURE2, normalUV, normalUV + move2); // Normal map texture |
|---|
| 224 | glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture |
|---|
| 225 | glVertex3f(this->xWidth, this->waterPos.y, 0.0f); |
|---|
| 226 | glEnd(); |
|---|
| 227 | |
|---|
| 228 | this->shader->deactivateShader(); |
|---|
| 229 | |
|---|
| 230 | mat.unselect(); |
|---|
| 231 | |
|---|
| 232 | /*if(pos.y < this->waterPos.y) |
|---|
| 233 | { |
|---|
| 234 | // draw some fog |
|---|
| 235 | this->fog->activate(); |
|---|
| 236 | } |
|---|
| 237 | else |
|---|
| 238 | { |
|---|
| 239 | this->fog->deactivate(); |
|---|
| 240 | }*/ |
|---|
| 241 | |
|---|
| 242 | glPopMatrix(); |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | /** |
|---|
| 246 | * @brief tick tack, calculates the flow of the water |
|---|
| 247 | */ |
|---|
| 248 | void MappedWater::tick(float dt) |
|---|
| 249 | { |
|---|
| 250 | // makes the water flow |
|---|
| 251 | // TODO change it so that the waterflow doesnt depend on the frame rate |
|---|
| 252 | this->move += this->g_WaterFlow; |
|---|
| 253 | this->move2 = this->move * this->kNormalMapScale; |
|---|
| 254 | this->refrUV = this->g_WaterUV; |
|---|
| 255 | this->normalUV = this->g_WaterUV * this->kNormalMapScale; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | /** |
|---|
| 259 | * @brief prepares everything to render the reflection texutre |
|---|
| 260 | */ |
|---|
| 261 | void MappedWater::activateReflection() |
|---|
| 262 | { |
|---|
| 263 | // To create the reflection texture we just need to set the view port |
|---|
| 264 | // to our texture map size, then render the current scene our camera |
|---|
| 265 | // is looking at to the already allocated texture unit. Since this |
|---|
| 266 | // is a reflection of the top of the water surface we use clipping |
|---|
| 267 | // planes to only render the top of the world as a reflection. If |
|---|
| 268 | // we are below the water we don't flip the reflection but just use |
|---|
| 269 | // the current view of the top as we are seeing through the water. |
|---|
| 270 | // When you look through water at the surface it isn't really reflected, |
|---|
| 271 | // only when looking down from above the water on the surface. |
|---|
| 272 | |
|---|
| 273 | // save viewport matrix and change the viewport size |
|---|
| 274 | glPushAttrib(GL_VIEWPORT_BIT); |
|---|
| 275 | glViewport(0,0, textureSize, textureSize); |
|---|
| 276 | |
|---|
| 277 | glMatrixMode(GL_MODELVIEW); |
|---|
| 278 | glPushMatrix(); |
|---|
| 279 | |
|---|
| 280 | // If our camera is above the water we will render the scene flipped upside down. |
|---|
| 281 | // In order to line up the reflection nicely with the world we have to translate |
|---|
| 282 | // the world to the position of our reflected surface, multiplied by two. |
|---|
| 283 | glEnable(GL_CLIP_PLANE0); |
|---|
| 284 | Vector pos = State::getCameraNode()->getAbsCoor(); |
|---|
| 285 | |
|---|
| 286 | if(pos.y > waterPos.y) |
|---|
| 287 | { |
|---|
| 288 | // Translate the world, then flip it upside down |
|---|
| 289 | glTranslatef(0.0f, waterPos.y*2.0f, 0.0f); |
|---|
| 290 | glScalef(1.0, -1.0, 1.0); |
|---|
| 291 | |
|---|
| 292 | // Since the world is updside down we need to change the culling to FRONT |
|---|
| 293 | glCullFace(GL_FRONT); |
|---|
| 294 | |
|---|
| 295 | // Set our plane equation and turn clipping on |
|---|
| 296 | double plane[4] = {0.0, 1.0, 0.0, -waterPos.y}; |
|---|
| 297 | glClipPlane(GL_CLIP_PLANE0, plane); |
|---|
| 298 | } |
|---|
| 299 | else |
|---|
| 300 | { |
|---|
| 301 | // If the camera is below the water we don't want to flip the world, |
|---|
| 302 | // but just render it clipped so only the top is drawn. |
|---|
| 303 | double plane[4] = {0.0, 1.0, 0.0, waterPos.y}; |
|---|
| 304 | glClipPlane(GL_CLIP_PLANE0, plane); |
|---|
| 305 | } |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | /** |
|---|
| 309 | * @brief ends the reflection and saves the graphic buffer into a texture |
|---|
| 310 | */ |
|---|
| 311 | void MappedWater::deactivateReflection() |
|---|
| 312 | { |
|---|
| 313 | glDisable(GL_CLIP_PLANE0); |
|---|
| 314 | glCullFace(GL_BACK); |
|---|
| 315 | |
|---|
| 316 | // Create the texture and store it on the video card |
|---|
| 317 | mat.renderToTexture(0, GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize); |
|---|
| 318 | |
|---|
| 319 | glPopMatrix(); |
|---|
| 320 | glPopAttrib(); |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | /** |
|---|
| 324 | * @brief prepares everything to render the refraction texutre |
|---|
| 325 | */ |
|---|
| 326 | void MappedWater::activateRefraction() |
|---|
| 327 | { |
|---|
| 328 | // To create the refraction and depth textures we do the same thing |
|---|
| 329 | // we did for the reflection texture, except we don't need to turn |
|---|
| 330 | // the world upside down. We want to find the depth of the water, |
|---|
| 331 | // not the depth of the sky and above water terrain. |
|---|
| 332 | |
|---|
| 333 | // save viewport matrix and change the viewport size |
|---|
| 334 | glPushAttrib(GL_VIEWPORT_BIT); |
|---|
| 335 | glViewport(0,0, textureSize, textureSize); |
|---|
| 336 | |
|---|
| 337 | glMatrixMode(GL_MODELVIEW); |
|---|
| 338 | glPushMatrix(); |
|---|
| 339 | |
|---|
| 340 | // If our camera is above the water we will render only the parts that |
|---|
| 341 | // are under the water. If the camera is below the water we render |
|---|
| 342 | // only the stuff above the water. Like the reflection texture, we |
|---|
| 343 | // incorporate clipping planes to only render what we need. |
|---|
| 344 | |
|---|
| 345 | // If the camera is above water, render the data below the water |
|---|
| 346 | glEnable(GL_CLIP_PLANE0); |
|---|
| 347 | Vector pos = State::getCameraNode()->getAbsCoor(); |
|---|
| 348 | if(pos.y > waterPos.y) |
|---|
| 349 | { |
|---|
| 350 | double plane[4] = {0.0, -1.0, 0.0, waterPos.y}; |
|---|
| 351 | glClipPlane(GL_CLIP_PLANE0, plane); |
|---|
| 352 | |
|---|
| 353 | } |
|---|
| 354 | // If the camera is below the water, only render the data above the water |
|---|
| 355 | else |
|---|
| 356 | { |
|---|
| 357 | glCullFace(GL_FRONT); |
|---|
| 358 | double plane[4] = {0.0, 1.0, 0.0, -waterPos.y}; |
|---|
| 359 | glClipPlane(GL_CLIP_PLANE0, plane); |
|---|
| 360 | } |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | /** |
|---|
| 364 | * @brief ends the refraction and saves the graphic buffer into a texture |
|---|
| 365 | */ |
|---|
| 366 | void MappedWater::deactivateRefraction() |
|---|
| 367 | { |
|---|
| 368 | glDisable(GL_CLIP_PLANE0); |
|---|
| 369 | glCullFace(GL_BACK); |
|---|
| 370 | |
|---|
| 371 | // Create the texture and store it on the video card |
|---|
| 372 | mat.renderToTexture(1, GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize); |
|---|
| 373 | |
|---|
| 374 | glPopMatrix(); |
|---|
| 375 | glPopAttrib(); |
|---|
| 376 | } |
|---|