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