Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/environments/water.cc @ 10557

Last change on this file since 10557 was 10114, checked in by patrick, 18 years ago

merged network back to trunk

File size: 9.2 KB
RevLine 
[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
[9869]25#include "resource_shader.h"
[6467]26
[6470]27#include "skybox.h"
[6766]28#include "state.h"
[6467]29
[6766]30
[6695]31#include "network_game_manager.h"
[6470]32
[5357]33
[10114]34
35ObjectListDefinition(Water);
[9869]36CREATE_FACTORY(Water);
[9406]37
[3608]38
[6455]39Water::Water(const TiXmlElement* root)
[4010]40{
[9869]41  this->registerObject(this, Water::_objectList);
[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();
[9869]57  this->waterShader = ResourceShader("shaders/water.vert", "shaders/water.frag");
[6467]58
[6766]59  // To test the Wave equation
60  //this->wave(5.0,4.0, 1, 10);
[9656]61
62  height_handle = registerVarId( new SynchronizeableFloat( &height, &height, "height", PERMISSION_MASTER_SERVER ) );
63  resX_handle = registerVarId( new SynchronizeableUInt( &resX, &resX, "resX", PERMISSION_MASTER_SERVER ) );
64  resY_handle = registerVarId( new SynchronizeableUInt( &resY, &resY, "resY", PERMISSION_MASTER_SERVER ) );
65  sizeX_handle = registerVarId( new SynchronizeableFloat( &sizeX, &sizeX, "sizeX", PERMISSION_MASTER_SERVER ) );
66  sizeY_handle = registerVarId( new SynchronizeableFloat( &sizeY, &sizeY, "sizeY", PERMISSION_MASTER_SERVER ) );
[4010]67}
68
[6455]69Water::~Water()
[4261]70{
71}
72
[6455]73void Water::loadParams(const TiXmlElement* root)
[4010]74{
[6455]75  WorldEntity::loadParams(root);
[4620]76
[6455]77  LoadParam(root, "size", this, Water, setSize)
[6684]78  .describe("the size of the WaterSurface")
[7198]79  .defaultValues(1.0f, 1.0f);
[6341]80
[6455]81  LoadParam(root, "resolution", this, Water, setResolution)
[6684]82  .describe("sets the resolution of the water surface")
[7198]83  .defaultValues(10, 10);
[6458]84
85  LoadParam(root, "height", this, Water, setHeight)
[6684]86  .describe("the height of the Waves")
[7198]87  .defaultValues(0.5f);
[4012]88}
[3803]89
[6771]90/**
91 * @brief rebuilds the Grid below the WaterSurface, killing all hight information
92 *
93 * This should be called on all subGrid changes except wave and tick.
94 */
[6455]95void Water::rebuildGrid()
[4012]96{
[6518]97  if (this->velocities != NULL)
98  {
99    assert (this->grid != NULL);
100    for (unsigned int i = 0; i < this->grid->rows(); i++)
101      delete[] this->velocities[i];
102    delete[] this->velocities;
103  }
[6610]104
[6684]105  //   WE DO NOT NEED THIS AS IT IS DONE IN WORLDENTITY->setModel();
106  //   if (this->grid != NULL)
107  //     this->grid = NULL;
[6610]108
[6455]109  this->grid = new Grid(this->sizeX, this->sizeY, this->resX, this->resY);
[6518]110  this->velocities = new float*[this->resX];
111  for (unsigned int i = 0; i < this->grid->rows(); i++)
[6610]112  {
[6518]113    this->velocities[i] = new float[this->resY];
[6610]114    for (unsigned int j = 0; j < this->resY; j++)
115      this->velocities[i][j] = 0.0;
116  }
[6456]117  this->setModel(this->grid, 0);
[6307]118}
[3411]119
[6766]120void Water::wave(float x, float y, float z, float force)
121{
122  unsigned int row = 0, column = 0;
123  if (!this->posToGridPoint( x, z, row, column))
124    return;
125  this->grid->height(row, column) -= force / ((y - this->getAbsCoor().y) +.1);
126}
127
[6771]128/**
129 * after this a rebuild() must be called
130 */
[6455]131void Water::setResolution(unsigned int resX, unsigned int resY)
[3803]132{
[6455]133  this->resX = resX;
134  this->resY = resY;
[3803]135}
136
[6771]137/**
138 * after this a rebuild() must be called
139 */
[6455]140void Water::setSize(float sizeX, float sizeY)
[3803]141{
[6455]142  this->sizeX = sizeX;
143  this->sizeY = sizeY;
[3803]144}
145
[6458]146void Water::setHeight(float height)
147{
148  this->height = height;
149}
150
151
[6771]152/**
153 * @brief calculated the Position in the Grid, this Point is nearest to
154 * @param x, the x-position over the Grid
155 * @param z: the z-Position(or y) over the Grid
156 * @param row returns the row if not out of range
157 * @param column returns the column if not out of range
158 * @returns true if a valid point is found, false if any x or y are out of range
159 */
[6766]160bool Water::posToGridPoint(float x, float z, unsigned int& row, unsigned int& column)
161{
162  float lower = this->getAbsCoor().y - this->sizeY *.5;
163  float left = this->getAbsCoor().x - this->sizeX *.5;
164  if (x > left && x < left + this->sizeX)
165    row = (unsigned int) ((x- left) / this->grid->gridSpacing());
166  else return false;
167  if (z > lower && z < lower + this->sizeY)
168    column = (unsigned int)((z-lower) / this->grid->gridSpacing());
169  else return false;
170  return true;
171}
172
173
[6457]174void Water::draw() const
175{
[6771]176  assert (this->grid != NULL);
[6684]177  {
[7117]178    glPushAttrib(GL_ENABLE_BIT);
179    glEnable(GL_TEXTURE_2D);
[6771]180    glMatrixMode(GL_MODELVIEW);
181    glPushMatrix();
[6470]182
[6771]183    /* translate */
184    glTranslatef (this->getAbsCoor ().x,
185                  this->getAbsCoor ().y,
186                  this->getAbsCoor ().z);
187    Vector tmpRot = this->getAbsDir().getSpacialAxis();
188    glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
[6523]189
[7042]190     if (State::getSkyBox())
191     {
192       glBindTexture(GL_TEXTURE_2D, State::getSkyBox()->getTexture(SKY_RIGHT));
193       glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
[6771]194
[7042]195       glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
196       glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
197       glEnable(GL_TEXTURE_GEN_S);
198       glEnable(GL_TEXTURE_GEN_T);
199     }
200     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
201
202//    SkyBox::enableCubeMap();
[6771]203    this->grid->draw();
[6684]204    // this->waterShader->activateShader();
205    //  this->waterMaterial->select();
206    //Shader::deactivateShader();
[6523]207
[6860]208    SkyBox::disableCubeMap();
[7043]209    glDisable(GL_TEXTURE_GEN_S);
210    glDisable(GL_TEXTURE_GEN_T);
[6803]211
[6780]212    glPopMatrix();
[7117]213    glPopAttrib();
[6684]214  }
[6457]215}
[6456]216
217void Water::tick(float dt)
218{
[9869]219  ObjectManager::EntityList entityList = State::getObjectManager()->getEntityList(OM_GROUP_01_PROJ);
[7370]220  ObjectManager::EntityList::iterator entity = entityList.begin();
[6766]221  while (entity != entityList.end())
222  {
223    this->wave((*entity)->getAbsCoor(), 10.0*dt);
224    entity++;
225  }
226
227
[6680]228  if (unlikely(this->velocities == NULL))
229    return;
[6684]230  /*
231      THE OLD USELESS ALGORITHM
232    phase += dt *.1;
233    for (unsigned int i = 0; i < this->grid->rows(); i++)
[6457]234    {
[6684]235      for (unsigned int j = 0; j < this->grid->columns(); j++)
236      {
237        this->grid->height(i,j) = this->height*sin(((float)i/(float)this->grid->rows() *phase)+
238            this->height*cos((float)j/(float)this->grid->columns()) * phase * 2.0);
239      }
[6457]240    }
[6684]241    this->grid->rebuildNormals(this->height);*/
[6518]242
243
244  unsigned int i, j;
245  float u;
246
247  // wave/advection
248  // calc movement
[6684]249  for(j = 1; j < this->grid->rows() - 1; j++)
250  {
251    for(i = 1; i < this->grid->columns() - 1; i++)
252    {
[6518]253      u =  this->grid->height(i+1,j)+ this->grid->height(i-1, j) +
[6684]254           this->grid->height(i, j+1) + this->grid->height(i, j-1) -
255           4 * this->grid->height(i, j);
[6518]256      this->velocities[i][j] += dt * this->viscosity * this->viscosity * u / this->height;
[6766]257      this->grid->height(i, j) += dt * this->velocities[i][j]  /* + dt * this->cohesion * u / this->height;*/;
258      this->grid->height(i, j) *= .99;
[6518]259    }
260  }
[6684]261  // boundraries
262  for (j = 0; j < this->grid->rows(); j++)
263  {
264    this->grid->height(0,j) = this->grid->height(1,j);
265    this->grid->height(this->grid->rows()-1,j) = this->grid->height(this->grid->rows()-2, j);
266  }
267  for (i = 0; i < this->grid->rows(); i++)
268  {
269    this->grid->height(i,0) = this->grid->height(i,1);
270    this->grid->height(i,this->grid->columns()-1) = this->grid->height(i, this->grid->columns()-2);
271  }
[6756]272  /*
273  for(j = 1; j < this->grid->rows() - 1; j++) {
[6684]274      for(i = 1; i < this->grid->columns() - 1; i++) {
275        this->grid->height(i, j) += dt * this->velocities[i][j];
276      }
277    }*/
[6518]278
279  // calc normals
[6684]280  //   float l[3];
281  //   float m[3];
282  //   for(j = 1; j < this->grid->rows() -1; j++) {
283  //     for(i = 1; i < this->grid->columns() - 1; i++) {
284  //       l[0] = this->grid->vertexG(i, j-1).x - this->grid->vertexG(i, j+1).x;
285  //       l[1] = this->grid->vertexG(i, j-1).y - this->grid->vertexG(i, j+1).y;
286  //       l[2] = this->grid->vertexG(i, j-1).z - this->grid->vertexG(i, j+1).z;
287  //       m[0] = this->grid->vertexG(i-1,j).x - this->grid->vertexG(i+1, j).x;
288  //       m[1] = this->grid->vertexG(i-1,j).y - this->grid->vertexG(i+1, j).y;
289  //       m[2] = this->grid->vertexG(i-1,j).z - this->grid->vertexG(i+1, j).z;
290  //       this->grid->normalG(i, j).x = l[1] * m[2] - l[2] * m[1];
291  //       this->grid->normalG(i, j).y = l[2] * m[0] - l[0] * m[2];
292  //       this->grid->normalG(i, j).z = l[0] * m[1] - l[1] * m[0];
293  //     }
294  //   }
[6519]295  this->grid->rebuildNormals(this->height);
[6456]296}
[6695]297
298
299
300/**
[7954]301 * function to handle changes in synced vars
302 * @param id ids which have changed
[6695]303 */
[7954]304void Water::varChangeHandler( std::list< int > & id )
[6695]305{
[7954]306  if ( std::find( id.begin(), id.end(), height_handle ) != id.end() ||
307       std::find( id.begin(), id.end(), resX_handle ) != id.end() ||
308       std::find( id.begin(), id.end(), resY_handle ) != id.end() ||
309       std::find( id.begin(), id.end(), sizeX_handle ) != id.end() ||
310       std::find( id.begin(), id.end(), sizeY_handle ) != id.end()
311     )
[6695]312  {
[7954]313    this->rebuildGrid();
[6695]314  }
[9656]315
[7954]316  WorldEntity::varChangeHandler( id );
[6695]317}
Note: See TracBrowser for help on using the repository browser.