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 <Cockpit/panel.hxx>
62 #include <Controls/controls.hxx>
63 #include <FDM/flight.hxx>
64 #include <Main/options.hxx>
65 #include <Main/fg_init.hxx>
66 #include <Main/views.hxx>
68 #include <NetworkOLK/network.h>
70 #include <Time/fg_time.hxx>
72 #if defined( WIN32 ) && !defined( __CYGWIN__ )
73 # include <simgear/screen/win32-printer.h>
74 # include <simgear/screen/GlBitmaps.h>
79 * A virtual trackball implementation
80 * Written by Gavin Bell for Silicon Graphics, November 1988.
83 * Pass the x and y coordinates of the last and current positions of
84 * the mouse, scaled so they are from (-1.0 ... 1.0).
86 * The resulting rotation is returned as a quaternion rotation in the
90 trackball(float q[4], float p1x, float p1y, float p2x, float p2y);
93 * Given two quaternions, add them together to get a third quaternion.
94 * Adding quaternions to get a compound rotation is analagous to adding
95 * translations to get a compound translation. When incrementally
96 * adding rotations, the first argument here should be the new
98 * rotation, the second and third the total rotation (which will be
99 * over-written with the resulting new total rotation).
102 add_quats(float *q1, float *q2, float *dest);
105 * A useful function, builds a rotation matrix in Matrix based on
109 build_rotmatrix(float m[4][4], float q[4]);
112 * This function computes a quaternion based on an axis (defined by
113 * the given vector) and an angle about which to rotate. The angle is
114 * expressed in radians. The result is put into the third argument.
117 axis_to_quat(float a[3], float phi, float q[4]);
122 FG_USING_STD(string);
124 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
128 #if defined(WIN32) || defined(__CYGWIN32__)
129 #define WIN32_CURSOR_TWEAKS
130 #elif (GLUT_API_VERSION >= 4 || GLUT_XLIB_IMPLEMENTATION >= 9)
131 #define X_CURSOR_TWEAKS
134 // hack, should come from an include someplace
135 extern void fgInitVisuals( void );
136 extern void fgReshape( int width, int height );
137 extern void fgRenderFrame( void );
140 fntTexFont *guiFntHandle = 0;
142 static puMenuBar *mainMenuBar = 0;
143 //static puButton *hideMenuButton = 0;
145 static puDialogBox *dialogBox = 0;
146 static puFrame *dialogFrame = 0;
147 static puText *dialogBoxMessage = 0;
148 static puOneShot *dialogBoxOkButton = 0;
151 static puDialogBox *YNdialogBox = 0;
152 static puFrame *YNdialogFrame = 0;
153 static puText *YNdialogBoxMessage = 0;
154 static puOneShot *YNdialogBoxOkButton = 0;
155 static puOneShot *YNdialogBoxNoButton = 0;
157 static char msg_OK[] = "OK";
158 static char msg_NO[] = "NO";
159 static char msg_YES[] = "YES";
160 static char msg_CANCEL[] = "Cancel";
161 static char msg_RESET[] = "Reset";
163 char *gui_msg_OK; // "OK"
164 char *gui_msg_NO; // "NO"
165 char *gui_msg_YES; // "YES"
166 char *gui_msg_CANCEL; // "CANCEL"
167 char *gui_msg_RESET; // "RESET"
169 static char global_dialog_string[256];
171 // from autopilot.cxx
172 extern void NewAltitude( puObject *cb );
173 extern void NewHeading( puObject *cb );
174 extern void fgAPAdjust( puObject * );
175 extern void NewTgtAirport( puObject *cb );
176 bool fgAPTerrainFollowEnabled( void );
177 bool fgAPAltitudeEnabled( void );
178 bool fgAPHeadingEnabled( void );
179 bool fgAPWayPointEnabled( void );
180 bool fgAPAutoThrottleEnabled( void );
183 extern void fgLatLonFormatToggle( puObject *);
185 /* --------------------------------------------------------------------
187 ---------------------------------------------------------------------*/
191 static int _savedX = 0;
192 static int _savedY = 0;
193 static int last_buttons = 0 ;
194 static int mouse_active = 0;
195 static int menu_on = 0;
196 static int mouse_joystick_control = 0;
198 //static time_t mouse_off_time;
199 //static int mouse_timed_out;
201 // to allow returning to previous view
202 // on second left click in MOUSE_VIEW mode
203 // This has file scope so that it can be reset
204 // if the little rodent is moved NHV
205 static int _mVtoggle;
207 // we break up the glutGetModifiers return mask
208 // once per loop and stash what we need in these
209 static int glut_active_shift;
210 static int glut_active_ctrl;
211 static int glut_active_alt;
213 static float lastquat[4];
214 static float curquat[4];
215 static float _quat0[4];
216 float quat_mat[4][4];
218 // uncomment this for view to exactly follow mouse in MOUSE_VIEW mode
219 // else smooth out the view panning to .01 radian per frame
220 // see view_offset smoothing mechanism in main.cxx
221 #define NO_SMOOTH_MOUSE_VIEW
223 // uncomment following to
224 #define RESET_VIEW_ON_LEAVING_MOUSE_VIEW
226 /* --------------------------------------------------------------------
227 Support for mouse as control yoke (david@megginson.com)
229 - right button toggles between pointer and yoke
230 - horizontal drag with no buttons moves ailerons
231 - vertical drag with no buttons moves elevators
232 - horizontal drag with left button moves brakes (left=on)
233 - vertical drag with left button moves throttle (up=more)
234 - horizontal drag with middle button moves rudder
235 - vertical drag with middle button moves trim
237 For the *_sensitivity variables, a lower number means more sensitive.
239 TODO: figure out how to keep pointer from leaving window in yoke mode.
240 TODO: add thresholds and null zones
241 TODO: sensitivity should be configurable at user option.
242 TODO: allow differential braking (this will be useful if FlightGear
243 ever supports tail-draggers like the DC-3)
244 ---------------------------------------------------------------------*/
252 MouseMode mouse_mode = MOUSE_POINTER;
254 static double aileron_sensitivity = 1.0/500.0;
255 static double elevator_sensitivity = 1.0/500.0;
256 static double brake_sensitivity = 1.0/250.0;
257 static double throttle_sensitivity = 1.0/250.0;
258 static double rudder_sensitivity = 1.0/500.0;
259 static double trim_sensitivity = 1.0/1000.0;
261 static inline void Quat0( void ) {
262 curquat[0] = _quat0[0];
263 curquat[1] = _quat0[1];
264 curquat[2] = _quat0[2];
265 curquat[3] = _quat0[3];
268 static inline int left_button( void ) {
269 return( last_buttons & (1 << GLUT_LEFT_BUTTON) );
272 static inline int middle_button( void ) {
273 return( last_buttons & (1 << GLUT_MIDDLE_BUTTON) );
276 static inline int right_button( void ) {
277 return( last_buttons & (1 << GLUT_RIGHT_BUTTON) );
280 static inline void TurnCursorOn( void )
283 #if defined(WIN32_CURSOR_TWEAKS)
284 switch (mouse_mode) {
286 glutSetCursor(GLUT_CURSOR_INHERIT);
289 glutSetCursor(GLUT_CURSOR_CROSSHAIR);
292 glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
296 #if defined(X_CURSOR_TWEAKS)
297 glutWarpPointer( current_view.get_winWidth()/2, current_view.get_winHeight()/2);
301 static inline void TurnCursorOff( void )
304 #if defined(WIN32_CURSOR_TWEAKS)
305 glutSetCursor(GLUT_CURSOR_NONE);
306 #elif defined(X_CURSOR_TWEAKS)
307 glutWarpPointer( current_view.get_winWidth(), current_view.get_winHeight());
311 void maybeToggleMouse( void )
313 #if defined(WIN32_CURSOR_TWEAKS)
314 static int first_time = ~0;
315 static int mouse_changed = 0;
319 mouse_changed = ~mouse_changed;
323 if( mouse_mode != MOUSE_POINTER )
325 if( mouse_changed ) {
326 mouse_changed = ~mouse_changed;
332 first_time = ~first_time;
333 #endif // #ifdef WIN32
336 // Call with FALSE to init and TRUE to restore
337 void BusyCursor( int restore )
339 static GLenum cursor = (GLenum) 0;
341 glutSetCursor(cursor);
343 cursor = (GLenum) glutGet( (GLenum) GLUT_WINDOW_CURSOR );
344 #if defined(WIN32_CURSOR_TWEAKS)
347 glutSetCursor( GLUT_CURSOR_WAIT );
351 int guiGetMouseButton(void)
356 void guiGetMouse(int *x, int *y)
362 void guiMotionFunc ( int x, int y )
364 int ww, wh, need_warp = 0;
367 // FGTime *t = FGTime::cur_time_params;
368 // if( mouse_timed_out ) {
369 // if( t->get_cur_time() > mouse_off_time ) {
370 // moused_timed_out = 0;
372 // glutPostRedisplay () ;
376 if (mouse_mode == MOUSE_POINTER) {
378 glutPostRedisplay () ;
380 if( x == _mX && y == _mY)
383 // reset left click MOUSE_VIEW toggle feature
386 ww = current_view.get_winWidth();
387 wh = current_view.get_winHeight();
389 switch (mouse_mode) {
391 if( !mouse_joystick_control ) {
392 mouse_joystick_control = 1;
393 current_options.set_control_mode( fgOPTIONS::FG_MOUSE );
395 if ( left_button() ) {
396 offset = (_mX - x) * brake_sensitivity;
397 controls.move_brake(FGControls::ALL_WHEELS, offset);
398 offset = (_mY - y) * throttle_sensitivity;
399 controls.move_throttle(FGControls::ALL_ENGINES, offset);
400 } else if ( right_button() ) {
401 if( !fgAPHeadingEnabled() ) {
402 offset = (x - _mX) * rudder_sensitivity;
403 controls.move_rudder(offset);
405 if( !fgAPAltitudeEnabled() ) {
406 offset = (_mY - y) * trim_sensitivity;
407 controls.move_elevator_trim(offset);
410 if( !fgAPHeadingEnabled() ) {
411 offset = (x - _mX) * aileron_sensitivity;
412 controls.move_aileron(offset);
414 if( !fgAPAltitudeEnabled() ) {
415 offset = (_mY - y) * elevator_sensitivity;
416 controls.move_elevator(offset);
420 // Keep the mouse in the window.
421 if (x < 5 || x > ww-5 || y < 5 || y > wh-5) {
430 #define CONTRAINED_MOUSE_VIEW_Y
431 #ifdef CONTRAINED_MOUSE_VIEW_Y
435 #endif // CONTRAINED_MOUSE_VIEW_Y
437 } else if( y >= wh-1) {
438 #ifdef CONTRAINED_MOUSE_VIEW_Y
442 #endif // CONTRAINED_MOUSE_VIEW_Y
445 // wrap MOUSE_VIEW mode cursor x position
449 } else if ( x >= ww-1 ) {
453 // try to get FG_PI movement in each half of screen
457 if( middle_button() ) {
459 (2.0f * _mX - W) / W,
460 0, //(H - 2.0f * y) / H, // 3
462 0 //(H - 2.0f * _mY) / H // 1
469 0, //(2.0f * _mX - W) / W, // 0
470 (H - 2.0f * y) / H, // 3
471 0, //(2.0f * x - W) / W, // 2
472 (H - 2.0f * _mY) / H // 1
475 add_quats(lastquat, curquat, curquat);
476 build_rotmatrix(quat_mat, curquat);
479 // this could be done in above quat
480 // but requires redoing view pipeline
481 offset = current_view.get_goal_view_offset();
482 offset += ((_mX - x) * FG_2PI / W );
483 while (offset < 0.0) {
486 while (offset > FG_2PI) {
489 current_view.set_goal_view_offset(offset);
490 #ifdef NO_SMOOTH_MOUSE_VIEW
491 current_view.set_view_offset(offset);
500 glutWarpPointer(x, y);
502 // Record the new mouse position.
508 void guiMouseFunc(int button, int updown, int x, int y)
512 // private MOUSE_VIEW state variables
513 // to allow alternate left clicks in MOUSE_VIEW mode
514 // to toggle between current offsets and straight ahead
516 static int _mVx, _mVy, _Vx, _Vy;
517 static float _quat[4];
518 static double _view_offset;
520 // general purpose variables
523 glutModifiers = glutGetModifiers();
524 glut_active_shift = glutModifiers & GLUT_ACTIVE_SHIFT;
525 glut_active_ctrl = glutModifiers & GLUT_ACTIVE_CTRL;
526 glut_active_alt = glutModifiers & GLUT_ACTIVE_ALT;
528 // Was the left button pressed?
529 if (updown == GLUT_DOWN ) {
530 if( button == GLUT_LEFT_BUTTON)
532 switch (mouse_mode) {
539 // resume previous view offsets
544 curquat[0] = _quat[0];
545 curquat[1] = _quat[1];
546 curquat[2] = _quat[2];
547 curquat[3] = _quat[3];
548 current_view.set_goal_view_offset(_view_offset);
549 #ifdef NO_SMOOTH_MOUSE_VIEW
550 current_view.set_view_offset(_view_offset);
558 _quat[0] = curquat[0];
559 _quat[1] = curquat[1];
560 _quat[2] = curquat[2];
561 _quat[3] = curquat[3];
562 x = current_view.get_winWidth()/2;
563 y = current_view.get_winHeight()/2;
565 _view_offset = current_view.get_goal_view_offset();
566 current_view.set_goal_view_offset(0.0);
567 #ifdef NO_SMOOTH_MOUSE_VIEW
568 current_view.set_view_offset(0.0);
571 glutWarpPointer( x , y);
572 build_rotmatrix(quat_mat, curquat);
573 _mVtoggle = ~_mVtoggle;
576 }else if ( button == GLUT_RIGHT_BUTTON) {
577 switch (mouse_mode) {
579 mouse_mode = MOUSE_YOKE;
580 mouse_joystick_control = 0;
583 // start with zero point in center of screen
584 _mX = current_view.get_winWidth()/2;
585 _mY = current_view.get_winHeight()/2;
587 // try to have the MOUSE_YOKE position
588 // reflect the current stick position
589 offset = controls.get_aileron();
590 x = _mX - (int)(offset * aileron_sensitivity);
591 offset = controls.get_elevator();
592 y = _mY - (int)(offset * elevator_sensitivity);
594 glutSetCursor(GLUT_CURSOR_CROSSHAIR);
595 FG_LOG( FG_INPUT, FG_INFO, "Mouse in yoke mode" );
599 mouse_mode = MOUSE_VIEW;
600 current_options.set_control_mode( fgOPTIONS::FG_JOYSTICK );
601 x = current_view.get_winWidth()/2;
602 y = current_view.get_winHeight()/2;
605 build_rotmatrix(quat_mat, curquat);
606 glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
607 FG_LOG( FG_INPUT, FG_INFO, "Mouse in view mode" );
611 mouse_mode = MOUSE_POINTER;
614 #ifdef RESET_VIEW_ON_LEAVING_MOUSE_VIEW
616 build_rotmatrix(quat_mat, curquat);
617 current_view.set_goal_view_offset(0.0);
618 #ifdef NO_SMOOTH_MOUSE_VIEW
619 current_view.set_view_offset(0.0);
621 #endif // RESET_VIEW_ON_LEAVING_MOUSE_VIEW
622 glutSetCursor(GLUT_CURSOR_INHERIT);
627 FG_LOG( FG_INPUT, FG_INFO, "Mouse in pointer mode" );
630 glutWarpPointer( x, y );
631 } // END RIGHT BUTTON
632 } // END UPDOWN == GLUT_DOWN
634 // Note which button is pressed.
635 if ( updown == GLUT_DOWN ) {
636 last_buttons |= ( 1 << button ) ;
638 last_buttons &= ~( 1 << button ) ;
641 // If we're in pointer mode, let PUI
642 // know what's going on.
643 if (mouse_mode == MOUSE_POINTER) {
644 puMouse (button, updown, x,y);
647 // Register the new position (if it
648 // hasn't been registered already).
652 glutPostRedisplay ();
655 /* ================ General Purpose Functions ================ */
657 // Intercept the Escape Key
658 void ConfirmExitDialog(void)
660 FG_PUSH_PUI_DIALOG( YNdialogBox );
663 // General Purpose Message Box
664 void mkDialog (const char *txt)
666 strncpy(global_dialog_string, txt, 256);
667 dialogBoxMessage->setLabel(global_dialog_string);
668 FG_PUSH_PUI_DIALOG( dialogBox );
671 // Repair any damage done to the Panel by other Gui Items
672 void guiFixPanel( void )
676 if ( current_options.get_panel_status() ) {
677 FGView *v = ¤t_view;
678 FGTime *t = FGTime::cur_time_params;
680 if( (toggle_pause = !t->getPause()) )
681 t->togglePauseMode();
683 // this seems to be the only way to do this :-(
684 // problem is the viewport has been mucked with
685 xglViewport(0, 0 , (GLint)(v->winWidth), (GLint)(v->winHeight) );
686 FGPanel::OurPanel->ReInit(0, 0, 1024, 768);
689 t->togglePauseMode();
693 // Toggle the Menu and Mouse display state
694 void guiToggleMenu(void)
697 // printf("Hiding Menu\n");
698 mainMenuBar->hide ();
699 #if defined(WIN32_CURSOR_TWEAKS)
700 if( mouse_mode == MOUSE_POINTER )
702 #endif // #ifdef WIN32_CURSOR_TWEAKS
704 // printf("Showing Menu\n");
705 mainMenuBar->reveal();
708 #endif // #ifdef WIN32
713 /* -----------------------------------------------------------------------
714 the Gui callback functions
715 ____________________________________________________________________*/
717 void reInit(puObject *cb)
721 build_rotmatrix(quat_mat, curquat);
722 fgReInitSubsystems();
726 // This is the accessor function
727 void guiTogglePanel(puObject *cb)
729 current_options.toggle_panel();
732 //void MenuHideMenuCb(puObject *cb)
733 void hideMenuCb (puObject *cb)
738 void goodBye(puObject *)
740 // FG_LOG( FG_INPUT, FG_ALERT,
741 // "Program exiting normally at user request." );
742 cout << "Program exiting normally at user request." << endl;
744 #ifdef FG_NETWORK_OLK
745 if ( current_options.get_network_olk() ) {
746 if ( net_is_registered == 0 ) fgd_send_com( "8", FGFS_host);
757 void goAwayCb (puObject *me)
759 FG_POP_PUI_DIALOG( dialogBox );
762 void mkDialogInit (void)
764 // printf("mkDialogInit\n");
765 int x = (current_options.get_xsize()/2 - 400/2);
766 int y = (current_options.get_ysize()/2 - 100/2);
767 dialogBox = new puDialogBox (x, y); // 150, 50
769 dialogFrame = new puFrame (0,0,400,100);
770 dialogBoxMessage = new puText (10, 70);
771 dialogBoxMessage -> setLabel ("");
772 dialogBoxOkButton = new puOneShot (180, 10, 240, 50);
773 dialogBoxOkButton -> setLegend (gui_msg_OK);
774 dialogBoxOkButton -> makeReturnDefault (TRUE );
775 dialogBoxOkButton -> setCallback (goAwayCb);
777 FG_FINALIZE_PUI_DIALOG( dialogBox );
780 void MayBeGoodBye(puObject *)
785 void goAwayYesNoCb(puObject *me)
787 FG_POP_PUI_DIALOG( YNdialogBox);
790 void ConfirmExitDialogInit(void)
792 char msg[] = "Really Quit";
795 // printf("ConfirmExitDialogInit\n");
796 int len = 200 - puGetStringWidth( puGetDefaultLabelFont(), msg )/2;
798 int x = (current_options.get_xsize()/2 - 400/2);
799 int y = (current_options.get_ysize()/2 - 100/2);
801 YNdialogBox = new puDialogBox (x, y); // 150, 50
802 // YNdialogBox = new puDialogBox (150, 50);
804 YNdialogFrame = new puFrame (0,0,400, 100);
806 YNdialogBoxMessage = new puText (len, 70);
807 YNdialogBoxMessage -> setDefaultValue (msg);
808 YNdialogBoxMessage -> getDefaultValue (&s);
809 YNdialogBoxMessage -> setLabel (s);
811 YNdialogBoxOkButton = new puOneShot (100, 10, 160, 50);
812 YNdialogBoxOkButton -> setLegend (gui_msg_OK);
813 YNdialogBoxOkButton -> setCallback (goodBye);
815 YNdialogBoxNoButton = new puOneShot (240, 10, 300, 50);
816 YNdialogBoxNoButton -> setLegend (gui_msg_NO);
817 // YNdialogBoxNoButton -> makeReturnDefault (TRUE );
818 YNdialogBoxNoButton -> setCallback (goAwayYesNoCb);
820 FG_FINALIZE_PUI_DIALOG( YNdialogBox );
823 void notCb (puObject *)
825 mkDialog ("This function isn't implemented yet");
828 void helpCb (puObject *)
832 #if defined(FX) && !defined(WIN32)
833 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
834 if ( global_fullscreen ) {
835 global_fullscreen = false;
836 XMesaSetFXmode( XMESA_FX_WINDOW );
842 string url = "http://www.flightgear.org/Docs/InstallGuide/getstart.html";
844 if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
845 command = "netscape -remote \"openURL(" + url + ")\" &";
847 command = "netscape " + url + " &";
850 command = "webrun.bat";
853 system( command.c_str() );
854 //string text = "Help started in netscape window.";
856 //mkDialog (text.c_str());
857 mkDialog ("Help started in netscape window.");
860 #if defined( WIN32 ) && !defined( __CYGWIN__)
862 static void rotateView( double roll, double pitch, double yaw )
867 static GlBitmap *b1 = NULL;
868 extern FGInterface cur_view_fdm;
869 GLubyte *hiResScreenCapture( int multiplier )
871 float oldfov = current_options.get_fov();
872 float fov = oldfov / multiplier;
873 FGView *v = ¤t_view;
874 current_options.set_fov(fov);
875 v->force_update_fov_math();
877 int cur_width = current_view.get_winWidth( );
878 int cur_height = current_view.get_winHeight( );
879 if (b1) delete( b1 );
880 // New empty (mostly) bitmap
881 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
883 for ( y = 0; y < multiplier; y++ )
885 for ( x = 0; x < multiplier; x++ )
887 fgReshape( cur_width, cur_height );
889 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
893 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
896 current_view.UpdateViewParams(cur_view_fdm);
897 current_options.set_fov(oldfov);
898 v->force_update_fov_math();
899 return b1->getBitmap();
904 #if defined( WIN32 ) && !defined( __CYGWIN__)
905 // win32 print screen function
906 void printScreen ( puObject *obj ) {
907 bool show_pu_cursor = false;
909 if ( !puCursorIsHidden() ) {
910 show_pu_cursor = true;
916 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
917 int cur_width = current_view.get_winWidth( );
918 int cur_height = current_view.get_winHeight( );
919 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
920 p.End( hiResScreenCapture(3) );
923 mainMenuBar->reveal();
926 if ( show_pu_cursor ) {
931 #endif // #ifdef WIN32
934 void dumpSnapShot ( puObject *obj ) {
939 // do a screen snap shot
940 void fgDumpSnapShot () {
941 bool show_pu_cursor = false;
945 if ( !puCursorIsHidden() ) {
946 show_pu_cursor = true;
951 fgReshape( current_options.get_xsize(), current_options.get_ysize() );
953 // we need two render frames here to clear the menu and cursor
954 // ... not sure why but doing an extra fgFenderFrame() shoulnd't
959 my_glDumpWindow( "fgfs-screen.ppm",
960 current_options.get_xsize(),
961 current_options.get_ysize() );
963 mkDialog ("Snap shot saved to fgfs-screen.ppm");
965 if ( show_pu_cursor ) {
971 mainMenuBar->reveal();
977 /// The beginnings of teleportation :-)
978 // Needs cleaning up but works
979 // These statics should disapear when this is a class
980 static puDialogBox *AptDialog = 0;
981 static puFrame *AptDialogFrame = 0;
982 static puText *AptDialogMessage = 0;
983 static puInput *AptDialogInput = 0;
985 static char NewAirportId[16];
986 static char NewAirportLabel[] = "Enter New Airport ID";
988 static puOneShot *AptDialogOkButton = 0;
989 static puOneShot *AptDialogCancelButton = 0;
990 static puOneShot *AptDialogResetButton = 0;
992 void AptDialog_Cancel(puObject *)
994 FG_POP_PUI_DIALOG( AptDialog );
997 void AptDialog_OK (puObject *)
999 FGPath path( current_options.get_fg_root() );
1000 path.append( "Airports" );
1001 path.append( "simple.gdbm" );
1002 FGAirports airports( path.c_str() );
1005 FGTime *t = FGTime::cur_time_params;
1006 int PauseMode = t->getPause();
1008 t->togglePauseMode();
1011 AptDialogInput->getValue(&s);
1014 cout << "AptDialog_OK " << AptId << " " << AptId.length() << endl;
1016 AptDialog_Cancel( NULL );
1018 if ( AptId.length() ) {
1019 // set initial position from airport id
1020 FG_LOG( FG_GENERAL, FG_INFO,
1021 "Attempting to set starting position from airport code "
1024 if ( airports.search( AptId, &a ) )
1026 current_options.set_airport_id( AptId.c_str() );
1028 fgReInitSubsystems();
1031 AptId += " not in database.";
1032 mkDialog(AptId.c_str());
1035 if( PauseMode != t->getPause() )
1036 t->togglePauseMode();
1039 void AptDialog_Reset(puObject *)
1041 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
1042 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1043 AptDialogInput->setValue ( NewAirportId );
1044 AptDialogInput->setCursor( 0 ) ;
1047 void NewAirport(puObject *cb)
1049 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
1050 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1051 // cout << "NewAirport " << NewAirportId << endl;
1052 AptDialogInput->setValue( NewAirportId );
1054 FG_PUSH_PUI_DIALOG( AptDialog );
1057 static void NewAirportInit(void)
1059 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1060 int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
1061 NewAirportLabel ) / 2;
1063 AptDialog = new puDialogBox (150, 50);
1065 AptDialogFrame = new puFrame (0,0,350, 150);
1066 AptDialogMessage = new puText (len, 110);
1067 AptDialogMessage -> setLabel (NewAirportLabel);
1069 AptDialogInput = new puInput (50, 70, 300, 100);
1070 AptDialogInput -> setValue (NewAirportId);
1071 AptDialogInput -> acceptInput();
1073 AptDialogOkButton = new puOneShot (50, 10, 110, 50);
1074 AptDialogOkButton -> setLegend (gui_msg_OK);
1075 AptDialogOkButton -> setCallback (AptDialog_OK);
1076 AptDialogOkButton -> makeReturnDefault(TRUE);
1078 AptDialogCancelButton = new puOneShot (140, 10, 210, 50);
1079 AptDialogCancelButton -> setLegend (gui_msg_CANCEL);
1080 AptDialogCancelButton -> setCallback (AptDialog_Cancel);
1082 AptDialogResetButton = new puOneShot (240, 10, 300, 50);
1083 AptDialogResetButton -> setLegend (gui_msg_RESET);
1084 AptDialogResetButton -> setCallback (AptDialog_Reset);
1086 cout << "NewAirportInit " << NewAirportId << endl;
1087 FG_FINALIZE_PUI_DIALOG( AptDialog );
1090 #ifdef FG_NETWORK_OLK
1092 /// The beginnings of networking :-)
1093 // Needs cleaning up but works
1094 // These statics should disapear when this is a class
1095 static puDialogBox *NetIdDialog = 0;
1096 static puFrame *NetIdDialogFrame = 0;
1097 static puText *NetIdDialogMessage = 0;
1098 static puInput *NetIdDialogInput = 0;
1100 static char NewNetId[16];
1101 static char NewNetIdLabel[] = "Enter New Callsign";
1102 extern char *fgd_callsign;
1104 static puOneShot *NetIdDialogOkButton = 0;
1105 static puOneShot *NetIdDialogCancelButton = 0;
1107 void NetIdDialog_Cancel(puObject *)
1109 FG_POP_PUI_DIALOG( NetIdDialog );
1112 void NetIdDialog_OK (puObject *)
1116 FGTime *t = FGTime::cur_time_params;
1117 int PauseMode = t->getPause();
1119 t->togglePauseMode();
1121 The following needs some cleanup because
1122 "string options.NetId" and "char *net_callsign"
1124 NetIdDialogInput->getValue(&net_callsign);
1125 NetId = net_callsign;
1127 NetIdDialog_Cancel( NULL );
1128 current_options.set_net_id( NetId.c_str() );
1129 strcpy( fgd_callsign, net_callsign);
1130 // strcpy( fgd_callsign, current_options.get_net_id().c_str());
1131 /* Entering a callsign indicates : user wants Net HUD Info */
1132 net_hud_display = 1;
1134 if( PauseMode != t->getPause() )
1135 t->togglePauseMode();
1138 void NewCallSign(puObject *cb)
1140 sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1141 // sprintf( NewNetId, "%s", fgd_callsign );
1142 NetIdDialogInput->setValue( NewNetId );
1144 FG_PUSH_PUI_DIALOG( NetIdDialog );
1147 static void NewNetIdInit(void)
1149 sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1150 // sprintf( NewNetId, "%s", fgd_callsign );
1151 int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
1152 NewNetIdLabel ) / 2;
1154 NetIdDialog = new puDialogBox (150, 50);
1156 NetIdDialogFrame = new puFrame (0,0,350, 150);
1157 NetIdDialogMessage = new puText (len, 110);
1158 NetIdDialogMessage -> setLabel (NewNetIdLabel);
1160 NetIdDialogInput = new puInput (50, 70, 300, 100);
1161 NetIdDialogInput -> setValue (NewNetId);
1162 NetIdDialogInput -> acceptInput();
1164 NetIdDialogOkButton = new puOneShot (50, 10, 110, 50);
1165 NetIdDialogOkButton -> setLegend (gui_msg_OK);
1166 NetIdDialogOkButton -> setCallback (NetIdDialog_OK);
1167 NetIdDialogOkButton -> makeReturnDefault(TRUE);
1169 NetIdDialogCancelButton = new puOneShot (240, 10, 300, 50);
1170 NetIdDialogCancelButton -> setLegend (gui_msg_CANCEL);
1171 NetIdDialogCancelButton -> setCallback (NetIdDialog_Cancel);
1174 FG_FINALIZE_PUI_DIALOG( NetIdDialog );
1177 static void net_display_toggle( puObject *cb)
1179 net_hud_display = (net_hud_display) ? 0 : 1;
1180 printf("Toggle net_hud_display : %d\n", net_hud_display);
1183 static void net_register( puObject *cb)
1185 fgd_send_com( "1", FGFS_host );
1186 net_is_registered = 0;
1187 printf("Registering to deamon\n");
1190 static void net_unregister( puObject *cb)
1192 fgd_send_com( "8", FGFS_host );
1193 net_is_registered = -1;
1194 printf("Unregistering from deamon\n");
1198 /*************** Deamon communication **********/
1200 // These statics should disapear when this is a class
1201 static puDialogBox *NetFGDDialog = 0;
1202 static puFrame *NetFGDDialogFrame = 0;
1203 static puText *NetFGDDialogMessage = 0;
1204 //static puInput *NetFGDDialogInput = 0;
1206 //static char NewNetId[16];
1207 static char NewNetFGDLabel[] = "Scan for deamon ";
1208 static char NewFGDHost[64] = "olk.mcp.de";
1209 static int NewFGDPortLo = 10000;
1210 static int NewFGDPortHi = 10001;
1212 //extern char *fgd_callsign;
1213 extern u_short base_port, end_port;
1214 extern int fgd_ip, verbose, current_port;
1215 extern char *fgd_host;
1218 static puOneShot *NetFGDDialogOkButton = 0;
1219 static puOneShot *NetFGDDialogCancelButton = 0;
1220 static puOneShot *NetFGDDialogScanButton = 0;
1222 static puInput *NetFGDHostDialogInput = 0;
1223 static puInput *NetFGDPortLoDialogInput = 0;
1224 static puInput *NetFGDPortHiDialogInput = 0;
1226 void NetFGDDialog_Cancel(puObject *)
1228 FG_POP_PUI_DIALOG( NetFGDDialog );
1231 void NetFGDDialog_OK (puObject *)
1235 FGTime *t = FGTime::cur_time_params;
1236 int PauseMode = t->getPause();
1237 if(!PauseMode) t->togglePauseMode();
1238 NetFGDHostDialogInput->getValue( &NetFGD );
1239 strcpy( fgd_host, NetFGD);
1240 NetFGDPortLoDialogInput->getValue( (int *) &base_port );
1241 NetFGDPortHiDialogInput->getValue( (int *) &end_port );
1242 NetFGDDialog_Cancel( NULL );
1243 if( PauseMode != t->getPause() )
1244 t->togglePauseMode();
1247 void NetFGDDialog_SCAN (puObject *)
1252 FGTime *t = FGTime::cur_time_params;
1253 int PauseMode = t->getPause();
1254 if(!PauseMode) t->togglePauseMode();
1255 // printf("Vor getvalue %s\n");
1256 NetFGDHostDialogInput->getValue( &NetFGD );
1257 // printf("Vor strcpy %s\n", (char *) NetFGD);
1258 strcpy( fgd_host, NetFGD);
1259 NetFGDPortLoDialogInput->getValue( (int *) &base_port );
1260 NetFGDPortHiDialogInput->getValue( (int *) &end_port );
1261 printf("FGD: %s Port-Start: %d Port-End: %d\n", fgd_host,
1262 base_port, end_port);
1263 net_resolv_fgd(fgd_host);
1264 printf("Resolve : %d\n", net_r);
1265 if( PauseMode != t->getPause() ) t->togglePauseMode();
1268 strcpy( fgd_name, "");
1269 for( current_port = base_port; ( current_port <= end_port); current_port++) {
1270 fgd_send_com("0" , FGFS_host);
1271 sprintf( NewNetFGDLabel , "Scanning for deamon Port: %d", current_port);
1272 printf("FGD: searching %s\n", fgd_name);
1273 if ( strcmp( fgd_name, "") != 0 ) {
1274 sprintf( NewNetFGDLabel , "Found %s at Port: %d",
1275 fgd_name, current_port);
1276 fgd_port = current_port;
1277 current_port = end_port+1;
1280 current_port = end_port = base_port = fgd_port;
1282 NetFGDDialog_Cancel( NULL );
1286 void net_fgd_scan(puObject *cb)
1288 NewFGDPortLo = base_port;
1289 NewFGDPortHi = end_port;
1290 strcpy( NewFGDHost, fgd_host);
1291 NetFGDPortLoDialogInput->setValue( NewFGDPortLo );
1292 NetFGDPortHiDialogInput->setValue( NewFGDPortHi );
1293 NetFGDHostDialogInput->setValue( NewFGDHost );
1295 FG_PUSH_PUI_DIALOG( NetFGDDialog );
1299 static void NewNetFGDInit(void)
1301 // sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1302 // sprintf( NewNetId, "%s", fgd_callsign );
1303 int len = 170 - puGetStringWidth( puGetDefaultLabelFont(),
1304 NewNetFGDLabel ) / 2;
1306 NetFGDDialog = new puDialogBox (310, 30);
1308 NetFGDDialogFrame = new puFrame (0,0,320, 170);
1309 NetFGDDialogMessage = new puText (len, 140);
1310 NetFGDDialogMessage -> setLabel (NewNetFGDLabel);
1312 NetFGDPortLoDialogInput = new puInput (50, 70, 127, 100);
1313 NetFGDPortLoDialogInput -> setValue (NewFGDPortLo);
1314 NetFGDPortLoDialogInput -> acceptInput();
1316 NetFGDPortHiDialogInput = new puInput (199, 70, 275, 100);
1317 NetFGDPortHiDialogInput -> setValue (NewFGDPortHi);
1318 NetFGDPortHiDialogInput -> acceptInput();
1320 NetFGDHostDialogInput = new puInput (50, 100, 275, 130);
1321 NetFGDHostDialogInput -> setValue (NewFGDHost);
1322 NetFGDHostDialogInput -> acceptInput();
1324 NetFGDDialogScanButton = new puOneShot (130, 10, 200, 50);
1325 NetFGDDialogScanButton -> setLegend ("Scan");
1326 NetFGDDialogScanButton -> setCallback (NetFGDDialog_SCAN);
1327 NetFGDDialogScanButton -> makeReturnDefault(FALSE);
1329 NetFGDDialogOkButton = new puOneShot (50, 10, 120, 50);
1330 NetFGDDialogOkButton -> setLegend (gui_msg_OK);
1331 NetFGDDialogOkButton -> setCallback (NetFGDDialog_OK);
1332 NetFGDDialogOkButton -> makeReturnDefault(TRUE);
1334 NetFGDDialogCancelButton = new puOneShot (210, 10, 280, 50);
1335 NetFGDDialogCancelButton -> setLegend (gui_msg_CANCEL);
1336 NetFGDDialogCancelButton -> setCallback (NetFGDDialog_Cancel);
1339 FG_FINALIZE_PUI_DIALOG( NetFGDDialog );
1343 static void net_display_toggle( puObject *cb)
1345 net_hud_display = (net_hud_display) ? 0 : 1;
1346 printf("Toggle net_hud_display : %d\n", net_hud_display);
1353 /*************** End Networking **************/
1357 /* -----------------------------------------------------------------------
1359 ---------------------------------------------------------------------*/
1360 char *fileSubmenu [] = {
1361 "Exit", /* "Close", "---------", */
1362 #if defined( WIN32 ) && !defined( __CYGWIN__)
1366 /* "---------", "Save", */
1369 puCallback fileSubmenuCb [] = {
1370 MayBeGoodBye, /* hideMenuCb, NULL, */
1371 #if defined( WIN32 ) && !defined( __CYGWIN__)
1380 char *editSubmenu [] = {
1383 puCallback editSubmenuCb [] = {
1388 char *viewSubmenu [] = {
1389 /* "Cockpit View > ", "View >","------------", */ "Toggle Panel...", NULL
1391 puCallback viewSubmenuCb [] = {
1392 /* notCb, notCb, NULL, */ guiTogglePanel, NULL
1395 char *aircraftSubmenu [] = {
1396 "Autopilot", "Heading", "Altitude", "Navigation", "Airport",
1397 /* "Communication", */ NULL
1399 puCallback aircraftSubmenuCb [] = {
1400 fgAPAdjust, NewHeading, NewAltitude, fgLatLonFormatToggle, NewTgtAirport,
1404 char *environmentSubmenu [] = {
1405 "Airport", /* "Terrain", "Weather", */ NULL
1407 puCallback environmentSubmenuCb [] = {
1408 NewAirport, /* notCb, notCb, */ NULL
1412 char *optionsSubmenu [] = {
1413 "Preferences", "Realism & Reliablity...", NULL
1415 puCallback optionsSubmenuCb [] = {
1420 #ifdef FG_NETWORK_OLK
1421 char *networkSubmenu [] = {
1422 "Unregister from FGD ", /* "Send MSG to All", "Send MSG", "Show Pilots", */
1424 "Scan for Deamons", "Enter Callsign", /* "Display Netinfos", */
1425 "Toggle Display", NULL
1427 puCallback networkSubmenuCb [] = {
1428 /* notCb, notCb, notCb, notCb, */
1431 net_fgd_scan, NewCallSign,
1432 net_display_toggle, NULL
1436 char *helpSubmenu [] = {
1437 /* "About...", */ "Help", NULL
1439 puCallback helpSubmenuCb [] = {
1440 /* notCb, */ helpCb, NULL
1444 /* -------------------------------------------------------------------------
1446 _____________________________________________________________________*/
1451 char *mesa_win_state;
1455 puSetDefaultStyle ( PUSTYLE_SMALL_BEVELLED ); //PUSTYLE_DEFAULT
1456 puSetDefaultColourScheme (0.8, 0.8, 0.8, 0.4);
1458 // Initialize our GLOBAL GUI STRINGS
1459 gui_msg_OK = msg_OK; // "OK"
1460 gui_msg_NO = msg_NO; // "NO"
1461 gui_msg_YES = msg_YES; // "YES"
1462 gui_msg_CANCEL = msg_CANCEL; // "CANCEL"
1463 gui_msg_RESET = msg_RESET; // "RESET"
1465 // Next check home directory
1467 char* envp = ::getenv( "FG_FONTS" );
1468 if ( envp != NULL ) {
1469 fntpath.set( envp );
1471 fntpath.set( current_options.get_fg_root() );
1472 fntpath.append( "Fonts" );
1475 // Install our fast fonts
1476 fntpath.append( "typewriter.txf" );
1477 guiFntHandle = new fntTexFont ;
1478 guiFntHandle -> load ( (char *)fntpath.c_str() ) ;
1479 puFont GuiFont ( guiFntHandle, 15 ) ;
1480 puSetDefaultFonts( GuiFont, GuiFont ) ;
1481 guiFnt = puGetDefaultLabelFont();
1483 if ( current_options.get_mouse_pointer() == 0 ) {
1484 // no preference specified for mouse pointer, attempt to autodetect...
1485 // Determine if we need to render the cursor, or if the windowing
1486 // system will do it. First test if we are rendering with glide.
1487 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1488 // Test for the MESA_GLX_FX env variable
1489 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
1490 // test if we are fullscreen mesa/glide
1491 if ( (mesa_win_state[0] == 'f') ||
1492 (mesa_win_state[0] == 'F') ) {
1497 mouse_active = ~mouse_active;
1498 } else if ( current_options.get_mouse_pointer() == 1 ) {
1499 // don't show pointer
1500 } else if ( current_options.get_mouse_pointer() == 2 ) {
1501 // force showing pointer
1503 mouse_active = ~mouse_active;
1506 // MOUSE_VIEW mode stuff
1507 trackball(_quat0, 0.0, 0.0, 0.0, 0.0);
1509 build_rotmatrix(quat_mat, curquat);
1511 // Set up our Dialog Boxes
1512 ConfirmExitDialogInit();
1514 #ifdef FG_NETWORK_OLK
1520 // Make the menu bar
1521 mainMenuBar = new puMenuBar ();
1522 mainMenuBar -> add_submenu ("File", fileSubmenu, fileSubmenuCb);
1523 // mainMenuBar -> add_submenu ("Edit", editSubmenu, editSubmenuCb);
1524 mainMenuBar -> add_submenu ("View", viewSubmenu, viewSubmenuCb);
1525 mainMenuBar -> add_submenu ("Aircraft", aircraftSubmenu, aircraftSubmenuCb);
1526 mainMenuBar -> add_submenu ("Environment", environmentSubmenu, environmentSubmenuCb);
1527 // mainMenuBar -> add_submenu ("Options", optionsSubmenu, optionsSubmenuCb);
1528 #ifdef FG_NETWORK_OLK
1529 mainMenuBar -> add_submenu ("Network", networkSubmenu, networkSubmenuCb);
1531 mainMenuBar -> add_submenu ("Help", helpSubmenu, helpSubmenuCb);
1532 mainMenuBar-> close ();
1533 // Set up menu bar toggle
1542 * Implementation of a virtual trackball.
1543 * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
1544 * the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
1546 * Vector manip code:
1548 * Original code from:
1549 * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
1551 * Much mucking with by:
1554 #if defined(_WIN32) && !defined( __CYGWIN32__ )
1555 #pragma warning (disable:4244) /* disable bogus conversion warnings */
1559 //#include "trackball.h"
1562 * This size should really be based on the distance from the center of
1563 * rotation to the point on the object underneath the mouse. That
1564 * point would then track the mouse as closely as possible. This is a
1565 * simple example, though, so that is left as an Exercise for the
1568 #define TRACKBALLSIZE (0.8f)
1569 #define SQRT(x) sqrt(x)
1572 * Local function prototypes (not defined in trackball.h)
1574 static float tb_project_to_sphere(float, float, float);
1575 static void normalize_quat(float [4]);
1586 vset(float *v, float x, float y, float z)
1594 vsub(const float *src1, const float *src2, float *dst)
1596 dst[0] = src1[0] - src2[0];
1597 dst[1] = src1[1] - src2[1];
1598 dst[2] = src1[2] - src2[2];
1602 vcopy(const float *v1, float *v2)
1605 for (i = 0 ; i < 3 ; i++)
1610 vcross(const float *v1, const float *v2, float *cross)
1614 temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
1615 temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
1616 temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
1621 vlength(const float *v)
1623 float tmp = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
1628 vscale(float *v, float div)
1638 vscale(v,1.0/vlength(v));
1642 vdot(const float *v1, const float *v2)
1644 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
1648 vadd(const float *src1, const float *src2, float *dst)
1650 dst[0] = src1[0] + src2[0];
1651 dst[1] = src1[1] + src2[1];
1652 dst[2] = src1[2] + src2[2];
1656 * Given an axis and angle, compute quaternion.
1659 axis_to_quat(float a[3], float phi, float q[4])
1661 double sinphi2, cosphi2;
1662 double phi2 = phi/2.0;
1663 sinphi2 = sin(phi2);
1664 cosphi2 = cos(phi2);
1672 * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
1673 * if we are away from the center of the sphere.
1676 tb_project_to_sphere(float r, float x, float y)
1682 if (d < r * 0.70710678118654752440) { /* Inside sphere */
1685 } else { /* On hyperbola */
1686 t = r / 1.41421356237309504880;
1693 * Quaternions always obey: a^2 + b^2 + c^2 + d^2 = 1.0
1694 * If they don't add up to 1.0, dividing by their magnitued will
1697 * Note: See the following for more information on quaternions:
1699 * - Shoemake, K., Animating rotation with quaternion curves, Computer
1700 * Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
1701 * - Pletinckx, D., Quaternion calculus as a basic tool in computer
1702 * graphics, The Visual Computer 5, 2-13, 1989.
1705 normalize_quat(float q[4])
1710 tmp = q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3];
1711 mag = 1.0 / SQRT(tmp);
1712 for (i = 0; i < 4; i++)
1717 * Ok, simulate a track-ball. Project the points onto the virtual
1718 * trackball, then figure out the axis of rotation, which is the cross
1719 * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
1720 * Note: This is a deformed trackball-- is a trackball in the center,
1721 * but is deformed into a hyperbolic sheet of rotation away from the
1722 * center. This particular function was chosen after trying out
1723 * several variations.
1725 * It is assumed that the arguments to this routine are in the range
1729 trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
1731 float a[3]; /* Axis of rotation */
1732 float phi; /* how much to rotate about axis */
1733 float p1[3], p2[3], d[3];
1736 if (p1x == p2x && p1y == p2y) {
1744 * First, figure out z-coordinates for projection of P1 and P2 to
1747 vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
1748 vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
1751 * Now, we want the cross product of P1 and P2
1756 * Figure out how much to rotate around that axis.
1759 t = vlength(d) / (2.0*TRACKBALLSIZE);
1762 * Avoid problems with out-of-control values...
1764 if (t > 1.0) t = 1.0;
1765 if (t < -1.0) t = -1.0;
1766 phi = 2.0 * asin(t);
1768 axis_to_quat(a,phi,q);
1772 * Given two rotations, e1 and e2, expressed as quaternion rotations,
1773 * figure out the equivalent single rotation and stuff it into dest.
1775 * This routine also normalizes the result every RENORMCOUNT times it is
1776 * called, to keep error from creeping in.
1778 * NOTE: This routine is written so that q1 or q2 may be the same
1779 * as dest (or each other).
1782 #define RENORMCOUNT 97
1785 add_quats(float q1[4], float q2[4], float dest[4])
1788 float t1[4], t2[4], t3[4];
1792 printf("q1 = %f %f %f %f\n", q1[0], q1[1], q1[2], q1[3]);
1793 printf("q2 = %f %f %f %f\n", q2[0], q2[1], q2[2], q2[3]);
1805 tf[3] = q1[3] * q2[3] - vdot(q1,q2);
1808 printf("tf = %f %f %f %f\n", tf[0], tf[1], tf[2], tf[3]);
1816 if (++count > RENORMCOUNT) {
1818 normalize_quat(dest);
1823 * Build a rotation matrix, given a quaternion rotation.
1827 build_rotmatrix(float m[4][4], float q[4])
1829 //#define TRANSPOSED_QUAT
1830 #ifndef TRANSPOSED_QUAT
1831 m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
1832 m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
1833 m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
1836 m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
1837 m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
1838 m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
1841 m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
1842 m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
1843 m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
1850 #else // TRANSPOSED_QUAT
1851 m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
1852 m[0][1] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
1853 m[0][2] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
1856 m[1][0] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
1857 m[1][1] = 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
1858 m[1][2] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
1861 m[2][0] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
1862 m[2][1] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
1863 m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);