| 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: Benjamin Grauer |
|---|
| 13 | co-programmer: ... |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
|---|
| 17 | |
|---|
| 18 | #include "water.h" |
|---|
| 19 | #include "factory.h" |
|---|
| 20 | #include "load_param.h" |
|---|
| 21 | |
|---|
| 22 | #include "grid.h" |
|---|
| 23 | #include "material.h" |
|---|
| 24 | |
|---|
| 25 | #include "resource_manager.h" |
|---|
| 26 | #include "shader.h" |
|---|
| 27 | |
|---|
| 28 | #include "skybox.h" |
|---|
| 29 | |
|---|
| 30 | #include "network_game_manager.h" |
|---|
| 31 | |
|---|
| 32 | using namespace std; |
|---|
| 33 | |
|---|
| 34 | CREATE_FACTORY(Water, CL_WATER); |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | Water::Water(const TiXmlElement* root) |
|---|
| 38 | { |
|---|
| 39 | this->setClassID(CL_WATER, "Water"); |
|---|
| 40 | this->toList(OM_ENVIRON); |
|---|
| 41 | |
|---|
| 42 | this->resX = this->resY = 10; |
|---|
| 43 | this->sizeX = this->sizeY = 1.0f; |
|---|
| 44 | this->height = 0.5f; |
|---|
| 45 | this->grid = NULL; |
|---|
| 46 | |
|---|
| 47 | this->velocities = NULL; |
|---|
| 48 | this->viscosity = 5; |
|---|
| 49 | this->cohesion = .0000000001; |
|---|
| 50 | |
|---|
| 51 | if (root != NULL) |
|---|
| 52 | this->loadParams(root); |
|---|
| 53 | |
|---|
| 54 | this->rebuildGrid(); |
|---|
| 55 | this->waterMaterial = new Material(); |
|---|
| 56 | this->waterShader = (Shader*)ResourceManager::getInstance()->load("shaders/water.vert", SHADER, RP_GAME, (void*)"shaders/water.frag"); |
|---|
| 57 | |
|---|
| 58 | this->grid->height(this->grid->columns()/2,this->grid->rows()/2) = 100; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | Water::~Water() |
|---|
| 62 | { |
|---|
| 63 | delete this->grid; |
|---|
| 64 | delete this->waterMaterial; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | void Water::loadParams(const TiXmlElement* root) |
|---|
| 68 | { |
|---|
| 69 | WorldEntity::loadParams(root); |
|---|
| 70 | |
|---|
| 71 | LoadParam(root, "size", this, Water, setSize) |
|---|
| 72 | .describe("the size of the WaterSurface") |
|---|
| 73 | .defaultValues(2, 1.0f, 1.0f); |
|---|
| 74 | |
|---|
| 75 | LoadParam(root, "resolution", this, Water, setResolution) |
|---|
| 76 | .describe("sets the resolution of the water surface") |
|---|
| 77 | .defaultValues(2, 10, 10); |
|---|
| 78 | |
|---|
| 79 | LoadParam(root, "height", this, Water, setHeight) |
|---|
| 80 | .describe("the height of the Waves") |
|---|
| 81 | .defaultValues(1, 0.5f); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | void Water::rebuildGrid() |
|---|
| 85 | { |
|---|
| 86 | if (this->velocities != NULL) |
|---|
| 87 | { |
|---|
| 88 | assert (this->grid != NULL); |
|---|
| 89 | for (unsigned int i = 0; i < this->grid->rows(); i++) |
|---|
| 90 | delete[] this->velocities[i]; |
|---|
| 91 | delete[] this->velocities; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | // WE DO NOT NEED THIS AS IT IS DONE IN WORLDENTITY->setModel(); |
|---|
| 95 | // if (this->grid != NULL) |
|---|
| 96 | // this->grid = NULL; |
|---|
| 97 | |
|---|
| 98 | this->grid = new Grid(this->sizeX, this->sizeY, this->resX, this->resY); |
|---|
| 99 | this->velocities = new float*[this->resX]; |
|---|
| 100 | for (unsigned int i = 0; i < this->grid->rows(); i++) |
|---|
| 101 | { |
|---|
| 102 | this->velocities[i] = new float[this->resY]; |
|---|
| 103 | for (unsigned int j = 0; j < this->resY; j++) |
|---|
| 104 | this->velocities[i][j] = 0.0; |
|---|
| 105 | } |
|---|
| 106 | this->setModel(this->grid, 0); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | void Water::setResolution(unsigned int resX, unsigned int resY) |
|---|
| 110 | { |
|---|
| 111 | this->resX = resX; |
|---|
| 112 | this->resY = resY; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | void Water::setSize(float sizeX, float sizeY) |
|---|
| 116 | { |
|---|
| 117 | this->sizeX = sizeX; |
|---|
| 118 | this->sizeY = sizeY; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | void Water::setHeight(float height) |
|---|
| 122 | { |
|---|
| 123 | this->height = height; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | void Water::draw() const |
|---|
| 128 | { |
|---|
| 129 | //SkyBox::enableCubeMap(); |
|---|
| 130 | |
|---|
| 131 | glBindTexture(GL_TEXTURE_2D, 15); |
|---|
| 132 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
|---|
| 133 | |
|---|
| 134 | glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); |
|---|
| 135 | glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); |
|---|
| 136 | glEnable(GL_TEXTURE_GEN_S); |
|---|
| 137 | glEnable(GL_TEXTURE_GEN_T); |
|---|
| 138 | |
|---|
| 139 | glEnable(GL_BLEND); |
|---|
| 140 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|---|
| 141 | // this->waterShader->activateShader(); |
|---|
| 142 | // this->waterMaterial->select(); |
|---|
| 143 | WorldEntity::draw(); |
|---|
| 144 | //Shader::deactivateShader(); |
|---|
| 145 | |
|---|
| 146 | SkyBox::disableCubeMap(); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | void Water::tick(float dt) |
|---|
| 150 | { |
|---|
| 151 | /* |
|---|
| 152 | THE OLD USELESS ALGORITHM |
|---|
| 153 | phase += dt *.1; |
|---|
| 154 | for (unsigned int i = 0; i < this->grid->rows(); i++) |
|---|
| 155 | { |
|---|
| 156 | for (unsigned int j = 0; j < this->grid->columns(); j++) |
|---|
| 157 | { |
|---|
| 158 | this->grid->height(i,j) = this->height*sin(((float)i/(float)this->grid->rows() *phase)+ |
|---|
| 159 | this->height*cos((float)j/(float)this->grid->columns()) * phase * 2.0); |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | this->grid->rebuildNormals(this->height);*/ |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | unsigned int i, j; |
|---|
| 166 | float u; |
|---|
| 167 | |
|---|
| 168 | // wave/advection |
|---|
| 169 | // calc movement |
|---|
| 170 | for(j = 1; j < this->grid->rows() - 1; j++) { |
|---|
| 171 | for(i = 1; i < this->grid->columns() - 1; i++) { |
|---|
| 172 | u = this->grid->height(i+1,j)+ this->grid->height(i-1, j) + |
|---|
| 173 | this->grid->height(i, j+1) + this->grid->height(i, j-1) - |
|---|
| 174 | 4 * this->grid->height(i, j); |
|---|
| 175 | this->velocities[i][j] += dt * this->viscosity * this->viscosity * u / this->height; |
|---|
| 176 | this->grid->height(i, j) += dt * this->velocities[i][j]; |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | /* // advect |
|---|
| 180 | for(j = 1; j < this->grid->rows() - 1; j++) { |
|---|
| 181 | for(i = 1; i < this->grid->columns() - 1; i++) { |
|---|
| 182 | this->grid->height(i, j) += dt * this->velocities[i][j]; |
|---|
| 183 | } |
|---|
| 184 | }*/ |
|---|
| 185 | // bound |
|---|
| 186 | // unsigned int w = this->grid->columns - 1; |
|---|
| 187 | // for(i = 0; i < this->grid->columns; i++) { |
|---|
| 188 | // _map[i][0].u[1] = _map[i][1 ].u[1]; |
|---|
| 189 | // _map[i][w].u[1] = _map[i][w-1].u[1]; |
|---|
| 190 | // _map[0][i].u[1] = _map[1 ][i].u[1]; |
|---|
| 191 | // _map[w][i].u[1] = _map[w-1][i].u[1]; |
|---|
| 192 | // } |
|---|
| 193 | |
|---|
| 194 | // diffusion |
|---|
| 195 | for(j = 1; j < this->grid->rows() - 1; j++) { |
|---|
| 196 | for(i = 1; i < this->grid->columns() - 1 ; i++) { |
|---|
| 197 | u = this->grid->height(i+1, j) + this->grid->height(i-1, j) + |
|---|
| 198 | this->grid->height(i, j+1) + this->grid->height(i, j-1) - |
|---|
| 199 | 4* this->grid->height(i, j); |
|---|
| 200 | this->grid->height(i,j) += dt * this->cohesion * u / this->height; |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | // calc normals |
|---|
| 205 | // float l[3]; |
|---|
| 206 | // float m[3]; |
|---|
| 207 | // for(j = 1; j < this->grid->rows() -1; j++) { |
|---|
| 208 | // for(i = 1; i < this->grid->columns() - 1; i++) { |
|---|
| 209 | // l[0] = this->grid->vertexG(i, j-1).x - this->grid->vertexG(i, j+1).x; |
|---|
| 210 | // l[1] = this->grid->vertexG(i, j-1).y - this->grid->vertexG(i, j+1).y; |
|---|
| 211 | // l[2] = this->grid->vertexG(i, j-1).z - this->grid->vertexG(i, j+1).z; |
|---|
| 212 | // m[0] = this->grid->vertexG(i-1,j).x - this->grid->vertexG(i+1, j).x; |
|---|
| 213 | // m[1] = this->grid->vertexG(i-1,j).y - this->grid->vertexG(i+1, j).y; |
|---|
| 214 | // m[2] = this->grid->vertexG(i-1,j).z - this->grid->vertexG(i+1, j).z; |
|---|
| 215 | // this->grid->normalG(i, j).x = l[1] * m[2] - l[2] * m[1]; |
|---|
| 216 | // this->grid->normalG(i, j).y = l[2] * m[0] - l[0] * m[2]; |
|---|
| 217 | // this->grid->normalG(i, j).z = l[0] * m[1] - l[1] * m[0]; |
|---|
| 218 | // } |
|---|
| 219 | // } |
|---|
| 220 | this->grid->rebuildNormals(this->height); |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | /** |
|---|
| 225 | * Writes data from network containing information about the state |
|---|
| 226 | * @param data pointer to data |
|---|
| 227 | * @param length length of data |
|---|
| 228 | * @param sender hostID of sender |
|---|
| 229 | */ |
|---|
| 230 | int Water::writeBytes( const byte * data, int length, int sender ) |
|---|
| 231 | { |
|---|
| 232 | setRequestedSync( false ); |
|---|
| 233 | setIsOutOfSync( false ); |
|---|
| 234 | |
|---|
| 235 | SYNCHELP_READ_BEGIN(); |
|---|
| 236 | |
|---|
| 237 | SYNCHELP_READ_FKT( Water::writeState ); |
|---|
| 238 | |
|---|
| 239 | return SYNCHELP_READ_N; |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | /** |
|---|
| 244 | * data copied in data will bee sent to another host |
|---|
| 245 | * @param data pointer to data |
|---|
| 246 | * @param maxLength max length of data |
|---|
| 247 | * @return the number of bytes writen |
|---|
| 248 | */ |
|---|
| 249 | int Water::readBytes( byte * data, int maxLength, int * reciever ) |
|---|
| 250 | { |
|---|
| 251 | SYNCHELP_WRITE_BEGIN(); |
|---|
| 252 | |
|---|
| 253 | if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() ) |
|---|
| 254 | { |
|---|
| 255 | (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() ); |
|---|
| 256 | setRequestedSync( true ); |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | int rec = this->getRequestSync(); |
|---|
| 260 | if ( rec > 0 ) |
|---|
| 261 | { |
|---|
| 262 | *reciever = rec; |
|---|
| 263 | SYNCHELP_WRITE_FKT( Water::readState ); |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | *reciever = 0; |
|---|
| 267 | return SYNCHELP_WRITE_N; |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | /** |
|---|
| 273 | * data copied in data will bee sent to another host |
|---|
| 274 | * @param data pointer to data |
|---|
| 275 | * @param maxLength max length of data |
|---|
| 276 | * @return the number of bytes writen |
|---|
| 277 | */ |
|---|
| 278 | int Water::readState( byte * data, int maxLength ) |
|---|
| 279 | { |
|---|
| 280 | SYNCHELP_WRITE_BEGIN(); |
|---|
| 281 | |
|---|
| 282 | SYNCHELP_WRITE_FKT( WorldEntity::readState ); |
|---|
| 283 | |
|---|
| 284 | // sync the size |
|---|
| 285 | SYNCHELP_WRITE_FLOAT( this->sizeX ); |
|---|
| 286 | SYNCHELP_WRITE_FLOAT( this->sizeY ); |
|---|
| 287 | |
|---|
| 288 | //sync resolution |
|---|
| 289 | SYNCHELP_WRITE_INT( this->resX ); |
|---|
| 290 | SYNCHELP_WRITE_INT( this->resY ); |
|---|
| 291 | |
|---|
| 292 | //sync the height |
|---|
| 293 | SYNCHELP_WRITE_FLOAT( this->height ); |
|---|
| 294 | |
|---|
| 295 | return SYNCHELP_WRITE_N; |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | /** |
|---|
| 300 | * Writes data from network containing information about the state |
|---|
| 301 | * @param data pointer to data |
|---|
| 302 | * @param length length of data |
|---|
| 303 | * @param sender hostID of sender |
|---|
| 304 | */ |
|---|
| 305 | int Water::writeState( const byte * data, int length, int sender ) |
|---|
| 306 | { |
|---|
| 307 | SYNCHELP_READ_BEGIN(); |
|---|
| 308 | |
|---|
| 309 | SYNCHELP_READ_FKT( WorldEntity::writeState ); |
|---|
| 310 | |
|---|
| 311 | float f1, f2; |
|---|
| 312 | int i1, i2; |
|---|
| 313 | |
|---|
| 314 | //read the size |
|---|
| 315 | SYNCHELP_READ_FLOAT( f1 ); |
|---|
| 316 | SYNCHELP_READ_FLOAT( f2 ); |
|---|
| 317 | this->sizeX = f1; |
|---|
| 318 | this->sizeY = f2; |
|---|
| 319 | PRINTF(0)("Setting Water to size: %f x %f\n", f1, f2); |
|---|
| 320 | |
|---|
| 321 | //read the resolution |
|---|
| 322 | SYNCHELP_READ_INT( i1 ); |
|---|
| 323 | SYNCHELP_READ_INT( i2 ); |
|---|
| 324 | this->resX = i1; |
|---|
| 325 | this->resY = i2; |
|---|
| 326 | PRINTF(0)("Setting Water to resolution: %i x %i\n", i1, i2); |
|---|
| 327 | |
|---|
| 328 | //read the height |
|---|
| 329 | SYNCHELP_READ_FLOAT( f1 ); |
|---|
| 330 | this->height = f1; |
|---|
| 331 | |
|---|
| 332 | this->rebuildGrid(); |
|---|
| 333 | |
|---|
| 334 | return SYNCHELP_READ_N; |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | |
|---|