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