| 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, "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 |   if (this->grid != NULL) | 
|---|
| 130 |   { | 
|---|
| 131 |     //SkyBox::enableCubeMap(); | 
|---|
| 132 |     WorldEntity::draw(); | 
|---|
| 133 |     glBindTexture(GL_TEXTURE_2D, 15); | 
|---|
| 134 |     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); | 
|---|
| 135 |  | 
|---|
| 136 |     glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); | 
|---|
| 137 |     glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); | 
|---|
| 138 |     glEnable(GL_TEXTURE_GEN_S); | 
|---|
| 139 |     glEnable(GL_TEXTURE_GEN_T); | 
|---|
| 140 |  | 
|---|
| 141 |     glEnable(GL_BLEND); | 
|---|
| 142 |     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 
|---|
| 143 |     // this->waterShader->activateShader(); | 
|---|
| 144 |     //  this->waterMaterial->select(); | 
|---|
| 145 |     //Shader::deactivateShader(); | 
|---|
| 146 |  | 
|---|
| 147 |     SkyBox::disableCubeMap(); | 
|---|
| 148 |   } | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | void Water::tick(float dt) | 
|---|
| 152 | { | 
|---|
| 153 |   if (unlikely(this->velocities == NULL)) | 
|---|
| 154 |     return; | 
|---|
| 155 |   /* | 
|---|
| 156 |       THE OLD USELESS ALGORITHM | 
|---|
| 157 |     phase += dt *.1; | 
|---|
| 158 |     for (unsigned int i = 0; i < this->grid->rows(); i++) | 
|---|
| 159 |     { | 
|---|
| 160 |       for (unsigned int j = 0; j < this->grid->columns(); j++) | 
|---|
| 161 |       { | 
|---|
| 162 |         this->grid->height(i,j) = this->height*sin(((float)i/(float)this->grid->rows() *phase)+ | 
|---|
| 163 |             this->height*cos((float)j/(float)this->grid->columns()) * phase * 2.0); | 
|---|
| 164 |       } | 
|---|
| 165 |     } | 
|---|
| 166 |     this->grid->rebuildNormals(this->height);*/ | 
|---|
| 167 |  | 
|---|
| 168 |  | 
|---|
| 169 |   unsigned int i, j; | 
|---|
| 170 |   float u; | 
|---|
| 171 |  | 
|---|
| 172 |   // wave/advection | 
|---|
| 173 |   // calc movement | 
|---|
| 174 |   for(j = 1; j < this->grid->rows() - 1; j++) | 
|---|
| 175 |   { | 
|---|
| 176 |     for(i = 1; i < this->grid->columns() - 1; i++) | 
|---|
| 177 |     { | 
|---|
| 178 |       u =  this->grid->height(i+1,j)+ this->grid->height(i-1, j) + | 
|---|
| 179 |            this->grid->height(i, j+1) + this->grid->height(i, j-1) - | 
|---|
| 180 |            4 * this->grid->height(i, j); | 
|---|
| 181 |       this->velocities[i][j] += dt * this->viscosity * this->viscosity * u / this->height; | 
|---|
| 182 |       this->grid->height(i, j) += dt * this->velocities[i][j]; | 
|---|
| 183 |     } | 
|---|
| 184 |   } | 
|---|
| 185 |   // boundraries | 
|---|
| 186 |   for (j = 0; j < this->grid->rows(); j++) | 
|---|
| 187 |   { | 
|---|
| 188 |     this->grid->height(0,j) = this->grid->height(1,j); | 
|---|
| 189 |     this->grid->height(this->grid->rows()-1,j) = this->grid->height(this->grid->rows()-2, j); | 
|---|
| 190 |   } | 
|---|
| 191 |   for (i = 0; i < this->grid->rows(); i++) | 
|---|
| 192 |   { | 
|---|
| 193 |     this->grid->height(i,0) = this->grid->height(i,1); | 
|---|
| 194 |     this->grid->height(i,this->grid->columns()-1) = this->grid->height(i, this->grid->columns()-2); | 
|---|
| 195 |   } | 
|---|
| 196 |   /*  // advect | 
|---|
| 197 |     for(j = 1; j < this->grid->rows() - 1; j++) { | 
|---|
| 198 |       for(i = 1; i < this->grid->columns() - 1; i++) { | 
|---|
| 199 |         this->grid->height(i, j) += dt * this->velocities[i][j]; | 
|---|
| 200 |       } | 
|---|
| 201 |     }*/ | 
|---|
| 202 |   // bound | 
|---|
| 203 |   //   unsigned int w = this->grid->columns - 1; | 
|---|
| 204 |   //   for(i = 0; i < this->grid->columns; i++) { | 
|---|
| 205 |   //     _map[i][0].u[1] = _map[i][1  ].u[1]; | 
|---|
| 206 |   //     _map[i][w].u[1] = _map[i][w-1].u[1]; | 
|---|
| 207 |   //     _map[0][i].u[1] = _map[1  ][i].u[1]; | 
|---|
| 208 |   //     _map[w][i].u[1] = _map[w-1][i].u[1]; | 
|---|
| 209 |   //   } | 
|---|
| 210 |  | 
|---|
| 211 |   // diffusion | 
|---|
| 212 |   for(j = 1; j < this->grid->rows() - 1; j++) | 
|---|
| 213 |   { | 
|---|
| 214 |     for(i = 1; i < this->grid->columns() - 1 ; i++) | 
|---|
| 215 |     { | 
|---|
| 216 |       u = this->grid->height(i+1, j) + this->grid->height(i-1, j) + | 
|---|
| 217 |           this->grid->height(i, j+1) + this->grid->height(i, j-1) - | 
|---|
| 218 |           4* this->grid->height(i, j); | 
|---|
| 219 |       this->grid->height(i,j) += dt * this->cohesion * u / this->height; | 
|---|
| 220 |     } | 
|---|
| 221 |   } | 
|---|
| 222 |  | 
|---|
| 223 |   // calc normals | 
|---|
| 224 |   //   float l[3]; | 
|---|
| 225 |   //   float m[3]; | 
|---|
| 226 |   //   for(j = 1; j < this->grid->rows() -1; j++) { | 
|---|
| 227 |   //     for(i = 1; i < this->grid->columns() - 1; i++) { | 
|---|
| 228 |   //       l[0] = this->grid->vertexG(i, j-1).x - this->grid->vertexG(i, j+1).x; | 
|---|
| 229 |   //       l[1] = this->grid->vertexG(i, j-1).y - this->grid->vertexG(i, j+1).y; | 
|---|
| 230 |   //       l[2] = this->grid->vertexG(i, j-1).z - this->grid->vertexG(i, j+1).z; | 
|---|
| 231 |   //       m[0] = this->grid->vertexG(i-1,j).x - this->grid->vertexG(i+1, j).x; | 
|---|
| 232 |   //       m[1] = this->grid->vertexG(i-1,j).y - this->grid->vertexG(i+1, j).y; | 
|---|
| 233 |   //       m[2] = this->grid->vertexG(i-1,j).z - this->grid->vertexG(i+1, j).z; | 
|---|
| 234 |   //       this->grid->normalG(i, j).x = l[1] * m[2] - l[2] * m[1]; | 
|---|
| 235 |   //       this->grid->normalG(i, j).y = l[2] * m[0] - l[0] * m[2]; | 
|---|
| 236 |   //       this->grid->normalG(i, j).z = l[0] * m[1] - l[1] * m[0]; | 
|---|
| 237 |   //     } | 
|---|
| 238 |   //   } | 
|---|
| 239 |   this->grid->rebuildNormals(this->height); | 
|---|
| 240 | } | 
|---|
| 241 |  | 
|---|
| 242 |  | 
|---|
| 243 | /** | 
|---|
| 244 |  * Writes data from network containing information about the state | 
|---|
| 245 |  * @param data pointer to data | 
|---|
| 246 |  * @param length length of data | 
|---|
| 247 |  * @param sender hostID of sender | 
|---|
| 248 |  */ | 
|---|
| 249 | int Water::writeBytes( const byte * data, int length, int sender ) | 
|---|
| 250 | { | 
|---|
| 251 |   setRequestedSync( false ); | 
|---|
| 252 |   setIsOutOfSync( false ); | 
|---|
| 253 |  | 
|---|
| 254 |   SYNCHELP_READ_BEGIN(); | 
|---|
| 255 |  | 
|---|
| 256 |   SYNCHELP_READ_FKT( Water::writeState ); | 
|---|
| 257 |  | 
|---|
| 258 |   return SYNCHELP_READ_N; | 
|---|
| 259 | } | 
|---|
| 260 |  | 
|---|
| 261 |  | 
|---|
| 262 | /** | 
|---|
| 263 |  * data copied in data will bee sent to another host | 
|---|
| 264 |  * @param data pointer to data | 
|---|
| 265 |  * @param maxLength max length of data | 
|---|
| 266 |  * @return the number of bytes writen | 
|---|
| 267 |  */ | 
|---|
| 268 | int Water::readBytes( byte * data, int maxLength, int * reciever ) | 
|---|
| 269 | { | 
|---|
| 270 |   SYNCHELP_WRITE_BEGIN(); | 
|---|
| 271 |  | 
|---|
| 272 |   if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() ) | 
|---|
| 273 |   { | 
|---|
| 274 |     (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() ); | 
|---|
| 275 |     setRequestedSync( true ); | 
|---|
| 276 |   } | 
|---|
| 277 |  | 
|---|
| 278 |   int rec = this->getRequestSync(); | 
|---|
| 279 |   if ( rec > 0 ) | 
|---|
| 280 |   { | 
|---|
| 281 |     *reciever = rec; | 
|---|
| 282 |     SYNCHELP_WRITE_FKT( Water::readState ); | 
|---|
| 283 |   } | 
|---|
| 284 |  | 
|---|
| 285 |   *reciever = 0; | 
|---|
| 286 |   return SYNCHELP_WRITE_N; | 
|---|
| 287 | } | 
|---|
| 288 |  | 
|---|
| 289 |  | 
|---|
| 290 |  | 
|---|
| 291 | /** | 
|---|
| 292 |  * data copied in data will bee sent to another host | 
|---|
| 293 |  * @param data pointer to data | 
|---|
| 294 |  * @param maxLength max length of data | 
|---|
| 295 |  * @return the number of bytes writen | 
|---|
| 296 |  */ | 
|---|
| 297 | int Water::readState( byte * data, int maxLength ) | 
|---|
| 298 | { | 
|---|
| 299 |   SYNCHELP_WRITE_BEGIN(); | 
|---|
| 300 |  | 
|---|
| 301 |   SYNCHELP_WRITE_FKT( WorldEntity::readState ); | 
|---|
| 302 |  | 
|---|
| 303 |   // sync the size | 
|---|
| 304 |   SYNCHELP_WRITE_FLOAT( this->sizeX ); | 
|---|
| 305 |   SYNCHELP_WRITE_FLOAT( this->sizeY ); | 
|---|
| 306 |  | 
|---|
| 307 |   //sync resolution | 
|---|
| 308 |   SYNCHELP_WRITE_INT( this->resX ); | 
|---|
| 309 |   SYNCHELP_WRITE_INT( this->resY ); | 
|---|
| 310 |  | 
|---|
| 311 |   //sync the height | 
|---|
| 312 |   SYNCHELP_WRITE_FLOAT( this->height ); | 
|---|
| 313 |  | 
|---|
| 314 |   return SYNCHELP_WRITE_N; | 
|---|
| 315 | } | 
|---|
| 316 |  | 
|---|
| 317 |  | 
|---|
| 318 | /** | 
|---|
| 319 |  * Writes data from network containing information about the state | 
|---|
| 320 |  * @param data pointer to data | 
|---|
| 321 |  * @param length length of data | 
|---|
| 322 |  * @param sender hostID of sender | 
|---|
| 323 |  */ | 
|---|
| 324 | int Water::writeState( const byte * data, int length, int sender ) | 
|---|
| 325 | { | 
|---|
| 326 |   SYNCHELP_READ_BEGIN(); | 
|---|
| 327 |  | 
|---|
| 328 |   SYNCHELP_READ_FKT( WorldEntity::writeState ); | 
|---|
| 329 |  | 
|---|
| 330 |   float f1, f2; | 
|---|
| 331 |   int i1, i2; | 
|---|
| 332 |  | 
|---|
| 333 |   //read the size | 
|---|
| 334 |   SYNCHELP_READ_FLOAT( f1 ); | 
|---|
| 335 |   SYNCHELP_READ_FLOAT( f2 ); | 
|---|
| 336 |   this->sizeX = f1; | 
|---|
| 337 |   this->sizeY = f2; | 
|---|
| 338 |   PRINTF(0)("Setting Water to size: %f x %f\n", f1, f2); | 
|---|
| 339 |  | 
|---|
| 340 |   //read the resolution | 
|---|
| 341 |   SYNCHELP_READ_INT( i1 ); | 
|---|
| 342 |   SYNCHELP_READ_INT( i2 ); | 
|---|
| 343 |   this->resX = i1; | 
|---|
| 344 |   this->resY = i2; | 
|---|
| 345 |   PRINTF(0)("Setting Water to resolution: %i x %i\n", i1, i2); | 
|---|
| 346 |  | 
|---|
| 347 |   //read the height | 
|---|
| 348 |   SYNCHELP_READ_FLOAT( f1 ); | 
|---|
| 349 |   this->height = f1; | 
|---|
| 350 |  | 
|---|
| 351 |   this->rebuildGrid(); | 
|---|
| 352 |  | 
|---|
| 353 |   return SYNCHELP_READ_N; | 
|---|
| 354 | } | 
|---|
| 355 |  | 
|---|
| 356 |  | 
|---|