| [4597] | 1 | /* | 
|---|
| [3416] | 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: | 
|---|
| [4261] | 12 |    main-programmer: Benjamin Grauer | 
|---|
 | 13 |    co-programmer: ... | 
|---|
| [3411] | 14 | */ | 
|---|
 | 15 |  | 
|---|
| [3590] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY | 
|---|
 | 17 |  | 
|---|
| [6455] | 18 | #include "water.h" | 
|---|
 | 19 | #include "factory.h" | 
|---|
| [5356] | 20 | #include "load_param.h" | 
|---|
| [3608] | 21 |  | 
|---|
| [6455] | 22 | #include "grid.h" | 
|---|
| [6457] | 23 | #include "material.h" | 
|---|
| [6455] | 24 |  | 
|---|
| [6467] | 25 | #include "resource_manager.h" | 
|---|
 | 26 | #include "shader.h" | 
|---|
 | 27 |  | 
|---|
| [6470] | 28 | #include "skybox.h" | 
|---|
| [6467] | 29 |  | 
|---|
| [6470] | 30 |  | 
|---|
| [5356] | 31 | using namespace std; | 
|---|
| [5357] | 32 |  | 
|---|
| [6455] | 33 | CREATE_FACTORY(Water, CL_WATER); | 
|---|
| [3608] | 34 |  | 
|---|
| [4010] | 35 |  | 
|---|
| [6455] | 36 | Water::Water(const TiXmlElement* root) | 
|---|
| [4010] | 37 | { | 
|---|
| [6456] | 38 |   this->setClassID(CL_WATER, "Water"); | 
|---|
| [6457] | 39 |   this->toList(OM_ENVIRON); | 
|---|
| [6456] | 40 |  | 
|---|
| [6455] | 41 |   this->resX = this->resY = 10; | 
|---|
| [6458] | 42 |   this->sizeX = this->sizeY = 1.0f; | 
|---|
 | 43 |   this->height = 0.5f; | 
|---|
| [6455] | 44 |   this->grid = NULL; | 
|---|
| [4010] | 45 |  | 
|---|
| [6518] | 46 |   this->velocities = NULL; | 
|---|
 | 47 |   this->viscosity = 5; | 
|---|
| [6522] | 48 |   this->cohesion = .0000000001; | 
|---|
| [6518] | 49 |  | 
|---|
| [6455] | 50 |   if (root != NULL) | 
|---|
 | 51 |     this->loadParams(root); | 
|---|
| [6456] | 52 |  | 
|---|
 | 53 |   this->rebuildGrid(); | 
|---|
| [6457] | 54 |   this->waterMaterial = new Material(); | 
|---|
| [6467] | 55 |   this->waterShader = (Shader*)ResourceManager::getInstance()->load("shaders/water.vert", SHADER, RP_GAME, (void*)"shaders/water.frag"); | 
|---|
 | 56 |  | 
|---|
| [6519] | 57 |   this->grid->height(this->grid->columns()/2,this->grid->rows()/2) = 100; | 
|---|
| [4010] | 58 | } | 
|---|
 | 59 |  | 
|---|
| [6455] | 60 | Water::~Water() | 
|---|
| [4261] | 61 | { | 
|---|
| [6456] | 62 |   delete this->grid; | 
|---|
| [6457] | 63 |   delete this->waterMaterial; | 
|---|
| [4261] | 64 | } | 
|---|
 | 65 |  | 
|---|
| [6455] | 66 | void Water::loadParams(const TiXmlElement* root) | 
|---|
| [4010] | 67 | { | 
|---|
| [6455] | 68 |   WorldEntity::loadParams(root); | 
|---|
| [4620] | 69 |  | 
|---|
| [6455] | 70 |   LoadParam(root, "size", this, Water, setSize) | 
|---|
 | 71 |       .describe("the size of the WaterSurface") | 
|---|
 | 72 |       .defaultValues(2, 1.0f, 1.0f); | 
|---|
| [6341] | 73 |  | 
|---|
| [6455] | 74 |   LoadParam(root, "resolution", this, Water, setResolution) | 
|---|
 | 75 |       .describe("sets the resolution of the water surface") | 
|---|
 | 76 |       .defaultValues(2, 10, 10); | 
|---|
| [6458] | 77 |  | 
|---|
 | 78 |   LoadParam(root, "height", this, Water, setHeight) | 
|---|
 | 79 |       .describe("the height of the Waves") | 
|---|
 | 80 |       .defaultValues(1, 0.5f); | 
|---|
| [4012] | 81 | } | 
|---|
| [3803] | 82 |  | 
|---|
| [6455] | 83 | void Water::rebuildGrid() | 
|---|
| [4012] | 84 | { | 
|---|
| [6518] | 85 |   if (this->velocities != NULL) | 
|---|
 | 86 |   { | 
|---|
 | 87 |     assert (this->grid != NULL); | 
|---|
 | 88 |     for (unsigned int i = 0; i < this->grid->rows(); i++) | 
|---|
 | 89 |       delete[] this->velocities[i]; | 
|---|
 | 90 |     delete[] this->velocities; | 
|---|
 | 91 |   } | 
|---|
| [6455] | 92 |   if (this->grid != NULL) | 
|---|
 | 93 |     delete this->grid; | 
|---|
 | 94 |   this->grid = new Grid(this->sizeX, this->sizeY, this->resX, this->resY); | 
|---|
| [6518] | 95 |   this->velocities = new float*[this->resX]; | 
|---|
 | 96 |   for (unsigned int i = 0; i < this->grid->rows(); i++) | 
|---|
 | 97 |     this->velocities[i] = new float[this->resY]; | 
|---|
| [3411] | 98 |  | 
|---|
| [6456] | 99 |   this->setModel(this->grid, 0); | 
|---|
| [6307] | 100 | } | 
|---|
| [3411] | 101 |  | 
|---|
| [6455] | 102 | void Water::setResolution(unsigned int resX, unsigned int resY) | 
|---|
| [3803] | 103 | { | 
|---|
| [6455] | 104 |   this->resX = resX; | 
|---|
 | 105 |   this->resY = resY; | 
|---|
| [3803] | 106 | } | 
|---|
 | 107 |  | 
|---|
| [6455] | 108 | void Water::setSize(float sizeX, float sizeY) | 
|---|
| [3803] | 109 | { | 
|---|
| [6455] | 110 |   this->sizeX = sizeX; | 
|---|
 | 111 |   this->sizeY = sizeY; | 
|---|
| [3803] | 112 | } | 
|---|
 | 113 |  | 
|---|
| [6458] | 114 | void Water::setHeight(float height) | 
|---|
 | 115 | { | 
|---|
 | 116 |   this->height = height; | 
|---|
 | 117 | } | 
|---|
 | 118 |  | 
|---|
 | 119 |  | 
|---|
| [6457] | 120 | void Water::draw() const | 
|---|
 | 121 | { | 
|---|
| [6523] | 122 |   //SkyBox::enableCubeMap(); | 
|---|
| [6470] | 123 |  | 
|---|
| [6523] | 124 |   glBindTexture(GL_TEXTURE_2D, 15); | 
|---|
 | 125 |   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); | 
|---|
 | 126 |  | 
|---|
 | 127 |   glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); | 
|---|
 | 128 |   glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); | 
|---|
 | 129 |   glEnable(GL_TEXTURE_GEN_S); | 
|---|
 | 130 |   glEnable(GL_TEXTURE_GEN_T); | 
|---|
 | 131 |  | 
|---|
 | 132 |   glEnable(GL_BLEND); | 
|---|
 | 133 |   glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 
|---|
 | 134 |   // this->waterShader->activateShader(); | 
|---|
| [6467] | 135 | //  this->waterMaterial->select(); | 
|---|
| [6457] | 136 |   WorldEntity::draw(); | 
|---|
| [6470] | 137 |   //Shader::deactivateShader(); | 
|---|
 | 138 |  | 
|---|
| [6471] | 139 |   SkyBox::disableCubeMap(); | 
|---|
| [6457] | 140 | } | 
|---|
| [6456] | 141 |  | 
|---|
 | 142 | void Water::tick(float dt) | 
|---|
 | 143 | { | 
|---|
| [6518] | 144 | /* | 
|---|
 | 145 |     THE OLD USELESS ALGORITHM | 
|---|
| [6457] | 146 |   phase += dt *.1; | 
|---|
 | 147 |   for (unsigned int i = 0; i < this->grid->rows(); i++) | 
|---|
 | 148 |   { | 
|---|
 | 149 |     for (unsigned int j = 0; j < this->grid->columns(); j++) | 
|---|
 | 150 |     { | 
|---|
| [6458] | 151 |       this->grid->height(i,j) = this->height*sin(((float)i/(float)this->grid->rows() *phase)+ | 
|---|
| [6467] | 152 |           this->height*cos((float)j/(float)this->grid->columns()) * phase * 2.0); | 
|---|
| [6457] | 153 |     } | 
|---|
 | 154 |   } | 
|---|
| [6518] | 155 |   this->grid->rebuildNormals(this->height);*/ | 
|---|
 | 156 |  | 
|---|
 | 157 |  | 
|---|
 | 158 |   unsigned int i, j; | 
|---|
 | 159 |   float u; | 
|---|
 | 160 |  | 
|---|
 | 161 |   // wave/advection | 
|---|
 | 162 |   // calc movement | 
|---|
 | 163 |   for(j = 1; j < this->grid->rows() - 1; j++) { | 
|---|
 | 164 |     for(i = 1; i < this->grid->columns() - 1; i++) { | 
|---|
 | 165 |       u =  this->grid->height(i+1,j)+ this->grid->height(i-1, j) + | 
|---|
 | 166 |           this->grid->height(i, j+1) + this->grid->height(i, j-1) - | 
|---|
 | 167 |           4 * this->grid->height(i, j); | 
|---|
 | 168 |       this->velocities[i][j] += dt * this->viscosity * this->viscosity * u / this->height; | 
|---|
| [6522] | 169 |       this->grid->height(i, j) += dt * this->velocities[i][j]; | 
|---|
| [6518] | 170 |     } | 
|---|
 | 171 |   } | 
|---|
| [6522] | 172 | /*  // advect | 
|---|
| [6518] | 173 |   for(j = 1; j < this->grid->rows() - 1; j++) { | 
|---|
 | 174 |     for(i = 1; i < this->grid->columns() - 1; i++) { | 
|---|
 | 175 |       this->grid->height(i, j) += dt * this->velocities[i][j]; | 
|---|
 | 176 |     } | 
|---|
| [6522] | 177 |   }*/ | 
|---|
| [6518] | 178 |   // bound | 
|---|
 | 179 | //   unsigned int w = this->grid->columns - 1; | 
|---|
 | 180 | //   for(i = 0; i < this->grid->columns; i++) { | 
|---|
 | 181 | //     _map[i][0].u[1] = _map[i][1  ].u[1]; | 
|---|
 | 182 | //     _map[i][w].u[1] = _map[i][w-1].u[1]; | 
|---|
 | 183 | //     _map[0][i].u[1] = _map[1  ][i].u[1]; | 
|---|
 | 184 | //     _map[w][i].u[1] = _map[w-1][i].u[1]; | 
|---|
 | 185 | //   } | 
|---|
 | 186 |  | 
|---|
 | 187 |   // diffusion | 
|---|
 | 188 |   for(j = 1; j < this->grid->rows() - 1; j++) { | 
|---|
 | 189 |     for(i = 1; i < this->grid->columns() - 1 ; i++) { | 
|---|
 | 190 |       u = this->grid->height(i+1, j) + this->grid->height(i-1, j) + | 
|---|
 | 191 |           this->grid->height(i, j+1) + this->grid->height(i, j-1) - | 
|---|
 | 192 |           4* this->grid->height(i, j); | 
|---|
 | 193 |       this->grid->height(i,j) += dt * this->cohesion * u / this->height; | 
|---|
 | 194 |     } | 
|---|
 | 195 |   } | 
|---|
 | 196 |  | 
|---|
 | 197 |   // calc normals | 
|---|
| [6519] | 198 | //   float l[3]; | 
|---|
 | 199 | //   float m[3]; | 
|---|
 | 200 | //   for(j = 1; j < this->grid->rows() -1; j++) { | 
|---|
 | 201 | //     for(i = 1; i < this->grid->columns() - 1; i++) { | 
|---|
 | 202 | //       l[0] = this->grid->vertexG(i, j-1).x - this->grid->vertexG(i, j+1).x; | 
|---|
 | 203 | //       l[1] = this->grid->vertexG(i, j-1).y - this->grid->vertexG(i, j+1).y; | 
|---|
 | 204 | //       l[2] = this->grid->vertexG(i, j-1).z - this->grid->vertexG(i, j+1).z; | 
|---|
 | 205 | //       m[0] = this->grid->vertexG(i-1,j).x - this->grid->vertexG(i+1, j).x; | 
|---|
 | 206 | //       m[1] = this->grid->vertexG(i-1,j).y - this->grid->vertexG(i+1, j).y; | 
|---|
 | 207 | //       m[2] = this->grid->vertexG(i-1,j).z - this->grid->vertexG(i+1, j).z; | 
|---|
 | 208 | //       this->grid->normalG(i, j).x = l[1] * m[2] - l[2] * m[1]; | 
|---|
 | 209 | //       this->grid->normalG(i, j).y = l[2] * m[0] - l[0] * m[2]; | 
|---|
 | 210 | //       this->grid->normalG(i, j).z = l[0] * m[1] - l[1] * m[0]; | 
|---|
 | 211 | //     } | 
|---|
 | 212 | //   } | 
|---|
 | 213 |   this->grid->rebuildNormals(this->height); | 
|---|
| [6456] | 214 | } | 
|---|