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 | #include "network_game_manager.h" |
---|
31 | |
---|
32 | using namespace std; |
---|
33 | |
---|
34 | CREATE_FACTORY(Water, CL_WATER); |
---|
35 | |
---|
36 | |
---|
37 | Water::Water(const TiXmlElement* root) |
---|
38 | { |
---|
39 | this->setClassID(CL_WATER, "Water"); |
---|
40 | this->toList(OM_ENVIRON); |
---|
41 | |
---|
42 | this->resX = this->resY = 10; |
---|
43 | this->sizeX = this->sizeY = 1.0f; |
---|
44 | this->height = 0.5f; |
---|
45 | this->grid = NULL; |
---|
46 | |
---|
47 | this->velocities = NULL; |
---|
48 | this->viscosity = 5; |
---|
49 | this->cohesion = .0000000001; |
---|
50 | |
---|
51 | if (root != NULL) |
---|
52 | this->loadParams(root); |
---|
53 | |
---|
54 | this->rebuildGrid(); |
---|
55 | this->waterMaterial = new Material(); |
---|
56 | this->waterShader = (Shader*)ResourceManager::getInstance()->load("shaders/water.vert", SHADER, RP_GAME, "shaders/water.frag"); |
---|
57 | |
---|
58 | this->grid->height(this->grid->columns()/2,this->grid->rows()/2) = 100; |
---|
59 | } |
---|
60 | |
---|
61 | Water::~Water() |
---|
62 | { |
---|
63 | delete this->grid; |
---|
64 | delete this->waterMaterial; |
---|
65 | } |
---|
66 | |
---|
67 | void Water::loadParams(const TiXmlElement* root) |
---|
68 | { |
---|
69 | WorldEntity::loadParams(root); |
---|
70 | |
---|
71 | LoadParam(root, "size", this, Water, setSize) |
---|
72 | .describe("the size of the WaterSurface") |
---|
73 | .defaultValues(2, 1.0f, 1.0f); |
---|
74 | |
---|
75 | LoadParam(root, "resolution", this, Water, setResolution) |
---|
76 | .describe("sets the resolution of the water surface") |
---|
77 | .defaultValues(2, 10, 10); |
---|
78 | |
---|
79 | LoadParam(root, "height", this, Water, setHeight) |
---|
80 | .describe("the height of the Waves") |
---|
81 | .defaultValues(1, 0.5f); |
---|
82 | } |
---|
83 | |
---|
84 | void Water::rebuildGrid() |
---|
85 | { |
---|
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 | } |
---|
93 | |
---|
94 | // WE DO NOT NEED THIS AS IT IS DONE IN WORLDENTITY->setModel(); |
---|
95 | // if (this->grid != NULL) |
---|
96 | // this->grid = NULL; |
---|
97 | |
---|
98 | this->grid = new Grid(this->sizeX, this->sizeY, this->resX, this->resY); |
---|
99 | this->velocities = new float*[this->resX]; |
---|
100 | for (unsigned int i = 0; i < this->grid->rows(); i++) |
---|
101 | { |
---|
102 | this->velocities[i] = new float[this->resY]; |
---|
103 | for (unsigned int j = 0; j < this->resY; j++) |
---|
104 | this->velocities[i][j] = 0.0; |
---|
105 | } |
---|
106 | this->setModel(this->grid, 0); |
---|
107 | } |
---|
108 | |
---|
109 | void Water::setResolution(unsigned int resX, unsigned int resY) |
---|
110 | { |
---|
111 | this->resX = resX; |
---|
112 | this->resY = resY; |
---|
113 | } |
---|
114 | |
---|
115 | void Water::setSize(float sizeX, float sizeY) |
---|
116 | { |
---|
117 | this->sizeX = sizeX; |
---|
118 | this->sizeY = sizeY; |
---|
119 | } |
---|
120 | |
---|
121 | void Water::setHeight(float height) |
---|
122 | { |
---|
123 | this->height = height; |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | void Water::draw() const |
---|
128 | { |
---|
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); |
---|
135 | |
---|
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); |
---|
140 | |
---|
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(); |
---|
146 | |
---|
147 | SkyBox::disableCubeMap(); |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | void Water::tick(float dt) |
---|
152 | { |
---|
153 | if (unlikely(this->velocities == NULL)) |
---|
154 | return; |
---|
155 | /* |
---|
156 | THE OLD USELESS ALGORITHM |
---|
157 | phase += dt *.1; |
---|
158 | for (unsigned int i = 0; i < this->grid->rows(); i++) |
---|
159 | { |
---|
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 | } |
---|
165 | } |
---|
166 | this->grid->rebuildNormals(this->height);*/ |
---|
167 | |
---|
168 | |
---|
169 | unsigned int i, j; |
---|
170 | float u; |
---|
171 | |
---|
172 | // wave/advection |
---|
173 | // calc movement |
---|
174 | for(j = 1; j < this->grid->rows() - 1; j++) |
---|
175 | { |
---|
176 | for(i = 1; i < this->grid->columns() - 1; i++) |
---|
177 | { |
---|
178 | u = this->grid->height(i+1,j)+ this->grid->height(i-1, j) + |
---|
179 | this->grid->height(i, j+1) + this->grid->height(i, j-1) - |
---|
180 | 4 * this->grid->height(i, j); |
---|
181 | this->velocities[i][j] += dt * this->viscosity * this->viscosity * u / this->height; |
---|
182 | this->grid->height(i, j) += dt * this->velocities[i][j] + dt * this->cohesion * u / this->height;; |
---|
183 | } |
---|
184 | } |
---|
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 | /* |
---|
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 | }*/ |
---|
202 | |
---|
203 | // calc normals |
---|
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 | // } |
---|
219 | this->grid->rebuildNormals(this->height); |
---|
220 | } |
---|
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 | */ |
---|
229 | int 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 | */ |
---|
248 | int 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 | */ |
---|
277 | int 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 | */ |
---|
304 | int 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 | |
---|