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 */
32 #if defined(__WATCOMC__) || defined(__MSC__)
35 # include <sys/time.h>
42 /* assumes -I/usr/include/mesa in compile command */
47 #include "../aircraft/aircraft.h"
48 #include "../scenery/scenery.h"
50 #define FG_RAD_2_DEG(RAD) ((RAD) * 180.0 / M_PI)
51 #define FG_DEG_2_RAD(DEG) ((DEG) * M_PI / 180.0)
53 /* This is a record containing all the info for the aircraft currently
55 struct aircraft_params current_aircraft;
58 static GLfloat win_ratio = 1.0;
61 extern struct mesh *mesh_ptr;
62 /* Function prototypes */
63 GLint fgSceneryCompile();
64 static void fgSceneryDraw();
65 /* pointer to terrain mesh structure */
69 double fogDensity = 2000.0;
72 #define DEFAULT_MODEL_HZ 20
73 #define DEFAULT_MULTILOOP 6
77 /**************************************************************************
78 * fgInitVisuals() -- Initialize various GL/view parameters
79 **************************************************************************/
81 static void fgInitVisuals() {
82 /* if the 4th field is 0.0, this specifies a direction ... */
83 static GLfloat sun_vec[4] = {3.0, 1.0, 3.0, 0.0 };
84 static GLfloat color[4] = { 0.3, 0.7, 0.2, 1.0 };
85 static GLfloat fogColor[4] = {0.65, 0.65, 0.85, 1.0};
87 glEnable( GL_DEPTH_TEST );
89 glEnable( GL_CULL_FACE );
91 /* If enabled, normal vectors specified with glNormal are scaled
92 to unit length after transformation. See glNormal. */
93 glEnable( GL_NORMALIZE );
95 glLightfv( GL_LIGHT0, GL_POSITION, sun_vec );
96 glEnable( GL_LIGHTING );
97 glEnable( GL_LIGHT0 );
99 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color );
100 glShadeModel( GL_FLAT ); /* glShadeModel( GL_SMOOTH ); */
103 glFogi (GL_FOG_MODE, GL_LINEAR);
104 /* glFogf (GL_FOG_START, 1.0); */
105 glFogf (GL_FOG_END, fogDensity);
106 glFogfv (GL_FOG_COLOR, fogColor);
107 /* glFogf (GL_FOG_DENSITY, fogDensity); */
108 /* glHint (GL_FOG_HINT, GL_FASTEST); */
110 glClearColor(0.6, 0.6, 0.9, 1.0);
114 /**************************************************************************
115 * Update the view volume, position, and orientation
116 **************************************************************************/
118 static void fgUpdateViewParams() {
119 double pos_x, pos_y, pos_z;
120 struct flight_params *f;
122 f = ¤t_aircraft.flight;
124 /* Tell GL we are about to modify the projection parameters */
125 glMatrixMode(GL_PROJECTION);
127 gluPerspective(45.0, 1.0/win_ratio, 1.0, 6000.0);
129 glMatrixMode(GL_MODELVIEW);
132 pos_x = FG_RAD_2_DEG(FG_Longitude) * 3600.0;
133 pos_y = FG_RAD_2_DEG(FG_Latitude) * 3600.0;
134 pos_z = FG_Altitude * 0.01; /* (Convert feet to aproximate arcsecs) */
136 glRotatef(FG_Phi, 1.0, 0.0, 0.0);
137 glRotatef(FG_Theta, 0.0, 1.0, 0.0);
138 glRotatef(FG_Psi, 0.0, 0.0, 1.0);
140 gluLookAt(pos_x, pos_y, pos_z,
141 pos_x + 1.0, pos_y, pos_z,
147 /**************************************************************************
148 * Update all Visuals (redraws anything graphics related)
149 **************************************************************************/
151 static void fgUpdateVisuals( void ) {
152 /* update view volume parameters */
153 fgUpdateViewParams();
155 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
157 /* Tell GL we are switching to model view parameters */
158 glMatrixMode(GL_MODELVIEW);
159 /* glLoadIdentity(); */
161 /* draw terrain mesh */
172 /**************************************************************************
173 * Timer management routines
174 **************************************************************************/
176 static struct itimerval t, ot;
178 /* This routine catches the SIGALRM */
179 void fgTimerCatch() {
180 struct flight_params *f;
181 static double lastSimtime = -99.9;
184 f = ¤t_aircraft.flight;
186 /* printf("In fgTimerCatch()\n"); */
188 Overrun = (lastSimtime == Simtime);
190 /* add this back in when you get simtime working */
192 printf("OVERRUN!!!\n");
195 /* update the flight model */
196 fgFlightModelUpdate(FG_LARCSIM, f, DEFAULT_MULTILOOP);
198 lastSimtime = Simtime;
199 signal(SIGALRM, fgTimerCatch);
202 /* this routine initializes the interval timer to generate a SIGALRM after
203 * the specified interval (dt) */
204 void fgTimerInit(float dt) {
210 usec = 1000000* (dt - (float) isec);
212 t.it_interval.tv_sec = isec;
213 t.it_interval.tv_usec = usec;
214 t.it_value.tv_sec = isec;
215 t.it_value.tv_usec = usec;
216 /* printf("fgTimerInit() called\n"); */
217 fgTimerCatch(); /* set up for SIGALRM signal catch */
218 terr = setitimer( ITIMER_REAL, &t, &ot );
219 if (terr) perror("Error returned from setitimer");
223 /**************************************************************************
224 * Scenery management routines
225 **************************************************************************/
227 static void fgSceneryInit() {
228 /* make terrain mesh */
229 mesh = fgSceneryCompile();
233 /* create the terrain mesh */
234 GLint fgSceneryCompile() {
237 mesh = mesh2GL(mesh_ptr);
243 /* draw the terrain mesh */
244 static void fgSceneryDraw() {
249 /* What should we do when we have nothing else to do? How about get
250 * ready for the next move?*/
251 static void fgMainLoop( void )
260 /**************************************************************************
261 * Handle new window size or exposure
262 **************************************************************************/
264 static void fgReshape( int width, int height ) {
265 /* Do this so we can call fgReshape(0,0) ourselves without having to know
266 * what the values of width & height are. */
267 if ( (height > 0) && (width > 0) ) {
268 win_ratio = (GLfloat) height / (GLfloat) width;
271 /* Inform gl of our view window size */
272 glViewport(0, 0, (GLint)width, (GLint)height);
274 fgUpdateViewParams();
276 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
280 /**************************************************************************
282 **************************************************************************/
284 int main( int argc, char *argv[] ) {
285 struct flight_params *f;
287 f = ¤t_aircraft.flight;
289 /* parse the scenery file */
290 parse_scenery(argv[1]);
293 /* initialize GLUT */
294 glutInit(&argc, argv);
296 /* Define Display Parameters */
297 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
299 /* Define initial window size */
300 glutInitWindowSize(640, 480);
302 /* Initialize the main window */
303 glutCreateWindow("Terrain Demo");
305 /* Define initial window size */
306 tkInitPosition(0, 0, 640, 480);
308 /* Define Display Parameters */
309 tkInitDisplayMode( TK_RGB | TK_DEPTH | TK_DOUBLE | TK_DIRECT );
311 /* Initialize the main window */
312 if (tkInitWindow("Terrain Demo") == GL_FALSE) {
317 /* setup view parameters, only makes GL calls */
320 /* fgSlewInit(-398673.28,120625.64, 53, 4.38); */
322 /* Initial Position */
323 FG_Latitude = FG_DEG_2_RAD( 120625.64 / 3600.0 );
324 FG_Longitude = FG_DEG_2_RAD( -398673.28 / 3600.0 );
325 FG_Altitude = 3.758099E+00;
327 printf("Initial position is: (%.4f, %.4f, %.2f)\n", FG_Latitude,
328 FG_Longitude, FG_Altitude);
330 /* Initial Velocity */
331 FG_V_north = 0.0 /* 7.287719E+00 */;
332 FG_V_east = 0.0 /* 1.521770E+03 */;
333 FG_V_down = 0.0 /* -1.265722E-05 */;
335 /* Initial Orientation */
336 FG_Phi = -2.658474E-06;
337 FG_Theta = 7.401790E-03;
340 /* Initial Angular B rates */
341 FG_P_body = 7.206685E-05;
342 FG_Q_body = 0.000000E+00;
343 FG_R_body = 9.492658E-05;
345 FG_Earth_position_angle = 0.000000E+00;
347 /* Mass properties and geometry values */
348 FG_Mass = 8.547270E+01;
349 FG_I_xx = 1.048000E+03;
350 FG_I_yy = 3.000000E+03;
351 FG_I_zz = 3.530000E+03;
352 FG_I_xz = 0.000000E+00;
354 /* CG position w.r.t. ref. point */
355 FG_Dx_cg = 0.000000E+00;
356 FG_Dy_cg = 0.000000E+00;
357 FG_Dz_cg = 0.000000E+00;
359 /* Set initial position and slew parameters */
360 /* fgSlewInit(-398391.3, 120070.4, 244, 3.1415); */ /* GLOBE Airport */
361 /* fgSlewInit(-335340,162540, 15, 4.38); */
362 /* fgSlewInit(-398673.28,120625.64, 53, 4.38); */
364 fgFlightModelInit(FG_LARCSIM, f, 1.0 / DEFAULT_MODEL_HZ);
366 /* build all objects */
369 /* initialize timer */
370 fgTimerInit( 1.0 / DEFAULT_MODEL_HZ );
373 /* call fgReshape() on window resizes */
374 glutReshapeFunc( fgReshape );
376 /* call key() on keyboard event */
377 glutKeyboardFunc( GLUTkey );
378 glutSpecialFunc( GLUTkey );
380 /* call fgMainLoop() whenever there is nothing else to do */
381 glutIdleFunc( fgMainLoop );
384 glutDisplayFunc( fgUpdateVisuals );
386 /* pass control off to the GLUT event handler */
389 /* call fgReshape() on expose events */
390 tkExposeFunc( fgReshape );
392 /* call fgReshape() on window resizes */
393 tkReshapeFunc( fgReshape );
395 /* call key() on keyboard event */
396 tkKeyDownFunc( GLTKkey );
398 /* call fgMainLoop() whenever there is nothing else to do */
399 tkIdleFunc( fgMainLoop );
402 tkDisplayFunc( fgUpdateVisuals );
404 /* pass control off to the tk event handler */
413 /* Revision 1.8 1997/05/30 03:54:10 curt
414 /* Made a bit more progress towards integrating the LaRCsim flight model.
416 * Revision 1.7 1997/05/29 22:39:49 curt
417 * Working on incorporating the LaRCsim flight model.
419 * Revision 1.6 1997/05/29 12:31:39 curt
420 * Minor tweaks, moving towards general flight model integration.
422 * Revision 1.5 1997/05/29 02:33:23 curt
423 * Updated to reflect changing interfaces in other "modules."
425 * Revision 1.4 1997/05/27 17:44:31 curt
426 * Renamed & rearranged variables and routines. Added some initial simple
427 * timer/alarm routines so the flight model can be updated on a regular
430 * Revision 1.3 1997/05/23 15:40:25 curt
431 * Added GNU copyright headers.
434 * Revision 1.2 1997/05/23 00:35:12 curt
435 * Trying to get fog to work ...
437 * Revision 1.1 1997/05/21 15:57:51 curt
438 * Renamed due to added GLUT support.
440 * Revision 1.3 1997/05/19 18:22:42 curt
441 * Parameter tweaking ... starting to stub in fog support.
443 * Revision 1.2 1997/05/17 00:17:34 curt
444 * Trying to stub in support for standard OpenGL.
446 * Revision 1.1 1997/05/16 16:05:52 curt