]> git.mxchange.org Git - flightgear.git/blob - Main/GLUTmain.c
Worked on better simulating real lighting.
[flightgear.git] / Main / GLUTmain.c
1 /**************************************************************************
2  * GLmain.c -- 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
27 #include <stdio.h>
28
29 #ifdef WIN32
30 #  include <windows.h>                     
31 #endif
32
33 #ifdef GLUT
34     #include <GL/glut.h>
35     #include "GLUTkey.h"
36 #endif
37
38 #include "../constants.h"
39
40 #include "../Aircraft/aircraft.h"
41 #include "../Scenery/mesh.h"
42 #include "../Scenery/scenery.h"
43 #include "../Math/fg_geodesy.h"
44 #include "../Math/fg_random.h"
45 #include "../Math/mat3.h"
46 #include "../Math/polar.h"
47 #include "../Time/fg_time.h"
48 #include "../Time/fg_timer.h"
49 #include "../Time/sunpos.h"
50 #include "../Weather/weather.h"
51
52
53 /* This is a record containing all the info for the aircraft currently
54    being operated */
55 struct aircraft_params current_aircraft;
56
57 /* view parameters */
58 static GLfloat win_ratio = 1.0;
59
60 /* sun direction */
61 static GLfloat sun_vec[4] = {1.0, 0.0, 0.0, 0.0 };
62
63 /* if the 4th field is 0.0, this specifies a direction ... */
64 /* clear color (sky) */
65 static GLfloat fgClearColor[4] = {0.60, 0.60, 0.90, 1.0};
66 /* fog color */
67 static GLfloat fgFogColor[4] =   {0.65, 0.65, 0.85, 1.0};
68
69 /* temporary hack */
70 /* extern struct mesh *mesh_ptr; */
71 /* Function prototypes */
72 /* GLint fgSceneryCompile_OLD(); */
73 /* static void fgSceneryDraw_OLD(); */
74
75 /* pointer to scenery structure */
76 /* static GLint scenery, runway; */
77
78 /* Another hack */
79 double fogDensity = 60000.0; /* in meters */
80 double view_offset = 0.0;
81 double goal_view_offset = 0.0;
82
83 /* Another hack */
84 #define DEFAULT_TIMER_HZ 20
85 #define DEFAULT_MULTILOOP 6
86 #define DEFAULT_MODEL_HZ (DEFAULT_TIMER_HZ * DEFAULT_MULTILOOP)
87
88 double Simtime;
89
90 /* Another hack */
91 int use_signals = 0;
92
93
94 /**************************************************************************
95  * fgInitVisuals() -- Initialize various GL/view parameters
96  **************************************************************************/
97
98 static void fgInitVisuals() {
99     glEnable( GL_DEPTH_TEST );
100     /* glFrontFace(GL_CW); */
101     glEnable( GL_CULL_FACE );
102
103     /* If enabled, normal vectors specified with glNormal are scaled
104        to unit length after transformation.  See glNormal. */
105     glEnable( GL_NORMALIZE );
106
107     glLightfv( GL_LIGHT0, GL_POSITION, sun_vec );
108     glEnable( GL_LIGHTING );
109     glEnable( GL_LIGHT0 );
110
111     glShadeModel( GL_FLAT ); /* glShadeModel( GL_SMOOTH ); */
112
113     glEnable( GL_FOG );
114     glFogi (GL_FOG_MODE, GL_LINEAR);
115     /* glFogf (GL_FOG_START, 1.0); */
116     glFogf (GL_FOG_END, fogDensity);
117     glFogfv (GL_FOG_COLOR, fgFogColor);
118     /* glFogf (GL_FOG_DENSITY, fogDensity); */
119     /* glHint (GL_FOG_HINT, GL_FASTEST); */
120
121     glClearColor(fgClearColor[0], fgClearColor[1], fgClearColor[2], 
122                  fgClearColor[3]);
123 }
124
125
126 /**************************************************************************
127  * Update the view volume, position, and orientation
128  **************************************************************************/
129
130 static void fgUpdateViewParams() {
131     struct fgCartesianPoint view_pos /*, alt_up */;
132     struct flight_params *f;
133     struct time_params *t;
134     MAT3mat R, TMP, UP, LOCAL, VIEW;
135     MAT3vec vec, view_up, forward, view_forward, local_up, nup, nsun;
136     double sun_angle, temp, ambient, diffuse, sky;
137     GLfloat color[4] = { 1.0, 1.0, 0.50, 1.0 };
138     GLfloat amb[3], diff[3], fog[4], clear[4];
139
140     f = &current_aircraft.flight;
141     t = &cur_time_params;
142
143     /* Tell GL we are about to modify the projection parameters */
144     glMatrixMode(GL_PROJECTION);
145     glLoadIdentity();
146     gluPerspective(45.0, 1.0/win_ratio, 1.0, 200000.0);
147
148     glMatrixMode(GL_MODELVIEW);
149     glLoadIdentity();
150     
151     /* calculate view position in current FG view coordinate system */
152     view_pos = fgPolarToCart(FG_Longitude, FG_Lat_geocentric, 
153                              FG_Radius_to_vehicle * FEET_TO_METER + 1.0);
154     view_pos.x -= scenery.center.x;
155     view_pos.y -= scenery.center.y;
156     view_pos.z -= scenery.center.z;
157
158     printf("View pos = %.4f, %.4f, %.4f\n", view_pos.x, view_pos.y, view_pos.z);
159
160     /* Derive the LOCAL aircraft rotation matrix (roll, pitch, yaw) */
161     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
162     MAT3rotate(R, vec, FG_Phi);
163     /* printf("Roll matrix\n"); */
164     /* MAT3print(R, stdout); */
165
166     MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
167     /* MAT3mult_vec(vec, vec, R); */
168     MAT3rotate(TMP, vec, FG_Theta);
169     /* printf("Pitch matrix\n"); */
170     /* MAT3print(TMP, stdout); */
171     MAT3mult(R, R, TMP);
172
173     MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
174     /* MAT3mult_vec(vec, vec, R); */
175     /* MAT3rotate(TMP, vec, FG_Psi - FG_PI_2); */
176     MAT3rotate(TMP, vec, -FG_Psi);
177     /* printf("Yaw matrix\n");
178     MAT3print(TMP, stdout); */
179     MAT3mult(LOCAL, R, TMP);
180     /* printf("LOCAL matrix\n"); */
181     /* MAT3print(LOCAL, stdout); */
182
183     /* Derive the local UP transformation matrix based on *geodetic*
184      * coordinates */
185     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
186     MAT3rotate(R, vec, FG_Longitude);     /* R = rotate about Z axis */
187     /* printf("Longitude matrix\n"); */
188     /* MAT3print(R, stdout); */
189
190     MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
191     MAT3mult_vec(vec, vec, R);
192     MAT3rotate(TMP, vec, -FG_Latitude);  /* TMP = rotate about X axis */
193     /* printf("Latitude matrix\n"); */
194     /* MAT3print(TMP, stdout); */
195
196     MAT3mult(UP, R, TMP);
197     /* printf("Local up matrix\n"); */
198     /* MAT3print(UP, stdout); */
199
200     MAT3_SET_VEC(local_up, 1.0, 0.0, 0.0);
201     MAT3mult_vec(local_up, local_up, UP);
202
203     printf("    Local Up = (%.4f, %.4f, %.4f)\n", local_up[0], local_up[1], 
204            local_up[2]);
205     
206     /* Alternative method to Derive local up vector based on
207      * *geodetic* coordinates */
208     /* alt_up = fgPolarToCart(FG_Longitude, FG_Latitude, 1.0); */
209     /* printf("    Alt Up = (%.4f, %.4f, %.4f)\n", 
210        alt_up.x, alt_up.y, alt_up.z); */
211
212     /* Derive the VIEW matrix */
213     MAT3mult(VIEW, LOCAL, UP);
214     /* printf("VIEW matrix\n"); */
215     /* MAT3print(VIEW, stdout); */
216
217     /* generate the current up, forward, and fwrd-view vectors */
218     MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
219     MAT3mult_vec(view_up, vec, VIEW);
220
221     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
222     MAT3mult_vec(forward, vec, VIEW);
223     printf("Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1], 
224            forward[2]);
225
226     MAT3rotate(TMP, view_up, view_offset);
227     MAT3mult_vec(view_forward, forward, TMP);
228
229     /* set up our view volume */
230     gluLookAt(view_pos.x, view_pos.y, view_pos.z,
231               view_pos.x + view_forward[0], view_pos.y + view_forward[1], 
232               view_pos.z + view_forward[2],
233               view_up[0], view_up[1], view_up[2]);
234
235     /* set the sun position */
236     glLightfv( GL_LIGHT0, GL_POSITION, sun_vec );
237
238     /* calculate lighting parameters based on sun's relative angle to
239      * local up */
240     MAT3_COPY_VEC(nup, local_up);
241     nsun[0] = t->fg_sunpos.x; 
242     nsun[1] = t->fg_sunpos.y;
243     nsun[2] = t->fg_sunpos.z;
244     MAT3_NORMALIZE_VEC(nup, temp);
245     MAT3_NORMALIZE_VEC(nsun, temp);
246
247     sun_angle = acos(MAT3_DOT_PRODUCT(nup, nsun));
248     printf("SUN ANGLE relative to current location = %.3f rads.\n", sun_angle);
249
250     /* ya kind'a have to plot this to see the magic */
251     ambient = 0.4 * pow(2.4, -sun_angle*sun_angle*sun_angle*sun_angle/3.0);
252     diffuse = 0.4 * cos(0.6*sun_angle*sun_angle);
253     sky = 0.85 * pow(1.6, -sun_angle*sun_angle*sun_angle*sun_angle/2.0) + 0.15;
254
255     if ( ambient < 0.1 ) { ambient = 0.1; }
256     if ( diffuse < 0.0 ) { diffuse = 0.0; }
257
258     if ( sky < 0.0 ) { sky = 0.0; }
259
260     amb[0] = color[0] * ambient;
261     amb[1] = color[1] * ambient;
262     amb[2] = color[2] * ambient;
263
264     diff[0] = color[0] * diffuse;
265     diff[1] = color[1] * diffuse;
266     diff[2] = color[2] * diffuse;
267
268     /* set lighting parameters */
269     glLightfv(GL_LIGHT0, GL_AMBIENT, amb );
270     glLightfv(GL_LIGHT0, GL_DIFFUSE, diff );
271
272     /* set fog color */
273     fog[0] = fgFogColor[0] * (ambient + diffuse);
274     fog[1] = fgFogColor[1] * (ambient + diffuse);
275     fog[2] = fgFogColor[2] * (ambient + diffuse);
276     fog[3] = fgFogColor[3];
277     glFogfv (GL_FOG_COLOR, fog);
278
279     /* set sky color */
280     clear[0] = fgClearColor[0] * sky;
281     clear[1] = fgClearColor[1] * sky;
282     clear[2] = fgClearColor[2] * sky;
283     clear[3] = fgClearColor[3];
284     glClearColor(clear[0], clear[1], clear[2], clear[3]);
285 }
286
287
288 /**************************************************************************
289  * Update all Visuals (redraws anything graphics related)
290  **************************************************************************/
291
292 static void fgUpdateVisuals( void ) {
293     /* update view volume parameters */
294     fgUpdateViewParams();
295
296     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
297
298     /* Tell GL we are switching to model view parameters */
299     glMatrixMode(GL_MODELVIEW);
300     /* glLoadIdentity(); */
301
302     /* draw scenery */
303     fgSceneryRender();
304
305     #ifdef GLUT
306       glutSwapBuffers();
307     #endif
308 }
309
310
311 /**************************************************************************
312  * Update internal time dependent calculations (i.e. flight model)
313  **************************************************************************/
314
315 void fgUpdateTimeDepCalcs(int multi_loop) {
316     struct flight_params *f;
317     struct time_params *t;
318     int i;
319
320     f = &current_aircraft.flight;
321     t = &cur_time_params;
322
323     /* update the flight model */
324     if ( multi_loop < 0 ) {
325         multi_loop = DEFAULT_MULTILOOP;
326     }
327
328     /* printf("updating flight model x %d\n", multi_loop); */
329     fgFlightModelUpdate(FG_LARCSIM, f, multi_loop);
330
331     /* refresh shared sun position and sun_vec */
332     fgUpdateSunPos();
333
334     /* the sun position has to be translated just like everything else */
335     sun_vec[0] = t->fg_sunpos.x - scenery.center.x; 
336     sun_vec[1] = t->fg_sunpos.y - scenery.center.y;
337     sun_vec[2] = t->fg_sunpos.z - scenery.center.z;
338     /* make this a directional light source only */
339     sun_vec[3] = 0.0;
340
341     /* update the view angle */
342     for ( i = 0; i < multi_loop; i++ ) {
343         if ( fabs(goal_view_offset - view_offset) < 0.05 ) {
344             view_offset = goal_view_offset;
345             break;
346         } else {
347             /* move view_offset towards goal_view_offset */
348             if ( goal_view_offset > view_offset ) {
349                 if ( goal_view_offset - view_offset < FG_PI ) {
350                     view_offset += 0.01;
351                 } else {
352                     view_offset -= 0.01;
353                 }
354             } else {
355                 if ( view_offset - goal_view_offset < FG_PI ) {
356                     view_offset -= 0.01;
357                 } else {
358                     view_offset += 0.01;
359                 }
360             }
361             if ( view_offset > FG_2PI ) {
362                 view_offset -= FG_2PI;
363             } else if ( view_offset < 0 ) {
364                 view_offset += FG_2PI;
365             }
366         }
367     }
368 }
369
370
371 void fgInitTimeDepCalcs() {
372     /* initialize timer */
373
374 #ifdef USE_ITIMER
375     fgTimerInit( 1.0 / DEFAULT_TIMER_HZ, fgUpdateTimeDepCalcs );
376 #endif USE_ITIMER
377
378 }
379
380
381 /**************************************************************************
382  * Scenery management routines
383  **************************************************************************/
384
385 /* static void fgSceneryInit_OLD() { */
386     /* make scenery */
387 /*     scenery = fgSceneryCompile_OLD();
388     runway = fgRunwayHack_OLD(0.69, 53.07);
389 } */
390
391
392 /* create the scenery */
393 /* GLint fgSceneryCompile_OLD() {
394     GLint scenery;
395
396     scenery = mesh2GL(mesh_ptr_OLD);
397
398     return(scenery);
399 }
400 */
401
402 /* hack in a runway */
403 /* GLint fgRunwayHack_OLD(double width, double length) {
404     static GLfloat concrete[4] = { 0.5, 0.5, 0.5, 1.0 };
405     static GLfloat line[4]     = { 0.9, 0.9, 0.9, 1.0 };
406     int i;
407     int num_lines = 16;
408     float line_len, line_width_2, cur_pos;
409
410     runway = glGenLists(1);
411     glNewList(runway, GL_COMPILE);
412     */
413     /* draw concrete */
414 /*    glBegin(GL_POLYGON);
415     glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, concrete );
416     glNormal3f(0.0, 0.0, 1.0);
417
418     glVertex3d( 0.0,   -width/2.0, 0.0);
419     glVertex3d( 0.0,    width/2.0, 0.0);
420     glVertex3d(length,  width/2.0, 0.0);
421     glVertex3d(length, -width/2.0, 0.0);
422     glEnd();
423     */
424     /* draw center line */
425 /*    glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, line );
426     line_len = length / ( 2 * num_lines + 1);
427     printf("line_len = %.3f\n", line_len);
428     line_width_2 = 0.02;
429     cur_pos = line_len;
430     for ( i = 0; i < num_lines; i++ ) {
431         glBegin(GL_POLYGON);
432         glVertex3d( cur_pos, -line_width_2, 0.005);
433         glVertex3d( cur_pos,  line_width_2, 0.005);
434         cur_pos += line_len;
435         glVertex3d( cur_pos,  line_width_2, 0.005);
436         glVertex3d( cur_pos, -line_width_2, 0.005);
437         cur_pos += line_len;
438         glEnd();
439     }
440
441     glEndList();
442
443     return(runway);
444 }
445 */
446
447 /* draw the scenery */
448 /*static void fgSceneryDraw_OLD() {
449     static float z = 32.35;
450
451     glPushMatrix();
452
453     glCallList(scenery);
454
455     printf("*** Drawing runway at %.2f\n", z);
456
457     glTranslatef( -398391.28, 120070.41, 32.35);
458     glRotatef(170.0, 0.0, 0.0, 1.0);
459     glCallList(runway);
460
461     glPopMatrix();
462 }
463 */
464
465 /* What should we do when we have nothing else to do?  How about get
466  * ready for the next move and update the display? */
467 static void fgMainLoop( void ) {
468     static int remainder = 0;
469     int elapsed, multi_loop;
470     double rough_elev;
471     struct flight_params *f;
472
473     f = &current_aircraft.flight;
474
475     elapsed = fgGetTimeInterval();
476     printf("Time interval is = %d, previous remainder is = %d\n", elapsed, 
477            remainder);
478     printf("--> Frame rate is = %.2f\n", 1000.0 / (float)elapsed);
479     elapsed += remainder;
480
481     multi_loop = ((float)elapsed * 0.001) * DEFAULT_MODEL_HZ;
482     remainder = elapsed - ((multi_loop*1000) / DEFAULT_MODEL_HZ);
483     printf("Model iterations needed = %d, new remainder = %d\n", multi_loop, 
484            remainder);
485
486     aircraft_debug(1);
487     fgUpdateVisuals();
488
489     if ( ! use_signals ) {
490         /* flight model */
491         fgUpdateTimeDepCalcs(multi_loop);
492     }
493
494     /* I'm just sticking this here for now, it should probably move 
495      * eventually */
496     rough_elev = mesh_altitude(FG_Longitude * RAD_TO_ARCSEC, 
497                                FG_Latitude  * RAD_TO_ARCSEC);
498     printf("Ground elevation is %.2f meters here.\n", rough_elev);
499     /* FG_Runway_altitude = rough_elev * METER_TO_FEET; */
500
501     if ( FG_Altitude * FEET_TO_METER < rough_elev + 3.758099) {
502         /* set this here, otherwise if we set runway height above our
503            current height we get a really nasty bounce. */
504         FG_Runway_altitude = FG_Altitude - 3.758099;
505
506         /* now set aircraft altitude above ground */
507         FG_Altitude = rough_elev * METER_TO_FEET + 3.758099;
508         printf("<*> resetting altitude to %.0f meters\n", 
509                FG_Altitude * FEET_TO_METER);
510     }
511
512     /* update the weather for our current position */
513     fgWeatherUpdate(FG_Longitude * RAD_TO_ARCSEC, 
514                     FG_Latitude * RAD_TO_ARCSEC, 
515                     FG_Altitude * FEET_TO_METER);
516 }
517
518
519 /**************************************************************************
520  * Handle new window size or exposure
521  **************************************************************************/
522
523 static void fgReshape( int width, int height ) {
524     /* Do this so we can call fgReshape(0,0) ourselves without having to know
525      * what the values of width & height are. */
526     if ( (height > 0) && (width > 0) ) {
527         win_ratio = (GLfloat) height / (GLfloat) width;
528     }
529
530     /* Inform gl of our view window size */
531     glViewport(0, 0, (GLint)width, (GLint)height);
532
533     fgUpdateViewParams();
534     
535     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
536 }
537
538
539 /**************************************************************************
540  * Main ...
541  **************************************************************************/
542
543 int main( int argc, char *argv[] ) {
544     struct flight_params *f;
545     double rough_elev;
546
547     f = &current_aircraft.flight;
548
549     printf("Flight Gear: prototype code to test OpenGL, LaRCsim, and VRML\n\n");
550
551
552     /**********************************************************************
553      * Initialize the Window/Graphics environment.
554      **********************************************************************/
555
556     #ifdef GLUT
557       /* initialize GLUT */
558       glutInit(&argc, argv);
559
560       /* Define Display Parameters */
561       glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
562
563       /* Define initial window size */
564       glutInitWindowSize(640, 480);
565
566       /* Initialize windows */
567       glutCreateWindow("Flight Gear");
568     #endif
569
570     /* seed the random number generater */
571     fg_srandom();
572
573     /* setup view parameters, only makes GL calls */
574     fgInitVisuals();
575
576
577     /****************************************************************
578      * The following section sets up the flight model EOM parameters and 
579      * should really be read in from one or more files.
580      ****************************************************************/
581
582     /* Globe Aiport, AZ */
583     FG_Runway_altitude = 3234.5;
584     FG_Runway_latitude = 120070.41;
585     FG_Runway_longitude = -398391.28;
586     FG_Runway_heading = 102.0 * DEG_TO_RAD;
587
588     /* Initial Position at GLOBE airport */
589     FG_Latitude  = (  120070.41 / 3600.0 ) * DEG_TO_RAD;
590     FG_Longitude = ( -398391.28 / 3600.0 ) * DEG_TO_RAD;
591     FG_Altitude = FG_Runway_altitude + 3.758099;
592
593     /* Initial Position north of the city of Globe */
594     /* FG_Latitude  = (  120625.64 / 3600.0 ) * DEG_TO_RAD; */
595     /* FG_Longitude = ( -398673.28 / 3600.0 ) * DEG_TO_RAD; */
596     /* FG_Altitude = 0.0 + 3.758099; */
597
598     printf("Initial position is: (%.4f, %.4f, %.2f)\n", FG_Latitude, 
599            FG_Longitude, FG_Altitude);
600
601       /* Initial Velocity */
602     FG_V_north = 0.0 /*  7.287719E+00 */;
603     FG_V_east  = 0.0 /*  1.521770E+03 */;
604     FG_V_down  = 0.0 /* -1.265722E-05 */;
605
606     /* Initial Orientation */
607     FG_Phi   = -2.658474E-06;
608     FG_Theta =  7.401790E-03;
609     FG_Psi   =  270.0 * DEG_TO_RAD;
610     /* FG_Psi   =  0.0 * DEG_TO_RAD; */
611
612     /* Initial Angular B rates */
613     FG_P_body = 7.206685E-05;
614     FG_Q_body = 0.000000E+00;
615     FG_R_body = 9.492658E-05;
616
617     FG_Earth_position_angle = 0.000000E+00;
618
619     /* Mass properties and geometry values */
620     FG_Mass = 8.547270E+01;
621     FG_I_xx = 1.048000E+03;
622     FG_I_yy = 3.000000E+03;
623     FG_I_zz = 3.530000E+03;
624     FG_I_xz = 0.000000E+00;
625
626     /* CG position w.r.t. ref. point */
627     FG_Dx_cg = 0.000000E+00;
628     FG_Dy_cg = 0.000000E+00;
629     FG_Dz_cg = 0.000000E+00;
630
631     /* Set initial position and slew parameters */
632     /* fgSlewInit(-398391.3, 120070.41, 244, 3.1415); */ /* GLOBE Airport */
633     /* fgSlewInit(-335340,162540, 15, 4.38); */
634     /* fgSlewInit(-398673.28,120625.64, 53, 4.38); */
635
636    /* Initialize the Scenery Management system */
637     fgSceneryInit();
638
639     /* Tell the Scenery Management system where we are so it can load
640      * the correct scenery data */
641     fgSceneryUpdate(FG_Latitude, FG_Longitude, FG_Altitude);
642
643     /* I'm just sticking this here for now, it should probably move 
644      * eventually */
645     rough_elev = mesh_altitude(FG_Longitude * RAD_TO_DEG * 3600.0, 
646                                FG_Latitude  * RAD_TO_DEG * 3600.0);
647     printf("Ground elevation is %.2f meters here.\n", rough_elev);
648     if ( rough_elev > -9990.0 ) {
649         FG_Runway_altitude = rough_elev * METER_TO_FEET;
650     }
651
652     if ( FG_Altitude < FG_Runway_altitude ) {
653         FG_Altitude = FG_Runway_altitude + 3.758099;
654     }
655     /* end of thing that I just stuck in that I should probably move */
656
657     /* Initialize the flight model data structures base on above values */
658     fgFlightModelInit( FG_LARCSIM, f, 1.0 / DEFAULT_MODEL_HZ );
659
660     if ( use_signals ) {
661         /* init timer routines, signals, etc.  Arrange for an alarm
662            signal to be generated, etc. */
663         fgInitTimeDepCalcs();
664     }
665
666     /* Initialize the weather modeling subsystem */
667     fgWeatherInit();
668
669     /**********************************************************************
670      * Initialize the Event Handlers.
671      **********************************************************************/
672
673     #ifdef GLUT
674       /* call fgReshape() on window resizes */
675       glutReshapeFunc( fgReshape );
676
677       /* call key() on keyboard event */
678       glutKeyboardFunc( GLUTkey );
679       glutSpecialFunc( GLUTspecialkey );
680
681       /* call fgMainLoop() whenever there is nothing else to do */
682       glutIdleFunc( fgMainLoop );
683
684       /* draw the scene */
685       glutDisplayFunc( fgUpdateVisuals );
686
687       /* pass control off to the GLUT event handler */
688       glutMainLoop();
689     #endif
690
691     return(0);
692 }
693
694
695 #ifdef NO_PRINTF
696   #include <stdarg.h>
697   int printf (const char *format, ...) {
698   }
699 #endif
700
701
702 /* $Log$
703 /* Revision 1.8  1997/08/19 23:55:03  curt
704 /* Worked on better simulating real lighting.
705 /*
706  * Revision 1.7  1997/08/16 12:22:38  curt
707  * Working on improving the lighting/shading.
708  *
709  * Revision 1.6  1997/08/13 20:24:56  curt
710  * Changes due to changing sunpos interface.
711  *
712  * Revision 1.5  1997/08/06 21:08:32  curt
713  * Sun position now *really* works (I think) ... I still have sun time warping
714  * code in place, probably should remove it soon.
715  *
716  * Revision 1.4  1997/08/06 15:41:26  curt
717  * Working on correct sun position.
718  *
719  * Revision 1.3  1997/08/06 00:24:22  curt
720  * Working on correct real time sun lighting.
721  *
722  * Revision 1.2  1997/08/04 20:25:15  curt
723  * Organizational tweaking.
724  *
725  * Revision 1.1  1997/08/02 18:45:00  curt
726  * Renamed GLmain.c GLUTmain.c
727  *
728  * Revision 1.43  1997/08/02 16:23:47  curt
729  * Misc. tweaks.
730  *
731  * Revision 1.42  1997/08/01 19:43:33  curt
732  * Making progress with coordinate system overhaul.
733  *
734  * Revision 1.41  1997/07/31 22:52:37  curt
735  * Working on redoing internal coordinate systems & scenery transformations.
736  *
737  * Revision 1.40  1997/07/30 16:12:42  curt
738  * Moved fg_random routines from Util/ to Math/
739  *
740  * Revision 1.39  1997/07/21 14:45:01  curt
741  * Minor tweaks.
742  *
743  * Revision 1.38  1997/07/19 23:04:47  curt
744  * Added an initial weather section.
745  *
746  * Revision 1.37  1997/07/19 22:34:02  curt
747  * Moved PI definitions to ../constants.h
748  * Moved random() stuff to ../Utils/ and renamed fg_random()
749  *
750  * Revision 1.36  1997/07/18 23:41:25  curt
751  * Tweaks for building with Cygnus Win32 compiler.
752  *
753  * Revision 1.35  1997/07/18 14:28:34  curt
754  * Hacked in some support for wind/turbulence.
755  *
756  * Revision 1.34  1997/07/16 20:04:48  curt
757  * Minor tweaks to aid Win32 port.
758  *
759  * Revision 1.33  1997/07/12 03:50:20  curt
760  * Added an #include <Windows32/Base.h> to help compiling for Win32
761  *
762  * Revision 1.32  1997/07/11 03:23:18  curt
763  * Solved some scenery display/orientation problems.  Still have a positioning
764  * (or transformation?) problem.
765  *
766  * Revision 1.31  1997/07/11 01:29:58  curt
767  * More tweaking of terrian floor.
768  *
769  * Revision 1.30  1997/07/10 04:26:37  curt
770  * We now can interpolated ground elevation for any position in the grid.  We
771  * can use this to enforce a "hard" ground.  We still need to enforce some
772  * bounds checking so that we don't try to lookup data points outside the
773  * grid data set.
774  *
775  * Revision 1.29  1997/07/09 21:31:12  curt
776  * Working on making the ground "hard."
777  *
778  * Revision 1.28  1997/07/08 18:20:12  curt
779  * Working on establishing a hard ground.
780  *
781  * Revision 1.27  1997/07/07 20:59:49  curt
782  * Working on scenery transformations to enable us to fly fluidly over the
783  * poles with no discontinuity/distortion in scenery.
784  *
785  * Revision 1.26  1997/07/05 20:43:34  curt
786  * renamed mat3 directory to Math so we could add other math related routines.
787  *
788  * Revision 1.25  1997/06/29 21:19:17  curt
789  * Working on scenery management system.
790  *
791  * Revision 1.24  1997/06/26 22:14:53  curt
792  * Beginning work on a scenery management system.
793  *
794  * Revision 1.23  1997/06/26 19:08:33  curt
795  * Restructuring make, adding automatic "make dep" support.
796  *
797  * Revision 1.22  1997/06/25 15:39:47  curt
798  * Minor changes to compile with rsxnt/win32.
799  *
800  * Revision 1.21  1997/06/22 21:44:41  curt
801  * Working on intergrating the VRML (subset) parser.
802  *
803  * Revision 1.20  1997/06/21 17:12:53  curt
804  * Capitalized subdirectory names.
805  *
806  * Revision 1.19  1997/06/18 04:10:31  curt
807  * A couple more runway tweaks ...
808  *
809  * Revision 1.18  1997/06/18 02:21:24  curt
810  * Hacked in a runway
811  *
812  * Revision 1.17  1997/06/17 16:51:58  curt
813  * Timer interval stuff now uses gettimeofday() instead of ftime()
814  *
815  * Revision 1.16  1997/06/17 04:19:16  curt
816  * More timer related tweaks with respect to view direction changes.
817  *
818  * Revision 1.15  1997/06/17 03:41:10  curt
819  * Nonsignal based interval timing is now working.
820  * This would be a good time to look at cleaning up the code structure a bit.
821  *
822  * Revision 1.14  1997/06/16 19:32:51  curt
823  * Starting to add general timer support.
824  *
825  * Revision 1.13  1997/06/02 03:40:06  curt
826  * A tiny bit more view tweaking.
827  *
828  * Revision 1.12  1997/06/02 03:01:38  curt
829  * Working on views (side, front, back, transitions, etc.)
830  *
831  * Revision 1.11  1997/05/31 19:16:25  curt
832  * Elevator trim added.
833  *
834  * Revision 1.10  1997/05/31 04:13:52  curt
835  * WE CAN NOW FLY!!!
836  *
837  * Continuing work on the LaRCsim flight model integration.
838  * Added some MSFS-like keyboard input handling.
839  *
840  * Revision 1.9  1997/05/30 19:27:01  curt
841  * The LaRCsim flight model is starting to look like it is working.
842  *
843  * Revision 1.8  1997/05/30 03:54:10  curt
844  * Made a bit more progress towards integrating the LaRCsim flight model.
845  *
846  * Revision 1.7  1997/05/29 22:39:49  curt
847  * Working on incorporating the LaRCsim flight model.
848  *
849  * Revision 1.6  1997/05/29 12:31:39  curt
850  * Minor tweaks, moving towards general flight model integration.
851  *
852  * Revision 1.5  1997/05/29 02:33:23  curt
853  * Updated to reflect changing interfaces in other "modules."
854  *
855  * Revision 1.4  1997/05/27 17:44:31  curt
856  * Renamed & rearranged variables and routines.   Added some initial simple
857  * timer/alarm routines so the flight model can be updated on a regular 
858  * interval.
859  *
860  * Revision 1.3  1997/05/23 15:40:25  curt
861  * Added GNU copyright headers.
862  * Fog now works!
863  *
864  * Revision 1.2  1997/05/23 00:35:12  curt
865  * Trying to get fog to work ...
866  *
867  * Revision 1.1  1997/05/21 15:57:51  curt
868  * Renamed due to added GLUT support.
869  *
870  * Revision 1.3  1997/05/19 18:22:42  curt
871  * Parameter tweaking ... starting to stub in fog support.
872  *
873  * Revision 1.2  1997/05/17 00:17:34  curt
874  * Trying to stub in support for standard OpenGL.
875  *
876  * Revision 1.1  1997/05/16 16:05:52  curt
877  * Initial revision.
878  *
879  */