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