13
static int wire_frame = 0;
14
static int normals = 0;
17
static float rotate_x = 30;
18
static float rotate_y = 15;
19
static float translate_z = 4;
20
static float surface[6 * RESOLUTION * (RESOLUTION + 1)];
21
static float normal[6 * RESOLUTION * (RESOLUTION + 1)];
22
static int texture = 0;
24
void draw_water_static (void)
26
glBindTexture(GL_TEXTURE_2D, texture);
28
static float z (const float x, const float y, const float t)
30
const float x2 = x - 3;
31
const float y2 = y + 1;
32
const float xx = x2 * x2;
33
const float yy = y2 * y2;
34
return ((2 * sinf (20 * sqrtf (xx + yy) - 4 * t) +
35
Noise (10 * x, 10 * y, t, 0)) / 200);
40
** Function to load a Jpeg file.
42
int load_texture (const char * filename,
45
const unsigned int size)
48
struct jpeg_decompress_struct cinfo;
49
struct jpeg_error_mgr jerr;
52
cinfo.err = jpeg_std_error (&jerr);
53
jpeg_create_decompress (&cinfo);
55
if (0 == (fd = fopen(filename, "rb")))
58
jpeg_stdio_src (&cinfo, fd);
59
jpeg_read_header (&cinfo, TRUE);
60
if ((cinfo.image_width != size) || (cinfo.image_height != size))
65
if (cinfo.out_color_space == JCS_GRAYSCALE)
69
if (cinfo.out_color_space != JCS_GRAYSCALE)
72
jpeg_start_decompress (&cinfo);
74
while (cinfo.output_scanline < cinfo.output_height)
77
(GL_RGB == format ? 3 * size : size) * cinfo.output_scanline;
78
jpeg_read_scanlines (&cinfo, &line, 1);
80
jpeg_finish_decompress (&cinfo);
81
jpeg_destroy_decompress (&cinfo);
86
** Function called to update rendering
88
void DisplayFunc (void)
90
const float t = 20. / 1000.;
91
const float delta = 2. / RESOLUTION;
92
const unsigned int length = 2 * (RESOLUTION + 1);
93
const float xn = (RESOLUTION + 1) * delta + 1;
99
unsigned int preindice;
101
/* Yes, I know, this is quite ugly... */
129
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
132
glTranslatef (0, 0, -translate_z);
133
glRotatef (rotate_y, 1, 0, 0);
134
glRotatef (rotate_x, 0, 1, 0);
137
for (j = 0; j < RESOLUTION; j++)
139
y = (j + 1) * delta - 1;
140
for (i = 0; i <= RESOLUTION; i++)
142
indice = 6 * (i + j * (RESOLUTION + 1));
145
surface[indice + 3] = x;
146
surface[indice + 4] = z (x, y, t);
147
surface[indice + 5] = y;
150
/* Values were computed during the previous loop */
151
preindice = 6 * (i + (j - 1) * (RESOLUTION + 1));
152
surface[indice] = surface[preindice + 3];
153
surface[indice + 1] = surface[preindice + 4];
154
surface[indice + 2] = surface[preindice + 5];
159
surface[indice + 1] = z (x, -1, t);
160
surface[indice + 2] = -1;
166
for (j = 0; j < RESOLUTION; j++)
167
for (i = 0; i <= RESOLUTION; i++)
169
indice = 6 * (i + j * (RESOLUTION + 1));
171
v1x = surface[indice + 3];
172
v1y = surface[indice + 4];
173
v1z = surface[indice + 5];
176
v2y = surface[indice + 1];
177
v2z = surface[indice + 2];
181
v3x = surface[indice + 9];
182
v3y = surface[indice + 10];
188
v3y = z (xn, v1z, t);
200
nx = (vby * vaz) - (vbz * vay);
201
ny = (vbz * vax) - (vbx * vaz);
202
nz = (vbx * vay) - (vby * vax);
204
l = sqrtf (nx * nx + ny * ny + nz * nz);
208
normal[indice + 3] = nx * l;
209
normal[indice + 4] = ny * l;
210
normal[indice + 5] = nz * l;
214
normal[indice + 3] = 0;
215
normal[indice + 4] = 1;
216
normal[indice + 5] = 0;
222
/* Values were computed during the previous loop */
223
preindice = 6 * (i + (j - 1) * (RESOLUTION + 1));
224
normal[indice] = normal[preindice + 3];
225
normal[indice + 1] = normal[preindice + 4];
226
normal[indice + 2] = normal[preindice + 5];
231
v1y = z (v1x, (j - 1) * delta - 1, t);
232
v1z = (j - 1) * delta - 1;
235
v3y = z (v3x, v2z, t);
246
nx = (vby * vaz) - (vbz * vay);
247
ny = (vbz * vax) - (vbx * vaz);
248
nz = (vbx * vay) - (vby * vax);
250
l = sqrtf (nx * nx + ny * ny + nz * nz);
254
normal[indice] = nx * l;
255
normal[indice + 1] = ny * l;
256
normal[indice + 2] = nz * l;
261
normal[indice + 1] = 1;
262
normal[indice + 2] = 0;
268
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
269
glDisable (GL_TEXTURE_2D);
270
glColor3f (1, 0.9, 0.7);
271
glBegin (GL_TRIANGLE_FAN);
272
glVertex3f (-1, 0, -1);
273
glVertex3f (-1, 0, 1);
274
glVertex3f ( 1, 0, 1);
275
glVertex3f ( 1, 0, -1);
278
glTranslatef (0, 0.2, 0);
280
/* Render wireframe? */
282
glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
285
glEnable (GL_TEXTURE_2D);
287
glEnableClientState (GL_NORMAL_ARRAY);
288
glEnableClientState (GL_VERTEX_ARRAY);
289
glNormalPointer (GL_FLOAT, 0, normal);
290
glVertexPointer (3, GL_FLOAT, 0, surface);
291
for (i = 0; i < RESOLUTION; i++)
292
glDrawArrays (GL_TRIANGLE_STRIP, i * length, length);
297
glDisable (GL_TEXTURE_2D);
300
for (j = 0; j < RESOLUTION; j++)
301
for (i = 0; i <= RESOLUTION; i++)
303
indice = 6 * (i + j * (RESOLUTION + 1));
304
glVertex3fv (&(surface[indice]));
305
glVertex3f (surface[indice] + normal[indice] / 50,
306
surface[indice + 1] + normal[indice + 1] / 50,
307
surface[indice + 2] + normal[indice + 2] / 50);
315
void init_water (void)
317
unsigned char total_texture[4 * 256 * 256];
318
unsigned char alpha_texture[256 * 256];
319
unsigned char caustic_texture[3 * 256 * 256];
324
/* OpenGL settings */
325
glClearColor (0, 0, 0, 0);
326
glEnable (GL_DEPTH_TEST);
328
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
329
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
330
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
332
/* Texture loading */
333
glGenTextures (1, &texture);
334
if (load_texture ("alpha.jpg", alpha_texture, GL_ALPHA, 256) != 0 ||
335
load_texture ("reflection.jpg", caustic_texture, GL_RGB, 256) != 0)
337
for (i = 0; i < 256 * 256; i++)
339
total_texture[4 * i] = caustic_texture[3 * i];
340
total_texture[4 * i + 1] = caustic_texture[3 * i + 1];
341
total_texture[4 * i + 2] = caustic_texture[3 * i + 2];
342
total_texture[4 * i + 3] = alpha_texture[i];
344
glBindTexture (GL_TEXTURE_2D, texture);
345
gluBuild2DMipmaps (GL_TEXTURE_2D, GL_RGBA, 256, 256, GL_RGBA,
346
GL_UNSIGNED_BYTE, total_texture);
347
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
348
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
349
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
350
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
351
glEnable (GL_TEXTURE_GEN_S);
352
glEnable (GL_TEXTURE_GEN_T);
353
glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
354
glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);