1 /**************************************************************************
2 * GLUTmain.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 **************************************************************************/
38 #include "../constants.h"
39 #include "../general.h"
41 #include "../Aircraft/aircraft.h"
42 #include "../Cockpit/cockpit.h"
43 #include "../Joystick/joystick.h"
44 #include "../Math/fg_geodesy.h"
45 #include "../Math/mat3.h"
46 #include "../Math/polar.h"
47 #include "../Scenery/mesh.h"
48 #include "../Scenery/scenery.h"
49 #include "../Time/fg_time.h"
50 #include "../Time/fg_timer.h"
51 #include "../Time/sunpos.h"
52 #include "../Weather/weather.h"
55 /* This is a record containing all the info for the aircraft currently
57 struct AIRCRAFT current_aircraft;
59 /* This is a record containing global housekeeping information */
60 struct GENERAL general;
62 /* This is a record containing current weather info */
63 struct WEATHER current_weather;
65 /* This is a record containing current view parameters */
66 struct VIEW current_view;
69 static GLfloat win_ratio = 1.0;
72 /* static GLfloat sun_vec[4] = {1.0, 0.0, 0.0, 0.0 }; */
74 /* if the 4th field is 0.0, this specifies a direction ... */
75 /* clear color (sky) */
76 static GLfloat fgClearColor[4] = {0.60, 0.60, 0.90, 1.0};
78 static GLfloat fgFogColor[4] = {0.65, 0.65, 0.85, 1.0};
81 /* extern struct mesh *mesh_ptr; */
82 /* Function prototypes */
83 /* GLint fgSceneryCompile_OLD(); */
84 /* static void fgSceneryDraw_OLD(); */
86 /* pointer to scenery structure */
87 /* static GLint scenery, runway; */
94 /* Yet another hack. This one used by the HUD code. Michele */
98 /**************************************************************************
99 * fgInitVisuals() -- Initialize various GL/view parameters
100 **************************************************************************/
102 static void fgInitVisuals() {
106 t = &cur_time_params;
107 w = ¤t_weather;
109 glEnable( GL_DEPTH_TEST );
110 /* glFrontFace(GL_CW); */
111 glEnable( GL_CULL_FACE );
113 /* If enabled, normal vectors specified with glNormal are scaled
114 to unit length after transformation. See glNormal. */
115 glEnable( GL_NORMALIZE );
117 glLightfv( GL_LIGHT0, GL_POSITION, t->sun_vec );
118 glEnable( GL_LIGHTING );
119 glEnable( GL_LIGHT0 );
121 glShadeModel( GL_FLAT ); /* glShadeModel( GL_SMOOTH ); */
124 glFogi (GL_FOG_MODE, GL_LINEAR);
125 /* glFogf (GL_FOG_START, 1.0); */
126 glFogf (GL_FOG_END, w->visibility);
127 glFogfv (GL_FOG_COLOR, fgFogColor);
128 /* glFogf (GL_FOG_DENSITY, w->visibility); */
129 /* glHint (GL_FOG_HINT, GL_FASTEST); */
131 glClearColor(fgClearColor[0], fgClearColor[1], fgClearColor[2],
136 /**************************************************************************
137 * Update the view volume, position, and orientation
138 **************************************************************************/
140 static void fgUpdateViewParams() {
144 double x_2, x_4, x_8, x_10;
145 double ambient, diffuse, sky;
146 GLfloat color[4] = { 1.0, 1.0, 0.50, 1.0 };
147 GLfloat amb[3], diff[3], fog[4], clear[4];
149 f = ¤t_aircraft.flight;
150 t = &cur_time_params;
155 /* Tell GL we are about to modify the projection parameters */
156 glMatrixMode(GL_PROJECTION);
158 gluPerspective(60.0, 1.0/win_ratio, 1.0, 200000.0);
160 glMatrixMode(GL_MODELVIEW);
163 /* set up our view volume */
164 gluLookAt(v->view_pos.x, v->view_pos.y, v->view_pos.z,
165 v->view_pos.x + v->view_forward[0],
166 v->view_pos.y + v->view_forward[1],
167 v->view_pos.z + v->view_forward[2],
168 v->view_up[0], v->view_up[1], v->view_up[2]);
170 /* set the sun position */
171 glLightfv( GL_LIGHT0, GL_POSITION, t->sun_vec );
173 /* calculate lighting parameters based on sun's relative angle to
175 /* ya kind'a have to plot this to see the magic */
177 /* x = t->sun_angle^8 */
178 x_2 = t->sun_angle * t->sun_angle;
183 ambient = 0.4 * pow(1.1, -x_10 / 30.0);
185 /* diffuse = 0.4 * cos(0.3 * x_2);
186 if ( t->sun_angle > FG_PI_2 + 0.05 ) {
193 sky = 0.85 * pow(1.2, -x_8 / 20.0) + 0.15;
195 if ( ambient < 0.1 ) { ambient = 0.1; }
196 if ( diffuse < 0.0 ) { diffuse = 0.0; }
198 if ( sky < 0.0 ) { sky = 0.0; }
200 amb[0] = color[0] * ambient;
201 amb[1] = color[1] * ambient;
202 amb[2] = color[2] * ambient;
204 diff[0] = color[0] * diffuse;
205 diff[1] = color[1] * diffuse;
206 diff[2] = color[2] * diffuse;
208 /* set lighting parameters */
209 glLightfv(GL_LIGHT0, GL_AMBIENT, amb );
210 glLightfv(GL_LIGHT0, GL_DIFFUSE, diff );
213 fog[0] = fgFogColor[0] * (ambient + diffuse);
214 fog[1] = fgFogColor[1] * (ambient + diffuse);
215 fog[2] = fgFogColor[2] * (ambient + diffuse);
216 fog[3] = fgFogColor[3];
217 glFogfv (GL_FOG_COLOR, fog);
220 clear[0] = fgClearColor[0] * sky;
221 clear[1] = fgClearColor[1] * sky;
222 clear[2] = fgClearColor[2] * sky;
223 clear[3] = fgClearColor[3];
224 glClearColor(clear[0], clear[1], clear[2], clear[3]);
228 /**************************************************************************
229 * Update all Visuals (redraws anything graphics related)
230 **************************************************************************/
232 static void fgUpdateVisuals( void ) {
233 /* update view volume parameters */
234 fgUpdateViewParams();
236 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
238 /* Tell GL we are switching to model view parameters */
239 glMatrixMode(GL_MODELVIEW);
240 /* glLoadIdentity(); */
255 /**************************************************************************
256 * Update internal time dependent calculations (i.e. flight model)
257 **************************************************************************/
259 void fgUpdateTimeDepCalcs(int multi_loop) {
265 f = ¤t_aircraft.flight;
266 t = &cur_time_params;
269 /* update the flight model */
270 if ( multi_loop < 0 ) {
271 multi_loop = DEFAULT_MULTILOOP;
274 /* printf("updating flight model x %d\n", multi_loop); */
275 fgFlightModelUpdate(FG_LARCSIM, f, multi_loop);
277 /* refresh shared sun position and sun_vec */
278 fgUpdateSunPos(scenery.center);
280 /* update the view angle */
281 for ( i = 0; i < multi_loop; i++ ) {
282 if ( fabs(v->goal_view_offset - v->view_offset) < 0.05 ) {
283 v->view_offset = v->goal_view_offset;
286 /* move v->view_offset towards v->goal_view_offset */
287 if ( v->goal_view_offset > v->view_offset ) {
288 if ( v->goal_view_offset - v->view_offset < FG_PI ) {
289 v->view_offset += 0.01;
291 v->view_offset -= 0.01;
294 if ( v->view_offset - v->goal_view_offset < FG_PI ) {
295 v->view_offset -= 0.01;
297 v->view_offset += 0.01;
300 if ( v->view_offset > FG_2PI ) {
301 v->view_offset -= FG_2PI;
302 } else if ( v->view_offset < 0 ) {
303 v->view_offset += FG_2PI;
310 void fgInitTimeDepCalcs() {
311 /* initialize timer */
314 fgTimerInit( 1.0 / DEFAULT_TIMER_HZ, fgUpdateTimeDepCalcs );
320 /**************************************************************************
321 * Scenery management routines
322 **************************************************************************/
324 /* static void fgSceneryInit_OLD() { */
326 /* scenery = fgSceneryCompile_OLD();
327 runway = fgRunwayHack_OLD(0.69, 53.07);
331 /* create the scenery */
332 /* GLint fgSceneryCompile_OLD() {
335 scenery = mesh2GL(mesh_ptr_OLD);
341 /* hack in a runway */
342 /* GLint fgRunwayHack_OLD(double width, double length) {
343 static GLfloat concrete[4] = { 0.5, 0.5, 0.5, 1.0 };
344 static GLfloat line[4] = { 0.9, 0.9, 0.9, 1.0 };
347 float line_len, line_width_2, cur_pos;
349 runway = glGenLists(1);
350 glNewList(runway, GL_COMPILE);
353 /* glBegin(GL_POLYGON);
354 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, concrete );
355 glNormal3f(0.0, 0.0, 1.0);
357 glVertex3d( 0.0, -width/2.0, 0.0);
358 glVertex3d( 0.0, width/2.0, 0.0);
359 glVertex3d(length, width/2.0, 0.0);
360 glVertex3d(length, -width/2.0, 0.0);
363 /* draw center line */
364 /* glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, line );
365 line_len = length / ( 2 * num_lines + 1);
366 printf("line_len = %.3f\n", line_len);
369 for ( i = 0; i < num_lines; i++ ) {
371 glVertex3d( cur_pos, -line_width_2, 0.005);
372 glVertex3d( cur_pos, line_width_2, 0.005);
374 glVertex3d( cur_pos, line_width_2, 0.005);
375 glVertex3d( cur_pos, -line_width_2, 0.005);
386 /* draw the scenery */
387 /*static void fgSceneryDraw_OLD() {
388 static float z = 32.35;
394 printf("*** Drawing runway at %.2f\n", z);
396 glTranslatef( -398391.28, 120070.41, 32.35);
397 glRotatef(170.0, 0.0, 0.0, 1.0);
404 /* What should we do when we have nothing else to do? How about get
405 * ready for the next move and update the display? */
406 static void fgMainLoop( void ) {
407 static int remainder = 0;
408 int elapsed, multi_loop;
416 a = ¤t_aircraft;
418 t = &cur_time_params;
424 /* fgJoystickRead( &joy_x, &joy_y, &joy_b1, &joy_b2 );
425 printf( "Joystick X %f Y %f B1 %d B2 %d\n",
426 joy_x, joy_y, joy_b1, joy_b2 );
428 fgAileronSet( joy_x ); */
430 /* update the weather for our current position */
431 fgWeatherUpdate(FG_Longitude * RAD_TO_ARCSEC,
432 FG_Latitude * RAD_TO_ARCSEC,
433 FG_Altitude * FEET_TO_METER);
435 /* Calculate model iterations needed */
436 elapsed = fgGetTimeInterval();
437 printf("Time interval is = %d, previous remainder is = %d\n", elapsed,
439 printf("--> Frame rate is = %.2f\n", 1000.0 / (float)elapsed);
440 elapsed += remainder;
442 multi_loop = ((float)elapsed * 0.001) * DEFAULT_MODEL_HZ;
443 remainder = elapsed - ((multi_loop*1000) / DEFAULT_MODEL_HZ);
444 printf("Model iterations needed = %d, new remainder = %d\n", multi_loop,
447 if ( ! use_signals ) {
449 fgUpdateTimeDepCalcs(multi_loop);
452 /* I'm just sticking this here for now, it should probably move
454 cur_elev = mesh_altitude(FG_Longitude * RAD_TO_ARCSEC,
455 FG_Latitude * RAD_TO_ARCSEC);
456 printf("Ground elevation is %.2f meters here.\n", cur_elev);
457 /* FG_Runway_altitude = cur_elev * METER_TO_FEET; */
459 if ( FG_Altitude * FEET_TO_METER < cur_elev + 3.758099) {
460 /* set this here, otherwise if we set runway height above our
461 current height we get a really nasty bounce. */
462 FG_Runway_altitude = FG_Altitude - 3.758099;
464 /* now set aircraft altitude above ground */
465 FG_Altitude = cur_elev * METER_TO_FEET + 3.758099;
466 printf("<*> resetting altitude to %.0f meters\n",
467 FG_Altitude * FEET_TO_METER);
470 fgAircraftOutputCurrent(a);
477 /**************************************************************************
478 * Handle new window size or exposure
479 **************************************************************************/
481 static void fgReshape( int width, int height ) {
482 /* Do this so we can call fgReshape(0,0) ourselves without having to know
483 * what the values of width & height are. */
484 if ( (height > 0) && (width > 0) ) {
485 win_ratio = (GLfloat) height / (GLfloat) width;
488 /* Inform gl of our view window size */
489 glViewport(0, 0, (GLint)width, (GLint)height);
491 fgUpdateViewParams();
493 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
497 /**************************************************************************
499 **************************************************************************/
501 int main( int argc, char *argv[] ) {
504 f = ¤t_aircraft.flight;
506 printf("Flight Gear: prototype version %s\n\n", VERSION);
508 /**********************************************************************
509 * Initialize the Window/Graphics environment.
510 **********************************************************************/
513 /* initialize GLUT */
514 glutInit(&argc, argv);
516 /* Define Display Parameters */
517 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
519 /* Define initial window size */
520 glutInitWindowSize(640, 480);
522 /* Initialize windows */
523 glutCreateWindow("Flight Gear");
526 /* This is the general house keeping init routine */
529 /* This is the top level init routine which calls all the other
530 * subsystem initialization routines. If you are adding a
531 * subsystem to flight gear, its initialization call should
532 * located in this routine.*/
535 /* setup view parameters, only makes GL calls */
539 /* init timer routines, signals, etc. Arrange for an alarm
540 signal to be generated, etc. */
541 fgInitTimeDepCalcs();
544 /**********************************************************************
545 * Initialize the Event Handlers.
546 **********************************************************************/
549 /* call fgReshape() on window resizes */
550 glutReshapeFunc( fgReshape );
552 /* call key() on keyboard event */
553 glutKeyboardFunc( GLUTkey );
554 glutSpecialFunc( GLUTspecialkey );
556 /* call fgMainLoop() whenever there is nothing else to do */
557 glutIdleFunc( fgMainLoop );
560 glutDisplayFunc( fgUpdateVisuals );
562 /* pass control off to the GLUT event handler */
572 int printf (const char *format, ...) {
578 /* Revision 1.19 1997/09/18 16:20:08 curt
579 /* At dusk/dawn add/remove stars in stages.
581 * Revision 1.18 1997/09/16 22:14:51 curt
582 * Tweaked time of day lighting equations. Don't draw stars during the day.
584 * Revision 1.17 1997/09/16 15:50:29 curt
585 * Working on star alignment and time issues.
587 * Revision 1.16 1997/09/13 02:00:06 curt
588 * Mostly working on stars and generating sidereal time for accurate star
591 * Revision 1.15 1997/09/05 14:17:27 curt
592 * More tweaking with stars.
594 * Revision 1.14 1997/09/05 01:35:53 curt
595 * Working on getting stars right.
597 * Revision 1.13 1997/09/04 02:17:34 curt
600 * Revision 1.12 1997/08/27 21:32:24 curt
601 * Restructured view calculation code. Added stars.
603 * Revision 1.11 1997/08/27 03:30:16 curt
604 * Changed naming scheme of basic shared structures.
606 * Revision 1.10 1997/08/25 20:27:22 curt
607 * Merged in initial HUD and Joystick code.
609 * Revision 1.9 1997/08/22 21:34:39 curt
610 * Doing a bit of reorganizing and house cleaning.
612 * Revision 1.8 1997/08/19 23:55:03 curt
613 * Worked on better simulating real lighting.
615 * Revision 1.7 1997/08/16 12:22:38 curt
616 * Working on improving the lighting/shading.
618 * Revision 1.6 1997/08/13 20:24:56 curt
619 * Changes due to changing sunpos interface.
621 * Revision 1.5 1997/08/06 21:08:32 curt
622 * Sun position now *really* works (I think) ... I still have sun time warping
623 * code in place, probably should remove it soon.
625 * Revision 1.4 1997/08/06 15:41:26 curt
626 * Working on correct sun position.
628 * Revision 1.3 1997/08/06 00:24:22 curt
629 * Working on correct real time sun lighting.
631 * Revision 1.2 1997/08/04 20:25:15 curt
632 * Organizational tweaking.
634 * Revision 1.1 1997/08/02 18:45:00 curt
635 * Renamed GLmain.c GLUTmain.c
637 * Revision 1.43 1997/08/02 16:23:47 curt
640 * Revision 1.42 1997/08/01 19:43:33 curt
641 * Making progress with coordinate system overhaul.
643 * Revision 1.41 1997/07/31 22:52:37 curt
644 * Working on redoing internal coordinate systems & scenery transformations.
646 * Revision 1.40 1997/07/30 16:12:42 curt
647 * Moved fg_random routines from Util/ to Math/
649 * Revision 1.39 1997/07/21 14:45:01 curt
652 * Revision 1.38 1997/07/19 23:04:47 curt
653 * Added an initial weather section.
655 * Revision 1.37 1997/07/19 22:34:02 curt
656 * Moved PI definitions to ../constants.h
657 * Moved random() stuff to ../Utils/ and renamed fg_random()
659 * Revision 1.36 1997/07/18 23:41:25 curt
660 * Tweaks for building with Cygnus Win32 compiler.
662 * Revision 1.35 1997/07/18 14:28:34 curt
663 * Hacked in some support for wind/turbulence.
665 * Revision 1.34 1997/07/16 20:04:48 curt
666 * Minor tweaks to aid Win32 port.
668 * Revision 1.33 1997/07/12 03:50:20 curt
669 * Added an #include <Windows32/Base.h> to help compiling for Win32
671 * Revision 1.32 1997/07/11 03:23:18 curt
672 * Solved some scenery display/orientation problems. Still have a positioning
673 * (or transformation?) problem.
675 * Revision 1.31 1997/07/11 01:29:58 curt
676 * More tweaking of terrian floor.
678 * Revision 1.30 1997/07/10 04:26:37 curt
679 * We now can interpolated ground elevation for any position in the grid. We
680 * can use this to enforce a "hard" ground. We still need to enforce some
681 * bounds checking so that we don't try to lookup data points outside the
684 * Revision 1.29 1997/07/09 21:31:12 curt
685 * Working on making the ground "hard."
687 * Revision 1.28 1997/07/08 18:20:12 curt
688 * Working on establishing a hard ground.
690 * Revision 1.27 1997/07/07 20:59:49 curt
691 * Working on scenery transformations to enable us to fly fluidly over the
692 * poles with no discontinuity/distortion in scenery.
694 * Revision 1.26 1997/07/05 20:43:34 curt
695 * renamed mat3 directory to Math so we could add other math related routines.
697 * Revision 1.25 1997/06/29 21:19:17 curt
698 * Working on scenery management system.
700 * Revision 1.24 1997/06/26 22:14:53 curt
701 * Beginning work on a scenery management system.
703 * Revision 1.23 1997/06/26 19:08:33 curt
704 * Restructuring make, adding automatic "make dep" support.
706 * Revision 1.22 1997/06/25 15:39:47 curt
707 * Minor changes to compile with rsxnt/win32.
709 * Revision 1.21 1997/06/22 21:44:41 curt
710 * Working on intergrating the VRML (subset) parser.
712 * Revision 1.20 1997/06/21 17:12:53 curt
713 * Capitalized subdirectory names.
715 * Revision 1.19 1997/06/18 04:10:31 curt
716 * A couple more runway tweaks ...
718 * Revision 1.18 1997/06/18 02:21:24 curt
721 * Revision 1.17 1997/06/17 16:51:58 curt
722 * Timer interval stuff now uses gettimeofday() instead of ftime()
724 * Revision 1.16 1997/06/17 04:19:16 curt
725 * More timer related tweaks with respect to view direction changes.
727 * Revision 1.15 1997/06/17 03:41:10 curt
728 * Nonsignal based interval timing is now working.
729 * This would be a good time to look at cleaning up the code structure a bit.
731 * Revision 1.14 1997/06/16 19:32:51 curt
732 * Starting to add general timer support.
734 * Revision 1.13 1997/06/02 03:40:06 curt
735 * A tiny bit more view tweaking.
737 * Revision 1.12 1997/06/02 03:01:38 curt
738 * Working on views (side, front, back, transitions, etc.)
740 * Revision 1.11 1997/05/31 19:16:25 curt
741 * Elevator trim added.
743 * Revision 1.10 1997/05/31 04:13:52 curt
746 * Continuing work on the LaRCsim flight model integration.
747 * Added some MSFS-like keyboard input handling.
749 * Revision 1.9 1997/05/30 19:27:01 curt
750 * The LaRCsim flight model is starting to look like it is working.
752 * Revision 1.8 1997/05/30 03:54:10 curt
753 * Made a bit more progress towards integrating the LaRCsim flight model.
755 * Revision 1.7 1997/05/29 22:39:49 curt
756 * Working on incorporating the LaRCsim flight model.
758 * Revision 1.6 1997/05/29 12:31:39 curt
759 * Minor tweaks, moving towards general flight model integration.
761 * Revision 1.5 1997/05/29 02:33:23 curt
762 * Updated to reflect changing interfaces in other "modules."
764 * Revision 1.4 1997/05/27 17:44:31 curt
765 * Renamed & rearranged variables and routines. Added some initial simple
766 * timer/alarm routines so the flight model can be updated on a regular
769 * Revision 1.3 1997/05/23 15:40:25 curt
770 * Added GNU copyright headers.
773 * Revision 1.2 1997/05/23 00:35:12 curt
774 * Trying to get fog to work ...
776 * Revision 1.1 1997/05/21 15:57:51 curt
777 * Renamed due to added GLUT support.
779 * Revision 1.3 1997/05/19 18:22:42 curt
780 * Parameter tweaking ... starting to stub in fog support.
782 * Revision 1.2 1997/05/17 00:17:34 curt
783 * Trying to stub in support for standard OpenGL.
785 * Revision 1.1 1997/05/16 16:05:52 curt