/* 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); /// sets start values and parameters this->initParams(); // now the standard values were loaded, if the values are specified in the .oxw file // loadParams will overwrite the standard values with the specified values if (root != NULL) this->loadParams(root); /// initialization of the textures this->initTextures(); /// initialization of the shaders this->initShaders(); } /** * @brief deltes shader and the uniform used by the camera */ MappedWater::~MappedWater() { delete shader; delete cam_uni; } /** * @brief initialization of loadable parameters, sets start standard values */ void MappedWater::initParams() { // those standardvalues will be overwritten if they're also set in the oxw file this->setWaterPos(0, 0, 0); this->setWaterSize(100, 100); this->setWaterUV(9); this->setWaterFlow(0.08); this->setLightPos(0, 10, 0); this->setWaterAngle(0); this->setNormalMapScale(0.25f); // initialization of the texture coords, speeds etc... // normalUV wont change anymore this->normalUV = this->waterUV * this->kNormalMapScale; // move and move2 are used for the reflection and refraction, the values are changed in tick() this->move = 0; this->move2 = this->move * this->kNormalMapScale; } /** * @brief initialization of the textures */ void MappedWater::initTextures() { // sets parameters for the textures this->textureSize = 512; unsigned int channels = 32; GLenum type = GL_RGBA; // set up refleciton texture Texture reflTex(GL_TEXTURE_2D, this->textureSize, this->textureSize, channels, type); mat.setDiffuseMap(reflTex, 0); // set up refraction texture Texture refrTex(GL_TEXTURE_2D, this->textureSize, this->textureSize, channels, type); mat.setDiffuseMap(refrTex, 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); // sets texture parameters for reflection texture 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); // sets texture parameters for refraction texture 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); } /** * @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, setLightPos); LoadParam(root, "wateruv", this, MappedWater, setWaterUV); LoadParam(root, "waterflow", this, MappedWater, setWaterFlow); LoadParam(root, "normalmapscale", this, MappedWater, setNormalMapScale); LoadParam(root, "waterangle", this, MappedWater, setWaterAngle); } /** * @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); glRotatef(this->waterAngle, 0, 1, 0); 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); glDisable(GL_BLEND); glBegin(GL_QUADS); // The back left vertice for the water glMultiTexCoord2f(GL_TEXTURE0, 0, waterUV); // Reflection texture glMultiTexCoord2f(GL_TEXTURE1, 0, waterUV - move); // Refraction texture glMultiTexCoord2f(GL_TEXTURE2, 0, normalUV + move2); // Normal map texture glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture glVertex3f(0, this->waterPos.y, 0); // The front left vertice for the water glMultiTexCoord2f(GL_TEXTURE0, 0, 0); // Reflection texture glMultiTexCoord2f(GL_TEXTURE1, 0, -move); // Refraction texture glMultiTexCoord2f(GL_TEXTURE2, 0, move2); // Normal map texture glMultiTexCoord2f(GL_TEXTURE3, 0, 0); // DUDV map texture glVertex3f(0, this->waterPos.y, this->zWidth); // The front right vertice for the water glMultiTexCoord2f(GL_TEXTURE0, waterUV, 0); // Reflection texture glMultiTexCoord2f(GL_TEXTURE1, waterUV, -move); // Refraction texture glMultiTexCoord2f(GL_TEXTURE2, normalUV, 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, waterUV, waterUV); // Reflection texture glMultiTexCoord2f(GL_TEXTURE1, waterUV, waterUV - 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); glEnd(); this->shader->deactivateShader(); mat.unselect(); glPopMatrix(); } /** * @brief tick tack, calculates the flow of the water */ void MappedWater::tick(float dt) { // makes the water flow this->move += this->waterFlow * dt; this->move2 = this->move * 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, waterPos.y*2, 0); glScalef(1, -1, 1); // 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, 1, 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, 1, 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, -1, 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, 1, 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(); }