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);
168 /* add this back in when you get simtime working */
170 printf("OVERRUN!!!\n");
173 /* update the flight model */
176 lastSimtime = Simtime;
177 signal(SIGALRM, fgTimerCatch);
180 /* this routine initializes the interval timer to generate a SIGALRM after
181 * the specified interval (dt) */
182 void fgTimerInit(float dt) {
188 usec = 1000000* (dt - (float) isec);
190 t.it_interval.tv_sec = isec;
191 t.it_interval.tv_usec = usec;
192 t.it_value.tv_sec = isec;
193 t.it_value.tv_usec = usec;
194 /* printf("fgTimerInit() called\n"); */
195 fgTimerCatch(); /* set up for SIGALRM signal catch */
196 terr = setitimer( ITIMER_REAL, &t, &ot );
197 if (terr) perror("Error returned from setitimer");
201 /**************************************************************************
202 * Scenery management routines
203 **************************************************************************/
205 static void fgSceneryInit() {
206 /* make terrain mesh */
207 mesh = fgSceneryCompile();
211 /* create the terrain mesh */
212 GLint fgSceneryCompile() {
215 mesh = mesh2GL(mesh_ptr);
221 /* draw the terrain mesh */
222 static void fgSceneryDraw() {
227 /* What should we do when we have nothing else to do? How about get
228 * ready for the next move?*/
229 static void fgMainLoop( void )
238 /**************************************************************************
239 * Handle new window size or exposure
240 **************************************************************************/
242 static void fgReshape( int width, int height ) {
243 /* Do this so we can call fgReshape(0,0) ourselves without having to know
244 * what the values of width & height are. */
245 if ( (height > 0) && (width > 0) ) {
246 win_ratio = (GLfloat) height / (GLfloat) width;
249 /* Inform gl of our view window size */
250 glViewport(0, 0, (GLint)width, (GLint)height);
252 fgUpdateViewParams();
254 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
258 /**************************************************************************
260 **************************************************************************/
262 int main( int argc, char *argv[] ) {
263 /* parse the scenery file */
264 parse_scenery(argv[1]);
267 /* initialize GLUT */
268 glutInit(&argc, argv);
270 /* Define Display Parameters */
271 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
273 /* Define initial window size */
274 glutInitWindowSize(640, 400);
276 /* Initialize the main window */
277 glutCreateWindow("Terrain Demo");
279 /* Define initial window size */
280 tkInitPosition(0, 0, 640, 400);
282 /* Define Display Parameters */
283 tkInitDisplayMode( TK_RGB | TK_DEPTH | TK_DOUBLE | TK_DIRECT );
285 /* Initialize the main window */
286 if (tkInitWindow("Terrain Demo") == GL_FALSE) {
291 /* setup view parameters, only makes GL calls */
294 /* Set initial position and slew parameters */
295 /* fgSlewInit(-398391.3, 120070.4, 244, 3.1415); */ /* GLOBE Airport */
296 /* fgSlewInit(-335340,162540, 15, 4.38); */
297 fgSlewInit(-398673.28,120625.64, 53, 4.38);
299 /* build all objects */
302 /* initialize timer */
303 fgTimerInit( 1.0 / DEFAULT_MODEL_HZ );
306 /* call fgReshape() on window resizes */
307 glutReshapeFunc( fgReshape );
309 /* call key() on keyboard event */
310 glutKeyboardFunc( GLUTkey );
311 glutSpecialFunc( GLUTkey );
313 /* call fgMainLoop() whenever there is nothing else to do */
314 glutIdleFunc( fgMainLoop );
317 glutDisplayFunc( fgUpdateVisuals );
319 /* pass control off to the GLUT event handler */
322 /* call fgReshape() on expose events */
323 tkExposeFunc( fgReshape );
325 /* call fgReshape() on window resizes */
326 tkReshapeFunc( fgReshape );
328 /* call key() on keyboard event */
329 tkKeyDownFunc( GLTKkey );
331 /* call fgMainLoop() whenever there is nothing else to do */
332 tkIdleFunc( fgMainLoop );
335 tkDisplayFunc( fgUpdateVisuals );
337 /* pass control off to the tk event handler */
346 /* Revision 1.6 1997/05/29 12:31:39 curt
347 /* Minor tweaks, moving towards general flight model integration.
349 * Revision 1.5 1997/05/29 02:33:23 curt
350 * Updated to reflect changing interfaces in other "modules."
352 * Revision 1.4 1997/05/27 17:44:31 curt
353 * Renamed & rearranged variables and routines. Added some initial simple
354 * timer/alarm routines so the flight model can be updated on a regular
357 * Revision 1.3 1997/05/23 15:40:25 curt
358 * Added GNU copyright headers.
361 * Revision 1.2 1997/05/23 00:35:12 curt
362 * Trying to get fog to work ...
364 * Revision 1.1 1997/05/21 15:57:51 curt
365 * Renamed due to added GLUT support.
367 * Revision 1.3 1997/05/19 18:22:42 curt
368 * Parameter tweaking ... starting to stub in fog support.
370 * Revision 1.2 1997/05/17 00:17:34 curt
371 * Trying to stub in support for standard OpenGL.
373 * Revision 1.1 1997/05/16 16:05:52 curt