]> git.mxchange.org Git - flightgear.git/blob - Main/GLUTmain.cxx
C++ - ifing the code.
[flightgear.git] / Main / GLUTmain.cxx
1 //
2 // GLUTmain.cxx -- top level sim routines
3 //
4 // Written by Curtis Olson for OpenGL, started May 1997.
5 //
6 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
7 //
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.
12 //
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.
17 //
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.
21 //
22 // $Id$
23 // (Log is kept at end of this file)
24
25
26 #include <config.h>
27
28 #ifdef HAVE_WINDOWS_H
29 #  include <windows.h>                     
30 #endif
31
32 #include <GL/glut.h>
33 #include <XGL/xgl.h>
34 #include <stdio.h>
35
36 #ifdef HAVE_STDLIB_H
37 #   include <stdlib.h>
38 #endif
39 #ifdef HAVE_GETOPT_H
40 #   include <getopt.h>
41 #endif
42
43 #include <Include/cmdargs.h>       // Line to command line arguments
44 #include <Include/fg_constants.h>  // for VERSION
45 #include <Include/general.h>
46
47 #include <Aircraft/aircraft.h>
48 #include <Astro/moon.hxx>
49 #include <Astro/planets.hxx>
50 #include <Astro/sky.hxx>
51 #include <Astro/stars.hxx>
52 #include <Astro/sun.hxx>
53 #include <Cockpit/cockpit.h>
54 #include <Debug/fg_debug.h>
55 #include <Joystick/joystick.h>
56 #include <Math/fg_geodesy.h>
57 #include <Math/mat3.h>
58 #include <Math/polar.h>
59 #include <Scenery/scenery.h>
60 #include <Scenery/tilemgr.hxx>
61 #include <Time/event.h>
62 #include <Time/fg_time.h>
63 #include <Time/fg_timer.h>
64 #include <Time/sunpos.hxx>
65 #include <Weather/weather.h>
66
67 #include "GLUTkey.hxx"
68 #include "fg_init.hxx"
69 #include "fg_getopt.h"
70 #include "views.hxx"
71
72
73 // This is a record containing global housekeeping information
74 fgGENERAL general;
75
76 // view parameters
77 static GLfloat win_ratio = 1.0;
78 static GLint winWidth, winHeight;
79
80 // Another hack
81 int use_signals = 0;
82
83 // Yet another hack. This one used by the HUD code. Michele
84 int show_hud;
85
86 // Yet another other hack. Used for my prototype instrument code. (Durk)
87 int displayInstruments; 
88
89 // The following defines flight gear options. Because glutlib will also
90 // want to parse its own options, those options must not be included here
91 // or they will get parsed by the main program option parser. Hence case
92 // is significant for any option added that might be in conflict with
93 // glutlib's parser.
94 //
95 // glutlib parses for:
96 //    -display
97 //    -direct   (invalid in Win32)
98 //    -geometry
99 //    -gldebug
100 //    -iconized
101 //    -indirect (invalid in Win32)
102 //    -synce
103 //
104 // Note that glutlib depends upon strings while this program's
105 // option parser wants only initial characters followed by numbers
106 // or pathnames.
107 //
108 const char *fg_cmdargopts = "a:c:Hhp:r:v:x:?";
109 //
110 // Where
111 //   -a aircraftfilename    aircraft start over ride
112 //   -c0x0000 - 0xffffffff  debug class setting
113 //    H,h.? help on command line use (does not need Option struct)
114 //   -p  priority
115 //   -r flightgear          root path to program support files
116 //   -v0 -v1                initial view mode (hud/no_hud currently)
117 //   -xlogpathname          debug logfile name
118 //
119 // Defaults in arguments to indicate not set on command line.
120 // Program defaults set variables from constants if neither command
121 // options or environmental variables effect values.
122 //
123
124 char  acArgbuf          [ MAXPATH + 1] = "\0";
125 int   debugArgValue                    = -2;
126 int   priorityArgValue                 = -1;
127 char  rootArgbuf        [ MAXPATH + 1] = "\0";
128 int   viewArg                          = -1;
129 char  logArgbuf         [ MAXPATH + 1] = "\0";
130
131 // There is a reason for defining the option structs by name and then
132 // creating an array of pointers to options. C++ is unfriendly to
133 // initializing arrays of objects that are not built in types. Always
134 // look forward. (Besides, you can follow what is going on better and
135 // add or modify with greater security. -ch
136 //
137 Option aircraftOption = { 'a',
138                           OPT_STRING,
139                           acArgbuf,
140                           "Startup aircraft pathname override"
141                         };
142 Option debugOption    = { 'c',
143                           OPT_LHEX,       // Long int (32 bits)
144                           &debugArgValue,
145                           "Debug trace level"
146                         };
147 Option priorityOption = { 'p',
148                           OPT_INTEGER,
149                           &priorityArgValue,
150                           "Debug priority Threshold"
151                         };
152 Option rootOption     = { 'r',
153                           OPT_STRING,
154                           rootArgbuf,
155                           "Root directory for execution"
156                         };
157 Option hudOption      = { 'v',
158                           OPT_INTEGER,
159                           &viewArg,
160                           "View mode start" // Naked,HUD,Panel,Chase,Tower...
161                         };
162
163 // Only naked view and HUD are implemented at this time
164 Option logfileOption  = { 'x',
165                           OPT_STRING,
166                           logArgbuf,
167                           "Debug log file name"
168                         };
169
170 #define OptsDefined 6
171 Option *CmdLineOptions[ OptsDefined ] = {
172   &aircraftOption,
173   &debugOption,
174   &hudOption,
175   &priorityOption,
176   &rootOption,
177   &logfileOption
178   };
179
180 const char *DefaultRootDir  = "\\Flightgear";
181 const char *DefaultAircraft = "Navion.acf";
182 const char *DefaultDebuglog = "fgdebug.log";
183 const int   DefaultViewMode = HUD_VIEW;
184
185 // Debug defaults handled in fg_debug.c
186
187 // fgInitVisuals() -- Initialize various GL/view parameters
188 static void fgInitVisuals( void ) {
189     struct fgLIGHT *l;
190     struct fgWEATHER *w;
191
192     l = &cur_light_params;
193     w = &current_weather;
194
195     // xglDisable( GL_DITHER );
196
197     // If enabled, normal vectors specified with glNormal are scaled
198     // to unit length after transformation.  See glNormal.
199     xglEnable( GL_NORMALIZE );
200
201     xglEnable( GL_LIGHTING );
202     xglEnable( GL_LIGHT0 );
203     xglLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec );
204
205     xglFogi (GL_FOG_MODE, GL_LINEAR);
206     xglFogf (GL_FOG_START, 10.0);
207     xglFogf (GL_FOG_END, w->visibility);
208     // xglFogf (GL_FOG_DENSITY, w->visibility);
209     xglHint (GL_FOG_HINT, GL_NICEST /* GL_FASTEST */ );
210
211     // draw wire frame
212     // xglPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
213 }
214
215
216 // Update the view volume, position, and orientation
217 static void fgUpdateViewParams( void ) {
218     fgFLIGHT *f;
219     struct fgLIGHT *l;
220     // struct fgTIME *t;
221     struct fgVIEW *v;
222
223     f = current_aircraft.flight;
224     l = &cur_light_params;
225     // t = &cur_time_params;
226     v = &current_view;
227
228     fgViewUpdate(f, v, l);
229
230     if (displayInstruments) {
231         xglViewport( 0, (GLint)(winHeight / 2 ) , 
232                      (GLint)winWidth, (GLint)winHeight / 2 );
233         // Tell GL we are about to modify the projection parameters
234         xglMatrixMode(GL_PROJECTION);
235         xglLoadIdentity();
236         gluPerspective(65.0, 2.0/win_ratio, 1.0, 100000.0);
237     } else {
238         xglViewport(0, 0 , (GLint)winWidth, (GLint) winHeight);
239         // Tell GL we are about to modify the projection parameters
240         xglMatrixMode(GL_PROJECTION);
241         xglLoadIdentity();
242         gluPerspective(65.0, 1.0/win_ratio, 10.0, 100000.0);
243     }
244
245     xglMatrixMode(GL_MODELVIEW);
246     xglLoadIdentity();
247     
248     // set up our view volume (default)
249     gluLookAt(v->view_pos.x, v->view_pos.y, v->view_pos.z,
250                v->view_pos.x + v->view_forward[0], 
251                v->view_pos.y + v->view_forward[1], 
252                v->view_pos.z + v->view_forward[2],
253                v->view_up[0], v->view_up[1], v->view_up[2]);
254
255     // look almost straight up (testing and eclipse watching)
256     /* gluLookAt(v->view_pos.x, v->view_pos.y, v->view_pos.z,
257                v->view_pos.x + v->view_up[0] + .001, 
258                v->view_pos.y + v->view_up[1] + .001, 
259                v->view_pos.z + v->view_up[2] + .001,
260                v->view_up[0], v->view_up[1], v->view_up[2]); */
261
262     // lock view horizontally towards sun (testing)
263     /* gluLookAt(v->view_pos.x, v->view_pos.y, v->view_pos.z,
264                v->view_pos.x + v->surface_to_sun[0], 
265                v->view_pos.y + v->surface_to_sun[1], 
266                v->view_pos.z + v->surface_to_sun[2],
267                v->view_up[0], v->view_up[1], v->view_up[2]); */
268
269     // lock view horizontally towards south (testing)
270     /* gluLookAt(v->view_pos.x, v->view_pos.y, v->view_pos.z,
271                v->view_pos.x + v->surface_south[0], 
272                v->view_pos.y + v->surface_south[1], 
273                v->view_pos.z + v->surface_south[2],
274                v->view_up[0], v->view_up[1], v->view_up[2]); */
275
276     // set the sun position
277     xglLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec );
278 }
279
280
281 // Draw a basic instrument panel
282 static void fgUpdateInstrViewParams( void ) {
283     xglViewport(0, 0 , (GLint)winWidth, (GLint)winHeight / 2);
284   
285     xglMatrixMode(GL_PROJECTION);
286     xglPushMatrix();
287   
288     xglLoadIdentity();
289     gluOrtho2D(0, 640, 0, 480);
290     xglMatrixMode(GL_MODELVIEW);
291     xglPushMatrix();
292     xglLoadIdentity();
293
294     xglColor3f(1.0, 1.0, 1.0);
295     xglIndexi(7);
296   
297     xglDisable(GL_DEPTH_TEST);
298     xglDisable(GL_LIGHTING);
299   
300     xglLineWidth(1);
301     xglColor3f (0.5, 0.5, 0.5);
302
303     xglBegin(GL_QUADS);
304     xglVertex2f(0.0, 0.00);
305     xglVertex2f(0.0, 480.0);
306     xglVertex2f(640.0,480.0);
307     xglVertex2f(640.0, 0.0);
308     xglEnd();
309
310     xglRectf(0.0,0.0, 640, 480);
311     xglEnable(GL_DEPTH_TEST);
312     xglEnable(GL_LIGHTING);
313     xglMatrixMode(GL_PROJECTION);
314     xglPopMatrix();
315     xglMatrixMode(GL_MODELVIEW);
316     xglPopMatrix();
317 }
318
319
320 // Update all Visuals (redraws anything graphics related)
321 static void fgRenderFrame( void ) {
322     struct fgLIGHT *l;
323     struct fgTIME *t;
324     struct fgVIEW *v;
325     double angle;
326     GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
327     GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
328
329     l = &cur_light_params;
330     t = &cur_time_params;
331     v = &current_view;
332
333     // update view volume parameters
334     fgUpdateViewParams();
335
336     xglClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
337
338     // Tell GL we are switching to model view parameters
339     xglMatrixMode(GL_MODELVIEW);
340     // xglLoadIdentity();
341
342     // draw sky
343     xglDisable( GL_DEPTH_TEST );
344     xglDisable( GL_LIGHTING );
345     xglDisable( GL_CULL_FACE );
346     xglDisable( GL_FOG );
347     xglShadeModel( GL_SMOOTH );
348     fgSkyRender();
349
350     // setup transformation for drawing astronomical objects
351     xglPushMatrix();
352     // Translate to view position
353     xglTranslatef( v->view_pos.x, v->view_pos.y, v->view_pos.z );
354     // Rotate based on gst (sidereal time)
355     angle = t->gst * 15.041085; /* should be 15.041085, Curt thought it was 15*/
356     // printf("Rotating astro objects by %.2f degrees\n",angle);
357     xglRotatef( angle, 0.0, 0.0, -1.0 );
358
359     // draw stars and planets
360     fgStarsRender();
361     fgPlanetsRender();
362
363     // draw the sun
364     fgSunRender();
365
366     // render the moon
367     xglEnable( GL_LIGHTING );
368     // set lighting parameters
369     xglLightfv(GL_LIGHT0, GL_AMBIENT, white );
370     xglLightfv(GL_LIGHT0, GL_DIFFUSE, white );
371     xglEnable( GL_CULL_FACE );
372     
373     // Let's try some blending technique's (Durk)
374     glEnable(GL_BLEND);
375     glBlendFunc(GL_ONE, GL_ONE);
376     fgMoonRender();
377     glDisable(GL_BLEND);
378
379     xglPopMatrix();
380
381     // draw scenery
382     xglShadeModel( /* GL_FLAT */ GL_SMOOTH ); 
383     xglEnable( GL_DEPTH_TEST );
384     xglEnable( GL_FOG );
385     xglFogfv (GL_FOG_COLOR, l->fog_color);
386     // set lighting parameters
387     xglLightfv(GL_LIGHT0, GL_AMBIENT, l->scene_ambient );
388     xglLightfv(GL_LIGHT0, GL_DIFFUSE, l->scene_diffuse );
389     // texture parameters
390     xglEnable( GL_TEXTURE_2D ); /* xglDisable( GL_TEXTURE_2D ); */
391     xglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ) ;
392     xglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) ;
393     xglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ) ;
394     xglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
395                       GL_LINEAR /* GL_LINEAR_MIPMAP_LINEAR */ ) ;
396     xglTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ) ;
397     xglHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ) ;
398     // set base color (I don't think this is doing anything here)
399     xglColor4fv(white);
400
401     fgTileMgrRender();
402
403     xglDisable( GL_TEXTURE_2D );
404
405     // display HUD
406     if( show_hud ) {
407         fgCockpitUpdate();
408     }
409
410     // display instruments
411     if (displayInstruments) {
412         fgUpdateInstrViewParams();
413     }
414
415     xglutSwapBuffers();
416 }
417
418
419 // Update internal time dependent calculations (i.e. flight model)
420 void fgUpdateTimeDepCalcs(int multi_loop) {
421     fgFLIGHT *f;
422     struct fgTIME *t;
423     struct fgVIEW *v;
424     int i;
425
426     f = current_aircraft.flight;
427     t = &cur_time_params;
428     v = &current_view;
429
430     // update the flight model
431     if ( multi_loop < 0 ) {
432         multi_loop = DEFAULT_MULTILOOP;
433     }
434
435     // printf("updating flight model x %d\n", multi_loop);
436     fgFlightModelUpdate(FG_LARCSIM, f, multi_loop);
437
438     // update the view angle
439     for ( i = 0; i < multi_loop; i++ ) {
440         if ( fabs(v->goal_view_offset - v->view_offset) < 0.05 ) {
441             v->view_offset = v->goal_view_offset;
442             break;
443         } else {
444             // move v->view_offset towards v->goal_view_offset
445             if ( v->goal_view_offset > v->view_offset ) {
446                 if ( v->goal_view_offset - v->view_offset < FG_PI ) {
447                     v->view_offset += 0.01;
448                 } else {
449                     v->view_offset -= 0.01;
450                 }
451             } else {
452                 if ( v->view_offset - v->goal_view_offset < FG_PI ) {
453                     v->view_offset -= 0.01;
454                 } else {
455                     v->view_offset += 0.01;
456                 }
457             }
458             if ( v->view_offset > FG_2PI ) {
459                 v->view_offset -= FG_2PI;
460             } else if ( v->view_offset < 0 ) {
461                 v->view_offset += FG_2PI;
462             }
463         }
464     }
465 }
466
467
468 void fgInitTimeDepCalcs( void ) {
469     // initialize timer
470
471 #ifdef HAVE_SETITIMER
472     fgTimerInit( 1.0 / DEFAULT_TIMER_HZ, fgUpdateTimeDepCalcs );
473 #endif HAVE_SETITIMER
474
475 }
476
477
478 // Scenery management routines
479
480 /* static void fgSceneryInit_OLD() { */
481     /* make scenery */
482 /*     scenery = fgSceneryCompile_OLD();
483     runway = fgRunwayHack_OLD(0.69, 53.07);
484 } */
485
486
487 /* create the scenery */
488 /* GLint fgSceneryCompile_OLD() {
489     GLint scenery;
490
491     scenery = mesh2GL(mesh_ptr_OLD);
492
493     return(scenery);
494 }
495 */
496
497 /* hack in a runway */
498 /* GLint fgRunwayHack_OLD(double width, double length) {
499     static GLfloat concrete[4] = { 0.5, 0.5, 0.5, 1.0 };
500     static GLfloat line[4]     = { 0.9, 0.9, 0.9, 1.0 };
501     int i;
502     int num_lines = 16;
503     float line_len, line_width_2, cur_pos;
504
505     runway = xglGenLists(1);
506     xglNewList(runway, GL_COMPILE);
507     */
508     /* draw concrete */
509 /*    xglBegin(GL_POLYGON);
510     xglMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, concrete );
511     xglNormal3f(0.0, 0.0, 1.0);
512
513     xglVertex3d( 0.0,   -width/2.0, 0.0);
514     xglVertex3d( 0.0,    width/2.0, 0.0);
515     xglVertex3d(length,  width/2.0, 0.0);
516     xglVertex3d(length, -width/2.0, 0.0);
517     xglEnd();
518     */
519     /* draw center line */
520 /*    xglMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, line );
521     line_len = length / ( 2 * num_lines + 1);
522     printf("line_len = %.3f\n", line_len);
523     line_width_2 = 0.02;
524     cur_pos = line_len;
525     for ( i = 0; i < num_lines; i++ ) {
526         xglBegin(GL_POLYGON);
527         xglVertex3d( cur_pos, -line_width_2, 0.005);
528         xglVertex3d( cur_pos,  line_width_2, 0.005);
529         cur_pos += line_len;
530         xglVertex3d( cur_pos,  line_width_2, 0.005);
531         xglVertex3d( cur_pos, -line_width_2, 0.005);
532         cur_pos += line_len;
533         xglEnd();
534     }
535
536     xglEndList();
537
538     return(runway);
539 }
540 */
541
542 /* draw the scenery */
543 /*static void fgSceneryDraw_OLD() {
544     static float z = 32.35;
545
546     xglPushMatrix();
547
548     xglCallList(scenery);
549
550     printf("*** Drawing runway at %.2f\n", z);
551
552     xglTranslatef( -398391.28, 120070.41, 32.35);
553     xglRotatef(170.0, 0.0, 0.0, 1.0);
554     xglCallList(runway);
555
556     xglPopMatrix();
557 }
558 */
559
560
561 // What should we do when we have nothing else to do?  How about get
562 // ready for the next move and update the display?
563 static void fgMainLoop( void ) {
564     static int remainder = 0;
565     int elapsed, multi_loop;
566     double cur_elev;
567     // double joy_x, joy_y;
568     // int joy_b1, joy_b2;
569     fgAIRCRAFT *a;
570     fgFLIGHT *f;
571     struct fgTIME *t;
572
573     fgPrintf( FG_ALL, FG_DEBUG, "Running Main Loop\n");
574     fgPrintf( FG_ALL, FG_DEBUG, "======= ==== ====\n");
575
576     a = &current_aircraft;
577     f = a->flight;
578     t = &cur_time_params;
579
580     // update "time"
581     fgTimeUpdate(f, t);
582
583     // Read joystick
584     /* fgJoystickRead( &joy_x, &joy_y, &joy_b1, &joy_b2 );
585     printf( "Joystick X %f  Y %f  B1 %d  B2 %d\n",  
586             joy_x, joy_y, joy_b1, joy_b2 );
587     fgElevSet( -joy_y );
588     fgAileronSet( joy_x ); */
589
590     // Calculate model iterations needed
591     elapsed = fgGetTimeInterval();
592     fgPrintf( FG_ALL, FG_BULK, 
593               "Time interval is = %d, previous remainder is = %d\n", 
594               elapsed, remainder);
595     fgPrintf( FG_ALL, FG_BULK, 
596               "--> Frame rate is = %.2f\n", 1000.0 / (float)elapsed);
597     elapsed += remainder;
598
599     multi_loop = (int)(((float)elapsed * 0.001) * DEFAULT_MODEL_HZ);
600     remainder = elapsed - ((multi_loop*1000) / DEFAULT_MODEL_HZ);
601     fgPrintf( FG_ALL, FG_BULK, 
602               "Model iterations needed = %d, new remainder = %d\n", 
603               multi_loop, remainder);
604         
605     // Run flight model
606     if ( ! use_signals ) {
607         // flight model
608         fgUpdateTimeDepCalcs(multi_loop);
609     }
610
611     // I'm just sticking this here for now, it should probably move
612     // eventually
613     /* cur_elev = mesh_altitude(FG_Longitude * RAD_TO_ARCSEC, 
614                                FG_Latitude  * RAD_TO_ARCSEC); */
615     // there is no ground collision detection really, so for now I
616     // just hard code the ground elevation to be 0 */
617     cur_elev = 0;
618
619     // printf("Ground elevation is %.2f meters here.\n", cur_elev);
620     // FG_Runway_altitude = cur_elev * METER_TO_FEET;
621
622     if ( FG_Altitude * FEET_TO_METER < cur_elev + 3.758099) {
623         // set this here, otherwise if we set runway height above our
624         // current height we get a really nasty bounce.
625         FG_Runway_altitude = FG_Altitude - 3.758099;
626
627         // now set aircraft altitude above ground
628         FG_Altitude = cur_elev * METER_TO_FEET + 3.758099;
629         fgPrintf( FG_ALL, FG_BULK, "<*> resetting altitude to %.0f meters\n", 
630                FG_Altitude * FEET_TO_METER);
631     }
632
633     fgAircraftOutputCurrent(a);
634
635     // see if we need to load any new scenery tiles
636     fgTileMgrUpdate();
637
638     // Process/manage pending events
639     fgEventProcess();
640
641     // redraw display
642     fgRenderFrame();
643
644     fgPrintf( FG_ALL, FG_DEBUG, "\n");
645 }
646
647
648 // Handle new window size or exposure
649 static void fgReshape( int width, int height ) {
650     // Do this so we can call fgReshape(0,0) ourselves without having
651     // to know what the values of width & height are.
652     if ( (height > 0) && (width > 0) ) {
653         win_ratio = (GLfloat) height / (GLfloat) width;
654     }
655
656     winWidth = width;
657     winHeight = height;
658
659     // Inform gl of our view window size (now handled elsewhere)
660     // xglViewport(0, 0, (GLint)width, (GLint)height);
661     fgUpdateViewParams();
662     
663     // xglClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
664 }
665
666
667 // Main ...
668 int main( int argc, char *argv[] ) {
669     fgFLIGHT *f;
670     int parse_result;  // Used in command line argument.
671
672     f = current_aircraft.flight;
673     //  First things first... We must have startup options dealt with.
674
675     printf("Flight Gear:  Version %s\n\n", VERSION);
676
677     // Initialize the Window/Graphics environment.
678
679     // initialize GLUT
680     xglutInit(&argc, argv);
681
682     // Define Display Parameters
683     xglutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
684
685     // Define initial window size
686     xglutInitWindowSize(640, 480);
687
688     // Initialize windows
689     xglutCreateWindow("Flight Gear");
690
691     // xglutInit above will extract all non-general program command line.
692     // We only need wory about our own.
693
694     parse_result = getargs( argc, argv, OptsDefined, CmdLineOptions, NULL);
695
696     switch( parse_result ) {
697     case ALLDONE:
698         break;
699
700     case HELP:
701         print_desc( OptsDefined, CmdLineOptions );
702         exit(0);
703
704     case INVALID:
705     default:
706         printf( "Flight Gear: Command line invalid.");
707         exit(0);
708     }
709
710     // Deal with the effects of options no set by manipulating the command
711     // line, or possibly set to invalid states.
712
713     if(( viewArg >= 0) && (viewArg <= 1)) {
714         show_hud = viewArg; // For now view_mode TRUE - no HUD, else show_hud.
715     } else {
716         show_hud = DefaultViewMode;
717     }
718
719     // All other command line option responses are handled in the various
720     // initialization routines (or ignored if not implemented.
721
722     // This is the general house keeping init routine. It initializes the
723     // debug trail scheme and then any other stuff.
724
725     if( !fgInitGeneral()) {
726
727         // This is the top level init routine which calls all the other
728         // subsystem initialization routines.  If you are adding a
729         // subsystem to flight gear, its initialization call should
730         // located in this routine.
731         if( !fgInitSubsystems()) {
732
733             // setup view parameters, only makes GL calls
734             fgInitVisuals();
735
736             if ( use_signals ) {
737                 // init timer routines, signals, etc.  Arrange for an
738                 // alarm signal to be generated, etc.
739                 fgInitTimeDepCalcs();
740             }
741
742             // Initialize the GLUT Event Handlers.
743
744             // call fgReshape() on window resizes
745             xglutReshapeFunc( fgReshape );
746
747             // call key() on keyboard event
748             xglutKeyboardFunc( GLUTkey );
749             glutSpecialFunc( GLUTspecialkey );
750
751             // call fgMainLoop() whenever there is
752             // nothing else to do
753             xglutIdleFunc( fgMainLoop );
754
755             // draw the scene
756             xglutDisplayFunc( fgRenderFrame );
757
758             // pass control off to the GLUT event handler
759             glutMainLoop();
760
761         }  // End if subsystems initialize ok
762     } // End if general initializations went ok
763
764     if( fg_DebugOutput ) {
765         fclose( fg_DebugOutput );
766     }
767     return(0);
768 }
769
770
771 #ifdef __SUNPRO_CC
772 extern "C" {
773     void __eprintf( void ) {
774     }
775 }
776 #endif
777
778
779 // $Log$
780 // Revision 1.2  1998/04/22 13:25:41  curt
781 // C++ - ifing the code.
782 // Starting a bit of reorganization of lighting code.
783 //
784 // Revision 1.1  1998/04/21 17:02:39  curt
785 // Prepairing for C++ integration.
786 //
787 // Revision 1.71  1998/04/18 04:11:26  curt
788 // Moved fg_debug to it's own library, added zlib support.
789 //
790 // Revision 1.70  1998/04/14 02:21:02  curt
791 // Incorporated autopilot heading hold contributed by:  Jeff Goeke-Smith
792 // <jgoeke@voyager.net>
793 //
794 // Revision 1.69  1998/04/08 23:35:34  curt
795 // Tweaks to Gnu automake/autoconf system.
796 //
797 // Revision 1.68  1998/04/03 22:09:03  curt
798 // Converting to Gnu autoconf system.
799 //
800 // Revision 1.67  1998/03/23 21:24:37  curt
801 // Source code formating tweaks.
802 //
803 // Revision 1.66  1998/03/14 00:31:20  curt
804 // Beginning initial terrain texturing experiments.
805 //
806 // Revision 1.65  1998/03/09 22:45:57  curt
807 // Minor tweaks for building on sparc platform.
808 //
809 // Revision 1.64  1998/02/20 00:16:23  curt
810 // Thursday's tweaks.
811 //
812 // Revision 1.63  1998/02/16 16:17:39  curt
813 // Minor tweaks.
814 //
815 // Revision 1.62  1998/02/16 13:39:42  curt
816 // Miscellaneous weekend tweaks.  Fixed? a cache problem that caused whole
817 // tiles to occasionally be missing.
818 //
819 // Revision 1.61  1998/02/12 21:59:46  curt
820 // Incorporated code changes contributed by Charlie Hotchkiss
821 // <chotchkiss@namg.us.anritsu.com>
822 //
823 // Revision 1.60  1998/02/11 02:50:40  curt
824 // Minor changes.
825 //
826 // Revision 1.59  1998/02/09 22:56:54  curt
827 // Removed "depend" files from cvs control.  Other minor make tweaks.
828 //
829 // Revision 1.58  1998/02/09 15:07:49  curt
830 // Minor tweaks.
831 //
832 // Revision 1.57  1998/02/07 15:29:40  curt
833 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
834 // <chotchkiss@namg.us.anritsu.com>
835 //
836 // Revision 1.56  1998/02/03 23:20:23  curt
837 // Lots of little tweaks to fix various consistency problems discovered by
838 // Solaris' CC.  Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
839 // passed arguments along to the real printf().  Also incorporated HUD changes
840 // by Michele America.
841 //
842 // Revision 1.55  1998/02/02 20:53:58  curt
843 // Incorporated Durk's changes.
844 //
845 // Revision 1.54  1998/01/31 00:43:10  curt
846 // Added MetroWorks patches from Carmen Volpe.
847 //
848 // Revision 1.53  1998/01/27 18:35:54  curt
849 // Minor tweaks.
850 //
851 // Revision 1.52  1998/01/27 00:47:56  curt
852 // Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
853 // system and commandline/config file processing code.
854 //
855 // Revision 1.51  1998/01/26 15:57:05  curt
856 // Tweaks for dynamic scenery development.
857 //
858 // Revision 1.50  1998/01/19 19:27:07  curt
859 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
860 // This should simplify things tremendously.
861 //
862 // Revision 1.49  1998/01/19 18:40:31  curt
863 // Tons of little changes to clean up the code and to remove fatal errors
864 // when building with the c++ compiler.
865 //
866 // Revision 1.48  1998/01/19 18:35:46  curt
867 // Minor tweaks and fixes for cygwin32.
868 //
869 // Revision 1.47  1998/01/13 00:23:08  curt
870 // Initial changes to support loading and management of scenery tiles.  Note,
871 // there's still a fair amount of work left to be done.
872 //
873 // Revision 1.46  1998/01/08 02:22:06  curt
874 // Beginning to integrate Tile management subsystem.
875 //
876 // Revision 1.45  1998/01/07 03:18:55  curt
877 // Moved astronomical stuff from .../Src/Scenery to .../Src/Astro/
878 //
879 // Revision 1.44  1997/12/30 22:22:31  curt
880 // Further integration of event manager.
881 //
882 // Revision 1.43  1997/12/30 20:47:43  curt
883 // Integrated new event manager with subsystem initializations.
884 //
885 // Revision 1.42  1997/12/30 16:36:47  curt
886 // Merged in Durk's changes ...
887 //
888 // Revision 1.41  1997/12/30 13:06:56  curt
889 // A couple lighting tweaks ...
890 //
891 // Revision 1.40  1997/12/30 01:38:37  curt
892 // Switched back to per vertex normals and smooth shading for terrain.
893 //
894 // Revision 1.39  1997/12/22 23:45:45  curt
895 // First stab at sunset/sunrise sky glow effects.
896 //
897 // Revision 1.38  1997/12/22 04:14:28  curt
898 // Aligned sky with sun so dusk/dawn effects can be correct relative to the sun.
899 //
900 // Revision 1.37  1997/12/19 23:34:03  curt
901 // Lot's of tweaking with sky rendering and lighting.
902 //
903 // Revision 1.36  1997/12/19 16:44:57  curt
904 // Working on scene rendering order and options.
905 //
906 // Revision 1.35  1997/12/18 23:32:32  curt
907 // First stab at sky dome actually starting to look reasonable. :-)
908 //
909 // Revision 1.34  1997/12/17 23:13:34  curt
910 // Began working on rendering a sky.
911 //
912 // Revision 1.33  1997/12/15 23:54:45  curt
913 // Add xgl wrappers for debugging.
914 // Generate terrain normals on the fly.
915 //
916 // Revision 1.32  1997/12/15 20:59:08  curt
917 // Misc. tweaks.
918 //
919 // Revision 1.31  1997/12/12 21:41:25  curt
920 // More light/material property tweaking ... still a ways off.
921 //
922 // Revision 1.30  1997/12/12 19:52:47  curt
923 // Working on lightling and material properties.
924 //
925 // Revision 1.29  1997/12/11 04:43:54  curt
926 // Fixed sun vector and lighting problems.  I thing the moon is now lit
927 // correctly.
928 //
929 // Revision 1.28  1997/12/10 22:37:45  curt
930 // Prepended "fg" on the name of all global structures that didn't have it yet.
931 // i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
932 //
933 // Revision 1.27  1997/12/09 05:11:54  curt
934 // Working on tweaking lighting.
935 //
936 // Revision 1.26  1997/12/09 04:25:29  curt
937 // Working on adding a global lighting params structure.
938 //
939 // Revision 1.25  1997/12/08 22:54:09  curt
940 // Enabled GL_CULL_FACE.
941 //
942 // Revision 1.24  1997/11/25 19:25:32  curt
943 // Changes to integrate Durk's moon/sun code updates + clean up.
944 //
945 // Revision 1.23  1997/11/15 18:16:34  curt
946 // minor tweaks.
947 //
948 // Revision 1.22  1997/10/30 12:38:41  curt
949 // Working on new scenery subsystem.
950 //
951 // Revision 1.21  1997/09/23 00:29:38  curt
952 // Tweaks to get things to compile with gcc-win32.
953 //
954 // Revision 1.20  1997/09/22 14:44:19  curt
955 // Continuing to try to align stars correctly.
956 //
957 // Revision 1.19  1997/09/18 16:20:08  curt
958 // At dusk/dawn add/remove stars in stages.
959 //
960 // Revision 1.18  1997/09/16 22:14:51  curt
961 // Tweaked time of day lighting equations.  Don't draw stars during the day.
962 //
963 // Revision 1.17  1997/09/16 15:50:29  curt
964 // Working on star alignment and time issues.
965 //
966 // Revision 1.16  1997/09/13 02:00:06  curt
967 // Mostly working on stars and generating sidereal time for accurate star
968 // placement.
969 //
970 // Revision 1.15  1997/09/05 14:17:27  curt
971 // More tweaking with stars.
972 //
973 // Revision 1.14  1997/09/05 01:35:53  curt
974 // Working on getting stars right.
975 //
976 // Revision 1.13  1997/09/04 02:17:34  curt
977 // Shufflin' stuff.
978 //
979 // Revision 1.12  1997/08/27 21:32:24  curt
980 // Restructured view calculation code.  Added stars.
981 //
982 // Revision 1.11  1997/08/27 03:30:16  curt
983 // Changed naming scheme of basic shared structures.
984 //
985 // Revision 1.10  1997/08/25 20:27:22  curt
986 // Merged in initial HUD and Joystick code.
987 //
988 // Revision 1.9  1997/08/22 21:34:39  curt
989 // Doing a bit of reorganizing and house cleaning.
990 //
991 // Revision 1.8  1997/08/19 23:55:03  curt
992 // Worked on better simulating real lighting.
993 //
994 // Revision 1.7  1997/08/16 12:22:38  curt
995 // Working on improving the lighting/shading.
996 //
997 // Revision 1.6  1997/08/13 20:24:56  curt
998 // Changes due to changing sunpos interface.
999 //
1000 // Revision 1.5  1997/08/06 21:08:32  curt
1001 // Sun position now//really* works (I think) ... I still have sun time warping
1002 // code in place, probably should remove it soon.
1003 //
1004 // Revision 1.4  1997/08/06 15:41:26  curt
1005 // Working on correct sun position.
1006 //
1007 // Revision 1.3  1997/08/06 00:24:22  curt
1008 // Working on correct real time sun lighting.
1009 //
1010 // Revision 1.2  1997/08/04 20:25:15  curt
1011 // Organizational tweaking.
1012 //
1013 // Revision 1.1  1997/08/02 18:45:00  curt
1014 // Renamed GLmain.c GLUTmain.c
1015 //
1016 // Revision 1.43  1997/08/02 16:23:47  curt
1017 // Misc. tweaks.
1018 //
1019 // Revision 1.42  1997/08/01 19:43:33  curt
1020 // Making progress with coordinate system overhaul.
1021 //
1022 // Revision 1.41  1997/07/31 22:52:37  curt
1023 // Working on redoing internal coordinate systems & scenery transformations.
1024 //
1025 // Revision 1.40  1997/07/30 16:12:42  curt
1026 // Moved fg_random routines from Util/ to Math/
1027 //
1028 // Revision 1.39  1997/07/21 14:45:01  curt
1029 // Minor tweaks.
1030 //
1031 // Revision 1.38  1997/07/19 23:04:47  curt
1032 // Added an initial weather section.
1033 //
1034 // Revision 1.37  1997/07/19 22:34:02  curt
1035 // Moved PI definitions to ../constants.h
1036 // Moved random() stuff to ../Utils/ and renamed fg_random()
1037 //
1038 // Revision 1.36  1997/07/18 23:41:25  curt
1039 // Tweaks for building with Cygnus Win32 compiler.
1040 //
1041 // Revision 1.35  1997/07/18 14:28:34  curt
1042 // Hacked in some support for wind/turbulence.
1043 //
1044 // Revision 1.34  1997/07/16 20:04:48  curt
1045 // Minor tweaks to aid Win32 port.
1046 //
1047 // Revision 1.33  1997/07/12 03:50:20  curt
1048 // Added an #include <Windows32/Base.h> to help compiling for Win32
1049 //
1050 // Revision 1.32  1997/07/11 03:23:18  curt
1051 // Solved some scenery display/orientation problems.  Still have a positioning
1052 // (or transformation?) problem.
1053 //
1054 // Revision 1.31  1997/07/11 01:29:58  curt
1055 // More tweaking of terrian floor.
1056 //
1057 // Revision 1.30  1997/07/10 04:26:37  curt
1058 // We now can interpolated ground elevation for any position in the grid.  We
1059 // can use this to enforce a "hard" ground.  We still need to enforce some
1060 // bounds checking so that we don't try to lookup data points outside the
1061 // grid data set.
1062 //
1063 // Revision 1.29  1997/07/09 21:31:12  curt
1064 // Working on making the ground "hard."
1065 //
1066 // Revision 1.28  1997/07/08 18:20:12  curt
1067 // Working on establishing a hard ground.
1068 //
1069 // Revision 1.27  1997/07/07 20:59:49  curt
1070 // Working on scenery transformations to enable us to fly fluidly over the
1071 // poles with no discontinuity/distortion in scenery.
1072 //
1073 // Revision 1.26  1997/07/05 20:43:34  curt
1074 // renamed mat3 directory to Math so we could add other math related routines.
1075 //
1076 // Revision 1.25  1997/06/29 21:19:17  curt
1077 // Working on scenery management system.
1078 //
1079 // Revision 1.24  1997/06/26 22:14:53  curt
1080 // Beginning work on a scenery management system.
1081 //
1082 // Revision 1.23  1997/06/26 19:08:33  curt
1083 // Restructuring make, adding automatic "make dep" support.
1084 //
1085 // Revision 1.22  1997/06/25 15:39:47  curt
1086 // Minor changes to compile with rsxnt/win32.
1087 //
1088 // Revision 1.21  1997/06/22 21:44:41  curt
1089 // Working on intergrating the VRML (subset) parser.
1090 //
1091 // Revision 1.20  1997/06/21 17:12:53  curt
1092 // Capitalized subdirectory names.
1093 //
1094 // Revision 1.19  1997/06/18 04:10:31  curt
1095 // A couple more runway tweaks ...
1096 //
1097 // Revision 1.18  1997/06/18 02:21:24  curt
1098 // Hacked in a runway
1099 //
1100 // Revision 1.17  1997/06/17 16:51:58  curt
1101 // Timer interval stuff now uses gettimeofday() instead of ftime()
1102 //
1103 // Revision 1.16  1997/06/17 04:19:16  curt
1104 // More timer related tweaks with respect to view direction changes.
1105 //
1106 // Revision 1.15  1997/06/17 03:41:10  curt
1107 // Nonsignal based interval timing is now working.
1108 // This would be a good time to look at cleaning up the code structure a bit.
1109 //
1110 // Revision 1.14  1997/06/16 19:32:51  curt
1111 // Starting to add general timer support.
1112 //
1113 // Revision 1.13  1997/06/02 03:40:06  curt
1114 // A tiny bit more view tweaking.
1115 //
1116 // Revision 1.12  1997/06/02 03:01:38  curt
1117 // Working on views (side, front, back, transitions, etc.)
1118 //
1119 // Revision 1.11  1997/05/31 19:16:25  curt
1120 // Elevator trim added.
1121 //
1122 // Revision 1.10  1997/05/31 04:13:52  curt
1123 // WE CAN NOW FLY!!!
1124 //
1125 // Continuing work on the LaRCsim flight model integration.
1126 // Added some MSFS-like keyboard input handling.
1127 //
1128 // Revision 1.9  1997/05/30 19:27:01  curt
1129 // The LaRCsim flight model is starting to look like it is working.
1130 //
1131 // Revision 1.8  1997/05/30 03:54:10  curt
1132 // Made a bit more progress towards integrating the LaRCsim flight model.
1133 //
1134 // Revision 1.7  1997/05/29 22:39:49  curt
1135 // Working on incorporating the LaRCsim flight model.
1136 //
1137 // Revision 1.6  1997/05/29 12:31:39  curt
1138 // Minor tweaks, moving towards general flight model integration.
1139 //
1140 // Revision 1.5  1997/05/29 02:33:23  curt
1141 // Updated to reflect changing interfaces in other "modules."
1142 //
1143 // Revision 1.4  1997/05/27 17:44:31  curt
1144 // Renamed & rearranged variables and routines.   Added some initial simple
1145 // timer/alarm routines so the flight model can be updated on a regular 
1146 // interval.
1147 //
1148 // Revision 1.3  1997/05/23 15:40:25  curt
1149 // Added GNU copyright headers.
1150 // Fog now works!
1151 //
1152 // Revision 1.2  1997/05/23 00:35:12  curt
1153 // Trying to get fog to work ...
1154 //
1155 // Revision 1.1  1997/05/21 15:57:51  curt
1156 // Renamed due to added GLUT support.
1157 //
1158 // Revision 1.3  1997/05/19 18:22:42  curt
1159 // Parameter tweaking ... starting to stub in fog support.
1160 //
1161 // Revision 1.2  1997/05/17 00:17:34  curt
1162 // Trying to stub in support for standard OpenGL.
1163 //
1164 // Revision 1.1  1997/05/16 16:05:52  curt
1165 // Initial revision.
1166 //