Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: splash splash

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