1 // GLUTkey.cxx -- handle GLUT keyboard events
3 // Written by Curtis Olson, started May 1997.
5 // Copyright (C) 1997 Curtis L. Olson - curt@me.umn.edu
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 // (Log is kept at end of this file)
38 #include <Aircraft/aircraft.hxx>
39 #include <Astro/solarsystem.hxx>
40 #include <Astro/sky.hxx>
41 #include <Autopilot/autopilot.hxx>
42 #include <Cockpit/hud.hxx>
43 #include <Debug/logstream.hxx>
45 #include <Include/fg_constants.h>
46 #include <Objects/material.hxx>
48 #include <Time/light.hxx>
49 #include <Weather/weather.hxx>
51 #include "GLUTkey.hxx"
52 #include "options.hxx"
55 #if defined(FX) && defined(XMESA)
56 # include <GL/xmesa.h>
57 static int fullscreen = 1;
61 // Force an update of the sky and lighting parameters
62 static void local_update_sky_and_lighting_params( void ) {
64 SolarSystem::theSolarSystem->rebuild();
65 cur_light_params.Update();
70 // Handle keyboard events
71 void GLUTkey(unsigned char k, int x, int y) {
77 static bool winding_ccw = true;
79 f = current_aircraft.fdm_state;
84 FG_LOG( FG_INPUT, FG_DEBUG, "Key hit = " << k );
85 puKeyboard(k, PU_DOWN );
87 if ( GLUT_ACTIVE_ALT && glutGetModifiers() ) {
88 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
96 case 18: // Ctrl-R key
98 winding_ccw = !winding_ccw;
100 glFrontFace ( GL_CCW );
102 glFrontFace ( GL_CW );
105 case 19: // Ctrl-S key
106 fgAPToggleAutoThrottle();
108 case 20: // Ctrl-T key
109 fgAPToggleTerrainFollow();
111 case 49: // numeric keypad 1
112 v->set_goal_view_offset( FG_PI * 0.75 );
114 case 50: // numeric keypad 2
115 v->set_goal_view_offset( FG_PI );
117 case 51: // numeric keypad 3
118 v->set_goal_view_offset( FG_PI * 1.25 );
120 case 52: // numeric keypad 4
121 v->set_goal_view_offset( FG_PI * 0.50 );
123 case 54: // numeric keypad 6
124 v->set_goal_view_offset( FG_PI * 1.50 );
126 case 55: // numeric keypad 7
127 v->set_goal_view_offset( FG_PI * 0.25 );
129 case 56: // numeric keypad 8
130 v->set_goal_view_offset( 0.00 );
132 case 57: // numeric keypad 9
133 v->set_goal_view_offset( FG_PI * 1.75 );
136 // status = current_options.get_hud_status();
137 // current_options.set_hud_status(!status);
138 HUD_brightkey( true );
142 fgHUDInit2(¤t_aircraft);
146 local_update_sky_and_lighting_params();
150 local_update_sky_and_lighting_params();
153 #if defined(FX) && !defined(WIN32)
154 fullscreen = ( !fullscreen );
155 #if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
156 XMesaSetFXmode(fullscreen ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW);
161 fov = current_options.get_fov();
163 if ( fov > FG_FOV_MAX ) {
166 current_options.set_fov(fov);
167 v->force_update_fov_math();
170 tmp = w->get_visibility(); // in meters
172 w->set_visibility( tmp );
176 FG_LOG( FG_INPUT, FG_DEBUG, "" );
178 case 50: // numeric keypad 2
179 controls.move_elevator(-0.05);
181 case 56: // numeric keypad 8
182 controls.move_elevator(0.05);
184 case 49: // numeric keypad 1
185 controls.move_elevator_trim(-0.001);
187 case 55: // numeric keypad 7
188 controls.move_elevator_trim(0.001);
190 case 52: // numeric keypad 4
191 controls.move_aileron(-0.05);
193 case 54: // numeric keypad 6
194 controls.move_aileron(0.05);
196 case 48: // numeric keypad Ins
197 controls.move_rudder(-0.05);
199 case 13: // numeric keypad Enter
200 controls.move_rudder(0.05);
202 case 53: // numeric keypad 5
203 controls.set_aileron(0.0);
204 controls.set_elevator(0.0);
205 controls.set_rudder(0.0);
207 case 57: // numeric keypad 9 (Pg Up)
208 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
210 case 51: // numeric keypad 3 (Pg Dn)
211 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
216 b_ret = int( controls.get_brake( 0 ) );
217 b_set = double(!b_ret);
218 controls.set_brake( FGControls::ALL_WHEELS, b_set);
221 HUD_brightkey( false );
224 fgHUDInit(¤t_aircraft); // normal HUD
228 local_update_sky_and_lighting_params();
231 t->pause = !t->pause;
232 // printf position and attitude information
233 FG_LOG( FG_INPUT, FG_INFO,
234 "Lon = " << f->get_Longitude() * RAD_TO_DEG
235 << " Lat = " << f->get_Latitude() * RAD_TO_DEG
236 << " Altitude = " << f->get_Altitude() * FEET_TO_METER );
237 FG_LOG( FG_INPUT, FG_INFO,
238 "Heading = " << f->get_Psi() * RAD_TO_DEG
239 << " Roll = " << f->get_Phi() * RAD_TO_DEG
240 << " Pitch = " << f->get_Theta() * RAD_TO_DEG );
244 local_update_sky_and_lighting_params();
247 fov = current_options.get_fov();
249 if ( fov < FG_FOV_MIN ) {
252 current_options.set_fov(fov);
253 v->force_update_fov_math();
256 tmp = w->get_visibility(); // in meters
258 w->set_visibility( tmp );
261 // if( fg_DebugOutput ) {
262 // fclose( fg_DebugOutput );
264 FG_LOG( FG_INPUT, FG_ALERT,
265 "Program exiting normally at user request." );
272 // Handle "special" keyboard events
273 void GLUTspecialkey(int k, int x, int y) {
278 FG_LOG( FG_INPUT, FG_DEBUG, "Special key hit = " << k );
279 puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN);
281 if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
282 FG_LOG( FG_INPUT, FG_DEBUG, " SHIFTED" );
284 case GLUT_KEY_END: // numeric keypad 1
285 v->set_goal_view_offset( FG_PI * 0.75 );
287 case GLUT_KEY_DOWN: // numeric keypad 2
288 v->set_goal_view_offset( FG_PI );
290 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3
291 v->set_goal_view_offset( FG_PI * 1.25 );
293 case GLUT_KEY_LEFT: // numeric keypad 4
294 v->set_goal_view_offset( FG_PI * 0.50 );
296 case GLUT_KEY_RIGHT: // numeric keypad 6
297 v->set_goal_view_offset( FG_PI * 1.50 );
299 case GLUT_KEY_HOME: // numeric keypad 7
300 v->set_goal_view_offset( FG_PI * 0.25 );
302 case GLUT_KEY_UP: // numeric keypad 8
303 v->set_goal_view_offset( 0.00 );
305 case GLUT_KEY_PAGE_UP: // numeric keypad 9
306 v->set_goal_view_offset( FG_PI * 1.75 );
310 FG_LOG( FG_INPUT, FG_DEBUG, "" );
312 case GLUT_KEY_F8: // F8 toggles fog ... off fastest nicest...
313 current_options.cycle_fog();
315 if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
316 FG_LOG( FG_INPUT, FG_INFO, "Fog disabled" );
317 } else if ( current_options.get_fog() ==
318 fgOPTIONS::FG_FOG_FASTEST )
320 FG_LOG( FG_INPUT, FG_INFO,
321 "Fog enabled, hint set to fastest" );
322 } else if ( current_options.get_fog() ==
323 fgOPTIONS::FG_FOG_NICEST )
325 FG_LOG( FG_INPUT, FG_INFO,
326 "Fog enabled, hint set to nicest" );
330 case GLUT_KEY_F9: // F9 toggles textures on and off...
331 if ( material_mgr.get_textures_loaded() ) {
332 current_options.get_textures() ?
333 current_options.set_textures(false) :
334 current_options.set_textures(true);
335 FG_LOG( FG_INPUT, FG_INFO, "Toggling texture" );
337 FG_LOG( FG_INPUT, FG_INFO,
338 "No textures loaded, cannot toggle" );
341 case GLUT_KEY_F10: // F10 toggles menu on and off...
342 FG_LOG(FG_INPUT, FG_INFO, "Invoking call back function");
344 setValue ((int) !(hideMenuButton -> getValue() ) );
345 hideMenuButton -> invokeCallback();
349 controls.move_elevator(0.05);
352 controls.move_elevator(-0.05);
355 controls.move_aileron(-0.05);
358 controls.move_aileron(0.05);
360 case GLUT_KEY_HOME: // numeric keypad 1
361 controls.move_elevator_trim(0.001);
363 case GLUT_KEY_END: // numeric keypad 7
364 controls.move_elevator_trim(-0.001);
366 case GLUT_KEY_INSERT: // numeric keypad Ins
367 controls.move_rudder(-0.05);
369 case 13: // numeric keypad Enter
370 controls.move_rudder(0.05);
372 case 53: // numeric keypad 5
373 controls.set_aileron(0.0);
374 controls.set_elevator(0.0);
375 controls.set_rudder(0.0);
377 case GLUT_KEY_PAGE_UP: // numeric keypad 9 (Pg Up)
378 controls.move_throttle( FGControls::ALL_ENGINES, 0.01 );
380 case GLUT_KEY_PAGE_DOWN: // numeric keypad 3 (Pg Dn)
381 controls.move_throttle( FGControls::ALL_ENGINES, -0.01 );
389 // Revision 1.39 1999/02/05 21:29:07 curt
390 // Modifications to incorporate Jon S. Berndts flight model code.
392 // Revision 1.38 1998/12/11 20:26:25 curt
393 // Fixed view frustum culling accuracy bug so we can look out the sides and
394 // back without tri-stripes dropping out.
396 // Revision 1.37 1998/12/09 18:50:22 curt
397 // Converted "class fgVIEW" to "class FGView" and updated to make data
398 // members private and make required accessor functions.
400 // Revision 1.36 1998/12/06 13:51:20 curt
401 // Turned "struct fgWEATHER" into "class FGWeather".
403 // Revision 1.35 1998/12/05 16:13:17 curt
404 // Renamed class fgCONTROLS to class FGControls.
406 // Revision 1.34 1998/12/05 15:54:17 curt
407 // Renamed class fgFLIGHT to class FGState as per request by JSB.
409 // Revision 1.33 1998/12/03 01:17:12 curt
410 // Converted fgFLIGHT to a class.
412 // Revision 1.32 1998/11/06 21:18:06 curt
413 // Converted to new logstream debugging facility. This allows release
414 // builds with no messages at all (and no performance impact) by using
415 // the -DFG_NDEBUG flag.
417 // Revision 1.31 1998/11/02 18:25:37 curt
418 // Check for __CYGWIN__ (b20) as well as __CYGWIN32__ (pre b20 compilers)
419 // Other misc. tweaks.
421 // Revision 1.30 1998/10/25 14:08:46 curt
422 // Turned "struct fgCONTROLS" into a class, with inlined accessor functions.
424 // Revision 1.29 1998/10/20 14:58:57 curt
425 // Ctrl-R now reverses default polygon winding so I can see if a hole in the
426 // terrain is a result of improper winding, or if it is just an empty hole.
428 // Revision 1.28 1998/10/17 01:34:20 curt
431 // Revision 1.27 1998/10/02 12:46:46 curt
432 // Added an "auto throttle"
434 // Revision 1.26 1998/10/01 00:38:04 curt
435 // More altitude hold tweaks.
437 // Revision 1.25 1998/09/29 02:03:36 curt
440 // Revision 1.24 1998/09/26 13:16:44 curt
441 // C++-ified the comments.
443 // Revision 1.23 1998/09/17 18:35:30 curt
444 // Added F8 to toggle fog and F9 to toggle texturing.
446 // Revision 1.22 1998/09/15 04:27:27 curt
447 // Changes for new Astro code.
449 // Revision 1.21 1998/08/29 13:09:25 curt
450 // Changes to event manager from Bernie Bright.
452 // Revision 1.20 1998/08/24 20:11:12 curt
453 // Added i/I to toggle full vs. minimal HUD.
454 // Added a --hud-tris vs --hud-culled option.
455 // Moved options accessor funtions to options.hxx.
457 // Revision 1.19 1998/08/05 00:19:33 curt
458 // Added a local routine to update lighting params every frame when time is
461 // Revision 1.18 1998/07/30 23:48:24 curt
462 // Output position & orientation when pausing.
463 // Eliminated libtool use.
464 // Added options to specify initial position and orientation.
465 // Changed default fov to 55 degrees.
466 // Added command line option to start in paused or unpaused state.
468 // Revision 1.17 1998/07/27 18:41:23 curt
469 // Added a pause command "p"
470 // Fixed some initialization order problems between pui and glut.
471 // Added an --enable/disable-sound option.
473 // Revision 1.16 1998/07/16 17:33:34 curt
474 // "H" / "h" now control hud brightness as well with off being one of the
476 // Better checking for xmesa/fx 3dfx fullscreen/window support for deciding
477 // whether or not to build in the feature.
478 // Translucent menu support.
479 // HAVE_AUDIO_SUPPORT -> ENABLE_AUDIO_SUPPORT
480 // Use fork() / wait() for playing mp3 init music in background under unix.
481 // Changed default tile diameter to 5.
483 // Revision 1.15 1998/07/13 21:01:34 curt
484 // Wrote access functions for current fgOPTIONS.
486 // Revision 1.14 1998/07/06 02:42:02 curt
487 // Added support for switching between fullscreen and window mode for
490 // Added a basic splash screen. Restructured the main loop and top level
491 // initialization routines to do this.
493 // Hacked in some support for playing a startup mp3 sound file while rest
494 // of sim initializes. Currently only works in Unix using the mpg123 player.
495 // Waits for the mpg123 player to finish before initializing internal
498 // Revision 1.13 1998/06/27 16:54:32 curt
499 // Replaced "extern displayInstruments" with a entry in fgOPTIONS.
500 // Don't change the view port when displaying the panel.
502 // Revision 1.12 1998/06/12 14:27:26 curt
503 // Pui -> PUI, Gui -> GUI.
505 // Revision 1.11 1998/06/12 00:57:38 curt
506 // Added support for Pui/Gui.
507 // Converted fog to GL_FOG_EXP2.
508 // Link to static simulator parts.
509 // Update runfg.bat to try to be a little smarter.
511 // Revision 1.10 1998/05/27 02:24:05 curt
512 // View optimizations by Norman Vine.
514 // Revision 1.9 1998/05/16 13:05:21 curt
515 // Added limits to fov.
517 // Revision 1.8 1998/05/13 18:29:56 curt
518 // Added a keyboard binding to dynamically adjust field of view.
519 // Added a command line option to specify fov.
520 // Adjusted terrain color.
521 // Root path info moved to fgOPTIONS.
522 // Added ability to parse options out of a config file.
524 // Revision 1.7 1998/05/07 23:14:14 curt
525 // Added "D" key binding to set autopilot heading.
526 // Made frame rate calculation average out over last 10 frames.
527 // Borland C++ floating point exception workaround.
528 // Added a --tile-radius=n option.
530 // Revision 1.6 1998/04/28 01:20:20 curt
531 // Type-ified fgTIME and fgVIEW.
532 // Added a command line option to disable textures.
534 // Revision 1.5 1998/04/25 22:06:29 curt
535 // Edited cvs log messages in source files ... bad bad bad!
537 // Revision 1.4 1998/04/25 20:24:00 curt
538 // Cleaned up initialization sequence to eliminate interdependencies
539 // between sun position, lighting, and view position. This creates a
540 // valid single pass initialization path.
542 // Revision 1.3 1998/04/24 14:19:29 curt
545 // Revision 1.2 1998/04/24 00:49:17 curt
546 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
547 // Trying out some different option parsing code.
548 // Some code reorganization.
550 // Revision 1.1 1998/04/22 13:25:40 curt
551 // C++ - ifing the code.
552 // Starting a bit of reorganization of lighting code.
554 // Revision 1.33 1998/04/18 04:11:25 curt
555 // Moved fg_debug to it's own library, added zlib support.
557 // Revision 1.32 1998/04/14 02:21:01 curt
558 // Incorporated autopilot heading hold contributed by: Jeff Goeke-Smith
559 // <jgoeke@voyager.net>
561 // Revision 1.31 1998/04/08 23:34:05 curt
562 // Patch from Durk to fix trim reversal with numlock key active.
564 // Revision 1.30 1998/04/03 22:09:02 curt
565 // Converting to Gnu autoconf system.
567 // Revision 1.29 1998/02/07 15:29:40 curt
568 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
569 // <chotchkiss@namg.us.anritsu.com>
571 // Revision 1.28 1998/02/03 23:20:23 curt
572 // Lots of little tweaks to fix various consistency problems discovered by
573 // Solaris' CC. Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
574 // passed arguments along to the real printf(). Also incorporated HUD changes
575 // by Michele America.
577 // Revision 1.27 1998/01/27 00:47:55 curt
578 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
579 // system and commandline/config file processing code.
581 // Revision 1.26 1998/01/19 19:27:07 curt
582 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
583 // This should simplify things tremendously.
585 // Revision 1.25 1998/01/05 18:44:34 curt
586 // Add an option to advance/decrease time from keyboard.
588 // Revision 1.24 1997/12/30 16:36:46 curt
589 // Merged in Durk's changes ...
591 // Revision 1.23 1997/12/15 23:54:44 curt
592 // Add xgl wrappers for debugging.
593 // Generate terrain normals on the fly.
595 // Revision 1.22 1997/12/10 22:37:45 curt
596 // Prepended "fg" on the name of all global structures that didn't have it yet.
597 // i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
599 // Revision 1.21 1997/08/27 21:32:23 curt
600 // Restructured view calculation code. Added stars.
602 // Revision 1.20 1997/08/27 03:30:13 curt
603 // Changed naming scheme of basic shared structures.
605 // Revision 1.19 1997/08/25 20:27:21 curt
606 // Merged in initial HUD and Joystick code.
608 // Revision 1.18 1997/08/22 21:34:38 curt
609 // Doing a bit of reorganizing and house cleaning.
611 // Revision 1.17 1997/07/19 22:34:02 curt
612 // Moved PI definitions to ../constants.h
613 // Moved random() stuff to ../Utils/ and renamed fg_random()
615 // Revision 1.16 1997/07/18 23:41:24 curt
616 // Tweaks for building with Cygnus Win32 compiler.
618 // Revision 1.15 1997/07/16 20:04:47 curt
619 // Minor tweaks to aid Win32 port.
621 // Revision 1.14 1997/07/12 03:50:20 curt
622 // Added an #include <Windows32/Base.h> to help compiling for Win32
624 // Revision 1.13 1997/06/25 15:39:46 curt
625 // Minor changes to compile with rsxnt/win32.
627 // Revision 1.12 1997/06/21 17:12:52 curt
628 // Capitalized subdirectory names.
630 // Revision 1.11 1997/06/18 04:10:31 curt
631 // A couple more runway tweaks ...
633 // Revision 1.10 1997/06/18 02:21:23 curt
634 // Hacked in a runway
636 // Revision 1.9 1997/06/02 03:40:06 curt
637 // A tiny bit more view tweaking.
639 // Revision 1.8 1997/06/02 03:01:38 curt
640 // Working on views (side, front, back, transitions, etc.)
642 // Revision 1.7 1997/05/31 19:16:25 curt
643 // Elevator trim added.
645 // Revision 1.6 1997/05/31 04:13:52 curt
648 // Continuing work on the LaRCsim flight model integration.
649 // Added some MSFS-like keyboard input handling.
651 // Revision 1.5 1997/05/30 23:26:19 curt
652 // Added elevator/aileron controls.
654 // Revision 1.4 1997/05/27 17:44:31 curt
655 // Renamed & rearranged variables and routines. Added some initial simple
656 // timer/alarm routines so the flight model can be updated on a regular
659 // Revision 1.3 1997/05/23 15:40:25 curt
660 // Added GNU copyright headers.
663 // Revision 1.2 1997/05/23 00:35:12 curt
664 // Trying to get fog to work ...
666 // Revision 1.1 1997/05/21 15:57:50 curt
667 // Renamed due to added GLUT support.
669 // Revision 1.2 1997/05/19 18:22:41 curt
670 // Parameter tweaking ... starting to stub in fog support.
672 // Revision 1.1 1997/05/16 16:05:51 curt