[7672] | 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" |
---|
[7675] | 17 | #include "util/loading/load_param.h" |
---|
[7687] | 18 | #include "util/loading/factory.h" |
---|
[7672] | 19 | |
---|
[7687] | 20 | |
---|
[7780] | 21 | |
---|
| 22 | |
---|
[7687] | 23 | CREATE_FACTORY(MappedWater, CL_MAPPED_WATER); |
---|
| 24 | |
---|
| 25 | |
---|
[7672] | 26 | MappedWater::MappedWater(const TiXmlElement* root) |
---|
[7700] | 27 | : texture(GL_TEXTURE_2D) |
---|
[7672] | 28 | { |
---|
[7687] | 29 | this->setClassID(CL_MAPPED_WATER, "MappedWater"); |
---|
| 30 | this->toList(OM_ENVIRON); |
---|
[7740] | 31 | |
---|
[7672] | 32 | if (root != NULL) |
---|
| 33 | this->loadParams(root); |
---|
[7740] | 34 | |
---|
[7780] | 35 | //! HACK FIXME HACK FIXME HACK FIXME HACK FIXME |
---|
| 36 | //! todo: rename texture to reflection texture |
---|
| 37 | // set up refleciton texture |
---|
| 38 | //mat.setDiffuseMap(&this->texture, 0); |
---|
| 39 | mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 0); |
---|
| 40 | mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 1); |
---|
| 41 | mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 2); |
---|
| 42 | mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 3); |
---|
| 43 | mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 4); |
---|
| 44 | // load refraction texture |
---|
| 45 | //mat.setDiffuseMap(&this->refractionTexture, 1); |
---|
| 46 | // load normal map |
---|
| 47 | //mat.setDiffuseMap("pictures/normalmap.bmp", GL_TEXTURE_2D, 2); |
---|
| 48 | // load dudv map |
---|
| 49 | //mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 3); |
---|
| 50 | // set up depth texture |
---|
| 51 | //mat.setDiffuseMap(&this->depthTexture, 4); |
---|
| 52 | |
---|
| 53 | // set the size of the refraction and reflection textures |
---|
| 54 | this->textureSize = 512; |
---|
[7740] | 55 | |
---|
[7780] | 56 | shader = new Shader("/home/lieni/orxonox/data/trunk/shaders/water.vert", "/home/lieni/orxonox/data/trunk/shaders/mapped_water.frag"); |
---|
[7672] | 57 | } |
---|
| 58 | |
---|
| 59 | MappedWater::~MappedWater() |
---|
| 60 | { |
---|
[7780] | 61 | delete shader; |
---|
[7672] | 62 | } |
---|
| 63 | |
---|
| 64 | void MappedWater::loadParams(const TiXmlElement* root) |
---|
| 65 | { |
---|
[7675] | 66 | WorldEntity::loadParams(root); |
---|
[7672] | 67 | |
---|
[7687] | 68 | LoadParam(root, "waterHeight", this, MappedWater, setHeight); |
---|
[7672] | 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | void MappedWater::draw() const |
---|
| 73 | { |
---|
[7740] | 74 | glPushMatrix(); |
---|
[7687] | 75 | glTranslatef(0,this->waterHeight,0); |
---|
[7672] | 76 | |
---|
[7687] | 77 | //glEnable(GL_LIGHTING); |
---|
| 78 | |
---|
[7740] | 79 | |
---|
[7687] | 80 | //mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 1); |
---|
| 81 | //mat.setTransparency(1.0); |
---|
| 82 | //mat.setDiffuse(1.0, 0, .1); |
---|
[7740] | 83 | |
---|
| 84 | |
---|
[7700] | 85 | // HACK |
---|
| 86 | |
---|
| 87 | //glDisable(GL_BLEND); |
---|
| 88 | //glActiveTexture(GL_TEXTURE0); |
---|
| 89 | //glBindTexture(GL_TEXTURE_2D, this->texture); |
---|
| 90 | |
---|
[7687] | 91 | mat.select(); |
---|
[7780] | 92 | // Shader init |
---|
| 93 | this->shader->activateShader(); |
---|
| 94 | GLint uniform; |
---|
| 95 | |
---|
| 96 | // Set the variable "reflection" to correspond to the first texture unit |
---|
| 97 | uniform = glGetUniformLocationARB(shader->getProgram(), "reflection"); |
---|
| 98 | glUniform1iARB(uniform, 0); //second paramter is the texture unit |
---|
[7687] | 99 | |
---|
[7780] | 100 | // Set the variable "refraction" to correspond to the second texture unit |
---|
| 101 | //uniform = glGetUniformLocationARB(shader->getProgram(), "refraction"); |
---|
| 102 | //glUniform1iARB(uniform, 0); |
---|
| 103 | // FIXME glUniform1iARB(uniform, 1); |
---|
| 104 | |
---|
| 105 | // Set the variable "normalMap" to correspond to the third texture unit |
---|
| 106 | //uniform = glGetUniformLocationARB(shader->getProgram(), "normalMap"); |
---|
| 107 | //glUniform1iARB(uniform, 2); |
---|
| 108 | |
---|
| 109 | // Set the variable "dudvMap" to correspond to the fourth texture unit |
---|
| 110 | uniform = glGetUniformLocationARB(shader->getProgram(), "dudvMap"); |
---|
| 111 | glUniform1iARB(uniform, 3); |
---|
| 112 | |
---|
| 113 | // Set the variable "depthMap" to correspond to the fifth texture unit |
---|
| 114 | //uniform = glGetUniformLocationARB(shader->getProgram(), "depthMap"); |
---|
| 115 | //glUniform1iARB(uniform, 4); |
---|
| 116 | // FIXME we dont have a depthMap yet :-( |
---|
| 117 | |
---|
| 118 | // Give the variable "waterColor" a blue color |
---|
| 119 | uniform = glGetUniformLocationARB(shader->getProgram(), "waterColor"); |
---|
| 120 | glUniform4fARB(uniform, 0.1f, 0.2f, 0.4f, 1.0f); |
---|
| 121 | |
---|
| 122 | // FIXME set camera and light information |
---|
| 123 | Vector lightPos(100.0f, 150.0f, 100.0f); |
---|
| 124 | |
---|
| 125 | // Store the camera position in a variable |
---|
| 126 | //CVector3 vPosition = g_Camera.Position(); |
---|
| 127 | Vector vPosition(50.0f, 50.0f, 50.0f); |
---|
| 128 | |
---|
| 129 | // Give the variable "lightPos" our hard coded light position |
---|
| 130 | uniform = glGetUniformLocationARB(shader->getProgram(), "lightPos"); |
---|
| 131 | glUniform4fARB(uniform, lightPos.x, lightPos.y, lightPos.z, 1.0f); |
---|
| 132 | |
---|
| 133 | // Give the variable "cameraPos" our camera position |
---|
| 134 | uniform = glGetUniformLocationARB(shader->getProgram(), "cameraPos"); |
---|
| 135 | glUniform4fARB(uniform, vPosition.x, vPosition.y, vPosition.z, 1.0f); |
---|
| 136 | |
---|
[7687] | 137 | glBegin(GL_QUADS); |
---|
| 138 | glNormal3f(0,1,0); |
---|
[7780] | 139 | glMultiTexCoord2f(GL_TEXTURE0, 0,0); |
---|
| 140 | glMultiTexCoord2f(GL_TEXTURE1, 0,0); |
---|
| 141 | glMultiTexCoord2f(GL_TEXTURE2, 0,0); |
---|
| 142 | glMultiTexCoord2f(GL_TEXTURE3, 0,0); |
---|
| 143 | glMultiTexCoord2f(GL_TEXTURE4, 0,0); |
---|
[7687] | 144 | glVertex3f(0,0,0); |
---|
[7780] | 145 | |
---|
| 146 | glMultiTexCoord2f(GL_TEXTURE0, 1,0); |
---|
| 147 | glMultiTexCoord2f(GL_TEXTURE1, 1,0); |
---|
| 148 | glMultiTexCoord2f(GL_TEXTURE2, 1,0); |
---|
| 149 | glMultiTexCoord2f(GL_TEXTURE3, 1,0); |
---|
| 150 | glMultiTexCoord2f(GL_TEXTURE4, 1,0); |
---|
[7687] | 151 | glVertex3f(100,0,0); |
---|
[7780] | 152 | |
---|
| 153 | glMultiTexCoord2f(GL_TEXTURE0, 1,1); |
---|
| 154 | glMultiTexCoord2f(GL_TEXTURE1, 1,1); |
---|
| 155 | glMultiTexCoord2f(GL_TEXTURE2, 1,1); |
---|
| 156 | glMultiTexCoord2f(GL_TEXTURE3, 1,1); |
---|
| 157 | glMultiTexCoord2f(GL_TEXTURE4, 1,1); |
---|
[7687] | 158 | glVertex3f(100,0,-100); |
---|
[7780] | 159 | |
---|
| 160 | glMultiTexCoord2f(GL_TEXTURE0, 0,1); |
---|
| 161 | glMultiTexCoord2f(GL_TEXTURE1, 0,1); |
---|
| 162 | glMultiTexCoord2f(GL_TEXTURE2, 0,1); |
---|
| 163 | glMultiTexCoord2f(GL_TEXTURE3, 0,1); |
---|
| 164 | glMultiTexCoord2f(GL_TEXTURE4, 0,1); |
---|
[7687] | 165 | glVertex3f(0,0,-100); |
---|
| 166 | glEnd(); |
---|
[7780] | 167 | this->shader->deactivateShader(); |
---|
[7687] | 168 | |
---|
| 169 | glPopMatrix(); |
---|
[7672] | 170 | } |
---|
| 171 | |
---|
| 172 | void MappedWater::tick(float dt) |
---|
| 173 | { |
---|
| 174 | |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | void MappedWater::setHeight(float height) |
---|
| 178 | { |
---|
| 179 | this->waterHeight = height; |
---|
| 180 | } |
---|
[7687] | 181 | |
---|
[7700] | 182 | void MappedWater::activateReflection() |
---|
| 183 | { |
---|
| 184 | glPushAttrib(GL_VIEWPORT_BIT); |
---|
[7741] | 185 | // glPushMatrix(); |
---|
[7740] | 186 | |
---|
[7741] | 187 | //glLoadIdentity(); |
---|
[7700] | 188 | glViewport(0,0, textureSize, textureSize); |
---|
[7687] | 189 | |
---|
[7741] | 190 | |
---|
[7700] | 191 | // Clear the color and depth bits, reset the matrix and position our camera. |
---|
[7741] | 192 | |
---|
[7700] | 193 | //g_Camera.Look(); |
---|
| 194 | |
---|
| 195 | |
---|
| 196 | // If our camera is above the water we will render the scene flipped upside down. |
---|
| 197 | // In order to line up the reflection nicely with the world we have to translate |
---|
| 198 | // the world to the position of our reflected surface, multiplied by two. |
---|
| 199 | //if(g_Camera.Position().y > waterHeight) |
---|
| 200 | //{ |
---|
| 201 | // Translate the world, then flip it upside down |
---|
| 202 | // glTranslatef(0.0f, waterHeight*2.0f, 0.0f); |
---|
| 203 | // glScalef(1.0, -1.0, 1.0); |
---|
| 204 | |
---|
| 205 | // Since the world is updside down we need to change the culling to FRONT |
---|
| 206 | //glCullFace(GL_FRONT); |
---|
| 207 | |
---|
| 208 | // Set our plane equation and turn clipping on |
---|
| 209 | // double plane[4] = {0.0, 1.0, 0.0, -waterHeight}; |
---|
| 210 | // glEnable(GL_CLIP_PLANE0); |
---|
| 211 | // glClipPlane(GL_CLIP_PLANE0, plane); |
---|
| 212 | |
---|
| 213 | // Render the world upside down and clipped (only render the top flipped). |
---|
| 214 | // If we don't turn OFF caustics for the reflection texture we get horrible |
---|
| 215 | // artifacts in the water. That is why we set bRenderCaustics to FALSE. |
---|
| 216 | //RenderWorld(false); |
---|
| 217 | |
---|
| 218 | // Turn clipping off |
---|
| 219 | // glDisable(GL_CLIP_PLANE0); |
---|
| 220 | |
---|
| 221 | // Restore back-face culling |
---|
| 222 | // glCullFace(GL_BACK); |
---|
| 223 | //} |
---|
| 224 | /*else |
---|
| 225 | { |
---|
| 226 | // If the camera is below the water we don't want to flip the world, |
---|
| 227 | // but just render it clipped so only the top is drawn. |
---|
| 228 | double plane[4] = {0.0, 1.0, 0.0, waterHeight}; |
---|
| 229 | glEnable(GL_CLIP_PLANE0); |
---|
| 230 | glClipPlane(GL_CLIP_PLANE0, plane); |
---|
| 231 | RenderWorld(true); |
---|
| 232 | glDisable(GL_CLIP_PLANE0); |
---|
| 233 | }*/ |
---|
[7741] | 234 | } |
---|
[7700] | 235 | |
---|
| 236 | |
---|
| 237 | void MappedWater::deactivateReflection() |
---|
| 238 | { |
---|
[7780] | 239 | //glBindTexture(GL_TEXTURE_2D, texture.getTexture()); is done by mat.select(); |
---|
[7700] | 240 | |
---|
[7741] | 241 | //mat.setDiffuseMap(&texture, 0); |
---|
[7780] | 242 | |
---|
| 243 | //mat.select(); |
---|
| 244 | //glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize); |
---|
[7740] | 245 | |
---|
[7741] | 246 | |
---|
[7740] | 247 | glPopAttrib(); |
---|
[7741] | 248 | // glPopMatrix(); |
---|
[7700] | 249 | } |
---|
| 250 | |
---|
| 251 | void MappedWater::activateRefraction() |
---|
| 252 | { |
---|
| 253 | |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | void MappedWater::deactivateRefraction() |
---|
| 257 | { |
---|
| 258 | |
---|
| 259 | } |
---|