Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6710 was 6695, checked in by patrick, 19 years ago

merged the network branche to the trunk

File size: 9.0 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"
19#include "factory.h"
[5356]20#include "load_param.h"
[3608]21
[6455]22#include "grid.h"
[6457]23#include "material.h"
[6455]24
[6467]25#include "resource_manager.h"
26#include "shader.h"
27
[6470]28#include "skybox.h"
[6467]29
[6695]30#include "network_game_manager.h"
[6470]31
[5356]32using namespace std;
[5357]33
[6455]34CREATE_FACTORY(Water, CL_WATER);
[3608]35
[4010]36
[6455]37Water::Water(const TiXmlElement* root)
[4010]38{
[6456]39  this->setClassID(CL_WATER, "Water");
[6457]40  this->toList(OM_ENVIRON);
[6456]41
[6455]42  this->resX = this->resY = 10;
[6458]43  this->sizeX = this->sizeY = 1.0f;
44  this->height = 0.5f;
[6455]45  this->grid = NULL;
[4010]46
[6518]47  this->velocities = NULL;
48  this->viscosity = 5;
[6522]49  this->cohesion = .0000000001;
[6518]50
[6455]51  if (root != NULL)
52    this->loadParams(root);
[6456]53
54  this->rebuildGrid();
[6457]55  this->waterMaterial = new Material();
[6645]56  this->waterShader = (Shader*)ResourceManager::getInstance()->load("shaders/water.vert", SHADER, RP_GAME, "shaders/water.frag");
[6467]57
[6519]58  this->grid->height(this->grid->columns()/2,this->grid->rows()/2) = 100;
[4010]59}
60
[6455]61Water::~Water()
[4261]62{
[6456]63  delete this->grid;
[6457]64  delete this->waterMaterial;
[4261]65}
66
[6455]67void Water::loadParams(const TiXmlElement* root)
[4010]68{
[6455]69  WorldEntity::loadParams(root);
[4620]70
[6455]71  LoadParam(root, "size", this, Water, setSize)
[6684]72  .describe("the size of the WaterSurface")
73  .defaultValues(2, 1.0f, 1.0f);
[6341]74
[6455]75  LoadParam(root, "resolution", this, Water, setResolution)
[6684]76  .describe("sets the resolution of the water surface")
77  .defaultValues(2, 10, 10);
[6458]78
79  LoadParam(root, "height", this, Water, setHeight)
[6684]80  .describe("the height of the Waves")
81  .defaultValues(1, 0.5f);
[4012]82}
[3803]83
[6455]84void Water::rebuildGrid()
[4012]85{
[6518]86  if (this->velocities != NULL)
87  {
88    assert (this->grid != NULL);
89    for (unsigned int i = 0; i < this->grid->rows(); i++)
90      delete[] this->velocities[i];
91    delete[] this->velocities;
92  }
[6610]93
[6684]94  //   WE DO NOT NEED THIS AS IT IS DONE IN WORLDENTITY->setModel();
95  //   if (this->grid != NULL)
96  //     this->grid = NULL;
[6610]97
[6455]98  this->grid = new Grid(this->sizeX, this->sizeY, this->resX, this->resY);
[6518]99  this->velocities = new float*[this->resX];
100  for (unsigned int i = 0; i < this->grid->rows(); i++)
[6610]101  {
[6518]102    this->velocities[i] = new float[this->resY];
[6610]103    for (unsigned int j = 0; j < this->resY; j++)
104      this->velocities[i][j] = 0.0;
105  }
[6456]106  this->setModel(this->grid, 0);
[6307]107}
[3411]108
[6455]109void Water::setResolution(unsigned int resX, unsigned int resY)
[3803]110{
[6455]111  this->resX = resX;
112  this->resY = resY;
[3803]113}
114
[6455]115void Water::setSize(float sizeX, float sizeY)
[3803]116{
[6455]117  this->sizeX = sizeX;
118  this->sizeY = sizeY;
[3803]119}
120
[6458]121void Water::setHeight(float height)
122{
123  this->height = height;
124}
125
126
[6457]127void Water::draw() const
128{
[6684]129  if (this->grid != NULL)
130  {
131    //SkyBox::enableCubeMap();
132    WorldEntity::draw();
133    glBindTexture(GL_TEXTURE_2D, 15);
134    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
[6470]135
[6684]136    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
137    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
138    glEnable(GL_TEXTURE_GEN_S);
139    glEnable(GL_TEXTURE_GEN_T);
[6523]140
[6684]141    glEnable(GL_BLEND);
142    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
143    // this->waterShader->activateShader();
144    //  this->waterMaterial->select();
145    //Shader::deactivateShader();
[6523]146
[6684]147    SkyBox::disableCubeMap();
148  }
[6457]149}
[6456]150
151void Water::tick(float dt)
152{
[6680]153  if (unlikely(this->velocities == NULL))
154    return;
[6684]155  /*
156      THE OLD USELESS ALGORITHM
157    phase += dt *.1;
158    for (unsigned int i = 0; i < this->grid->rows(); i++)
[6457]159    {
[6684]160      for (unsigned int j = 0; j < this->grid->columns(); j++)
161      {
162        this->grid->height(i,j) = this->height*sin(((float)i/(float)this->grid->rows() *phase)+
163            this->height*cos((float)j/(float)this->grid->columns()) * phase * 2.0);
164      }
[6457]165    }
[6684]166    this->grid->rebuildNormals(this->height);*/
[6518]167
168
169  unsigned int i, j;
170  float u;
171
172  // wave/advection
173  // calc movement
[6684]174  for(j = 1; j < this->grid->rows() - 1; j++)
175  {
176    for(i = 1; i < this->grid->columns() - 1; i++)
177    {
[6518]178      u =  this->grid->height(i+1,j)+ this->grid->height(i-1, j) +
[6684]179           this->grid->height(i, j+1) + this->grid->height(i, j-1) -
180           4 * this->grid->height(i, j);
[6518]181      this->velocities[i][j] += dt * this->viscosity * this->viscosity * u / this->height;
[6522]182      this->grid->height(i, j) += dt * this->velocities[i][j];
[6518]183    }
184  }
[6684]185  // boundraries
186  for (j = 0; j < this->grid->rows(); j++)
187  {
188    this->grid->height(0,j) = this->grid->height(1,j);
189    this->grid->height(this->grid->rows()-1,j) = this->grid->height(this->grid->rows()-2, j);
190  }
191  for (i = 0; i < this->grid->rows(); i++)
192  {
193    this->grid->height(i,0) = this->grid->height(i,1);
194    this->grid->height(i,this->grid->columns()-1) = this->grid->height(i, this->grid->columns()-2);
195  }
196  /*  // advect
197    for(j = 1; j < this->grid->rows() - 1; j++) {
198      for(i = 1; i < this->grid->columns() - 1; i++) {
199        this->grid->height(i, j) += dt * this->velocities[i][j];
200      }
201    }*/
[6518]202  // bound
[6684]203  //   unsigned int w = this->grid->columns - 1;
204  //   for(i = 0; i < this->grid->columns; i++) {
205  //     _map[i][0].u[1] = _map[i][1  ].u[1];
206  //     _map[i][w].u[1] = _map[i][w-1].u[1];
207  //     _map[0][i].u[1] = _map[1  ][i].u[1];
208  //     _map[w][i].u[1] = _map[w-1][i].u[1];
209  //   }
[6518]210
211  // diffusion
[6684]212  for(j = 1; j < this->grid->rows() - 1; j++)
213  {
214    for(i = 1; i < this->grid->columns() - 1 ; i++)
215    {
[6518]216      u = this->grid->height(i+1, j) + this->grid->height(i-1, j) +
217          this->grid->height(i, j+1) + this->grid->height(i, j-1) -
218          4* this->grid->height(i, j);
219      this->grid->height(i,j) += dt * this->cohesion * u / this->height;
220    }
221  }
222
223  // calc normals
[6684]224  //   float l[3];
225  //   float m[3];
226  //   for(j = 1; j < this->grid->rows() -1; j++) {
227  //     for(i = 1; i < this->grid->columns() - 1; i++) {
228  //       l[0] = this->grid->vertexG(i, j-1).x - this->grid->vertexG(i, j+1).x;
229  //       l[1] = this->grid->vertexG(i, j-1).y - this->grid->vertexG(i, j+1).y;
230  //       l[2] = this->grid->vertexG(i, j-1).z - this->grid->vertexG(i, j+1).z;
231  //       m[0] = this->grid->vertexG(i-1,j).x - this->grid->vertexG(i+1, j).x;
232  //       m[1] = this->grid->vertexG(i-1,j).y - this->grid->vertexG(i+1, j).y;
233  //       m[2] = this->grid->vertexG(i-1,j).z - this->grid->vertexG(i+1, j).z;
234  //       this->grid->normalG(i, j).x = l[1] * m[2] - l[2] * m[1];
235  //       this->grid->normalG(i, j).y = l[2] * m[0] - l[0] * m[2];
236  //       this->grid->normalG(i, j).z = l[0] * m[1] - l[1] * m[0];
237  //     }
238  //   }
[6519]239  this->grid->rebuildNormals(this->height);
[6456]240}
[6695]241
242
243/**
244 * Writes data from network containing information about the state
245 * @param data pointer to data
246 * @param length length of data
247 * @param sender hostID of sender
248 */
249int Water::writeBytes( const byte * data, int length, int sender )
250{
251  setRequestedSync( false );
252  setIsOutOfSync( false );
253
254  SYNCHELP_READ_BEGIN();
255
256  SYNCHELP_READ_FKT( Water::writeState );
257
258  return SYNCHELP_READ_N;
259}
260
261
262/**
263 * data copied in data will bee sent to another host
264 * @param data pointer to data
265 * @param maxLength max length of data
266 * @return the number of bytes writen
267 */
268int Water::readBytes( byte * data, int maxLength, int * reciever )
269{
270  SYNCHELP_WRITE_BEGIN();
271
272  if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() )
273  {
274    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
275    setRequestedSync( true );
276  }
277
278  int rec = this->getRequestSync();
279  if ( rec > 0 )
280  {
281    *reciever = rec;
282    SYNCHELP_WRITE_FKT( Water::readState );
283  }
284
285  *reciever = 0;
286  return SYNCHELP_WRITE_N;
287}
288
289
290
291/**
292 * data copied in data will bee sent to another host
293 * @param data pointer to data
294 * @param maxLength max length of data
295 * @return the number of bytes writen
296 */
297int Water::readState( byte * data, int maxLength )
298{
299  SYNCHELP_WRITE_BEGIN();
300
301  SYNCHELP_WRITE_FKT( WorldEntity::readState );
302
303  // sync the size
304  SYNCHELP_WRITE_FLOAT( this->sizeX );
305  SYNCHELP_WRITE_FLOAT( this->sizeY );
306
307  //sync resolution
308  SYNCHELP_WRITE_INT( this->resX );
309  SYNCHELP_WRITE_INT( this->resY );
310
311  //sync the height
312  SYNCHELP_WRITE_FLOAT( this->height );
313
314  return SYNCHELP_WRITE_N;
315}
316
317
318/**
319 * Writes data from network containing information about the state
320 * @param data pointer to data
321 * @param length length of data
322 * @param sender hostID of sender
323 */
324int Water::writeState( const byte * data, int length, int sender )
325{
326  SYNCHELP_READ_BEGIN();
327
328  SYNCHELP_READ_FKT( WorldEntity::writeState );
329
330  float f1, f2;
331  int i1, i2;
332
333  //read the size
334  SYNCHELP_READ_FLOAT( f1 );
335  SYNCHELP_READ_FLOAT( f2 );
336  this->sizeX = f1;
337  this->sizeY = f2;
338  PRINTF(0)("Setting Water to size: %f x %f\n", f1, f2);
339
340  //read the resolution
341  SYNCHELP_READ_INT( i1 );
342  SYNCHELP_READ_INT( i2 );
343  this->resX = i1;
344  this->resY = i2;
345  PRINTF(0)("Setting Water to resolution: %i x %i\n", i1, i2);
346
347  //read the height
348  SYNCHELP_READ_FLOAT( f1 );
349  this->height = f1;
350
351  this->rebuildGrid();
352
353  return SYNCHELP_READ_N;
354}
355
356
Note: See TracBrowser for help on using the repository browser.