Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

water painted again

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