Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/world_entities/environments/water.cc @ 9716

Last change on this file since 9716 was 9716, checked in by bensch, 18 years ago

more renamings

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