added a proper idle() function
[mesa.git] / progs / demos / bounce.c
1 /* $Id: bounce.c,v 1.3 2000/08/16 20:36:34 brianp Exp $ */
2
3 /*
4 * Bouncing ball demo.
5 *
6 * This program is in the public domain
7 *
8 * Brian Paul
9 *
10 * Conversion to GLUT by Mark J. Kilgard
11 */
12
13
14 #include <math.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <GL/glut.h>
18
19 #define COS(X) cos( (X) * 3.14159/180.0 )
20 #define SIN(X) sin( (X) * 3.14159/180.0 )
21
22 #define RED 1
23 #define WHITE 2
24 #define CYAN 3
25
26 GLboolean IndexMode = GL_FALSE;
27 GLuint Ball;
28 GLenum Mode;
29 GLfloat Zrot = 0.0, Zstep = 6.0;
30 GLfloat Xpos = 0.0, Ypos = 1.0;
31 GLfloat Xvel = 0.2, Yvel = 0.0;
32 GLfloat Xmin = -4.0, Xmax = 4.0;
33 GLfloat Ymin = -3.8, Ymax = 4.0;
34 GLfloat G = -0.1;
35
36 static GLuint
37 make_ball(void)
38 {
39 GLuint list;
40 GLfloat a, b;
41 GLfloat da = 18.0, db = 18.0;
42 GLfloat radius = 1.0;
43 GLuint color;
44 GLfloat x, y, z;
45
46 list = glGenLists(1);
47
48 glNewList(list, GL_COMPILE);
49
50 color = 0;
51 for (a = -90.0; a + da <= 90.0; a += da) {
52
53 glBegin(GL_QUAD_STRIP);
54 for (b = 0.0; b <= 360.0; b += db) {
55
56 if (color) {
57 glIndexi(RED);
58 glColor3f(1, 0, 0);
59 } else {
60 glIndexi(WHITE);
61 glColor3f(1, 1, 1);
62 }
63
64 x = radius * COS(b) * COS(a);
65 y = radius * SIN(b) * COS(a);
66 z = radius * SIN(a);
67 glVertex3f(x, y, z);
68
69 x = radius * COS(b) * COS(a + da);
70 y = radius * SIN(b) * COS(a + da);
71 z = radius * SIN(a + da);
72 glVertex3f(x, y, z);
73
74 color = 1 - color;
75 }
76 glEnd();
77
78 }
79
80 glEndList();
81
82 return list;
83 }
84
85 static void
86 reshape(int width, int height)
87 {
88 float aspect = (float) width / (float) height;
89 glViewport(0, 0, (GLint) width, (GLint) height);
90 glMatrixMode(GL_PROJECTION);
91 glLoadIdentity();
92 glOrtho(-6.0 * aspect, 6.0 * aspect, -6.0, 6.0, -6.0, 6.0);
93 glMatrixMode(GL_MODELVIEW);
94 }
95
96 /* ARGSUSED1 */
97 static void
98 key(unsigned char k, int x, int y)
99 {
100 switch (k) {
101 case 27: /* Escape */
102 exit(0);
103 }
104 }
105
106 static void
107 draw(void)
108 {
109 GLint i;
110
111 glClear(GL_COLOR_BUFFER_BIT);
112
113 glIndexi(CYAN);
114 glColor3f(0, 1, 1);
115 glBegin(GL_LINES);
116 for (i = -5; i <= 5; i++) {
117 glVertex2i(i, -5);
118 glVertex2i(i, 5);
119 }
120 for (i = -5; i <= 5; i++) {
121 glVertex2i(-5, i);
122 glVertex2i(5, i);
123 }
124 for (i = -5; i <= 5; i++) {
125 glVertex2i(i, -5);
126 glVertex2f(i * 1.15, -5.9);
127 }
128 glVertex2f(-5.3, -5.35);
129 glVertex2f(5.3, -5.35);
130 glVertex2f(-5.75, -5.9);
131 glVertex2f(5.75, -5.9);
132 glEnd();
133
134 glPushMatrix();
135 glTranslatef(Xpos, Ypos, 0.0);
136 glScalef(2.0, 2.0, 2.0);
137 glRotatef(8.0, 0.0, 0.0, 1.0);
138 glRotatef(90.0, 1.0, 0.0, 0.0);
139 glRotatef(Zrot, 0.0, 0.0, 1.0);
140
141 glCallList(Ball);
142
143 glPopMatrix();
144
145 glFlush();
146 glutSwapBuffers();
147 }
148
149 static void
150 idle(void)
151 {
152 static float vel0 = -100.0;
153
154 Zrot += Zstep;
155
156 Xpos += Xvel;
157 if (Xpos >= Xmax) {
158 Xpos = Xmax;
159 Xvel = -Xvel;
160 Zstep = -Zstep;
161 }
162 if (Xpos <= Xmin) {
163 Xpos = Xmin;
164 Xvel = -Xvel;
165 Zstep = -Zstep;
166 }
167 Ypos += Yvel;
168 Yvel += G;
169 if (Ypos < Ymin) {
170 Ypos = Ymin;
171 if (vel0 == -100.0)
172 vel0 = fabs(Yvel);
173 Yvel = vel0;
174 }
175 glutPostRedisplay();
176 }
177
178 static void
179 visible(int vis)
180 {
181 if (vis == GLUT_VISIBLE)
182 glutIdleFunc(idle);
183 else
184 glutIdleFunc(NULL);
185 }
186
187 int main(int argc, char *argv[])
188 {
189 glutInit(&argc, argv);
190 glutInitWindowPosition(0, 0);
191 glutInitWindowSize(600, 450);
192
193
194 IndexMode = argc > 1 && strcmp(argv[1], "-ci") == 0;
195 if (IndexMode)
196 glutInitDisplayMode(GLUT_INDEX | GLUT_DOUBLE);
197 else
198 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
199
200 glutCreateWindow("Bounce");
201 Ball = make_ball();
202 glCullFace(GL_BACK);
203 glEnable(GL_CULL_FACE);
204 glDisable(GL_DITHER);
205 glShadeModel(GL_FLAT);
206
207 glutDisplayFunc(draw);
208 glutReshapeFunc(reshape);
209 glutVisibilityFunc(visible);
210 glutKeyboardFunc(key);
211
212 if (IndexMode) {
213 glutSetColor(RED, 1.0, 0.0, 0.0);
214 glutSetColor(WHITE, 1.0, 1.0, 1.0);
215 glutSetColor(CYAN, 0.0, 1.0, 1.0);
216 }
217
218 glutMainLoop();
219 return 0; /* ANSI C requires main to return int. */
220 }