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_LON_2_DEG(RAD) ((RAD) * 180.0 / M_PI)
51 #define FG_LAT_2_DEG(RAD) (-1.0 * (RAD) * 180.0 / M_PI)
53 #define FG_DEG_2_LON(DEG) ((DEG) * M_PI / 180.0)
54 #define FG_DEG_2_LAT(DEG) (-1.0 * (DEG) * M_PI / 180.0)
56 /* This is a record containing all the info for the aircraft currently
58 struct aircraft_params current_aircraft;
61 static GLfloat win_ratio = 1.0;
64 extern struct mesh *mesh_ptr;
65 /* Function prototypes */
66 GLint fgSceneryCompile();
67 static void fgSceneryDraw();
68 /* pointer to terrain mesh structure */
72 double fogDensity = 2000.0;
75 #define DEFAULT_MODEL_HZ 20
76 #define DEFAULT_MULTILOOP 6
80 /**************************************************************************
81 * fgInitVisuals() -- Initialize various GL/view parameters
82 **************************************************************************/
84 static void fgInitVisuals() {
85 /* if the 4th field is 0.0, this specifies a direction ... */
86 static GLfloat sun_vec[4] = {3.0, 1.0, 3.0, 0.0 };
87 static GLfloat color[4] = { 0.3, 0.7, 0.2, 1.0 };
88 static GLfloat fogColor[4] = {0.65, 0.65, 0.85, 1.0};
90 glEnable( GL_DEPTH_TEST );
92 glEnable( GL_CULL_FACE );
94 /* If enabled, normal vectors specified with glNormal are scaled
95 to unit length after transformation. See glNormal. */
96 glEnable( GL_NORMALIZE );
98 glLightfv( GL_LIGHT0, GL_POSITION, sun_vec );
99 glEnable( GL_LIGHTING );
100 glEnable( GL_LIGHT0 );
102 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color );
103 glShadeModel( GL_FLAT ); /* glShadeModel( GL_SMOOTH ); */
106 glFogi (GL_FOG_MODE, GL_LINEAR);
107 /* glFogf (GL_FOG_START, 1.0); */
108 glFogf (GL_FOG_END, fogDensity);
109 glFogfv (GL_FOG_COLOR, fogColor);
110 /* glFogf (GL_FOG_DENSITY, fogDensity); */
111 /* glHint (GL_FOG_HINT, GL_FASTEST); */
113 glClearColor(0.6, 0.6, 0.9, 1.0);
117 /**************************************************************************
118 * Update the view volume, position, and orientation
119 **************************************************************************/
121 static void fgUpdateViewParams() {
122 double pos_x, pos_y, pos_z;
123 struct flight_params *f;
125 f = ¤t_aircraft.flight;
127 /* Tell GL we are about to modify the projection parameters */
128 glMatrixMode(GL_PROJECTION);
130 gluPerspective(45.0, 1.0/win_ratio, 1.0, 6000.0);
132 glMatrixMode(GL_MODELVIEW);
135 pos_x = FG_LAT_2_DEG(FG_Latitude) * 3600.0;
136 pos_y = FG_LON_2_DEG(FG_Longitude) * 3600.0;
139 gluLookAt(pos_x, pos_y, pos_z,
140 pos_x + cos(FG_Psi), pos_y + sin(FG_Psi), pos_z,
145 /**************************************************************************
146 * Update all Visuals (redraws anything graphics related)
147 **************************************************************************/
149 static void fgUpdateVisuals( void ) {
150 /* update view volume parameters */
151 fgUpdateViewParams();
153 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
155 /* Tell GL we are switching to model view parameters */
156 glMatrixMode(GL_MODELVIEW);
157 /* glLoadIdentity(); */
159 /* draw terrain mesh */
170 /**************************************************************************
171 * Timer management routines
172 **************************************************************************/
174 static struct itimerval t, ot;
176 /* This routine catches the SIGALRM */
177 void fgTimerCatch() {
178 struct flight_params *f;
179 static double lastSimtime = -99.9;
182 f = ¤t_aircraft.flight;
184 /* printf("In fgTimerCatch()\n"); */
186 Overrun = (lastSimtime == Simtime);
188 /* add this back in when you get simtime working */
190 printf("OVERRUN!!!\n");
193 /* update the flight model */
194 fgFlightModelUpdate(FG_LARCSIM, f, DEFAULT_MULTILOOP);
196 lastSimtime = Simtime;
197 signal(SIGALRM, fgTimerCatch);
200 /* this routine initializes the interval timer to generate a SIGALRM after
201 * the specified interval (dt) */
202 void fgTimerInit(float dt) {
208 usec = 1000000* (dt - (float) isec);
210 t.it_interval.tv_sec = isec;
211 t.it_interval.tv_usec = usec;
212 t.it_value.tv_sec = isec;
213 t.it_value.tv_usec = usec;
214 /* printf("fgTimerInit() called\n"); */
215 fgTimerCatch(); /* set up for SIGALRM signal catch */
216 terr = setitimer( ITIMER_REAL, &t, &ot );
217 if (terr) perror("Error returned from setitimer");
221 /**************************************************************************
222 * Scenery management routines
223 **************************************************************************/
225 static void fgSceneryInit() {
226 /* make terrain mesh */
227 mesh = fgSceneryCompile();
231 /* create the terrain mesh */
232 GLint fgSceneryCompile() {
235 mesh = mesh2GL(mesh_ptr);
241 /* draw the terrain mesh */
242 static void fgSceneryDraw() {
247 /* What should we do when we have nothing else to do? How about get
248 * ready for the next move?*/
249 static void fgMainLoop( void )
258 /**************************************************************************
259 * Handle new window size or exposure
260 **************************************************************************/
262 static void fgReshape( int width, int height ) {
263 /* Do this so we can call fgReshape(0,0) ourselves without having to know
264 * what the values of width & height are. */
265 if ( (height > 0) && (width > 0) ) {
266 win_ratio = (GLfloat) height / (GLfloat) width;
269 /* Inform gl of our view window size */
270 glViewport(0, 0, (GLint)width, (GLint)height);
272 fgUpdateViewParams();
274 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
278 /**************************************************************************
280 **************************************************************************/
282 int main( int argc, char *argv[] ) {
283 struct flight_params *f;
285 f = ¤t_aircraft.flight;
287 /* parse the scenery file */
288 parse_scenery(argv[1]);
291 /* initialize GLUT */
292 glutInit(&argc, argv);
294 /* Define Display Parameters */
295 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
297 /* Define initial window size */
298 glutInitWindowSize(640, 480);
300 /* Initialize the main window */
301 glutCreateWindow("Terrain Demo");
303 /* Define initial window size */
304 tkInitPosition(0, 0, 640, 480);
306 /* Define Display Parameters */
307 tkInitDisplayMode( TK_RGB | TK_DEPTH | TK_DOUBLE | TK_DIRECT );
309 /* Initialize the main window */
310 if (tkInitWindow("Terrain Demo") == GL_FALSE) {
315 /* setup view parameters, only makes GL calls */
318 /* fgSlewInit(-398673.28,120625.64, 53, 4.38); */
320 /* Initial Position */
321 FG_Latitude = FG_DEG_2_LAT( -398673.28 / 3600.0 );
322 FG_Longitude = FG_DEG_2_LON( 120625.64 / 3600.0 );
323 FG_Altitude = 3.758099E+00;
325 printf("Initial position is: (%.4f, %.4f, %.2f)\n", FG_Latitude,
326 FG_Longitude, FG_Altitude);
328 /* Initial Velocity */
329 FG_V_north = 0.0 /* 7.287719E+00 */;
330 FG_V_east = 0.0 /* 1.521770E+03 */;
331 FG_V_down = 0.0 /* -1.265722E-05 */;
333 /* Initial Orientation */
334 FG_Phi = -2.658474E-06;
335 FG_Theta = 7.401790E-03;
336 FG_Psi = 1.391358E-03;
338 /* Initial Angular B rates */
339 FG_P_body = 7.206685E-05;
340 FG_Q_body = 0.000000E+00;
341 FG_R_body = 9.492658E-05;
343 FG_Earth_position_angle = 0.000000E+00;
345 /* Mass properties and geometry values */
346 FG_Mass = 8.547270E+01;
347 FG_I_xx = 1.048000E+03;
348 FG_I_yy = 3.000000E+03;
349 FG_I_zz = 3.530000E+03;
350 FG_I_xz = 0.000000E+00;
352 /* CG position w.r.t. ref. point */
353 FG_Dx_cg = 0.000000E+00;
354 FG_Dy_cg = 0.000000E+00;
355 FG_Dz_cg = 0.000000E+00;
357 /* Set initial position and slew parameters */
358 /* fgSlewInit(-398391.3, 120070.4, 244, 3.1415); */ /* GLOBE Airport */
359 /* fgSlewInit(-335340,162540, 15, 4.38); */
360 /* fgSlewInit(-398673.28,120625.64, 53, 4.38); */
362 fgFlightModelInit(FG_LARCSIM, f, 1.0 / DEFAULT_MODEL_HZ);
364 /* build all objects */
367 /* initialize timer */
368 fgTimerInit( 1.0 / DEFAULT_MODEL_HZ );
371 /* call fgReshape() on window resizes */
372 glutReshapeFunc( fgReshape );
374 /* call key() on keyboard event */
375 glutKeyboardFunc( GLUTkey );
376 glutSpecialFunc( GLUTkey );
378 /* call fgMainLoop() whenever there is nothing else to do */
379 glutIdleFunc( fgMainLoop );
382 glutDisplayFunc( fgUpdateVisuals );
384 /* pass control off to the GLUT event handler */
387 /* call fgReshape() on expose events */
388 tkExposeFunc( fgReshape );
390 /* call fgReshape() on window resizes */
391 tkReshapeFunc( fgReshape );
393 /* call key() on keyboard event */
394 tkKeyDownFunc( GLTKkey );
396 /* call fgMainLoop() whenever there is nothing else to do */
397 tkIdleFunc( fgMainLoop );
400 tkDisplayFunc( fgUpdateVisuals );
402 /* pass control off to the tk event handler */
411 /* Revision 1.7 1997/05/29 22:39:49 curt
412 /* Working on incorporating the LaRCsim flight model.
414 * Revision 1.6 1997/05/29 12:31:39 curt
415 * Minor tweaks, moving towards general flight model integration.
417 * Revision 1.5 1997/05/29 02:33:23 curt
418 * Updated to reflect changing interfaces in other "modules."
420 * Revision 1.4 1997/05/27 17:44:31 curt
421 * Renamed & rearranged variables and routines. Added some initial simple
422 * timer/alarm routines so the flight model can be updated on a regular
425 * Revision 1.3 1997/05/23 15:40:25 curt
426 * Added GNU copyright headers.
429 * Revision 1.2 1997/05/23 00:35:12 curt
430 * Trying to get fog to work ...
432 * Revision 1.1 1997/05/21 15:57:51 curt
433 * Renamed due to added GLUT support.
435 * Revision 1.3 1997/05/19 18:22:42 curt
436 * Parameter tweaking ... starting to stub in fog support.
438 * Revision 1.2 1997/05/17 00:17:34 curt
439 * Trying to stub in support for standard OpenGL.
441 * Revision 1.1 1997/05/16 16:05:52 curt