1 /**************************************************************************
2 * GLmain.c -- top level sim routines
4 * Written by Curtis Olson for OpenGL, started May 1997.
6 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * (Log is kept at end of this file)
24 **************************************************************************/
30 #include <signal.h> /* for timer routines */
31 #include <sys/time.h> /* for timer routines */
37 /* assumes -I/usr/include/mesa in compile command */
42 #include "../aircraft/aircraft.h"
43 #include "../scenery/scenery.h"
45 /* This is a record containing all the info for the aircraft currently
47 struct aircraft_params current_aircraft;
50 static GLfloat win_ratio = 1.0;
53 extern struct mesh *mesh_ptr;
54 /* Function prototypes */
55 GLint fgSceneryCompile();
56 static void fgSceneryDraw();
57 /* pointer to terrain mesh structure */
61 double fogDensity = 2000.0;
64 #define DEFAULT_MODEL_HZ 20
70 /**************************************************************************
71 * fgInitVisuals() -- Initialize various GL/view parameters
72 **************************************************************************/
74 static void fgInitVisuals() {
75 /* if the 4th field is 0.0, this specifies a direction ... */
76 static GLfloat sun_vec[4] = {3.0, 1.0, 3.0, 0.0 };
77 static GLfloat color[4] = { 0.3, 0.7, 0.2, 1.0 };
78 static GLfloat fogColor[4] = {0.65, 0.65, 0.85, 1.0};
80 glEnable( GL_DEPTH_TEST );
82 glEnable( GL_CULL_FACE );
84 /* If enabled, normal vectors specified with glNormal are scaled
85 to unit length after transformation. See glNormal. */
86 glEnable( GL_NORMALIZE );
88 glLightfv( GL_LIGHT0, GL_POSITION, sun_vec );
89 glEnable( GL_LIGHTING );
90 glEnable( GL_LIGHT0 );
92 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color );
93 glShadeModel( GL_FLAT ); /* glShadeModel( GL_SMOOTH ); */
96 glFogi (GL_FOG_MODE, GL_LINEAR);
97 /* glFogf (GL_FOG_START, 1.0); */
98 glFogf (GL_FOG_END, fogDensity);
99 glFogfv (GL_FOG_COLOR, fogColor);
100 /* glFogf (GL_FOG_DENSITY, fogDensity); */
101 /* glHint (GL_FOG_HINT, GL_FASTEST); */
103 glClearColor(0.6, 0.6, 0.9, 1.0);
107 /**************************************************************************
108 * Update the view volume, position, and orientation
109 **************************************************************************/
111 static void fgUpdateViewParams() {
112 struct flight_params *f;
114 f = ¤t_aircraft.flight;
116 /* Tell GL we are about to modify the projection parameters */
117 glMatrixMode(GL_PROJECTION);
119 gluPerspective(45.0, 1.0/win_ratio, 1.0, 6000.0);
121 glMatrixMode(GL_MODELVIEW);
123 gluLookAt(f->pos_x, f->pos_y, f->pos_z,
124 f->pos_x + cos(f->Psi), f->pos_y + sin(f->Psi), f->pos_z,
129 /**************************************************************************
130 * Update all Visuals (redraws anything graphics related)
131 **************************************************************************/
133 static void fgUpdateVisuals( void ) {
134 /* update view volume parameters */
135 fgUpdateViewParams();
137 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
139 /* Tell GL we are switching to model view parameters */
140 glMatrixMode(GL_MODELVIEW);
141 /* glLoadIdentity(); */
143 /* draw terrain mesh */
154 /**************************************************************************
155 * Timer management routines
156 **************************************************************************/
158 static struct itimerval t, ot;
160 /* This routine catches the SIGALRM */
161 void fgTimerCatch() {
162 static double lastSimtime = -99.9;
164 /* printf("In fgTimerCatch()\n"); */
166 Overrun = (lastSimtime == Simtime);
169 printf("OVERRUN!!!\n");
172 lastSimtime = Simtime;
173 signal(SIGALRM, fgTimerCatch);
176 /* this routine initializes the interval timer to generate a SIGALRM after
177 * the specified interval (dt) */
178 void fgTimerInit(float dt) {
184 usec = 1000000* (dt - (float) isec);
186 t.it_interval.tv_sec = isec;
187 t.it_interval.tv_usec = usec;
188 t.it_value.tv_sec = isec;
189 t.it_value.tv_usec = usec;
190 /* printf("fgTimerInit() called\n"); */
191 fgTimerCatch(); /* set up for SIGALRM signal catch */
192 terr = setitimer( ITIMER_REAL, &t, &ot );
193 if (terr) perror("Error returned from setitimer");
197 /**************************************************************************
198 * Scenery management routines
199 **************************************************************************/
201 static void fgSceneryInit() {
202 /* make terrain mesh */
203 mesh = fgSceneryCompile();
207 /* create the terrain mesh */
208 GLint fgSceneryCompile() {
211 mesh = mesh2GL(mesh_ptr);
217 /* draw the terrain mesh */
218 static void fgSceneryDraw() {
223 /* What should we do when we have nothing else to do? How about get
224 * ready for the next move?*/
225 static void fgMainLoop( void )
234 /**************************************************************************
235 * Handle new window size or exposure
236 **************************************************************************/
238 static void fgReshape( int width, int height ) {
239 /* Do this so we can call fgReshape(0,0) ourselves without having to know
240 * what the values of width & height are. */
241 if ( (height > 0) && (width > 0) ) {
242 win_ratio = (GLfloat) height / (GLfloat) width;
245 /* Inform gl of our view window size */
246 glViewport(0, 0, (GLint)width, (GLint)height);
248 fgUpdateViewParams();
250 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
254 /**************************************************************************
256 **************************************************************************/
258 int main( int argc, char *argv[] ) {
259 /* parse the scenery file */
260 parse_scenery(argv[1]);
263 /* initialize GLUT */
264 glutInit(&argc, argv);
266 /* Define Display Parameters */
267 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
269 /* Define initial window size */
270 glutInitWindowSize(640, 400);
272 /* Initialize the main window */
273 glutCreateWindow("Terrain Demo");
275 /* Define initial window size */
276 tkInitPosition(0, 0, 640, 400);
278 /* Define Display Parameters */
279 tkInitDisplayMode( TK_RGB | TK_DEPTH | TK_DOUBLE | TK_DIRECT );
281 /* Initialize the main window */
282 if (tkInitWindow("Terrain Demo") == GL_FALSE) {
287 /* setup view parameters, only makes GL calls */
290 /* Set initial position and slew parameters */
291 /* fgSlewInit(-398391.3, 120070.4, 244, 3.1415); */ /* GLOBE Airport */
292 /* fgSlewInit(-335340,162540, 15, 4.38); */
293 fgSlewInit(-398673.28,120625.64, 53, 4.38);
295 /* build all objects */
298 /* initialize timer */
299 fgTimerInit( 1.0 / DEFAULT_MODEL_HZ );
302 /* call fgReshape() on window resizes */
303 glutReshapeFunc( fgReshape );
305 /* call key() on keyboard event */
306 glutKeyboardFunc( GLUTkey );
307 glutSpecialFunc( GLUTkey );
309 /* call fgMainLoop() whenever there is nothing else to do */
310 glutIdleFunc( fgMainLoop );
313 glutDisplayFunc( fgUpdateVisuals );
315 /* pass control off to the GLUT event handler */
318 /* call fgReshape() on expose events */
319 tkExposeFunc( fgReshape );
321 /* call fgReshape() on window resizes */
322 tkReshapeFunc( fgReshape );
324 /* call key() on keyboard event */
325 tkKeyDownFunc( GLTKkey );
327 /* call fgMainLoop() whenever there is nothing else to do */
328 tkIdleFunc( fgMainLoop );
331 tkDisplayFunc( fgUpdateVisuals );
333 /* pass control off to the tk event handler */
342 /* Revision 1.5 1997/05/29 02:33:23 curt
343 /* Updated to reflect changing interfaces in other "modules."
345 * Revision 1.4 1997/05/27 17:44:31 curt
346 * Renamed & rearranged variables and routines. Added some initial simple
347 * timer/alarm routines so the flight model can be updated on a regular
350 * Revision 1.3 1997/05/23 15:40:25 curt
351 * Added GNU copyright headers.
354 * Revision 1.2 1997/05/23 00:35:12 curt
355 * Trying to get fog to work ...
357 * Revision 1.1 1997/05/21 15:57:51 curt
358 * Renamed due to added GLUT support.
360 * Revision 1.3 1997/05/19 18:22:42 curt
361 * Parameter tweaking ... starting to stub in fog support.
363 * Revision 1.2 1997/05/17 00:17:34 curt
364 * Trying to stub in support for standard OpenGL.
366 * Revision 1.1 1997/05/16 16:05:52 curt