[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" |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | CREATE_FACTORY(MappedWater, CL_MAPPED_WATER); |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | MappedWater::MappedWater(const TiXmlElement* root) |
---|
| 28 | { |
---|
| 29 | this->setClassID(CL_MAPPED_WATER, "MappedWater"); |
---|
| 30 | this->toList(OM_ENVIRON); |
---|
| 31 | |
---|
| 32 | if (root != NULL) |
---|
| 33 | this->loadParams(root); |
---|
| 34 | |
---|
| 35 | //! todo: rename texture to reflection texture |
---|
| 36 | // set up refleciton texture |
---|
[7814] | 37 | // FIXME mat.setDiffuseMap(this->texture, 0); doesnt work, |
---|
[7796] | 38 | mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 0); |
---|
| 39 | // load refraction texture |
---|
| 40 | //mat.setDiffuseMap(&this->refractionTexture, 1); |
---|
| 41 | // load normal map |
---|
| 42 | //mat.setDiffuseMap("pictures/normalmap.bmp", GL_TEXTURE_2D, 2); |
---|
| 43 | // load dudv map |
---|
| 44 | //mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 3); |
---|
| 45 | // set up depth texture |
---|
| 46 | //mat.setDiffuseMap(&this->depthTexture, 4); |
---|
| 47 | |
---|
| 48 | // set the size of the refraction and reflection textures |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | /// MAKE THE MAPPING TEXTURE. |
---|
| 53 | // THIS IS A HACK AND SHOULD BE IN TEXTURE SOMEWHERE. |
---|
| 54 | this->textureSize = 512; |
---|
| 55 | unsigned int channels = 32; |
---|
| 56 | GLenum type = GL_RGBA; |
---|
| 57 | unsigned int* pTexture = new unsigned int [this->textureSize * this->textureSize * channels]; |
---|
| 58 | memset(pTexture, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int)); |
---|
| 59 | // Register the texture with OpenGL and bind it to the texture ID |
---|
| 60 | mat.select(); |
---|
| 61 | glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0)); |
---|
| 62 | |
---|
| 63 | // Create the texture and store it on the video card |
---|
| 64 | glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTexture); |
---|
| 65 | |
---|
[7814] | 66 | //gluBuild2DMipmaps(GL_TEXTURE_2D, channels, this->textureSize, this->textureSize, type, GL_UNSIGNED_INT, pTexture); |
---|
| 67 | |
---|
[7796] | 68 | // Set the texture quality |
---|
| 69 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); |
---|
| 70 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); |
---|
| 71 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
---|
| 72 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
---|
| 73 | |
---|
| 74 | // Since we stored the texture space with OpenGL, we can delete the image data |
---|
| 75 | delete [] pTexture; |
---|
| 76 | |
---|
[7814] | 77 | |
---|
| 78 | //shader = new Shader( "/home/stefalie/svn/orxonox/data/trunk/shaders/water.vert", "/home/stefalie/svn/orxonox/data/trunk/shaders/mapped_water.frag"); |
---|
| 79 | shader = new Shader( ResourceManager::getInstance()->getDataDir() + "/shaders/mapped_water.vert", ResourceManager::getInstance()->getDataDir() +"/shaders/mapped_water.frag"); |
---|
[7796] | 80 | } |
---|
| 81 | |
---|
| 82 | MappedWater::~MappedWater() |
---|
| 83 | { |
---|
| 84 | //delete shader; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | void MappedWater::loadParams(const TiXmlElement* root) |
---|
| 88 | { |
---|
| 89 | WorldEntity::loadParams(root); |
---|
| 90 | |
---|
| 91 | LoadParam(root, "waterHeight", this, MappedWater, setHeight); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | void MappedWater::draw() const |
---|
| 96 | { |
---|
| 97 | glPushMatrix(); |
---|
| 98 | glTranslatef(0,this->waterHeight,0); |
---|
| 99 | |
---|
[7814] | 100 | //HACK |
---|
| 101 | glTranslatef(550,0,100); |
---|
[7796] | 102 | //glEnable(GL_LIGHTING); |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | //mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 1); |
---|
| 106 | //mat.setTransparency(1.0); |
---|
| 107 | //mat.setDiffuse(1.0, 0, .1); |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | // HACK |
---|
| 111 | |
---|
[7814] | 112 | glDisable(GL_BLEND); |
---|
[7796] | 113 | //glActiveTexture(GL_TEXTURE0); |
---|
| 114 | //glBindTexture(GL_TEXTURE_2D, this->texture); |
---|
| 115 | |
---|
| 116 | mat.select(); |
---|
| 117 | glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0)); |
---|
| 118 | |
---|
[7814] | 119 | |
---|
[7816] | 120 | // Shader init |
---|
[7796] | 121 | //this->shader->activateShader(); |
---|
[7816] | 122 | //GLint uniform; |
---|
[7796] | 123 | |
---|
| 124 | // Set the variable "reflection" to correspond to the first texture unit |
---|
[7816] | 125 | //uniform = glGetUniformLocationARB(shader->getProgram(), "reflection"); |
---|
| 126 | //glUniform1iARB(uniform, 0); //second paramter is the texture unit |
---|
[7796] | 127 | |
---|
| 128 | // Set the variable "refraction" to correspond to the second texture unit |
---|
| 129 | //uniform = glGetUniformLocationARB(shader->getProgram(), "refraction"); |
---|
| 130 | //glUniform1iARB(uniform, 0); |
---|
| 131 | // FIXME glUniform1iARB(uniform, 1); |
---|
| 132 | |
---|
| 133 | // Set the variable "normalMap" to correspond to the third texture unit |
---|
| 134 | //uniform = glGetUniformLocationARB(shader->getProgram(), "normalMap"); |
---|
| 135 | //glUniform1iARB(uniform, 2); |
---|
| 136 | |
---|
| 137 | // Set the variable "dudvMap" to correspond to the fourth texture unit |
---|
[7816] | 138 | //uniform = glGetUniformLocationARB(shader->getProgram(), "dudvMap"); |
---|
| 139 | //glUniform1iARB(uniform, 3); |
---|
[7796] | 140 | |
---|
| 141 | // Set the variable "depthMap" to correspond to the fifth texture unit |
---|
| 142 | //uniform = glGetUniformLocationARB(shader->getProgram(), "depthMap"); |
---|
| 143 | //glUniform1iARB(uniform, 4); |
---|
| 144 | // FIXME we dont have a depthMap yet :-( |
---|
| 145 | |
---|
| 146 | // Give the variable "waterColor" a blue color |
---|
[7816] | 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 |
---|
| 151 | Vector lightPos(100.0f, 150.0f, 100.0f); |
---|
| 152 | |
---|
| 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 |
---|
[7816] | 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 |
---|
[7816] | 162 | //uniform = glGetUniformLocationARB(shader->getProgram(), "cameraPos"); |
---|
| 163 | //glUniform4fARB(uniform, vPosition.x, vPosition.y, vPosition.z, 1.0f); |
---|
| 164 | |
---|
[7796] | 165 | glBegin(GL_QUADS); |
---|
[7814] | 166 | glNormal3f(0, 1, 0); |
---|
| 167 | glMultiTexCoord2f(GL_TEXTURE0, 0, 0); |
---|
| 168 | glMultiTexCoord2f(GL_TEXTURE1, 0, 0); |
---|
| 169 | glMultiTexCoord2f(GL_TEXTURE2, 0, 0); |
---|
| 170 | glMultiTexCoord2f(GL_TEXTURE3, 0, 0); |
---|
| 171 | glMultiTexCoord2f(GL_TEXTURE4, 0, 0); |
---|
[7816] | 172 | glVertex3f(0, 0, 0); |
---|
[7796] | 173 | |
---|
[7814] | 174 | glMultiTexCoord2f(GL_TEXTURE0, 1, 0); |
---|
| 175 | glMultiTexCoord2f(GL_TEXTURE1, 1, 0); |
---|
| 176 | glMultiTexCoord2f(GL_TEXTURE2, 1, 0); |
---|
| 177 | glMultiTexCoord2f(GL_TEXTURE3, 1, 0); |
---|
| 178 | glMultiTexCoord2f(GL_TEXTURE4, 1, 0); |
---|
[7816] | 179 | glVertex3f(0, 0, 1000); |
---|
[7796] | 180 | |
---|
[7814] | 181 | glMultiTexCoord2f(GL_TEXTURE0, 1, 1); |
---|
| 182 | glMultiTexCoord2f(GL_TEXTURE1, 1, 1); |
---|
| 183 | glMultiTexCoord2f(GL_TEXTURE2, 1, 1); |
---|
| 184 | glMultiTexCoord2f(GL_TEXTURE3, 1, 1); |
---|
| 185 | glMultiTexCoord2f(GL_TEXTURE4, 1, 1); |
---|
[7816] | 186 | glVertex3f(1000, 0, 1000); |
---|
[7796] | 187 | |
---|
[7814] | 188 | glMultiTexCoord2f(GL_TEXTURE0, 0, 1); |
---|
| 189 | glMultiTexCoord2f(GL_TEXTURE1, 0, 1); |
---|
| 190 | glMultiTexCoord2f(GL_TEXTURE2, 0, 1); |
---|
| 191 | glMultiTexCoord2f(GL_TEXTURE3, 0, 1); |
---|
| 192 | glMultiTexCoord2f(GL_TEXTURE4, 0, 1); |
---|
[7816] | 193 | glVertex3f(1000, 0, 0); |
---|
[7796] | 194 | glEnd(); |
---|
| 195 | |
---|
[7816] | 196 | this->shader->deactivateShader(); |
---|
| 197 | |
---|
[7796] | 198 | glPopMatrix(); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | void MappedWater::tick(float dt) |
---|
| 202 | { |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | void MappedWater::setHeight(float height) |
---|
| 206 | { |
---|
| 207 | this->waterHeight = height; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | void MappedWater::activateReflection() |
---|
| 211 | { |
---|
| 212 | glPushAttrib(GL_VIEWPORT_BIT); |
---|
[7814] | 213 | |
---|
[7796] | 214 | |
---|
| 215 | //glLoadIdentity(); |
---|
| 216 | glViewport(0,0, textureSize, textureSize); |
---|
| 217 | |
---|
[7814] | 218 | glPushMatrix(); |
---|
[7796] | 219 | // Clear the color and depth bits, reset the matrix and position our camera. |
---|
| 220 | |
---|
| 221 | //g_Camera.Look(); |
---|
| 222 | |
---|
| 223 | |
---|
| 224 | // If our camera is above the water we will render the scene flipped upside down. |
---|
| 225 | // In order to line up the reflection nicely with the world we have to translate |
---|
| 226 | // the world to the position of our reflected surface, multiplied by two. |
---|
| 227 | //if(g_Camera.Position().y > waterHeight) |
---|
| 228 | //{ |
---|
| 229 | // Translate the world, then flip it upside down |
---|
[7814] | 230 | glTranslatef(0.0f, this->waterHeight*2.0f, 0.0f); |
---|
| 231 | glScalef(1.0, -1.0, 1.0); |
---|
[7796] | 232 | |
---|
| 233 | // Since the world is updside down we need to change the culling to FRONT |
---|
[7814] | 234 | glCullFace(GL_FRONT); |
---|
[7796] | 235 | |
---|
| 236 | // Set our plane equation and turn clipping on |
---|
[7814] | 237 | double plane[4] = {0.0, 1.0, 0.0, -waterHeight}; |
---|
| 238 | //glEnable(GL_CLIP_PLANE0); |
---|
| 239 | //glClipPlane(GL_CLIP_PLANE0, plane); |
---|
[7796] | 240 | |
---|
| 241 | // Render the world upside down and clipped (only render the top flipped). |
---|
| 242 | // If we don't turn OFF caustics for the reflection texture we get horrible |
---|
| 243 | // artifacts in the water. That is why we set bRenderCaustics to FALSE. |
---|
| 244 | //RenderWorld(false); |
---|
| 245 | |
---|
| 246 | // Turn clipping off |
---|
| 247 | // glDisable(GL_CLIP_PLANE0); |
---|
| 248 | |
---|
| 249 | // Restore back-face culling |
---|
| 250 | // glCullFace(GL_BACK); |
---|
| 251 | //} |
---|
| 252 | /*else |
---|
| 253 | { |
---|
| 254 | // If the camera is below the water we don't want to flip the world, |
---|
| 255 | // but just render it clipped so only the top is drawn. |
---|
| 256 | double plane[4] = {0.0, 1.0, 0.0, waterHeight}; |
---|
| 257 | glEnable(GL_CLIP_PLANE0); |
---|
| 258 | glClipPlane(GL_CLIP_PLANE0, plane); |
---|
| 259 | RenderWorld(true); |
---|
| 260 | glDisable(GL_CLIP_PLANE0); |
---|
| 261 | }*/ |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | |
---|
| 265 | void MappedWater::deactivateReflection() |
---|
| 266 | { |
---|
| 267 | // glBindTexture(GL_TEXTURE_2D, texture.getTexture()); //is done by mat.select(); |
---|
[7814] | 268 | //glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0)); |
---|
[7796] | 269 | |
---|
| 270 | //mat.setDiffuseMap(&texture, 0); |
---|
[7814] | 271 | mat.select(); |
---|
[7796] | 272 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize); |
---|
[7814] | 273 | //glDisable(GL_CLIP_PLANE0); |
---|
[7796] | 274 | |
---|
[7814] | 275 | glPopMatrix(); |
---|
[7796] | 276 | |
---|
| 277 | glPopAttrib(); |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | void MappedWater::activateRefraction() |
---|
| 281 | { |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | void MappedWater::deactivateRefraction() |
---|
| 285 | { |
---|
| 286 | } |
---|