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 FG_RAD_2_DEG(RAD) ((RAD) * 180.0 / M_PI)
53 #define FG_DEG_2_RAD(DEG) ((DEG) * M_PI / 180.0)
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 extern struct mesh *mesh_ptr;
64 /* Function prototypes */
65 GLint fgSceneryCompile();
66 static void fgSceneryDraw();
67 /* pointer to terrain mesh structure */
71 double fogDensity = 2000.0;
74 #define DEFAULT_MODEL_HZ 20
75 #define DEFAULT_MULTILOOP 6
79 /**************************************************************************
80 * fgInitVisuals() -- Initialize various GL/view parameters
81 **************************************************************************/
83 static void fgInitVisuals() {
84 /* if the 4th field is 0.0, this specifies a direction ... */
85 static GLfloat sun_vec[4] = {3.0, 1.0, 3.0, 0.0 };
86 static GLfloat color[4] = { 0.3, 0.7, 0.2, 1.0 };
87 static GLfloat fogColor[4] = {0.65, 0.65, 0.85, 1.0};
89 glEnable( GL_DEPTH_TEST );
91 glEnable( GL_CULL_FACE );
93 /* If enabled, normal vectors specified with glNormal are scaled
94 to unit length after transformation. See glNormal. */
95 glEnable( GL_NORMALIZE );
97 glLightfv( GL_LIGHT0, GL_POSITION, sun_vec );
98 glEnable( GL_LIGHTING );
99 glEnable( GL_LIGHT0 );
101 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color );
102 glShadeModel( GL_FLAT ); /* glShadeModel( GL_SMOOTH ); */
105 glFogi (GL_FOG_MODE, GL_LINEAR);
106 /* glFogf (GL_FOG_START, 1.0); */
107 glFogf (GL_FOG_END, fogDensity);
108 glFogfv (GL_FOG_COLOR, fogColor);
109 /* glFogf (GL_FOG_DENSITY, fogDensity); */
110 /* glHint (GL_FOG_HINT, GL_FASTEST); */
112 glClearColor(0.6, 0.6, 0.9, 1.0);
116 /**************************************************************************
117 * Update the view volume, position, and orientation
118 **************************************************************************/
120 static void fgUpdateViewParams() {
121 double pos_x, pos_y, pos_z;
122 struct flight_params *f;
124 MAT3vec vec, forward, up;
126 f = ¤t_aircraft.flight;
128 /* Tell GL we are about to modify the projection parameters */
129 glMatrixMode(GL_PROJECTION);
131 gluPerspective(45.0, 1.0/win_ratio, 1.0, 6000.0);
133 glMatrixMode(GL_MODELVIEW);
136 /* calculate position in arc seconds */
137 pos_x = FG_RAD_2_DEG(FG_Longitude) * 3600.0;
138 pos_y = FG_RAD_2_DEG(FG_Latitude) * 3600.0;
139 pos_z = FG_Altitude * 0.01; /* (Convert feet to aproximate arcsecs) */
141 /* build current rotation matrix */
142 MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
143 MAT3rotate(R, vec, FG_Phi);
144 /* printf("Roll matrix\n"); */
145 /* MAT3print(R, stdout); */
147 MAT3_SET_VEC(vec, 0.0, -1.0, 0.0);
148 MAT3rotate(tmp, vec, FG_Theta);
149 /* printf("Pitch matrix\n"); */
150 /* MAT3print(tmp, stdout); */
153 MAT3_SET_VEC(vec, 0.0, 0.0, -1.0);
154 MAT3rotate(tmp, vec, M_PI + M_PI_2 + FG_Psi );
155 /* printf("Yaw matrix\n");
156 MAT3print(tmp, stdout); */
159 /* MAT3print(R, stdout); */
161 /* generate the current forward and up vectors */
162 MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
163 MAT3mult_vec(forward, vec, R);
164 printf("Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1],
166 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
167 MAT3mult_vec(up, vec, R);
169 gluLookAt(pos_x, pos_y, pos_z,
170 pos_x + forward[0], pos_y + forward[1], pos_z + forward[2],
171 up[0], up[1], up[2]);
176 /**************************************************************************
177 * Update all Visuals (redraws anything graphics related)
178 **************************************************************************/
180 static void fgUpdateVisuals( void ) {
181 /* update view volume parameters */
182 fgUpdateViewParams();
184 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
186 /* Tell GL we are switching to model view parameters */
187 glMatrixMode(GL_MODELVIEW);
188 /* glLoadIdentity(); */
190 /* draw terrain mesh */
201 /**************************************************************************
202 * Timer management routines
203 **************************************************************************/
205 static struct itimerval t, ot;
207 /* This routine catches the SIGALRM */
208 void fgTimerCatch() {
209 struct flight_params *f;
210 static double lastSimtime = -99.9;
213 f = ¤t_aircraft.flight;
215 /* printf("In fgTimerCatch()\n"); */
217 Overrun = (lastSimtime == Simtime);
219 /* add this back in when you get simtime working */
221 printf("OVERRUN!!!\n");
224 /* update the flight model */
225 fgFlightModelUpdate(FG_LARCSIM, f, DEFAULT_MULTILOOP);
227 lastSimtime = Simtime;
228 signal(SIGALRM, fgTimerCatch);
231 /* this routine initializes the interval timer to generate a SIGALRM after
232 * the specified interval (dt) */
233 void fgTimerInit(float dt) {
239 usec = 1000000* (dt - (float) isec);
241 t.it_interval.tv_sec = isec;
242 t.it_interval.tv_usec = usec;
243 t.it_value.tv_sec = isec;
244 t.it_value.tv_usec = usec;
245 /* printf("fgTimerInit() called\n"); */
246 fgTimerCatch(); /* set up for SIGALRM signal catch */
247 terr = setitimer( ITIMER_REAL, &t, &ot );
248 if (terr) perror("Error returned from setitimer");
252 /**************************************************************************
253 * Scenery management routines
254 **************************************************************************/
256 static void fgSceneryInit() {
257 /* make terrain mesh */
258 mesh = fgSceneryCompile();
262 /* create the terrain mesh */
263 GLint fgSceneryCompile() {
266 mesh = mesh2GL(mesh_ptr);
272 /* draw the terrain mesh */
273 static void fgSceneryDraw() {
278 /* What should we do when we have nothing else to do? How about get
279 * ready for the next move?*/
280 static void fgMainLoop( void )
289 /**************************************************************************
290 * Handle new window size or exposure
291 **************************************************************************/
293 static void fgReshape( int width, int height ) {
294 /* Do this so we can call fgReshape(0,0) ourselves without having to know
295 * what the values of width & height are. */
296 if ( (height > 0) && (width > 0) ) {
297 win_ratio = (GLfloat) height / (GLfloat) width;
300 /* Inform gl of our view window size */
301 glViewport(0, 0, (GLint)width, (GLint)height);
303 fgUpdateViewParams();
305 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
309 /**************************************************************************
311 **************************************************************************/
313 int main( int argc, char *argv[] ) {
314 struct flight_params *f;
316 f = ¤t_aircraft.flight;
318 /* parse the scenery file */
319 parse_scenery(argv[1]);
322 /* initialize GLUT */
323 glutInit(&argc, argv);
325 /* Define Display Parameters */
326 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
328 /* Define initial window size */
329 glutInitWindowSize(640, 480);
331 /* Initialize the main window */
332 glutCreateWindow("Terrain Demo");
334 /* Define initial window size */
335 tkInitPosition(0, 0, 640, 480);
337 /* Define Display Parameters */
338 tkInitDisplayMode( TK_RGB | TK_DEPTH | TK_DOUBLE | TK_DIRECT );
340 /* Initialize the main window */
341 if (tkInitWindow("Terrain Demo") == GL_FALSE) {
346 /* setup view parameters, only makes GL calls */
349 /* fgSlewInit(-398673.28,120625.64, 53, 4.38); */
351 /* Initial Position */
352 FG_Latitude = FG_DEG_2_RAD( 120625.64 / 3600.0 );
353 FG_Longitude = FG_DEG_2_RAD( -398673.28 / 3600.0 );
354 FG_Altitude = 3.758099E+00;
356 printf("Initial position is: (%.4f, %.4f, %.2f)\n", FG_Latitude,
357 FG_Longitude, FG_Altitude);
359 /* Initial Velocity */
360 FG_V_north = 0.0 /* 7.287719E+00 */;
361 FG_V_east = 0.0 /* 1.521770E+03 */;
362 FG_V_down = 0.0 /* -1.265722E-05 */;
364 /* Initial Orientation */
365 FG_Phi = -2.658474E-06;
366 FG_Theta = 7.401790E-03;
367 FG_Psi = 2.14 /* 4.38 */;
369 /* Initial Angular B rates */
370 FG_P_body = 7.206685E-05;
371 FG_Q_body = 0.000000E+00;
372 FG_R_body = 9.492658E-05;
374 FG_Earth_position_angle = 0.000000E+00;
376 /* Mass properties and geometry values */
377 FG_Mass = 8.547270E+01;
378 FG_I_xx = 1.048000E+03;
379 FG_I_yy = 3.000000E+03;
380 FG_I_zz = 3.530000E+03;
381 FG_I_xz = 0.000000E+00;
383 /* CG position w.r.t. ref. point */
384 FG_Dx_cg = 0.000000E+00;
385 FG_Dy_cg = 0.000000E+00;
386 FG_Dz_cg = 0.000000E+00;
388 /* Set initial position and slew parameters */
389 /* fgSlewInit(-398391.3, 120070.4, 244, 3.1415); */ /* GLOBE Airport */
390 /* fgSlewInit(-335340,162540, 15, 4.38); */
391 /* fgSlewInit(-398673.28,120625.64, 53, 4.38); */
393 fgFlightModelInit(FG_LARCSIM, f, 1.0 / DEFAULT_MODEL_HZ);
395 /* build all objects */
398 /* initialize timer */
399 fgTimerInit( 1.0 / DEFAULT_MODEL_HZ );
402 /* call fgReshape() on window resizes */
403 glutReshapeFunc( fgReshape );
405 /* call key() on keyboard event */
406 glutKeyboardFunc( GLUTkey );
407 glutSpecialFunc( GLUTkey );
409 /* call fgMainLoop() whenever there is nothing else to do */
410 glutIdleFunc( fgMainLoop );
413 glutDisplayFunc( fgUpdateVisuals );
415 /* pass control off to the GLUT event handler */
418 /* call fgReshape() on expose events */
419 tkExposeFunc( fgReshape );
421 /* call fgReshape() on window resizes */
422 tkReshapeFunc( fgReshape );
424 /* call key() on keyboard event */
425 tkKeyDownFunc( GLTKkey );
427 /* call fgMainLoop() whenever there is nothing else to do */
428 tkIdleFunc( fgMainLoop );
431 tkDisplayFunc( fgUpdateVisuals );
433 /* pass control off to the tk event handler */
442 /* Revision 1.9 1997/05/30 19:27:01 curt
443 /* The LaRCsim flight model is starting to look like it is working.
445 * Revision 1.8 1997/05/30 03:54:10 curt
446 * Made a bit more progress towards integrating the LaRCsim flight model.
448 * Revision 1.7 1997/05/29 22:39:49 curt
449 * Working on incorporating the LaRCsim flight model.
451 * Revision 1.6 1997/05/29 12:31:39 curt
452 * Minor tweaks, moving towards general flight model integration.
454 * Revision 1.5 1997/05/29 02:33:23 curt
455 * Updated to reflect changing interfaces in other "modules."
457 * Revision 1.4 1997/05/27 17:44:31 curt
458 * Renamed & rearranged variables and routines. Added some initial simple
459 * timer/alarm routines so the flight model can be updated on a regular
462 * Revision 1.3 1997/05/23 15:40:25 curt
463 * Added GNU copyright headers.
466 * Revision 1.2 1997/05/23 00:35:12 curt
467 * Trying to get fog to work ...
469 * Revision 1.1 1997/05/21 15:57:51 curt
470 * Renamed due to added GLUT support.
472 * Revision 1.3 1997/05/19 18:22:42 curt
473 * Parameter tweaking ... starting to stub in fog support.
475 * Revision 1.2 1997/05/17 00:17:34 curt
476 * Trying to stub in support for standard OpenGL.
478 * Revision 1.1 1997/05/16 16:05:52 curt