comparison Xcode/TemplatesForXcodeTiger/SDL OpenGL Application/atlantis/atlantis.c @ 3329:232e5e00e398

Added missing templates
author Sam Lantinga <slouken@libsdl.org>
date Tue, 29 Sep 2009 07:59:23 +0000
parents
children
comparison
equal deleted inserted replaced
3328:7da18fe4cdd9 3329:232e5e00e398
1
2 /* Copyright (c) Mark J. Kilgard, 1994. */
3
4 /**
5 * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
6 * ALL RIGHTS RESERVED
7 * Permission to use, copy, modify, and distribute this software for
8 * any purpose and without fee is hereby granted, provided that the above
9 * copyright notice appear in all copies and that both the copyright notice
10 * and this permission notice appear in supporting documentation, and that
11 * the name of Silicon Graphics, Inc. not be used in advertising
12 * or publicity pertaining to distribution of the software without specific,
13 * written prior permission.
14 *
15 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
16 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
18 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
20 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
21 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
22 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
23 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
24 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
26 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
27 *
28 * US Government Users Restricted Rights
29 * Use, duplication, or disclosure by the Government is subject to
30 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
31 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
32 * clause at DFARS 252.227-7013 and/or in similar or successor
33 * clauses in the FAR or the DOD or NASA FAR Supplement.
34 * Unpublished-- rights reserved under the copyright laws of the
35 * United States. Contractor/manufacturer is Silicon Graphics,
36 * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
37 *
38 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
39 */
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <math.h>
44 #include <sys/time.h>
45 #include <GLUT/glut.h>
46 #include "atlantis.h"
47
48 fishRec sharks[NUM_SHARKS];
49 fishRec momWhale;
50 fishRec babyWhale;
51 fishRec dolph;
52
53 GLboolean Timing = GL_TRUE;
54
55 int w_win = 640;
56 int h_win = 480;
57 GLint count = 0;
58 GLenum StrMode = GL_VENDOR;
59
60 GLboolean moving;
61
62 static double mtime(void)
63 {
64 struct timeval tk_time;
65 struct timezone tz;
66
67 gettimeofday(&tk_time, &tz);
68
69 return 4294.967296 * tk_time.tv_sec + 0.000001 * tk_time.tv_usec;
70 }
71
72 static double filter(double in, double *save)
73 {
74 static double k1 = 0.9;
75 static double k2 = 0.05;
76
77 save[3] = in;
78 save[1] = save[0]*k1 + k2*(save[3] + save[2]);
79
80 save[0]=save[1];
81 save[2]=save[3];
82
83 return(save[1]);
84 }
85
86 void DrawStr(const char *str)
87 {
88 GLint i = 0;
89
90 if(!str) return;
91
92 while(str[i])
93 {
94 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, str[i]);
95 i++;
96 }
97 }
98
99 void
100 InitFishs(void)
101 {
102 int i;
103
104 for (i = 0; i < NUM_SHARKS; i++) {
105 sharks[i].x = 70000.0 + rand() % 6000;
106 sharks[i].y = rand() % 6000;
107 sharks[i].z = rand() % 6000;
108 sharks[i].psi = rand() % 360 - 180.0;
109 sharks[i].v = 1.0;
110 }
111
112 dolph.x = 30000.0;
113 dolph.y = 0.0;
114 dolph.z = 6000.0;
115 dolph.psi = 90.0;
116 dolph.theta = 0.0;
117 dolph.v = 3.0;
118
119 momWhale.x = 70000.0;
120 momWhale.y = 0.0;
121 momWhale.z = 0.0;
122 momWhale.psi = 90.0;
123 momWhale.theta = 0.0;
124 momWhale.v = 3.0;
125
126 babyWhale.x = 60000.0;
127 babyWhale.y = -2000.0;
128 babyWhale.z = -2000.0;
129 babyWhale.psi = 90.0;
130 babyWhale.theta = 0.0;
131 babyWhale.v = 3.0;
132 }
133
134 void
135 Atlantis_Init(void)
136 {
137 static float ambient[] = {0.2, 0.2, 0.2, 1.0};
138 static float diffuse[] = {1.0, 1.0, 1.0, 1.0};
139 static float position[] = {0.0, 1.0, 0.0, 0.0};
140 static float mat_shininess[] = {90.0};
141 static float mat_specular[] = {0.8, 0.8, 0.8, 1.0};
142 static float mat_diffuse[] = {0.46, 0.66, 0.795, 1.0};
143 static float mat_ambient[] = {0.3, 0.4, 0.5, 1.0};
144 static float lmodel_ambient[] = {0.4, 0.4, 0.4, 1.0};
145 static float lmodel_localviewer[] = {0.0};
146 //GLfloat map1[4] = {0.0, 0.0, 0.0, 0.0};
147 //GLfloat map2[4] = {0.0, 0.0, 0.0, 0.0};
148 static float fog_color[] = {0.0, 0.5, 0.9, 1.0};
149
150 glFrontFace(GL_CCW);
151
152 glDepthFunc(GL_LESS);
153 glEnable(GL_DEPTH_TEST);
154
155 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
156 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
157 glLightfv(GL_LIGHT0, GL_POSITION, position);
158 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
159 glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_localviewer);
160 glEnable(GL_LIGHTING);
161 glEnable(GL_LIGHT0);
162
163 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
164 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
165 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
166 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
167
168 InitFishs();
169
170 glEnable(GL_FOG);
171 glFogi(GL_FOG_MODE, GL_EXP);
172 glFogf(GL_FOG_DENSITY, 0.0000025);
173 glFogfv(GL_FOG_COLOR, fog_color);
174
175 glClearColor(0.0, 0.5, 0.9, 1.0);
176 }
177
178 void
179 Atlantis_Reshape(int width, int height)
180 {
181 w_win = width;
182 h_win = height;
183
184 glViewport(0, 0, width, height);
185
186 glMatrixMode(GL_PROJECTION);
187 glLoadIdentity();
188 gluPerspective(60.0, (GLfloat) width / (GLfloat) height, 20000.0, 300000.0);
189 glMatrixMode(GL_MODELVIEW);
190 }
191
192 void
193 Atlantis_Animate(void)
194 {
195 int i;
196
197 for (i = 0; i < NUM_SHARKS; i++) {
198 SharkPilot(&sharks[i]);
199 SharkMiss(i);
200 }
201 WhalePilot(&dolph);
202 dolph.phi++;
203 //glutPostRedisplay();
204 WhalePilot(&momWhale);
205 momWhale.phi++;
206 WhalePilot(&babyWhale);
207 babyWhale.phi++;
208 }
209
210 void
211 Atlantis_Key(unsigned char key, int x, int y)
212 {
213 switch (key) {
214 case 't':
215 Timing = !Timing;
216 break;
217 case ' ':
218 switch(StrMode)
219 {
220 case GL_EXTENSIONS:
221 StrMode = GL_VENDOR;
222 break;
223 case GL_VENDOR:
224 StrMode = GL_RENDERER;
225 break;
226 case GL_RENDERER:
227 StrMode = GL_VERSION;
228 break;
229 case GL_VERSION:
230 StrMode = GL_EXTENSIONS;
231 break;
232 }
233 break;
234 case 27: /* Esc will quit */
235 exit(1);
236 break;
237 case 's': /* "s" start animation */
238 moving = GL_TRUE;
239 //glutIdleFunc(Animate);
240 break;
241 case 'a': /* "a" stop animation */
242 moving = GL_FALSE;
243 //glutIdleFunc(NULL);
244 break;
245 case '.': /* "." will advance frame */
246 if (!moving) {
247 Atlantis_Animate();
248 }
249 }
250 }
251 /*
252 void Display(void)
253 {
254 static float P123[3] = {-448.94, -203.14, 9499.60};
255 static float P124[3] = {-442.64, -185.20, 9528.07};
256 static float P125[3] = {-441.07, -148.05, 9528.07};
257 static float P126[3] = {-443.43, -128.84, 9499.60};
258 static float P127[3] = {-456.87, -146.78, 9466.67};
259 static float P128[3] = {-453.68, -183.93, 9466.67};
260
261 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
262
263 glPushMatrix();
264 FishTransform(&dolph);
265 DrawDolphin(&dolph);
266 glPopMatrix();
267
268 glutSwapBuffers();
269 }
270 */
271
272 void
273 Atlantis_Display(void)
274 {
275 int i;
276 static double th[4] = {0.0, 0.0, 0.0, 0.0};
277 static double t1 = 0.0, t2 = 0.0, t;
278 char num_str[128];
279
280 t1 = t2;
281
282 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
283
284 for (i = 0; i < NUM_SHARKS; i++) {
285 glPushMatrix();
286 FishTransform(&sharks[i]);
287 DrawShark(&sharks[i]);
288 glPopMatrix();
289 }
290
291 glPushMatrix();
292 FishTransform(&dolph);
293 DrawDolphin(&dolph);
294 glPopMatrix();
295
296 glPushMatrix();
297 FishTransform(&momWhale);
298 DrawWhale(&momWhale);
299 glPopMatrix();
300
301 glPushMatrix();
302 FishTransform(&babyWhale);
303 glScalef(0.45, 0.45, 0.3);
304 DrawWhale(&babyWhale);
305 glPopMatrix();
306
307 if(Timing)
308 {
309 t2 = mtime();
310 t = t2 - t1;
311 if(t > 0.0001) t = 1.0 / t;
312
313 glDisable(GL_LIGHTING);
314 //glDisable(GL_DEPTH_TEST);
315
316 glColor3f(1.0, 0.0, 0.0);
317
318 glMatrixMode (GL_PROJECTION);
319 glPushMatrix();
320 glLoadIdentity();
321 glOrtho(0, w_win, 0, h_win, -10.0, 10.0);
322
323 glRasterPos2f(5.0, 5.0);
324
325 switch(StrMode)
326 {
327 case GL_VENDOR:
328 sprintf(num_str, "%0.2f Hz, %dx%d, VENDOR: ", filter(t, th), w_win, h_win);
329 DrawStr(num_str);
330 DrawStr(glGetString(GL_VENDOR));
331 break;
332 case GL_RENDERER:
333 sprintf(num_str, "%0.2f Hz, %dx%d, RENDERER: ", filter(t, th), w_win, h_win);
334 DrawStr(num_str);
335 DrawStr(glGetString(GL_RENDERER));
336 break;
337 case GL_VERSION:
338 sprintf(num_str, "%0.2f Hz, %dx%d, VERSION: ", filter(t, th), w_win, h_win);
339 DrawStr(num_str);
340 DrawStr(glGetString(GL_VERSION));
341 break;
342 case GL_EXTENSIONS:
343 sprintf(num_str, "%0.2f Hz, %dx%d, EXTENSIONS: ", filter(t, th), w_win, h_win);
344 DrawStr(num_str);
345 DrawStr(glGetString(GL_EXTENSIONS));
346 break;
347 }
348
349 glPopMatrix();
350 glMatrixMode(GL_MODELVIEW);
351
352 glEnable(GL_LIGHTING);
353 //glEnable(GL_DEPTH_TEST);
354 }
355
356 count++;
357
358 glutSwapBuffers();
359 }
360
361 /*
362 void
363 Visible(int state)
364 {
365 if (state == GLUT_VISIBLE) {
366 if (moving)
367 glutIdleFunc(Animate);
368 } else {
369 if (moving)
370 glutIdleFunc(NULL);
371 }
372 }
373
374
375 void
376 timingSelect(int value)
377 {
378 switch(value)
379 {
380 case 1:
381 StrMode = GL_VENDOR;
382 break;
383 case 2:
384 StrMode = GL_RENDERER;
385 break;
386 case 3:
387 StrMode = GL_VERSION;
388 break;
389 case 4:
390 StrMode = GL_EXTENSIONS;
391 break;
392 }
393 }
394
395 void
396 menuSelect(int value)
397 {
398 switch (value) {
399 case 1:
400 moving = GL_TRUE;
401 glutIdleFunc(Animate);
402 break;
403 case 2:
404 moving = GL_FALSE;
405 glutIdleFunc(NULL);
406 break;
407 case 4:
408 exit(0);
409 break;
410 }
411 }
412
413 int
414 main(int argc, char **argv)
415 {
416 GLboolean fullscreen = GL_FALSE;
417 GLint time_menu;
418
419 srand(0);
420
421 glutInit(&argc, argv);
422 if (argc > 1 && !strcmp(argv[1], "-w"))
423 fullscreen = GL_FALSE;
424
425 //glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
426 glutInitDisplayString("rgba double depth=24");
427 if (fullscreen) {
428 glutGameModeString("1024x768:32");
429 glutEnterGameMode();
430 } else {
431 glutInitWindowSize(320, 240);
432 glutCreateWindow("Atlantis Timing");
433 }
434 Init();
435 glutDisplayFunc(Display);
436 glutReshapeFunc(Reshape);
437 glutKeyboardFunc(Key);
438 moving = GL_TRUE;
439 glutIdleFunc(Animate);
440 glutVisibilityFunc(Visible);
441
442 time_menu = glutCreateMenu(timingSelect);
443 glutAddMenuEntry("GL_VENDOR", 1);
444 glutAddMenuEntry("GL_RENDERER", 2);
445 glutAddMenuEntry("GL_VERSION", 3);
446 glutAddMenuEntry("GL_EXTENSIONS", 4);
447
448 glutCreateMenu(menuSelect);
449 glutAddMenuEntry("Start motion", 1);
450 glutAddMenuEntry("Stop motion", 2);
451 glutAddSubMenu("Timing Mode", time_menu);
452 glutAddMenuEntry("Quit", 4);
453
454 //glutAttachMenu(GLUT_RIGHT_BUTTON);
455 glutAttachMenu(GLUT_RIGHT_BUTTON);
456 glutMainLoop();
457 return 0; // ANSI C requires main to return int.
458 }
459 */