Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

try fix

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