/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Stefan Lienard co-programmer: ... */ #include "mapped_water.h" #include "util/loading/load_param.h" #include "util/loading/factory.h" #include "util/loading/resource_manager.h" #include "state.h" CREATE_FACTORY(MappedWater, CL_MAPPED_WATER); /** * @brief constructor * @param root xml data */ MappedWater::MappedWater(const TiXmlElement* root) { this->setClassID(CL_MAPPED_WATER, "MappedWater"); this->toList(OM_ENVIRON); if (root != NULL) this->loadParams(root); this->textureSize = 512; unsigned int channels = 32; GLenum type = GL_RGBA; /// loads the textures // set up refleciton texture //Texture reflTex(GL_TEXTURE_2D, this->textureSize, this->textureSize, channels, type); //mat.setDiffuseMap(reflTex, 0); mat.setDiffuseMap("pictures/refl.bmp", GL_TEXTURE_2D, 0); // load refraction texture //Texture refrTex(GL_TEXTURE_2D, this->textureSize, this->textureSize, channels, type); //mat.setDiffuseMap(refrTex, 1); mat.setDiffuseMap("pictures/refr.bmp", GL_TEXTURE_2D, 1); // load normal map mat.setDiffuseMap("pictures/normalmap.bmp", GL_TEXTURE_2D, 2); // load dudv map mat.setDiffuseMap("pictures/dudvmap.bmp", GL_TEXTURE_2D, 3); // set up depth texture //mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 4); // set the size of the refraction and reflection textures // initialization of the texture coords, speeds etc... this->move = 0.0f; this->kNormalMapScale = 0.25f; //unsigned int channels = 32; //GLenum type = GL_RGBA; //unsigned int* pTextureReflection = new unsigned int [this->textureSize * this->textureSize * channels]; //memset(pTextureReflection, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int)); //unsigned int* pTextureRefraction = new unsigned int [this->textureSize * this->textureSize * channels]; //memset(pTextureRefraction, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int)); // Register the texture with OpenGL and bind it to the texture ID //mat.select(); //glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0)); // Create the texture and store it on the video card //glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureReflection); //gluBuild2DMipmaps(GL_TEXTURE_2D, channels, this->textureSize, this->textureSize, type, GL_UNSIGNED_INT, pTexture); //the same for the refraction //glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(1)); //glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureRefraction); /* unsigned int channels = 32; GLenum type = GL_RGBA; unsigned int* pTextureReflection = new unsigned int [this->textureSize * this->textureSize * channels]; memset(pTextureReflection, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int)); //mat.select();*/ glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(0)); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); //glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureReflection); //unsigned int* pTextureRefraction = new unsigned int [this->textureSize * this->textureSize * channels]; //memset(pTextureRefraction, 0, this->textureSize * this->textureSize * channels * sizeof(unsigned int)); glBindTexture(GL_TEXTURE_2D, this->mat.getDiffuseTexture(1)); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); //glTexImage2D(GL_TEXTURE_2D, 0, channels, this->textureSize, this->textureSize, 0, type, GL_UNSIGNED_INT, pTextureRefraction); // Set the texture quality // Since we stored the texture space with OpenGL, we can delete the image data //delete [] pTextureReflection; //delete [] pTextureRefraction; /// initialization of the shaders this->initShaders(); /// fog, doesnt work the way i want it to work /*this->fog = new FogEffect(); fog->setFogColor(0.1f, 0.2f, 0.4f); fog->setFogRange(0.0f, 500.0f); fog->setFogDensity(0.03f);*/ } /** * @brief deltes shader and the uniform used by the camera */ MappedWater::~MappedWater() { delete shader; delete cam_uni; //delete fog; } /** * @brief initialization of the shaders */ void MappedWater::initShaders() { // load shader files shader = new Shader( ResourceManager::getInstance()->getDataDir() + "/shaders/mapped_water.vert", ResourceManager::getInstance()->getDataDir() +"/shaders/mapped_water.frag"); this->shader->activateShader(); // Set the variable "reflection" to correspond to the first texture unit Shader::Uniform(shader, "reflection").set(0); // Set the variable "refraction" to correspond to the second texture unit Shader::Uniform(shader, "refraction").set(1); // Set the variable "normalMap" to correspond to the third texture unit Shader::Uniform(shader, "normalMap").set(2); // Set the variable "dudvMap" to correspond to the fourth texture unit Shader::Uniform(shader, "dudvMap").set(3); // Set the variable "depthMap" to correspond to the fifth texture unit Shader::Uniform(shader, "depthMap").set(2); // Give the variable "waterColor" a blue color Shader::Uniform(shader, "waterColor").set(0.1f, 0.2f, 0.4f, 1.0f); // Give the variable "lightPos" our hard coded light position Shader::Uniform(shader, "lightPos").set(lightPos.x, lightPos.y, lightPos.z, 1.0f); // uniform for the camera position cam_uni = new Shader::Uniform(shader, "cameraPos"); this->shader->deactivateShader(); } /** * @brief ends the refraction and saves the graphic buffer into a texture * @param root xml data */ void MappedWater::loadParams(const TiXmlElement* root) { WorldEntity::loadParams(root); LoadParam(root, "waterpos", this, MappedWater, setWaterPos); LoadParam(root, "watersize", this, MappedWater, setWaterSize); LoadParam(root, "lightpos", this, MappedWater, setLightPosition); LoadParam(root, "wateruv", this, MappedWater, setWaterUV); LoadParam(root, "waterflow", this, MappedWater, setWaterFlow); } /** * @brief activates the water shader and draws a quad with four textures on it */ void MappedWater::draw() const { glMatrixMode(GL_MODELVIEW); glPushMatrix(); glTranslatef(this->waterPos.x ,0 ,this->waterPos.z); mat.select(); this->shader->activateShader(); // reset the camera uniform to the current cam position Vector pos = State::getCameraNode()->getAbsCoor(); cam_uni->set(pos.x, pos.y, pos.z, 1.0f); glBegin(GL_QUADS); // The back left vertice for the water glMultiTexCoord2f(GL_TEXTURE0, 0.0f, g_WaterUV); // Reflection texture glMultiTexCoord2f(GL_TEXTURE1, 0.0f, refrUV - move); // Refraction texture glMultiTexCoord2f(GL_TEXTURE2, 0.0f, normalUV + move2); // Normal map texture glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture glVertex3f(0.0f, this->waterPos.y, 0.0f); // The front left vertice for the water glMultiTexCoord2f(GL_TEXTURE0, 0.0f, 0.0f); // Reflection texture glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 0.0f - move); // Refraction texture glMultiTexCoord2f(GL_TEXTURE2, 0.0f, 0.0f + move2); // Normal map texture glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture glVertex3f(0.0f, this->waterPos.y, this->zWidth); // The front right vertice for the water glMultiTexCoord2f(GL_TEXTURE0, g_WaterUV, 0.0f); // Reflection texture glMultiTexCoord2f(GL_TEXTURE1, refrUV, 0.0f - move); // Refraction texture glMultiTexCoord2f(GL_TEXTURE2, normalUV, 0.0f + move2); // Normal map texture glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture glVertex3f(this->xWidth, this->waterPos.y, this->zWidth); // The back right vertice for the water glMultiTexCoord2f(GL_TEXTURE0, g_WaterUV, g_WaterUV); // Reflection texture glMultiTexCoord2f(GL_TEXTURE1, refrUV, refrUV - move); // Refraction texture glMultiTexCoord2f(GL_TEXTURE2, normalUV, normalUV + move2); // Normal map texture glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture glVertex3f(this->xWidth, this->waterPos.y, 0.0f); glEnd(); this->shader->deactivateShader(); mat.unselect(); /*if(pos.y < this->waterPos.y) { // draw some fog this->fog->activate(); } else { this->fog->deactivate(); }*/ glPopMatrix(); } /** * @brief tick tack, calculates the flow of the water */ void MappedWater::tick(float dt) { // makes the water flow // TODO change it so that the waterflow doesnt depend on the frame rate this->move += this->g_WaterFlow; this->move2 = this->move * this->kNormalMapScale; this->refrUV = this->g_WaterUV; this->normalUV = this->g_WaterUV * this->kNormalMapScale; } /** * @brief prepares everything to render the reflection texutre */ void MappedWater::activateReflection() { // To create the reflection texture we just need to set the view port // to our texture map size, then render the current scene our camera // is looking at to the already allocated texture unit. Since this // is a reflection of the top of the water surface we use clipping // planes to only render the top of the world as a reflection. If // we are below the water we don't flip the reflection but just use // the current view of the top as we are seeing through the water. // When you look through water at the surface it isn't really reflected, // only when looking down from above the water on the surface. // save viewport matrix and change the viewport size glPushAttrib(GL_VIEWPORT_BIT); glViewport(0,0, textureSize, textureSize); glMatrixMode(GL_MODELVIEW); glPushMatrix(); // If our camera is above the water we will render the scene flipped upside down. // In order to line up the reflection nicely with the world we have to translate // the world to the position of our reflected surface, multiplied by two. glEnable(GL_CLIP_PLANE0); Vector pos = State::getCameraNode()->getAbsCoor(); if(pos.y > waterPos.y) { // Translate the world, then flip it upside down glTranslatef(0.0f, waterPos.y*2.0f, 0.0f); glScalef(1.0, -1.0, 1.0); // Since the world is updside down we need to change the culling to FRONT glCullFace(GL_FRONT); // Set our plane equation and turn clipping on double plane[4] = {0.0, 1.0, 0.0, -waterPos.y}; glClipPlane(GL_CLIP_PLANE0, plane); } else { // If the camera is below the water we don't want to flip the world, // but just render it clipped so only the top is drawn. double plane[4] = {0.0, 1.0, 0.0, waterPos.y}; glClipPlane(GL_CLIP_PLANE0, plane); } } /** * @brief ends the reflection and saves the graphic buffer into a texture */ void MappedWater::deactivateReflection() { glDisable(GL_CLIP_PLANE0); glCullFace(GL_BACK); // Create the texture and store it on the video card mat.renderToTexture(0, GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize); glPopMatrix(); glPopAttrib(); } /** * @brief prepares everything to render the refraction texutre */ void MappedWater::activateRefraction() { // To create the refraction and depth textures we do the same thing // we did for the reflection texture, except we don't need to turn // the world upside down. We want to find the depth of the water, // not the depth of the sky and above water terrain. // save viewport matrix and change the viewport size glPushAttrib(GL_VIEWPORT_BIT); glViewport(0,0, textureSize, textureSize); glMatrixMode(GL_MODELVIEW); glPushMatrix(); // If our camera is above the water we will render only the parts that // are under the water. If the camera is below the water we render // only the stuff above the water. Like the reflection texture, we // incorporate clipping planes to only render what we need. // If the camera is above water, render the data below the water glEnable(GL_CLIP_PLANE0); Vector pos = State::getCameraNode()->getAbsCoor(); if(pos.y > waterPos.y) { double plane[4] = {0.0, -1.0, 0.0, waterPos.y}; glClipPlane(GL_CLIP_PLANE0, plane); } // If the camera is below the water, only render the data above the water else { glCullFace(GL_FRONT); double plane[4] = {0.0, 1.0, 0.0, -waterPos.y}; glClipPlane(GL_CLIP_PLANE0, plane); } } /** * @brief ends the refraction and saves the graphic buffer into a texture */ void MappedWater::deactivateRefraction() { glDisable(GL_CLIP_PLANE0); glCullFace(GL_BACK); // Create the texture and store it on the video card mat.renderToTexture(1, GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize); glPopMatrix(); glPopAttrib(); }