File: bubble.c

package info (click to toggle)
glut 3.7-14
  • links: PTS
  • area: main
  • in suites: woody
  • size: 12,556 kB
  • ctags: 45,170
  • sloc: ansic: 148,716; makefile: 35,208; ada: 2,062; yacc: 473; fortran: 290; lex: 131; csh: 51; sed: 49; sh: 33
file content (450 lines) | stat: -rw-r--r-- 9,946 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "texture.h"

/* Some <math.h> files do not define M_PI... */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#ifndef __sgi
/* Most math.h's do not define float versions of the trig functions. */
#define sinf(x) ((float)sin((x)))
#define cosf(x) ((float)cos((x)))
#endif

static float transp = .5f;
static int normals;
static float phase = .05;
static int tess = 10;
static float freq = 10.f, scale = .03;
static float transx = 1.0, transy, rotx, roty;
static int ox = -1, oy = -1;
static int mot = 0;
#define PAN	1
#define ROT	2

void
pan(const int x, const int y) {
    transx +=  (x-ox)/5.;
    transy -= (y-oy)/5.;
    ox = x; oy = y;
    glutPostRedisplay();
}

void
rotate(const int x, const int y) {
    rotx += x-ox;
    if (rotx > 360.) rotx -= 360.;
    else if (rotx < -360.) rotx += 360.;
    roty += y-oy;
    if (roty > 360.) roty -= 360.;
    else if (roty < -360.) roty += 360.;
    ox = x; oy = y;
    glutPostRedisplay();
}

void
motion(int x, int y) {
    if (mot == PAN) pan(x, y);
    else if (mot == ROT) rotate(x,y);
}

void
mouse(int button, int state, int x, int y) {
    if(state == GLUT_DOWN) {
	switch(button) {
	case GLUT_LEFT_BUTTON:
	    mot = PAN;
	    motion(ox = x, oy = y);
	    break;
	case GLUT_MIDDLE_BUTTON:
	    mot = ROT;
	    motion(ox = x, oy = y);
	    break;
	case GLUT_RIGHT_BUTTON:
	    break;
	}
    } else if (state == GLUT_UP) {
	mot = 0;
    }
}

void init(char *fname) {
    int width, height, components;
    GLubyte *image;

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_SMOOTH);

    if (!(image = (GLubyte *)read_texture(fname, &width, &height, &components))) {
	perror(fname);
	exit(EXIT_FAILURE);
    }
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
		GL_RGBA, GL_UNSIGNED_BYTE, image);

    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);

    glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_GEN_T);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_CULL_FACE);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glFrontFace(GL_CW);
    glCullFace(GL_BACK);
    glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
    glEnable(GL_COLOR_MATERIAL);
#if 1
    glMaterialf (GL_FRONT, GL_SHININESS, 64.0);
    glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
    {
    GLfloat pos[4] = { 0, 0, 1, 1};
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    }
#endif
    { int e; if (e = glGetError()) printf("error %x\n", e); }

#if 0
    glClearColor(.2,.2f,.58f,1.f);
#endif
    glBlendFunc(GL_SRC_ALPHA, GL_ONE);
    glEnable(GL_BLEND);
}

#if 0
cube(void) {
    glBegin(GL_QUAD_STRIP);
	glVertex3f(-1.,-1.,-1.);
	glVertex3f(-1., 1.,-1.);
	glVertex3f( 1.,-1.,-1.);
	glVertex3f( 1., 1.,-1.);
	glVertex3f(-1.,-1.,-1.);
	glVertex3f(-1.,-1.,-1.);
	glVertex3f(-1.,-1.,-1.);
    glEnd();
}
#endif

/*
 * bilinear (longitude/lattitude) tesselation
 * x = cos(theta)*cos(phi)
 * y = sin(phi)
 * z = sin(theta)*cos(phi)
 */

float *normalize(float *v) {
    static float vv[3];
    float len;

    len = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
    vv[0] = v[0]/len; vv[1] = v[1]/len; vv[2] = v[2]/len;
    return vv;
}

void
bubble(float rad) {
    float v[3],s;
    float phi, mod;
    static float off;
    int i, j;
#define N tess
    off += phase;
#if 0
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
#endif
#if 1
    /* top cap */
    glBegin(GL_TRIANGLE_FAN);
    glNormal3f(0.f, 1.f, 0.f);
    glVertex3f(0.f, 1.f, 0.f);
    v[1] = rad*sinf(phi = M_PI*(N+2)/(2*N));
    mod = 1+scale*sinf(freq*v[1]+off);
    s = cosf(phi)*rad*mod;
    for(i = 0; i <= 2*N; i++) {
	v[0] = s*cosf((M_PI/N)*i);
	v[2] = s*sinf((M_PI/N)*i);
	glNormal3fv(v);
	glVertex3fv(v);
    }
    glEnd();
#endif
#if 1
    for(i = 1; i < N-1; i++) {
	float v0[3], v1[3], s0, s1;
	v0[1] = rad*sinf(phi=M_PI/2+M_PI/N*i); s0 = rad*cosf(phi);
	mod = mod = 1+scale*sin(freq*v0[1]+off); s0 *= mod;
	v1[1] = rad*sinf(phi=M_PI/2+M_PI/N*(i+1)); s1 = rad*cosf(phi);
	mod = mod = 1+scale*sin(freq*v1[1]+off); s1 *= mod;
	glBegin(GL_TRIANGLE_STRIP);
	for(j = 0; j <= 2*N; j++) {
	    float x, z;
	    v0[0] = s0*(x = sinf(M_PI/N*j));
	    v0[2] = s0*(z = cosf(M_PI/N*j));
	    v1[0] = s1*x; v1[2] = s1*z;
	    glNormal3fv(normalize(v1));
	    glVertex3fv(v1);
	    glNormal3fv(normalize(v0));
	    glVertex3fv(v0);
	}
	glEnd();
#if 1
	if (normals) {
	float nscale = 1.2;
	/*glDisable(GL_LIGHTING);*/
	glBegin(GL_LINES);
	/*glColor3f(1.f, 1.f, 0.f);*/
	for(j = 0; j <= 2*N; j++) {
	    GLfloat x[3], x1, z1, *vv;
	    v0[0] = s0*(x1 = sinf(M_PI/N*j));
	    v0[2] = s0*(z1 = cosf(M_PI/N*j));
	    glVertex3fv(vv = normalize(v0));
	    x[0] = nscale*vv[0]; x[1] = nscale*vv[1]; x[2] = nscale*vv[2];
	    glVertex3fv(x);

	    v1[0] = s1*x1; v1[2] = s1*z1;
	    glVertex3fv(vv = normalize(v1));
	    x[0] = nscale*vv[0]; x[1] = nscale*vv[1]; x[2] = nscale*vv[2];
	    glVertex3fv(x);
	}
	glEnd();
	/*glEnable(GL_LIGHTING);*/
	}
#endif
    }
#endif
#if 1
    /* bottom cap */
    glBegin(GL_TRIANGLE_FAN);
    glNormal3f(0.f, -1.f, 0.f);
    glVertex3f(0.f, -1.f, 0.f);
    v[1] = -rad*sinf(phi=M_PI*(N+2)/(2*N));
    mod = 1+scale*sin(freq*v[1]+off);
    s = cosf(phi)*rad*mod;
    for(i = 2*N; i >= 0; --i) {
	v[0] = s*cosf((M_PI/N)*i);
	v[2] = s*sinf((M_PI/N)*i);
	glNormal3fv(v);
	glVertex3fv(v);
    }
    glEnd();
#endif
}

void display(void) {
#if 0
    static float phase;
#endif

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

#if 0
    glMatrixMode(GL_TEXTURE);
    glPushMatrix();
    s = sinf(phase); t = s; phase += M_PI/25.f;
    if (phase > 2*M_PI) phase -= 2*M_PI;
    glTranslatef(.5f, -0.5f, 0.f);
    glScalef(.1f, .1f, 1.f);
    glTranslatef(s, t, 0.f);
    glMatrixMode(GL_MODELVIEW);
#endif

    glPushMatrix();
    glTranslatef(0., 0., -100.+transx);
    glRotatef(roty, 1.0, 0.0, 0.0);
    glScalef(10.f, 10.f, 10.f);
    glColor4f(1.f,1.f,1.f,transp);
    bubble(1.0f);
    glPopMatrix ();
#if 0
    glMatrixMode(GL_TEXTURE);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
#endif
    glutSwapBuffers();
}

void reshape(int w, int h) {
    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
#if 0
    if (w <= h)
       glOrtho (-3.5, 3.5, -3.5*(GLfloat)h/(GLfloat)w, 
                3.5*(GLfloat)h/(GLfloat)w, -3.5, 10.5);
    else
       glOrtho (-3.5*(GLfloat)w/(GLfloat)h, 
                3.5*(GLfloat)w/(GLfloat)h, -3.5, 3.5, -3.5, 10.5);
#endif
    gluPerspective(40., 1.0, 10.0, 200000.);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void ffunc(void) {
    static int state = 1;
    if (state ^= 1)
	glBlendFunc(GL_SRC_ALPHA, GL_SRC_ALPHA);
    else
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

void tfunc(void) {
    static int state = 1;
    if (state ^= 1)
	glEnable(GL_TEXTURE_2D);
    else
	glDisable(GL_TEXTURE_2D);
}

void lfunc(void) {
    static int state = 1;
    if (state ^= 1)
	glEnable(GL_LIGHTING);
    else
	glDisable(GL_LIGHTING);
    glEnable(GL_COLOR_MATERIAL);
}

void bfunc(void) {
    static int state = 1;
    if (state ^= 1) {
	glEnable(GL_BLEND);
	glDisable(GL_DEPTH_TEST);
    } else {
	glDisable(GL_BLEND);
	glEnable(GL_DEPTH_TEST);
    }
}

void cfunc(void) {
    static int state = 1;
    if (state ^= 1)
	glEnable(GL_CULL_FACE);
    else
	glDisable(GL_CULL_FACE);
}

void wfunc(void) {
    static int state;
    glPolygonMode(GL_FRONT_AND_BACK, (state ^= 1) ? GL_LINE : GL_FILL);
}

void ufunc(void) {
	tess *= 2;
	if (tess > 160) tess = 160;
}
void Ufunc(void) {
	tess /= 2;
	if (tess < 10) tess = 10;
}

void pfunc(void) {
    phase += .01;
}

void Pfunc(void) {
    phase -= .01;
}

void nfunc(void) {
    normals ^= 1;
}

void xfunc(void) {
    transp += .1;
}

void Xfunc(void) {
    transp -= .1;
}

void yfunc(void) {
    static int state;
    if (state ^= 1)
	/*glClearColor(.2,.2f,.58f,0.f);*/
	glClearColor(.0,.0f,.65,0.f);
    else
	glClearColor(0.f, 0.f, 0.f, 0.f);
}

void up(void) {
    scale += .01f;
}
void down(void) {
    scale -= .01f;
}
void left(void) {
    freq -= 1.f;
}
void right(void) {
    freq += 1.f;
}

/*ARGSUSED1*/
void key (unsigned char key, int x, int y) {
    switch (key) {
    case 'b': bfunc(); break;
    case 'c': cfunc(); break;
    case 'l': lfunc(); break;
    case 't': tfunc(); break;
    case 'f': ffunc(); break;
    case 'n': nfunc(); break;
    case 'u': ufunc(); break;
    case 'U': Ufunc(); break;
    case 'p': pfunc(); break;
    case 'P': Pfunc(); break;
    case 'w': wfunc(); break;
    case 'x': xfunc(); break;
    case 'X': Xfunc(); break;
    case 'y': yfunc(); break;
    case '\033': exit(EXIT_SUCCESS); break;
    default: break;
    }
}

/*ARGSUSED1*/
void
special(int key, int x, int y) {
    switch(key) {
    case GLUT_KEY_UP:	up(); break;
    case GLUT_KEY_DOWN:	down(); break;
    case GLUT_KEY_LEFT:	left(); break;
    case GLUT_KEY_RIGHT:right(); break;
    }
}

void animate(void) {
    glutPostRedisplay();
}

int main(int argc, char** argv) {
    glutInitWindowSize(256, 256);
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowPosition(100, 100);
    glutCreateWindow (argv[0]);
    init(argc > 1 ? argv[1] : "data/cv.rgb");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(key);
    glutSpecialFunc(special);
    glutIdleFunc(animate);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
    return 0;
}