[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" |
---|
[7193] | 19 | #include "util/loading/factory.h" |
---|
| 20 | #include "util/loading/load_param.h" |
---|
[3608] | 21 | |
---|
[6455] | 22 | #include "grid.h" |
---|
[6457] | 23 | #include "material.h" |
---|
[6455] | 24 | |
---|
[7193] | 25 | #include "util/loading/resource_manager.h" |
---|
[6467] | 26 | #include "shader.h" |
---|
| 27 | |
---|
[6470] | 28 | #include "skybox.h" |
---|
[6766] | 29 | #include "state.h" |
---|
[6467] | 30 | |
---|
[6766] | 31 | |
---|
[6695] | 32 | #include "network_game_manager.h" |
---|
[6470] | 33 | |
---|
[5356] | 34 | using namespace std; |
---|
[5357] | 35 | |
---|
[6455] | 36 | CREATE_FACTORY(Water, CL_WATER); |
---|
[3608] | 37 | |
---|
[4010] | 38 | |
---|
[6455] | 39 | Water::Water(const TiXmlElement* root) |
---|
[4010] | 40 | { |
---|
[6456] | 41 | this->setClassID(CL_WATER, "Water"); |
---|
[6457] | 42 | this->toList(OM_ENVIRON); |
---|
[6456] | 43 | |
---|
[6455] | 44 | this->resX = this->resY = 10; |
---|
[6458] | 45 | this->sizeX = this->sizeY = 1.0f; |
---|
| 46 | this->height = 0.5f; |
---|
[6455] | 47 | this->grid = NULL; |
---|
[4010] | 48 | |
---|
[6518] | 49 | this->velocities = NULL; |
---|
| 50 | this->viscosity = 5; |
---|
[6522] | 51 | this->cohesion = .0000000001; |
---|
[6518] | 52 | |
---|
[6455] | 53 | if (root != NULL) |
---|
| 54 | this->loadParams(root); |
---|
[6456] | 55 | |
---|
| 56 | this->rebuildGrid(); |
---|
[6645] | 57 | this->waterShader = (Shader*)ResourceManager::getInstance()->load("shaders/water.vert", SHADER, RP_GAME, "shaders/water.frag"); |
---|
[6467] | 58 | |
---|
[6766] | 59 | // To test the Wave equation |
---|
| 60 | //this->wave(5.0,4.0, 1, 10); |
---|
[4010] | 61 | } |
---|
| 62 | |
---|
[6455] | 63 | Water::~Water() |
---|
[4261] | 64 | { |
---|
| 65 | } |
---|
| 66 | |
---|
[6455] | 67 | void Water::loadParams(const TiXmlElement* root) |
---|
[4010] | 68 | { |
---|
[6455] | 69 | WorldEntity::loadParams(root); |
---|
[4620] | 70 | |
---|
[6455] | 71 | LoadParam(root, "size", this, Water, setSize) |
---|
[6684] | 72 | .describe("the size of the WaterSurface") |
---|
[7198] | 73 | .defaultValues(1.0f, 1.0f); |
---|
[6341] | 74 | |
---|
[6455] | 75 | LoadParam(root, "resolution", this, Water, setResolution) |
---|
[6684] | 76 | .describe("sets the resolution of the water surface") |
---|
[7198] | 77 | .defaultValues(10, 10); |
---|
[6458] | 78 | |
---|
| 79 | LoadParam(root, "height", this, Water, setHeight) |
---|
[6684] | 80 | .describe("the height of the Waves") |
---|
[7198] | 81 | .defaultValues(0.5f); |
---|
[4012] | 82 | } |
---|
[3803] | 83 | |
---|
[6771] | 84 | /** |
---|
| 85 | * @brief rebuilds the Grid below the WaterSurface, killing all hight information |
---|
| 86 | * |
---|
| 87 | * This should be called on all subGrid changes except wave and tick. |
---|
| 88 | */ |
---|
[6455] | 89 | void Water::rebuildGrid() |
---|
[4012] | 90 | { |
---|
[6518] | 91 | if (this->velocities != NULL) |
---|
| 92 | { |
---|
| 93 | assert (this->grid != NULL); |
---|
| 94 | for (unsigned int i = 0; i < this->grid->rows(); i++) |
---|
| 95 | delete[] this->velocities[i]; |
---|
| 96 | delete[] this->velocities; |
---|
| 97 | } |
---|
[6610] | 98 | |
---|
[6684] | 99 | // WE DO NOT NEED THIS AS IT IS DONE IN WORLDENTITY->setModel(); |
---|
| 100 | // if (this->grid != NULL) |
---|
| 101 | // this->grid = NULL; |
---|
[6610] | 102 | |
---|
[6455] | 103 | this->grid = new Grid(this->sizeX, this->sizeY, this->resX, this->resY); |
---|
[6518] | 104 | this->velocities = new float*[this->resX]; |
---|
| 105 | for (unsigned int i = 0; i < this->grid->rows(); i++) |
---|
[6610] | 106 | { |
---|
[6518] | 107 | this->velocities[i] = new float[this->resY]; |
---|
[6610] | 108 | for (unsigned int j = 0; j < this->resY; j++) |
---|
| 109 | this->velocities[i][j] = 0.0; |
---|
| 110 | } |
---|
[6456] | 111 | this->setModel(this->grid, 0); |
---|
[6307] | 112 | } |
---|
[3411] | 113 | |
---|
[6766] | 114 | void Water::wave(float x, float y, float z, float force) |
---|
| 115 | { |
---|
| 116 | unsigned int row = 0, column = 0; |
---|
| 117 | if (!this->posToGridPoint( x, z, row, column)) |
---|
| 118 | return; |
---|
| 119 | this->grid->height(row, column) -= force / ((y - this->getAbsCoor().y) +.1); |
---|
| 120 | } |
---|
| 121 | |
---|
[6771] | 122 | /** |
---|
| 123 | * after this a rebuild() must be called |
---|
| 124 | */ |
---|
[6455] | 125 | void Water::setResolution(unsigned int resX, unsigned int resY) |
---|
[3803] | 126 | { |
---|
[6455] | 127 | this->resX = resX; |
---|
| 128 | this->resY = resY; |
---|
[3803] | 129 | } |
---|
| 130 | |
---|
[6771] | 131 | /** |
---|
| 132 | * after this a rebuild() must be called |
---|
| 133 | */ |
---|
[6455] | 134 | void Water::setSize(float sizeX, float sizeY) |
---|
[3803] | 135 | { |
---|
[6455] | 136 | this->sizeX = sizeX; |
---|
| 137 | this->sizeY = sizeY; |
---|
[3803] | 138 | } |
---|
| 139 | |
---|
[6458] | 140 | void Water::setHeight(float height) |
---|
| 141 | { |
---|
| 142 | this->height = height; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | |
---|
[6771] | 146 | /** |
---|
| 147 | * @brief calculated the Position in the Grid, this Point is nearest to |
---|
| 148 | * @param x, the x-position over the Grid |
---|
| 149 | * @param z: the z-Position(or y) over the Grid |
---|
| 150 | * @param row returns the row if not out of range |
---|
| 151 | * @param column returns the column if not out of range |
---|
| 152 | * @returns true if a valid point is found, false if any x or y are out of range |
---|
| 153 | */ |
---|
[6766] | 154 | bool Water::posToGridPoint(float x, float z, unsigned int& row, unsigned int& column) |
---|
| 155 | { |
---|
| 156 | float lower = this->getAbsCoor().y - this->sizeY *.5; |
---|
| 157 | float left = this->getAbsCoor().x - this->sizeX *.5; |
---|
| 158 | if (x > left && x < left + this->sizeX) |
---|
| 159 | row = (unsigned int) ((x- left) / this->grid->gridSpacing()); |
---|
| 160 | else return false; |
---|
| 161 | if (z > lower && z < lower + this->sizeY) |
---|
| 162 | column = (unsigned int)((z-lower) / this->grid->gridSpacing()); |
---|
| 163 | else return false; |
---|
| 164 | return true; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | |
---|
[6457] | 168 | void Water::draw() const |
---|
| 169 | { |
---|
[6771] | 170 | assert (this->grid != NULL); |
---|
[6684] | 171 | { |
---|
[7117] | 172 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 173 | glEnable(GL_TEXTURE_2D); |
---|
[6771] | 174 | glMatrixMode(GL_MODELVIEW); |
---|
| 175 | glPushMatrix(); |
---|
[6470] | 176 | |
---|
[6771] | 177 | /* translate */ |
---|
| 178 | glTranslatef (this->getAbsCoor ().x, |
---|
| 179 | this->getAbsCoor ().y, |
---|
| 180 | this->getAbsCoor ().z); |
---|
| 181 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
| 182 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
[6523] | 183 | |
---|
[7042] | 184 | if (State::getSkyBox()) |
---|
| 185 | { |
---|
| 186 | glBindTexture(GL_TEXTURE_2D, State::getSkyBox()->getTexture(SKY_RIGHT)); |
---|
| 187 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
---|
[6771] | 188 | |
---|
[7042] | 189 | glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); |
---|
| 190 | glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); |
---|
| 191 | glEnable(GL_TEXTURE_GEN_S); |
---|
| 192 | glEnable(GL_TEXTURE_GEN_T); |
---|
| 193 | } |
---|
| 194 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
---|
| 195 | |
---|
| 196 | // SkyBox::enableCubeMap(); |
---|
[6771] | 197 | this->grid->draw(); |
---|
[6684] | 198 | // this->waterShader->activateShader(); |
---|
| 199 | // this->waterMaterial->select(); |
---|
| 200 | //Shader::deactivateShader(); |
---|
[6523] | 201 | |
---|
[6860] | 202 | SkyBox::disableCubeMap(); |
---|
[7043] | 203 | glDisable(GL_TEXTURE_GEN_S); |
---|
| 204 | glDisable(GL_TEXTURE_GEN_T); |
---|
[6803] | 205 | |
---|
[6780] | 206 | glPopMatrix(); |
---|
[7117] | 207 | glPopAttrib(); |
---|
[6684] | 208 | } |
---|
[6457] | 209 | } |
---|
[6456] | 210 | |
---|
| 211 | void Water::tick(float dt) |
---|
| 212 | { |
---|
[6766] | 213 | std::list<WorldEntity*> entityList = State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ); |
---|
| 214 | std::list<WorldEntity*>::iterator entity = entityList.begin(); |
---|
| 215 | while (entity != entityList.end()) |
---|
| 216 | { |
---|
| 217 | this->wave((*entity)->getAbsCoor(), 10.0*dt); |
---|
| 218 | entity++; |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | |
---|
[6680] | 222 | if (unlikely(this->velocities == NULL)) |
---|
| 223 | return; |
---|
[6684] | 224 | /* |
---|
| 225 | THE OLD USELESS ALGORITHM |
---|
| 226 | phase += dt *.1; |
---|
| 227 | for (unsigned int i = 0; i < this->grid->rows(); i++) |
---|
[6457] | 228 | { |
---|
[6684] | 229 | for (unsigned int j = 0; j < this->grid->columns(); j++) |
---|
| 230 | { |
---|
| 231 | this->grid->height(i,j) = this->height*sin(((float)i/(float)this->grid->rows() *phase)+ |
---|
| 232 | this->height*cos((float)j/(float)this->grid->columns()) * phase * 2.0); |
---|
| 233 | } |
---|
[6457] | 234 | } |
---|
[6684] | 235 | this->grid->rebuildNormals(this->height);*/ |
---|
[6518] | 236 | |
---|
| 237 | |
---|
| 238 | unsigned int i, j; |
---|
| 239 | float u; |
---|
| 240 | |
---|
| 241 | // wave/advection |
---|
| 242 | // calc movement |
---|
[6684] | 243 | for(j = 1; j < this->grid->rows() - 1; j++) |
---|
| 244 | { |
---|
| 245 | for(i = 1; i < this->grid->columns() - 1; i++) |
---|
| 246 | { |
---|
[6518] | 247 | u = this->grid->height(i+1,j)+ this->grid->height(i-1, j) + |
---|
[6684] | 248 | this->grid->height(i, j+1) + this->grid->height(i, j-1) - |
---|
| 249 | 4 * this->grid->height(i, j); |
---|
[6518] | 250 | this->velocities[i][j] += dt * this->viscosity * this->viscosity * u / this->height; |
---|
[6766] | 251 | this->grid->height(i, j) += dt * this->velocities[i][j] /* + dt * this->cohesion * u / this->height;*/; |
---|
| 252 | this->grid->height(i, j) *= .99; |
---|
[6518] | 253 | } |
---|
| 254 | } |
---|
[6684] | 255 | // boundraries |
---|
| 256 | for (j = 0; j < this->grid->rows(); j++) |
---|
| 257 | { |
---|
| 258 | this->grid->height(0,j) = this->grid->height(1,j); |
---|
| 259 | this->grid->height(this->grid->rows()-1,j) = this->grid->height(this->grid->rows()-2, j); |
---|
| 260 | } |
---|
| 261 | for (i = 0; i < this->grid->rows(); i++) |
---|
| 262 | { |
---|
| 263 | this->grid->height(i,0) = this->grid->height(i,1); |
---|
| 264 | this->grid->height(i,this->grid->columns()-1) = this->grid->height(i, this->grid->columns()-2); |
---|
| 265 | } |
---|
[6756] | 266 | /* |
---|
| 267 | for(j = 1; j < this->grid->rows() - 1; j++) { |
---|
[6684] | 268 | for(i = 1; i < this->grid->columns() - 1; i++) { |
---|
| 269 | this->grid->height(i, j) += dt * this->velocities[i][j]; |
---|
| 270 | } |
---|
| 271 | }*/ |
---|
[6518] | 272 | |
---|
| 273 | // calc normals |
---|
[6684] | 274 | // float l[3]; |
---|
| 275 | // float m[3]; |
---|
| 276 | // for(j = 1; j < this->grid->rows() -1; j++) { |
---|
| 277 | // for(i = 1; i < this->grid->columns() - 1; i++) { |
---|
| 278 | // l[0] = this->grid->vertexG(i, j-1).x - this->grid->vertexG(i, j+1).x; |
---|
| 279 | // l[1] = this->grid->vertexG(i, j-1).y - this->grid->vertexG(i, j+1).y; |
---|
| 280 | // l[2] = this->grid->vertexG(i, j-1).z - this->grid->vertexG(i, j+1).z; |
---|
| 281 | // m[0] = this->grid->vertexG(i-1,j).x - this->grid->vertexG(i+1, j).x; |
---|
| 282 | // m[1] = this->grid->vertexG(i-1,j).y - this->grid->vertexG(i+1, j).y; |
---|
| 283 | // m[2] = this->grid->vertexG(i-1,j).z - this->grid->vertexG(i+1, j).z; |
---|
| 284 | // this->grid->normalG(i, j).x = l[1] * m[2] - l[2] * m[1]; |
---|
| 285 | // this->grid->normalG(i, j).y = l[2] * m[0] - l[0] * m[2]; |
---|
| 286 | // this->grid->normalG(i, j).z = l[0] * m[1] - l[1] * m[0]; |
---|
| 287 | // } |
---|
| 288 | // } |
---|
[6519] | 289 | this->grid->rebuildNormals(this->height); |
---|
[6456] | 290 | } |
---|
[6695] | 291 | |
---|
| 292 | |
---|
| 293 | /** |
---|
| 294 | * Writes data from network containing information about the state |
---|
| 295 | * @param data pointer to data |
---|
| 296 | * @param length length of data |
---|
| 297 | * @param sender hostID of sender |
---|
| 298 | */ |
---|
| 299 | int Water::writeBytes( const byte * data, int length, int sender ) |
---|
| 300 | { |
---|
| 301 | setRequestedSync( false ); |
---|
| 302 | setIsOutOfSync( false ); |
---|
| 303 | |
---|
| 304 | SYNCHELP_READ_BEGIN(); |
---|
| 305 | |
---|
[6815] | 306 | SYNCHELP_READ_FKT( Water::writeState, NWT_WAT_STATE ); |
---|
[6695] | 307 | |
---|
| 308 | return SYNCHELP_READ_N; |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | |
---|
| 312 | /** |
---|
| 313 | * data copied in data will bee sent to another host |
---|
| 314 | * @param data pointer to data |
---|
| 315 | * @param maxLength max length of data |
---|
| 316 | * @return the number of bytes writen |
---|
| 317 | */ |
---|
| 318 | int Water::readBytes( byte * data, int maxLength, int * reciever ) |
---|
| 319 | { |
---|
| 320 | SYNCHELP_WRITE_BEGIN(); |
---|
| 321 | |
---|
| 322 | if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() ) |
---|
| 323 | { |
---|
| 324 | (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() ); |
---|
| 325 | setRequestedSync( true ); |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | int rec = this->getRequestSync(); |
---|
| 329 | if ( rec > 0 ) |
---|
| 330 | { |
---|
| 331 | *reciever = rec; |
---|
[6815] | 332 | SYNCHELP_WRITE_FKT( Water::readState, NWT_WAT_STATE ); |
---|
[6695] | 333 | } |
---|
| 334 | |
---|
| 335 | *reciever = 0; |
---|
| 336 | return SYNCHELP_WRITE_N; |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | |
---|
| 340 | |
---|
| 341 | /** |
---|
| 342 | * data copied in data will bee sent to another host |
---|
| 343 | * @param data pointer to data |
---|
| 344 | * @param maxLength max length of data |
---|
| 345 | * @return the number of bytes writen |
---|
| 346 | */ |
---|
| 347 | int Water::readState( byte * data, int maxLength ) |
---|
| 348 | { |
---|
| 349 | SYNCHELP_WRITE_BEGIN(); |
---|
| 350 | |
---|
[6815] | 351 | SYNCHELP_WRITE_FKT( WorldEntity::readState, NWT_WAT_WE_STATE ); |
---|
[6695] | 352 | |
---|
| 353 | // sync the size |
---|
[6815] | 354 | SYNCHELP_WRITE_FLOAT( this->sizeX, NWT_WAT_SIZEX ); |
---|
| 355 | SYNCHELP_WRITE_FLOAT( this->sizeY, NWT_WAT_SIZEY ); |
---|
[6695] | 356 | |
---|
| 357 | //sync resolution |
---|
[6815] | 358 | SYNCHELP_WRITE_INT( this->resX, NWT_WAT_RESX ); |
---|
| 359 | SYNCHELP_WRITE_INT( this->resY, NWT_WAT_RESY ); |
---|
[6695] | 360 | |
---|
| 361 | //sync the height |
---|
[6815] | 362 | SYNCHELP_WRITE_FLOAT( this->height, NWT_WAT_HEIGHT ); |
---|
[6695] | 363 | |
---|
| 364 | return SYNCHELP_WRITE_N; |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | |
---|
| 368 | /** |
---|
| 369 | * Writes data from network containing information about the state |
---|
| 370 | * @param data pointer to data |
---|
| 371 | * @param length length of data |
---|
| 372 | * @param sender hostID of sender |
---|
| 373 | */ |
---|
| 374 | int Water::writeState( const byte * data, int length, int sender ) |
---|
| 375 | { |
---|
| 376 | SYNCHELP_READ_BEGIN(); |
---|
| 377 | |
---|
[6815] | 378 | SYNCHELP_READ_FKT( WorldEntity::writeState, NWT_WAT_WE_STATE ); |
---|
[6695] | 379 | |
---|
| 380 | float f1, f2; |
---|
| 381 | int i1, i2; |
---|
| 382 | |
---|
| 383 | //read the size |
---|
[6815] | 384 | SYNCHELP_READ_FLOAT( f1, NWT_WAT_SIZEX ); |
---|
| 385 | SYNCHELP_READ_FLOAT( f2, NWT_WAT_SIZEY ); |
---|
[6695] | 386 | this->sizeX = f1; |
---|
| 387 | this->sizeY = f2; |
---|
| 388 | PRINTF(0)("Setting Water to size: %f x %f\n", f1, f2); |
---|
| 389 | |
---|
| 390 | //read the resolution |
---|
[6815] | 391 | SYNCHELP_READ_INT( i1, NWT_WAT_RESX ); |
---|
| 392 | SYNCHELP_READ_INT( i2, NWT_WAT_RESY ); |
---|
[6695] | 393 | this->resX = i1; |
---|
| 394 | this->resY = i2; |
---|
| 395 | PRINTF(0)("Setting Water to resolution: %i x %i\n", i1, i2); |
---|
| 396 | |
---|
| 397 | //read the height |
---|
[6815] | 398 | SYNCHELP_READ_FLOAT( f1, NWT_WAT_HEIGHT ); |
---|
[6695] | 399 | this->height = f1; |
---|
| 400 | |
---|
| 401 | this->rebuildGrid(); |
---|
| 402 | |
---|
| 403 | return SYNCHELP_READ_N; |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | |
---|