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 global housekeeping information */
56 struct fgGENERAL general;
59 static GLfloat win_ratio = 1.0;
62 static GLfloat fgFogColor[4] = {0.65, 0.65, 0.85, 1.0};
65 /* pointer to scenery structure */
66 /* static GLint scenery, runway; */
73 /* Yet another hack. This one used by the HUD code. Michele */
77 /**************************************************************************
78 * fgInitVisuals() -- Initialize various GL/view parameters
79 **************************************************************************/
81 static void fgInitVisuals() {
86 l = &cur_light_params;
90 glEnable( GL_DEPTH_TEST );
91 /* glFrontFace(GL_CW); */
92 glEnable( GL_CULL_FACE );
94 /* glDisable( GL_DITHER ); */
96 /* If enabled, normal vectors specified with glNormal are scaled
97 to unit length after transformation. See glNormal. */
98 glEnable( GL_NORMALIZE );
100 glEnable( GL_LIGHTING );
101 glEnable( GL_LIGHT0 );
102 glLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec );
104 glShadeModel( GL_FLAT ); /* glShadeModel( GL_SMOOTH ); */
107 glFogi (GL_FOG_MODE, GL_LINEAR);
108 /* glFogf (GL_FOG_START, 1.0); */
109 glFogf (GL_FOG_END, w->visibility);
110 glFogfv (GL_FOG_COLOR, fgFogColor);
111 /* glFogf (GL_FOG_DENSITY, w->visibility); */
112 /* glHint (GL_FOG_HINT, GL_FASTEST); */
114 /* initial screen color */
115 glClearColor(0.0, 0.0, 0.0, 1.0);
119 /**************************************************************************
120 * Update the view volume, position, and orientation
121 **************************************************************************/
123 static void fgUpdateViewParams() {
129 double x_2, x_4, x_8, x_10;
130 double light, ambient, diffuse, sky_brightness;
131 /* if the 4th field is 0.0, this specifies a direction ... */
132 /* clear color (sky) */
133 GLfloat sky_color[4] = {0.60, 0.60, 0.90, 1.0};
134 GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
136 f = ¤t_aircraft.flight;
137 l = &cur_light_params;
138 t = &cur_time_params;
143 /* Tell GL we are about to modify the projection parameters */
144 glMatrixMode(GL_PROJECTION);
146 gluPerspective(55.0, 1.0/win_ratio, 1.0, 200000.0);
148 glMatrixMode(GL_MODELVIEW);
151 /* set up our view volume */
152 gluLookAt(v->view_pos.x, v->view_pos.y, v->view_pos.z,
153 v->view_pos.x + v->view_forward[0],
154 v->view_pos.y + v->view_forward[1],
155 v->view_pos.z + v->view_forward[2],
156 v->view_up[0], v->view_up[1], v->view_up[2]);
158 /* set the sun position */
159 glLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec );
161 /* calculate lighting parameters based on sun's relative angle to
163 /* ya kind'a have to plot this to see the magic */
165 /* x = t->sun_angle^8 */
166 x_2 = l->sun_angle * l->sun_angle;
171 light = pow(1.1, -x_10 / 30.0);
172 ambient = 0.3 * light;
173 diffuse = 0.9 * light;
175 sky_brightness = 0.85 * pow(1.2, -x_8 / 20.0) + 0.15;
177 /* sky_brightness = 0.15; */ /* to force a dark sky (for testing) */
179 if ( ambient < 0.1 ) { ambient = 0.1; }
180 if ( diffuse < 0.0 ) { diffuse = 0.0; }
182 if ( sky_brightness < 0.0 ) { sky_brightness = 0.0; }
184 l->scene_ambient[0] = white[0] * ambient;
185 l->scene_ambient[1] = white[1] * ambient;
186 l->scene_ambient[2] = white[2] * ambient;
188 l->scene_diffuse[0] = white[0] * diffuse;
189 l->scene_diffuse[1] = white[1] * diffuse;
190 l->scene_diffuse[2] = white[2] * diffuse;
192 /* set lighting parameters */
193 glLightfv(GL_LIGHT0, GL_AMBIENT, l->scene_ambient );
194 glLightfv(GL_LIGHT0, GL_DIFFUSE, l->scene_diffuse );
197 l->scene_fog[0] = fgFogColor[0] * (ambient + diffuse);
198 l->scene_fog[1] = fgFogColor[1] * (ambient + diffuse);
199 l->scene_fog[2] = fgFogColor[2] * (ambient + diffuse);
200 l->scene_fog[3] = fgFogColor[3];
201 glFogfv (GL_FOG_COLOR, l->scene_fog);
204 l->scene_clear[0] = sky_color[0] * sky_brightness;
205 l->scene_clear[1] = sky_color[1] * sky_brightness;
206 l->scene_clear[2] = sky_color[2] * sky_brightness;
207 l->scene_clear[3] = sky_color[3];
209 glClearColor(l->scene_clear[0], l->scene_clear[1],
210 l->scene_clear[2], l->scene_clear[3]);
214 /**************************************************************************
215 * Update all Visuals (redraws anything graphics related)
216 **************************************************************************/
218 static void fgUpdateVisuals( void ) {
219 /* update view volume parameters */
220 fgUpdateViewParams();
222 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
224 /* Tell GL we are switching to model view parameters */
225 glMatrixMode(GL_MODELVIEW);
226 /* glLoadIdentity(); */
231 /* draw astronomical objects */
244 /**************************************************************************
245 * Update internal time dependent calculations (i.e. flight model)
246 **************************************************************************/
248 void fgUpdateTimeDepCalcs(int multi_loop) {
254 f = ¤t_aircraft.flight;
255 t = &cur_time_params;
258 /* update the flight model */
259 if ( multi_loop < 0 ) {
260 multi_loop = DEFAULT_MULTILOOP;
263 /* printf("updating flight model x %d\n", multi_loop); */
264 fgFlightModelUpdate(FG_LARCSIM, f, multi_loop);
266 /* refresh shared sun position and sun_vec */
267 fgUpdateSunPos(scenery.center);
269 /* update the view angle */
270 for ( i = 0; i < multi_loop; i++ ) {
271 if ( fabs(v->goal_view_offset - v->view_offset) < 0.05 ) {
272 v->view_offset = v->goal_view_offset;
275 /* move v->view_offset towards v->goal_view_offset */
276 if ( v->goal_view_offset > v->view_offset ) {
277 if ( v->goal_view_offset - v->view_offset < FG_PI ) {
278 v->view_offset += 0.01;
280 v->view_offset -= 0.01;
283 if ( v->view_offset - v->goal_view_offset < FG_PI ) {
284 v->view_offset -= 0.01;
286 v->view_offset += 0.01;
289 if ( v->view_offset > FG_2PI ) {
290 v->view_offset -= FG_2PI;
291 } else if ( v->view_offset < 0 ) {
292 v->view_offset += FG_2PI;
299 void fgInitTimeDepCalcs() {
300 /* initialize timer */
303 fgTimerInit( 1.0 / DEFAULT_TIMER_HZ, fgUpdateTimeDepCalcs );
309 /**************************************************************************
310 * Scenery management routines
311 **************************************************************************/
313 /* static void fgSceneryInit_OLD() { */
315 /* scenery = fgSceneryCompile_OLD();
316 runway = fgRunwayHack_OLD(0.69, 53.07);
320 /* create the scenery */
321 /* GLint fgSceneryCompile_OLD() {
324 scenery = mesh2GL(mesh_ptr_OLD);
330 /* hack in a runway */
331 /* GLint fgRunwayHack_OLD(double width, double length) {
332 static GLfloat concrete[4] = { 0.5, 0.5, 0.5, 1.0 };
333 static GLfloat line[4] = { 0.9, 0.9, 0.9, 1.0 };
336 float line_len, line_width_2, cur_pos;
338 runway = glGenLists(1);
339 glNewList(runway, GL_COMPILE);
342 /* glBegin(GL_POLYGON);
343 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, concrete );
344 glNormal3f(0.0, 0.0, 1.0);
346 glVertex3d( 0.0, -width/2.0, 0.0);
347 glVertex3d( 0.0, width/2.0, 0.0);
348 glVertex3d(length, width/2.0, 0.0);
349 glVertex3d(length, -width/2.0, 0.0);
352 /* draw center line */
353 /* glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, line );
354 line_len = length / ( 2 * num_lines + 1);
355 printf("line_len = %.3f\n", line_len);
358 for ( i = 0; i < num_lines; i++ ) {
360 glVertex3d( cur_pos, -line_width_2, 0.005);
361 glVertex3d( cur_pos, line_width_2, 0.005);
363 glVertex3d( cur_pos, line_width_2, 0.005);
364 glVertex3d( cur_pos, -line_width_2, 0.005);
375 /* draw the scenery */
376 /*static void fgSceneryDraw_OLD() {
377 static float z = 32.35;
383 printf("*** Drawing runway at %.2f\n", z);
385 glTranslatef( -398391.28, 120070.41, 32.35);
386 glRotatef(170.0, 0.0, 0.0, 1.0);
393 /* What should we do when we have nothing else to do? How about get
394 * ready for the next move and update the display? */
395 static void fgMainLoop( void ) {
396 static int remainder = 0;
397 int elapsed, multi_loop;
401 struct fgAIRCRAFT *a;
405 a = ¤t_aircraft;
407 t = &cur_time_params;
413 /* fgJoystickRead( &joy_x, &joy_y, &joy_b1, &joy_b2 );
414 printf( "Joystick X %f Y %f B1 %d B2 %d\n",
415 joy_x, joy_y, joy_b1, joy_b2 );
417 fgAileronSet( joy_x ); */
419 /* update the weather for our current position */
420 fgWeatherUpdate(FG_Longitude * RAD_TO_ARCSEC,
421 FG_Latitude * RAD_TO_ARCSEC,
422 FG_Altitude * FEET_TO_METER);
424 /* Calculate model iterations needed */
425 elapsed = fgGetTimeInterval();
426 printf("Time interval is = %d, previous remainder is = %d\n", elapsed,
428 printf("--> Frame rate is = %.2f\n", 1000.0 / (float)elapsed);
429 elapsed += remainder;
431 multi_loop = ((float)elapsed * 0.001) * DEFAULT_MODEL_HZ;
432 remainder = elapsed - ((multi_loop*1000) / DEFAULT_MODEL_HZ);
433 printf("Model iterations needed = %d, new remainder = %d\n", multi_loop,
436 if ( ! use_signals ) {
438 fgUpdateTimeDepCalcs(multi_loop);
441 /* I'm just sticking this here for now, it should probably move
443 cur_elev = mesh_altitude(FG_Longitude * RAD_TO_ARCSEC,
444 FG_Latitude * RAD_TO_ARCSEC);
445 printf("Ground elevation is %.2f meters here.\n", cur_elev);
446 /* FG_Runway_altitude = cur_elev * METER_TO_FEET; */
448 if ( FG_Altitude * FEET_TO_METER < cur_elev + 3.758099) {
449 /* set this here, otherwise if we set runway height above our
450 current height we get a really nasty bounce. */
451 FG_Runway_altitude = FG_Altitude - 3.758099;
453 /* now set aircraft altitude above ground */
454 FG_Altitude = cur_elev * METER_TO_FEET + 3.758099;
455 printf("<*> resetting altitude to %.0f meters\n",
456 FG_Altitude * FEET_TO_METER);
459 fgAircraftOutputCurrent(a);
466 /**************************************************************************
467 * Handle new window size or exposure
468 **************************************************************************/
470 static void fgReshape( int width, int height ) {
471 /* Do this so we can call fgReshape(0,0) ourselves without having to know
472 * what the values of width & height are. */
473 if ( (height > 0) && (width > 0) ) {
474 win_ratio = (GLfloat) height / (GLfloat) width;
477 /* Inform gl of our view window size */
478 glViewport(0, 0, (GLint)width, (GLint)height);
480 fgUpdateViewParams();
482 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
486 /**************************************************************************
488 **************************************************************************/
490 int main( int argc, char *argv[] ) {
493 f = ¤t_aircraft.flight;
495 printf("Flight Gear: prototype version %s\n\n", VERSION);
497 /**********************************************************************
498 * Initialize the Window/Graphics environment.
499 **********************************************************************/
502 /* initialize GLUT */
503 glutInit(&argc, argv);
505 /* Define Display Parameters */
506 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
508 /* Define initial window size */
509 glutInitWindowSize(640, 480);
511 /* Initialize windows */
512 glutCreateWindow("Flight Gear");
515 /* This is the general house keeping init routine */
518 /* This is the top level init routine which calls all the other
519 * subsystem initialization routines. If you are adding a
520 * subsystem to flight gear, its initialization call should
521 * located in this routine.*/
524 /* setup view parameters, only makes GL calls */
528 /* init timer routines, signals, etc. Arrange for an alarm
529 signal to be generated, etc. */
530 fgInitTimeDepCalcs();
533 /**********************************************************************
534 * Initialize the Event Handlers.
535 **********************************************************************/
538 /* call fgReshape() on window resizes */
539 glutReshapeFunc( fgReshape );
541 /* call key() on keyboard event */
542 glutKeyboardFunc( GLUTkey );
543 glutSpecialFunc( GLUTspecialkey );
545 /* call fgMainLoop() whenever there is nothing else to do */
546 glutIdleFunc( fgMainLoop );
549 glutDisplayFunc( fgUpdateVisuals );
551 /* pass control off to the GLUT event handler */
561 int printf (const char *format, ...) {
567 /* Revision 1.32 1997/12/15 20:59:08 curt
570 * Revision 1.31 1997/12/12 21:41:25 curt
571 * More light/material property tweaking ... still a ways off.
573 * Revision 1.30 1997/12/12 19:52:47 curt
574 * Working on lightling and material properties.
576 * Revision 1.29 1997/12/11 04:43:54 curt
577 * Fixed sun vector and lighting problems. I thing the moon is now lit
580 * Revision 1.28 1997/12/10 22:37:45 curt
581 * Prepended "fg" on the name of all global structures that didn't have it yet.
582 * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
584 * Revision 1.27 1997/12/09 05:11:54 curt
585 * Working on tweaking lighting.
587 * Revision 1.26 1997/12/09 04:25:29 curt
588 * Working on adding a global lighting params structure.
590 * Revision 1.25 1997/12/08 22:54:09 curt
591 * Enabled GL_CULL_FACE.
593 * Revision 1.24 1997/11/25 19:25:32 curt
594 * Changes to integrate Durk's moon/sun code updates + clean up.
596 * Revision 1.23 1997/11/15 18:16:34 curt
599 * Revision 1.22 1997/10/30 12:38:41 curt
600 * Working on new scenery subsystem.
602 * Revision 1.21 1997/09/23 00:29:38 curt
603 * Tweaks to get things to compile with gcc-win32.
605 * Revision 1.20 1997/09/22 14:44:19 curt
606 * Continuing to try to align stars correctly.
608 * Revision 1.19 1997/09/18 16:20:08 curt
609 * At dusk/dawn add/remove stars in stages.
611 * Revision 1.18 1997/09/16 22:14:51 curt
612 * Tweaked time of day lighting equations. Don't draw stars during the day.
614 * Revision 1.17 1997/09/16 15:50:29 curt
615 * Working on star alignment and time issues.
617 * Revision 1.16 1997/09/13 02:00:06 curt
618 * Mostly working on stars and generating sidereal time for accurate star
621 * Revision 1.15 1997/09/05 14:17:27 curt
622 * More tweaking with stars.
624 * Revision 1.14 1997/09/05 01:35:53 curt
625 * Working on getting stars right.
627 * Revision 1.13 1997/09/04 02:17:34 curt
630 * Revision 1.12 1997/08/27 21:32:24 curt
631 * Restructured view calculation code. Added stars.
633 * Revision 1.11 1997/08/27 03:30:16 curt
634 * Changed naming scheme of basic shared structures.
636 * Revision 1.10 1997/08/25 20:27:22 curt
637 * Merged in initial HUD and Joystick code.
639 * Revision 1.9 1997/08/22 21:34:39 curt
640 * Doing a bit of reorganizing and house cleaning.
642 * Revision 1.8 1997/08/19 23:55:03 curt
643 * Worked on better simulating real lighting.
645 * Revision 1.7 1997/08/16 12:22:38 curt
646 * Working on improving the lighting/shading.
648 * Revision 1.6 1997/08/13 20:24:56 curt
649 * Changes due to changing sunpos interface.
651 * Revision 1.5 1997/08/06 21:08:32 curt
652 * Sun position now *really* works (I think) ... I still have sun time warping
653 * code in place, probably should remove it soon.
655 * Revision 1.4 1997/08/06 15:41:26 curt
656 * Working on correct sun position.
658 * Revision 1.3 1997/08/06 00:24:22 curt
659 * Working on correct real time sun lighting.
661 * Revision 1.2 1997/08/04 20:25:15 curt
662 * Organizational tweaking.
664 * Revision 1.1 1997/08/02 18:45:00 curt
665 * Renamed GLmain.c GLUTmain.c
667 * Revision 1.43 1997/08/02 16:23:47 curt
670 * Revision 1.42 1997/08/01 19:43:33 curt
671 * Making progress with coordinate system overhaul.
673 * Revision 1.41 1997/07/31 22:52:37 curt
674 * Working on redoing internal coordinate systems & scenery transformations.
676 * Revision 1.40 1997/07/30 16:12:42 curt
677 * Moved fg_random routines from Util/ to Math/
679 * Revision 1.39 1997/07/21 14:45:01 curt
682 * Revision 1.38 1997/07/19 23:04:47 curt
683 * Added an initial weather section.
685 * Revision 1.37 1997/07/19 22:34:02 curt
686 * Moved PI definitions to ../constants.h
687 * Moved random() stuff to ../Utils/ and renamed fg_random()
689 * Revision 1.36 1997/07/18 23:41:25 curt
690 * Tweaks for building with Cygnus Win32 compiler.
692 * Revision 1.35 1997/07/18 14:28:34 curt
693 * Hacked in some support for wind/turbulence.
695 * Revision 1.34 1997/07/16 20:04:48 curt
696 * Minor tweaks to aid Win32 port.
698 * Revision 1.33 1997/07/12 03:50:20 curt
699 * Added an #include <Windows32/Base.h> to help compiling for Win32
701 * Revision 1.32 1997/07/11 03:23:18 curt
702 * Solved some scenery display/orientation problems. Still have a positioning
703 * (or transformation?) problem.
705 * Revision 1.31 1997/07/11 01:29:58 curt
706 * More tweaking of terrian floor.
708 * Revision 1.30 1997/07/10 04:26:37 curt
709 * We now can interpolated ground elevation for any position in the grid. We
710 * can use this to enforce a "hard" ground. We still need to enforce some
711 * bounds checking so that we don't try to lookup data points outside the
714 * Revision 1.29 1997/07/09 21:31:12 curt
715 * Working on making the ground "hard."
717 * Revision 1.28 1997/07/08 18:20:12 curt
718 * Working on establishing a hard ground.
720 * Revision 1.27 1997/07/07 20:59:49 curt
721 * Working on scenery transformations to enable us to fly fluidly over the
722 * poles with no discontinuity/distortion in scenery.
724 * Revision 1.26 1997/07/05 20:43:34 curt
725 * renamed mat3 directory to Math so we could add other math related routines.
727 * Revision 1.25 1997/06/29 21:19:17 curt
728 * Working on scenery management system.
730 * Revision 1.24 1997/06/26 22:14:53 curt
731 * Beginning work on a scenery management system.
733 * Revision 1.23 1997/06/26 19:08:33 curt
734 * Restructuring make, adding automatic "make dep" support.
736 * Revision 1.22 1997/06/25 15:39:47 curt
737 * Minor changes to compile with rsxnt/win32.
739 * Revision 1.21 1997/06/22 21:44:41 curt
740 * Working on intergrating the VRML (subset) parser.
742 * Revision 1.20 1997/06/21 17:12:53 curt
743 * Capitalized subdirectory names.
745 * Revision 1.19 1997/06/18 04:10:31 curt
746 * A couple more runway tweaks ...
748 * Revision 1.18 1997/06/18 02:21:24 curt
751 * Revision 1.17 1997/06/17 16:51:58 curt
752 * Timer interval stuff now uses gettimeofday() instead of ftime()
754 * Revision 1.16 1997/06/17 04:19:16 curt
755 * More timer related tweaks with respect to view direction changes.
757 * Revision 1.15 1997/06/17 03:41:10 curt
758 * Nonsignal based interval timing is now working.
759 * This would be a good time to look at cleaning up the code structure a bit.
761 * Revision 1.14 1997/06/16 19:32:51 curt
762 * Starting to add general timer support.
764 * Revision 1.13 1997/06/02 03:40:06 curt
765 * A tiny bit more view tweaking.
767 * Revision 1.12 1997/06/02 03:01:38 curt
768 * Working on views (side, front, back, transitions, etc.)
770 * Revision 1.11 1997/05/31 19:16:25 curt
771 * Elevator trim added.
773 * Revision 1.10 1997/05/31 04:13:52 curt
776 * Continuing work on the LaRCsim flight model integration.
777 * Added some MSFS-like keyboard input handling.
779 * Revision 1.9 1997/05/30 19:27:01 curt
780 * The LaRCsim flight model is starting to look like it is working.
782 * Revision 1.8 1997/05/30 03:54:10 curt
783 * Made a bit more progress towards integrating the LaRCsim flight model.
785 * Revision 1.7 1997/05/29 22:39:49 curt
786 * Working on incorporating the LaRCsim flight model.
788 * Revision 1.6 1997/05/29 12:31:39 curt
789 * Minor tweaks, moving towards general flight model integration.
791 * Revision 1.5 1997/05/29 02:33:23 curt
792 * Updated to reflect changing interfaces in other "modules."
794 * Revision 1.4 1997/05/27 17:44:31 curt
795 * Renamed & rearranged variables and routines. Added some initial simple
796 * timer/alarm routines so the flight model can be updated on a regular
799 * Revision 1.3 1997/05/23 15:40:25 curt
800 * Added GNU copyright headers.
803 * Revision 1.2 1997/05/23 00:35:12 curt
804 * Trying to get fog to work ...
806 * Revision 1.1 1997/05/21 15:57:51 curt
807 * Renamed due to added GLUT support.
809 * Revision 1.3 1997/05/19 18:22:42 curt
810 * Parameter tweaking ... starting to stub in fog support.
812 * Revision 1.2 1997/05/17 00:17:34 curt
813 * Trying to stub in support for standard OpenGL.
815 * Revision 1.1 1997/05/16 16:05:52 curt