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