Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: some waves

File size: 5.1 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
30
31using namespace std;
32
33CREATE_FACTORY(Water, CL_WATER);
34
35
36Water::Water(const TiXmlElement* root)
37{
38  this->setClassID(CL_WATER, "Water");
39  this->toList(OM_ENVIRON);
40
41  this->resX = this->resY = 10;
42  this->sizeX = this->sizeY = 1.0f;
43  this->height = 0.5f;
44  this->grid = NULL;
45
46  this->velocities = NULL;
47  this->viscosity = 5;
48  this->cohesion = .00002;
49
50  if (root != NULL)
51    this->loadParams(root);
52
53  this->rebuildGrid();
54  this->waterMaterial = new Material();
55  this->waterShader = (Shader*)ResourceManager::getInstance()->load("shaders/water.vert", SHADER, RP_GAME, (void*)"shaders/water.frag");
56
57  this->grid->height(this->grid->columns()/2,this->grid->rows()/2) = 1000;
58}
59
60Water::~Water()
61{
62  delete this->grid;
63  delete this->waterMaterial;
64}
65
66void Water::loadParams(const TiXmlElement* root)
67{
68  WorldEntity::loadParams(root);
69
70  LoadParam(root, "size", this, Water, setSize)
71      .describe("the size of the WaterSurface")
72      .defaultValues(2, 1.0f, 1.0f);
73
74  LoadParam(root, "resolution", this, Water, setResolution)
75      .describe("sets the resolution of the water surface")
76      .defaultValues(2, 10, 10);
77
78  LoadParam(root, "height", this, Water, setHeight)
79      .describe("the height of the Waves")
80      .defaultValues(1, 0.5f);
81}
82
83void Water::rebuildGrid()
84{
85  if (this->velocities != NULL)
86  {
87    assert (this->grid != NULL);
88    for (unsigned int i = 0; i < this->grid->rows(); i++)
89      delete[] this->velocities[i];
90    delete[] this->velocities;
91  }
92  if (this->grid != NULL)
93    delete this->grid;
94  this->grid = new Grid(this->sizeX, this->sizeY, this->resX, this->resY);
95  this->velocities = new float*[this->resX];
96  for (unsigned int i = 0; i < this->grid->rows(); i++)
97    this->velocities[i] = new float[this->resY];
98
99  this->setModel(this->grid, 0);
100}
101
102void Water::setResolution(unsigned int resX, unsigned int resY)
103{
104  this->resX = resX;
105  this->resY = resY;
106}
107
108void Water::setSize(float sizeX, float sizeY)
109{
110  this->sizeX = sizeX;
111  this->sizeY = sizeY;
112}
113
114void Water::setHeight(float height)
115{
116  this->height = height;
117}
118
119
120void Water::draw() const
121{
122  SkyBox::enableCubeMap();
123
124 // this->waterShader->activateShader();
125//  this->waterMaterial->select();
126  WorldEntity::draw();
127  //Shader::deactivateShader();
128
129  SkyBox::disableCubeMap();
130}
131
132void Water::tick(float dt)
133{
134/*
135    THE OLD USELESS ALGORITHM
136  phase += dt *.1;
137  for (unsigned int i = 0; i < this->grid->rows(); i++)
138  {
139    for (unsigned int j = 0; j < this->grid->columns(); j++)
140    {
141      this->grid->height(i,j) = this->height*sin(((float)i/(float)this->grid->rows() *phase)+
142          this->height*cos((float)j/(float)this->grid->columns()) * phase * 2.0);
143    }
144  }
145  this->grid->rebuildNormals(this->height);*/
146
147
148  unsigned int i, j;
149  float u;
150
151  // wave/advection
152  // calc movement
153  for(j = 1; j < this->grid->rows() - 1; j++) {
154    for(i = 1; i < this->grid->columns() - 1; i++) {
155      u =  this->grid->height(i+1,j)+ this->grid->height(i-1, j) +
156          this->grid->height(i, j+1) + this->grid->height(i, j-1) -
157          4 * this->grid->height(i, j);
158      this->velocities[i][j] += dt * this->viscosity * this->viscosity * u / this->height;
159    }
160  }
161  // advect
162  for(j = 1; j < this->grid->rows() - 1; j++) {
163    for(i = 1; i < this->grid->columns() - 1; i++) {
164      this->grid->height(i, j) += dt * this->velocities[i][j];
165    }
166  }
167  // bound
168//   unsigned int w = this->grid->columns - 1;
169//   for(i = 0; i < this->grid->columns; i++) {
170//     _map[i][0].u[1] = _map[i][1  ].u[1];
171//     _map[i][w].u[1] = _map[i][w-1].u[1];
172//     _map[0][i].u[1] = _map[1  ][i].u[1];
173//     _map[w][i].u[1] = _map[w-1][i].u[1];
174//   }
175
176  // diffusion
177  for(j = 1; j < this->grid->rows() - 1; j++) {
178    for(i = 1; i < this->grid->columns() - 1 ; i++) {
179      u = this->grid->height(i+1, j) + this->grid->height(i-1, j) +
180          this->grid->height(i, j+1) + this->grid->height(i, j-1) -
181          4* this->grid->height(i, j);
182      this->grid->height(i,j) += dt * this->cohesion * u / this->height;
183    }
184  }
185
186  // calc normals
187/*  float l[3];
188  float m[3];
189  for(j = 1; j < _width-1; j++) {
190    for(i = 1; i < _width - 1; i++) {
191      l[0] = _map[i][j-1].u[0] - _map[i][j+1].u[0];
192      l[1] = _map[i][j-1].u[1] - _map[i][j+1].u[1];
193      l[2] = _map[i][j-1].u[2] - _map[i][j+1].u[2];
194      m[0] = _map[i-1][j].u[0] - _map[i+1][j].u[0];
195      m[1] = _map[i-1][j].u[1] - _map[i+1][j].u[1];
196      m[2] = _map[i-1][j].u[2] - _map[i+1][j].u[2];
197      _map[i][j].n[0] = l[1]*m[2]-l[2]*m[1];
198      _map[i][j].n[1] = l[2]*m[0]-l[0]*m[2];
199      _map[i][j].n[2] = l[0]*m[1]-l[1]*m[0];
200    }
201  }*/
202}
Note: See TracBrowser for help on using the repository browser.