adjust to match the uname changes.
[AROS-Contrib.git] / Demo / Mesa / bounce.c
blobc30ffea0be56d9f00dd550ebbf13215fe89fce96
2 /*
3 * Bouncing ball demo.
5 * This program is in the public domain
7 * Brian Paul
9 * Conversion to GLUT by Mark J. Kilgard
13 #include <math.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <GL/glut.h>
18 #define COS(X) cos( (X) * 3.14159/180.0 )
19 #define SIN(X) sin( (X) * 3.14159/180.0 )
21 #define RED 1
22 #define WHITE 2
23 #define VIOLET 3
25 GLboolean IndexMode = GL_FALSE;
26 GLuint Ball;
27 GLenum Mode;
28 GLfloat Zrot = 0.0, Zstep = 180.0;
29 GLfloat Xpos = 0.0, Ypos = 1.0;
30 GLfloat Xvel = 2.0, Yvel = 0.0;
31 GLfloat Xmin = -4.0, Xmax = 4.0;
32 GLfloat Ymin = -3.8, Ymax = 4.0;
33 GLfloat G = -9.8;
35 static GLuint
36 make_ball(void)
38 GLuint list;
39 GLfloat a, b;
40 GLfloat da = 22.5, db = 22.5;
41 GLfloat radius = 1.0;
42 GLuint color;
43 GLfloat x, y, z;
45 list = glGenLists(1);
47 glNewList(list, GL_COMPILE);
49 color = 0;
50 for (a = -90.0; a + da <= 90.0; a += da) {
52 glBegin(GL_QUAD_STRIP);
53 for (b = 0.0; b <= 360.0; b += db) {
55 if (color) {
56 glIndexi(RED);
57 glColor3f(1, 0, 0);
58 } else {
59 glIndexi(WHITE);
60 glColor3f(1, 1, 1);
63 x = radius * COS(b) * COS(a);
64 y = radius * SIN(b) * COS(a);
65 z = radius * SIN(a);
66 glVertex3f(x, y, z);
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);
73 color = 1 - color;
75 glEnd();
79 glEndList();
81 return list;
84 static void
85 reshape(int width, int height)
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);
95 /* ARGSUSED1 */
96 static void
97 key(unsigned char k, int x, int y)
99 switch (k) {
100 case 27: /* Escape */
101 exit(0);
105 static void
106 draw(void)
108 GLint i;
110 glClearColor(0.6f, 0.6f, 0.6f, 1.0f);
111 glClear(GL_COLOR_BUFFER_BIT);
113 glIndexi(VIOLET);
114 glColor3f(0.7, 0.45, 0.7);
115 glLineWidth(2.5);
116 glBegin(GL_LINES);
117 for (i = -5; i <= 5; i++) {
118 glVertex2i(i, -5);
119 glVertex2i(i, 5);
121 for (i = -5; i <= 5; i++) {
122 glVertex2i(-5, i);
123 glVertex2i(5, i);
125 for (i = -5; i <= 5; i++) {
126 glVertex2i(i, -5);
127 glVertex2f(i * 1.15, -5.9);
129 glVertex2f(-5.3, -5.35);
130 glVertex2f(5.3, -5.35);
131 glVertex2f(-5.75, -5.9);
132 glVertex2f(5.75, -5.9);
133 glEnd();
134 glLineWidth(1.0);
136 glPushMatrix();
137 glTranslatef(Xpos, Ypos, 0.0);
138 glScalef(2.0, 2.0, 2.0);
139 glRotatef(-20.0, 0.0, 0.0, 1.0);
140 glRotatef(90.0, 1.0, 0.0, 0.0);
141 glRotatef(Zrot, 0.0, 0.0, 1.0);
143 glCallList(Ball);
145 glPopMatrix();
147 glFlush();
148 glutSwapBuffers();
152 static void
153 idle(void)
155 static float vel0 = -100.0;
156 static double t0 = -1.;
157 double t, dt;
158 t = glutGet(GLUT_ELAPSED_TIME) / 1000.;
159 if (t0 < 0.)
160 t0 = t;
161 dt = t - t0;
162 t0 = t;
164 Zrot -= Zstep*dt;
166 Xpos += Xvel*dt;
167 if (Xpos >= Xmax) {
168 Xpos = Xmax;
169 Xvel = -Xvel;
170 Zstep = -Zstep;
172 if (Xpos <= Xmin) {
173 Xpos = Xmin;
174 Xvel = -Xvel;
175 Zstep = -Zstep;
177 Ypos += Yvel*dt;
178 Yvel += G*dt;
179 if (Ypos < Ymin) {
180 Ypos = Ymin;
181 if (vel0 == -100.0)
182 vel0 = fabs(Yvel);
183 Yvel = vel0;
185 glutPostRedisplay();
188 static void
189 visible(int vis)
191 if (vis == GLUT_VISIBLE)
192 glutIdleFunc(idle);
193 else
194 glutIdleFunc(NULL);
197 #include <proto/exec.h>
198 #include <proto/dos.h>
200 #define ARG_TEMPLATE "WINPOSX=X/N/K,WINPOSY=Y/N/K"
201 #define ARG_X 0
202 #define ARG_Y 1
203 #define NUM_ARGS 2
205 static IPTR args[NUM_ARGS];
206 WORD winx = -1, winy = -1;
207 #define W 320
208 #define H 200
210 static void correctpos(void)
212 if (winx == -1) winx = 100;
213 if (winy == -1) winy = 100;
216 static void getarguments(void)
218 struct RDArgs *myargs;
220 if ((myargs = ReadArgs(ARG_TEMPLATE, args, NULL)))
223 if (args[ARG_X]) winx = *(IPTR *)args[ARG_X];
224 if (args[ARG_Y]) winy = *(IPTR *)args[ARG_Y];
226 FreeArgs(myargs);
231 int main(int argc, char *argv[])
233 getarguments();
234 correctpos();
236 glutInit(&argc, argv);
237 glutInitWindowPosition(winx, winy);
238 glutInitWindowSize(W, H);
241 IndexMode = argc > 1 && strcmp(argv[1], "-ci") == 0;
242 if (IndexMode)
243 glutInitDisplayMode(GLUT_INDEX | GLUT_DOUBLE);
244 else
245 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
247 glutCreateWindow("Bounce");
248 Ball = make_ball();
249 glCullFace(GL_BACK);
250 glEnable(GL_CULL_FACE);
251 glDisable(GL_DITHER);
252 glShadeModel(GL_FLAT);
254 glutDisplayFunc(draw);
255 glutReshapeFunc(reshape);
256 glutVisibilityFunc(visible);
257 glutKeyboardFunc(key);
259 if (IndexMode) {
260 glutSetColor(RED, 1.0, 0.0, 0.0);
261 glutSetColor(WHITE, 1.0, 1.0, 1.0);
262 glutSetColor(VIOLET, 0.7, 0.45, 0.7);
265 glutMainLoop();
266 return 0; /* ANSI C requires main to return int. */