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"
49 #include "../mat3/mat3.h"
52 #define DEG_TO_RAD 0.017453292
53 #define RAD_TO_DEG 57.29577951
55 /* This is a record containing all the info for the aircraft currently
57 struct aircraft_params current_aircraft;
60 static GLfloat win_ratio = 1.0;
63 static GLfloat sun_vec[4] = {-3.0, 1.0, 2.0, 0.0 };
66 extern struct mesh *mesh_ptr;
67 /* Function prototypes */
68 GLint fgSceneryCompile();
69 static void fgSceneryDraw();
70 /* pointer to terrain mesh structure */
74 double fogDensity = 2000.0;
77 #define DEFAULT_MODEL_HZ 20
78 #define DEFAULT_MULTILOOP 6
82 /**************************************************************************
83 * fgInitVisuals() -- Initialize various GL/view parameters
84 **************************************************************************/
86 static void fgInitVisuals() {
87 /* if the 4th field is 0.0, this specifies a direction ... */
88 static GLfloat color[4] = { 0.3, 0.7, 0.2, 1.0 };
89 static GLfloat fogColor[4] = {0.65, 0.65, 0.85, 1.0};
91 glEnable( GL_DEPTH_TEST );
93 glEnable( GL_CULL_FACE );
95 /* If enabled, normal vectors specified with glNormal are scaled
96 to unit length after transformation. See glNormal. */
97 glEnable( GL_NORMALIZE );
99 glLightfv( GL_LIGHT0, GL_POSITION, sun_vec );
100 glEnable( GL_LIGHTING );
101 glEnable( GL_LIGHT0 );
103 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color );
104 glShadeModel( GL_FLAT ); /* glShadeModel( GL_SMOOTH ); */
107 glFogi (GL_FOG_MODE, GL_LINEAR);
108 /* glFogf (GL_FOG_START, 1.0); */
109 glFogf (GL_FOG_END, fogDensity);
110 glFogfv (GL_FOG_COLOR, fogColor);
111 /* glFogf (GL_FOG_DENSITY, fogDensity); */
112 /* glHint (GL_FOG_HINT, GL_FASTEST); */
114 glClearColor(0.6, 0.6, 0.9, 1.0);
118 /**************************************************************************
119 * Update the view volume, position, and orientation
120 **************************************************************************/
122 static void fgUpdateViewParams() {
123 double pos_x, pos_y, pos_z;
124 struct flight_params *f;
126 MAT3vec vec, forward, up;
128 f = ¤t_aircraft.flight;
130 /* Tell GL we are about to modify the projection parameters */
131 glMatrixMode(GL_PROJECTION);
133 gluPerspective(45.0, 1.0/win_ratio, 1.0, 6000.0);
135 glMatrixMode(GL_MODELVIEW);
138 /* calculate position in arc seconds */
139 pos_x = (FG_Longitude * RAD_TO_DEG) * 3600.0;
140 pos_y = (FG_Latitude * RAD_TO_DEG) * 3600.0;
141 pos_z = FG_Altitude * 0.01; /* (Convert feet to aproximate arcsecs) */
143 /* build current rotation matrix */
144 MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
145 MAT3rotate(R, vec, FG_Phi);
146 /* printf("Roll matrix\n"); */
147 /* MAT3print(R, stdout); */
149 MAT3_SET_VEC(vec, 0.0, -1.0, 0.0);
150 MAT3rotate(tmp, vec, FG_Theta);
151 /* printf("Pitch matrix\n"); */
152 /* MAT3print(tmp, stdout); */
155 MAT3_SET_VEC(vec, 0.0, 0.0, -1.0);
156 MAT3rotate(tmp, vec, M_PI + M_PI_2 + FG_Psi );
157 /* printf("Yaw matrix\n");
158 MAT3print(tmp, stdout); */
161 /* MAT3print(R, stdout); */
163 /* generate the current forward and up vectors */
164 MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
165 MAT3mult_vec(forward, vec, R);
166 printf("Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1],
168 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
169 MAT3mult_vec(up, vec, R);
171 gluLookAt(pos_x, pos_y, pos_z,
172 pos_x + forward[0], pos_y + forward[1], pos_z + forward[2],
173 up[0], up[1], up[2]);
175 glLightfv( GL_LIGHT0, GL_POSITION, sun_vec );
179 /**************************************************************************
180 * Update all Visuals (redraws anything graphics related)
181 **************************************************************************/
183 static void fgUpdateVisuals( void ) {
184 /* update view volume parameters */
185 fgUpdateViewParams();
187 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
189 /* Tell GL we are switching to model view parameters */
190 glMatrixMode(GL_MODELVIEW);
191 /* glLoadIdentity(); */
193 /* draw terrain mesh */
204 /**************************************************************************
205 * Timer management routines
206 **************************************************************************/
208 static struct itimerval t, ot;
210 /* This routine catches the SIGALRM */
211 void fgTimerCatch() {
212 struct flight_params *f;
213 static double lastSimtime = -99.9;
216 /* ignore any SIGALRM's until we come back from our EOM iteration */
217 signal(SIGALRM, SIG_IGN);
219 f = ¤t_aircraft.flight;
221 /* printf("In fgTimerCatch()\n"); */
223 Overrun = (lastSimtime == Simtime);
225 /* add this back in when you get simtime working */
227 printf("OVERRUN!!!\n");
230 /* update the flight model */
231 fgFlightModelUpdate(FG_LARCSIM, f, DEFAULT_MULTILOOP);
233 lastSimtime = Simtime;
234 signal(SIGALRM, fgTimerCatch);
237 /* this routine initializes the interval timer to generate a SIGALRM after
238 * the specified interval (dt) */
239 void fgTimerInit(float dt) {
245 usec = 1000000* (dt - (float) isec);
247 t.it_interval.tv_sec = isec;
248 t.it_interval.tv_usec = usec;
249 t.it_value.tv_sec = isec;
250 t.it_value.tv_usec = usec;
251 /* printf("fgTimerInit() called\n"); */
252 fgTimerCatch(); /* set up for SIGALRM signal catch */
253 terr = setitimer( ITIMER_REAL, &t, &ot );
254 if (terr) perror("Error returned from setitimer");
258 /**************************************************************************
259 * Scenery management routines
260 **************************************************************************/
262 static void fgSceneryInit() {
263 /* make terrain mesh */
264 mesh = fgSceneryCompile();
268 /* create the terrain mesh */
269 GLint fgSceneryCompile() {
272 mesh = mesh2GL(mesh_ptr);
278 /* draw the terrain mesh */
279 static void fgSceneryDraw() {
284 /* What should we do when we have nothing else to do? How about get
285 * ready for the next move?*/
286 static void fgMainLoop( void )
295 /**************************************************************************
296 * Handle new window size or exposure
297 **************************************************************************/
299 static void fgReshape( int width, int height ) {
300 /* Do this so we can call fgReshape(0,0) ourselves without having to know
301 * what the values of width & height are. */
302 if ( (height > 0) && (width > 0) ) {
303 win_ratio = (GLfloat) height / (GLfloat) width;
306 /* Inform gl of our view window size */
307 glViewport(0, 0, (GLint)width, (GLint)height);
309 fgUpdateViewParams();
311 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
315 /**************************************************************************
317 **************************************************************************/
319 int main( int argc, char *argv[] ) {
320 struct flight_params *f;
322 f = ¤t_aircraft.flight;
324 /* parse the scenery file */
325 parse_scenery(argv[1]);
328 /* initialize GLUT */
329 glutInit(&argc, argv);
331 /* Define Display Parameters */
332 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
334 /* Define initial window size */
335 glutInitWindowSize(640, 480);
337 /* Initialize the main window */
338 glutCreateWindow("Terrain Demo");
340 /* Define initial window size */
341 tkInitPosition(0, 0, 640, 480);
343 /* Define Display Parameters */
344 tkInitDisplayMode( TK_RGB | TK_DEPTH | TK_DOUBLE | TK_DIRECT );
346 /* Initialize the main window */
347 if (tkInitWindow("Terrain Demo") == GL_FALSE) {
352 /* setup view parameters, only makes GL calls */
355 /* Globe Aiport, AZ */
356 FG_Runway_altitude = 3234.5;
357 FG_Runway_latitude = 120070.41;
358 FG_Runway_longitude = -398391.28;
359 FG_Runway_heading = 102.0 * DEG_TO_RAD;
361 /* Initial Position */
362 FG_Latitude = ( 120070.41 / 3600.0 ) * DEG_TO_RAD;
363 FG_Longitude = ( -398391.28 / 3600.0 ) * DEG_TO_RAD;
364 FG_Altitude = FG_Runway_altitude + 3.758099;
366 printf("Initial position is: (%.4f, %.4f, %.2f)\n", FG_Latitude,
367 FG_Longitude, FG_Altitude);
370 /* Initial Velocity */
371 FG_V_north = 0.0 /* 7.287719E+00 */;
372 FG_V_east = 0.0 /* 1.521770E+03 */;
373 FG_V_down = 0.0 /* -1.265722E-05 */;
375 /* Initial Orientation */
376 FG_Phi = -2.658474E-06;
377 FG_Theta = 7.401790E-03;
378 FG_Psi = 102.0 * DEG_TO_RAD;
380 /* Initial Angular B rates */
381 FG_P_body = 7.206685E-05;
382 FG_Q_body = 0.000000E+00;
383 FG_R_body = 9.492658E-05;
385 FG_Earth_position_angle = 0.000000E+00;
387 /* Mass properties and geometry values */
388 FG_Mass = 8.547270E+01;
389 FG_I_xx = 1.048000E+03;
390 FG_I_yy = 3.000000E+03;
391 FG_I_zz = 3.530000E+03;
392 FG_I_xz = 0.000000E+00;
394 /* CG position w.r.t. ref. point */
395 FG_Dx_cg = 0.000000E+00;
396 FG_Dy_cg = 0.000000E+00;
397 FG_Dz_cg = 0.000000E+00;
399 /* Set initial position and slew parameters */
400 /* fgSlewInit(-398391.3, 120070.41, 244, 3.1415); */ /* GLOBE Airport */
401 /* fgSlewInit(-335340,162540, 15, 4.38); */
402 /* fgSlewInit(-398673.28,120625.64, 53, 4.38); */
404 fgFlightModelInit(FG_LARCSIM, f, 1.0/(DEFAULT_MODEL_HZ*DEFAULT_MULTILOOP));
406 /* build all objects */
409 /* initialize timer */
410 fgTimerInit( 1.0 / DEFAULT_MODEL_HZ );
413 /* call fgReshape() on window resizes */
414 glutReshapeFunc( fgReshape );
416 /* call key() on keyboard event */
417 glutKeyboardFunc( GLUTkey );
418 glutSpecialFunc( GLUTspecialkey );
420 /* call fgMainLoop() whenever there is nothing else to do */
421 glutIdleFunc( fgMainLoop );
424 glutDisplayFunc( fgUpdateVisuals );
426 /* pass control off to the GLUT event handler */
429 /* call fgReshape() on expose events */
430 tkExposeFunc( fgReshape );
432 /* call fgReshape() on window resizes */
433 tkReshapeFunc( fgReshape );
435 /* call key() on keyboard event */
436 tkKeyDownFunc( GLTKkey );
438 /* call fgMainLoop() whenever there is nothing else to do */
439 tkIdleFunc( fgMainLoop );
442 tkDisplayFunc( fgUpdateVisuals );
444 /* pass control off to the tk event handler */
453 /* Revision 1.11 1997/05/31 19:16:25 curt
454 /* Elevator trim added.
456 * Revision 1.10 1997/05/31 04:13:52 curt
459 * Continuing work on the LaRCsim flight model integration.
460 * Added some MSFS-like keyboard input handling.
462 * Revision 1.9 1997/05/30 19:27:01 curt
463 * The LaRCsim flight model is starting to look like it is working.
465 * Revision 1.8 1997/05/30 03:54:10 curt
466 * Made a bit more progress towards integrating the LaRCsim flight model.
468 * Revision 1.7 1997/05/29 22:39:49 curt
469 * Working on incorporating the LaRCsim flight model.
471 * Revision 1.6 1997/05/29 12:31:39 curt
472 * Minor tweaks, moving towards general flight model integration.
474 * Revision 1.5 1997/05/29 02:33:23 curt
475 * Updated to reflect changing interfaces in other "modules."
477 * Revision 1.4 1997/05/27 17:44:31 curt
478 * Renamed & rearranged variables and routines. Added some initial simple
479 * timer/alarm routines so the flight model can be updated on a regular
482 * Revision 1.3 1997/05/23 15:40:25 curt
483 * Added GNU copyright headers.
486 * Revision 1.2 1997/05/23 00:35:12 curt
487 * Trying to get fog to work ...
489 * Revision 1.1 1997/05/21 15:57:51 curt
490 * Renamed due to added GLUT support.
492 * Revision 1.3 1997/05/19 18:22:42 curt
493 * Parameter tweaking ... starting to stub in fog support.
495 * Revision 1.2 1997/05/17 00:17:34 curt
496 * Trying to stub in support for standard OpenGL.
498 * Revision 1.1 1997/05/16 16:05:52 curt