1 /**************************************************************************
4 * Written 1998 by Durk Talsma, started Juni, 1998. For the flight gear
7 * Additional mouse supported added by David Megginson, 1999.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 **************************************************************************/
31 #include <simgear/compiler.h>
33 #ifdef FG_MATH_EXCEPTION_CLASH
42 #include <simgear/xgl/xgl.h>
44 #if defined(FX) && defined(XMESA)
45 # include <GL/xmesa.h>
53 #include <simgear/constants.h>
54 #include <simgear/debug/logstream.hxx>
55 #include <simgear/misc/fgpath.hxx>
56 #include <simgear/screen/screen-dump.hxx>
58 #include <Include/general.hxx>
59 #include <Aircraft/aircraft.hxx>
60 #include <Airports/simple.hxx>
61 #include <Autopilot/auto_gui.hxx>
62 #include <Autopilot/newauto.hxx>
63 #include <Cockpit/panel.hxx>
64 #include <Controls/controls.hxx>
65 #include <FDM/flight.hxx>
66 #include <Main/options.hxx>
67 #include <Main/fg_init.hxx>
68 #include <Main/views.hxx>
70 #include <NetworkOLK/network.h>
72 #include <Time/fg_time.hxx>
74 #if defined( WIN32 ) && !defined( __CYGWIN__ )
75 # include <simgear/screen/win32-printer.h>
76 # include <simgear/screen/GlBitmaps.h>
81 * A virtual trackball implementation
82 * Written by Gavin Bell for Silicon Graphics, November 1988.
85 * Pass the x and y coordinates of the last and current positions of
86 * the mouse, scaled so they are from (-1.0 ... 1.0).
88 * The resulting rotation is returned as a quaternion rotation in the
92 trackball(float q[4], float p1x, float p1y, float p2x, float p2y);
95 * Given two quaternions, add them together to get a third quaternion.
96 * Adding quaternions to get a compound rotation is analagous to adding
97 * translations to get a compound translation. When incrementally
98 * adding rotations, the first argument here should be the new
100 * rotation, the second and third the total rotation (which will be
101 * over-written with the resulting new total rotation).
104 add_quats(float *q1, float *q2, float *dest);
107 * A useful function, builds a rotation matrix in Matrix based on
111 build_rotmatrix(float m[4][4], float q[4]);
114 * This function computes a quaternion based on an axis (defined by
115 * the given vector) and an angle about which to rotate. The angle is
116 * expressed in radians. The result is put into the third argument.
119 axis_to_quat(float a[3], float phi, float q[4]);
124 FG_USING_STD(string);
126 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
130 #if defined(WIN32) || defined(__CYGWIN32__)
131 #define WIN32_CURSOR_TWEAKS
132 #elif (GLUT_API_VERSION >= 4 || GLUT_XLIB_IMPLEMENTATION >= 9)
133 #define X_CURSOR_TWEAKS
136 // hack, should come from an include someplace
137 extern void fgInitVisuals( void );
138 extern void fgReshape( int width, int height );
139 extern void fgRenderFrame( void );
142 fntTexFont *guiFntHandle = 0;
144 static puMenuBar *mainMenuBar = 0;
145 //static puButton *hideMenuButton = 0;
147 static puDialogBox *dialogBox = 0;
148 static puFrame *dialogFrame = 0;
149 static puText *dialogBoxMessage = 0;
150 static puOneShot *dialogBoxOkButton = 0;
153 static puDialogBox *YNdialogBox = 0;
154 static puFrame *YNdialogFrame = 0;
155 static puText *YNdialogBoxMessage = 0;
156 static puOneShot *YNdialogBoxOkButton = 0;
157 static puOneShot *YNdialogBoxNoButton = 0;
159 static char msg_OK[] = "OK";
160 static char msg_NO[] = "NO";
161 static char msg_YES[] = "YES";
162 static char msg_CANCEL[] = "Cancel";
163 static char msg_RESET[] = "Reset";
165 char *gui_msg_OK; // "OK"
166 char *gui_msg_NO; // "NO"
167 char *gui_msg_YES; // "YES"
168 char *gui_msg_CANCEL; // "CANCEL"
169 char *gui_msg_RESET; // "RESET"
171 static char global_dialog_string[256];
173 // from autopilot.cxx
174 // extern void NewAltitude( puObject *cb );
175 // extern void NewHeading( puObject *cb );
176 // extern void fgAPAdjust( puObject * );
177 // extern void NewTgtAirport( puObject *cb );
178 // bool fgAPTerrainFollowEnabled( void );
179 // bool fgAPAltitudeEnabled( void );
180 // bool fgAPHeadingEnabled( void );
181 // bool fgAPWayPointEnabled( void );
182 // bool fgAPAutoThrottleEnabled( void );
185 extern void fgLatLonFormatToggle( puObject *);
187 /* --------------------------------------------------------------------
189 ---------------------------------------------------------------------*/
193 static int _savedX = 0;
194 static int _savedY = 0;
195 static int last_buttons = 0 ;
196 static int mouse_active = 0;
197 static int menu_on = 0;
198 static int mouse_joystick_control = 0;
200 //static time_t mouse_off_time;
201 //static int mouse_timed_out;
203 // to allow returning to previous view
204 // on second left click in MOUSE_VIEW mode
205 // This has file scope so that it can be reset
206 // if the little rodent is moved NHV
207 static int _mVtoggle;
209 // we break up the glutGetModifiers return mask
210 // once per loop and stash what we need in these
211 static int glut_active_shift;
212 static int glut_active_ctrl;
213 static int glut_active_alt;
215 static float lastquat[4];
216 static float curquat[4];
217 static float _quat0[4];
218 float quat_mat[4][4];
220 // uncomment this for view to exactly follow mouse in MOUSE_VIEW mode
221 // else smooth out the view panning to .01 radian per frame
222 // see view_offset smoothing mechanism in main.cxx
223 #define NO_SMOOTH_MOUSE_VIEW
225 // uncomment following to
226 #define RESET_VIEW_ON_LEAVING_MOUSE_VIEW
228 /* --------------------------------------------------------------------
229 Support for mouse as control yoke (david@megginson.com)
231 - right button toggles between pointer and yoke
232 - horizontal drag with no buttons moves ailerons
233 - vertical drag with no buttons moves elevators
234 - horizontal drag with left button moves brakes (left=on)
235 - vertical drag with left button moves throttle (up=more)
236 - horizontal drag with middle button moves rudder
237 - vertical drag with middle button moves trim
239 For the *_sensitivity variables, a lower number means more sensitive.
241 TODO: figure out how to keep pointer from leaving window in yoke mode.
242 TODO: add thresholds and null zones
243 TODO: sensitivity should be configurable at user option.
244 TODO: allow differential braking (this will be useful if FlightGear
245 ever supports tail-draggers like the DC-3)
246 ---------------------------------------------------------------------*/
254 MouseMode mouse_mode = MOUSE_POINTER;
256 static double aileron_sensitivity = 1.0/500.0;
257 static double elevator_sensitivity = 1.0/500.0;
258 static double brake_sensitivity = 1.0/250.0;
259 static double throttle_sensitivity = 1.0/250.0;
260 static double rudder_sensitivity = 1.0/500.0;
261 static double trim_sensitivity = 1.0/1000.0;
263 static inline void Quat0( void ) {
264 curquat[0] = _quat0[0];
265 curquat[1] = _quat0[1];
266 curquat[2] = _quat0[2];
267 curquat[3] = _quat0[3];
270 static inline int left_button( void ) {
271 return( last_buttons & (1 << GLUT_LEFT_BUTTON) );
274 static inline int middle_button( void ) {
275 return( last_buttons & (1 << GLUT_MIDDLE_BUTTON) );
278 static inline int right_button( void ) {
279 return( last_buttons & (1 << GLUT_RIGHT_BUTTON) );
282 static inline void TurnCursorOn( void )
285 #if defined(WIN32_CURSOR_TWEAKS)
286 switch (mouse_mode) {
288 glutSetCursor(GLUT_CURSOR_INHERIT);
291 glutSetCursor(GLUT_CURSOR_CROSSHAIR);
294 glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
298 #if defined(X_CURSOR_TWEAKS)
299 glutWarpPointer( current_view.get_winWidth()/2, current_view.get_winHeight()/2);
303 static inline void TurnCursorOff( void )
306 #if defined(WIN32_CURSOR_TWEAKS)
307 glutSetCursor(GLUT_CURSOR_NONE);
308 #elif defined(X_CURSOR_TWEAKS)
309 glutWarpPointer( current_view.get_winWidth(), current_view.get_winHeight());
313 void maybeToggleMouse( void )
315 #if defined(WIN32_CURSOR_TWEAKS)
316 static int first_time = ~0;
317 static int mouse_changed = 0;
321 mouse_changed = ~mouse_changed;
325 if( mouse_mode != MOUSE_POINTER )
327 if( mouse_changed ) {
328 mouse_changed = ~mouse_changed;
334 first_time = ~first_time;
335 #endif // #ifdef WIN32
338 // Call with FALSE to init and TRUE to restore
339 void BusyCursor( int restore )
341 static GLenum cursor = (GLenum) 0;
343 glutSetCursor(cursor);
345 cursor = (GLenum) glutGet( (GLenum) GLUT_WINDOW_CURSOR );
346 #if defined(WIN32_CURSOR_TWEAKS)
349 glutSetCursor( GLUT_CURSOR_WAIT );
353 int guiGetMouseButton(void)
358 void guiGetMouse(int *x, int *y)
364 void guiMotionFunc ( int x, int y )
366 int ww, wh, need_warp = 0;
369 // FGTime *t = FGTime::cur_time_params;
370 // if( mouse_timed_out ) {
371 // if( t->get_cur_time() > mouse_off_time ) {
372 // moused_timed_out = 0;
374 // glutPostRedisplay () ;
378 if (mouse_mode == MOUSE_POINTER) {
380 glutPostRedisplay () ;
382 if( x == _mX && y == _mY)
385 // reset left click MOUSE_VIEW toggle feature
388 ww = current_view.get_winWidth();
389 wh = current_view.get_winHeight();
391 switch (mouse_mode) {
393 if( !mouse_joystick_control ) {
394 mouse_joystick_control = 1;
395 current_options.set_control_mode( fgOPTIONS::FG_MOUSE );
397 if ( left_button() ) {
398 offset = (_mX - x) * brake_sensitivity;
399 controls.move_brake(FGControls::ALL_WHEELS, offset);
400 offset = (_mY - y) * throttle_sensitivity;
401 controls.move_throttle(FGControls::ALL_ENGINES, offset);
402 } else if ( right_button() ) {
403 if( ! current_autopilot->get_HeadingEnabled() ) {
404 offset = (x - _mX) * rudder_sensitivity;
405 controls.move_rudder(offset);
407 if( ! current_autopilot->get_AltitudeEnabled() ) {
408 offset = (_mY - y) * trim_sensitivity;
409 controls.move_elevator_trim(offset);
412 if( ! current_autopilot->get_HeadingEnabled() ) {
413 offset = (x - _mX) * aileron_sensitivity;
414 controls.move_aileron(offset);
416 if( ! current_autopilot->get_AltitudeEnabled() ) {
417 offset = (_mY - y) * elevator_sensitivity;
418 controls.move_elevator(offset);
422 // Keep the mouse in the window.
423 if (x < 5 || x > ww-5 || y < 5 || y > wh-5) {
432 #define CONTRAINED_MOUSE_VIEW_Y
433 #ifdef CONTRAINED_MOUSE_VIEW_Y
437 #endif // CONTRAINED_MOUSE_VIEW_Y
439 } else if( y >= wh-1) {
440 #ifdef CONTRAINED_MOUSE_VIEW_Y
444 #endif // CONTRAINED_MOUSE_VIEW_Y
447 // wrap MOUSE_VIEW mode cursor x position
451 } else if ( x >= ww-1 ) {
455 // try to get FG_PI movement in each half of screen
459 if( middle_button() ) {
461 (2.0f * _mX - W) / W,
462 0, //(H - 2.0f * y) / H, // 3
464 0 //(H - 2.0f * _mY) / H // 1
471 0, //(2.0f * _mX - W) / W, // 0
472 (H - 2.0f * y) / H, // 3
473 0, //(2.0f * x - W) / W, // 2
474 (H - 2.0f * _mY) / H // 1
477 add_quats(lastquat, curquat, curquat);
478 build_rotmatrix(quat_mat, curquat);
481 // this could be done in above quat
482 // but requires redoing view pipeline
483 offset = current_view.get_goal_view_offset();
484 offset += ((_mX - x) * FG_2PI / W );
485 while (offset < 0.0) {
488 while (offset > FG_2PI) {
491 current_view.set_goal_view_offset(offset);
492 #ifdef NO_SMOOTH_MOUSE_VIEW
493 current_view.set_view_offset(offset);
502 glutWarpPointer(x, y);
504 // Record the new mouse position.
510 void guiMouseFunc(int button, int updown, int x, int y)
514 // private MOUSE_VIEW state variables
515 // to allow alternate left clicks in MOUSE_VIEW mode
516 // to toggle between current offsets and straight ahead
518 static int _mVx, _mVy, _Vx, _Vy;
519 static float _quat[4];
520 static double _view_offset;
522 // general purpose variables
525 glutModifiers = glutGetModifiers();
526 glut_active_shift = glutModifiers & GLUT_ACTIVE_SHIFT;
527 glut_active_ctrl = glutModifiers & GLUT_ACTIVE_CTRL;
528 glut_active_alt = glutModifiers & GLUT_ACTIVE_ALT;
530 // Was the left button pressed?
531 if (updown == GLUT_DOWN ) {
532 if( button == GLUT_LEFT_BUTTON)
534 switch (mouse_mode) {
541 // resume previous view offsets
546 curquat[0] = _quat[0];
547 curquat[1] = _quat[1];
548 curquat[2] = _quat[2];
549 curquat[3] = _quat[3];
550 current_view.set_goal_view_offset(_view_offset);
551 #ifdef NO_SMOOTH_MOUSE_VIEW
552 current_view.set_view_offset(_view_offset);
560 _quat[0] = curquat[0];
561 _quat[1] = curquat[1];
562 _quat[2] = curquat[2];
563 _quat[3] = curquat[3];
564 x = current_view.get_winWidth()/2;
565 y = current_view.get_winHeight()/2;
567 _view_offset = current_view.get_goal_view_offset();
568 current_view.set_goal_view_offset(0.0);
569 #ifdef NO_SMOOTH_MOUSE_VIEW
570 current_view.set_view_offset(0.0);
573 glutWarpPointer( x , y);
574 build_rotmatrix(quat_mat, curquat);
575 _mVtoggle = ~_mVtoggle;
578 }else if ( button == GLUT_RIGHT_BUTTON) {
579 switch (mouse_mode) {
581 mouse_mode = MOUSE_YOKE;
582 mouse_joystick_control = 0;
585 // start with zero point in center of screen
586 _mX = current_view.get_winWidth()/2;
587 _mY = current_view.get_winHeight()/2;
589 // try to have the MOUSE_YOKE position
590 // reflect the current stick position
591 offset = controls.get_aileron();
592 x = _mX - (int)(offset * aileron_sensitivity);
593 offset = controls.get_elevator();
594 y = _mY - (int)(offset * elevator_sensitivity);
596 glutSetCursor(GLUT_CURSOR_CROSSHAIR);
597 FG_LOG( FG_INPUT, FG_INFO, "Mouse in yoke mode" );
601 mouse_mode = MOUSE_VIEW;
602 current_options.set_control_mode( fgOPTIONS::FG_JOYSTICK );
603 x = current_view.get_winWidth()/2;
604 y = current_view.get_winHeight()/2;
607 build_rotmatrix(quat_mat, curquat);
608 glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
609 FG_LOG( FG_INPUT, FG_INFO, "Mouse in view mode" );
613 mouse_mode = MOUSE_POINTER;
616 #ifdef RESET_VIEW_ON_LEAVING_MOUSE_VIEW
618 build_rotmatrix(quat_mat, curquat);
619 current_view.set_goal_view_offset(0.0);
620 #ifdef NO_SMOOTH_MOUSE_VIEW
621 current_view.set_view_offset(0.0);
623 #endif // RESET_VIEW_ON_LEAVING_MOUSE_VIEW
624 glutSetCursor(GLUT_CURSOR_INHERIT);
629 FG_LOG( FG_INPUT, FG_INFO, "Mouse in pointer mode" );
632 glutWarpPointer( x, y );
633 } // END RIGHT BUTTON
634 } // END UPDOWN == GLUT_DOWN
636 // Note which button is pressed.
637 if ( updown == GLUT_DOWN ) {
638 last_buttons |= ( 1 << button ) ;
640 last_buttons &= ~( 1 << button ) ;
643 // If we're in pointer mode, let PUI
644 // know what's going on.
645 if (mouse_mode == MOUSE_POINTER) {
646 if (!puMouse (button, updown, x,y)) {
647 current_panel->doMouseAction(button, updown, x, y);
651 // Register the new position (if it
652 // hasn't been registered already).
656 glutPostRedisplay ();
659 /* ================ General Purpose Functions ================ */
661 // Intercept the Escape Key
662 void ConfirmExitDialog(void)
664 FG_PUSH_PUI_DIALOG( YNdialogBox );
667 // General Purpose Message Box
668 void mkDialog (const char *txt)
670 strncpy(global_dialog_string, txt, 256);
671 dialogBoxMessage->setLabel(global_dialog_string);
672 FG_PUSH_PUI_DIALOG( dialogBox );
675 // Repair any damage done to the Panel by other Gui Items
676 void guiFixPanel( void )
680 if ( current_options.get_panel_status() ) {
681 // FGView *v = ¤t_view;
682 FGTime *t = FGTime::cur_time_params;
684 if( (toggle_pause = !t->getPause()) )
685 t->togglePauseMode();
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 ( current_options.get_network_olk() ) {
745 if ( net_is_registered == 0 ) fgd_send_com( "8", FGFS_host);
756 void goAwayCb (puObject *me)
758 FG_POP_PUI_DIALOG( dialogBox );
761 void mkDialogInit (void)
763 // printf("mkDialogInit\n");
764 int x = (current_options.get_xsize()/2 - 400/2);
765 int y = (current_options.get_ysize()/2 - 100/2);
766 dialogBox = new puDialogBox (x, y); // 150, 50
768 dialogFrame = new puFrame (0,0,400,100);
769 dialogBoxMessage = new puText (10, 70);
770 dialogBoxMessage -> setLabel ("");
771 dialogBoxOkButton = new puOneShot (180, 10, 240, 50);
772 dialogBoxOkButton -> setLegend (gui_msg_OK);
773 dialogBoxOkButton -> makeReturnDefault (TRUE );
774 dialogBoxOkButton -> setCallback (goAwayCb);
776 FG_FINALIZE_PUI_DIALOG( dialogBox );
779 void MayBeGoodBye(puObject *)
784 void goAwayYesNoCb(puObject *me)
786 FG_POP_PUI_DIALOG( YNdialogBox);
789 void ConfirmExitDialogInit(void)
791 char msg[] = "Really Quit";
794 // printf("ConfirmExitDialogInit\n");
795 int len = 200 - puGetStringWidth( puGetDefaultLabelFont(), msg )/2;
797 int x = (current_options.get_xsize()/2 - 400/2);
798 int y = (current_options.get_ysize()/2 - 100/2);
800 YNdialogBox = new puDialogBox (x, y); // 150, 50
801 // YNdialogBox = new puDialogBox (150, 50);
803 YNdialogFrame = new puFrame (0,0,400, 100);
805 YNdialogBoxMessage = new puText (len, 70);
806 YNdialogBoxMessage -> setDefaultValue (msg);
807 YNdialogBoxMessage -> getDefaultValue (&s);
808 YNdialogBoxMessage -> setLabel (s);
810 YNdialogBoxOkButton = new puOneShot (100, 10, 160, 50);
811 YNdialogBoxOkButton -> setLegend (gui_msg_OK);
812 YNdialogBoxOkButton -> makeReturnDefault (TRUE );
813 YNdialogBoxOkButton -> setCallback (goodBye);
815 YNdialogBoxNoButton = new puOneShot (240, 10, 300, 50);
816 YNdialogBoxNoButton -> setLegend (gui_msg_NO);
817 YNdialogBoxNoButton -> setCallback (goAwayYesNoCb);
819 FG_FINALIZE_PUI_DIALOG( YNdialogBox );
822 void notCb (puObject *)
824 mkDialog ("This function isn't implemented yet");
827 void helpCb (puObject *)
831 #if defined(FX) && !defined(WIN32)
832 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
833 if ( global_fullscreen ) {
834 global_fullscreen = false;
835 XMesaSetFXmode( XMESA_FX_WINDOW );
841 string url = "http://www.flightgear.org/Docs/InstallGuide/getstart.html";
843 if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
844 command = "netscape -remote \"openURL(" + url + ")\" &";
846 command = "netscape " + url + " &";
849 command = "webrun.bat";
852 system( command.c_str() );
853 //string text = "Help started in netscape window.";
855 //mkDialog (text.c_str());
856 mkDialog ("Help started in netscape window.");
859 #if defined( WIN32 ) && !defined( __CYGWIN__)
861 static void rotateView( double roll, double pitch, double yaw )
866 static GlBitmap *b1 = NULL;
867 extern FGInterface cur_view_fdm;
868 GLubyte *hiResScreenCapture( int multiplier )
870 float oldfov = current_options.get_fov();
871 float fov = oldfov / multiplier;
872 FGView *v = ¤t_view;
873 current_options.set_fov(fov);
874 v->force_update_fov_math();
876 int cur_width = current_view.get_winWidth( );
877 int cur_height = current_view.get_winHeight( );
878 if (b1) delete( b1 );
879 // New empty (mostly) bitmap
880 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
882 for ( y = 0; y < multiplier; y++ )
884 for ( x = 0; x < multiplier; x++ )
886 fgReshape( cur_width, cur_height );
888 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
892 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
895 current_view.UpdateViewParams(cur_view_fdm);
896 current_options.set_fov(oldfov);
897 v->force_update_fov_math();
898 return b1->getBitmap();
903 #if defined( WIN32 ) && !defined( __CYGWIN__)
904 // win32 print screen function
905 void printScreen ( puObject *obj ) {
906 bool show_pu_cursor = false;
908 if ( !puCursorIsHidden() ) {
909 show_pu_cursor = true;
915 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
916 int cur_width = current_view.get_winWidth( );
917 int cur_height = current_view.get_winHeight( );
918 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
919 p.End( hiResScreenCapture(3) );
922 mainMenuBar->reveal();
925 if ( show_pu_cursor ) {
930 #endif // #ifdef WIN32
933 void dumpSnapShot ( puObject *obj ) {
938 // do a screen snap shot
939 void fgDumpSnapShot () {
940 bool show_pu_cursor = false;
944 if ( !puCursorIsHidden() ) {
945 show_pu_cursor = true;
950 fgReshape( current_options.get_xsize(), current_options.get_ysize() );
952 // we need two render frames here to clear the menu and cursor
953 // ... not sure why but doing an extra fgFenderFrame() shoulnd't
958 my_glDumpWindow( "fgfs-screen.ppm",
959 current_options.get_xsize(),
960 current_options.get_ysize() );
962 mkDialog ("Snap shot saved to fgfs-screen.ppm");
964 if ( show_pu_cursor ) {
970 mainMenuBar->reveal();
976 /// The beginnings of teleportation :-)
977 // Needs cleaning up but works
978 // These statics should disapear when this is a class
979 static puDialogBox *AptDialog = 0;
980 static puFrame *AptDialogFrame = 0;
981 static puText *AptDialogMessage = 0;
982 static puInput *AptDialogInput = 0;
984 static char NewAirportId[16];
985 static char NewAirportLabel[] = "Enter New Airport ID";
987 static puOneShot *AptDialogOkButton = 0;
988 static puOneShot *AptDialogCancelButton = 0;
989 static puOneShot *AptDialogResetButton = 0;
991 void AptDialog_Cancel(puObject *)
993 FG_POP_PUI_DIALOG( AptDialog );
996 void AptDialog_OK (puObject *)
998 FGPath path( current_options.get_fg_root() );
999 path.append( "Airports" );
1000 path.append( "simple.gdbm" );
1001 FGAirports airports( path.c_str() );
1004 FGTime *t = FGTime::cur_time_params;
1005 int PauseMode = t->getPause();
1007 t->togglePauseMode();
1010 AptDialogInput->getValue(&s);
1013 cout << "AptDialog_OK " << AptId << " " << AptId.length() << endl;
1015 AptDialog_Cancel( NULL );
1017 if ( AptId.length() ) {
1018 // set initial position from airport id
1019 FG_LOG( FG_GENERAL, FG_INFO,
1020 "Attempting to set starting position from airport code "
1023 if ( airports.search( AptId, &a ) )
1025 current_options.set_airport_id( AptId.c_str() );
1027 fgReInitSubsystems();
1030 AptId += " not in database.";
1031 mkDialog(AptId.c_str());
1034 if( PauseMode != t->getPause() )
1035 t->togglePauseMode();
1038 void AptDialog_Reset(puObject *)
1040 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
1041 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1042 AptDialogInput->setValue ( NewAirportId );
1043 AptDialogInput->setCursor( 0 ) ;
1046 void NewAirport(puObject *cb)
1048 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
1049 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1050 // cout << "NewAirport " << NewAirportId << endl;
1051 AptDialogInput->setValue( NewAirportId );
1053 FG_PUSH_PUI_DIALOG( AptDialog );
1056 static void NewAirportInit(void)
1058 sprintf( NewAirportId, "%s", current_options.get_airport_id().c_str() );
1059 int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
1060 NewAirportLabel ) / 2;
1062 AptDialog = new puDialogBox (150, 50);
1064 AptDialogFrame = new puFrame (0,0,350, 150);
1065 AptDialogMessage = new puText (len, 110);
1066 AptDialogMessage -> setLabel (NewAirportLabel);
1068 AptDialogInput = new puInput (50, 70, 300, 100);
1069 AptDialogInput -> setValue (NewAirportId);
1070 AptDialogInput -> acceptInput();
1072 AptDialogOkButton = new puOneShot (50, 10, 110, 50);
1073 AptDialogOkButton -> setLegend (gui_msg_OK);
1074 AptDialogOkButton -> setCallback (AptDialog_OK);
1075 AptDialogOkButton -> makeReturnDefault(TRUE);
1077 AptDialogCancelButton = new puOneShot (140, 10, 210, 50);
1078 AptDialogCancelButton -> setLegend (gui_msg_CANCEL);
1079 AptDialogCancelButton -> setCallback (AptDialog_Cancel);
1081 AptDialogResetButton = new puOneShot (240, 10, 300, 50);
1082 AptDialogResetButton -> setLegend (gui_msg_RESET);
1083 AptDialogResetButton -> setCallback (AptDialog_Reset);
1085 cout << "NewAirportInit " << NewAirportId << endl;
1086 FG_FINALIZE_PUI_DIALOG( AptDialog );
1089 #ifdef FG_NETWORK_OLK
1091 /// The beginnings of networking :-)
1092 // Needs cleaning up but works
1093 // These statics should disapear when this is a class
1094 static puDialogBox *NetIdDialog = 0;
1095 static puFrame *NetIdDialogFrame = 0;
1096 static puText *NetIdDialogMessage = 0;
1097 static puInput *NetIdDialogInput = 0;
1099 static char NewNetId[16];
1100 static char NewNetIdLabel[] = "Enter New Callsign";
1101 extern char *fgd_callsign;
1103 static puOneShot *NetIdDialogOkButton = 0;
1104 static puOneShot *NetIdDialogCancelButton = 0;
1106 void NetIdDialog_Cancel(puObject *)
1108 FG_POP_PUI_DIALOG( NetIdDialog );
1111 void NetIdDialog_OK (puObject *)
1115 FGTime *t = FGTime::cur_time_params;
1116 int PauseMode = t->getPause();
1118 t->togglePauseMode();
1120 The following needs some cleanup because
1121 "string options.NetId" and "char *net_callsign"
1123 NetIdDialogInput->getValue(&net_callsign);
1124 NetId = net_callsign;
1126 NetIdDialog_Cancel( NULL );
1127 current_options.set_net_id( NetId.c_str() );
1128 strcpy( fgd_callsign, net_callsign);
1129 // strcpy( fgd_callsign, current_options.get_net_id().c_str());
1130 /* Entering a callsign indicates : user wants Net HUD Info */
1131 net_hud_display = 1;
1133 if( PauseMode != t->getPause() )
1134 t->togglePauseMode();
1137 void NewCallSign(puObject *cb)
1139 sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1140 // sprintf( NewNetId, "%s", fgd_callsign );
1141 NetIdDialogInput->setValue( NewNetId );
1143 FG_PUSH_PUI_DIALOG( NetIdDialog );
1146 static void NewNetIdInit(void)
1148 sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1149 // sprintf( NewNetId, "%s", fgd_callsign );
1150 int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
1151 NewNetIdLabel ) / 2;
1153 NetIdDialog = new puDialogBox (150, 50);
1155 NetIdDialogFrame = new puFrame (0,0,350, 150);
1156 NetIdDialogMessage = new puText (len, 110);
1157 NetIdDialogMessage -> setLabel (NewNetIdLabel);
1159 NetIdDialogInput = new puInput (50, 70, 300, 100);
1160 NetIdDialogInput -> setValue (NewNetId);
1161 NetIdDialogInput -> acceptInput();
1163 NetIdDialogOkButton = new puOneShot (50, 10, 110, 50);
1164 NetIdDialogOkButton -> setLegend (gui_msg_OK);
1165 NetIdDialogOkButton -> setCallback (NetIdDialog_OK);
1166 NetIdDialogOkButton -> makeReturnDefault(TRUE);
1168 NetIdDialogCancelButton = new puOneShot (240, 10, 300, 50);
1169 NetIdDialogCancelButton -> setLegend (gui_msg_CANCEL);
1170 NetIdDialogCancelButton -> setCallback (NetIdDialog_Cancel);
1173 FG_FINALIZE_PUI_DIALOG( NetIdDialog );
1176 static void net_display_toggle( puObject *cb)
1178 net_hud_display = (net_hud_display) ? 0 : 1;
1179 printf("Toggle net_hud_display : %d\n", net_hud_display);
1182 static void net_register( puObject *cb)
1184 fgd_send_com( "1", FGFS_host );
1185 net_is_registered = 0;
1186 printf("Registering to deamon\n");
1189 static void net_unregister( puObject *cb)
1191 fgd_send_com( "8", FGFS_host );
1192 net_is_registered = -1;
1193 printf("Unregistering from deamon\n");
1197 /*************** Deamon communication **********/
1199 // These statics should disapear when this is a class
1200 static puDialogBox *NetFGDDialog = 0;
1201 static puFrame *NetFGDDialogFrame = 0;
1202 static puText *NetFGDDialogMessage = 0;
1203 //static puInput *NetFGDDialogInput = 0;
1205 //static char NewNetId[16];
1206 static char NewNetFGDLabel[] = "Scan for deamon ";
1207 static char NewFGDHost[64] = "olk.mcp.de";
1208 static int NewFGDPortLo = 10000;
1209 static int NewFGDPortHi = 10001;
1211 //extern char *fgd_callsign;
1212 extern u_short base_port, end_port;
1213 extern int fgd_ip, verbose, current_port;
1214 extern char *fgd_host;
1217 static puOneShot *NetFGDDialogOkButton = 0;
1218 static puOneShot *NetFGDDialogCancelButton = 0;
1219 static puOneShot *NetFGDDialogScanButton = 0;
1221 static puInput *NetFGDHostDialogInput = 0;
1222 static puInput *NetFGDPortLoDialogInput = 0;
1223 static puInput *NetFGDPortHiDialogInput = 0;
1225 void NetFGDDialog_Cancel(puObject *)
1227 FG_POP_PUI_DIALOG( NetFGDDialog );
1230 void NetFGDDialog_OK (puObject *)
1234 FGTime *t = FGTime::cur_time_params;
1235 int PauseMode = t->getPause();
1236 if(!PauseMode) t->togglePauseMode();
1237 NetFGDHostDialogInput->getValue( &NetFGD );
1238 strcpy( fgd_host, NetFGD);
1239 NetFGDPortLoDialogInput->getValue( (int *) &base_port );
1240 NetFGDPortHiDialogInput->getValue( (int *) &end_port );
1241 NetFGDDialog_Cancel( NULL );
1242 if( PauseMode != t->getPause() )
1243 t->togglePauseMode();
1246 void NetFGDDialog_SCAN (puObject *)
1251 FGTime *t = FGTime::cur_time_params;
1252 int PauseMode = t->getPause();
1253 if(!PauseMode) t->togglePauseMode();
1254 // printf("Vor getvalue %s\n");
1255 NetFGDHostDialogInput->getValue( &NetFGD );
1256 // printf("Vor strcpy %s\n", (char *) NetFGD);
1257 strcpy( fgd_host, NetFGD);
1258 NetFGDPortLoDialogInput->getValue( (int *) &base_port );
1259 NetFGDPortHiDialogInput->getValue( (int *) &end_port );
1260 printf("FGD: %s Port-Start: %d Port-End: %d\n", fgd_host,
1261 base_port, end_port);
1262 net_resolv_fgd(fgd_host);
1263 printf("Resolve : %d\n", net_r);
1264 if( PauseMode != t->getPause() ) t->togglePauseMode();
1267 strcpy( fgd_name, "");
1268 for( current_port = base_port; ( current_port <= end_port); current_port++) {
1269 fgd_send_com("0" , FGFS_host);
1270 sprintf( NewNetFGDLabel , "Scanning for deamon Port: %d", current_port);
1271 printf("FGD: searching %s\n", fgd_name);
1272 if ( strcmp( fgd_name, "") != 0 ) {
1273 sprintf( NewNetFGDLabel , "Found %s at Port: %d",
1274 fgd_name, current_port);
1275 fgd_port = current_port;
1276 current_port = end_port+1;
1279 current_port = end_port = base_port = fgd_port;
1281 NetFGDDialog_Cancel( NULL );
1285 void net_fgd_scan(puObject *cb)
1287 NewFGDPortLo = base_port;
1288 NewFGDPortHi = end_port;
1289 strcpy( NewFGDHost, fgd_host);
1290 NetFGDPortLoDialogInput->setValue( NewFGDPortLo );
1291 NetFGDPortHiDialogInput->setValue( NewFGDPortHi );
1292 NetFGDHostDialogInput->setValue( NewFGDHost );
1294 FG_PUSH_PUI_DIALOG( NetFGDDialog );
1298 static void NewNetFGDInit(void)
1300 // sprintf( NewNetId, "%s", current_options.get_net_id().c_str() );
1301 // sprintf( NewNetId, "%s", fgd_callsign );
1302 int len = 170 - puGetStringWidth( puGetDefaultLabelFont(),
1303 NewNetFGDLabel ) / 2;
1305 NetFGDDialog = new puDialogBox (310, 30);
1307 NetFGDDialogFrame = new puFrame (0,0,320, 170);
1308 NetFGDDialogMessage = new puText (len, 140);
1309 NetFGDDialogMessage -> setLabel (NewNetFGDLabel);
1311 NetFGDPortLoDialogInput = new puInput (50, 70, 127, 100);
1312 NetFGDPortLoDialogInput -> setValue (NewFGDPortLo);
1313 NetFGDPortLoDialogInput -> acceptInput();
1315 NetFGDPortHiDialogInput = new puInput (199, 70, 275, 100);
1316 NetFGDPortHiDialogInput -> setValue (NewFGDPortHi);
1317 NetFGDPortHiDialogInput -> acceptInput();
1319 NetFGDHostDialogInput = new puInput (50, 100, 275, 130);
1320 NetFGDHostDialogInput -> setValue (NewFGDHost);
1321 NetFGDHostDialogInput -> acceptInput();
1323 NetFGDDialogScanButton = new puOneShot (130, 10, 200, 50);
1324 NetFGDDialogScanButton -> setLegend ("Scan");
1325 NetFGDDialogScanButton -> setCallback (NetFGDDialog_SCAN);
1326 NetFGDDialogScanButton -> makeReturnDefault(FALSE);
1328 NetFGDDialogOkButton = new puOneShot (50, 10, 120, 50);
1329 NetFGDDialogOkButton -> setLegend (gui_msg_OK);
1330 NetFGDDialogOkButton -> setCallback (NetFGDDialog_OK);
1331 NetFGDDialogOkButton -> makeReturnDefault(TRUE);
1333 NetFGDDialogCancelButton = new puOneShot (210, 10, 280, 50);
1334 NetFGDDialogCancelButton -> setLegend (gui_msg_CANCEL);
1335 NetFGDDialogCancelButton -> setCallback (NetFGDDialog_Cancel);
1338 FG_FINALIZE_PUI_DIALOG( NetFGDDialog );
1342 static void net_display_toggle( puObject *cb)
1344 net_hud_display = (net_hud_display) ? 0 : 1;
1345 printf("Toggle net_hud_display : %d\n", net_hud_display);
1352 /*************** End Networking **************/
1356 /* -----------------------------------------------------------------------
1358 ---------------------------------------------------------------------*/
1359 char *fileSubmenu [] = {
1360 "Exit", /* "Close", "---------", */
1361 #if defined( WIN32 ) && !defined( __CYGWIN__)
1365 /* "---------", "Save", */
1368 puCallback fileSubmenuCb [] = {
1369 MayBeGoodBye, /* hideMenuCb, NULL, */
1370 #if defined( WIN32 ) && !defined( __CYGWIN__)
1379 char *editSubmenu [] = {
1382 puCallback editSubmenuCb [] = {
1387 extern void fgHUDalphaAdjust( puObject * );
1388 char *viewSubmenu [] = {
1390 /* "Cockpit View > ", "View >","------------", */
1391 "Toggle Panel...", NULL
1393 puCallback viewSubmenuCb [] = {
1395 /* notCb, notCb, NULL, */
1396 guiTogglePanel, NULL
1399 char *aircraftSubmenu [] = {
1400 "Autopilot", "Heading", "Altitude", "Navigation", "Airport",
1401 /* "Communication", */ NULL
1403 puCallback aircraftSubmenuCb [] = {
1404 fgAPAdjust, NewHeading, NewAltitude, fgLatLonFormatToggle, NewTgtAirport,
1408 char *environmentSubmenu [] = {
1409 "Airport", /* "Terrain", "Weather", */ NULL
1411 puCallback environmentSubmenuCb [] = {
1412 NewAirport, /* notCb, notCb, */ NULL
1416 char *optionsSubmenu [] = {
1417 "Preferences", "Realism & Reliablity...", NULL
1419 puCallback optionsSubmenuCb [] = {
1424 #ifdef FG_NETWORK_OLK
1425 char *networkSubmenu [] = {
1426 "Unregister from FGD ", /* "Send MSG to All", "Send MSG", "Show Pilots", */
1428 "Scan for Deamons", "Enter Callsign", /* "Display Netinfos", */
1429 "Toggle Display", NULL
1431 puCallback networkSubmenuCb [] = {
1432 /* notCb, notCb, notCb, notCb, */
1435 net_fgd_scan, NewCallSign,
1436 net_display_toggle, NULL
1440 char *helpSubmenu [] = {
1441 /* "About...", */ "Help", NULL
1443 puCallback helpSubmenuCb [] = {
1444 /* notCb, */ helpCb, NULL
1448 /* -------------------------------------------------------------------------
1450 _____________________________________________________________________*/
1455 char *mesa_win_state;
1459 puSetDefaultStyle ( PUSTYLE_SMALL_BEVELLED ); //PUSTYLE_DEFAULT
1460 puSetDefaultColourScheme (0.8, 0.8, 0.8, 0.4);
1462 // Initialize our GLOBAL GUI STRINGS
1463 gui_msg_OK = msg_OK; // "OK"
1464 gui_msg_NO = msg_NO; // "NO"
1465 gui_msg_YES = msg_YES; // "YES"
1466 gui_msg_CANCEL = msg_CANCEL; // "CANCEL"
1467 gui_msg_RESET = msg_RESET; // "RESET"
1469 // Next check home directory
1471 char* envp = ::getenv( "FG_FONTS" );
1472 if ( envp != NULL ) {
1473 fntpath.set( envp );
1475 fntpath.set( current_options.get_fg_root() );
1476 fntpath.append( "Fonts" );
1479 // Install our fast fonts
1480 fntpath.append( "typewriter.txf" );
1481 guiFntHandle = new fntTexFont ;
1482 guiFntHandle -> load ( (char *)fntpath.c_str() ) ;
1483 puFont GuiFont ( guiFntHandle, 15 ) ;
1484 puSetDefaultFonts( GuiFont, GuiFont ) ;
1485 guiFnt = puGetDefaultLabelFont();
1487 if ( current_options.get_mouse_pointer() == 0 ) {
1488 // no preference specified for mouse pointer, attempt to autodetect...
1489 // Determine if we need to render the cursor, or if the windowing
1490 // system will do it. First test if we are rendering with glide.
1491 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1492 // Test for the MESA_GLX_FX env variable
1493 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
1494 // test if we are fullscreen mesa/glide
1495 if ( (mesa_win_state[0] == 'f') ||
1496 (mesa_win_state[0] == 'F') ) {
1501 mouse_active = ~mouse_active;
1502 } else if ( current_options.get_mouse_pointer() == 1 ) {
1503 // don't show pointer
1504 } else if ( current_options.get_mouse_pointer() == 2 ) {
1505 // force showing pointer
1507 mouse_active = ~mouse_active;
1510 // MOUSE_VIEW mode stuff
1511 trackball(_quat0, 0.0, 0.0, 0.0, 0.0);
1513 build_rotmatrix(quat_mat, curquat);
1515 // Set up our Dialog Boxes
1516 ConfirmExitDialogInit();
1518 #ifdef FG_NETWORK_OLK
1524 // Make the menu bar
1525 mainMenuBar = new puMenuBar ();
1526 mainMenuBar -> add_submenu ("File", fileSubmenu, fileSubmenuCb);
1527 // mainMenuBar -> add_submenu ("Edit", editSubmenu, editSubmenuCb);
1528 mainMenuBar -> add_submenu ("View", viewSubmenu, viewSubmenuCb);
1529 mainMenuBar -> add_submenu ("Aircraft", aircraftSubmenu, aircraftSubmenuCb);
1530 mainMenuBar -> add_submenu ("Environment", environmentSubmenu, environmentSubmenuCb);
1531 // mainMenuBar -> add_submenu ("Options", optionsSubmenu, optionsSubmenuCb);
1532 #ifdef FG_NETWORK_OLK
1533 mainMenuBar -> add_submenu ("Network", networkSubmenu, networkSubmenuCb);
1535 mainMenuBar -> add_submenu ("Help", helpSubmenu, helpSubmenuCb);
1536 mainMenuBar-> close ();
1537 // Set up menu bar toggle
1546 * Implementation of a virtual trackball.
1547 * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
1548 * the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
1550 * Vector manip code:
1552 * Original code from:
1553 * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
1555 * Much mucking with by:
1558 #if defined(_WIN32) && !defined( __CYGWIN32__ )
1559 #pragma warning (disable:4244) /* disable bogus conversion warnings */
1563 //#include "trackball.h"
1566 * This size should really be based on the distance from the center of
1567 * rotation to the point on the object underneath the mouse. That
1568 * point would then track the mouse as closely as possible. This is a
1569 * simple example, though, so that is left as an Exercise for the
1572 #define TRACKBALLSIZE (0.8f)
1573 #define SQRT(x) sqrt(x)
1576 * Local function prototypes (not defined in trackball.h)
1578 static float tb_project_to_sphere(float, float, float);
1579 static void normalize_quat(float [4]);
1590 vset(float *v, float x, float y, float z)
1598 vsub(const float *src1, const float *src2, float *dst)
1600 dst[0] = src1[0] - src2[0];
1601 dst[1] = src1[1] - src2[1];
1602 dst[2] = src1[2] - src2[2];
1606 vcopy(const float *v1, float *v2)
1609 for (i = 0 ; i < 3 ; i++)
1614 vcross(const float *v1, const float *v2, float *cross)
1618 temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
1619 temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
1620 temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
1625 vlength(const float *v)
1627 float tmp = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
1632 vscale(float *v, float div)
1642 vscale(v,1.0/vlength(v));
1646 vdot(const float *v1, const float *v2)
1648 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
1652 vadd(const float *src1, const float *src2, float *dst)
1654 dst[0] = src1[0] + src2[0];
1655 dst[1] = src1[1] + src2[1];
1656 dst[2] = src1[2] + src2[2];
1660 * Given an axis and angle, compute quaternion.
1663 axis_to_quat(float a[3], float phi, float q[4])
1665 double sinphi2, cosphi2;
1666 double phi2 = phi/2.0;
1667 sinphi2 = sin(phi2);
1668 cosphi2 = cos(phi2);
1676 * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
1677 * if we are away from the center of the sphere.
1680 tb_project_to_sphere(float r, float x, float y)
1686 if (d < r * 0.70710678118654752440) { /* Inside sphere */
1689 } else { /* On hyperbola */
1690 t = r / 1.41421356237309504880;
1697 * Quaternions always obey: a^2 + b^2 + c^2 + d^2 = 1.0
1698 * If they don't add up to 1.0, dividing by their magnitued will
1701 * Note: See the following for more information on quaternions:
1703 * - Shoemake, K., Animating rotation with quaternion curves, Computer
1704 * Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
1705 * - Pletinckx, D., Quaternion calculus as a basic tool in computer
1706 * graphics, The Visual Computer 5, 2-13, 1989.
1709 normalize_quat(float q[4])
1714 tmp = q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3];
1715 mag = 1.0 / SQRT(tmp);
1716 for (i = 0; i < 4; i++)
1721 * Ok, simulate a track-ball. Project the points onto the virtual
1722 * trackball, then figure out the axis of rotation, which is the cross
1723 * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
1724 * Note: This is a deformed trackball-- is a trackball in the center,
1725 * but is deformed into a hyperbolic sheet of rotation away from the
1726 * center. This particular function was chosen after trying out
1727 * several variations.
1729 * It is assumed that the arguments to this routine are in the range
1733 trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
1735 float a[3]; /* Axis of rotation */
1736 float phi; /* how much to rotate about axis */
1737 float p1[3], p2[3], d[3];
1740 if (p1x == p2x && p1y == p2y) {
1748 * First, figure out z-coordinates for projection of P1 and P2 to
1751 vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
1752 vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
1755 * Now, we want the cross product of P1 and P2
1760 * Figure out how much to rotate around that axis.
1763 t = vlength(d) / (2.0*TRACKBALLSIZE);
1766 * Avoid problems with out-of-control values...
1768 if (t > 1.0) t = 1.0;
1769 if (t < -1.0) t = -1.0;
1770 phi = 2.0 * asin(t);
1772 axis_to_quat(a,phi,q);
1776 * Given two rotations, e1 and e2, expressed as quaternion rotations,
1777 * figure out the equivalent single rotation and stuff it into dest.
1779 * This routine also normalizes the result every RENORMCOUNT times it is
1780 * called, to keep error from creeping in.
1782 * NOTE: This routine is written so that q1 or q2 may be the same
1783 * as dest (or each other).
1786 #define RENORMCOUNT 97
1789 add_quats(float q1[4], float q2[4], float dest[4])
1792 float t1[4], t2[4], t3[4];
1796 printf("q1 = %f %f %f %f\n", q1[0], q1[1], q1[2], q1[3]);
1797 printf("q2 = %f %f %f %f\n", q2[0], q2[1], q2[2], q2[3]);
1809 tf[3] = q1[3] * q2[3] - vdot(q1,q2);
1812 printf("tf = %f %f %f %f\n", tf[0], tf[1], tf[2], tf[3]);
1820 if (++count > RENORMCOUNT) {
1822 normalize_quat(dest);
1827 * Build a rotation matrix, given a quaternion rotation.
1831 build_rotmatrix(float m[4][4], float q[4])
1833 //#define TRANSPOSED_QUAT
1834 #ifndef TRANSPOSED_QUAT
1835 m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
1836 m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
1837 m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
1840 m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
1841 m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
1842 m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
1845 m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
1846 m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
1847 m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
1854 #else // TRANSPOSED_QUAT
1855 m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
1856 m[0][1] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
1857 m[0][2] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
1860 m[1][0] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
1861 m[1][1] = 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
1862 m[1][2] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
1865 m[2][0] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
1866 m[2][1] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
1867 m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);