Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6760 was 6756, checked in by bensch, 20 years ago

trunk: Burst Particles

File size: 8.4 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;
[6756]182      this->grid->height(i, j) += dt * this->velocities[i][j] + dt * this->cohesion * u / this->height;;
[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  }
[6756]196  /*
197  for(j = 1; j < this->grid->rows() - 1; j++) {
[6684]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
203  // calc normals
[6684]204  //   float l[3];
205  //   float m[3];
206  //   for(j = 1; j < this->grid->rows() -1; j++) {
207  //     for(i = 1; i < this->grid->columns() - 1; i++) {
208  //       l[0] = this->grid->vertexG(i, j-1).x - this->grid->vertexG(i, j+1).x;
209  //       l[1] = this->grid->vertexG(i, j-1).y - this->grid->vertexG(i, j+1).y;
210  //       l[2] = this->grid->vertexG(i, j-1).z - this->grid->vertexG(i, j+1).z;
211  //       m[0] = this->grid->vertexG(i-1,j).x - this->grid->vertexG(i+1, j).x;
212  //       m[1] = this->grid->vertexG(i-1,j).y - this->grid->vertexG(i+1, j).y;
213  //       m[2] = this->grid->vertexG(i-1,j).z - this->grid->vertexG(i+1, j).z;
214  //       this->grid->normalG(i, j).x = l[1] * m[2] - l[2] * m[1];
215  //       this->grid->normalG(i, j).y = l[2] * m[0] - l[0] * m[2];
216  //       this->grid->normalG(i, j).z = l[0] * m[1] - l[1] * m[0];
217  //     }
218  //   }
[6519]219  this->grid->rebuildNormals(this->height);
[6456]220}
[6695]221
222
223/**
224 * Writes data from network containing information about the state
225 * @param data pointer to data
226 * @param length length of data
227 * @param sender hostID of sender
228 */
229int Water::writeBytes( const byte * data, int length, int sender )
230{
231  setRequestedSync( false );
232  setIsOutOfSync( false );
233
234  SYNCHELP_READ_BEGIN();
235
236  SYNCHELP_READ_FKT( Water::writeState );
237
238  return SYNCHELP_READ_N;
239}
240
241
242/**
243 * data copied in data will bee sent to another host
244 * @param data pointer to data
245 * @param maxLength max length of data
246 * @return the number of bytes writen
247 */
248int Water::readBytes( byte * data, int maxLength, int * reciever )
249{
250  SYNCHELP_WRITE_BEGIN();
251
252  if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() )
253  {
254    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
255    setRequestedSync( true );
256  }
257
258  int rec = this->getRequestSync();
259  if ( rec > 0 )
260  {
261    *reciever = rec;
262    SYNCHELP_WRITE_FKT( Water::readState );
263  }
264
265  *reciever = 0;
266  return SYNCHELP_WRITE_N;
267}
268
269
270
271/**
272 * data copied in data will bee sent to another host
273 * @param data pointer to data
274 * @param maxLength max length of data
275 * @return the number of bytes writen
276 */
277int Water::readState( byte * data, int maxLength )
278{
279  SYNCHELP_WRITE_BEGIN();
280
281  SYNCHELP_WRITE_FKT( WorldEntity::readState );
282
283  // sync the size
284  SYNCHELP_WRITE_FLOAT( this->sizeX );
285  SYNCHELP_WRITE_FLOAT( this->sizeY );
286
287  //sync resolution
288  SYNCHELP_WRITE_INT( this->resX );
289  SYNCHELP_WRITE_INT( this->resY );
290
291  //sync the height
292  SYNCHELP_WRITE_FLOAT( this->height );
293
294  return SYNCHELP_WRITE_N;
295}
296
297
298/**
299 * Writes data from network containing information about the state
300 * @param data pointer to data
301 * @param length length of data
302 * @param sender hostID of sender
303 */
304int Water::writeState( const byte * data, int length, int sender )
305{
306  SYNCHELP_READ_BEGIN();
307
308  SYNCHELP_READ_FKT( WorldEntity::writeState );
309
310  float f1, f2;
311  int i1, i2;
312
313  //read the size
314  SYNCHELP_READ_FLOAT( f1 );
315  SYNCHELP_READ_FLOAT( f2 );
316  this->sizeX = f1;
317  this->sizeY = f2;
318  PRINTF(0)("Setting Water to size: %f x %f\n", f1, f2);
319
320  //read the resolution
321  SYNCHELP_READ_INT( i1 );
322  SYNCHELP_READ_INT( i2 );
323  this->resX = i1;
324  this->resY = i2;
325  PRINTF(0)("Setting Water to resolution: %i x %i\n", i1, i2);
326
327  //read the height
328  SYNCHELP_READ_FLOAT( f1 );
329  this->height = f1;
330
331  this->rebuildGrid();
332
333  return SYNCHELP_READ_N;
334}
335
336
Note: See TracBrowser for help on using the repository browser.