[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 | |
---|
[7986] | 36 | PRINTF(0)("MaxTextureUnits: %i\n", Material::getMaxTextureUnits()); |
---|
| 37 | |
---|
| 38 | // TODO rename texture to reflection texture |
---|
| 39 | /// loads the textures |
---|
[7796] | 40 | // set up refleciton texture |
---|
[7814] | 41 | // FIXME mat.setDiffuseMap(this->texture, 0); doesnt work, |
---|
[7796] | 42 | mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 0); |
---|
| 43 | // load refraction texture |
---|
[7986] | 44 | mat.setDiffuseMap("pictures/error_texture.png", GL_TEXTURE_2D, 1); |
---|
[7796] | 45 | // load normal map |
---|
[7823] | 46 | mat.setDiffuseMap("pictures/normalmap.bmp", GL_TEXTURE_2D, 2); |
---|
[7796] | 47 | // load dudv map |
---|
[7986] | 48 | mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 3); |
---|
[7796] | 49 | // set up depth texture |
---|
[7993] | 50 | mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 4); |
---|
[7796] | 51 | |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | /// MAKE THE MAPPING TEXTURE. |
---|
[7986] | 55 | // THIS IS A HACK AND SHOULD BE IN TEXTURE SOMEWHERE |
---|
| 56 | // set the size of the refraction and reflection textures |
---|
[7796] | 57 | this->textureSize = 512; |
---|
| 58 | unsigned int channels = 32; |
---|
| 59 | GLenum type = GL_RGBA; |
---|
[7823] | 60 | unsigned int* pTextureReflection = new unsigned int [this->textureSize * this->textureSize * channels]; |
---|
| 61 | memset(pTextureReflection, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int)); |
---|
[7986] | 62 | unsigned int* pTextureRefraction = new unsigned int [this->textureSize * this->textureSize * channels]; |
---|
| 63 | memset(pTextureRefraction, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int)); |
---|
[7796] | 64 | // Register the texture with OpenGL and bind it to the texture ID |
---|
| 65 | mat.select(); |
---|
| 66 | glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0)); |
---|
| 67 | |
---|
| 68 | // Create the texture and store it on the video card |
---|
[7823] | 69 | glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureReflection); |
---|
[7796] | 70 | |
---|
[7814] | 71 | //gluBuild2DMipmaps(GL_TEXTURE_2D, channels, this->textureSize, this->textureSize, type, GL_UNSIGNED_INT, pTexture); |
---|
| 72 | |
---|
[7823] | 73 | //the same for the refraction |
---|
[7986] | 74 | glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(1)); |
---|
| 75 | glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureRefraction); |
---|
[7823] | 76 | |
---|
[7796] | 77 | // Set the texture quality |
---|
| 78 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); |
---|
| 79 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); |
---|
| 80 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
---|
| 81 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
---|
| 82 | |
---|
| 83 | // Since we stored the texture space with OpenGL, we can delete the image data |
---|
[7823] | 84 | delete [] pTextureReflection; |
---|
[7986] | 85 | delete [] pTextureRefraction; |
---|
[7796] | 86 | |
---|
[7814] | 87 | |
---|
[7986] | 88 | /// initialization of the texture coords, speeds etc... |
---|
[7823] | 89 | this->move = 0.0f; |
---|
| 90 | this->g_WaterUV = 35.0f; |
---|
| 91 | this->kNormalMapScale = 0.25f; |
---|
| 92 | this->g_WaterFlow = 0.0015f; |
---|
[7983] | 93 | |
---|
[7986] | 94 | /// initialization of the shaders |
---|
| 95 | // load shader files |
---|
[8025] | 96 | /* shader = new Shader( ResourceManager::getInstance()->getDataDir() + "/shaders/mapped_water.vert", ResourceManager::getInstance()->getDataDir() +"/shaders/mapped_water.frag"); |
---|
[7986] | 97 | |
---|
[7983] | 98 | this->shader->activateShader(); |
---|
| 99 | // Set the variable "reflection" to correspond to the first texture unit |
---|
| 100 | Shader::Uniform(shader, "reflection").set(0); |
---|
| 101 | // Set the variable "refraction" to correspond to the second texture unit |
---|
[7986] | 102 | Shader::Uniform(shader, "refraction").set(1); |
---|
[7983] | 103 | // Set the variable "normalMap" to correspond to the third texture unit |
---|
| 104 | Shader::Uniform(shader, "normalMap").set(2); |
---|
| 105 | // Set the variable "dudvMap" to correspond to the fourth texture unit |
---|
| 106 | Shader::Uniform(shader, "dudvMap").set(3); |
---|
[7986] | 107 | // Set the variable "depthMap" to correspond to the fifth texture unit |
---|
[7993] | 108 | Shader::Uniform(shader, "depthMap").set(4); |
---|
[7986] | 109 | // Give the variable "waterColor" a blue color |
---|
| 110 | Shader::Uniform(shader, "waterColor").set(0.1f, 0.2f, 0.4f, 1.0f); |
---|
| 111 | // Give the variable "lightPos" our hard coded light position |
---|
| 112 | Shader::Uniform(shader, "lightPos").set(lightPos.x, lightPos.y, lightPos.z, 1.0f); |
---|
| 113 | // uniform for the camera position |
---|
| 114 | cam_uni = new Shader::Uniform(shader, "cameraPos"); |
---|
[7983] | 115 | |
---|
[8025] | 116 | this->shader->deactivateShader();*/ |
---|
[7796] | 117 | } |
---|
| 118 | |
---|
| 119 | MappedWater::~MappedWater() |
---|
| 120 | { |
---|
[8025] | 121 | //delete shader; |
---|
| 122 | //delete cam_uni; |
---|
[7796] | 123 | } |
---|
| 124 | |
---|
| 125 | void MappedWater::loadParams(const TiXmlElement* root) |
---|
| 126 | { |
---|
| 127 | WorldEntity::loadParams(root); |
---|
| 128 | |
---|
| 129 | LoadParam(root, "waterHeight", this, MappedWater, setHeight); |
---|
[7986] | 130 | LoadParam(root, "lightPos", this, MappedWater, setLightPosition); |
---|
[7796] | 131 | } |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | void MappedWater::draw() const |
---|
| 135 | { |
---|
| 136 | glPushMatrix(); |
---|
| 137 | glTranslatef(0,this->waterHeight,0); |
---|
| 138 | |
---|
[7982] | 139 | mat.unselect(); |
---|
[7796] | 140 | mat.select(); |
---|
| 141 | |
---|
[8025] | 142 | ///this->shader->activateShader(); |
---|
[7796] | 143 | |
---|
[7986] | 144 | // reset the camera uniform to the current cam position |
---|
| 145 | Vector pos = State::getCameraNode()->getAbsCoor(); |
---|
[8025] | 146 | ///cam_uni->set(pos.x, pos.y, pos.z, 1.0f); |
---|
[7796] | 147 | |
---|
| 148 | glBegin(GL_QUADS); |
---|
[7823] | 149 | // The back left vertice for the water |
---|
[7983] | 150 | glMultiTexCoord2f(GL_TEXTURE0, 0.0f, g_WaterUV); // Reflection texture |
---|
| 151 | glMultiTexCoord2f(GL_TEXTURE1, 0.0f, refrUV - move); // Refraction texture |
---|
| 152 | glMultiTexCoord2f(GL_TEXTURE2, 0.0f, normalUV + move2); // Normal map texture |
---|
| 153 | glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture |
---|
| 154 | glMultiTexCoord2f(GL_TEXTURE4, 0, 0); // Depth texture |
---|
[7823] | 155 | glVertex3f(0.0f, waterHeight, 0.0f); |
---|
[7796] | 156 | |
---|
[7823] | 157 | // The front left vertice for the water |
---|
[7983] | 158 | glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 0.0f); // Reflection texture |
---|
| 159 | glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 0.0f - move); // Refraction texture |
---|
| 160 | glMultiTexCoord2f(GL_TEXTURE2, 0.0f, 0.0f + move2); // Normal map texture |
---|
| 161 | glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture |
---|
| 162 | glMultiTexCoord2f(GL_TEXTURE4, 0, 0); // Depth texture |
---|
[7823] | 163 | glVertex3f(0.0f, waterHeight, 1000.0f); |
---|
[7796] | 164 | |
---|
[7823] | 165 | // The front right vertice for the water |
---|
[7983] | 166 | glMultiTexCoord2f(GL_TEXTURE0, g_WaterUV, 0.0f); // Reflection texture |
---|
| 167 | glMultiTexCoord2f(GL_TEXTURE1, refrUV, 0.0f - move); // Refraction texture |
---|
| 168 | glMultiTexCoord2f(GL_TEXTURE2, normalUV, 0.0f + move2); // Normal map texture |
---|
| 169 | glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture |
---|
| 170 | glMultiTexCoord2f(GL_TEXTURE4, 0, 0); // Depth texture |
---|
[7823] | 171 | glVertex3f(1000.0f, waterHeight, 1000.0f); |
---|
[7796] | 172 | |
---|
[7823] | 173 | // The back right vertice for the water |
---|
[7983] | 174 | glMultiTexCoord2f(GL_TEXTURE0, g_WaterUV, g_WaterUV); // Reflection texture |
---|
| 175 | glMultiTexCoord2f(GL_TEXTURE1, refrUV, refrUV - move); // Refraction texture |
---|
| 176 | glMultiTexCoord2f(GL_TEXTURE2, normalUV, normalUV + move2); // Normal map texture |
---|
| 177 | glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture |
---|
| 178 | glMultiTexCoord2f(GL_TEXTURE4, 0, 0); // Depth texture |
---|
[7823] | 179 | glVertex3f(1000.0f, waterHeight, 0.0f); |
---|
[7796] | 180 | glEnd(); |
---|
| 181 | |
---|
[8025] | 182 | ///this->shader->deactivateShader(); |
---|
[7816] | 183 | |
---|
[7983] | 184 | mat.unselect(); |
---|
[7823] | 185 | |
---|
[7796] | 186 | glPopMatrix(); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | void MappedWater::tick(float dt) |
---|
| 190 | { |
---|
[7983] | 191 | // makes the water flow |
---|
| 192 | this->move += this->g_WaterFlow; |
---|
[7982] | 193 | this->move2 = this->move * this->kNormalMapScale; |
---|
| 194 | this->refrUV = this->g_WaterUV; |
---|
| 195 | this->normalUV = this->g_WaterUV * this->kNormalMapScale; |
---|
[7796] | 196 | } |
---|
| 197 | |
---|
| 198 | void MappedWater::setHeight(float height) |
---|
| 199 | { |
---|
| 200 | this->waterHeight = height; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | void MappedWater::activateReflection() |
---|
| 204 | { |
---|
[8018] | 205 | // save viewport matrix and change the viewport size |
---|
[7796] | 206 | glPushAttrib(GL_VIEWPORT_BIT); |
---|
| 207 | glViewport(0,0, textureSize, textureSize); |
---|
| 208 | |
---|
[7814] | 209 | glPushMatrix(); |
---|
[7796] | 210 | |
---|
| 211 | // If our camera is above the water we will render the scene flipped upside down. |
---|
| 212 | // In order to line up the reflection nicely with the world we have to translate |
---|
| 213 | // the world to the position of our reflected surface, multiplied by two. |
---|
[8025] | 214 | glEnable(GL_CLIP_PLANE0); |
---|
[8018] | 215 | Vector pos = State::getCameraNode()->getAbsCoor(); |
---|
| 216 | if(pos.y > waterHeight) |
---|
| 217 | { |
---|
| 218 | // Translate the world, then flip it upside down |
---|
| 219 | glTranslatef(0.0f, waterHeight*2.0f, 0.0f); |
---|
| 220 | glScalef(1.0, -1.0, 1.0); |
---|
[7796] | 221 | |
---|
[8018] | 222 | // Since the world is updside down we need to change the culling to FRONT |
---|
| 223 | glCullFace(GL_FRONT); |
---|
[7796] | 224 | |
---|
[8018] | 225 | // Set our plane equation and turn clipping on |
---|
| 226 | double plane[4] = {0.0, 1.0, 0.0, -waterHeight}; |
---|
| 227 | glClipPlane(GL_CLIP_PLANE0, plane); |
---|
| 228 | } |
---|
| 229 | else |
---|
[7796] | 230 | { |
---|
[8018] | 231 | // If the camera is below the water we don't want to flip the world, |
---|
| 232 | // but just render it clipped so only the top is drawn. |
---|
| 233 | double plane[4] = {0.0, 1.0, 0.0, waterHeight}; |
---|
| 234 | glClipPlane(GL_CLIP_PLANE0, plane); |
---|
| 235 | } |
---|
[7796] | 236 | } |
---|
| 237 | |
---|
| 238 | |
---|
| 239 | void MappedWater::deactivateReflection() |
---|
| 240 | { |
---|
[8018] | 241 | glDisable(GL_CLIP_PLANE0); |
---|
| 242 | glCullFace(GL_BACK); |
---|
[7796] | 243 | |
---|
[7814] | 244 | mat.select(); |
---|
[7796] | 245 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize); |
---|
| 246 | |
---|
[7814] | 247 | glPopMatrix(); |
---|
[7796] | 248 | glPopAttrib(); |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | void MappedWater::activateRefraction() |
---|
[8025] | 252 | {/* |
---|
| 253 | // To create the refraction and depth textures we do the same thing |
---|
| 254 | // we did for the reflection texture, except we don't need to turn |
---|
| 255 | // the world upside down. We want to find the depth of the water, |
---|
| 256 | // not the depth of the sky and above water terrain. |
---|
| 257 | |
---|
| 258 | // save viewport matrix and change the viewport size |
---|
| 259 | glPushAttrib(GL_VIEWPORT_BIT); |
---|
| 260 | glViewport(0,0, textureSize, textureSize); |
---|
| 261 | |
---|
| 262 | glPushMatrix(); |
---|
| 263 | |
---|
| 264 | // If our camera is above the water we will render only the parts that |
---|
| 265 | // are under the water. If the camera is below the water we render |
---|
| 266 | // only the stuff above the water. Like the reflection texture, we |
---|
| 267 | // incorporate clipping planes to only render what we need. |
---|
| 268 | |
---|
| 269 | // If the camera is above water, render the data below the water |
---|
| 270 | glEnable(GL_CLIP_PLANE0); |
---|
| 271 | Vector pos = State::getCameraNode()->getAbsCoor(); |
---|
| 272 | if(pos.y > waterHeight) |
---|
| 273 | { |
---|
| 274 | double plane[4] = {0.0, -1.0, 0.0, waterHeight}; |
---|
| 275 | glClipPlane(GL_CLIP_PLANE0, plane); |
---|
| 276 | } |
---|
| 277 | // If the camera is below the water, only render the data above the water |
---|
| 278 | else |
---|
| 279 | { |
---|
| 280 | glCullFace(GL_FRONT); |
---|
| 281 | double plane[4] = {0.0, 1.0, 0.0, -waterHeight}; |
---|
| 282 | glClipPlane(GL_CLIP_PLANE0, plane); |
---|
| 283 | }*/ |
---|
[7796] | 284 | } |
---|
| 285 | |
---|
| 286 | void MappedWater::deactivateRefraction() |
---|
[8025] | 287 | {/* |
---|
| 288 | glDisable(GL_CLIP_PLANE0); |
---|
| 289 | glCullFace(GL_BACK); |
---|
| 290 | |
---|
| 291 | mat.select(); |
---|
| 292 | // Bind the current scene to our refraction texture |
---|
| 293 | // glBindTexture(GL_TEXTURE_2D, g_Texture[REFRACTION_ID]); |
---|
| 294 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize); |
---|
| 295 | |
---|
| 296 | // Bind the current scene to our depth texture |
---|
| 297 | // glBindTexture(GL_TEXTURE_2D, g_Texture[DEPTH_ID]); |
---|
| 298 | glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, textureSize, textureSize, 0); |
---|
| 299 | |
---|
| 300 | glPopMatrix(); |
---|
| 301 | glPopAttrib(); |
---|
| 302 | */ |
---|
[7796] | 303 | } |
---|