Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8227 was 7954, checked in by patrick, 18 years ago

trunk: merged the network branche back to trunk.

File size: 9.1 KB
Line 
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 "util/loading/factory.h"
20#include "util/loading/load_param.h"
21
22#include "grid.h"
23#include "material.h"
24
25#include "util/loading/resource_manager.h"
26#include "shader.h"
27
28#include "skybox.h"
29#include "state.h"
30
31
32#include "network_game_manager.h"
33
34using namespace std;
35
36CREATE_FACTORY(Water, CL_WATER);
37
38
39Water::Water(const TiXmlElement* root)
40{
41  this->setClassID(CL_WATER, "Water");
42  this->toList(OM_ENVIRON);
43
44  this->resX = this->resY = 10;
45  this->sizeX = this->sizeY = 1.0f;
46  this->height = 0.5f;
47  this->grid = NULL;
48
49  this->velocities = NULL;
50  this->viscosity = 5;
51  this->cohesion = .0000000001;
52
53  if (root != NULL)
54    this->loadParams(root);
55
56  this->rebuildGrid();
57  this->waterShader = (Shader*)ResourceManager::getInstance()->load("shaders/water.vert", SHADER, RP_GAME, "shaders/water.frag");
58
59  // To test the Wave equation
60  //this->wave(5.0,4.0, 1, 10);
61 
62  height_handle = registerVarId( new SynchronizeableFloat( &height, &height, "height" ) );
63  resX_handle = registerVarId( new SynchronizeableUInt( &resX, &resX, "resX" ) );
64  resY_handle = registerVarId( new SynchronizeableUInt( &resY, &resY, "resY" ) );
65  sizeX_handle = registerVarId( new SynchronizeableFloat( &sizeX, &sizeX, "sizeX" ) );
66  sizeY_handle = registerVarId( new SynchronizeableFloat( &sizeY, &sizeY, "sizeY" ) );
67}
68
69Water::~Water()
70{
71}
72
73void Water::loadParams(const TiXmlElement* root)
74{
75  WorldEntity::loadParams(root);
76
77  LoadParam(root, "size", this, Water, setSize)
78  .describe("the size of the WaterSurface")
79  .defaultValues(1.0f, 1.0f);
80
81  LoadParam(root, "resolution", this, Water, setResolution)
82  .describe("sets the resolution of the water surface")
83  .defaultValues(10, 10);
84
85  LoadParam(root, "height", this, Water, setHeight)
86  .describe("the height of the Waves")
87  .defaultValues(0.5f);
88}
89
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 */
95void Water::rebuildGrid()
96{
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  }
104
105  //   WE DO NOT NEED THIS AS IT IS DONE IN WORLDENTITY->setModel();
106  //   if (this->grid != NULL)
107  //     this->grid = NULL;
108
109  this->grid = new Grid(this->sizeX, this->sizeY, this->resX, this->resY);
110  this->velocities = new float*[this->resX];
111  for (unsigned int i = 0; i < this->grid->rows(); i++)
112  {
113    this->velocities[i] = new float[this->resY];
114    for (unsigned int j = 0; j < this->resY; j++)
115      this->velocities[i][j] = 0.0;
116  }
117  this->setModel(this->grid, 0);
118}
119
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
128/**
129 * after this a rebuild() must be called
130 */
131void Water::setResolution(unsigned int resX, unsigned int resY)
132{
133  this->resX = resX;
134  this->resY = resY;
135}
136
137/**
138 * after this a rebuild() must be called
139 */
140void Water::setSize(float sizeX, float sizeY)
141{
142  this->sizeX = sizeX;
143  this->sizeY = sizeY;
144}
145
146void Water::setHeight(float height)
147{
148  this->height = height;
149}
150
151
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 */
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
174void Water::draw() const
175{
176  assert (this->grid != NULL);
177  {
178    glPushAttrib(GL_ENABLE_BIT);
179    glEnable(GL_TEXTURE_2D);
180    glMatrixMode(GL_MODELVIEW);
181    glPushMatrix();
182
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 );
189
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);
194
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();
203    this->grid->draw();
204    // this->waterShader->activateShader();
205    //  this->waterMaterial->select();
206    //Shader::deactivateShader();
207
208    SkyBox::disableCubeMap();
209    glDisable(GL_TEXTURE_GEN_S);
210    glDisable(GL_TEXTURE_GEN_T);
211
212    glPopMatrix();
213    glPopAttrib();
214  }
215}
216
217void Water::tick(float dt)
218{
219  ObjectManager::EntityList entityList = State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ);
220  ObjectManager::EntityList::iterator entity = entityList.begin();
221  while (entity != entityList.end())
222  {
223    this->wave((*entity)->getAbsCoor(), 10.0*dt);
224    entity++;
225  }
226
227
228  if (unlikely(this->velocities == NULL))
229    return;
230  /*
231      THE OLD USELESS ALGORITHM
232    phase += dt *.1;
233    for (unsigned int i = 0; i < this->grid->rows(); i++)
234    {
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      }
240    }
241    this->grid->rebuildNormals(this->height);*/
242
243
244  unsigned int i, j;
245  float u;
246
247  // wave/advection
248  // calc movement
249  for(j = 1; j < this->grid->rows() - 1; j++)
250  {
251    for(i = 1; i < this->grid->columns() - 1; i++)
252    {
253      u =  this->grid->height(i+1,j)+ this->grid->height(i-1, j) +
254           this->grid->height(i, j+1) + this->grid->height(i, j-1) -
255           4 * this->grid->height(i, j);
256      this->velocities[i][j] += dt * this->viscosity * this->viscosity * u / this->height;
257      this->grid->height(i, j) += dt * this->velocities[i][j]  /* + dt * this->cohesion * u / this->height;*/;
258      this->grid->height(i, j) *= .99;
259    }
260  }
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  }
272  /*
273  for(j = 1; j < this->grid->rows() - 1; j++) {
274      for(i = 1; i < this->grid->columns() - 1; i++) {
275        this->grid->height(i, j) += dt * this->velocities[i][j];
276      }
277    }*/
278
279  // calc normals
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  //   }
295  this->grid->rebuildNormals(this->height);
296}
297
298
299
300/**
301 * function to handle changes in synced vars
302 * @param id ids which have changed
303 */
304void Water::varChangeHandler( std::list< int > & id )
305{
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     )
312  {
313    this->rebuildGrid();
314  }
315 
316  WorldEntity::varChangeHandler( id );
317}
Note: See TracBrowser for help on using the repository browser.