]> git.mxchange.org Git - flightgear.git/blob - Main/GLUTkey.cxx
Added F8 to toggle fog and F9 to toggle texturing.
[flightgear.git] / Main / GLUTkey.cxx
1 /**************************************************************************
2  * GLUTkey.c -- handle GLUT keyboard events
3  *
4  * Written by Curtis Olson, 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 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #ifdef HAVE_WINDOWS_H
32 #  include <windows.h>                     
33 #endif
34
35 #include <GL/glut.h>
36 #include <XGL/xgl.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39
40 #include <Aircraft/aircraft.h>
41 // #include <Astro/orbits.hxx>
42 // #include <Astro/sun.hxx>
43 #include <Astro/solarsystem.hxx>
44 #include <Astro/sky.hxx>
45 #include <Autopilot/autopilot.h> // Added autopilot.h to list, Jeff Goeke-Smith
46 #include <Cockpit/hud.hxx>
47 #include <Debug/fg_debug.h>
48 #include <GUI/gui.h>
49 #include <Include/fg_constants.h>
50 #include <Objects/material.hxx>
51 #include <PUI/pu.h>
52 #include <Time/light.hxx>
53 #include <Weather/weather.h>
54
55 #include "GLUTkey.hxx"
56 #include "options.hxx"
57 #include "views.hxx"
58
59 #if defined(FX) && defined(XMESA)
60 #  include <GL/xmesa.h>
61    static int fullscreen = 1;
62 #endif
63
64
65 // Force an update of the sky and lighting parameters
66 static void local_update_sky_and_lighting_params( void ) {
67     // fgSunInit();
68     SolarSystem::theSolarSystem->rebuild();
69     cur_light_params.Update();
70     fgSkyColorsInit();
71 }
72
73
74 /* Handle keyboard events */
75 void GLUTkey(unsigned char k, int x, int y) {
76     fgCONTROLS *c;
77     fgFLIGHT *f;
78     fgTIME *t;
79     fgVIEW *v;
80     struct fgWEATHER *w;
81     float fov, tmp;
82
83     c = current_aircraft.controls;
84     f = current_aircraft.flight;
85     t = &cur_time_params;
86     v = &current_view;
87     w = &current_weather;
88
89     fgPrintf( FG_INPUT, FG_DEBUG, "Key hit = %d", k);
90     puKeyboard(k, PU_DOWN );
91
92     if ( GLUT_ACTIVE_ALT && glutGetModifiers() ) {
93         fgPrintf( FG_INPUT, FG_DEBUG, " SHIFTED\n");
94         switch (k) {
95         case 49: /* numeric keypad 1 */
96             v->goal_view_offset = FG_PI * 0.75;
97             return;
98         case 50: /* numeric keypad 2 */
99             v->goal_view_offset = FG_PI;
100             return;
101         case 51: /* numeric keypad 3 */
102             v->goal_view_offset = FG_PI * 1.25;
103             return;
104         case 52: /* numeric keypad 4 */
105             v->goal_view_offset = FG_PI * 0.50;
106             return;
107         case 54: /* numeric keypad 6 */
108             v->goal_view_offset = FG_PI * 1.50;
109             return;
110         case 55: /* numeric keypad 7 */
111             v->goal_view_offset = FG_PI * 0.25;
112             return;
113         case 56: /* numeric keypad 8 */
114             v->goal_view_offset = 0.00;
115             return;
116         case 57: /* numeric keypad 9 */
117             v->goal_view_offset = FG_PI * 1.75;
118             return;
119         case 72: /* H key */
120             // status = current_options.get_hud_status();
121             // current_options.set_hud_status(!status);
122             HUD_brightkey( true );
123             return;
124         case 73: /* i key */
125             // Minimal Hud
126             fgHUDInit2(&current_aircraft);
127             return;
128         case 77: /* M key */
129             t->warp -= 60;
130             local_update_sky_and_lighting_params();
131             return;
132         case 84: /* T key */
133             t->warp_delta -= 30;
134             local_update_sky_and_lighting_params();
135             return;
136         case 87: /* W key */
137 #if defined(FX) && !defined(WIN32)
138             fullscreen = ( !fullscreen );
139 #if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
140             XMesaSetFXmode(fullscreen ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW);
141 #endif
142 #endif
143             return;
144         case 88: /* X key */
145             fov = current_options.get_fov();
146             fov *= 1.05;
147             if ( fov > FG_FOV_MAX ) {
148                 fov = FG_FOV_MAX;
149             }
150             current_options.set_fov(fov);
151             v->update_fov = TRUE;
152             return;
153         case 90: /* Z key */
154             tmp = fgWeatherGetVisibility();   /* in meters */
155             tmp /= 1.10;
156             fgWeatherSetVisibility( tmp );
157             return;
158         // autopilot additions
159         case 65: /* A key */
160                 fgAPSetMode(1);
161                 return;
162         case 83: /* S key */
163                 fgAPSetMode(0);
164                 return;
165         case 68: /* D key */
166                 fgAPSetHeading(AP_CURRENT_HEADING);
167                 return;
168         }
169     } else {
170         fgPrintf( FG_INPUT, FG_DEBUG, "\n");
171         switch (k) {
172         case 50: /* numeric keypad 2 */
173             fgElevMove(-0.05);
174             return;
175         case 56: /* numeric keypad 8 */
176             fgElevMove(0.05);
177             return;
178         case 49: /* numeric keypad 1 */
179             fgElevTrimMove(-0.001);
180             return;
181         case 55: /* numeric keypad 7 */
182             fgElevTrimMove(0.001);
183             return;
184         case 52: /* numeric keypad 4 */
185             fgAileronMove(-0.05);
186             return;
187         case 54: /* numeric keypad 6 */
188             fgAileronMove(0.05);
189             return;
190         case 48: /* numeric keypad Ins */
191             fgRudderMove(-0.05);
192             return;
193         case 13: /* numeric keypad Enter */
194             fgRudderMove(0.05);
195             return;
196         case 53: /* numeric keypad 5 */
197             fgAileronSet(0.0);
198             fgElevSet(0.0);
199             fgRudderSet(0.0);
200             return;
201         case 57: /* numeric keypad 9 (Pg Up) */
202             fgThrottleMove(0, 0.01);
203             return;
204         case 51: /* numeric keypad 3 (Pg Dn) */
205             fgThrottleMove(0, -0.01);
206             return;
207         case 104: /* h key */
208             HUD_brightkey( false );
209             return;
210         case 105: /* i key */
211             fgHUDInit(&current_aircraft);  // normal HUD
212             return;
213         case 109: /* m key */
214             t->warp += 60;
215             local_update_sky_and_lighting_params();
216             return;
217         case 112: /* p key */
218             t->pause = !t->pause;
219             // printf position and attitude information
220             fgPrintf( FG_INPUT, FG_INFO,
221                       "Lon = %.4f  Lat = %.4f  Altitude = %.1f\n", 
222                       FG_Longitude * RAD_TO_DEG,
223                       FG_Latitude * RAD_TO_DEG,
224                       FG_Altitude * FEET_TO_METER);
225             fgPrintf( FG_INPUT, FG_INFO,
226                       "Heading = %.2f  Roll = %.2f  Pitch = %.2f\n", 
227                       FG_Psi * RAD_TO_DEG,
228                       FG_Phi * RAD_TO_DEG,
229                       FG_Theta * RAD_TO_DEG);
230             return;
231         case 116: /* t key */
232             t->warp_delta += 30;
233             local_update_sky_and_lighting_params();
234             return;
235         case 120: /* X key */
236             fov = current_options.get_fov();
237             fov /= 1.05;
238             if ( fov < FG_FOV_MIN ) {
239                 fov = FG_FOV_MIN;
240             }
241             current_options.set_fov(fov);
242             v->update_fov = TRUE;
243             return;
244         case 122: /* z key */
245             tmp = fgWeatherGetVisibility();   /* in meters */
246             tmp *= 1.10;
247             fgWeatherSetVisibility( tmp );
248             return;
249         case 27: /* ESC */
250             // if( fg_DebugOutput ) {
251             //   fclose( fg_DebugOutput );
252             // }
253             fgPrintf( FG_INPUT, FG_EXIT, 
254                       "Program exiting normally at user request.\n");
255         }
256     }
257 }
258
259
260 /* Handle "special" keyboard events */
261 void GLUTspecialkey(int k, int x, int y) {
262     fgCONTROLS *c;
263     fgVIEW *v;
264
265     c = current_aircraft.controls;
266     v = &current_view;
267
268     fgPrintf( FG_INPUT, FG_DEBUG, "Special key hit = %d", k);
269     puKeyboard(k + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN);
270
271     if ( GLUT_ACTIVE_SHIFT && glutGetModifiers() ) {
272         fgPrintf( FG_INPUT, FG_DEBUG, " SHIFTED\n");
273         switch (k) {
274         case GLUT_KEY_END: /* numeric keypad 1 */
275             v->goal_view_offset = FG_PI * 0.75;
276             return;
277         case GLUT_KEY_DOWN: /* numeric keypad 2 */
278             v->goal_view_offset = FG_PI;
279             return;
280         case GLUT_KEY_PAGE_DOWN: /* numeric keypad 3 */
281             v->goal_view_offset = FG_PI * 1.25;
282             return;
283         case GLUT_KEY_LEFT: /* numeric keypad 4 */
284             v->goal_view_offset = FG_PI * 0.50;
285             return;
286         case GLUT_KEY_RIGHT: /* numeric keypad 6 */
287             v->goal_view_offset = FG_PI * 1.50;
288             return;
289         case GLUT_KEY_HOME: /* numeric keypad 7 */
290             v->goal_view_offset = FG_PI * 0.25;
291             return;
292         case GLUT_KEY_UP: /* numeric keypad 8 */
293             v->goal_view_offset = 0.00;
294             return;
295         case GLUT_KEY_PAGE_UP: /* numeric keypad 9 */
296             v->goal_view_offset = FG_PI * 1.75;
297             return;
298         }
299     } else {
300         fgPrintf( FG_INPUT, FG_DEBUG, "\n");
301         switch (k) {
302         case GLUT_KEY_F8: /* F8 toggles fog ... off fastest nicest... */
303             current_options.cycle_fog();
304         
305             if ( current_options.get_fog() == fgOPTIONS::FG_FOG_DISABLED ) {
306                 fgPrintf( FG_INPUT, FG_INFO, "Fog disabled\n" );
307             } else if ( current_options.get_fog() == 
308                         fgOPTIONS::FG_FOG_FASTEST )
309             {
310                 fgPrintf( FG_INPUT, FG_INFO, 
311                           "Fog enabled, hint set to fastest\n" );
312             } else if ( current_options.get_fog() ==
313                         fgOPTIONS::FG_FOG_NICEST )
314             {
315                 fgPrintf( FG_INPUT, FG_INFO,
316                           "Fog enabled, hint set to nicest\n" );
317             }
318
319             return;
320         case GLUT_KEY_F9: /* F9 toggles textures on and off... */
321             if ( material_mgr.get_textures_loaded() ) {
322                 current_options.get_textures() ?
323                     current_options.set_textures(false) :
324                     current_options.set_textures(true);
325                 fgPrintf( FG_INPUT, FG_INFO, "Toggling texture\n" );
326             } else {
327                 fgPrintf( FG_INPUT, FG_INFO, 
328                           "No textures loaded, cannot toggle\n" );
329             }
330             return;
331         case GLUT_KEY_F10: /* F10 toggles menu on and off... */
332             fgPrintf(FG_INPUT, FG_INFO, "Invoking call back function");
333             hideMenuButton -> 
334                 setValue ((int) !(hideMenuButton -> getValue() ) );
335             hideMenuButton -> invokeCallback();
336             //exit(1);
337             return;
338         case GLUT_KEY_UP:
339             fgElevMove(0.05);
340             return;
341         case GLUT_KEY_DOWN:
342             fgElevMove(-0.05);
343             return;
344         case GLUT_KEY_LEFT:
345             fgAileronMove(-0.05);
346             return;
347         case GLUT_KEY_RIGHT:
348             fgAileronMove(0.05);
349             return;
350         case GLUT_KEY_HOME: /* numeric keypad 1 */
351             fgElevTrimMove(0.001);
352             return;
353         case GLUT_KEY_END: /* numeric keypad 7 */
354             fgElevTrimMove(-0.001);
355             return;
356         case GLUT_KEY_INSERT: /* numeric keypad Ins */
357             fgRudderMove(-0.05);
358             return;
359         case 13: /* numeric keypad Enter */
360             fgRudderMove(0.05);
361             return;
362         case 53: /* numeric keypad 5 */
363             fgAileronSet(0.0);
364             fgElevSet(0.0);
365             fgRudderSet(0.0);
366             return;
367         case GLUT_KEY_PAGE_UP: /* numeric keypad 9 (Pg Up) */
368             fgThrottleMove(0, 0.01);
369             return;
370         case GLUT_KEY_PAGE_DOWN: /* numeric keypad 3 (Pg Dn) */
371             fgThrottleMove(0, -0.01);
372             return;
373         }
374     }
375 }
376
377
378 /* $Log$
379 /* Revision 1.23  1998/09/17 18:35:30  curt
380 /* Added F8 to toggle fog and F9 to toggle texturing.
381 /*
382  * Revision 1.22  1998/09/15 04:27:27  curt
383  * Changes for new Astro code.
384  *
385  * Revision 1.21  1998/08/29 13:09:25  curt
386  * Changes to event manager from Bernie Bright.
387  *
388  * Revision 1.20  1998/08/24 20:11:12  curt
389  * Added i/I to toggle full vs. minimal HUD.
390  * Added a --hud-tris vs --hud-culled option.
391  * Moved options accessor funtions to options.hxx.
392  *
393  * Revision 1.19  1998/08/05 00:19:33  curt
394  * Added a local routine to update lighting params every frame when time is
395  * accelerated.
396  *
397  * Revision 1.18  1998/07/30 23:48:24  curt
398  * Output position & orientation when pausing.
399  * Eliminated libtool use.
400  * Added options to specify initial position and orientation.
401  * Changed default fov to 55 degrees.
402  * Added command line option to start in paused or unpaused state.
403  *
404  * Revision 1.17  1998/07/27 18:41:23  curt
405  * Added a pause command "p"
406  * Fixed some initialization order problems between pui and glut.
407  * Added an --enable/disable-sound option.
408  *
409  * Revision 1.16  1998/07/16 17:33:34  curt
410  * "H" / "h" now control hud brightness as well with off being one of the
411  *   states.
412  * Better checking for xmesa/fx 3dfx fullscreen/window support for deciding
413  *   whether or not to build in the feature.
414  * Translucent menu support.
415  * HAVE_AUDIO_SUPPORT -> ENABLE_AUDIO_SUPPORT
416  * Use fork() / wait() for playing mp3 init music in background under unix.
417  * Changed default tile diameter to 5.
418  *
419  * Revision 1.15  1998/07/13 21:01:34  curt
420  * Wrote access functions for current fgOPTIONS.
421  *
422  * Revision 1.14  1998/07/06 02:42:02  curt
423  * Added support for switching between fullscreen and window mode for
424  * Mesa/3dfx/glide.
425  *
426  * Added a basic splash screen.  Restructured the main loop and top level
427  * initialization routines to do this.
428  *
429  * Hacked in some support for playing a startup mp3 sound file while rest
430  * of sim initializes.  Currently only works in Unix using the mpg123 player.
431  * Waits for the mpg123 player to finish before initializing internal
432  * sound drivers.
433  *
434  * Revision 1.13  1998/06/27 16:54:32  curt
435  * Replaced "extern displayInstruments" with a entry in fgOPTIONS.
436  * Don't change the view port when displaying the panel.
437  *
438  * Revision 1.12  1998/06/12 14:27:26  curt
439  * Pui -> PUI, Gui -> GUI.
440  *
441  * Revision 1.11  1998/06/12 00:57:38  curt
442  * Added support for Pui/Gui.
443  * Converted fog to GL_FOG_EXP2.
444  * Link to static simulator parts.
445  * Update runfg.bat to try to be a little smarter.
446  *
447  * Revision 1.10  1998/05/27 02:24:05  curt
448  * View optimizations by Norman Vine.
449  *
450  * Revision 1.9  1998/05/16 13:05:21  curt
451  * Added limits to fov.
452  *
453  * Revision 1.8  1998/05/13 18:29:56  curt
454  * Added a keyboard binding to dynamically adjust field of view.
455  * Added a command line option to specify fov.
456  * Adjusted terrain color.
457  * Root path info moved to fgOPTIONS.
458  * Added ability to parse options out of a config file.
459  *
460  * Revision 1.7  1998/05/07 23:14:14  curt
461  * Added "D" key binding to set autopilot heading.
462  * Made frame rate calculation average out over last 10 frames.
463  * Borland C++ floating point exception workaround.
464  * Added a --tile-radius=n option.
465  *
466  * Revision 1.6  1998/04/28 01:20:20  curt
467  * Type-ified fgTIME and fgVIEW.
468  * Added a command line option to disable textures.
469  *
470  * Revision 1.5  1998/04/25 22:06:29  curt
471  * Edited cvs log messages in source files ... bad bad bad!
472  *
473  * Revision 1.4  1998/04/25 20:24:00  curt
474  * Cleaned up initialization sequence to eliminate interdependencies
475  * between sun position, lighting, and view position.  This creates a
476  * valid single pass initialization path.
477  *
478  * Revision 1.3  1998/04/24 14:19:29  curt
479  * Fog tweaks.
480  *
481  * Revision 1.2  1998/04/24 00:49:17  curt
482  * Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
483  * Trying out some different option parsing code.
484  * Some code reorganization.
485  *
486  * Revision 1.1  1998/04/22 13:25:40  curt
487  * C++ - ifing the code.
488  * Starting a bit of reorganization of lighting code.
489  *
490  * Revision 1.33  1998/04/18 04:11:25  curt
491  * Moved fg_debug to it's own library, added zlib support.
492  *
493  * Revision 1.32  1998/04/14 02:21:01  curt
494  * Incorporated autopilot heading hold contributed by:  Jeff Goeke-Smith
495  * <jgoeke@voyager.net>
496  *
497  * Revision 1.31  1998/04/08 23:34:05  curt
498  * Patch from Durk to fix trim reversal with numlock key active.
499  *
500  * Revision 1.30  1998/04/03 22:09:02  curt
501  * Converting to Gnu autoconf system.
502  *
503  * Revision 1.29  1998/02/07 15:29:40  curt
504  * Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
505  * <chotchkiss@namg.us.anritsu.com>
506  *
507  * Revision 1.28  1998/02/03 23:20:23  curt
508  * Lots of little tweaks to fix various consistency problems discovered by
509  * Solaris' CC.  Fixed a bug in fg_debug.c with how the fgPrintf() wrapper
510  * passed arguments along to the real printf().  Also incorporated HUD changes
511  * by Michele America.
512  *
513  * Revision 1.27  1998/01/27 00:47:55  curt
514  * Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
515  * system and commandline/config file processing code.
516  *
517  * Revision 1.26  1998/01/19 19:27:07  curt
518  * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
519  * This should simplify things tremendously.
520  *
521  * Revision 1.25  1998/01/05 18:44:34  curt
522  * Add an option to advance/decrease time from keyboard.
523  *
524  * Revision 1.24  1997/12/30 16:36:46  curt
525  * Merged in Durk's changes ...
526  *
527  * Revision 1.23  1997/12/15 23:54:44  curt
528  * Add xgl wrappers for debugging.
529  * Generate terrain normals on the fly.
530  *
531  * Revision 1.22  1997/12/10 22:37:45  curt
532  * Prepended "fg" on the name of all global structures that didn't have it yet.
533  * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
534  *
535  * Revision 1.21  1997/08/27 21:32:23  curt
536  * Restructured view calculation code.  Added stars.
537  *
538  * Revision 1.20  1997/08/27 03:30:13  curt
539  * Changed naming scheme of basic shared structures.
540  *
541  * Revision 1.19  1997/08/25 20:27:21  curt
542  * Merged in initial HUD and Joystick code.
543  *
544  * Revision 1.18  1997/08/22 21:34:38  curt
545  * Doing a bit of reorganizing and house cleaning.
546  *
547  * Revision 1.17  1997/07/19 22:34:02  curt
548  * Moved PI definitions to ../constants.h
549  * Moved random() stuff to ../Utils/ and renamed fg_random()
550  *
551  * Revision 1.16  1997/07/18 23:41:24  curt
552  * Tweaks for building with Cygnus Win32 compiler.
553  *
554  * Revision 1.15  1997/07/16 20:04:47  curt
555  * Minor tweaks to aid Win32 port.
556  *
557  * Revision 1.14  1997/07/12 03:50:20  curt
558  * Added an #include <Windows32/Base.h> to help compiling for Win32
559  *
560  * Revision 1.13  1997/06/25 15:39:46  curt
561  * Minor changes to compile with rsxnt/win32.
562  *
563  * Revision 1.12  1997/06/21 17:12:52  curt
564  * Capitalized subdirectory names.
565  *
566  * Revision 1.11  1997/06/18 04:10:31  curt
567  * A couple more runway tweaks ...
568  *
569  * Revision 1.10  1997/06/18 02:21:23  curt
570  * Hacked in a runway
571  *
572  * Revision 1.9  1997/06/02 03:40:06  curt
573  * A tiny bit more view tweaking.
574  *
575  * Revision 1.8  1997/06/02 03:01:38  curt
576  * Working on views (side, front, back, transitions, etc.)
577  *
578  * Revision 1.7  1997/05/31 19:16:25  curt
579  * Elevator trim added.
580  *
581  * Revision 1.6  1997/05/31 04:13:52  curt
582  * WE CAN NOW FLY!!!
583  *
584  * Continuing work on the LaRCsim flight model integration.
585  * Added some MSFS-like keyboard input handling.
586  *
587  * Revision 1.5  1997/05/30 23:26:19  curt
588  * Added elevator/aileron controls.
589  *
590  * Revision 1.4  1997/05/27 17:44:31  curt
591  * Renamed & rearranged variables and routines.   Added some initial simple
592  * timer/alarm routines so the flight model can be updated on a regular 
593  * interval.
594  *
595  * Revision 1.3  1997/05/23 15:40:25  curt
596  * Added GNU copyright headers.
597  * Fog now works!
598  *
599  * Revision 1.2  1997/05/23 00:35:12  curt
600  * Trying to get fog to work ...
601  *
602  * Revision 1.1  1997/05/21 15:57:50  curt
603  * Renamed due to added GLUT support.
604  *
605  * Revision 1.2  1997/05/19 18:22:41  curt
606  * Parameter tweaking ... starting to stub in fog support.
607  *
608  * Revision 1.1  1997/05/16 16:05:51  curt
609  * Initial revision.
610  *
611  */