/* -*- Objc -*- */ /* * $Id: Main.m,v 1.1 2003/09/03 22:36:18 dam Exp $ * * Copyright (C) 2003 Free Software Foundation, Inc. * * This file is part of GNU Hégémonie. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #include #include "Common/Camera.h" #include "Common/Testing.h" static Camera *_camera; static BOOL _fillMode = YES; static NSDate *_date; static unsigned _framesCount; static void _display (void) { if (-[_date timeIntervalSinceNow] >= 1.0) { NSString *title = [NSString stringWithFormat: @"%.1f fps", (double)_framesCount / -[_date timeIntervalSinceNow]]; glutSetWindowTitle ([title cString]); _framesCount = 0; ASSIGN(_date, [[NSDate alloc] init]); } _framesCount++; glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity (); gluLookAt ((GLdouble)[_camera eye].x, (GLdouble)[_camera eye].y, (GLdouble)[_camera eye].z, (GLdouble)[_camera center].x, (GLdouble)[_camera center].y, (GLdouble)[_camera center].z, (GLdouble)[_camera up].x, (GLdouble)[_camera up].y, (GLdouble)[_camera up].z); testing_display (_camera); glutSwapBuffers (); glutPostRedisplay (); } static void _reshape (const int width, const int height) { glViewport (0, 0, (GLsizei)width, (GLsizei)height); glMatrixMode (GL_PROJECTION); glLoadIdentity (); [_camera setAspect: (double)width / (double)height]; gluPerspective ((GLdouble)[_camera fovy], (GLdouble)[_camera aspect], (GLdouble)[_camera zNear], (GLdouble)[_camera zFar]); glMatrixMode (GL_MODELVIEW); } static void _special (int key, int x, int y) { switch (key) { case GLUT_KEY_LEFT: [_camera strafLeft: 3.0f]; break; case GLUT_KEY_UP: [_camera forward: 3.0f]; break; case GLUT_KEY_RIGHT: [_camera strafRight: 3.0f]; break; case GLUT_KEY_DOWN: [_camera backward: 3.0f]; break; case GLUT_KEY_PAGE_UP: [_camera moveUp: 3.0f]; break; case GLUT_KEY_PAGE_DOWN: [_camera moveDown: 3.0f]; break; default: testing_special (key, x, y); break; } glutPostRedisplay(); } static void _keyboard (unsigned char key, int x, int y) { switch (key) { case 27: /* ESC KEY */ exit (EXIT_SUCCESS); break; case 'h': [_camera rotateHeading: 2.0]; break; case 'k': [_camera rotateHeading: -2.0]; break; case 'u': [_camera rotatePitch: 1.0]; break; case 'j': [_camera rotatePitch: -1.0]; break; case 'y': [_camera rotateRoll: -1.0]; break; case 'i': [_camera rotateRoll: 1.0]; break; case 'o': [_camera rotateAroundViewHeading: 1.0]; break; case 't': [_camera rotateAroundViewHeading: -1.0]; break; case 'f': _fillMode = !_fillMode; glPolygonMode (GL_FRONT_AND_BACK, (_fillMode ? GL_FILL : GL_LINE)); default: testing_keyboard (key, x, y); break; } glutPostRedisplay(); } static void _initGL (void) { const GLfloat lightAmbient[] = {0.75f, 0.75f, 0.75f, 1.0f}; const GLfloat lightDiffuse[] = {0.7f, 0.7f, 0.7f, 1.0f}; // const GLfloat lightPosition[] = {10.0f, 350.0f, 10.0f}; glEnable (GL_LIGHT0); glLightfv (GL_LIGHT0, GL_AMBIENT, lightAmbient); glLightfv (GL_LIGHT0, GL_DIFFUSE, lightDiffuse); glShadeModel (GL_SMOOTH); } static void _init (void) { _camera = [[Camera alloc] initWithFovy: 50 aspect: 1024.0f / 768.0f zNear: 0.01 zFar: 750.0 eye: MakeCoord (0.0, 197.0, 0.0) center: MakeCoord (0.0, 197.0, 30.0) up: MakeCoord (0.0, 1.0, 0.0)]; testing_init (); _date = [[NSDate alloc] init]; _framesCount = 0; } int main (int argc, char **argv) { CREATE_AUTORELEASE_POOL(pool); _init (); glutInit (&argc, argv); glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize (1024, 768); glutCreateWindow ([[[NSProcessInfo processInfo] processName] cString]); _initGL (); glutDisplayFunc (_display); glutReshapeFunc (_reshape); glutSpecialFunc (_special); glutKeyboardFunc (_keyboard); glutMainLoop(); RELEASE(pool); return EXIT_SUCCESS; }