1 /**************************************************************************
4 * Written 1998 by Durk Talsma, started Juni, 1998. For the flight gear
7 * Additional mouse supported added by David Megginson, 1999.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 **************************************************************************/
31 #include <simgear/compiler.h>
33 #ifdef FG_MATH_EXCEPTION_CLASH
42 #include <simgear/xgl/xgl.h>
44 #if defined(FX) && defined(XMESA)
45 # include <GL/xmesa.h>
54 #include <simgear/constants.h>
55 #include <simgear/debug/logstream.hxx>
56 #include <simgear/misc/fgpath.hxx>
57 #include <simgear/screen/screen-dump.hxx>
59 #include <Include/general.hxx>
60 #include <Aircraft/aircraft.hxx>
61 #include <Airports/simple.hxx>
62 #include <Autopilot/auto_gui.hxx>
63 #include <Autopilot/newauto.hxx>
64 #include <Cockpit/panel.hxx>
65 #include <Controls/controls.hxx>
66 #include <FDM/flight.hxx>
67 #include <Main/bfi.hxx>
68 #include <Main/fg_init.hxx>
69 #include <Main/fg_io.hxx>
70 #include <Main/globals.hxx>
71 #include <Main/save.hxx>
73 #include <NetworkOLK/network.h>
76 #if defined( WIN32 ) && !defined( __CYGWIN__ )
77 # include <simgear/screen/win32-printer.h>
78 # include <simgear/screen/GlBitmaps.h>
83 * A virtual trackball implementation
84 * Written by Gavin Bell for Silicon Graphics, November 1988.
87 * Pass the x and y coordinates of the last and current positions of
88 * the mouse, scaled so they are from (-1.0 ... 1.0).
90 * The resulting rotation is returned as a quaternion rotation in the
94 trackball(float q[4], float p1x, float p1y, float p2x, float p2y);
97 * Given two quaternions, add them together to get a third quaternion.
98 * Adding quaternions to get a compound rotation is analagous to adding
99 * translations to get a compound translation. When incrementally
100 * adding rotations, the first argument here should be the new
102 * rotation, the second and third the total rotation (which will be
103 * over-written with the resulting new total rotation).
106 add_quats(float *q1, float *q2, float *dest);
109 * A useful function, builds a rotation matrix in Matrix based on
113 build_rotmatrix(float m[4][4], float q[4]);
116 * This function computes a quaternion based on an axis (defined by
117 * the given vector) and an angle about which to rotate. The angle is
118 * expressed in radians. The result is put into the third argument.
121 axis_to_quat(float a[3], float phi, float q[4]);
126 FG_USING_STD(string);
128 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
132 #if defined(WIN32) || defined(__CYGWIN32__)
133 #define WIN32_CURSOR_TWEAKS
134 #elif (GLUT_API_VERSION >= 4 || GLUT_XLIB_IMPLEMENTATION >= 9)
135 #define X_CURSOR_TWEAKS
138 // hack, should come from an include someplace
139 extern void fgInitVisuals( void );
140 extern void fgReshape( int width, int height );
141 extern void fgRenderFrame( void );
144 fntTexFont *guiFntHandle = 0;
146 static puMenuBar *mainMenuBar = 0;
147 //static puButton *hideMenuButton = 0;
149 static puDialogBox *dialogBox = 0;
150 static puFrame *dialogFrame = 0;
151 static puText *dialogBoxMessage = 0;
152 static puOneShot *dialogBoxOkButton = 0;
155 static puDialogBox *YNdialogBox = 0;
156 static puFrame *YNdialogFrame = 0;
157 static puText *YNdialogBoxMessage = 0;
158 static puOneShot *YNdialogBoxOkButton = 0;
159 static puOneShot *YNdialogBoxNoButton = 0;
161 static char msg_OK[] = "OK";
162 static char msg_NO[] = "NO";
163 static char msg_YES[] = "YES";
164 static char msg_CANCEL[] = "Cancel";
165 static char msg_RESET[] = "Reset";
167 char *gui_msg_OK; // "OK"
168 char *gui_msg_NO; // "NO"
169 char *gui_msg_YES; // "YES"
170 char *gui_msg_CANCEL; // "CANCEL"
171 char *gui_msg_RESET; // "RESET"
173 static char global_dialog_string[256];
175 // from autopilot.cxx
176 // extern void NewAltitude( puObject *cb );
177 // extern void NewHeading( puObject *cb );
178 // extern void fgAPAdjust( puObject * );
179 // extern void NewTgtAirport( puObject *cb );
180 // bool fgAPTerrainFollowEnabled( void );
181 // bool fgAPAltitudeEnabled( void );
182 // bool fgAPHeadingEnabled( void );
183 // bool fgAPWayPointEnabled( void );
184 // bool fgAPAutoThrottleEnabled( void );
187 extern void fgLatLonFormatToggle( puObject *);
189 /* --------------------------------------------------------------------
191 ---------------------------------------------------------------------*/
195 static int _savedX = 0;
196 static int _savedY = 0;
197 static int last_buttons = 0 ;
198 static int mouse_active = 0;
199 static int menu_on = 0;
200 static int mouse_joystick_control = 0;
202 //static time_t mouse_off_time;
203 //static int mouse_timed_out;
205 // to allow returning to previous view
206 // on second left click in MOUSE_VIEW mode
207 // This has file scope so that it can be reset
208 // if the little rodent is moved NHV
209 static int _mVtoggle;
211 // we break up the glutGetModifiers return mask
212 // once per loop and stash what we need in these
213 static int glut_active_shift;
214 static int glut_active_ctrl;
215 static int glut_active_alt;
217 static float lastquat[4];
218 static float curquat[4];
219 static float _quat0[4];
220 float quat_mat[4][4];
222 // uncomment this for view to exactly follow mouse in MOUSE_VIEW mode
223 // else smooth out the view panning to .01 radian per frame
224 // see view_offset smoothing mechanism in main.cxx
225 #define NO_SMOOTH_MOUSE_VIEW
227 // uncomment following to
228 #define RESET_VIEW_ON_LEAVING_MOUSE_VIEW
230 /* --------------------------------------------------------------------
231 Support for mouse as control yoke (david@megginson.com)
233 - right button toggles between pointer and yoke
234 - horizontal drag with no buttons moves ailerons
235 - vertical drag with no buttons moves elevators
236 - horizontal drag with left button moves brakes (left=on)
237 - vertical drag with left button moves throttle (up=more)
238 - horizontal drag with middle button moves rudder
239 - vertical drag with middle button moves trim
241 For the *_sensitivity variables, a lower number means more sensitive.
243 TODO: figure out how to keep pointer from leaving window in yoke mode.
244 TODO: add thresholds and null zones
245 TODO: sensitivity should be configurable at user option.
246 TODO: allow differential braking (this will be useful if FlightGear
247 ever supports tail-draggers like the DC-3)
248 ---------------------------------------------------------------------*/
256 MouseMode mouse_mode = MOUSE_POINTER;
258 static double aileron_sensitivity = 1.0/500.0;
259 static double elevator_sensitivity = 1.0/500.0;
260 static double brake_sensitivity = 1.0/250.0;
261 static double throttle_sensitivity = 1.0/250.0;
262 static double rudder_sensitivity = 1.0/500.0;
263 static double trim_sensitivity = 1.0/1000.0;
265 static inline void Quat0( void ) {
266 curquat[0] = _quat0[0];
267 curquat[1] = _quat0[1];
268 curquat[2] = _quat0[2];
269 curquat[3] = _quat0[3];
272 static inline int left_button( void ) {
273 return( last_buttons & (1 << GLUT_LEFT_BUTTON) );
276 static inline int middle_button( void ) {
277 return( last_buttons & (1 << GLUT_MIDDLE_BUTTON) );
280 static inline int right_button( void ) {
281 return( last_buttons & (1 << GLUT_RIGHT_BUTTON) );
284 static inline void TurnCursorOn( void )
287 #if defined(WIN32_CURSOR_TWEAKS)
288 switch (mouse_mode) {
290 glutSetCursor(GLUT_CURSOR_INHERIT);
293 glutSetCursor(GLUT_CURSOR_CROSSHAIR);
296 glutSetCursor(GLUT_CURSOR_LEFT_RIGHT);
300 #if defined(X_CURSOR_TWEAKS)
301 glutWarpPointer( globals->get_options()->get_xsize()/2,
302 globals->get_options()->get_ysize()/2);
306 static inline void TurnCursorOff( void )
309 #if defined(WIN32_CURSOR_TWEAKS)
310 glutSetCursor(GLUT_CURSOR_NONE);
311 #elif defined(X_CURSOR_TWEAKS)
312 glutWarpPointer( globals->get_options()->get_xsize(),
313 globals->get_options()->get_ysize());
317 void maybeToggleMouse( void )
319 #if defined(WIN32_CURSOR_TWEAKS)
320 static int first_time = ~0;
321 static int mouse_changed = 0;
325 mouse_changed = ~mouse_changed;
329 if( mouse_mode != MOUSE_POINTER )
331 if( mouse_changed ) {
332 mouse_changed = ~mouse_changed;
338 first_time = ~first_time;
339 #endif // #ifdef WIN32
342 // Call with FALSE to init and TRUE to restore
343 void BusyCursor( int restore )
345 static GLenum cursor = (GLenum) 0;
347 glutSetCursor(cursor);
349 cursor = (GLenum) glutGet( (GLenum) GLUT_WINDOW_CURSOR );
350 #if defined(WIN32_CURSOR_TWEAKS)
353 glutSetCursor( GLUT_CURSOR_WAIT );
357 int guiGetMouseButton(void)
362 void guiGetMouse(int *x, int *y)
368 void guiMotionFunc ( int x, int y )
370 int ww, wh, need_warp = 0;
374 if (mouse_mode == MOUSE_POINTER) {
376 glutPostRedisplay () ;
378 if( x == _mX && y == _mY)
381 // reset left click MOUSE_VIEW toggle feature
384 ww = globals->get_options()->get_xsize();
385 wh = globals->get_options()->get_ysize();
387 switch (mouse_mode) {
389 if( !mouse_joystick_control ) {
390 mouse_joystick_control = 1;
391 globals->get_options()->set_control_mode( FGOptions::FG_MOUSE );
393 if ( left_button() ) {
394 offset = (_mX - x) * brake_sensitivity;
395 controls.move_brake(FGControls::ALL_WHEELS, offset);
396 offset = (_mY - y) * throttle_sensitivity;
397 controls.move_throttle(FGControls::ALL_ENGINES, offset);
398 } else if ( right_button() ) {
399 if( ! current_autopilot->get_HeadingEnabled() ) {
400 offset = (x - _mX) * rudder_sensitivity;
401 controls.move_rudder(offset);
403 if( ! current_autopilot->get_AltitudeEnabled() ) {
404 offset = (_mY - y) * trim_sensitivity;
405 controls.move_elevator_trim(offset);
408 if( ! current_autopilot->get_HeadingEnabled() ) {
409 offset = (x - _mX) * aileron_sensitivity;
410 controls.move_aileron(offset);
412 if( ! current_autopilot->get_AltitudeEnabled() ) {
413 offset = (_mY - y) * elevator_sensitivity;
414 controls.move_elevator(offset);
418 // Keep the mouse in the window.
419 if (x < 5 || x > ww-5 || y < 5 || y > wh-5) {
428 #define CONTRAINED_MOUSE_VIEW_Y
429 #ifdef CONTRAINED_MOUSE_VIEW_Y
433 #endif // CONTRAINED_MOUSE_VIEW_Y
435 } else if( y >= wh-1) {
436 #ifdef CONTRAINED_MOUSE_VIEW_Y
440 #endif // CONTRAINED_MOUSE_VIEW_Y
443 // wrap MOUSE_VIEW mode cursor x position
447 } else if ( x >= ww-1 ) {
451 // try to get FG_PI movement in each half of screen
455 if( middle_button() ) {
457 (2.0f * _mX - W) / W,
458 0, //(H - 2.0f * y) / H, // 3
460 0 //(H - 2.0f * _mY) / H // 1
467 0, //(2.0f * _mX - W) / W, // 0
468 (H - 2.0f * y) / H, // 3
469 0, //(2.0f * x - W) / W, // 2
470 (H - 2.0f * _mY) / H // 1
473 add_quats(lastquat, curquat, curquat);
474 build_rotmatrix(quat_mat, curquat);
477 // this could be done in above quat
478 // but requires redoing view pipeline
479 offset = globals->get_current_view()->get_goal_view_offset();
480 offset += ((_mX - x) * FG_2PI / W );
481 while (offset < 0.0) {
484 while (offset > FG_2PI) {
487 globals->get_current_view()->set_goal_view_offset(offset);
488 #ifdef NO_SMOOTH_MOUSE_VIEW
489 globals->get_current_view()->set_view_offset(offset);
498 glutWarpPointer(x, y);
500 // Record the new mouse position.
506 void guiMouseFunc(int button, int updown, int x, int y)
510 // private MOUSE_VIEW state variables
511 // to allow alternate left clicks in MOUSE_VIEW mode
512 // to toggle between current offsets and straight ahead
514 static int _mVx, _mVy, _Vx, _Vy;
515 static float _quat[4];
516 static double _view_offset;
518 // general purpose variables
521 glutModifiers = glutGetModifiers();
522 glut_active_shift = glutModifiers & GLUT_ACTIVE_SHIFT;
523 glut_active_ctrl = glutModifiers & GLUT_ACTIVE_CTRL;
524 glut_active_alt = glutModifiers & GLUT_ACTIVE_ALT;
526 // Was the left button pressed?
527 if (updown == GLUT_DOWN ) {
528 if( button == GLUT_LEFT_BUTTON)
530 switch (mouse_mode) {
537 // resume previous view offsets
542 curquat[0] = _quat[0];
543 curquat[1] = _quat[1];
544 curquat[2] = _quat[2];
545 curquat[3] = _quat[3];
546 globals->get_current_view()->set_goal_view_offset(_view_offset);
547 #ifdef NO_SMOOTH_MOUSE_VIEW
548 globals->get_current_view()->set_view_offset(_view_offset);
556 _quat[0] = curquat[0];
557 _quat[1] = curquat[1];
558 _quat[2] = curquat[2];
559 _quat[3] = curquat[3];
560 x = globals->get_options()->get_xsize()/2;
561 y = globals->get_options()->get_ysize()/2;
564 globals->get_current_view()->get_goal_view_offset();
565 globals->get_current_view()->set_goal_view_offset(0.0);
566 #ifdef NO_SMOOTH_MOUSE_VIEW
567 globals->get_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 = globals->get_options()->get_xsize()/2;
584 _mY = globals->get_options()->get_ysize()/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 globals->get_options()->set_control_mode( FGOptions::FG_JOYSTICK );
600 x = globals->get_options()->get_xsize()/2;
601 y = globals->get_options()->get_ysize()/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 globals->get_current_view()->set_goal_view_offset(0.0);
617 #ifdef NO_SMOOTH_MOUSE_VIEW
618 globals->get_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 if (!puMouse (button, updown, x,y)) {
644 current_panel->doMouseAction(button, updown, x, y);
648 // Register the new position (if it
649 // hasn't been registered already).
653 glutPostRedisplay ();
656 /* ================ General Purpose Functions ================ */
658 // Intercept the Escape Key
659 void ConfirmExitDialog(void)
661 FG_PUSH_PUI_DIALOG( YNdialogBox );
664 // General Purpose Message Box
665 void mkDialog (const char *txt)
667 strncpy(global_dialog_string, txt, 256);
668 dialogBoxMessage->setLabel(global_dialog_string);
669 FG_PUSH_PUI_DIALOG( dialogBox );
672 // Toggle the Menu and Mouse display state
673 void guiToggleMenu(void)
676 // printf("Hiding Menu\n");
677 mainMenuBar->hide ();
678 #if defined(WIN32_CURSOR_TWEAKS)
679 if( mouse_mode == MOUSE_POINTER )
681 #endif // #ifdef WIN32_CURSOR_TWEAKS
683 // printf("Showing Menu\n");
684 mainMenuBar->reveal();
687 #endif // #ifdef WIN32
692 /* -----------------------------------------------------------------------
693 the Gui callback functions
694 ____________________________________________________________________*/
696 static void saveFlight(puObject *cv)
699 ofstream output("fgfs.sav");
700 if (output.good() && fgSaveFlight(output)) {
702 mkDialog("Saved flight to ./fgfs.sav");
703 FG_LOG(FG_INPUT, FG_INFO, "Saved flight to fgfs.sav");
705 mkDialog("Cannot save flight to ./fgfs.sav");
706 FG_LOG(FG_INPUT, FG_ALERT, "Cannot save flight to fgfs.sav");
711 static void loadFlight(puObject *cb)
714 ifstream input("fgfs.sav");
715 if (input.good() && fgLoadFlight(input)) {
717 mkDialog("Loaded flight from fgfs.sav");
718 FG_LOG(FG_INPUT, FG_INFO, "Restored flight from ./fgfs.sav");
720 mkDialog("Failed to load flight from fgfs.sav");
721 FG_LOG(FG_INPUT, FG_ALERT, "Cannot load flight from ./fgfs.sav");
726 void reInit(puObject *cb)
730 build_rotmatrix(quat_mat, curquat);
731 fgReInitSubsystems();
735 static void toggleClouds(puObject *cb)
737 FGBFI::setClouds( !FGBFI::getClouds() );
740 // This is the accessor function
741 void guiTogglePanel(puObject *cb)
743 globals->get_options()->toggle_panel();
746 //void MenuHideMenuCb(puObject *cb)
747 void hideMenuCb (puObject *cb)
752 void goodBye(puObject *)
754 // FG_LOG( FG_INPUT, FG_ALERT,
755 // "Program exiting normally at user request." );
756 cout << "Program exiting normally at user request." << endl;
758 #ifdef FG_NETWORK_OLK
759 if ( globals->get_options()->get_network_olk() ) {
760 if ( net_is_registered == 0 ) fgd_send_com( "8", FGFS_host);
764 // close all external I/O connections
771 void goAwayCb (puObject *me)
773 FG_POP_PUI_DIALOG( dialogBox );
776 void mkDialogInit (void)
778 // printf("mkDialogInit\n");
779 int x = (globals->get_options()->get_xsize()/2 - 400/2);
780 int y = (globals->get_options()->get_ysize()/2 - 100/2);
781 dialogBox = new puDialogBox (x, y); // 150, 50
783 dialogFrame = new puFrame (0,0,400,100);
784 dialogBoxMessage = new puText (10, 70);
785 dialogBoxMessage -> setLabel ("");
786 dialogBoxOkButton = new puOneShot (180, 10, 240, 50);
787 dialogBoxOkButton -> setLegend (gui_msg_OK);
788 dialogBoxOkButton -> makeReturnDefault (TRUE );
789 dialogBoxOkButton -> setCallback (goAwayCb);
791 FG_FINALIZE_PUI_DIALOG( dialogBox );
794 void MayBeGoodBye(puObject *)
799 void goAwayYesNoCb(puObject *me)
801 FG_POP_PUI_DIALOG( YNdialogBox);
804 void ConfirmExitDialogInit(void)
806 char msg[] = "Really Quit";
809 // printf("ConfirmExitDialogInit\n");
810 int len = 200 - puGetStringWidth( puGetDefaultLabelFont(), msg )/2;
812 int x = (globals->get_options()->get_xsize()/2 - 400/2);
813 int y = (globals->get_options()->get_ysize()/2 - 100/2);
815 YNdialogBox = new puDialogBox (x, y); // 150, 50
816 // YNdialogBox = new puDialogBox (150, 50);
818 YNdialogFrame = new puFrame (0,0,400, 100);
820 YNdialogBoxMessage = new puText (len, 70);
821 YNdialogBoxMessage -> setDefaultValue (msg);
822 YNdialogBoxMessage -> getDefaultValue (&s);
823 YNdialogBoxMessage -> setLabel (s);
825 YNdialogBoxOkButton = new puOneShot (100, 10, 160, 50);
826 YNdialogBoxOkButton -> setLegend (gui_msg_OK);
827 YNdialogBoxOkButton -> makeReturnDefault (TRUE );
828 YNdialogBoxOkButton -> setCallback (goodBye);
830 YNdialogBoxNoButton = new puOneShot (240, 10, 300, 50);
831 YNdialogBoxNoButton -> setLegend (gui_msg_NO);
832 YNdialogBoxNoButton -> setCallback (goAwayYesNoCb);
834 FG_FINALIZE_PUI_DIALOG( YNdialogBox );
837 void notCb (puObject *)
839 mkDialog ("This function isn't implemented yet");
842 void helpCb (puObject *)
846 #if defined(FX) && !defined(WIN32)
847 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
848 if ( global_fullscreen ) {
849 global_fullscreen = false;
850 XMesaSetFXmode( XMESA_FX_WINDOW );
856 string url = "http://www.flightgear.org/Docs/InstallGuide/getstart.html";
858 if ( system("xwininfo -name Netscape > /dev/null 2>&1") == 0 ) {
859 command = "netscape -remote \"openURL(" + url + ")\" &";
861 command = "netscape " + url + " &";
864 command = "webrun.bat";
867 system( command.c_str() );
868 //string text = "Help started in netscape window.";
870 //mkDialog (text.c_str());
871 mkDialog ("Help started in netscape window.");
874 #if defined( WIN32 ) && !defined( __CYGWIN__)
876 static void rotateView( double roll, double pitch, double yaw )
881 static GlBitmap *b1 = NULL;
882 extern FGInterface cur_view_fdm;
883 GLubyte *hiResScreenCapture( int multiplier )
885 float oldfov = globals->get_options()->get_fov();
886 float fov = oldfov / multiplier;
887 FGViewer *v = globals->get_current_view();
888 globals->get_options()->set_fov(fov);
889 v->force_update_fov_math();
891 int cur_width = globals->get_current_view()->get_winWidth( );
892 int cur_height = globals->get_current_view()->get_winHeight( );
893 if (b1) delete( b1 );
894 // New empty (mostly) bitmap
895 b1 = new GlBitmap( GL_RGB, 1, 1, (unsigned char *)"123" );
897 for ( y = 0; y < multiplier; y++ )
899 for ( x = 0; x < multiplier; x++ )
901 fgReshape( cur_width, cur_height );
903 rotateView( 0, (y*fov)-((multiplier-1)*fov/2), (x*fov)-((multiplier-1)*fov/2) );
907 b1->copyBitmap( &b2, cur_width*x, cur_height*y );
910 globals->get_current_view()->UpdateViewParams(cur_view_fdm);
911 globals->get_options()->set_fov(oldfov);
912 v->force_update_fov_math();
913 return b1->getBitmap();
918 #if defined( WIN32 ) && !defined( __CYGWIN__)
919 // win32 print screen function
920 void printScreen ( puObject *obj ) {
921 bool show_pu_cursor = false;
923 if ( !puCursorIsHidden() ) {
924 show_pu_cursor = true;
930 CGlPrinter p( CGlPrinter::PRINT_BITMAP );
931 int cur_width = globals->get_current_view()->get_winWidth( );
932 int cur_height = globals->get_current_view()->get_winHeight( );
933 p.Begin( "FlightGear", cur_width*3, cur_height*3 );
934 p.End( hiResScreenCapture(3) );
937 mainMenuBar->reveal();
940 if ( show_pu_cursor ) {
945 #endif // #ifdef WIN32
948 void dumpSnapShot ( puObject *obj ) {
953 // do a screen snap shot
954 void fgDumpSnapShot () {
955 bool show_pu_cursor = false;
957 int freeze = globals->get_freeze();
959 globals->set_freeze( true );
963 if ( !puCursorIsHidden() ) {
964 show_pu_cursor = true;
969 fgReshape( globals->get_options()->get_xsize(),
970 globals->get_options()->get_ysize() );
972 // we need two render frames here to clear the menu and cursor
973 // ... not sure why but doing an extra fgFenderFrame() shoulnd't
978 my_glDumpWindow( "fgfs-screen.ppm",
979 globals->get_options()->get_xsize(),
980 globals->get_options()->get_ysize() );
982 mkDialog ("Snap shot saved to fgfs-screen.ppm");
984 if ( show_pu_cursor ) {
990 mainMenuBar->reveal();
994 globals->set_freeze( false );
998 /// The beginnings of teleportation :-)
999 // Needs cleaning up but works
1000 // These statics should disapear when this is a class
1001 static puDialogBox *AptDialog = 0;
1002 static puFrame *AptDialogFrame = 0;
1003 static puText *AptDialogMessage = 0;
1004 static puInput *AptDialogInput = 0;
1006 static char NewAirportId[16];
1007 static char NewAirportLabel[] = "Enter New Airport ID";
1009 static puOneShot *AptDialogOkButton = 0;
1010 static puOneShot *AptDialogCancelButton = 0;
1011 static puOneShot *AptDialogResetButton = 0;
1013 void AptDialog_Cancel(puObject *)
1015 FG_POP_PUI_DIALOG( AptDialog );
1018 void AptDialog_OK (puObject *)
1020 FGPath path( globals->get_options()->get_fg_root() );
1021 path.append( "Airports" );
1022 path.append( "simple.mk4" );
1023 FGAirports airports( path.c_str() );
1027 int freeze = globals->get_freeze();
1029 globals->set_freeze( true );
1032 AptDialogInput->getValue(&s);
1035 cout << "AptDialog_OK " << AptId << " " << AptId.length() << endl;
1037 AptDialog_Cancel( NULL );
1039 if ( AptId.length() ) {
1040 // set initial position from airport id
1041 FG_LOG( FG_GENERAL, FG_INFO,
1042 "Attempting to set starting position from airport code "
1045 if ( airports.search( AptId, &a ) )
1047 globals->get_options()->set_airport_id( AptId.c_str() );
1048 globals->get_options()->set_altitude( -9999.0 );
1049 // fgSetPosFromAirportID( AptId );
1050 fgSetPosFromAirportIDandHdg( AptId,
1051 cur_fdm_state->get_Psi() * RAD_TO_DEG);
1053 fgReInitSubsystems();
1056 AptId += " not in database.";
1057 mkDialog(AptId.c_str());
1061 globals->set_freeze( false );
1064 void AptDialog_Reset(puObject *)
1066 // strncpy( NewAirportId, globals->get_options()->get_airport_id().c_str(), 16 );
1067 sprintf( NewAirportId, "%s", globals->get_options()->get_airport_id().c_str() );
1068 AptDialogInput->setValue ( NewAirportId );
1069 AptDialogInput->setCursor( 0 ) ;
1072 void NewAirport(puObject *cb)
1074 // strncpy( NewAirportId, globals->get_options()->get_airport_id().c_str(), 16 );
1075 sprintf( NewAirportId, "%s", globals->get_options()->get_airport_id().c_str() );
1076 // cout << "NewAirport " << NewAirportId << endl;
1077 AptDialogInput->setValue( NewAirportId );
1079 FG_PUSH_PUI_DIALOG( AptDialog );
1082 static void NewAirportInit(void)
1084 sprintf( NewAirportId, "%s", globals->get_options()->get_airport_id().c_str() );
1085 int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
1086 NewAirportLabel ) / 2;
1088 AptDialog = new puDialogBox (150, 50);
1090 AptDialogFrame = new puFrame (0,0,350, 150);
1091 AptDialogMessage = new puText (len, 110);
1092 AptDialogMessage -> setLabel (NewAirportLabel);
1094 AptDialogInput = new puInput (50, 70, 300, 100);
1095 AptDialogInput -> setValue (NewAirportId);
1096 AptDialogInput -> acceptInput();
1098 AptDialogOkButton = new puOneShot (50, 10, 110, 50);
1099 AptDialogOkButton -> setLegend (gui_msg_OK);
1100 AptDialogOkButton -> setCallback (AptDialog_OK);
1101 AptDialogOkButton -> makeReturnDefault(TRUE);
1103 AptDialogCancelButton = new puOneShot (140, 10, 210, 50);
1104 AptDialogCancelButton -> setLegend (gui_msg_CANCEL);
1105 AptDialogCancelButton -> setCallback (AptDialog_Cancel);
1107 AptDialogResetButton = new puOneShot (240, 10, 300, 50);
1108 AptDialogResetButton -> setLegend (gui_msg_RESET);
1109 AptDialogResetButton -> setCallback (AptDialog_Reset);
1111 cout << "NewAirportInit " << NewAirportId << endl;
1112 FG_FINALIZE_PUI_DIALOG( AptDialog );
1115 #ifdef FG_NETWORK_OLK
1117 /// The beginnings of networking :-)
1118 // Needs cleaning up but works
1119 // These statics should disapear when this is a class
1120 static puDialogBox *NetIdDialog = 0;
1121 static puFrame *NetIdDialogFrame = 0;
1122 static puText *NetIdDialogMessage = 0;
1123 static puInput *NetIdDialogInput = 0;
1125 static char NewNetId[16];
1126 static char NewNetIdLabel[] = "Enter New Callsign";
1127 extern char *fgd_callsign;
1129 static puOneShot *NetIdDialogOkButton = 0;
1130 static puOneShot *NetIdDialogCancelButton = 0;
1132 void NetIdDialog_Cancel(puObject *)
1134 FG_POP_PUI_DIALOG( NetIdDialog );
1137 void NetIdDialog_OK (puObject *)
1141 bool freeze = globals->get_freeze();
1143 globals->set_freeze( true );
1145 The following needs some cleanup because
1146 "string options.NetId" and "char *net_callsign"
1148 NetIdDialogInput->getValue(&net_callsign);
1149 NetId = net_callsign;
1151 NetIdDialog_Cancel( NULL );
1152 globals->get_options()->set_net_id( NetId.c_str() );
1153 strcpy( fgd_callsign, net_callsign);
1154 // strcpy( fgd_callsign, globals->get_options()->get_net_id().c_str());
1155 /* Entering a callsign indicates : user wants Net HUD Info */
1156 net_hud_display = 1;
1159 globals->set_freeze( false );
1162 void NewCallSign(puObject *cb)
1164 sprintf( NewNetId, "%s", globals->get_options()->get_net_id().c_str() );
1165 // sprintf( NewNetId, "%s", fgd_callsign );
1166 NetIdDialogInput->setValue( NewNetId );
1168 FG_PUSH_PUI_DIALOG( NetIdDialog );
1171 static void NewNetIdInit(void)
1173 sprintf( NewNetId, "%s", globals->get_options()->get_net_id().c_str() );
1174 // sprintf( NewNetId, "%s", fgd_callsign );
1175 int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
1176 NewNetIdLabel ) / 2;
1178 NetIdDialog = new puDialogBox (150, 50);
1180 NetIdDialogFrame = new puFrame (0,0,350, 150);
1181 NetIdDialogMessage = new puText (len, 110);
1182 NetIdDialogMessage -> setLabel (NewNetIdLabel);
1184 NetIdDialogInput = new puInput (50, 70, 300, 100);
1185 NetIdDialogInput -> setValue (NewNetId);
1186 NetIdDialogInput -> acceptInput();
1188 NetIdDialogOkButton = new puOneShot (50, 10, 110, 50);
1189 NetIdDialogOkButton -> setLegend (gui_msg_OK);
1190 NetIdDialogOkButton -> setCallback (NetIdDialog_OK);
1191 NetIdDialogOkButton -> makeReturnDefault(TRUE);
1193 NetIdDialogCancelButton = new puOneShot (240, 10, 300, 50);
1194 NetIdDialogCancelButton -> setLegend (gui_msg_CANCEL);
1195 NetIdDialogCancelButton -> setCallback (NetIdDialog_Cancel);
1198 FG_FINALIZE_PUI_DIALOG( NetIdDialog );
1201 static void net_display_toggle( puObject *cb)
1203 net_hud_display = (net_hud_display) ? 0 : 1;
1204 printf("Toggle net_hud_display : %d\n", net_hud_display);
1207 static void net_register( puObject *cb)
1209 fgd_send_com( "1", FGFS_host );
1210 net_is_registered = 0;
1211 printf("Registering to deamon\n");
1214 static void net_unregister( puObject *cb)
1216 fgd_send_com( "8", FGFS_host );
1217 net_is_registered = -1;
1218 printf("Unregistering from deamon\n");
1222 /*************** Deamon communication **********/
1224 // These statics should disapear when this is a class
1225 static puDialogBox *NetFGDDialog = 0;
1226 static puFrame *NetFGDDialogFrame = 0;
1227 static puText *NetFGDDialogMessage = 0;
1228 //static puInput *NetFGDDialogInput = 0;
1230 //static char NewNetId[16];
1231 static char NewNetFGDLabel[] = "Scan for deamon ";
1232 static char NewFGDHost[64] = "olk.mcp.de";
1233 static int NewFGDPortLo = 10000;
1234 static int NewFGDPortHi = 10001;
1236 //extern char *fgd_callsign;
1237 extern u_short base_port, end_port;
1238 extern int fgd_ip, verbose, current_port;
1239 extern char *fgd_host;
1242 static puOneShot *NetFGDDialogOkButton = 0;
1243 static puOneShot *NetFGDDialogCancelButton = 0;
1244 static puOneShot *NetFGDDialogScanButton = 0;
1246 static puInput *NetFGDHostDialogInput = 0;
1247 static puInput *NetFGDPortLoDialogInput = 0;
1248 static puInput *NetFGDPortHiDialogInput = 0;
1250 void NetFGDDialog_Cancel(puObject *)
1252 FG_POP_PUI_DIALOG( NetFGDDialog );
1255 void NetFGDDialog_OK (puObject *)
1259 bool freeze = globals->get_freeze();
1261 globals->set_freeze( true );
1262 NetFGDHostDialogInput->getValue( &NetFGD );
1263 strcpy( fgd_host, NetFGD);
1264 NetFGDPortLoDialogInput->getValue( (int *) &base_port );
1265 NetFGDPortHiDialogInput->getValue( (int *) &end_port );
1266 NetFGDDialog_Cancel( NULL );
1268 globals->set_freeze( false );
1271 void NetFGDDialog_SCAN (puObject *)
1276 bool freeze = globals->get_freeze();
1278 globals->set_freeze( true );
1279 // printf("Vor getvalue %s\n");
1280 NetFGDHostDialogInput->getValue( &NetFGD );
1281 // printf("Vor strcpy %s\n", (char *) NetFGD);
1282 strcpy( fgd_host, NetFGD);
1283 NetFGDPortLoDialogInput->getValue( (int *) &base_port );
1284 NetFGDPortHiDialogInput->getValue( (int *) &end_port );
1285 printf("FGD: %s Port-Start: %d Port-End: %d\n", fgd_host,
1286 base_port, end_port);
1287 net_resolv_fgd(fgd_host);
1288 printf("Resolve : %d\n", net_r);
1290 globals->set_freeze( false );
1293 strcpy( fgd_name, "");
1294 for( current_port = base_port; ( current_port <= end_port); current_port++) {
1295 fgd_send_com("0" , FGFS_host);
1296 sprintf( NewNetFGDLabel , "Scanning for deamon Port: %d", current_port);
1297 printf("FGD: searching %s\n", fgd_name);
1298 if ( strcmp( fgd_name, "") != 0 ) {
1299 sprintf( NewNetFGDLabel , "Found %s at Port: %d",
1300 fgd_name, current_port);
1301 fgd_port = current_port;
1302 current_port = end_port+1;
1305 current_port = end_port = base_port = fgd_port;
1307 NetFGDDialog_Cancel( NULL );
1311 void net_fgd_scan(puObject *cb)
1313 NewFGDPortLo = base_port;
1314 NewFGDPortHi = end_port;
1315 strcpy( NewFGDHost, fgd_host);
1316 NetFGDPortLoDialogInput->setValue( NewFGDPortLo );
1317 NetFGDPortHiDialogInput->setValue( NewFGDPortHi );
1318 NetFGDHostDialogInput->setValue( NewFGDHost );
1320 FG_PUSH_PUI_DIALOG( NetFGDDialog );
1324 static void NewNetFGDInit(void)
1326 // sprintf( NewNetId, "%s", globals->get_options()->get_net_id().c_str() );
1327 // sprintf( NewNetId, "%s", fgd_callsign );
1328 int len = 170 - puGetStringWidth( puGetDefaultLabelFont(),
1329 NewNetFGDLabel ) / 2;
1331 NetFGDDialog = new puDialogBox (310, 30);
1333 NetFGDDialogFrame = new puFrame (0,0,320, 170);
1334 NetFGDDialogMessage = new puText (len, 140);
1335 NetFGDDialogMessage -> setLabel (NewNetFGDLabel);
1337 NetFGDPortLoDialogInput = new puInput (50, 70, 127, 100);
1338 NetFGDPortLoDialogInput -> setValue (NewFGDPortLo);
1339 NetFGDPortLoDialogInput -> acceptInput();
1341 NetFGDPortHiDialogInput = new puInput (199, 70, 275, 100);
1342 NetFGDPortHiDialogInput -> setValue (NewFGDPortHi);
1343 NetFGDPortHiDialogInput -> acceptInput();
1345 NetFGDHostDialogInput = new puInput (50, 100, 275, 130);
1346 NetFGDHostDialogInput -> setValue (NewFGDHost);
1347 NetFGDHostDialogInput -> acceptInput();
1349 NetFGDDialogScanButton = new puOneShot (130, 10, 200, 50);
1350 NetFGDDialogScanButton -> setLegend ("Scan");
1351 NetFGDDialogScanButton -> setCallback (NetFGDDialog_SCAN);
1352 NetFGDDialogScanButton -> makeReturnDefault(FALSE);
1354 NetFGDDialogOkButton = new puOneShot (50, 10, 120, 50);
1355 NetFGDDialogOkButton -> setLegend (gui_msg_OK);
1356 NetFGDDialogOkButton -> setCallback (NetFGDDialog_OK);
1357 NetFGDDialogOkButton -> makeReturnDefault(TRUE);
1359 NetFGDDialogCancelButton = new puOneShot (210, 10, 280, 50);
1360 NetFGDDialogCancelButton -> setLegend (gui_msg_CANCEL);
1361 NetFGDDialogCancelButton -> setCallback (NetFGDDialog_Cancel);
1364 FG_FINALIZE_PUI_DIALOG( NetFGDDialog );
1368 static void net_display_toggle( puObject *cb)
1370 net_hud_display = (net_hud_display) ? 0 : 1;
1371 printf("Toggle net_hud_display : %d\n", net_hud_display);
1378 /*************** End Networking **************/
1382 /* -----------------------------------------------------------------------
1384 ---------------------------------------------------------------------*/
1385 char *fileSubmenu [] = {
1386 "Exit", /* "Close", "---------", */
1387 #if defined( WIN32 ) && !defined( __CYGWIN__)
1397 puCallback fileSubmenuCb [] = {
1398 MayBeGoodBye, /* hideMenuCb, NULL, */
1399 #if defined( WIN32 ) && !defined( __CYGWIN__)
1412 char *editSubmenu [] = {
1415 puCallback editSubmenuCb [] = {
1420 extern void fgHUDalphaAdjust( puObject * );
1421 char *viewSubmenu [] = {
1423 /* "Cockpit View > ", "View >","------------", */
1424 "Toggle Panel...", NULL
1426 puCallback viewSubmenuCb [] = {
1428 /* notCb, notCb, NULL, */
1429 guiTogglePanel, NULL
1434 char *autopilotSubmenu [] = {
1435 "Toggle HUD Format", "Adjust AP Settings",
1437 "Clear Route", "Skip Current Waypoint", "Add Waypoint",
1439 "Set Altitude", "Set Heading",
1443 puCallback autopilotSubmenuCb [] = {
1444 fgLatLonFormatToggle, fgAPAdjust,
1446 ClearRoute, PopWayPoint, AddWayPoint,
1448 NewAltitude, NewHeading,
1452 char *environmentSubmenu [] = {
1454 "Goto Airport", /* "Terrain", "Weather", */ NULL
1456 puCallback environmentSubmenuCb [] = {
1458 NewAirport, /* notCb, notCb, */ NULL
1462 char *optionsSubmenu [] = {
1463 "Preferences", "Realism & Reliablity...", NULL
1465 puCallback optionsSubmenuCb [] = {
1470 #ifdef FG_NETWORK_OLK
1471 char *networkSubmenu [] = {
1472 "Unregister from FGD ", /* "Send MSG to All", "Send MSG", "Show Pilots", */
1474 "Scan for Deamons", "Enter Callsign", /* "Display Netinfos", */
1475 "Toggle Display", NULL
1477 puCallback networkSubmenuCb [] = {
1478 /* notCb, notCb, notCb, notCb, */
1481 net_fgd_scan, NewCallSign,
1482 net_display_toggle, NULL
1486 char *helpSubmenu [] = {
1487 /* "About...", */ "Help", NULL
1489 puCallback helpSubmenuCb [] = {
1490 /* notCb, */ helpCb, NULL
1494 /* -------------------------------------------------------------------------
1496 _____________________________________________________________________*/
1501 char *mesa_win_state;
1505 puSetDefaultStyle ( PUSTYLE_SMALL_BEVELLED ); //PUSTYLE_DEFAULT
1506 puSetDefaultColourScheme (0.8, 0.8, 0.8, 0.4);
1508 // Initialize our GLOBAL GUI STRINGS
1509 gui_msg_OK = msg_OK; // "OK"
1510 gui_msg_NO = msg_NO; // "NO"
1511 gui_msg_YES = msg_YES; // "YES"
1512 gui_msg_CANCEL = msg_CANCEL; // "CANCEL"
1513 gui_msg_RESET = msg_RESET; // "RESET"
1515 // Next check home directory
1517 char* envp = ::getenv( "FG_FONTS" );
1518 if ( envp != NULL ) {
1519 fntpath.set( envp );
1521 fntpath.set( globals->get_options()->get_fg_root() );
1522 fntpath.append( "Fonts" );
1525 // Install our fast fonts
1526 fntpath.append( "typewriter.txf" );
1527 guiFntHandle = new fntTexFont ;
1528 guiFntHandle -> load ( (char *)fntpath.c_str() ) ;
1529 puFont GuiFont ( guiFntHandle, 15 ) ;
1530 puSetDefaultFonts( GuiFont, GuiFont ) ;
1531 guiFnt = puGetDefaultLabelFont();
1533 if ( globals->get_options()->get_mouse_pointer() == 0 ) {
1534 // no preference specified for mouse pointer, attempt to autodetect...
1535 // Determine if we need to render the cursor, or if the windowing
1536 // system will do it. First test if we are rendering with glide.
1537 if ( strstr ( general.get_glRenderer(), "Glide" ) ) {
1538 // Test for the MESA_GLX_FX env variable
1539 if ( (mesa_win_state = getenv( "MESA_GLX_FX" )) != NULL) {
1540 // test if we are fullscreen mesa/glide
1541 if ( (mesa_win_state[0] == 'f') ||
1542 (mesa_win_state[0] == 'F') ) {
1547 mouse_active = ~mouse_active;
1548 } else if ( globals->get_options()->get_mouse_pointer() == 1 ) {
1549 // don't show pointer
1550 } else if ( globals->get_options()->get_mouse_pointer() == 2 ) {
1551 // force showing pointer
1553 mouse_active = ~mouse_active;
1556 // MOUSE_VIEW mode stuff
1557 trackball(_quat0, 0.0, 0.0, 0.0, 0.0);
1559 build_rotmatrix(quat_mat, curquat);
1561 // Set up our Dialog Boxes
1562 ConfirmExitDialogInit();
1564 #ifdef FG_NETWORK_OLK
1570 // Make the menu bar
1571 mainMenuBar = new puMenuBar ();
1572 mainMenuBar -> add_submenu ("File", fileSubmenu, fileSubmenuCb);
1573 // mainMenuBar -> add_submenu ("Edit", editSubmenu, editSubmenuCb);
1574 mainMenuBar -> add_submenu ("View", viewSubmenu, viewSubmenuCb);
1575 mainMenuBar -> add_submenu ("Environment", environmentSubmenu, environmentSubmenuCb);
1576 mainMenuBar -> add_submenu ("Autopilot", autopilotSubmenu, autopilotSubmenuCb);
1577 // mainMenuBar -> add_submenu ("Options", optionsSubmenu, optionsSubmenuCb);
1578 #ifdef FG_NETWORK_OLK
1579 if ( globals->get_options()->get_network_olk() ) {
1580 mainMenuBar -> add_submenu ("Network", networkSubmenu, networkSubmenuCb);
1583 mainMenuBar -> add_submenu ("Help", helpSubmenu, helpSubmenuCb);
1584 mainMenuBar-> close ();
1585 // Set up menu bar toggle
1594 * Implementation of a virtual trackball.
1595 * Implemented by Gavin Bell, lots of ideas from Thant Tessman and
1596 * the August '88 issue of Siggraph's "Computer Graphics," pp. 121-129.
1598 * Vector manip code:
1600 * Original code from:
1601 * David M. Ciemiewicz, Mark Grossman, Henry Moreton, and Paul Haeberli
1603 * Much mucking with by:
1606 #if defined(_WIN32) && !defined( __CYGWIN32__ )
1607 #pragma warning (disable:4244) /* disable bogus conversion warnings */
1611 //#include "trackball.h"
1614 * This size should really be based on the distance from the center of
1615 * rotation to the point on the object underneath the mouse. That
1616 * point would then track the mouse as closely as possible. This is a
1617 * simple example, though, so that is left as an Exercise for the
1620 #define TRACKBALLSIZE (0.8f)
1621 #define SQRT(x) sqrt(x)
1624 * Local function prototypes (not defined in trackball.h)
1626 static float tb_project_to_sphere(float, float, float);
1627 static void normalize_quat(float [4]);
1638 vset(float *v, float x, float y, float z)
1646 vsub(const float *src1, const float *src2, float *dst)
1648 dst[0] = src1[0] - src2[0];
1649 dst[1] = src1[1] - src2[1];
1650 dst[2] = src1[2] - src2[2];
1654 vcopy(const float *v1, float *v2)
1657 for (i = 0 ; i < 3 ; i++)
1662 vcross(const float *v1, const float *v2, float *cross)
1666 temp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
1667 temp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
1668 temp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
1673 vlength(const float *v)
1675 float tmp = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
1680 vscale(float *v, float div)
1690 vscale(v,1.0/vlength(v));
1694 vdot(const float *v1, const float *v2)
1696 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
1700 vadd(const float *src1, const float *src2, float *dst)
1702 dst[0] = src1[0] + src2[0];
1703 dst[1] = src1[1] + src2[1];
1704 dst[2] = src1[2] + src2[2];
1708 * Given an axis and angle, compute quaternion.
1711 axis_to_quat(float a[3], float phi, float q[4])
1713 double sinphi2, cosphi2;
1714 double phi2 = phi/2.0;
1715 sinphi2 = sin(phi2);
1716 cosphi2 = cos(phi2);
1724 * Project an x,y pair onto a sphere of radius r OR a hyperbolic sheet
1725 * if we are away from the center of the sphere.
1728 tb_project_to_sphere(float r, float x, float y)
1734 if (d < r * 0.70710678118654752440) { /* Inside sphere */
1737 } else { /* On hyperbola */
1738 t = r / 1.41421356237309504880;
1745 * Quaternions always obey: a^2 + b^2 + c^2 + d^2 = 1.0
1746 * If they don't add up to 1.0, dividing by their magnitued will
1749 * Note: See the following for more information on quaternions:
1751 * - Shoemake, K., Animating rotation with quaternion curves, Computer
1752 * Graphics 19, No 3 (Proc. SIGGRAPH'85), 245-254, 1985.
1753 * - Pletinckx, D., Quaternion calculus as a basic tool in computer
1754 * graphics, The Visual Computer 5, 2-13, 1989.
1757 normalize_quat(float q[4])
1762 tmp = q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3];
1763 mag = 1.0 / SQRT(tmp);
1764 for (i = 0; i < 4; i++)
1769 * Ok, simulate a track-ball. Project the points onto the virtual
1770 * trackball, then figure out the axis of rotation, which is the cross
1771 * product of P1 P2 and O P1 (O is the center of the ball, 0,0,0)
1772 * Note: This is a deformed trackball-- is a trackball in the center,
1773 * but is deformed into a hyperbolic sheet of rotation away from the
1774 * center. This particular function was chosen after trying out
1775 * several variations.
1777 * It is assumed that the arguments to this routine are in the range
1781 trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
1783 float a[3]; /* Axis of rotation */
1784 float phi; /* how much to rotate about axis */
1785 float p1[3], p2[3], d[3];
1788 if (p1x == p2x && p1y == p2y) {
1796 * First, figure out z-coordinates for projection of P1 and P2 to
1799 vset(p1,p1x,p1y,tb_project_to_sphere(TRACKBALLSIZE,p1x,p1y));
1800 vset(p2,p2x,p2y,tb_project_to_sphere(TRACKBALLSIZE,p2x,p2y));
1803 * Now, we want the cross product of P1 and P2
1808 * Figure out how much to rotate around that axis.
1811 t = vlength(d) / (2.0*TRACKBALLSIZE);
1814 * Avoid problems with out-of-control values...
1816 if (t > 1.0) t = 1.0;
1817 if (t < -1.0) t = -1.0;
1818 phi = 2.0 * asin(t);
1820 axis_to_quat(a,phi,q);
1824 * Given two rotations, e1 and e2, expressed as quaternion rotations,
1825 * figure out the equivalent single rotation and stuff it into dest.
1827 * This routine also normalizes the result every RENORMCOUNT times it is
1828 * called, to keep error from creeping in.
1830 * NOTE: This routine is written so that q1 or q2 may be the same
1831 * as dest (or each other).
1834 #define RENORMCOUNT 97
1837 add_quats(float q1[4], float q2[4], float dest[4])
1840 float t1[4], t2[4], t3[4];
1844 printf("q1 = %f %f %f %f\n", q1[0], q1[1], q1[2], q1[3]);
1845 printf("q2 = %f %f %f %f\n", q2[0], q2[1], q2[2], q2[3]);
1857 tf[3] = q1[3] * q2[3] - vdot(q1,q2);
1860 printf("tf = %f %f %f %f\n", tf[0], tf[1], tf[2], tf[3]);
1868 if (++count > RENORMCOUNT) {
1870 normalize_quat(dest);
1875 * Build a rotation matrix, given a quaternion rotation.
1879 build_rotmatrix(float m[4][4], float q[4])
1881 //#define TRANSPOSED_QUAT
1882 #ifndef TRANSPOSED_QUAT
1883 m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
1884 m[0][1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
1885 m[0][2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
1888 m[1][0] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
1889 m[1][1]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
1890 m[1][2] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
1893 m[2][0] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
1894 m[2][1] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
1895 m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
1902 #else // TRANSPOSED_QUAT
1903 m[0][0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]);
1904 m[0][1] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
1905 m[0][2] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
1908 m[1][0] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
1909 m[1][1] = 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
1910 m[1][2] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
1913 m[2][0] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
1914 m[2][1] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
1915 m[2][2] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);