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