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 <Include/compiler.h>
33 #ifdef FG_MATH_EXCEPTION_CLASH
44 #if defined(FX) && defined(XMESA)
45 # include <GL/xmesa.h>
53 #include <Include/general.hxx>
54 #include <Include/fg_constants.h>
55 #include <Debug/logstream.hxx>
56 #include <Aircraft/aircraft.hxx>
57 #include <Airports/simple.hxx>
58 #include <Cockpit/panel.hxx>
59 #include <Controls/controls.hxx>
60 #include <FDM/flight.hxx>
61 #include <Main/options.hxx>
62 #include <Main/fg_init.hxx>
63 #include <Main/views.hxx>
64 #include <Misc/fgpath.hxx>
66 #include <NetworkOLK/network.h>
68 #include <Screen/screen-dump.hxx>
69 #include <Time/fg_time.hxx>
71 #if defined( WIN32 ) && !defined( __CYGWIN__ )
72 # include <Screen/win32-printer.h>
73 # include <Screen/GlBitmaps.h>
78 * A virtual trackball implementation
79 * Written by Gavin Bell for Silicon Graphics, November 1988.
82 * Pass the x and y coordinates of the last and current positions of
83 * the mouse, scaled so they are from (-1.0 ... 1.0).
85 * The resulting rotation is returned as a quaternion rotation in the
89 trackball(float q[4], float p1x, float p1y, float p2x, float p2y);
92 * Given two quaternions, add them together to get a third quaternion.
93 * Adding quaternions to get a compound rotation is analagous to adding
94 * translations to get a compound translation. When incrementally
95 * adding rotations, the first argument here should be the new
97 * rotation, the second and third the total rotation (which will be
98 * over-written with the resulting new total rotation).
101 add_quats(float *q1, float *q2, float *dest);
104 * A useful function, builds a rotation matrix in Matrix based on
108 build_rotmatrix(float m[4][4], float q[4]);
111 * This function computes a quaternion based on an axis (defined by
112 * the given vector) and an angle about which to rotate. The angle is
113 * expressed in radians. The result is put into the third argument.
116 axis_to_quat(float a[3], float phi, float q[4]);
121 FG_USING_STD(string);
123 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
127 #if defined(WIN32) || defined(__CYGWIN32__)
128 #define WIN32_CURSOR_TWEAKS
129 #elif (GLUT_API_VERSION >= 4 || GLUT_XLIB_IMPLEMENTATION >= 9)
130 #define X_CURSOR_TWEAKS
133 // hack, should come from an include someplace
134 extern void fgInitVisuals( void );
135 extern void fgReshape( int width, int height );
136 extern void fgRenderFrame( void );
139 fntTexFont *guiFntHandle = 0;
141 static puMenuBar *mainMenuBar = 0;
142 //static puButton *hideMenuButton = 0;
144 static puDialogBox *dialogBox = 0;
145 static puFrame *dialogFrame = 0;
146 static puText *dialogBoxMessage = 0;
147 static puOneShot *dialogBoxOkButton = 0;
150 static puDialogBox *YNdialogBox = 0;
151 static puFrame *YNdialogFrame = 0;
152 static puText *YNdialogBoxMessage = 0;
153 static puOneShot *YNdialogBoxOkButton = 0;
154 static puOneShot *YNdialogBoxNoButton = 0;
156 static char msg_OK[] = "OK";
157 static char msg_NO[] = "NO";
158 static char msg_YES[] = "YES";
159 static char msg_CANCEL[] = "Cancel";
160 static char msg_RESET[] = "Reset";
162 char *gui_msg_OK; // "OK"
163 char *gui_msg_NO; // "NO"
164 char *gui_msg_YES; // "YES"
165 char *gui_msg_CANCEL; // "CANCEL"
166 char *gui_msg_RESET; // "RESET"
168 static char global_dialog_string[256];
170 // from autopilot.cxx
171 extern void NewAltitude( puObject *cb );
172 extern void NewHeading( puObject *cb );
173 extern void fgAPAdjust( puObject * );
174 extern void NewTgtAirport( puObject *cb );
175 bool fgAPTerrainFollowEnabled( void );
176 bool fgAPAltitudeEnabled( void );
177 bool fgAPHeadingEnabled( void );
178 bool fgAPWayPointEnabled( void );
179 bool fgAPAutoThrottleEnabled( void );
182 extern void fgLatLonFormatToggle( puObject *);
184 /* --------------------------------------------------------------------
186 ---------------------------------------------------------------------*/
190 static int _savedX = 0;
191 static int _savedY = 0;
192 static int last_buttons = 0 ;
193 static int mouse_active = 0;
194 static int menu_on = 0;
195 static int mouse_joystick_control = 0;
197 //static time_t mouse_off_time;
198 //static int mouse_timed_out;
200 // to allow returning to previous view
201 // on second left click in MOUSE_VIEW mode
202 // This has file scope so that it can be reset
203 // if the little rodent is moved NHV
204 static int _mVtoggle;
206 // we break up the glutGetModifiers return mask
207 // once per loop and stash what we need in these
208 static int glut_active_shift;
209 static int glut_active_ctrl;
210 static int glut_active_alt;
212 static float lastquat[4];
213 static float curquat[4];
214 static float _quat0[4];
215 float quat_mat[4][4];
217 // uncomment this for view to exactly follow mouse in MOUSE_VIEW mode
218 // else smooth out the view panning to .01 radian per frame
219 // see view_offset smoothing mechanism in main.cxx
220 #define NO_SMOOTH_MOUSE_VIEW
222 // uncomment following to
223 #define RESET_VIEW_ON_LEAVING_MOUSE_VIEW
225 /* --------------------------------------------------------------------
226 Support for mouse as control yoke (david@megginson.com)
228 - right button toggles between pointer and yoke
229 - horizontal drag with no buttons moves ailerons
230 - vertical drag with no buttons moves elevators
231 - horizontal drag with left button moves brakes (left=on)
232 - vertical drag with left button moves throttle (up=more)
233 - horizontal drag with middle button moves rudder
234 - vertical drag with middle button moves trim
236 For the *_sensitivity variables, a lower number means more sensitive.
238 TODO: figure out how to keep pointer from leaving window in yoke mode.
239 TODO: add thresholds and null zones
240 TODO: sensitivity should be configurable at user option.
241 TODO: allow differential braking (this will be useful if FlightGear
242 ever supports tail-draggers like the DC-3)
243 ---------------------------------------------------------------------*/
251 MouseMode mouse_mode = MOUSE_POINTER;
253 static double aileron_sensitivity = 1.0/500.0;
254 static double elevator_sensitivity = 1.0/500.0;
255 static double brake_sensitivity = 1.0/250.0;
256 static double throttle_sensitivity = 1.0/250.0;
257 static double rudder_sensitivity = 1.0/500.0;
258 static double trim_sensitivity = 1.0/1000.0;
260 static inline void Quat0( void ) {
261 curquat[0] = _quat0[0];
262 curquat[1] = _quat0[1];
263 curquat[2] = _quat0[2];
264 curquat[3] = _quat0[3];
267 static inline int left_button( void ) {
268 return( last_buttons & (1 << GLUT_LEFT_BUTTON) );
271 static inline int middle_button( void ) {
272 return( last_buttons & (1 << GLUT_MIDDLE_BUTTON) );
275 static inline int right_button( void ) {
276 return( last_buttons & (1 << GLUT_RIGHT_BUTTON) );
279 static inline void TurnCursorOn( void )
282 #if defined(WIN32_CURSOR_TWEAKS)
283 switch (mouse_mode) {
285 glutSetCursor(GLUT_CURSOR_INHERIT);
288 glutSetCursor(GLUT_CURSOR_CROSSHAIR);
291 glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
295 #if defined(X_CURSOR_TWEAKS)
296 glutWarpPointer( current_view.get_winWidth()/2, current_view.get_winHeight()/2);
300 static inline void TurnCursorOff( void )
303 #if defined(WIN32_CURSOR_TWEAKS)
304 glutSetCursor(GLUT_CURSOR_NONE);
305 #elif defined(X_CURSOR_TWEAKS)
306 glutWarpPointer( current_view.get_winWidth(), current_view.get_winHeight());
310 void maybeToggleMouse( void )
312 #if defined(WIN32_CURSOR_TWEAKS)
313 static int first_time = ~0;
314 static int mouse_changed = 0;
318 mouse_changed = ~mouse_changed;
322 if( mouse_mode != MOUSE_POINTER )
324 if( mouse_changed ) {
325 mouse_changed = ~mouse_changed;
331 first_time = ~first_time;
332 #endif // #ifdef WIN32
335 // Call with FALSE to init and TRUE to restore
336 void BusyCursor( int restore )
338 static GLenum cursor = (GLenum) 0;
340 glutSetCursor(cursor);
342 cursor = (GLenum) glutGet( (GLenum) GLUT_WINDOW_CURSOR );
343 #if defined(WIN32_CURSOR_TWEAKS)
346 glutSetCursor( GLUT_CURSOR_WAIT );
350 int guiGetMouseButton(void)
355 void guiGetMouse(int *x, int *y)
361 void guiMotionFunc ( int x, int y )
363 int ww, wh, need_warp = 0;
366 // FGTime *t = FGTime::cur_time_params;
367 // if( mouse_timed_out ) {
368 // if( t->get_cur_time() > mouse_off_time ) {
369 // moused_timed_out = 0;
371 // glutPostRedisplay () ;
375 if (mouse_mode == MOUSE_POINTER) {
377 glutPostRedisplay () ;
379 if( x == _mX && y == _mY)
382 // reset left click MOUSE_VIEW toggle feature
385 ww = current_view.get_winWidth();
386 wh = current_view.get_winHeight();
388 switch (mouse_mode) {
390 if( !mouse_joystick_control ) {
391 mouse_joystick_control = 1;
392 current_options.set_control_mode( fgOPTIONS::FG_MOUSE );
394 if ( left_button() ) {
395 offset = (_mX - x) * brake_sensitivity;
396 controls.move_brake(FGControls::ALL_WHEELS, offset);
397 offset = (_mY - y) * throttle_sensitivity;
398 controls.move_throttle(FGControls::ALL_ENGINES, offset);
399 } else if ( right_button() ) {
400 if( !fgAPHeadingEnabled() ) {
401 offset = (x - _mX) * rudder_sensitivity;
402 controls.move_rudder(offset);
404 if( !fgAPAltitudeEnabled() ) {
405 offset = (_mY - y) * trim_sensitivity;
406 controls.move_elevator_trim(offset);
409 if( !fgAPHeadingEnabled() ) {
410 offset = (x - _mX) * aileron_sensitivity;
411 controls.move_aileron(offset);
413 if( !fgAPAltitudeEnabled() ) {
414 offset = (_mY - y) * elevator_sensitivity;
415 controls.move_elevator(offset);
419 // Keep the mouse in the window.
420 if (x < 5 || x > ww-5 || y < 5 || y > wh-5) {
429 #define CONTRAINED_MOUSE_VIEW_Y
430 #ifdef CONTRAINED_MOUSE_VIEW_Y
434 #endif // CONTRAINED_MOUSE_VIEW_Y
436 } else if( y >= wh-1) {
437 #ifdef CONTRAINED_MOUSE_VIEW_Y
441 #endif // CONTRAINED_MOUSE_VIEW_Y
444 // wrap MOUSE_VIEW mode cursor x position
448 } else if ( x >= ww-1 ) {
452 // try to get FG_PI movement in each half of screen
456 if( middle_button() ) {
458 (2.0f * _mX - W) / W,
459 0, //(H - 2.0f * y) / H, // 3
461 0 //(H - 2.0f * _mY) / H // 1
468 0, //(2.0f * _mX - W) / W, // 0
469 (H - 2.0f * y) / H, // 3
470 0, //(2.0f * x - W) / W, // 2
471 (H - 2.0f * _mY) / H // 1
474 add_quats(lastquat, curquat, curquat);
475 build_rotmatrix(quat_mat, curquat);
478 // this could be done in above quat
479 // but requires redoing view pipeline
480 offset = current_view.get_goal_view_offset();
481 offset += ((_mX - x) * FG_2PI / W );
482 while (offset < 0.0) {
485 while (offset > FG_2PI) {
488 current_view.set_goal_view_offset(offset);
489 #ifdef NO_SMOOTH_MOUSE_VIEW
490 current_view.set_view_offset(offset);
499 glutWarpPointer(x, y);
501 // Record the new mouse position.
507 void guiMouseFunc(int button, int updown, int x, int y)
511 // private MOUSE_VIEW state variables
512 // to allow alternate left clicks in MOUSE_VIEW mode
513 // to toggle between current offsets and straight ahead
515 static int _mVx, _mVy, _Vx, _Vy;
516 static float _quat[4];
517 static double _view_offset;
519 // general purpose variables
522 glutModifiers = glutGetModifiers();
523 glut_active_shift = glutModifiers & GLUT_ACTIVE_SHIFT;
524 glut_active_ctrl = glutModifiers & GLUT_ACTIVE_CTRL;
525 glut_active_alt = glutModifiers & GLUT_ACTIVE_ALT;
527 // Was the left button pressed?
528 if (updown == GLUT_DOWN ) {
529 if( button == GLUT_LEFT_BUTTON)
531 switch (mouse_mode) {
538 // resume previous view offsets
543 curquat[0] = _quat[0];
544 curquat[1] = _quat[1];
545 curquat[2] = _quat[2];
546 curquat[3] = _quat[3];
547 current_view.set_goal_view_offset(_view_offset);
548 #ifdef NO_SMOOTH_MOUSE_VIEW
549 current_view.set_view_offset(_view_offset);
557 _quat[0] = curquat[0];
558 _quat[1] = curquat[1];
559 _quat[2] = curquat[2];
560 _quat[3] = curquat[3];
561 x = current_view.get_winWidth()/2;
562 y = current_view.get_winHeight()/2;
564 _view_offset = current_view.get_goal_view_offset();
565 current_view.set_goal_view_offset(0.0);
566 #ifdef NO_SMOOTH_MOUSE_VIEW
567 current_view.set_view_offset(0.0);
570 glutWarpPointer( x , y);
571 build_rotmatrix(quat_mat, curquat);
572 _mVtoggle = ~_mVtoggle;
575 }else if ( button == GLUT_RIGHT_BUTTON) {
576 switch (mouse_mode) {
578 mouse_mode = MOUSE_YOKE;
579 mouse_joystick_control = 0;
582 // start with zero point in center of screen
583 _mX = current_view.get_winWidth()/2;
584 _mY = current_view.get_winHeight()/2;
586 // try to have the MOUSE_YOKE position
587 // reflect the current stick position
588 offset = controls.get_aileron();
589 x = _mX - (int)(offset * aileron_sensitivity);
590 offset = controls.get_elevator();
591 y = _mY - (int)(offset * elevator_sensitivity);
593 glutSetCursor(GLUT_CURSOR_CROSSHAIR);
594 FG_LOG( FG_INPUT, FG_INFO, "Mouse in yoke mode" );
598 mouse_mode = MOUSE_VIEW;
599 current_options.set_control_mode( fgOPTIONS::FG_JOYSTICK );
600 x = current_view.get_winWidth()/2;
601 y = current_view.get_winHeight()/2;
604 build_rotmatrix(quat_mat, curquat);
605 glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
606 FG_LOG( FG_INPUT, FG_INFO, "Mouse in view mode" );
610 mouse_mode = MOUSE_POINTER;
613 #ifdef RESET_VIEW_ON_LEAVING_MOUSE_VIEW
615 build_rotmatrix(quat_mat, curquat);
616 current_view.set_goal_view_offset(0.0);
617 #ifdef NO_SMOOTH_MOUSE_VIEW
618 current_view.set_view_offset(0.0);
620 #endif // RESET_VIEW_ON_LEAVING_MOUSE_VIEW
621 glutSetCursor(GLUT_CURSOR_INHERIT);
626 FG_LOG( FG_INPUT, FG_INFO, "Mouse in pointer mode" );
629 glutWarpPointer( x, y );
630 } // END RIGHT BUTTON
631 } // END UPDOWN == GLUT_DOWN
633 // Note which button is pressed.
634 if ( updown == GLUT_DOWN ) {
635 last_buttons |= ( 1 << button ) ;
637 last_buttons &= ~( 1 << button ) ;
640 // If we're in pointer mode, let PUI
641 // know what's going on.
642 if (mouse_mode == MOUSE_POINTER) {
643 puMouse (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 // Repair any damage done to the Panel by other Gui Items
671 void guiFixPanel( void )
675 if ( current_options.get_panel_status() ) {
676 FGView *v = ¤t_view;
677 FGTime *t = FGTime::cur_time_params;
679 if( (toggle_pause = !t->getPause()) )
680 t->togglePauseMode();
682 // this seems to be the only way to do this :-(
683 // problem is the viewport has been mucked with
684 xglViewport(0, 0 , (GLint)(v->winWidth), (GLint)(v->winHeight) );
685 FGPanel::OurPanel->ReInit(0, 0, 1024, 768);
688 t->togglePauseMode();
692 // Toggle the Menu and Mouse display state
693 void guiToggleMenu(void)
696 // printf("Hiding Menu\n");
697 mainMenuBar->hide ();
698 #if defined(WIN32_CURSOR_TWEAKS)
699 if( mouse_mode == MOUSE_POINTER )
701 #endif // #ifdef WIN32_CURSOR_TWEAKS
703 // printf("Showing Menu\n");
704 mainMenuBar->reveal();
707 #endif // #ifdef WIN32
712 /* -----------------------------------------------------------------------
713 the Gui callback functions
714 ____________________________________________________________________*/
716 void reInit(puObject *cb)
720 build_rotmatrix(quat_mat, curquat);
721 fgReInitSubsystems();
725 // This is the accessor function
726 void guiTogglePanel(puObject *cb)
728 current_options.toggle_panel();
731 //void MenuHideMenuCb(puObject *cb)
732 void hideMenuCb (puObject *cb)
737 void goodBye(puObject *)
739 // FG_LOG( FG_INPUT, FG_ALERT,
740 // "Program exiting normally at user request." );
741 cout << "Program exiting normally at user request." << endl;
743 #ifdef FG_NETWORK_OLK
744 if ( net_is_registered == 0 ) fgd_send_com( "8", FGFS_host);
754 void goAwayCb (puObject *me)
756 FG_POP_PUI_DIALOG( dialogBox );
759 void mkDialogInit (void)
761 // printf("mkDialogInit\n");
762 int x = (current_options.get_xsize()/2 - 400/2);
763 int y = (current_options.get_ysize()/2 - 100/2);
764 dialogBox = new puDialogBox (x, y); // 150, 50
766 dialogFrame = new puFrame (0,0,400,100);
767 dialogBoxMessage = new puText (10, 70);
768 dialogBoxMessage -> setLabel ("");
769 dialogBoxOkButton = new puOneShot (180, 10, 240, 50);
770 dialogBoxOkButton -> setLegend (gui_msg_OK);
771 dialogBoxOkButton -> makeReturnDefault (TRUE );
772 dialogBoxOkButton -> setCallback (goAwayCb);
774 FG_FINALIZE_PUI_DIALOG( dialogBox );
777 void MayBeGoodBye(puObject *)
782 void goAwayYesNoCb(puObject *me)
784 FG_POP_PUI_DIALOG( YNdialogBox);
787 void ConfirmExitDialogInit(void)
789 char msg[] = "Really Quit";
792 // printf("ConfirmExitDialogInit\n");
793 int len = 200 - puGetStringWidth( puGetDefaultLabelFont(), msg )/2;
795 int x = (current_options.get_xsize()/2 - 400/2);
796 int y = (current_options.get_ysize()/2 - 100/2);
798 YNdialogBox = new puDialogBox (x, y); // 150, 50
799 // YNdialogBox = new puDialogBox (150, 50);
801 YNdialogFrame = new puFrame (0,0,400, 100);
803 YNdialogBoxMessage = new puText (len, 70);
804 YNdialogBoxMessage -> setDefaultValue (msg);
805 YNdialogBoxMessage -> getDefaultValue (&s);
806 YNdialogBoxMessage -> setLabel (s);
808 YNdialogBoxOkButton = new puOneShot (100, 10, 160, 50);
809 YNdialogBoxOkButton -> setLegend (gui_msg_OK);
810 YNdialogBoxOkButton -> setCallback (goodBye);
812 YNdialogBoxNoButton = new puOneShot (240, 10, 300, 50);
813 YNdialogBoxNoButton -> setLegend (gui_msg_NO);
814 // YNdialogBoxNoButton -> makeReturnDefault (TRUE );
815 YNdialogBoxNoButton -> setCallback (goAwayYesNoCb);
817 FG_FINALIZE_PUI_DIALOG( YNdialogBox );
820 void notCb (puObject *)
822 mkDialog ("This function isn't implemented yet");
825 void helpCb (puObject *)
829 #if defined(FX) && !defined(WIN32)
830 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
831 if ( global_fullscreen ) {
832 global_fullscreen = false;
833 XMesaSetFXmode( XMESA_FX_WINDOW );
839 string url = "http://www.flightgear.org/Docs/InstallGuide/getstart.html";
841 if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
842 command = "netscape -remote \"openURL(" + url + ")\" &";
844 command = "netscape " + url + " &";
847 command = "webrun.bat";
850 system( command.c_str() );
851 //string text = "Help started in netscape window.";
853 //mkDialog (text.c_str());
854 mkDialog ("Help started in netscape window.");
857 #if defined( WIN32 ) && !defined( __CYGWIN__)
859 static void rotateView( double roll, double pitch, double yaw )
864 static GLBitmap *b1 = NULL;
865 extern FGInterface cur_view_fdm;
866 GLubyte *hiResScreenCapture( int multiplier )
868 float oldfov = current_options.get_fov();
869 float fov = oldfov / multiplier;
870 FGView *v = ¤t_view;
871 current_options.set_fov(fov);
872 v->force_update_fov_math();
874 int cur_width = current_view.get_winWidth( );
875 int cur_height = current_view.get_winHeight( );
876 if (b1) delete( b1 );
877 // New empty (mostly) bitmap
878 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
880 for ( y = 0; y < multiplier; y++ )
882 for ( x = 0; x < multiplier; x++ )
884 fgReshape( cur_width, cur_height );
886 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
890 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
893 current_view.UpdateViewParams(cur_view_fdm);
894 current_options.set_fov(oldfov);
895 v->force_update_fov_math();
896 return b1->getBitmap();
901 #if defined( WIN32 ) && !defined( __CYGWIN__)
902 // win32 print screen function
903 void printScreen ( puObject *obj ) {
904 bool show_pu_cursor = false;
906 if ( !puCursorIsHidden() ) {
907 show_pu_cursor = true;
913 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
914 int cur_width = current_view.get_winWidth( );
915 int cur_height = current_view.get_winHeight( );
916 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
917 p.End( hiResScreenCapture(3) );
920 mainMenuBar->reveal();
923 if ( show_pu_cursor ) {
928 #endif // #ifdef WIN32
931 void dumpSnapShot ( puObject *obj ) {
936 // do a screen snap shot
937 void fgDumpSnapShot () {
938 bool show_pu_cursor = false;
942 if ( !puCursorIsHidden() ) {
943 show_pu_cursor = true;
948 fgReshape( current_options.get_xsize(), current_options.get_ysize() );
950 // we need two render frames here to clear the menu and cursor
951 // ... not sure why but doing an extra fgFenderFrame() shoulnd't
956 my_glDumpWindow( "fgfs-screen.ppm",
957 current_options.get_xsize(),
958 current_options.get_ysize() );
960 mkDialog ("Snap shot saved to fgfs-screen.ppm");
962 if ( show_pu_cursor ) {
968 mainMenuBar->reveal();
974 /// The beginnings of teleportation :-)
975 // Needs cleaning up but works
976 // These statics should disapear when this is a class
977 static puDialogBox *AptDialog = 0;
978 static puFrame *AptDialogFrame = 0;
979 static puText *AptDialogMessage = 0;
980 static puInput *AptDialogInput = 0;
982 static char NewAirportId[16];
983 static char NewAirportLabel[] = "Enter New Airport ID";
985 static puOneShot *AptDialogOkButton = 0;
986 static puOneShot *AptDialogCancelButton = 0;
987 static puOneShot *AptDialogResetButton = 0;
989 void AptDialog_Cancel(puObject *)
991 FG_POP_PUI_DIALOG( AptDialog );
994 void AptDialog_OK (puObject *)
999 FGTime *t = FGTime::cur_time_params;
1000 int PauseMode = t->getPause();
1002 t->togglePauseMode();
1005 AptDialogInput->getValue(&s);
1008 cout << "AptDialog_OK " << AptId << " " << AptId.length() << endl;
1010 AptDialog_Cancel( NULL );
1012 if ( AptId.length() ) {
1013 // set initial position from airport id
1014 FG_LOG( FG_GENERAL, FG_INFO,
1015 "Attempting to set starting position from airport code "
1018 airports.load("apt_simple");
1019 if ( airports.search( AptId, &a ) )
1021 current_options.set_airport_id( AptId.c_str() );
1023 fgReInitSubsystems();
1026 AptId += " not in database.";
1027 mkDialog(AptId.c_str());
1030 if( PauseMode != t->getPause() )
1031 t->togglePauseMode();
1034 void AptDialog_Reset(puObject *)
1036 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
1037 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1038 AptDialogInput->setValue ( NewAirportId );
1039 AptDialogInput->setCursor( 0 ) ;
1042 void NewAirport(puObject *cb)
1044 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
1045 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1046 // cout << "NewAirport " << NewAirportId << endl;
1047 AptDialogInput->setValue( NewAirportId );
1049 FG_PUSH_PUI_DIALOG( AptDialog );
1052 static void NewAirportInit(void)
1054 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1055 int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
1056 NewAirportLabel ) / 2;
1058 AptDialog = new puDialogBox (150, 50);
1060 AptDialogFrame = new puFrame (0,0,350, 150);
1061 AptDialogMessage = new puText (len, 110);
1062 AptDialogMessage -> setLabel (NewAirportLabel);
1064 AptDialogInput = new puInput (50, 70, 300, 100);
1065 AptDialogInput -> setValue (NewAirportId);
1066 AptDialogInput -> acceptInput();
1068 AptDialogOkButton = new puOneShot (50, 10, 110, 50);
1069 AptDialogOkButton -> setLegend (gui_msg_OK);
1070 AptDialogOkButton -> setCallback (AptDialog_OK);
1071 AptDialogOkButton -> makeReturnDefault(TRUE);
1073 AptDialogCancelButton = new puOneShot (140, 10, 210, 50);
1074 AptDialogCancelButton -> setLegend (gui_msg_CANCEL);
1075 AptDialogCancelButton -> setCallback (AptDialog_Cancel);
1077 AptDialogResetButton = new puOneShot (240, 10, 300, 50);
1078 AptDialogResetButton -> setLegend (gui_msg_RESET);
1079 AptDialogResetButton -> setCallback (AptDialog_Reset);
1081 cout << "NewAirportInit " << NewAirportId << endl;
1082 FG_FINALIZE_PUI_DIALOG( AptDialog );
1085 #ifdef FG_NETWORK_OLK
1087 /// The beginnings of networking :-)
1088 // Needs cleaning up but works
1089 // These statics should disapear when this is a class
1090 static puDialogBox *NetIdDialog = 0;
1091 static puFrame *NetIdDialogFrame = 0;
1092 static puText *NetIdDialogMessage = 0;
1093 static puInput *NetIdDialogInput = 0;
1095 static char NewNetId[16];
1096 static char NewNetIdLabel[] = "Enter New Callsign";
1097 extern char *fgd_callsign;
1099 static puOneShot *NetIdDialogOkButton = 0;
1100 static puOneShot *NetIdDialogCancelButton = 0;
1102 void NetIdDialog_Cancel(puObject *)
1104 FG_POP_PUI_DIALOG( NetIdDialog );
1107 void NetIdDialog_OK (puObject *)
1111 FGTime *t = FGTime::cur_time_params;
1112 int PauseMode = t->getPause();
1114 t->togglePauseMode();
1116 The following needs some cleanup because
1117 "string options.NetId" and "char *net_callsign"
1119 NetIdDialogInput->getValue(&net_callsign);
1120 NetId = net_callsign;
1122 NetIdDialog_Cancel( NULL );
1123 current_options.set_net_id( NetId.c_str() );
1124 strcpy( fgd_callsign, net_callsign);
1125 // strcpy( fgd_callsign, current_options.get_net_id().c_str());
1126 /* Entering a callsign indicates : user wants Net HUD Info */
1127 net_hud_display = 1;
1129 if( PauseMode != t->getPause() )
1130 t->togglePauseMode();
1133 void NewCallSign(puObject *cb)
1135 sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1136 // sprintf( NewNetId, "%s", fgd_callsign );
1137 NetIdDialogInput->setValue( NewNetId );
1139 FG_PUSH_PUI_DIALOG( NetIdDialog );
1142 static void NewNetIdInit(void)
1144 sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1145 // sprintf( NewNetId, "%s", fgd_callsign );
1146 int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
1147 NewNetIdLabel ) / 2;
1149 NetIdDialog = new puDialogBox (150, 50);
1151 NetIdDialogFrame = new puFrame (0,0,350, 150);
1152 NetIdDialogMessage = new puText (len, 110);
1153 NetIdDialogMessage -> setLabel (NewNetIdLabel);
1155 NetIdDialogInput = new puInput (50, 70, 300, 100);
1156 NetIdDialogInput -> setValue (NewNetId);
1157 NetIdDialogInput -> acceptInput();
1159 NetIdDialogOkButton = new puOneShot (50, 10, 110, 50);
1160 NetIdDialogOkButton -> setLegend (gui_msg_OK);
1161 NetIdDialogOkButton -> setCallback (NetIdDialog_OK);
1162 NetIdDialogOkButton -> makeReturnDefault(TRUE);
1164 NetIdDialogCancelButton = new puOneShot (240, 10, 300, 50);
1165 NetIdDialogCancelButton -> setLegend (gui_msg_CANCEL);
1166 NetIdDialogCancelButton -> setCallback (NetIdDialog_Cancel);
1169 FG_FINALIZE_PUI_DIALOG( NetIdDialog );
1172 static void net_display_toggle( puObject *cb)
1174 net_hud_display = (net_hud_display) ? 0 : 1;
1175 printf("Toggle net_hud_display : %d\n", net_hud_display);
1178 static void net_register( puObject *cb)
1180 fgd_send_com( "1", FGFS_host );
1181 net_is_registered = 0;
1182 printf("Registering to deamon\n");
1185 static void net_unregister( puObject *cb)
1187 fgd_send_com( "8", FGFS_host );
1188 net_is_registered = -1;
1189 printf("Unregistering from deamon\n");
1193 /*************** Deamon communication **********/
1195 // These statics should disapear when this is a class
1196 static puDialogBox *NetFGDDialog = 0;
1197 static puFrame *NetFGDDialogFrame = 0;
1198 static puText *NetFGDDialogMessage = 0;
1199 //static puInput *NetFGDDialogInput = 0;
1201 //static char NewNetId[16];
1202 static char NewNetFGDLabel[] = "Scan for deamon ";
1203 static char NewFGDHost[64] = "olk.mcp.de";
1204 static int NewFGDPortLo = 10000;
1205 static int NewFGDPortHi = 10001;
1207 //extern char *fgd_callsign;
1208 extern u_short base_port, end_port;
1209 extern int fgd_ip, verbose, current_port;
1210 extern char *fgd_host;
1213 static puOneShot *NetFGDDialogOkButton = 0;
1214 static puOneShot *NetFGDDialogCancelButton = 0;
1215 static puOneShot *NetFGDDialogScanButton = 0;
1217 static puInput *NetFGDHostDialogInput = 0;
1218 static puInput *NetFGDPortLoDialogInput = 0;
1219 static puInput *NetFGDPortHiDialogInput = 0;
1221 void NetFGDDialog_Cancel(puObject *)
1223 FG_POP_PUI_DIALOG( NetFGDDialog );
1226 void NetFGDDialog_OK (puObject *)
1230 FGTime *t = FGTime::cur_time_params;
1231 int PauseMode = t->getPause();
1232 if(!PauseMode) t->togglePauseMode();
1233 NetFGDHostDialogInput->getValue( &NetFGD );
1234 strcpy( fgd_host, NetFGD);
1235 NetFGDPortLoDialogInput->getValue( (int *) &base_port );
1236 NetFGDPortHiDialogInput->getValue( (int *) &end_port );
1237 NetFGDDialog_Cancel( NULL );
1238 if( PauseMode != t->getPause() )
1239 t->togglePauseMode();
1242 void NetFGDDialog_SCAN (puObject *)
1247 FGTime *t = FGTime::cur_time_params;
1248 int PauseMode = t->getPause();
1249 if(!PauseMode) t->togglePauseMode();
1250 // printf("Vor getvalue %s\n");
1251 NetFGDHostDialogInput->getValue( &NetFGD );
1252 // printf("Vor strcpy %s\n", (char *) NetFGD);
1253 strcpy( fgd_host, NetFGD);
1254 NetFGDPortLoDialogInput->getValue( (int *) &base_port );
1255 NetFGDPortHiDialogInput->getValue( (int *) &end_port );
1256 printf("FGD: %s Port-Start: %d Port-End: %d\n", fgd_host,
1257 base_port, end_port);
1258 net_resolv_fgd(fgd_host);
1259 printf("Resolve : %d\n", net_r);
1260 if( PauseMode != t->getPause() ) t->togglePauseMode();
1263 strcpy( fgd_name, "");
1264 for( current_port = base_port; ( current_port <= end_port); current_port++) {
1265 fgd_send_com("0" , FGFS_host);
1266 sprintf( NewNetFGDLabel , "Scanning for deamon Port: %d", current_port);
1267 printf("FGD: searching %s\n", fgd_name);
1268 if ( strcmp( fgd_name, "") != 0 ) {
1269 sprintf( NewNetFGDLabel , "Found %s at Port: %d",
1270 fgd_name, current_port);
1271 fgd_port = current_port;
1272 current_port = end_port+1;
1275 current_port = end_port = base_port = fgd_port;
1277 NetFGDDialog_Cancel( NULL );
1281 void net_fgd_scan(puObject *cb)
1283 NewFGDPortLo = base_port;
1284 NewFGDPortHi = end_port;
1285 strcpy( NewFGDHost, fgd_host);
1286 NetFGDPortLoDialogInput->setValue( NewFGDPortLo );
1287 NetFGDPortHiDialogInput->setValue( NewFGDPortHi );
1288 NetFGDHostDialogInput->setValue( NewFGDHost );
1290 FG_PUSH_PUI_DIALOG( NetFGDDialog );
1294 static void NewNetFGDInit(void)
1296 // sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1297 // sprintf( NewNetId, "%s", fgd_callsign );
1298 int len = 170 - puGetStringWidth( puGetDefaultLabelFont(),
1299 NewNetFGDLabel ) / 2;
1301 NetFGDDialog = new puDialogBox (310, 30);
1303 NetFGDDialogFrame = new puFrame (0,0,320, 170);
1304 NetFGDDialogMessage = new puText (len, 140);
1305 NetFGDDialogMessage -> setLabel (NewNetFGDLabel);
1307 NetFGDPortLoDialogInput = new puInput (50, 70, 127, 100);
1308 NetFGDPortLoDialogInput -> setValue (NewFGDPortLo);
1309 NetFGDPortLoDialogInput -> acceptInput();
1311 NetFGDPortHiDialogInput = new puInput (199, 70, 275, 100);
1312 NetFGDPortHiDialogInput -> setValue (NewFGDPortHi);
1313 NetFGDPortHiDialogInput -> acceptInput();
1315 NetFGDHostDialogInput = new puInput (50, 100, 275, 130);
1316 NetFGDHostDialogInput -> setValue (NewFGDHost);
1317 NetFGDHostDialogInput -> acceptInput();
1319 NetFGDDialogScanButton = new puOneShot (130, 10, 200, 50);
1320 NetFGDDialogScanButton -> setLegend ("Scan");
1321 NetFGDDialogScanButton -> setCallback (NetFGDDialog_SCAN);
1322 NetFGDDialogScanButton -> makeReturnDefault(FALSE);
1324 NetFGDDialogOkButton = new puOneShot (50, 10, 120, 50);
1325 NetFGDDialogOkButton -> setLegend (gui_msg_OK);
1326 NetFGDDialogOkButton -> setCallback (NetFGDDialog_OK);
1327 NetFGDDialogOkButton -> makeReturnDefault(TRUE);
1329 NetFGDDialogCancelButton = new puOneShot (210, 10, 280, 50);
1330 NetFGDDialogCancelButton -> setLegend (gui_msg_CANCEL);
1331 NetFGDDialogCancelButton -> setCallback (NetFGDDialog_Cancel);
1334 FG_FINALIZE_PUI_DIALOG( NetFGDDialog );
1338 static void net_display_toggle( puObject *cb)
1340 net_hud_display = (net_hud_display) ? 0 : 1;
1341 printf("Toggle net_hud_display : %d\n", net_hud_display);
1348 /*************** End Networking **************/
1352 /* -----------------------------------------------------------------------
1354 ---------------------------------------------------------------------*/
1355 char *fileSubmenu [] = {
1356 "Exit", /* "Close", "---------", */
1357 #if defined( WIN32 ) && !defined( __CYGWIN__)
1361 /* "---------", "Save", */
1364 puCallback fileSubmenuCb [] = {
1365 MayBeGoodBye, /* hideMenuCb, NULL, */
1366 #if defined( WIN32 ) && !defined( __CYGWIN__)
1375 char *editSubmenu [] = {
1378 puCallback editSubmenuCb [] = {
1383 char *viewSubmenu [] = {
1384 /* "Cockpit View > ", "View >","------------", */ "Toggle Panel...", NULL
1386 puCallback viewSubmenuCb [] = {
1387 /* notCb, notCb, NULL, guiTogglePanel, */ NULL
1390 char *aircraftSubmenu [] = {
1391 "Autopilot", "Heading", "Altitude", "Navigation", "Airport",
1392 /* "Communication", */ NULL
1394 puCallback aircraftSubmenuCb [] = {
1395 fgAPAdjust, NewHeading, NewAltitude, fgLatLonFormatToggle, NewTgtAirport,
1399 char *environmentSubmenu [] = {
1400 "Airport", /* "Terrain", "Weather", */ NULL
1402 puCallback environmentSubmenuCb [] = {
1403 NewAirport, /* notCb, notCb, */ NULL
1407 char *optionsSubmenu [] = {
1408 "Preferences", "Realism & Reliablity...", NULL
1410 puCallback optionsSubmenuCb [] = {
1415 #ifdef FG_NETWORK_OLK
1416 char *networkSubmenu [] = {
1417 "Unregister from FGD ", /* "Send MSG to All", "Send MSG", "Show Pilots", */
1419 "Scan for Deamons", "Enter Callsign", /* "Display Netinfos", */
1420 "Toggle Display", NULL
1422 puCallback networkSubmenuCb [] = {
1423 /* notCb, notCb, notCb, notCb, */
1426 net_fgd_scan, NewCallSign,
1427 net_display_toggle, NULL
1431 char *helpSubmenu [] = {
1432 /* "About...", */ "Help", NULL
1434 puCallback helpSubmenuCb [] = {
1435 /* notCb, */ helpCb, NULL
1439 /* -------------------------------------------------------------------------
1441 _____________________________________________________________________*/
1446 char *mesa_win_state;
1450 puSetDefaultStyle ( PUSTYLE_SMALL_BEVELLED ); //PUSTYLE_DEFAULT
1451 puSetDefaultColourScheme (0.8, 0.8, 0.8, 0.4);
1453 // Initialize our GLOBAL GUI STRINGS
1454 gui_msg_OK = msg_OK; // "OK"
1455 gui_msg_NO = msg_NO; // "NO"
1456 gui_msg_YES = msg_YES; // "YES"
1457 gui_msg_CANCEL = msg_CANCEL; // "CANCEL"
1458 gui_msg_RESET = msg_RESET; // "RESET"
1460 // Next check home directory
1462 char* envp = ::getenv( "FG_FONTS" );
1463 if ( envp != NULL ) {
1464 fntpath.set( envp );
1466 fntpath.set( current_options.get_fg_root() );
1467 fntpath.append( "Fonts" );
1470 // Install our fast fonts
1471 fntpath.append( "typewriter.txf" );
1472 guiFntHandle = new fntTexFont ;
1473 guiFntHandle -> load ( (char *)fntpath.c_str() ) ;
1474 puFont GuiFont ( guiFntHandle, 15 ) ;
1475 puSetDefaultFonts( GuiFont, GuiFont ) ;
1476 guiFnt = puGetDefaultLabelFont();
1478 if ( current_options.get_mouse_pointer() == 0 ) {
1479 // no preference specified for mouse pointer, attempt to autodetect...
1480 // Determine if we need to render the cursor, or if the windowing
1481 // system will do it. First test if we are rendering with glide.
1482 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1483 // Test for the MESA_GLX_FX env variable
1484 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
1485 // test if we are fullscreen mesa/glide
1486 if ( (mesa_win_state[0] == 'f') ||
1487 (mesa_win_state[0] == 'F') ) {
1492 mouse_active = ~mouse_active;
1493 } else if ( current_options.get_mouse_pointer() == 1 ) {
1494 // don't show pointer
1495 } else if ( current_options.get_mouse_pointer() == 2 ) {
1496 // force showing pointer
1498 mouse_active = ~mouse_active;
1501 // MOUSE_VIEW mode stuff
1502 trackball(_quat0, 0.0, 0.0, 0.0, 0.0);
1504 build_rotmatrix(quat_mat, curquat);
1506 // Set up our Dialog Boxes
1507 ConfirmExitDialogInit();
1509 #ifdef FG_NETWORK_OLK
1515 // Make the menu bar
1516 mainMenuBar = new puMenuBar ();
1517 mainMenuBar -> add_submenu ("File", fileSubmenu, fileSubmenuCb);
1518 // mainMenuBar -> add_submenu ("Edit", editSubmenu, editSubmenuCb);
1519 mainMenuBar -> add_submenu ("View", viewSubmenu, viewSubmenuCb);
1520 mainMenuBar -> add_submenu ("Aircraft", aircraftSubmenu, aircraftSubmenuCb);
1521 mainMenuBar -> add_submenu ("Environment", environmentSubmenu, environmentSubmenuCb);
1522 // mainMenuBar -> add_submenu ("Options", optionsSubmenu, optionsSubmenuCb);
1523 #ifdef FG_NETWORK_OLK
1524 mainMenuBar -> add_submenu ("Network", networkSubmenu, networkSubmenuCb);
1526 mainMenuBar -> add_submenu ("Help", helpSubmenu, helpSubmenuCb);
1527 mainMenuBar-> close ();
1528 // Set up menu bar toggle
1537 * Implementation of a virtual trackball.
1538 * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
1539 * the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
1541 * Vector manip code:
1543 * Original code from:
1544 * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
1546 * Much mucking with by:
1549 #if defined(_WIN32) && !defined( __CYGWIN32__ )
1550 #pragma warning (disable:4244) /* disable bogus conversion warnings */
1554 //#include "trackball.h"
1557 * This size should really be based on the distance from the center of
1558 * rotation to the point on the object underneath the mouse. That
1559 * point would then track the mouse as closely as possible. This is a
1560 * simple example, though, so that is left as an Exercise for the
1563 #define TRACKBALLSIZE (0.8f)
1564 #define SQRT(x) sqrt(x)
1567 * Local function prototypes (not defined in trackball.h)
1569 static float tb_project_to_sphere(float, float, float);
1570 static void normalize_quat(float [4]);
1581 vset(float *v, float x, float y, float z)
1589 vsub(const float *src1, const float *src2, float *dst)
1591 dst[0] = src1[0] - src2[0];
1592 dst[1] = src1[1] - src2[1];
1593 dst[2] = src1[2] - src2[2];
1597 vcopy(const float *v1, float *v2)
1600 for (i = 0 ; i < 3 ; i++)
1605 vcross(const float *v1, const float *v2, float *cross)
1609 temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
1610 temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
1611 temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
1616 vlength(const float *v)
1618 float tmp = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
1623 vscale(float *v, float div)
1633 vscale(v,1.0/vlength(v));
1637 vdot(const float *v1, const float *v2)
1639 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
1643 vadd(const float *src1, const float *src2, float *dst)
1645 dst[0] = src1[0] + src2[0];
1646 dst[1] = src1[1] + src2[1];
1647 dst[2] = src1[2] + src2[2];
1651 * Given an axis and angle, compute quaternion.
1654 axis_to_quat(float a[3], float phi, float q[4])
1656 double sinphi2, cosphi2;
1657 double phi2 = phi/2.0;
1658 sinphi2 = sin(phi2);
1659 cosphi2 = cos(phi2);
1667 * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
1668 * if we are away from the center of the sphere.
1671 tb_project_to_sphere(float r, float x, float y)
1677 if (d < r * 0.70710678118654752440) { /* Inside sphere */
1680 } else { /* On hyperbola */
1681 t = r / 1.41421356237309504880;
1688 * Quaternions always obey: a^2 + b^2 + c^2 + d^2 = 1.0
1689 * If they don't add up to 1.0, dividing by their magnitued will
1692 * Note: See the following for more information on quaternions:
1694 * - Shoemake, K., Animating rotation with quaternion curves, Computer
1695 * Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
1696 * - Pletinckx, D., Quaternion calculus as a basic tool in computer
1697 * graphics, The Visual Computer 5, 2-13, 1989.
1700 normalize_quat(float q[4])
1705 tmp = q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3];
1706 mag = 1.0 / SQRT(tmp);
1707 for (i = 0; i < 4; i++)
1712 * Ok, simulate a track-ball. Project the points onto the virtual
1713 * trackball, then figure out the axis of rotation, which is the cross
1714 * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
1715 * Note: This is a deformed trackball-- is a trackball in the center,
1716 * but is deformed into a hyperbolic sheet of rotation away from the
1717 * center. This particular function was chosen after trying out
1718 * several variations.
1720 * It is assumed that the arguments to this routine are in the range
1724 trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
1726 float a[3]; /* Axis of rotation */
1727 float phi; /* how much to rotate about axis */
1728 float p1[3], p2[3], d[3];
1731 if (p1x == p2x && p1y == p2y) {
1739 * First, figure out z-coordinates for projection of P1 and P2 to
1742 vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
1743 vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
1746 * Now, we want the cross product of P1 and P2
1751 * Figure out how much to rotate around that axis.
1754 t = vlength(d) / (2.0*TRACKBALLSIZE);
1757 * Avoid problems with out-of-control values...
1759 if (t > 1.0) t = 1.0;
1760 if (t < -1.0) t = -1.0;
1761 phi = 2.0 * asin(t);
1763 axis_to_quat(a,phi,q);
1767 * Given two rotations, e1 and e2, expressed as quaternion rotations,
1768 * figure out the equivalent single rotation and stuff it into dest.
1770 * This routine also normalizes the result every RENORMCOUNT times it is
1771 * called, to keep error from creeping in.
1773 * NOTE: This routine is written so that q1 or q2 may be the same
1774 * as dest (or each other).
1777 #define RENORMCOUNT 97
1780 add_quats(float q1[4], float q2[4], float dest[4])
1783 float t1[4], t2[4], t3[4];
1787 printf("q1 = %f %f %f %f\n", q1[0], q1[1], q1[2], q1[3]);
1788 printf("q2 = %f %f %f %f\n", q2[0], q2[1], q2[2], q2[3]);
1800 tf[3] = q1[3] * q2[3] - vdot(q1,q2);
1803 printf("tf = %f %f %f %f\n", tf[0], tf[1], tf[2], tf[3]);
1811 if (++count > RENORMCOUNT) {
1813 normalize_quat(dest);
1818 * Build a rotation matrix, given a quaternion rotation.
1822 build_rotmatrix(float m[4][4], float q[4])
1824 //#define TRANSPOSED_QUAT
1825 #ifndef TRANSPOSED_QUAT
1826 m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
1827 m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
1828 m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
1831 m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
1832 m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
1833 m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
1836 m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
1837 m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
1838 m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
1845 #else // TRANSPOSED_QUAT
1846 m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
1847 m[0][1] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
1848 m[0][2] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
1851 m[1][0] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
1852 m[1][1] = 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
1853 m[1][2] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
1856 m[2][0] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
1857 m[2][1] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
1858 m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);