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 | |
---|
31 | using namespace std; |
---|
32 | |
---|
33 | CREATE_FACTORY(Water, CL_WATER); |
---|
34 | |
---|
35 | |
---|
36 | Water::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 = .0000000001; |
---|
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, "shaders/water.frag"); |
---|
56 | |
---|
57 | this->grid->height(this->grid->columns()/2,this->grid->rows()/2) = 100; |
---|
58 | } |
---|
59 | |
---|
60 | Water::~Water() |
---|
61 | { |
---|
62 | delete this->grid; |
---|
63 | delete this->waterMaterial; |
---|
64 | } |
---|
65 | |
---|
66 | void 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 | |
---|
83 | void 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 | |
---|
93 | // WE DO NOT NEED THIS AS IT IS DONE IN WORLDENTITY->setModel(); |
---|
94 | // if (this->grid != NULL) |
---|
95 | // this->grid = NULL; |
---|
96 | |
---|
97 | this->grid = new Grid(this->sizeX, this->sizeY, this->resX, this->resY); |
---|
98 | this->velocities = new float*[this->resX]; |
---|
99 | for (unsigned int i = 0; i < this->grid->rows(); i++) |
---|
100 | { |
---|
101 | this->velocities[i] = new float[this->resY]; |
---|
102 | for (unsigned int j = 0; j < this->resY; j++) |
---|
103 | this->velocities[i][j] = 0.0; |
---|
104 | } |
---|
105 | this->setModel(this->grid, 0); |
---|
106 | } |
---|
107 | |
---|
108 | void Water::setResolution(unsigned int resX, unsigned int resY) |
---|
109 | { |
---|
110 | this->resX = resX; |
---|
111 | this->resY = resY; |
---|
112 | } |
---|
113 | |
---|
114 | void Water::setSize(float sizeX, float sizeY) |
---|
115 | { |
---|
116 | this->sizeX = sizeX; |
---|
117 | this->sizeY = sizeY; |
---|
118 | } |
---|
119 | |
---|
120 | void Water::setHeight(float height) |
---|
121 | { |
---|
122 | this->height = height; |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | void Water::draw() const |
---|
127 | { |
---|
128 | //SkyBox::enableCubeMap(); |
---|
129 | |
---|
130 | glBindTexture(GL_TEXTURE_2D, 15); |
---|
131 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
---|
132 | |
---|
133 | glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); |
---|
134 | glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); |
---|
135 | glEnable(GL_TEXTURE_GEN_S); |
---|
136 | glEnable(GL_TEXTURE_GEN_T); |
---|
137 | |
---|
138 | glEnable(GL_BLEND); |
---|
139 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
---|
140 | // this->waterShader->activateShader(); |
---|
141 | // this->waterMaterial->select(); |
---|
142 | WorldEntity::draw(); |
---|
143 | //Shader::deactivateShader(); |
---|
144 | |
---|
145 | SkyBox::disableCubeMap(); |
---|
146 | } |
---|
147 | |
---|
148 | void Water::tick(float dt) |
---|
149 | { |
---|
150 | if (unlikely(this->velocities == NULL)) |
---|
151 | return; |
---|
152 | /* |
---|
153 | THE OLD USELESS ALGORITHM |
---|
154 | phase += dt *.1; |
---|
155 | for (unsigned int i = 0; i < this->grid->rows(); i++) |
---|
156 | { |
---|
157 | for (unsigned int j = 0; j < this->grid->columns(); j++) |
---|
158 | { |
---|
159 | this->grid->height(i,j) = this->height*sin(((float)i/(float)this->grid->rows() *phase)+ |
---|
160 | this->height*cos((float)j/(float)this->grid->columns()) * phase * 2.0); |
---|
161 | } |
---|
162 | } |
---|
163 | this->grid->rebuildNormals(this->height);*/ |
---|
164 | |
---|
165 | |
---|
166 | unsigned int i, j; |
---|
167 | float u; |
---|
168 | |
---|
169 | // wave/advection |
---|
170 | // calc movement |
---|
171 | for(j = 1; j < this->grid->rows() - 1; j++) { |
---|
172 | for(i = 1; i < this->grid->columns() - 1; i++) { |
---|
173 | u = this->grid->height(i+1,j)+ this->grid->height(i-1, j) + |
---|
174 | this->grid->height(i, j+1) + this->grid->height(i, j-1) - |
---|
175 | 4 * this->grid->height(i, j); |
---|
176 | this->velocities[i][j] += dt * this->viscosity * this->viscosity * u / this->height; |
---|
177 | this->grid->height(i, j) += dt * this->velocities[i][j]; |
---|
178 | } |
---|
179 | } |
---|
180 | /* // advect |
---|
181 | for(j = 1; j < this->grid->rows() - 1; j++) { |
---|
182 | for(i = 1; i < this->grid->columns() - 1; i++) { |
---|
183 | this->grid->height(i, j) += dt * this->velocities[i][j]; |
---|
184 | } |
---|
185 | }*/ |
---|
186 | // bound |
---|
187 | // unsigned int w = this->grid->columns - 1; |
---|
188 | // for(i = 0; i < this->grid->columns; i++) { |
---|
189 | // _map[i][0].u[1] = _map[i][1 ].u[1]; |
---|
190 | // _map[i][w].u[1] = _map[i][w-1].u[1]; |
---|
191 | // _map[0][i].u[1] = _map[1 ][i].u[1]; |
---|
192 | // _map[w][i].u[1] = _map[w-1][i].u[1]; |
---|
193 | // } |
---|
194 | |
---|
195 | // diffusion |
---|
196 | for(j = 1; j < this->grid->rows() - 1; j++) { |
---|
197 | for(i = 1; i < this->grid->columns() - 1 ; i++) { |
---|
198 | u = this->grid->height(i+1, j) + this->grid->height(i-1, j) + |
---|
199 | this->grid->height(i, j+1) + this->grid->height(i, j-1) - |
---|
200 | 4* this->grid->height(i, j); |
---|
201 | this->grid->height(i,j) += dt * this->cohesion * u / this->height; |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | // calc normals |
---|
206 | // float l[3]; |
---|
207 | // float m[3]; |
---|
208 | // for(j = 1; j < this->grid->rows() -1; j++) { |
---|
209 | // for(i = 1; i < this->grid->columns() - 1; i++) { |
---|
210 | // l[0] = this->grid->vertexG(i, j-1).x - this->grid->vertexG(i, j+1).x; |
---|
211 | // l[1] = this->grid->vertexG(i, j-1).y - this->grid->vertexG(i, j+1).y; |
---|
212 | // l[2] = this->grid->vertexG(i, j-1).z - this->grid->vertexG(i, j+1).z; |
---|
213 | // m[0] = this->grid->vertexG(i-1,j).x - this->grid->vertexG(i+1, j).x; |
---|
214 | // m[1] = this->grid->vertexG(i-1,j).y - this->grid->vertexG(i+1, j).y; |
---|
215 | // m[2] = this->grid->vertexG(i-1,j).z - this->grid->vertexG(i+1, j).z; |
---|
216 | // this->grid->normalG(i, j).x = l[1] * m[2] - l[2] * m[1]; |
---|
217 | // this->grid->normalG(i, j).y = l[2] * m[0] - l[0] * m[2]; |
---|
218 | // this->grid->normalG(i, j).z = l[0] * m[1] - l[1] * m[0]; |
---|
219 | // } |
---|
220 | // } |
---|
221 | this->grid->rebuildNormals(this->height); |
---|
222 | } |
---|