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>
54 #include <simgear/constants.h>
55 #include <simgear/debug/logstream.hxx>
56 #include <simgear/misc/fgpath.hxx>
57 #include <simgear/screen/screen-dump.hxx>
59 #include <Include/general.hxx>
60 #include <Aircraft/aircraft.hxx>
61 #include <Airports/simple.hxx>
62 #include <Autopilot/auto_gui.hxx>
63 #include <Autopilot/newauto.hxx>
64 #include <Cockpit/panel.hxx>
65 #include <Controls/controls.hxx>
66 #include <FDM/flight.hxx>
67 #include <Main/bfi.hxx>
68 #include <Main/fg_init.hxx>
69 #include <Main/globals.hxx>
70 #include <Main/options.hxx>
71 #include <Main/views.hxx>
72 #include <Main/save.hxx>
74 #include <NetworkOLK/network.h>
77 #if defined( WIN32 ) && !defined( __CYGWIN__ )
78 # include <simgear/screen/win32-printer.h>
79 # include <simgear/screen/GlBitmaps.h>
84 * A virtual trackball implementation
85 * Written by Gavin Bell for Silicon Graphics, November 1988.
88 * Pass the x and y coordinates of the last and current positions of
89 * the mouse, scaled so they are from (-1.0 ... 1.0).
91 * The resulting rotation is returned as a quaternion rotation in the
95 trackball(float q[4], float p1x, float p1y, float p2x, float p2y);
98 * Given two quaternions, add them together to get a third quaternion.
99 * Adding quaternions to get a compound rotation is analagous to adding
100 * translations to get a compound translation. When incrementally
101 * adding rotations, the first argument here should be the new
103 * rotation, the second and third the total rotation (which will be
104 * over-written with the resulting new total rotation).
107 add_quats(float *q1, float *q2, float *dest);
110 * A useful function, builds a rotation matrix in Matrix based on
114 build_rotmatrix(float m[4][4], float q[4]);
117 * This function computes a quaternion based on an axis (defined by
118 * the given vector) and an angle about which to rotate. The angle is
119 * expressed in radians. The result is put into the third argument.
122 axis_to_quat(float a[3], float phi, float q[4]);
127 FG_USING_STD(string);
129 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
133 #if defined(WIN32) || defined(__CYGWIN32__)
134 #define WIN32_CURSOR_TWEAKS
135 #elif (GLUT_API_VERSION >= 4 || GLUT_XLIB_IMPLEMENTATION >= 9)
136 #define X_CURSOR_TWEAKS
139 // hack, should come from an include someplace
140 extern void fgInitVisuals( void );
141 extern void fgReshape( int width, int height );
142 extern void fgRenderFrame( void );
145 fntTexFont *guiFntHandle = 0;
147 static puMenuBar *mainMenuBar = 0;
148 //static puButton *hideMenuButton = 0;
150 static puDialogBox *dialogBox = 0;
151 static puFrame *dialogFrame = 0;
152 static puText *dialogBoxMessage = 0;
153 static puOneShot *dialogBoxOkButton = 0;
156 static puDialogBox *YNdialogBox = 0;
157 static puFrame *YNdialogFrame = 0;
158 static puText *YNdialogBoxMessage = 0;
159 static puOneShot *YNdialogBoxOkButton = 0;
160 static puOneShot *YNdialogBoxNoButton = 0;
162 static char msg_OK[] = "OK";
163 static char msg_NO[] = "NO";
164 static char msg_YES[] = "YES";
165 static char msg_CANCEL[] = "Cancel";
166 static char msg_RESET[] = "Reset";
168 char *gui_msg_OK; // "OK"
169 char *gui_msg_NO; // "NO"
170 char *gui_msg_YES; // "YES"
171 char *gui_msg_CANCEL; // "CANCEL"
172 char *gui_msg_RESET; // "RESET"
174 static char global_dialog_string[256];
176 // from autopilot.cxx
177 // extern void NewAltitude( puObject *cb );
178 // extern void NewHeading( puObject *cb );
179 // extern void fgAPAdjust( puObject * );
180 // extern void NewTgtAirport( puObject *cb );
181 // bool fgAPTerrainFollowEnabled( void );
182 // bool fgAPAltitudeEnabled( void );
183 // bool fgAPHeadingEnabled( void );
184 // bool fgAPWayPointEnabled( void );
185 // bool fgAPAutoThrottleEnabled( void );
188 extern void fgLatLonFormatToggle( puObject *);
190 /* --------------------------------------------------------------------
192 ---------------------------------------------------------------------*/
196 static int _savedX = 0;
197 static int _savedY = 0;
198 static int last_buttons = 0 ;
199 static int mouse_active = 0;
200 static int menu_on = 0;
201 static int mouse_joystick_control = 0;
203 //static time_t mouse_off_time;
204 //static int mouse_timed_out;
206 // to allow returning to previous view
207 // on second left click in MOUSE_VIEW mode
208 // This has file scope so that it can be reset
209 // if the little rodent is moved NHV
210 static int _mVtoggle;
212 // we break up the glutGetModifiers return mask
213 // once per loop and stash what we need in these
214 static int glut_active_shift;
215 static int glut_active_ctrl;
216 static int glut_active_alt;
218 static float lastquat[4];
219 static float curquat[4];
220 static float _quat0[4];
221 float quat_mat[4][4];
223 // uncomment this for view to exactly follow mouse in MOUSE_VIEW mode
224 // else smooth out the view panning to .01 radian per frame
225 // see view_offset smoothing mechanism in main.cxx
226 #define NO_SMOOTH_MOUSE_VIEW
228 // uncomment following to
229 #define RESET_VIEW_ON_LEAVING_MOUSE_VIEW
231 /* --------------------------------------------------------------------
232 Support for mouse as control yoke (david@megginson.com)
234 - right button toggles between pointer and yoke
235 - horizontal drag with no buttons moves ailerons
236 - vertical drag with no buttons moves elevators
237 - horizontal drag with left button moves brakes (left=on)
238 - vertical drag with left button moves throttle (up=more)
239 - horizontal drag with middle button moves rudder
240 - vertical drag with middle button moves trim
242 For the *_sensitivity variables, a lower number means more sensitive.
244 TODO: figure out how to keep pointer from leaving window in yoke mode.
245 TODO: add thresholds and null zones
246 TODO: sensitivity should be configurable at user option.
247 TODO: allow differential braking (this will be useful if FlightGear
248 ever supports tail-draggers like the DC-3)
249 ---------------------------------------------------------------------*/
257 MouseMode mouse_mode = MOUSE_POINTER;
259 static double aileron_sensitivity = 1.0/500.0;
260 static double elevator_sensitivity = 1.0/500.0;
261 static double brake_sensitivity = 1.0/250.0;
262 static double throttle_sensitivity = 1.0/250.0;
263 static double rudder_sensitivity = 1.0/500.0;
264 static double trim_sensitivity = 1.0/1000.0;
266 static inline void Quat0( void ) {
267 curquat[0] = _quat0[0];
268 curquat[1] = _quat0[1];
269 curquat[2] = _quat0[2];
270 curquat[3] = _quat0[3];
273 static inline int left_button( void ) {
274 return( last_buttons & (1 << GLUT_LEFT_BUTTON) );
277 static inline int middle_button( void ) {
278 return( last_buttons & (1 << GLUT_MIDDLE_BUTTON) );
281 static inline int right_button( void ) {
282 return( last_buttons & (1 << GLUT_RIGHT_BUTTON) );
285 static inline void TurnCursorOn( void )
288 #if defined(WIN32_CURSOR_TWEAKS)
289 switch (mouse_mode) {
291 glutSetCursor(GLUT_CURSOR_INHERIT);
294 glutSetCursor(GLUT_CURSOR_CROSSHAIR);
297 glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
301 #if defined(X_CURSOR_TWEAKS)
302 glutWarpPointer( current_view.get_winWidth()/2, current_view.get_winHeight()/2);
306 static inline void TurnCursorOff( void )
309 #if defined(WIN32_CURSOR_TWEAKS)
310 glutSetCursor(GLUT_CURSOR_NONE);
311 #elif defined(X_CURSOR_TWEAKS)
312 glutWarpPointer( current_view.get_winWidth(), current_view.get_winHeight());
316 void maybeToggleMouse( void )
318 #if defined(WIN32_CURSOR_TWEAKS)
319 static int first_time = ~0;
320 static int mouse_changed = 0;
324 mouse_changed = ~mouse_changed;
328 if( mouse_mode != MOUSE_POINTER )
330 if( mouse_changed ) {
331 mouse_changed = ~mouse_changed;
337 first_time = ~first_time;
338 #endif // #ifdef WIN32
341 // Call with FALSE to init and TRUE to restore
342 void BusyCursor( int restore )
344 static GLenum cursor = (GLenum) 0;
346 glutSetCursor(cursor);
348 cursor = (GLenum) glutGet( (GLenum) GLUT_WINDOW_CURSOR );
349 #if defined(WIN32_CURSOR_TWEAKS)
352 glutSetCursor( GLUT_CURSOR_WAIT );
356 int guiGetMouseButton(void)
361 void guiGetMouse(int *x, int *y)
367 void guiMotionFunc ( int x, int y )
369 int ww, wh, need_warp = 0;
373 if (mouse_mode == MOUSE_POINTER) {
375 glutPostRedisplay () ;
377 if( x == _mX && y == _mY)
380 // reset left click MOUSE_VIEW toggle feature
383 ww = current_view.get_winWidth();
384 wh = current_view.get_winHeight();
386 switch (mouse_mode) {
388 if( !mouse_joystick_control ) {
389 mouse_joystick_control = 1;
390 current_options.set_control_mode( fgOPTIONS::FG_MOUSE );
392 if ( left_button() ) {
393 offset = (_mX - x) * brake_sensitivity;
394 controls.move_brake(FGControls::ALL_WHEELS, offset);
395 offset = (_mY - y) * throttle_sensitivity;
396 controls.move_throttle(FGControls::ALL_ENGINES, offset);
397 } else if ( right_button() ) {
398 if( ! current_autopilot->get_HeadingEnabled() ) {
399 offset = (x - _mX) * rudder_sensitivity;
400 controls.move_rudder(offset);
402 if( ! current_autopilot->get_AltitudeEnabled() ) {
403 offset = (_mY - y) * trim_sensitivity;
404 controls.move_elevator_trim(offset);
407 if( ! current_autopilot->get_HeadingEnabled() ) {
408 offset = (x - _mX) * aileron_sensitivity;
409 controls.move_aileron(offset);
411 if( ! current_autopilot->get_AltitudeEnabled() ) {
412 offset = (_mY - y) * elevator_sensitivity;
413 controls.move_elevator(offset);
417 // Keep the mouse in the window.
418 if (x < 5 || x > ww-5 || y < 5 || y > wh-5) {
427 #define CONTRAINED_MOUSE_VIEW_Y
428 #ifdef CONTRAINED_MOUSE_VIEW_Y
432 #endif // CONTRAINED_MOUSE_VIEW_Y
434 } else if( y >= wh-1) {
435 #ifdef CONTRAINED_MOUSE_VIEW_Y
439 #endif // CONTRAINED_MOUSE_VIEW_Y
442 // wrap MOUSE_VIEW mode cursor x position
446 } else if ( x >= ww-1 ) {
450 // try to get FG_PI movement in each half of screen
454 if( middle_button() ) {
456 (2.0f * _mX - W) / W,
457 0, //(H - 2.0f * y) / H, // 3
459 0 //(H - 2.0f * _mY) / H // 1
466 0, //(2.0f * _mX - W) / W, // 0
467 (H - 2.0f * y) / H, // 3
468 0, //(2.0f * x - W) / W, // 2
469 (H - 2.0f * _mY) / H // 1
472 add_quats(lastquat, curquat, curquat);
473 build_rotmatrix(quat_mat, curquat);
476 // this could be done in above quat
477 // but requires redoing view pipeline
478 offset = current_view.get_goal_view_offset();
479 offset += ((_mX - x) * FG_2PI / W );
480 while (offset < 0.0) {
483 while (offset > FG_2PI) {
486 current_view.set_goal_view_offset(offset);
487 #ifdef NO_SMOOTH_MOUSE_VIEW
488 current_view.set_view_offset(offset);
497 glutWarpPointer(x, y);
499 // Record the new mouse position.
505 void guiMouseFunc(int button, int updown, int x, int y)
509 // private MOUSE_VIEW state variables
510 // to allow alternate left clicks in MOUSE_VIEW mode
511 // to toggle between current offsets and straight ahead
513 static int _mVx, _mVy, _Vx, _Vy;
514 static float _quat[4];
515 static double _view_offset;
517 // general purpose variables
520 glutModifiers = glutGetModifiers();
521 glut_active_shift = glutModifiers & GLUT_ACTIVE_SHIFT;
522 glut_active_ctrl = glutModifiers & GLUT_ACTIVE_CTRL;
523 glut_active_alt = glutModifiers & GLUT_ACTIVE_ALT;
525 // Was the left button pressed?
526 if (updown == GLUT_DOWN ) {
527 if( button == GLUT_LEFT_BUTTON)
529 switch (mouse_mode) {
536 // resume previous view offsets
541 curquat[0] = _quat[0];
542 curquat[1] = _quat[1];
543 curquat[2] = _quat[2];
544 curquat[3] = _quat[3];
545 current_view.set_goal_view_offset(_view_offset);
546 #ifdef NO_SMOOTH_MOUSE_VIEW
547 current_view.set_view_offset(_view_offset);
555 _quat[0] = curquat[0];
556 _quat[1] = curquat[1];
557 _quat[2] = curquat[2];
558 _quat[3] = curquat[3];
559 x = current_view.get_winWidth()/2;
560 y = current_view.get_winHeight()/2;
562 _view_offset = current_view.get_goal_view_offset();
563 current_view.set_goal_view_offset(0.0);
564 #ifdef NO_SMOOTH_MOUSE_VIEW
565 current_view.set_view_offset(0.0);
568 glutWarpPointer( x , y);
569 build_rotmatrix(quat_mat, curquat);
570 _mVtoggle = ~_mVtoggle;
573 }else if ( button == GLUT_RIGHT_BUTTON) {
574 switch (mouse_mode) {
576 mouse_mode = MOUSE_YOKE;
577 mouse_joystick_control = 0;
580 // start with zero point in center of screen
581 _mX = current_view.get_winWidth()/2;
582 _mY = current_view.get_winHeight()/2;
584 // try to have the MOUSE_YOKE position
585 // reflect the current stick position
586 offset = controls.get_aileron();
587 x = _mX - (int)(offset * aileron_sensitivity);
588 offset = controls.get_elevator();
589 y = _mY - (int)(offset * elevator_sensitivity);
591 glutSetCursor(GLUT_CURSOR_CROSSHAIR);
592 FG_LOG( FG_INPUT, FG_INFO, "Mouse in yoke mode" );
596 mouse_mode = MOUSE_VIEW;
597 current_options.set_control_mode( fgOPTIONS::FG_JOYSTICK );
598 x = current_view.get_winWidth()/2;
599 y = current_view.get_winHeight()/2;
602 build_rotmatrix(quat_mat, curquat);
603 glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
604 FG_LOG( FG_INPUT, FG_INFO, "Mouse in view mode" );
608 mouse_mode = MOUSE_POINTER;
611 #ifdef RESET_VIEW_ON_LEAVING_MOUSE_VIEW
613 build_rotmatrix(quat_mat, curquat);
614 current_view.set_goal_view_offset(0.0);
615 #ifdef NO_SMOOTH_MOUSE_VIEW
616 current_view.set_view_offset(0.0);
618 #endif // RESET_VIEW_ON_LEAVING_MOUSE_VIEW
619 glutSetCursor(GLUT_CURSOR_INHERIT);
624 FG_LOG( FG_INPUT, FG_INFO, "Mouse in pointer mode" );
627 glutWarpPointer( x, y );
628 } // END RIGHT BUTTON
629 } // END UPDOWN == GLUT_DOWN
631 // Note which button is pressed.
632 if ( updown == GLUT_DOWN ) {
633 last_buttons |= ( 1 << button ) ;
635 last_buttons &= ~( 1 << button ) ;
638 // If we're in pointer mode, let PUI
639 // know what's going on.
640 if (mouse_mode == MOUSE_POINTER) {
641 if (!puMouse (button, updown, x,y)) {
642 current_panel->doMouseAction(button, updown, x, y);
646 // Register the new position (if it
647 // hasn't been registered already).
651 glutPostRedisplay ();
654 /* ================ General Purpose Functions ================ */
656 // Intercept the Escape Key
657 void ConfirmExitDialog(void)
659 FG_PUSH_PUI_DIALOG( YNdialogBox );
662 // General Purpose Message Box
663 void mkDialog (const char *txt)
665 strncpy(global_dialog_string, txt, 256);
666 dialogBoxMessage->setLabel(global_dialog_string);
667 FG_PUSH_PUI_DIALOG( dialogBox );
670 // Toggle the Menu and Mouse display state
671 void guiToggleMenu(void)
674 // printf("Hiding Menu\n");
675 mainMenuBar->hide ();
676 #if defined(WIN32_CURSOR_TWEAKS)
677 if( mouse_mode == MOUSE_POINTER )
679 #endif // #ifdef WIN32_CURSOR_TWEAKS
681 // printf("Showing Menu\n");
682 mainMenuBar->reveal();
685 #endif // #ifdef WIN32
690 /* -----------------------------------------------------------------------
691 the Gui callback functions
692 ____________________________________________________________________*/
694 static void saveFlight(puObject *cv)
697 ofstream output("fgfs.sav");
698 if (output.good() && fgSaveFlight(output)) {
700 mkDialog("Saved flight to ./fgfs.sav");
701 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
703 mkDialog("Cannot save flight to ./fgfs.sav");
704 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
709 static void loadFlight(puObject *cb)
712 ifstream input("fgfs.sav");
713 if (input.good() && fgLoadFlight(input)) {
715 mkDialog("Loaded flight from fgfs.sav");
716 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from ./fgfs.sav");
718 mkDialog("Failed to load flight from fgfs.sav");
719 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from ./fgfs.sav");
724 void reInit(puObject *cb)
728 build_rotmatrix(quat_mat, curquat);
729 fgReInitSubsystems();
733 static void toggleClouds(puObject *cb)
735 FGBFI::setClouds( !FGBFI::getClouds() );
738 // This is the accessor function
739 void guiTogglePanel(puObject *cb)
741 current_options.toggle_panel();
744 //void MenuHideMenuCb(puObject *cb)
745 void hideMenuCb (puObject *cb)
750 void goodBye(puObject *)
752 // FG_LOG( FG_INPUT, FG_ALERT,
753 // "Program exiting normally at user request." );
754 cout << "Program exiting normally at user request." << endl;
756 #ifdef FG_NETWORK_OLK
757 if ( current_options.get_network_olk() ) {
758 if ( net_is_registered == 0 ) fgd_send_com( "8", FGFS_host);
769 void goAwayCb (puObject *me)
771 FG_POP_PUI_DIALOG( dialogBox );
774 void mkDialogInit (void)
776 // printf("mkDialogInit\n");
777 int x = (current_options.get_xsize()/2 - 400/2);
778 int y = (current_options.get_ysize()/2 - 100/2);
779 dialogBox = new puDialogBox (x, y); // 150, 50
781 dialogFrame = new puFrame (0,0,400,100);
782 dialogBoxMessage = new puText (10, 70);
783 dialogBoxMessage -> setLabel ("");
784 dialogBoxOkButton = new puOneShot (180, 10, 240, 50);
785 dialogBoxOkButton -> setLegend (gui_msg_OK);
786 dialogBoxOkButton -> makeReturnDefault (TRUE );
787 dialogBoxOkButton -> setCallback (goAwayCb);
789 FG_FINALIZE_PUI_DIALOG( dialogBox );
792 void MayBeGoodBye(puObject *)
797 void goAwayYesNoCb(puObject *me)
799 FG_POP_PUI_DIALOG( YNdialogBox);
802 void ConfirmExitDialogInit(void)
804 char msg[] = "Really Quit";
807 // printf("ConfirmExitDialogInit\n");
808 int len = 200 - puGetStringWidth( puGetDefaultLabelFont(), msg )/2;
810 int x = (current_options.get_xsize()/2 - 400/2);
811 int y = (current_options.get_ysize()/2 - 100/2);
813 YNdialogBox = new puDialogBox (x, y); // 150, 50
814 // YNdialogBox = new puDialogBox (150, 50);
816 YNdialogFrame = new puFrame (0,0,400, 100);
818 YNdialogBoxMessage = new puText (len, 70);
819 YNdialogBoxMessage -> setDefaultValue (msg);
820 YNdialogBoxMessage -> getDefaultValue (&s);
821 YNdialogBoxMessage -> setLabel (s);
823 YNdialogBoxOkButton = new puOneShot (100, 10, 160, 50);
824 YNdialogBoxOkButton -> setLegend (gui_msg_OK);
825 YNdialogBoxOkButton -> makeReturnDefault (TRUE );
826 YNdialogBoxOkButton -> setCallback (goodBye);
828 YNdialogBoxNoButton = new puOneShot (240, 10, 300, 50);
829 YNdialogBoxNoButton -> setLegend (gui_msg_NO);
830 YNdialogBoxNoButton -> setCallback (goAwayYesNoCb);
832 FG_FINALIZE_PUI_DIALOG( YNdialogBox );
835 void notCb (puObject *)
837 mkDialog ("This function isn't implemented yet");
840 void helpCb (puObject *)
844 #if defined(FX) && !defined(WIN32)
845 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
846 if ( global_fullscreen ) {
847 global_fullscreen = false;
848 XMesaSetFXmode( XMESA_FX_WINDOW );
854 string url = "http://www.flightgear.org/Docs/InstallGuide/getstart.html";
856 if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
857 command = "netscape -remote \"openURL(" + url + ")\" &";
859 command = "netscape " + url + " &";
862 command = "webrun.bat";
865 system( command.c_str() );
866 //string text = "Help started in netscape window.";
868 //mkDialog (text.c_str());
869 mkDialog ("Help started in netscape window.");
872 #if defined( WIN32 ) && !defined( __CYGWIN__)
874 static void rotateView( double roll, double pitch, double yaw )
879 static GlBitmap *b1 = NULL;
880 extern FGInterface cur_view_fdm;
881 GLubyte *hiResScreenCapture( int multiplier )
883 float oldfov = current_options.get_fov();
884 float fov = oldfov / multiplier;
885 FGView *v = ¤t_view;
886 current_options.set_fov(fov);
887 v->force_update_fov_math();
889 int cur_width = current_view.get_winWidth( );
890 int cur_height = current_view.get_winHeight( );
891 if (b1) delete( b1 );
892 // New empty (mostly) bitmap
893 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
895 for ( y = 0; y < multiplier; y++ )
897 for ( x = 0; x < multiplier; x++ )
899 fgReshape( cur_width, cur_height );
901 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
905 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
908 current_view.UpdateViewParams(cur_view_fdm);
909 current_options.set_fov(oldfov);
910 v->force_update_fov_math();
911 return b1->getBitmap();
916 #if defined( WIN32 ) && !defined( __CYGWIN__)
917 // win32 print screen function
918 void printScreen ( puObject *obj ) {
919 bool show_pu_cursor = false;
921 if ( !puCursorIsHidden() ) {
922 show_pu_cursor = true;
928 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
929 int cur_width = current_view.get_winWidth( );
930 int cur_height = current_view.get_winHeight( );
931 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
932 p.End( hiResScreenCapture(3) );
935 mainMenuBar->reveal();
938 if ( show_pu_cursor ) {
943 #endif // #ifdef WIN32
946 void dumpSnapShot ( puObject *obj ) {
951 // do a screen snap shot
952 void fgDumpSnapShot () {
953 bool show_pu_cursor = false;
955 int freeze = globals->get_freeze();
957 globals->set_freeze( true );
961 if ( !puCursorIsHidden() ) {
962 show_pu_cursor = true;
967 fgReshape( current_view.get_winWidth(), current_view.get_winHeight() );
969 // we need two render frames here to clear the menu and cursor
970 // ... not sure why but doing an extra fgFenderFrame() shoulnd't
975 my_glDumpWindow( "fgfs-screen.ppm",
976 current_options.get_xsize(),
977 current_options.get_ysize() );
979 mkDialog ("Snap shot saved to fgfs-screen.ppm");
981 if ( show_pu_cursor ) {
987 mainMenuBar->reveal();
991 globals->set_freeze( false );
995 /// The beginnings of teleportation :-)
996 // Needs cleaning up but works
997 // These statics should disapear when this is a class
998 static puDialogBox *AptDialog = 0;
999 static puFrame *AptDialogFrame = 0;
1000 static puText *AptDialogMessage = 0;
1001 static puInput *AptDialogInput = 0;
1003 static char NewAirportId[16];
1004 static char NewAirportLabel[] = "Enter New Airport ID";
1006 static puOneShot *AptDialogOkButton = 0;
1007 static puOneShot *AptDialogCancelButton = 0;
1008 static puOneShot *AptDialogResetButton = 0;
1010 void AptDialog_Cancel(puObject *)
1012 FG_POP_PUI_DIALOG( AptDialog );
1015 void AptDialog_OK (puObject *)
1017 FGPath path( current_options.get_fg_root() );
1018 path.append( "Airports" );
1019 path.append( "simple.mk4" );
1020 FGAirports airports( path.c_str() );
1024 int freeze = globals->get_freeze();
1026 globals->set_freeze( true );
1029 AptDialogInput->getValue(&s);
1032 cout << "AptDialog_OK " << AptId << " " << AptId.length() << endl;
1034 AptDialog_Cancel( NULL );
1036 if ( AptId.length() ) {
1037 // set initial position from airport id
1038 FG_LOG( FG_GENERAL, FG_INFO,
1039 "Attempting to set starting position from airport code "
1042 if ( airports.search( AptId, &a ) )
1044 current_options.set_airport_id( AptId.c_str() );
1046 fgReInitSubsystems();
1049 AptId += " not in database.";
1050 mkDialog(AptId.c_str());
1054 globals->set_freeze( false );
1057 void AptDialog_Reset(puObject *)
1059 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
1060 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1061 AptDialogInput->setValue ( NewAirportId );
1062 AptDialogInput->setCursor( 0 ) ;
1065 void NewAirport(puObject *cb)
1067 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
1068 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1069 // cout << "NewAirport " << NewAirportId << endl;
1070 AptDialogInput->setValue( NewAirportId );
1072 FG_PUSH_PUI_DIALOG( AptDialog );
1075 static void NewAirportInit(void)
1077 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1078 int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
1079 NewAirportLabel ) / 2;
1081 AptDialog = new puDialogBox (150, 50);
1083 AptDialogFrame = new puFrame (0,0,350, 150);
1084 AptDialogMessage = new puText (len, 110);
1085 AptDialogMessage -> setLabel (NewAirportLabel);
1087 AptDialogInput = new puInput (50, 70, 300, 100);
1088 AptDialogInput -> setValue (NewAirportId);
1089 AptDialogInput -> acceptInput();
1091 AptDialogOkButton = new puOneShot (50, 10, 110, 50);
1092 AptDialogOkButton -> setLegend (gui_msg_OK);
1093 AptDialogOkButton -> setCallback (AptDialog_OK);
1094 AptDialogOkButton -> makeReturnDefault(TRUE);
1096 AptDialogCancelButton = new puOneShot (140, 10, 210, 50);
1097 AptDialogCancelButton -> setLegend (gui_msg_CANCEL);
1098 AptDialogCancelButton -> setCallback (AptDialog_Cancel);
1100 AptDialogResetButton = new puOneShot (240, 10, 300, 50);
1101 AptDialogResetButton -> setLegend (gui_msg_RESET);
1102 AptDialogResetButton -> setCallback (AptDialog_Reset);
1104 cout << "NewAirportInit " << NewAirportId << endl;
1105 FG_FINALIZE_PUI_DIALOG( AptDialog );
1108 #ifdef FG_NETWORK_OLK
1110 /// The beginnings of networking :-)
1111 // Needs cleaning up but works
1112 // These statics should disapear when this is a class
1113 static puDialogBox *NetIdDialog = 0;
1114 static puFrame *NetIdDialogFrame = 0;
1115 static puText *NetIdDialogMessage = 0;
1116 static puInput *NetIdDialogInput = 0;
1118 static char NewNetId[16];
1119 static char NewNetIdLabel[] = "Enter New Callsign";
1120 extern char *fgd_callsign;
1122 static puOneShot *NetIdDialogOkButton = 0;
1123 static puOneShot *NetIdDialogCancelButton = 0;
1125 void NetIdDialog_Cancel(puObject *)
1127 FG_POP_PUI_DIALOG( NetIdDialog );
1130 void NetIdDialog_OK (puObject *)
1134 bool freeze = globals->get_freeze();
1136 globals->set_freeze( true );
1138 The following needs some cleanup because
1139 "string options.NetId" and "char *net_callsign"
1141 NetIdDialogInput->getValue(&net_callsign);
1142 NetId = net_callsign;
1144 NetIdDialog_Cancel( NULL );
1145 current_options.set_net_id( NetId.c_str() );
1146 strcpy( fgd_callsign, net_callsign);
1147 // strcpy( fgd_callsign, current_options.get_net_id().c_str());
1148 /* Entering a callsign indicates : user wants Net HUD Info */
1149 net_hud_display = 1;
1152 globals->set_freeze( false );
1155 void NewCallSign(puObject *cb)
1157 sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1158 // sprintf( NewNetId, "%s", fgd_callsign );
1159 NetIdDialogInput->setValue( NewNetId );
1161 FG_PUSH_PUI_DIALOG( NetIdDialog );
1164 static void NewNetIdInit(void)
1166 sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1167 // sprintf( NewNetId, "%s", fgd_callsign );
1168 int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
1169 NewNetIdLabel ) / 2;
1171 NetIdDialog = new puDialogBox (150, 50);
1173 NetIdDialogFrame = new puFrame (0,0,350, 150);
1174 NetIdDialogMessage = new puText (len, 110);
1175 NetIdDialogMessage -> setLabel (NewNetIdLabel);
1177 NetIdDialogInput = new puInput (50, 70, 300, 100);
1178 NetIdDialogInput -> setValue (NewNetId);
1179 NetIdDialogInput -> acceptInput();
1181 NetIdDialogOkButton = new puOneShot (50, 10, 110, 50);
1182 NetIdDialogOkButton -> setLegend (gui_msg_OK);
1183 NetIdDialogOkButton -> setCallback (NetIdDialog_OK);
1184 NetIdDialogOkButton -> makeReturnDefault(TRUE);
1186 NetIdDialogCancelButton = new puOneShot (240, 10, 300, 50);
1187 NetIdDialogCancelButton -> setLegend (gui_msg_CANCEL);
1188 NetIdDialogCancelButton -> setCallback (NetIdDialog_Cancel);
1191 FG_FINALIZE_PUI_DIALOG( NetIdDialog );
1194 static void net_display_toggle( puObject *cb)
1196 net_hud_display = (net_hud_display) ? 0 : 1;
1197 printf("Toggle net_hud_display : %d\n", net_hud_display);
1200 static void net_register( puObject *cb)
1202 fgd_send_com( "1", FGFS_host );
1203 net_is_registered = 0;
1204 printf("Registering to deamon\n");
1207 static void net_unregister( puObject *cb)
1209 fgd_send_com( "8", FGFS_host );
1210 net_is_registered = -1;
1211 printf("Unregistering from deamon\n");
1215 /*************** Deamon communication **********/
1217 // These statics should disapear when this is a class
1218 static puDialogBox *NetFGDDialog = 0;
1219 static puFrame *NetFGDDialogFrame = 0;
1220 static puText *NetFGDDialogMessage = 0;
1221 //static puInput *NetFGDDialogInput = 0;
1223 //static char NewNetId[16];
1224 static char NewNetFGDLabel[] = "Scan for deamon ";
1225 static char NewFGDHost[64] = "olk.mcp.de";
1226 static int NewFGDPortLo = 10000;
1227 static int NewFGDPortHi = 10001;
1229 //extern char *fgd_callsign;
1230 extern u_short base_port, end_port;
1231 extern int fgd_ip, verbose, current_port;
1232 extern char *fgd_host;
1235 static puOneShot *NetFGDDialogOkButton = 0;
1236 static puOneShot *NetFGDDialogCancelButton = 0;
1237 static puOneShot *NetFGDDialogScanButton = 0;
1239 static puInput *NetFGDHostDialogInput = 0;
1240 static puInput *NetFGDPortLoDialogInput = 0;
1241 static puInput *NetFGDPortHiDialogInput = 0;
1243 void NetFGDDialog_Cancel(puObject *)
1245 FG_POP_PUI_DIALOG( NetFGDDialog );
1248 void NetFGDDialog_OK (puObject *)
1252 bool freeze = globals->get_freeze();
1254 globals->set_freeze( true );
1255 NetFGDHostDialogInput->getValue( &NetFGD );
1256 strcpy( fgd_host, NetFGD);
1257 NetFGDPortLoDialogInput->getValue( (int *) &base_port );
1258 NetFGDPortHiDialogInput->getValue( (int *) &end_port );
1259 NetFGDDialog_Cancel( NULL );
1261 globals->set_freeze( false );
1264 void NetFGDDialog_SCAN (puObject *)
1269 bool freeze = globals->get_freeze();
1271 globals->set_freeze( true );
1272 // printf("Vor getvalue %s\n");
1273 NetFGDHostDialogInput->getValue( &NetFGD );
1274 // printf("Vor strcpy %s\n", (char *) NetFGD);
1275 strcpy( fgd_host, NetFGD);
1276 NetFGDPortLoDialogInput->getValue( (int *) &base_port );
1277 NetFGDPortHiDialogInput->getValue( (int *) &end_port );
1278 printf("FGD: %s Port-Start: %d Port-End: %d\n", fgd_host,
1279 base_port, end_port);
1280 net_resolv_fgd(fgd_host);
1281 printf("Resolve : %d\n", net_r);
1283 globals->set_freeze( false );
1286 strcpy( fgd_name, "");
1287 for( current_port = base_port; ( current_port <= end_port); current_port++) {
1288 fgd_send_com("0" , FGFS_host);
1289 sprintf( NewNetFGDLabel , "Scanning for deamon Port: %d", current_port);
1290 printf("FGD: searching %s\n", fgd_name);
1291 if ( strcmp( fgd_name, "") != 0 ) {
1292 sprintf( NewNetFGDLabel , "Found %s at Port: %d",
1293 fgd_name, current_port);
1294 fgd_port = current_port;
1295 current_port = end_port+1;
1298 current_port = end_port = base_port = fgd_port;
1300 NetFGDDialog_Cancel( NULL );
1304 void net_fgd_scan(puObject *cb)
1306 NewFGDPortLo = base_port;
1307 NewFGDPortHi = end_port;
1308 strcpy( NewFGDHost, fgd_host);
1309 NetFGDPortLoDialogInput->setValue( NewFGDPortLo );
1310 NetFGDPortHiDialogInput->setValue( NewFGDPortHi );
1311 NetFGDHostDialogInput->setValue( NewFGDHost );
1313 FG_PUSH_PUI_DIALOG( NetFGDDialog );
1317 static void NewNetFGDInit(void)
1319 // sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1320 // sprintf( NewNetId, "%s", fgd_callsign );
1321 int len = 170 - puGetStringWidth( puGetDefaultLabelFont(),
1322 NewNetFGDLabel ) / 2;
1324 NetFGDDialog = new puDialogBox (310, 30);
1326 NetFGDDialogFrame = new puFrame (0,0,320, 170);
1327 NetFGDDialogMessage = new puText (len, 140);
1328 NetFGDDialogMessage -> setLabel (NewNetFGDLabel);
1330 NetFGDPortLoDialogInput = new puInput (50, 70, 127, 100);
1331 NetFGDPortLoDialogInput -> setValue (NewFGDPortLo);
1332 NetFGDPortLoDialogInput -> acceptInput();
1334 NetFGDPortHiDialogInput = new puInput (199, 70, 275, 100);
1335 NetFGDPortHiDialogInput -> setValue (NewFGDPortHi);
1336 NetFGDPortHiDialogInput -> acceptInput();
1338 NetFGDHostDialogInput = new puInput (50, 100, 275, 130);
1339 NetFGDHostDialogInput -> setValue (NewFGDHost);
1340 NetFGDHostDialogInput -> acceptInput();
1342 NetFGDDialogScanButton = new puOneShot (130, 10, 200, 50);
1343 NetFGDDialogScanButton -> setLegend ("Scan");
1344 NetFGDDialogScanButton -> setCallback (NetFGDDialog_SCAN);
1345 NetFGDDialogScanButton -> makeReturnDefault(FALSE);
1347 NetFGDDialogOkButton = new puOneShot (50, 10, 120, 50);
1348 NetFGDDialogOkButton -> setLegend (gui_msg_OK);
1349 NetFGDDialogOkButton -> setCallback (NetFGDDialog_OK);
1350 NetFGDDialogOkButton -> makeReturnDefault(TRUE);
1352 NetFGDDialogCancelButton = new puOneShot (210, 10, 280, 50);
1353 NetFGDDialogCancelButton -> setLegend (gui_msg_CANCEL);
1354 NetFGDDialogCancelButton -> setCallback (NetFGDDialog_Cancel);
1357 FG_FINALIZE_PUI_DIALOG( NetFGDDialog );
1361 static void net_display_toggle( puObject *cb)
1363 net_hud_display = (net_hud_display) ? 0 : 1;
1364 printf("Toggle net_hud_display : %d\n", net_hud_display);
1371 /*************** End Networking **************/
1375 /* -----------------------------------------------------------------------
1377 ---------------------------------------------------------------------*/
1378 char *fileSubmenu [] = {
1379 "Exit", /* "Close", "---------", */
1380 #if defined( WIN32 ) && !defined( __CYGWIN__)
1390 puCallback fileSubmenuCb [] = {
1391 MayBeGoodBye, /* hideMenuCb, NULL, */
1392 #if defined( WIN32 ) && !defined( __CYGWIN__)
1405 char *editSubmenu [] = {
1408 puCallback editSubmenuCb [] = {
1413 extern void fgHUDalphaAdjust( puObject * );
1414 char *viewSubmenu [] = {
1416 /* "Cockpit View > ", "View >","------------", */
1417 "Toggle Panel...", NULL
1419 puCallback viewSubmenuCb [] = {
1421 /* notCb, notCb, NULL, */
1422 guiTogglePanel, NULL
1425 char *aircraftSubmenu [] = {
1426 "Autopilot", "Heading", "Altitude", "Navigation", "Airport",
1427 /* "Communication", */ NULL
1429 puCallback aircraftSubmenuCb [] = {
1430 fgAPAdjust, NewHeading, NewAltitude, fgLatLonFormatToggle, NewTgtAirport,
1434 char *environmentSubmenu [] = {
1436 "Goto Airport", /* "Terrain", "Weather", */ NULL
1438 puCallback environmentSubmenuCb [] = {
1440 NewAirport, /* notCb, notCb, */ NULL
1444 char *optionsSubmenu [] = {
1445 "Preferences", "Realism & Reliablity...", NULL
1447 puCallback optionsSubmenuCb [] = {
1452 #ifdef FG_NETWORK_OLK
1453 char *networkSubmenu [] = {
1454 "Unregister from FGD ", /* "Send MSG to All", "Send MSG", "Show Pilots", */
1456 "Scan for Deamons", "Enter Callsign", /* "Display Netinfos", */
1457 "Toggle Display", NULL
1459 puCallback networkSubmenuCb [] = {
1460 /* notCb, notCb, notCb, notCb, */
1463 net_fgd_scan, NewCallSign,
1464 net_display_toggle, NULL
1468 char *helpSubmenu [] = {
1469 /* "About...", */ "Help", NULL
1471 puCallback helpSubmenuCb [] = {
1472 /* notCb, */ helpCb, NULL
1476 /* -------------------------------------------------------------------------
1478 _____________________________________________________________________*/
1483 char *mesa_win_state;
1487 puSetDefaultStyle ( PUSTYLE_SMALL_BEVELLED ); //PUSTYLE_DEFAULT
1488 puSetDefaultColourScheme (0.8, 0.8, 0.8, 0.4);
1490 // Initialize our GLOBAL GUI STRINGS
1491 gui_msg_OK = msg_OK; // "OK"
1492 gui_msg_NO = msg_NO; // "NO"
1493 gui_msg_YES = msg_YES; // "YES"
1494 gui_msg_CANCEL = msg_CANCEL; // "CANCEL"
1495 gui_msg_RESET = msg_RESET; // "RESET"
1497 // Next check home directory
1499 char* envp = ::getenv( "FG_FONTS" );
1500 if ( envp != NULL ) {
1501 fntpath.set( envp );
1503 fntpath.set( current_options.get_fg_root() );
1504 fntpath.append( "Fonts" );
1507 // Install our fast fonts
1508 fntpath.append( "typewriter.txf" );
1509 guiFntHandle = new fntTexFont ;
1510 guiFntHandle -> load ( (char *)fntpath.c_str() ) ;
1511 puFont GuiFont ( guiFntHandle, 15 ) ;
1512 puSetDefaultFonts( GuiFont, GuiFont ) ;
1513 guiFnt = puGetDefaultLabelFont();
1515 if ( current_options.get_mouse_pointer() == 0 ) {
1516 // no preference specified for mouse pointer, attempt to autodetect...
1517 // Determine if we need to render the cursor, or if the windowing
1518 // system will do it. First test if we are rendering with glide.
1519 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1520 // Test for the MESA_GLX_FX env variable
1521 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
1522 // test if we are fullscreen mesa/glide
1523 if ( (mesa_win_state[0] == 'f') ||
1524 (mesa_win_state[0] == 'F') ) {
1529 mouse_active = ~mouse_active;
1530 } else if ( current_options.get_mouse_pointer() == 1 ) {
1531 // don't show pointer
1532 } else if ( current_options.get_mouse_pointer() == 2 ) {
1533 // force showing pointer
1535 mouse_active = ~mouse_active;
1538 // MOUSE_VIEW mode stuff
1539 trackball(_quat0, 0.0, 0.0, 0.0, 0.0);
1541 build_rotmatrix(quat_mat, curquat);
1543 // Set up our Dialog Boxes
1544 ConfirmExitDialogInit();
1546 #ifdef FG_NETWORK_OLK
1552 // Make the menu bar
1553 mainMenuBar = new puMenuBar ();
1554 mainMenuBar -> add_submenu ("File", fileSubmenu, fileSubmenuCb);
1555 // mainMenuBar -> add_submenu ("Edit", editSubmenu, editSubmenuCb);
1556 mainMenuBar -> add_submenu ("View", viewSubmenu, viewSubmenuCb);
1557 mainMenuBar -> add_submenu ("Aircraft", aircraftSubmenu, aircraftSubmenuCb);
1558 mainMenuBar -> add_submenu ("Environment", environmentSubmenu, environmentSubmenuCb);
1559 // mainMenuBar -> add_submenu ("Options", optionsSubmenu, optionsSubmenuCb);
1560 #ifdef FG_NETWORK_OLK
1561 mainMenuBar -> add_submenu ("Network", networkSubmenu, networkSubmenuCb);
1563 mainMenuBar -> add_submenu ("Help", helpSubmenu, helpSubmenuCb);
1564 mainMenuBar-> close ();
1565 // Set up menu bar toggle
1574 * Implementation of a virtual trackball.
1575 * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
1576 * the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
1578 * Vector manip code:
1580 * Original code from:
1581 * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
1583 * Much mucking with by:
1586 #if defined(_WIN32) && !defined( __CYGWIN32__ )
1587 #pragma warning (disable:4244) /* disable bogus conversion warnings */
1591 //#include "trackball.h"
1594 * This size should really be based on the distance from the center of
1595 * rotation to the point on the object underneath the mouse. That
1596 * point would then track the mouse as closely as possible. This is a
1597 * simple example, though, so that is left as an Exercise for the
1600 #define TRACKBALLSIZE (0.8f)
1601 #define SQRT(x) sqrt(x)
1604 * Local function prototypes (not defined in trackball.h)
1606 static float tb_project_to_sphere(float, float, float);
1607 static void normalize_quat(float [4]);
1618 vset(float *v, float x, float y, float z)
1626 vsub(const float *src1, const float *src2, float *dst)
1628 dst[0] = src1[0] - src2[0];
1629 dst[1] = src1[1] - src2[1];
1630 dst[2] = src1[2] - src2[2];
1634 vcopy(const float *v1, float *v2)
1637 for (i = 0 ; i < 3 ; i++)
1642 vcross(const float *v1, const float *v2, float *cross)
1646 temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
1647 temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
1648 temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
1653 vlength(const float *v)
1655 float tmp = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
1660 vscale(float *v, float div)
1670 vscale(v,1.0/vlength(v));
1674 vdot(const float *v1, const float *v2)
1676 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
1680 vadd(const float *src1, const float *src2, float *dst)
1682 dst[0] = src1[0] + src2[0];
1683 dst[1] = src1[1] + src2[1];
1684 dst[2] = src1[2] + src2[2];
1688 * Given an axis and angle, compute quaternion.
1691 axis_to_quat(float a[3], float phi, float q[4])
1693 double sinphi2, cosphi2;
1694 double phi2 = phi/2.0;
1695 sinphi2 = sin(phi2);
1696 cosphi2 = cos(phi2);
1704 * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
1705 * if we are away from the center of the sphere.
1708 tb_project_to_sphere(float r, float x, float y)
1714 if (d < r * 0.70710678118654752440) { /* Inside sphere */
1717 } else { /* On hyperbola */
1718 t = r / 1.41421356237309504880;
1725 * Quaternions always obey: a^2 + b^2 + c^2 + d^2 = 1.0
1726 * If they don't add up to 1.0, dividing by their magnitued will
1729 * Note: See the following for more information on quaternions:
1731 * - Shoemake, K., Animating rotation with quaternion curves, Computer
1732 * Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
1733 * - Pletinckx, D., Quaternion calculus as a basic tool in computer
1734 * graphics, The Visual Computer 5, 2-13, 1989.
1737 normalize_quat(float q[4])
1742 tmp = q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3];
1743 mag = 1.0 / SQRT(tmp);
1744 for (i = 0; i < 4; i++)
1749 * Ok, simulate a track-ball. Project the points onto the virtual
1750 * trackball, then figure out the axis of rotation, which is the cross
1751 * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
1752 * Note: This is a deformed trackball-- is a trackball in the center,
1753 * but is deformed into a hyperbolic sheet of rotation away from the
1754 * center. This particular function was chosen after trying out
1755 * several variations.
1757 * It is assumed that the arguments to this routine are in the range
1761 trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
1763 float a[3]; /* Axis of rotation */
1764 float phi; /* how much to rotate about axis */
1765 float p1[3], p2[3], d[3];
1768 if (p1x == p2x && p1y == p2y) {
1776 * First, figure out z-coordinates for projection of P1 and P2 to
1779 vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
1780 vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
1783 * Now, we want the cross product of P1 and P2
1788 * Figure out how much to rotate around that axis.
1791 t = vlength(d) / (2.0*TRACKBALLSIZE);
1794 * Avoid problems with out-of-control values...
1796 if (t > 1.0) t = 1.0;
1797 if (t < -1.0) t = -1.0;
1798 phi = 2.0 * asin(t);
1800 axis_to_quat(a,phi,q);
1804 * Given two rotations, e1 and e2, expressed as quaternion rotations,
1805 * figure out the equivalent single rotation and stuff it into dest.
1807 * This routine also normalizes the result every RENORMCOUNT times it is
1808 * called, to keep error from creeping in.
1810 * NOTE: This routine is written so that q1 or q2 may be the same
1811 * as dest (or each other).
1814 #define RENORMCOUNT 97
1817 add_quats(float q1[4], float q2[4], float dest[4])
1820 float t1[4], t2[4], t3[4];
1824 printf("q1 = %f %f %f %f\n", q1[0], q1[1], q1[2], q1[3]);
1825 printf("q2 = %f %f %f %f\n", q2[0], q2[1], q2[2], q2[3]);
1837 tf[3] = q1[3] * q2[3] - vdot(q1,q2);
1840 printf("tf = %f %f %f %f\n", tf[0], tf[1], tf[2], tf[3]);
1848 if (++count > RENORMCOUNT) {
1850 normalize_quat(dest);
1855 * Build a rotation matrix, given a quaternion rotation.
1859 build_rotmatrix(float m[4][4], float q[4])
1861 //#define TRANSPOSED_QUAT
1862 #ifndef TRANSPOSED_QUAT
1863 m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
1864 m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
1865 m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
1868 m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
1869 m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
1870 m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
1873 m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
1874 m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
1875 m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
1882 #else // TRANSPOSED_QUAT
1883 m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
1884 m[0][1] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
1885 m[0][2] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
1888 m[1][0] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
1889 m[1][1] = 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
1890 m[1][2] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
1893 m[2][0] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
1894 m[2][1] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
1895 m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);