1 /**************************************************************************
4 * Written 1998 by Durk Talsma, started Juni, 1998. For the flight gear
7 * Additional mouse supported added by David Megginson, 1999.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 **************************************************************************/
31 #include <simgear/compiler.h>
33 #ifdef FG_MATH_EXCEPTION_CLASH
42 #include <simgear/xgl/xgl.h>
44 #if defined(FX) && defined(XMESA)
45 # include <GL/xmesa.h>
53 #include <simgear/constants.h>
54 #include <simgear/debug/logstream.hxx>
55 #include <simgear/misc/fgpath.hxx>
56 #include <simgear/screen/screen-dump.hxx>
58 #include <Include/general.hxx>
59 #include <Aircraft/aircraft.hxx>
60 #include <Airports/simple.hxx>
61 #include <Autopilot/auto_gui.hxx>
62 #include <Autopilot/newauto.hxx>
63 #include <Cockpit/panel.hxx>
64 #include <Controls/controls.hxx>
65 #include <FDM/flight.hxx>
66 #include <Main/options.hxx>
67 #include <Main/fg_init.hxx>
68 #include <Main/views.hxx>
69 #include <Main/save.hxx>
70 #include <Main/bfi.hxx>
72 #include <NetworkOLK/network.h>
74 #include <Time/fg_time.hxx>
76 #if defined( WIN32 ) && !defined( __CYGWIN__ )
77 # include <simgear/screen/win32-printer.h>
78 # include <simgear/screen/GlBitmaps.h>
83 * A virtual trackball implementation
84 * Written by Gavin Bell for Silicon Graphics, November 1988.
87 * Pass the x and y coordinates of the last and current positions of
88 * the mouse, scaled so they are from (-1.0 ... 1.0).
90 * The resulting rotation is returned as a quaternion rotation in the
94 trackball(float q[4], float p1x, float p1y, float p2x, float p2y);
97 * Given two quaternions, add them together to get a third quaternion.
98 * Adding quaternions to get a compound rotation is analagous to adding
99 * translations to get a compound translation. When incrementally
100 * adding rotations, the first argument here should be the new
102 * rotation, the second and third the total rotation (which will be
103 * over-written with the resulting new total rotation).
106 add_quats(float *q1, float *q2, float *dest);
109 * A useful function, builds a rotation matrix in Matrix based on
113 build_rotmatrix(float m[4][4], float q[4]);
116 * This function computes a quaternion based on an axis (defined by
117 * the given vector) and an angle about which to rotate. The angle is
118 * expressed in radians. The result is put into the third argument.
121 axis_to_quat(float a[3], float phi, float q[4]);
126 FG_USING_STD(string);
128 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
132 #if defined(WIN32) || defined(__CYGWIN32__)
133 #define WIN32_CURSOR_TWEAKS
134 #elif (GLUT_API_VERSION >= 4 || GLUT_XLIB_IMPLEMENTATION >= 9)
135 #define X_CURSOR_TWEAKS
138 // hack, should come from an include someplace
139 extern void fgInitVisuals( void );
140 extern void fgReshape( int width, int height );
141 extern void fgRenderFrame( void );
144 fntTexFont *guiFntHandle = 0;
146 static puMenuBar *mainMenuBar = 0;
147 //static puButton *hideMenuButton = 0;
149 static puDialogBox *dialogBox = 0;
150 static puFrame *dialogFrame = 0;
151 static puText *dialogBoxMessage = 0;
152 static puOneShot *dialogBoxOkButton = 0;
155 static puDialogBox *YNdialogBox = 0;
156 static puFrame *YNdialogFrame = 0;
157 static puText *YNdialogBoxMessage = 0;
158 static puOneShot *YNdialogBoxOkButton = 0;
159 static puOneShot *YNdialogBoxNoButton = 0;
161 static char msg_OK[] = "OK";
162 static char msg_NO[] = "NO";
163 static char msg_YES[] = "YES";
164 static char msg_CANCEL[] = "Cancel";
165 static char msg_RESET[] = "Reset";
167 char *gui_msg_OK; // "OK"
168 char *gui_msg_NO; // "NO"
169 char *gui_msg_YES; // "YES"
170 char *gui_msg_CANCEL; // "CANCEL"
171 char *gui_msg_RESET; // "RESET"
173 static char global_dialog_string[256];
175 // from autopilot.cxx
176 // extern void NewAltitude( puObject *cb );
177 // extern void NewHeading( puObject *cb );
178 // extern void fgAPAdjust( puObject * );
179 // extern void NewTgtAirport( puObject *cb );
180 // bool fgAPTerrainFollowEnabled( void );
181 // bool fgAPAltitudeEnabled( void );
182 // bool fgAPHeadingEnabled( void );
183 // bool fgAPWayPointEnabled( void );
184 // bool fgAPAutoThrottleEnabled( void );
187 extern void fgLatLonFormatToggle( puObject *);
189 /* --------------------------------------------------------------------
191 ---------------------------------------------------------------------*/
195 static int _savedX = 0;
196 static int _savedY = 0;
197 static int last_buttons = 0 ;
198 static int mouse_active = 0;
199 static int menu_on = 0;
200 static int mouse_joystick_control = 0;
202 //static time_t mouse_off_time;
203 //static int mouse_timed_out;
205 // to allow returning to previous view
206 // on second left click in MOUSE_VIEW mode
207 // This has file scope so that it can be reset
208 // if the little rodent is moved NHV
209 static int _mVtoggle;
211 // we break up the glutGetModifiers return mask
212 // once per loop and stash what we need in these
213 static int glut_active_shift;
214 static int glut_active_ctrl;
215 static int glut_active_alt;
217 static float lastquat[4];
218 static float curquat[4];
219 static float _quat0[4];
220 float quat_mat[4][4];
222 // uncomment this for view to exactly follow mouse in MOUSE_VIEW mode
223 // else smooth out the view panning to .01 radian per frame
224 // see view_offset smoothing mechanism in main.cxx
225 #define NO_SMOOTH_MOUSE_VIEW
227 // uncomment following to
228 #define RESET_VIEW_ON_LEAVING_MOUSE_VIEW
230 /* --------------------------------------------------------------------
231 Support for mouse as control yoke (david@megginson.com)
233 - right button toggles between pointer and yoke
234 - horizontal drag with no buttons moves ailerons
235 - vertical drag with no buttons moves elevators
236 - horizontal drag with left button moves brakes (left=on)
237 - vertical drag with left button moves throttle (up=more)
238 - horizontal drag with middle button moves rudder
239 - vertical drag with middle button moves trim
241 For the *_sensitivity variables, a lower number means more sensitive.
243 TODO: figure out how to keep pointer from leaving window in yoke mode.
244 TODO: add thresholds and null zones
245 TODO: sensitivity should be configurable at user option.
246 TODO: allow differential braking (this will be useful if FlightGear
247 ever supports tail-draggers like the DC-3)
248 ---------------------------------------------------------------------*/
256 MouseMode mouse_mode = MOUSE_POINTER;
258 static double aileron_sensitivity = 1.0/500.0;
259 static double elevator_sensitivity = 1.0/500.0;
260 static double brake_sensitivity = 1.0/250.0;
261 static double throttle_sensitivity = 1.0/250.0;
262 static double rudder_sensitivity = 1.0/500.0;
263 static double trim_sensitivity = 1.0/1000.0;
265 static inline void Quat0( void ) {
266 curquat[0] = _quat0[0];
267 curquat[1] = _quat0[1];
268 curquat[2] = _quat0[2];
269 curquat[3] = _quat0[3];
272 static inline int left_button( void ) {
273 return( last_buttons & (1 << GLUT_LEFT_BUTTON) );
276 static inline int middle_button( void ) {
277 return( last_buttons & (1 << GLUT_MIDDLE_BUTTON) );
280 static inline int right_button( void ) {
281 return( last_buttons & (1 << GLUT_RIGHT_BUTTON) );
284 static inline void TurnCursorOn( void )
287 #if defined(WIN32_CURSOR_TWEAKS)
288 switch (mouse_mode) {
290 glutSetCursor(GLUT_CURSOR_INHERIT);
293 glutSetCursor(GLUT_CURSOR_CROSSHAIR);
296 glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
300 #if defined(X_CURSOR_TWEAKS)
301 glutWarpPointer( current_view.get_winWidth()/2, current_view.get_winHeight()/2);
305 static inline void TurnCursorOff( void )
308 #if defined(WIN32_CURSOR_TWEAKS)
309 glutSetCursor(GLUT_CURSOR_NONE);
310 #elif defined(X_CURSOR_TWEAKS)
311 glutWarpPointer( current_view.get_winWidth(), current_view.get_winHeight());
315 void maybeToggleMouse( void )
317 #if defined(WIN32_CURSOR_TWEAKS)
318 static int first_time = ~0;
319 static int mouse_changed = 0;
323 mouse_changed = ~mouse_changed;
327 if( mouse_mode != MOUSE_POINTER )
329 if( mouse_changed ) {
330 mouse_changed = ~mouse_changed;
336 first_time = ~first_time;
337 #endif // #ifdef WIN32
340 // Call with FALSE to init and TRUE to restore
341 void BusyCursor( int restore )
343 static GLenum cursor = (GLenum) 0;
345 glutSetCursor(cursor);
347 cursor = (GLenum) glutGet( (GLenum) GLUT_WINDOW_CURSOR );
348 #if defined(WIN32_CURSOR_TWEAKS)
351 glutSetCursor( GLUT_CURSOR_WAIT );
355 int guiGetMouseButton(void)
360 void guiGetMouse(int *x, int *y)
366 void guiMotionFunc ( int x, int y )
368 int ww, wh, need_warp = 0;
371 // FGTime *t = FGTime::cur_time_params;
372 // if( mouse_timed_out ) {
373 // if( t->get_cur_time() > mouse_off_time ) {
374 // moused_timed_out = 0;
376 // glutPostRedisplay () ;
380 if (mouse_mode == MOUSE_POINTER) {
382 glutPostRedisplay () ;
384 if( x == _mX && y == _mY)
387 // reset left click MOUSE_VIEW toggle feature
390 ww = current_view.get_winWidth();
391 wh = current_view.get_winHeight();
393 switch (mouse_mode) {
395 if( !mouse_joystick_control ) {
396 mouse_joystick_control = 1;
397 current_options.set_control_mode( fgOPTIONS::FG_MOUSE );
399 if ( left_button() ) {
400 offset = (_mX - x) * brake_sensitivity;
401 controls.move_brake(FGControls::ALL_WHEELS, offset);
402 offset = (_mY - y) * throttle_sensitivity;
403 controls.move_throttle(FGControls::ALL_ENGINES, offset);
404 } else if ( right_button() ) {
405 if( ! current_autopilot->get_HeadingEnabled() ) {
406 offset = (x - _mX) * rudder_sensitivity;
407 controls.move_rudder(offset);
409 if( ! current_autopilot->get_AltitudeEnabled() ) {
410 offset = (_mY - y) * trim_sensitivity;
411 controls.move_elevator_trim(offset);
414 if( ! current_autopilot->get_HeadingEnabled() ) {
415 offset = (x - _mX) * aileron_sensitivity;
416 controls.move_aileron(offset);
418 if( ! current_autopilot->get_AltitudeEnabled() ) {
419 offset = (_mY - y) * elevator_sensitivity;
420 controls.move_elevator(offset);
424 // Keep the mouse in the window.
425 if (x < 5 || x > ww-5 || y < 5 || y > wh-5) {
434 #define CONTRAINED_MOUSE_VIEW_Y
435 #ifdef CONTRAINED_MOUSE_VIEW_Y
439 #endif // CONTRAINED_MOUSE_VIEW_Y
441 } else if( y >= wh-1) {
442 #ifdef CONTRAINED_MOUSE_VIEW_Y
446 #endif // CONTRAINED_MOUSE_VIEW_Y
449 // wrap MOUSE_VIEW mode cursor x position
453 } else if ( x >= ww-1 ) {
457 // try to get FG_PI movement in each half of screen
461 if( middle_button() ) {
463 (2.0f * _mX - W) / W,
464 0, //(H - 2.0f * y) / H, // 3
466 0 //(H - 2.0f * _mY) / H // 1
473 0, //(2.0f * _mX - W) / W, // 0
474 (H - 2.0f * y) / H, // 3
475 0, //(2.0f * x - W) / W, // 2
476 (H - 2.0f * _mY) / H // 1
479 add_quats(lastquat, curquat, curquat);
480 build_rotmatrix(quat_mat, curquat);
483 // this could be done in above quat
484 // but requires redoing view pipeline
485 offset = current_view.get_goal_view_offset();
486 offset += ((_mX - x) * FG_2PI / W );
487 while (offset < 0.0) {
490 while (offset > FG_2PI) {
493 current_view.set_goal_view_offset(offset);
494 #ifdef NO_SMOOTH_MOUSE_VIEW
495 current_view.set_view_offset(offset);
504 glutWarpPointer(x, y);
506 // Record the new mouse position.
512 void guiMouseFunc(int button, int updown, int x, int y)
516 // private MOUSE_VIEW state variables
517 // to allow alternate left clicks in MOUSE_VIEW mode
518 // to toggle between current offsets and straight ahead
520 static int _mVx, _mVy, _Vx, _Vy;
521 static float _quat[4];
522 static double _view_offset;
524 // general purpose variables
527 glutModifiers = glutGetModifiers();
528 glut_active_shift = glutModifiers & GLUT_ACTIVE_SHIFT;
529 glut_active_ctrl = glutModifiers & GLUT_ACTIVE_CTRL;
530 glut_active_alt = glutModifiers & GLUT_ACTIVE_ALT;
532 // Was the left button pressed?
533 if (updown == GLUT_DOWN ) {
534 if( button == GLUT_LEFT_BUTTON)
536 switch (mouse_mode) {
543 // resume previous view offsets
548 curquat[0] = _quat[0];
549 curquat[1] = _quat[1];
550 curquat[2] = _quat[2];
551 curquat[3] = _quat[3];
552 current_view.set_goal_view_offset(_view_offset);
553 #ifdef NO_SMOOTH_MOUSE_VIEW
554 current_view.set_view_offset(_view_offset);
562 _quat[0] = curquat[0];
563 _quat[1] = curquat[1];
564 _quat[2] = curquat[2];
565 _quat[3] = curquat[3];
566 x = current_view.get_winWidth()/2;
567 y = current_view.get_winHeight()/2;
569 _view_offset = current_view.get_goal_view_offset();
570 current_view.set_goal_view_offset(0.0);
571 #ifdef NO_SMOOTH_MOUSE_VIEW
572 current_view.set_view_offset(0.0);
575 glutWarpPointer( x , y);
576 build_rotmatrix(quat_mat, curquat);
577 _mVtoggle = ~_mVtoggle;
580 }else if ( button == GLUT_RIGHT_BUTTON) {
581 switch (mouse_mode) {
583 mouse_mode = MOUSE_YOKE;
584 mouse_joystick_control = 0;
587 // start with zero point in center of screen
588 _mX = current_view.get_winWidth()/2;
589 _mY = current_view.get_winHeight()/2;
591 // try to have the MOUSE_YOKE position
592 // reflect the current stick position
593 offset = controls.get_aileron();
594 x = _mX - (int)(offset * aileron_sensitivity);
595 offset = controls.get_elevator();
596 y = _mY - (int)(offset * elevator_sensitivity);
598 glutSetCursor(GLUT_CURSOR_CROSSHAIR);
599 FG_LOG( FG_INPUT, FG_INFO, "Mouse in yoke mode" );
603 mouse_mode = MOUSE_VIEW;
604 current_options.set_control_mode( fgOPTIONS::FG_JOYSTICK );
605 x = current_view.get_winWidth()/2;
606 y = current_view.get_winHeight()/2;
609 build_rotmatrix(quat_mat, curquat);
610 glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
611 FG_LOG( FG_INPUT, FG_INFO, "Mouse in view mode" );
615 mouse_mode = MOUSE_POINTER;
618 #ifdef RESET_VIEW_ON_LEAVING_MOUSE_VIEW
620 build_rotmatrix(quat_mat, curquat);
621 current_view.set_goal_view_offset(0.0);
622 #ifdef NO_SMOOTH_MOUSE_VIEW
623 current_view.set_view_offset(0.0);
625 #endif // RESET_VIEW_ON_LEAVING_MOUSE_VIEW
626 glutSetCursor(GLUT_CURSOR_INHERIT);
631 FG_LOG( FG_INPUT, FG_INFO, "Mouse in pointer mode" );
634 glutWarpPointer( x, y );
635 } // END RIGHT BUTTON
636 } // END UPDOWN == GLUT_DOWN
638 // Note which button is pressed.
639 if ( updown == GLUT_DOWN ) {
640 last_buttons |= ( 1 << button ) ;
642 last_buttons &= ~( 1 << button ) ;
645 // If we're in pointer mode, let PUI
646 // know what's going on.
647 if (mouse_mode == MOUSE_POINTER) {
648 if (!puMouse (button, updown, x,y)) {
649 current_panel->doMouseAction(button, updown, x, y);
653 // Register the new position (if it
654 // hasn't been registered already).
658 glutPostRedisplay ();
661 /* ================ General Purpose Functions ================ */
663 // Intercept the Escape Key
664 void ConfirmExitDialog(void)
666 FG_PUSH_PUI_DIALOG( YNdialogBox );
669 // General Purpose Message Box
670 void mkDialog (const char *txt)
672 strncpy(global_dialog_string, txt, 256);
673 dialogBoxMessage->setLabel(global_dialog_string);
674 FG_PUSH_PUI_DIALOG( dialogBox );
677 // Repair any damage done to the Panel by other Gui Items
678 void guiFixPanel( void )
682 if ( current_options.get_panel_status() ) {
683 // FGView *v = ¤t_view;
684 FGTime *t = FGTime::cur_time_params;
686 if( (toggle_pause = !t->getPause()) )
687 t->togglePauseMode();
690 t->togglePauseMode();
694 // Toggle the Menu and Mouse display state
695 void guiToggleMenu(void)
698 // printf("Hiding Menu\n");
699 mainMenuBar->hide ();
700 #if defined(WIN32_CURSOR_TWEAKS)
701 if( mouse_mode == MOUSE_POINTER )
703 #endif // #ifdef WIN32_CURSOR_TWEAKS
705 // printf("Showing Menu\n");
706 mainMenuBar->reveal();
709 #endif // #ifdef WIN32
714 /* -----------------------------------------------------------------------
715 the Gui callback functions
716 ____________________________________________________________________*/
718 static void saveFlight(puObject *cv)
721 ofstream output("fgfs.sav");
722 if (output.good() && fgSaveFlight(output)) {
724 mkDialog("Saved flight to ./fgfs.sav");
725 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
727 mkDialog("Cannot save flight to ./fgfs.sav");
728 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
733 static void loadFlight(puObject *cb)
736 ifstream input("fgfs.sav");
737 if (input.good() && fgLoadFlight(input)) {
739 mkDialog("Loaded flight from fgfs.sav");
740 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from ./fgfs.sav");
742 mkDialog("Failed to load flight from fgfs.sav");
743 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from ./fgfs.sav");
748 void reInit(puObject *cb)
752 build_rotmatrix(quat_mat, curquat);
753 fgReInitSubsystems();
757 static void toggleClouds(puObject *cb)
759 FGBFI::setClouds(!FGBFI::getClouds());
762 // This is the accessor function
763 void guiTogglePanel(puObject *cb)
765 current_options.toggle_panel();
768 //void MenuHideMenuCb(puObject *cb)
769 void hideMenuCb (puObject *cb)
774 void goodBye(puObject *)
776 // FG_LOG( FG_INPUT, FG_ALERT,
777 // "Program exiting normally at user request." );
778 cout << "Program exiting normally at user request." << endl;
780 #ifdef FG_NETWORK_OLK
781 if ( current_options.get_network_olk() ) {
782 if ( net_is_registered == 0 ) fgd_send_com( "8", FGFS_host);
793 void goAwayCb (puObject *me)
795 FG_POP_PUI_DIALOG( dialogBox );
798 void mkDialogInit (void)
800 // printf("mkDialogInit\n");
801 int x = (current_options.get_xsize()/2 - 400/2);
802 int y = (current_options.get_ysize()/2 - 100/2);
803 dialogBox = new puDialogBox (x, y); // 150, 50
805 dialogFrame = new puFrame (0,0,400,100);
806 dialogBoxMessage = new puText (10, 70);
807 dialogBoxMessage -> setLabel ("");
808 dialogBoxOkButton = new puOneShot (180, 10, 240, 50);
809 dialogBoxOkButton -> setLegend (gui_msg_OK);
810 dialogBoxOkButton -> makeReturnDefault (TRUE );
811 dialogBoxOkButton -> setCallback (goAwayCb);
813 FG_FINALIZE_PUI_DIALOG( dialogBox );
816 void MayBeGoodBye(puObject *)
821 void goAwayYesNoCb(puObject *me)
823 FG_POP_PUI_DIALOG( YNdialogBox);
826 void ConfirmExitDialogInit(void)
828 char msg[] = "Really Quit";
831 // printf("ConfirmExitDialogInit\n");
832 int len = 200 - puGetStringWidth( puGetDefaultLabelFont(), msg )/2;
834 int x = (current_options.get_xsize()/2 - 400/2);
835 int y = (current_options.get_ysize()/2 - 100/2);
837 YNdialogBox = new puDialogBox (x, y); // 150, 50
838 // YNdialogBox = new puDialogBox (150, 50);
840 YNdialogFrame = new puFrame (0,0,400, 100);
842 YNdialogBoxMessage = new puText (len, 70);
843 YNdialogBoxMessage -> setDefaultValue (msg);
844 YNdialogBoxMessage -> getDefaultValue (&s);
845 YNdialogBoxMessage -> setLabel (s);
847 YNdialogBoxOkButton = new puOneShot (100, 10, 160, 50);
848 YNdialogBoxOkButton -> setLegend (gui_msg_OK);
849 YNdialogBoxOkButton -> makeReturnDefault (TRUE );
850 YNdialogBoxOkButton -> setCallback (goodBye);
852 YNdialogBoxNoButton = new puOneShot (240, 10, 300, 50);
853 YNdialogBoxNoButton -> setLegend (gui_msg_NO);
854 YNdialogBoxNoButton -> setCallback (goAwayYesNoCb);
856 FG_FINALIZE_PUI_DIALOG( YNdialogBox );
859 void notCb (puObject *)
861 mkDialog ("This function isn't implemented yet");
864 void helpCb (puObject *)
868 #if defined(FX) && !defined(WIN32)
869 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
870 if ( global_fullscreen ) {
871 global_fullscreen = false;
872 XMesaSetFXmode( XMESA_FX_WINDOW );
878 string url = "http://www.flightgear.org/Docs/InstallGuide/getstart.html";
880 if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
881 command = "netscape -remote \"openURL(" + url + ")\" &";
883 command = "netscape " + url + " &";
886 command = "webrun.bat";
889 system( command.c_str() );
890 //string text = "Help started in netscape window.";
892 //mkDialog (text.c_str());
893 mkDialog ("Help started in netscape window.");
896 #if defined( WIN32 ) && !defined( __CYGWIN__)
898 static void rotateView( double roll, double pitch, double yaw )
903 static GlBitmap *b1 = NULL;
904 extern FGInterface cur_view_fdm;
905 GLubyte *hiResScreenCapture( int multiplier )
907 float oldfov = current_options.get_fov();
908 float fov = oldfov / multiplier;
909 FGView *v = ¤t_view;
910 current_options.set_fov(fov);
911 v->force_update_fov_math();
913 int cur_width = current_view.get_winWidth( );
914 int cur_height = current_view.get_winHeight( );
915 if (b1) delete( b1 );
916 // New empty (mostly) bitmap
917 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
919 for ( y = 0; y < multiplier; y++ )
921 for ( x = 0; x < multiplier; x++ )
923 fgReshape( cur_width, cur_height );
925 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
929 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
932 current_view.UpdateViewParams(cur_view_fdm);
933 current_options.set_fov(oldfov);
934 v->force_update_fov_math();
935 return b1->getBitmap();
940 #if defined( WIN32 ) && !defined( __CYGWIN__)
941 // win32 print screen function
942 void printScreen ( puObject *obj ) {
943 bool show_pu_cursor = false;
945 if ( !puCursorIsHidden() ) {
946 show_pu_cursor = true;
952 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
953 int cur_width = current_view.get_winWidth( );
954 int cur_height = current_view.get_winHeight( );
955 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
956 p.End( hiResScreenCapture(3) );
959 mainMenuBar->reveal();
962 if ( show_pu_cursor ) {
967 #endif // #ifdef WIN32
970 void dumpSnapShot ( puObject *obj ) {
975 // do a screen snap shot
976 void fgDumpSnapShot () {
977 bool show_pu_cursor = false;
981 if ( !puCursorIsHidden() ) {
982 show_pu_cursor = true;
987 fgReshape( current_options.get_xsize(), current_options.get_ysize() );
989 // we need two render frames here to clear the menu and cursor
990 // ... not sure why but doing an extra fgFenderFrame() shoulnd't
995 my_glDumpWindow( "fgfs-screen.ppm",
996 current_options.get_xsize(),
997 current_options.get_ysize() );
999 mkDialog ("Snap shot saved to fgfs-screen.ppm");
1001 if ( show_pu_cursor ) {
1007 mainMenuBar->reveal();
1013 /// The beginnings of teleportation :-)
1014 // Needs cleaning up but works
1015 // These statics should disapear when this is a class
1016 static puDialogBox *AptDialog = 0;
1017 static puFrame *AptDialogFrame = 0;
1018 static puText *AptDialogMessage = 0;
1019 static puInput *AptDialogInput = 0;
1021 static char NewAirportId[16];
1022 static char NewAirportLabel[] = "Enter New Airport ID";
1024 static puOneShot *AptDialogOkButton = 0;
1025 static puOneShot *AptDialogCancelButton = 0;
1026 static puOneShot *AptDialogResetButton = 0;
1028 void AptDialog_Cancel(puObject *)
1030 FG_POP_PUI_DIALOG( AptDialog );
1033 void AptDialog_OK (puObject *)
1035 FGPath path( current_options.get_fg_root() );
1036 path.append( "Airports" );
1037 path.append( "simple.gdbm" );
1038 FGAirports airports( path.c_str() );
1041 FGTime *t = FGTime::cur_time_params;
1042 int PauseMode = t->getPause();
1044 t->togglePauseMode();
1047 AptDialogInput->getValue(&s);
1050 cout << "AptDialog_OK " << AptId << " " << AptId.length() << endl;
1052 AptDialog_Cancel( NULL );
1054 if ( AptId.length() ) {
1055 // set initial position from airport id
1056 FG_LOG( FG_GENERAL, FG_INFO,
1057 "Attempting to set starting position from airport code "
1060 if ( airports.search( AptId, &a ) )
1062 current_options.set_airport_id( AptId.c_str() );
1064 fgReInitSubsystems();
1067 AptId += " not in database.";
1068 mkDialog(AptId.c_str());
1071 if( PauseMode != t->getPause() )
1072 t->togglePauseMode();
1075 void AptDialog_Reset(puObject *)
1077 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
1078 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1079 AptDialogInput->setValue ( NewAirportId );
1080 AptDialogInput->setCursor( 0 ) ;
1083 void NewAirport(puObject *cb)
1085 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
1086 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1087 // cout << "NewAirport " << NewAirportId << endl;
1088 AptDialogInput->setValue( NewAirportId );
1090 FG_PUSH_PUI_DIALOG( AptDialog );
1093 static void NewAirportInit(void)
1095 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1096 int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
1097 NewAirportLabel ) / 2;
1099 AptDialog = new puDialogBox (150, 50);
1101 AptDialogFrame = new puFrame (0,0,350, 150);
1102 AptDialogMessage = new puText (len, 110);
1103 AptDialogMessage -> setLabel (NewAirportLabel);
1105 AptDialogInput = new puInput (50, 70, 300, 100);
1106 AptDialogInput -> setValue (NewAirportId);
1107 AptDialogInput -> acceptInput();
1109 AptDialogOkButton = new puOneShot (50, 10, 110, 50);
1110 AptDialogOkButton -> setLegend (gui_msg_OK);
1111 AptDialogOkButton -> setCallback (AptDialog_OK);
1112 AptDialogOkButton -> makeReturnDefault(TRUE);
1114 AptDialogCancelButton = new puOneShot (140, 10, 210, 50);
1115 AptDialogCancelButton -> setLegend (gui_msg_CANCEL);
1116 AptDialogCancelButton -> setCallback (AptDialog_Cancel);
1118 AptDialogResetButton = new puOneShot (240, 10, 300, 50);
1119 AptDialogResetButton -> setLegend (gui_msg_RESET);
1120 AptDialogResetButton -> setCallback (AptDialog_Reset);
1122 cout << "NewAirportInit " << NewAirportId << endl;
1123 FG_FINALIZE_PUI_DIALOG( AptDialog );
1126 #ifdef FG_NETWORK_OLK
1128 /// The beginnings of networking :-)
1129 // Needs cleaning up but works
1130 // These statics should disapear when this is a class
1131 static puDialogBox *NetIdDialog = 0;
1132 static puFrame *NetIdDialogFrame = 0;
1133 static puText *NetIdDialogMessage = 0;
1134 static puInput *NetIdDialogInput = 0;
1136 static char NewNetId[16];
1137 static char NewNetIdLabel[] = "Enter New Callsign";
1138 extern char *fgd_callsign;
1140 static puOneShot *NetIdDialogOkButton = 0;
1141 static puOneShot *NetIdDialogCancelButton = 0;
1143 void NetIdDialog_Cancel(puObject *)
1145 FG_POP_PUI_DIALOG( NetIdDialog );
1148 void NetIdDialog_OK (puObject *)
1152 FGTime *t = FGTime::cur_time_params;
1153 int PauseMode = t->getPause();
1155 t->togglePauseMode();
1157 The following needs some cleanup because
1158 "string options.NetId" and "char *net_callsign"
1160 NetIdDialogInput->getValue(&net_callsign);
1161 NetId = net_callsign;
1163 NetIdDialog_Cancel( NULL );
1164 current_options.set_net_id( NetId.c_str() );
1165 strcpy( fgd_callsign, net_callsign);
1166 // strcpy( fgd_callsign, current_options.get_net_id().c_str());
1167 /* Entering a callsign indicates : user wants Net HUD Info */
1168 net_hud_display = 1;
1170 if( PauseMode != t->getPause() )
1171 t->togglePauseMode();
1174 void NewCallSign(puObject *cb)
1176 sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1177 // sprintf( NewNetId, "%s", fgd_callsign );
1178 NetIdDialogInput->setValue( NewNetId );
1180 FG_PUSH_PUI_DIALOG( NetIdDialog );
1183 static void NewNetIdInit(void)
1185 sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1186 // sprintf( NewNetId, "%s", fgd_callsign );
1187 int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
1188 NewNetIdLabel ) / 2;
1190 NetIdDialog = new puDialogBox (150, 50);
1192 NetIdDialogFrame = new puFrame (0,0,350, 150);
1193 NetIdDialogMessage = new puText (len, 110);
1194 NetIdDialogMessage -> setLabel (NewNetIdLabel);
1196 NetIdDialogInput = new puInput (50, 70, 300, 100);
1197 NetIdDialogInput -> setValue (NewNetId);
1198 NetIdDialogInput -> acceptInput();
1200 NetIdDialogOkButton = new puOneShot (50, 10, 110, 50);
1201 NetIdDialogOkButton -> setLegend (gui_msg_OK);
1202 NetIdDialogOkButton -> setCallback (NetIdDialog_OK);
1203 NetIdDialogOkButton -> makeReturnDefault(TRUE);
1205 NetIdDialogCancelButton = new puOneShot (240, 10, 300, 50);
1206 NetIdDialogCancelButton -> setLegend (gui_msg_CANCEL);
1207 NetIdDialogCancelButton -> setCallback (NetIdDialog_Cancel);
1210 FG_FINALIZE_PUI_DIALOG( NetIdDialog );
1213 static void net_display_toggle( puObject *cb)
1215 net_hud_display = (net_hud_display) ? 0 : 1;
1216 printf("Toggle net_hud_display : %d\n", net_hud_display);
1219 static void net_register( puObject *cb)
1221 fgd_send_com( "1", FGFS_host );
1222 net_is_registered = 0;
1223 printf("Registering to deamon\n");
1226 static void net_unregister( puObject *cb)
1228 fgd_send_com( "8", FGFS_host );
1229 net_is_registered = -1;
1230 printf("Unregistering from deamon\n");
1234 /*************** Deamon communication **********/
1236 // These statics should disapear when this is a class
1237 static puDialogBox *NetFGDDialog = 0;
1238 static puFrame *NetFGDDialogFrame = 0;
1239 static puText *NetFGDDialogMessage = 0;
1240 //static puInput *NetFGDDialogInput = 0;
1242 //static char NewNetId[16];
1243 static char NewNetFGDLabel[] = "Scan for deamon ";
1244 static char NewFGDHost[64] = "olk.mcp.de";
1245 static int NewFGDPortLo = 10000;
1246 static int NewFGDPortHi = 10001;
1248 //extern char *fgd_callsign;
1249 extern u_short base_port, end_port;
1250 extern int fgd_ip, verbose, current_port;
1251 extern char *fgd_host;
1254 static puOneShot *NetFGDDialogOkButton = 0;
1255 static puOneShot *NetFGDDialogCancelButton = 0;
1256 static puOneShot *NetFGDDialogScanButton = 0;
1258 static puInput *NetFGDHostDialogInput = 0;
1259 static puInput *NetFGDPortLoDialogInput = 0;
1260 static puInput *NetFGDPortHiDialogInput = 0;
1262 void NetFGDDialog_Cancel(puObject *)
1264 FG_POP_PUI_DIALOG( NetFGDDialog );
1267 void NetFGDDialog_OK (puObject *)
1271 FGTime *t = FGTime::cur_time_params;
1272 int PauseMode = t->getPause();
1273 if(!PauseMode) t->togglePauseMode();
1274 NetFGDHostDialogInput->getValue( &NetFGD );
1275 strcpy( fgd_host, NetFGD);
1276 NetFGDPortLoDialogInput->getValue( (int *) &base_port );
1277 NetFGDPortHiDialogInput->getValue( (int *) &end_port );
1278 NetFGDDialog_Cancel( NULL );
1279 if( PauseMode != t->getPause() )
1280 t->togglePauseMode();
1283 void NetFGDDialog_SCAN (puObject *)
1288 FGTime *t = FGTime::cur_time_params;
1289 int PauseMode = t->getPause();
1290 if(!PauseMode) t->togglePauseMode();
1291 // printf("Vor getvalue %s\n");
1292 NetFGDHostDialogInput->getValue( &NetFGD );
1293 // printf("Vor strcpy %s\n", (char *) NetFGD);
1294 strcpy( fgd_host, NetFGD);
1295 NetFGDPortLoDialogInput->getValue( (int *) &base_port );
1296 NetFGDPortHiDialogInput->getValue( (int *) &end_port );
1297 printf("FGD: %s Port-Start: %d Port-End: %d\n", fgd_host,
1298 base_port, end_port);
1299 net_resolv_fgd(fgd_host);
1300 printf("Resolve : %d\n", net_r);
1301 if( PauseMode != t->getPause() ) t->togglePauseMode();
1304 strcpy( fgd_name, "");
1305 for( current_port = base_port; ( current_port <= end_port); current_port++) {
1306 fgd_send_com("0" , FGFS_host);
1307 sprintf( NewNetFGDLabel , "Scanning for deamon Port: %d", current_port);
1308 printf("FGD: searching %s\n", fgd_name);
1309 if ( strcmp( fgd_name, "") != 0 ) {
1310 sprintf( NewNetFGDLabel , "Found %s at Port: %d",
1311 fgd_name, current_port);
1312 fgd_port = current_port;
1313 current_port = end_port+1;
1316 current_port = end_port = base_port = fgd_port;
1318 NetFGDDialog_Cancel( NULL );
1322 void net_fgd_scan(puObject *cb)
1324 NewFGDPortLo = base_port;
1325 NewFGDPortHi = end_port;
1326 strcpy( NewFGDHost, fgd_host);
1327 NetFGDPortLoDialogInput->setValue( NewFGDPortLo );
1328 NetFGDPortHiDialogInput->setValue( NewFGDPortHi );
1329 NetFGDHostDialogInput->setValue( NewFGDHost );
1331 FG_PUSH_PUI_DIALOG( NetFGDDialog );
1335 static void NewNetFGDInit(void)
1337 // sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1338 // sprintf( NewNetId, "%s", fgd_callsign );
1339 int len = 170 - puGetStringWidth( puGetDefaultLabelFont(),
1340 NewNetFGDLabel ) / 2;
1342 NetFGDDialog = new puDialogBox (310, 30);
1344 NetFGDDialogFrame = new puFrame (0,0,320, 170);
1345 NetFGDDialogMessage = new puText (len, 140);
1346 NetFGDDialogMessage -> setLabel (NewNetFGDLabel);
1348 NetFGDPortLoDialogInput = new puInput (50, 70, 127, 100);
1349 NetFGDPortLoDialogInput -> setValue (NewFGDPortLo);
1350 NetFGDPortLoDialogInput -> acceptInput();
1352 NetFGDPortHiDialogInput = new puInput (199, 70, 275, 100);
1353 NetFGDPortHiDialogInput -> setValue (NewFGDPortHi);
1354 NetFGDPortHiDialogInput -> acceptInput();
1356 NetFGDHostDialogInput = new puInput (50, 100, 275, 130);
1357 NetFGDHostDialogInput -> setValue (NewFGDHost);
1358 NetFGDHostDialogInput -> acceptInput();
1360 NetFGDDialogScanButton = new puOneShot (130, 10, 200, 50);
1361 NetFGDDialogScanButton -> setLegend ("Scan");
1362 NetFGDDialogScanButton -> setCallback (NetFGDDialog_SCAN);
1363 NetFGDDialogScanButton -> makeReturnDefault(FALSE);
1365 NetFGDDialogOkButton = new puOneShot (50, 10, 120, 50);
1366 NetFGDDialogOkButton -> setLegend (gui_msg_OK);
1367 NetFGDDialogOkButton -> setCallback (NetFGDDialog_OK);
1368 NetFGDDialogOkButton -> makeReturnDefault(TRUE);
1370 NetFGDDialogCancelButton = new puOneShot (210, 10, 280, 50);
1371 NetFGDDialogCancelButton -> setLegend (gui_msg_CANCEL);
1372 NetFGDDialogCancelButton -> setCallback (NetFGDDialog_Cancel);
1375 FG_FINALIZE_PUI_DIALOG( NetFGDDialog );
1379 static void net_display_toggle( puObject *cb)
1381 net_hud_display = (net_hud_display) ? 0 : 1;
1382 printf("Toggle net_hud_display : %d\n", net_hud_display);
1389 /*************** End Networking **************/
1393 /* -----------------------------------------------------------------------
1395 ---------------------------------------------------------------------*/
1396 char *fileSubmenu [] = {
1397 "Exit", /* "Close", "---------", */
1398 #if defined( WIN32 ) && !defined( __CYGWIN__)
1408 puCallback fileSubmenuCb [] = {
1409 MayBeGoodBye, /* hideMenuCb, NULL, */
1410 #if defined( WIN32 ) && !defined( __CYGWIN__)
1423 char *editSubmenu [] = {
1426 puCallback editSubmenuCb [] = {
1431 extern void fgHUDalphaAdjust( puObject * );
1432 char *viewSubmenu [] = {
1434 /* "Cockpit View > ", "View >","------------", */
1435 "Toggle Panel...", NULL
1437 puCallback viewSubmenuCb [] = {
1439 /* notCb, notCb, NULL, */
1440 guiTogglePanel, NULL
1443 char *aircraftSubmenu [] = {
1444 "Autopilot", "Heading", "Altitude", "Navigation", "Airport",
1445 /* "Communication", */ NULL
1447 puCallback aircraftSubmenuCb [] = {
1448 fgAPAdjust, NewHeading, NewAltitude, fgLatLonFormatToggle, NewTgtAirport,
1452 char *environmentSubmenu [] = {
1454 "Goto Airport", /* "Terrain", "Weather", */ NULL
1456 puCallback environmentSubmenuCb [] = {
1458 NewAirport, /* notCb, notCb, */ NULL
1462 char *optionsSubmenu [] = {
1463 "Preferences", "Realism & Reliablity...", NULL
1465 puCallback optionsSubmenuCb [] = {
1470 #ifdef FG_NETWORK_OLK
1471 char *networkSubmenu [] = {
1472 "Unregister from FGD ", /* "Send MSG to All", "Send MSG", "Show Pilots", */
1474 "Scan for Deamons", "Enter Callsign", /* "Display Netinfos", */
1475 "Toggle Display", NULL
1477 puCallback networkSubmenuCb [] = {
1478 /* notCb, notCb, notCb, notCb, */
1481 net_fgd_scan, NewCallSign,
1482 net_display_toggle, NULL
1486 char *helpSubmenu [] = {
1487 /* "About...", */ "Help", NULL
1489 puCallback helpSubmenuCb [] = {
1490 /* notCb, */ helpCb, NULL
1494 /* -------------------------------------------------------------------------
1496 _____________________________________________________________________*/
1501 char *mesa_win_state;
1505 puSetDefaultStyle ( PUSTYLE_SMALL_BEVELLED ); //PUSTYLE_DEFAULT
1506 puSetDefaultColourScheme (0.8, 0.8, 0.8, 0.4);
1508 // Initialize our GLOBAL GUI STRINGS
1509 gui_msg_OK = msg_OK; // "OK"
1510 gui_msg_NO = msg_NO; // "NO"
1511 gui_msg_YES = msg_YES; // "YES"
1512 gui_msg_CANCEL = msg_CANCEL; // "CANCEL"
1513 gui_msg_RESET = msg_RESET; // "RESET"
1515 // Next check home directory
1517 char* envp = ::getenv( "FG_FONTS" );
1518 if ( envp != NULL ) {
1519 fntpath.set( envp );
1521 fntpath.set( current_options.get_fg_root() );
1522 fntpath.append( "Fonts" );
1525 // Install our fast fonts
1526 fntpath.append( "typewriter.txf" );
1527 guiFntHandle = new fntTexFont ;
1528 guiFntHandle -> load ( (char *)fntpath.c_str() ) ;
1529 puFont GuiFont ( guiFntHandle, 15 ) ;
1530 puSetDefaultFonts( GuiFont, GuiFont ) ;
1531 guiFnt = puGetDefaultLabelFont();
1533 if ( current_options.get_mouse_pointer() == 0 ) {
1534 // no preference specified for mouse pointer, attempt to autodetect...
1535 // Determine if we need to render the cursor, or if the windowing
1536 // system will do it. First test if we are rendering with glide.
1537 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1538 // Test for the MESA_GLX_FX env variable
1539 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
1540 // test if we are fullscreen mesa/glide
1541 if ( (mesa_win_state[0] == 'f') ||
1542 (mesa_win_state[0] == 'F') ) {
1547 mouse_active = ~mouse_active;
1548 } else if ( current_options.get_mouse_pointer() == 1 ) {
1549 // don't show pointer
1550 } else if ( current_options.get_mouse_pointer() == 2 ) {
1551 // force showing pointer
1553 mouse_active = ~mouse_active;
1556 // MOUSE_VIEW mode stuff
1557 trackball(_quat0, 0.0, 0.0, 0.0, 0.0);
1559 build_rotmatrix(quat_mat, curquat);
1561 // Set up our Dialog Boxes
1562 ConfirmExitDialogInit();
1564 #ifdef FG_NETWORK_OLK
1570 // Make the menu bar
1571 mainMenuBar = new puMenuBar ();
1572 mainMenuBar -> add_submenu ("File", fileSubmenu, fileSubmenuCb);
1573 // mainMenuBar -> add_submenu ("Edit", editSubmenu, editSubmenuCb);
1574 mainMenuBar -> add_submenu ("View", viewSubmenu, viewSubmenuCb);
1575 mainMenuBar -> add_submenu ("Aircraft", aircraftSubmenu, aircraftSubmenuCb);
1576 mainMenuBar -> add_submenu ("Environment", environmentSubmenu, environmentSubmenuCb);
1577 // mainMenuBar -> add_submenu ("Options", optionsSubmenu, optionsSubmenuCb);
1578 #ifdef FG_NETWORK_OLK
1579 mainMenuBar -> add_submenu ("Network", networkSubmenu, networkSubmenuCb);
1581 mainMenuBar -> add_submenu ("Help", helpSubmenu, helpSubmenuCb);
1582 mainMenuBar-> close ();
1583 // Set up menu bar toggle
1592 * Implementation of a virtual trackball.
1593 * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
1594 * the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
1596 * Vector manip code:
1598 * Original code from:
1599 * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
1601 * Much mucking with by:
1604 #if defined(_WIN32) && !defined( __CYGWIN32__ )
1605 #pragma warning (disable:4244) /* disable bogus conversion warnings */
1609 //#include "trackball.h"
1612 * This size should really be based on the distance from the center of
1613 * rotation to the point on the object underneath the mouse. That
1614 * point would then track the mouse as closely as possible. This is a
1615 * simple example, though, so that is left as an Exercise for the
1618 #define TRACKBALLSIZE (0.8f)
1619 #define SQRT(x) sqrt(x)
1622 * Local function prototypes (not defined in trackball.h)
1624 static float tb_project_to_sphere(float, float, float);
1625 static void normalize_quat(float [4]);
1636 vset(float *v, float x, float y, float z)
1644 vsub(const float *src1, const float *src2, float *dst)
1646 dst[0] = src1[0] - src2[0];
1647 dst[1] = src1[1] - src2[1];
1648 dst[2] = src1[2] - src2[2];
1652 vcopy(const float *v1, float *v2)
1655 for (i = 0 ; i < 3 ; i++)
1660 vcross(const float *v1, const float *v2, float *cross)
1664 temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
1665 temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
1666 temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
1671 vlength(const float *v)
1673 float tmp = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
1678 vscale(float *v, float div)
1688 vscale(v,1.0/vlength(v));
1692 vdot(const float *v1, const float *v2)
1694 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
1698 vadd(const float *src1, const float *src2, float *dst)
1700 dst[0] = src1[0] + src2[0];
1701 dst[1] = src1[1] + src2[1];
1702 dst[2] = src1[2] + src2[2];
1706 * Given an axis and angle, compute quaternion.
1709 axis_to_quat(float a[3], float phi, float q[4])
1711 double sinphi2, cosphi2;
1712 double phi2 = phi/2.0;
1713 sinphi2 = sin(phi2);
1714 cosphi2 = cos(phi2);
1722 * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
1723 * if we are away from the center of the sphere.
1726 tb_project_to_sphere(float r, float x, float y)
1732 if (d < r * 0.70710678118654752440) { /* Inside sphere */
1735 } else { /* On hyperbola */
1736 t = r / 1.41421356237309504880;
1743 * Quaternions always obey: a^2 + b^2 + c^2 + d^2 = 1.0
1744 * If they don't add up to 1.0, dividing by their magnitued will
1747 * Note: See the following for more information on quaternions:
1749 * - Shoemake, K., Animating rotation with quaternion curves, Computer
1750 * Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
1751 * - Pletinckx, D., Quaternion calculus as a basic tool in computer
1752 * graphics, The Visual Computer 5, 2-13, 1989.
1755 normalize_quat(float q[4])
1760 tmp = q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3];
1761 mag = 1.0 / SQRT(tmp);
1762 for (i = 0; i < 4; i++)
1767 * Ok, simulate a track-ball. Project the points onto the virtual
1768 * trackball, then figure out the axis of rotation, which is the cross
1769 * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
1770 * Note: This is a deformed trackball-- is a trackball in the center,
1771 * but is deformed into a hyperbolic sheet of rotation away from the
1772 * center. This particular function was chosen after trying out
1773 * several variations.
1775 * It is assumed that the arguments to this routine are in the range
1779 trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
1781 float a[3]; /* Axis of rotation */
1782 float phi; /* how much to rotate about axis */
1783 float p1[3], p2[3], d[3];
1786 if (p1x == p2x && p1y == p2y) {
1794 * First, figure out z-coordinates for projection of P1 and P2 to
1797 vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
1798 vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
1801 * Now, we want the cross product of P1 and P2
1806 * Figure out how much to rotate around that axis.
1809 t = vlength(d) / (2.0*TRACKBALLSIZE);
1812 * Avoid problems with out-of-control values...
1814 if (t > 1.0) t = 1.0;
1815 if (t < -1.0) t = -1.0;
1816 phi = 2.0 * asin(t);
1818 axis_to_quat(a,phi,q);
1822 * Given two rotations, e1 and e2, expressed as quaternion rotations,
1823 * figure out the equivalent single rotation and stuff it into dest.
1825 * This routine also normalizes the result every RENORMCOUNT times it is
1826 * called, to keep error from creeping in.
1828 * NOTE: This routine is written so that q1 or q2 may be the same
1829 * as dest (or each other).
1832 #define RENORMCOUNT 97
1835 add_quats(float q1[4], float q2[4], float dest[4])
1838 float t1[4], t2[4], t3[4];
1842 printf("q1 = %f %f %f %f\n", q1[0], q1[1], q1[2], q1[3]);
1843 printf("q2 = %f %f %f %f\n", q2[0], q2[1], q2[2], q2[3]);
1855 tf[3] = q1[3] * q2[3] - vdot(q1,q2);
1858 printf("tf = %f %f %f %f\n", tf[0], tf[1], tf[2], tf[3]);
1866 if (++count > RENORMCOUNT) {
1868 normalize_quat(dest);
1873 * Build a rotation matrix, given a quaternion rotation.
1877 build_rotmatrix(float m[4][4], float q[4])
1879 //#define TRANSPOSED_QUAT
1880 #ifndef TRANSPOSED_QUAT
1881 m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
1882 m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
1883 m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
1886 m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
1887 m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
1888 m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
1891 m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
1892 m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
1893 m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
1900 #else // TRANSPOSED_QUAT
1901 m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
1902 m[0][1] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
1903 m[0][2] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
1906 m[1][0] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
1907 m[1][1] = 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
1908 m[1][2] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
1911 m[2][0] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
1912 m[2][1] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
1913 m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);