Xcode/TemplatesForXcodeTiger/SDL OpenGL Application/atlantis/atlantis.c
branchSDL-1.2
changeset 5895 402eac854030
child 7191 75360622e65f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Xcode/TemplatesForXcodeTiger/SDL OpenGL Application/atlantis/atlantis.c	Sun Sep 11 14:44:37 2011 -0400
     1.3 @@ -0,0 +1,459 @@
     1.4 +
     1.5 +/* Copyright (c) Mark J. Kilgard, 1994. */
     1.6 +
     1.7 +/**
     1.8 + * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
     1.9 + * ALL RIGHTS RESERVED
    1.10 + * Permission to use, copy, modify, and distribute this software for
    1.11 + * any purpose and without fee is hereby granted, provided that the above
    1.12 + * copyright notice appear in all copies and that both the copyright notice
    1.13 + * and this permission notice appear in supporting documentation, and that
    1.14 + * the name of Silicon Graphics, Inc. not be used in advertising
    1.15 + * or publicity pertaining to distribution of the software without specific,
    1.16 + * written prior permission.
    1.17 + *
    1.18 + * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
    1.19 + * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
    1.20 + * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
    1.21 + * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
    1.22 + * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
    1.23 + * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
    1.24 + * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
    1.25 + * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
    1.26 + * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
    1.27 + * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
    1.28 + * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
    1.29 + * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
    1.30 + *
    1.31 + * US Government Users Restricted Rights
    1.32 + * Use, duplication, or disclosure by the Government is subject to
    1.33 + * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
    1.34 + * (c)(1)(ii) of the Rights in Technical Data and Computer Software
    1.35 + * clause at DFARS 252.227-7013 and/or in similar or successor
    1.36 + * clauses in the FAR or the DOD or NASA FAR Supplement.
    1.37 + * Unpublished-- rights reserved under the copyright laws of the
    1.38 + * United States.  Contractor/manufacturer is Silicon Graphics,
    1.39 + * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
    1.40 + *
    1.41 + * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
    1.42 + */
    1.43 +#include <stdio.h>
    1.44 +#include <stdlib.h>
    1.45 +#include <string.h>
    1.46 +#include <math.h>
    1.47 +#include <sys/time.h>
    1.48 +#include <GLUT/glut.h>
    1.49 +#include "atlantis.h"
    1.50 +
    1.51 +fishRec sharks[NUM_SHARKS];
    1.52 +fishRec momWhale;
    1.53 +fishRec babyWhale;
    1.54 +fishRec dolph;
    1.55 +
    1.56 +GLboolean Timing = GL_TRUE;
    1.57 +
    1.58 +int w_win = 640;
    1.59 +int h_win = 480;
    1.60 +GLint count  = 0;
    1.61 +GLenum StrMode = GL_VENDOR;
    1.62 +
    1.63 +GLboolean moving;
    1.64 +
    1.65 +static double mtime(void)
    1.66 +{
    1.67 +   struct timeval tk_time;
    1.68 +   struct timezone tz;
    1.69 +   
    1.70 +   gettimeofday(&tk_time, &tz);
    1.71 +   
    1.72 +   return 4294.967296 * tk_time.tv_sec + 0.000001 * tk_time.tv_usec;
    1.73 +}
    1.74 +
    1.75 +static double filter(double in, double *save)
    1.76 +{
    1.77 +	static double k1 = 0.9;
    1.78 +	static double k2 = 0.05;
    1.79 +
    1.80 +	save[3] = in;
    1.81 +	save[1] = save[0]*k1 + k2*(save[3] + save[2]);
    1.82 +
    1.83 +	save[0]=save[1];
    1.84 +	save[2]=save[3];
    1.85 +
    1.86 +	return(save[1]);
    1.87 +}
    1.88 +
    1.89 +void DrawStr(const char *str)
    1.90 +{
    1.91 +	GLint i = 0;
    1.92 +	
    1.93 +	if(!str) return;
    1.94 +        
    1.95 +	while(str[i])
    1.96 +	{
    1.97 +		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, str[i]);
    1.98 +		i++;
    1.99 +	}
   1.100 +}
   1.101 +
   1.102 +void
   1.103 +InitFishs(void)
   1.104 +{
   1.105 +    int i;
   1.106 +
   1.107 +    for (i = 0; i < NUM_SHARKS; i++) {
   1.108 +        sharks[i].x = 70000.0 + rand() % 6000;
   1.109 +        sharks[i].y = rand() % 6000;
   1.110 +        sharks[i].z = rand() % 6000;
   1.111 +        sharks[i].psi = rand() % 360 - 180.0;
   1.112 +        sharks[i].v = 1.0;
   1.113 +    }
   1.114 +
   1.115 +    dolph.x = 30000.0;
   1.116 +    dolph.y = 0.0;
   1.117 +    dolph.z = 6000.0;
   1.118 +    dolph.psi = 90.0;
   1.119 +    dolph.theta = 0.0;
   1.120 +    dolph.v = 3.0;
   1.121 +
   1.122 +    momWhale.x = 70000.0;
   1.123 +    momWhale.y = 0.0;
   1.124 +    momWhale.z = 0.0;
   1.125 +    momWhale.psi = 90.0;
   1.126 +    momWhale.theta = 0.0;
   1.127 +    momWhale.v = 3.0;
   1.128 +
   1.129 +    babyWhale.x = 60000.0;
   1.130 +    babyWhale.y = -2000.0;
   1.131 +    babyWhale.z = -2000.0;
   1.132 +    babyWhale.psi = 90.0;
   1.133 +    babyWhale.theta = 0.0;
   1.134 +    babyWhale.v = 3.0;
   1.135 +}
   1.136 +
   1.137 +void
   1.138 +Atlantis_Init(void)
   1.139 +{
   1.140 +    static float ambient[] = {0.2, 0.2, 0.2, 1.0};
   1.141 +    static float diffuse[] = {1.0, 1.0, 1.0, 1.0};
   1.142 +    static float position[] = {0.0, 1.0, 0.0, 0.0};
   1.143 +    static float mat_shininess[] = {90.0};
   1.144 +    static float mat_specular[] = {0.8, 0.8, 0.8, 1.0};
   1.145 +    static float mat_diffuse[] = {0.46, 0.66, 0.795, 1.0};
   1.146 +    static float mat_ambient[] = {0.3, 0.4, 0.5, 1.0};
   1.147 +    static float lmodel_ambient[] = {0.4, 0.4, 0.4, 1.0};
   1.148 +    static float lmodel_localviewer[] = {0.0};
   1.149 +    //GLfloat map1[4] = {0.0, 0.0, 0.0, 0.0};
   1.150 +    //GLfloat map2[4] = {0.0, 0.0, 0.0, 0.0};
   1.151 +    static float fog_color[] = {0.0, 0.5, 0.9, 1.0};
   1.152 +
   1.153 +    glFrontFace(GL_CCW);
   1.154 +
   1.155 +    glDepthFunc(GL_LESS);
   1.156 +    glEnable(GL_DEPTH_TEST);
   1.157 +
   1.158 +    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
   1.159 +    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
   1.160 +    glLightfv(GL_LIGHT0, GL_POSITION, position);
   1.161 +    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
   1.162 +    glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_localviewer);
   1.163 +    glEnable(GL_LIGHTING);
   1.164 +    glEnable(GL_LIGHT0);
   1.165 +
   1.166 +    glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
   1.167 +    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
   1.168 +    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
   1.169 +    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
   1.170 +
   1.171 +    InitFishs();
   1.172 +
   1.173 +    glEnable(GL_FOG);
   1.174 +	glFogi(GL_FOG_MODE, GL_EXP);
   1.175 +	glFogf(GL_FOG_DENSITY, 0.0000025);
   1.176 +	glFogfv(GL_FOG_COLOR, fog_color);
   1.177 +
   1.178 +    glClearColor(0.0, 0.5, 0.9, 1.0);
   1.179 +}
   1.180 +
   1.181 +void
   1.182 +Atlantis_Reshape(int width, int height)
   1.183 +{
   1.184 +	w_win = width;
   1.185 +	h_win = height;
   1.186 +	
   1.187 +    glViewport(0, 0, width, height);
   1.188 +
   1.189 +    glMatrixMode(GL_PROJECTION);
   1.190 +    glLoadIdentity();
   1.191 +    gluPerspective(60.0, (GLfloat) width / (GLfloat) height, 20000.0, 300000.0);
   1.192 +    glMatrixMode(GL_MODELVIEW);
   1.193 +}
   1.194 +
   1.195 +void
   1.196 +Atlantis_Animate(void)
   1.197 +{
   1.198 +    int i;
   1.199 +
   1.200 +    for (i = 0; i < NUM_SHARKS; i++) {
   1.201 +        SharkPilot(&sharks[i]);
   1.202 +        SharkMiss(i);
   1.203 +    }
   1.204 +    WhalePilot(&dolph);
   1.205 +    dolph.phi++;
   1.206 +    //glutPostRedisplay();
   1.207 +    WhalePilot(&momWhale);
   1.208 +    momWhale.phi++;
   1.209 +    WhalePilot(&babyWhale);
   1.210 +    babyWhale.phi++;
   1.211 +}
   1.212 +
   1.213 +void
   1.214 +Atlantis_Key(unsigned char key, int x, int y)
   1.215 +{
   1.216 +    switch (key) {
   1.217 +    case 't':
   1.218 +    	Timing = !Timing;
   1.219 +    break;
   1.220 +    case ' ':
   1.221 +    	switch(StrMode)
   1.222 +    	{
   1.223 +		    case GL_EXTENSIONS:
   1.224 +    			StrMode = GL_VENDOR;
   1.225 +		    break;
   1.226 +		    case GL_VENDOR:
   1.227 +		    	StrMode = GL_RENDERER;
   1.228 +		    break;
   1.229 +		    case GL_RENDERER:
   1.230 +		    	StrMode = GL_VERSION;
   1.231 +		    break;
   1.232 +		    case GL_VERSION:
   1.233 +		    	StrMode = GL_EXTENSIONS;
   1.234 +		    break;
   1.235 +		}
   1.236 +	break;
   1.237 +    case 27:           /* Esc will quit */
   1.238 +        exit(1);
   1.239 +    break;
   1.240 +    case 's':             		/* "s" start animation */
   1.241 +        moving = GL_TRUE;
   1.242 +        //glutIdleFunc(Animate);
   1.243 +    break;
   1.244 +    case 'a':          			/* "a" stop animation */
   1.245 +        moving = GL_FALSE;
   1.246 +        //glutIdleFunc(NULL);
   1.247 +    break;
   1.248 +    case '.':          			/* "." will advance frame */
   1.249 +        if (!moving) {
   1.250 +            Atlantis_Animate();
   1.251 +        }
   1.252 +    }
   1.253 +}
   1.254 +/*
   1.255 +void Display(void)
   1.256 +{
   1.257 +	static float P123[3] = {-448.94, -203.14, 9499.60};
   1.258 +	static float P124[3] = {-442.64, -185.20, 9528.07};
   1.259 +	static float P125[3] = {-441.07, -148.05, 9528.07};
   1.260 +	static float P126[3] = {-443.43, -128.84, 9499.60};
   1.261 +	static float P127[3] = {-456.87, -146.78, 9466.67};
   1.262 +	static float P128[3] = {-453.68, -183.93, 9466.67};
   1.263 +
   1.264 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   1.265 +	
   1.266 +	glPushMatrix();
   1.267 +    FishTransform(&dolph);
   1.268 +    DrawDolphin(&dolph);
   1.269 +    glPopMatrix();
   1.270 + 
   1.271 +	glutSwapBuffers();
   1.272 +}
   1.273 +*/
   1.274 +
   1.275 +void
   1.276 +Atlantis_Display(void)
   1.277 +{
   1.278 +    int i;
   1.279 +    static double th[4] = {0.0, 0.0, 0.0, 0.0};
   1.280 +	static double t1 = 0.0, t2 = 0.0, t;
   1.281 +	char num_str[128];
   1.282 +    
   1.283 +    t1 = t2;
   1.284 +
   1.285 +    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   1.286 +
   1.287 +    for (i = 0; i < NUM_SHARKS; i++) {
   1.288 +        glPushMatrix();
   1.289 +        FishTransform(&sharks[i]);
   1.290 +        DrawShark(&sharks[i]);
   1.291 +        glPopMatrix();
   1.292 +    }
   1.293 +
   1.294 +    glPushMatrix();
   1.295 +    FishTransform(&dolph);
   1.296 +    DrawDolphin(&dolph);
   1.297 +    glPopMatrix();
   1.298 +
   1.299 +    glPushMatrix();
   1.300 +    FishTransform(&momWhale);
   1.301 +    DrawWhale(&momWhale);
   1.302 +    glPopMatrix();
   1.303 +
   1.304 +    glPushMatrix();
   1.305 +    FishTransform(&babyWhale);
   1.306 +    glScalef(0.45, 0.45, 0.3);
   1.307 +    DrawWhale(&babyWhale);
   1.308 +    glPopMatrix();
   1.309 +    
   1.310 +    if(Timing)
   1.311 +    {
   1.312 +		t2 = mtime();
   1.313 +		t = t2 - t1;
   1.314 +		if(t > 0.0001) t = 1.0 / t;
   1.315 +		
   1.316 +		glDisable(GL_LIGHTING);
   1.317 +		//glDisable(GL_DEPTH_TEST);
   1.318 +		
   1.319 +		glColor3f(1.0, 0.0, 0.0);
   1.320 +		
   1.321 +		glMatrixMode (GL_PROJECTION);
   1.322 +		glPushMatrix();
   1.323 +		glLoadIdentity();
   1.324 +		glOrtho(0, w_win, 0, h_win, -10.0, 10.0);
   1.325 +		
   1.326 +		glRasterPos2f(5.0, 5.0);
   1.327 +		
   1.328 +		switch(StrMode)
   1.329 +		{
   1.330 +			case GL_VENDOR:
   1.331 +				sprintf(num_str, "%0.2f Hz, %dx%d, VENDOR: ", filter(t, th), w_win, h_win);
   1.332 +				DrawStr(num_str);
   1.333 +				DrawStr(glGetString(GL_VENDOR));
   1.334 +			break;
   1.335 +			case GL_RENDERER:
   1.336 +				sprintf(num_str, "%0.2f Hz, %dx%d, RENDERER: ", filter(t, th), w_win, h_win);
   1.337 +				DrawStr(num_str);
   1.338 +				DrawStr(glGetString(GL_RENDERER));
   1.339 +			break;
   1.340 +			case GL_VERSION:
   1.341 +				sprintf(num_str, "%0.2f Hz, %dx%d, VERSION: ", filter(t, th), w_win, h_win);
   1.342 +				DrawStr(num_str);
   1.343 +				DrawStr(glGetString(GL_VERSION));
   1.344 +			break;
   1.345 +			case GL_EXTENSIONS:
   1.346 +				sprintf(num_str, "%0.2f Hz, %dx%d, EXTENSIONS: ", filter(t, th), w_win, h_win);
   1.347 +				DrawStr(num_str);
   1.348 +				DrawStr(glGetString(GL_EXTENSIONS));
   1.349 +			break;
   1.350 +		}
   1.351 +		
   1.352 +		glPopMatrix();
   1.353 +		glMatrixMode(GL_MODELVIEW);
   1.354 +		
   1.355 +		glEnable(GL_LIGHTING);
   1.356 +		//glEnable(GL_DEPTH_TEST);
   1.357 +	}
   1.358 +	
   1.359 +    count++;
   1.360 +
   1.361 +    glutSwapBuffers();
   1.362 +}
   1.363 +
   1.364 +/*
   1.365 +void
   1.366 +Visible(int state)
   1.367 +{
   1.368 +    if (state == GLUT_VISIBLE) {
   1.369 +        if (moving)
   1.370 +            glutIdleFunc(Animate);
   1.371 +    } else {
   1.372 +        if (moving)
   1.373 +            glutIdleFunc(NULL);
   1.374 +    }
   1.375 +}
   1.376 +
   1.377 +
   1.378 +void
   1.379 +timingSelect(int value)
   1.380 +{
   1.381 +    switch(value)
   1.382 +    {
   1.383 +		case 1:
   1.384 +			StrMode = GL_VENDOR;
   1.385 +		break;
   1.386 +		case 2:
   1.387 +			StrMode = GL_RENDERER;
   1.388 +		break;
   1.389 +		case 3:
   1.390 +			StrMode = GL_VERSION;
   1.391 +		break;
   1.392 +		case 4:
   1.393 +			StrMode = GL_EXTENSIONS;
   1.394 +		break;
   1.395 +    }
   1.396 +}
   1.397 +
   1.398 +void
   1.399 +menuSelect(int value)
   1.400 +{
   1.401 +    switch (value) {
   1.402 +    case 1:
   1.403 +        moving = GL_TRUE;
   1.404 +        glutIdleFunc(Animate);
   1.405 +        break;
   1.406 +    case 2:
   1.407 +        moving = GL_FALSE;
   1.408 +        glutIdleFunc(NULL);
   1.409 +        break;
   1.410 +    case 4:
   1.411 +        exit(0);
   1.412 +        break;
   1.413 +    }
   1.414 +}
   1.415 +
   1.416 +int
   1.417 +main(int argc, char **argv)
   1.418 +{
   1.419 +	GLboolean fullscreen = GL_FALSE; 
   1.420 +	GLint time_menu;
   1.421 + 	
   1.422 + 	srand(0);
   1.423 +
   1.424 +        glutInit(&argc, argv);
   1.425 +	if (argc > 1 && !strcmp(argv[1], "-w"))
   1.426 +		fullscreen = GL_FALSE;
   1.427 +
   1.428 +	//glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
   1.429 +	glutInitDisplayString("rgba double depth=24");
   1.430 +	if (fullscreen) {
   1.431 +	  glutGameModeString("1024x768:32");
   1.432 +	  glutEnterGameMode();
   1.433 +	} else {
   1.434 +	  glutInitWindowSize(320, 240);
   1.435 +	  glutCreateWindow("Atlantis Timing");
   1.436 +	}
   1.437 +    Init();
   1.438 +    glutDisplayFunc(Display);
   1.439 +    glutReshapeFunc(Reshape);
   1.440 +    glutKeyboardFunc(Key);
   1.441 +    moving = GL_TRUE;
   1.442 +glutIdleFunc(Animate);
   1.443 +    glutVisibilityFunc(Visible);
   1.444 +    
   1.445 +    time_menu = glutCreateMenu(timingSelect);
   1.446 +    glutAddMenuEntry("GL_VENDOR", 1);
   1.447 +    glutAddMenuEntry("GL_RENDERER", 2);
   1.448 +    glutAddMenuEntry("GL_VERSION", 3);
   1.449 +    glutAddMenuEntry("GL_EXTENSIONS", 4);
   1.450 +    
   1.451 +    glutCreateMenu(menuSelect);
   1.452 +    glutAddMenuEntry("Start motion", 1);
   1.453 +    glutAddMenuEntry("Stop motion", 2);
   1.454 +    glutAddSubMenu("Timing Mode", time_menu);
   1.455 +    glutAddMenuEntry("Quit", 4);
   1.456 +    
   1.457 +    //glutAttachMenu(GLUT_RIGHT_BUTTON);
   1.458 +    glutAttachMenu(GLUT_RIGHT_BUTTON);
   1.459 +    glutMainLoop();
   1.460 +    return 0;             // ANSI C requires main to return int.
   1.461 +}
   1.462 +*/
   1.463 \ No newline at end of file