1 // $Id: Tb_Window.h 900 2009-08-13 20:00:45Z larry $ 2 // 3 /* A GL window with a trackball interface. This is a subclass of Fl_Gl_Window 4 that only implements the handle() method to keep track of mouse motions. 5 You subclass Tb_Window and implement draw(), and call the transform() method 6 to orient the scene. */ 7 8 #ifndef Tb_Window_h 9 #define Tb_Window_h 10 11 #include <FL/Fl.H> 12 #include <FL/glut.H> 13 #include <FL/Fl_Window.H> 14 #include <FL/gl.h> 15 16 #ifndef TRUE 17 #define TRUE 1 18 #endif 19 20 #ifndef FALSE 21 #define FALSE 0 22 #endif 23 24 /* Handy typedefs for declaring storage of 2- & 3-vectors. */ 25 26 typedef float XY[2]; 27 28 typedef float XYZ[3]; 29 30 31 /* Routines for the quaternion representation of rotations. [quat.c] */ 32 33 typedef struct 34 { 35 float s; 36 float v[3]; 37 } QUAT; 38 39 40 float *vnew (float x, float y, float z); 41 42 void vset (float *v, float x, float y, float z); 43 44 void vcopy (float *vsrc, float *vdst); 45 46 void vprint (float *v); 47 48 void vzero (float *v); 49 50 void vnormalize (float *v); 51 52 float vlength (float *v); 53 54 void vscale (float *v, float f); 55 56 void vmult (float *src1, float *src2, float *dst); 57 58 void vadd (float *src1, float *src2, float *dst); 59 60 void vsub (float *src1, float *src2, float *dst); 61 62 float vdot (float *v1, float *v2); 63 64 void vcross (float *v1, float *v2, float *cross); 65 66 void axis_to_quaternion (float *axis, float theta, QUAT * quat); 67 68 void qmult (QUAT * q1, QUAT * q2, QUAT * dest); 69 70 void qnormalize (QUAT * q); 71 72 void quaternion_to_rotmatrix (QUAT * q, float *m); 73 74 75 class Tb_Window:public Fl_Gl_Window 76 { 77 public: 78 Tb_Window (int x, int y, int w, int h, const char *l = 0) Fl_Gl_Window(x,y,w,h,l)79 : Fl_Gl_Window (x, y, w, h, l) { 80 glutInitWindowSize (w, h); 81 glutInitWindowPosition (x, y); 82 init (); 83 } 84 Tb_Window (int w, int h, const char *l = 0) Fl_Gl_Window(w,h,l)85 : Fl_Gl_Window (w, h, l) { 86 glutInitWindowSize (w, h); 87 init (); 88 } 89 90 ~Tb_Window (); 91 92 /* Set the trackball size and mouse scale. */ 93 tbsize(float f)94 void tbsize (float f) 95 { 96 Tbsize = f; 97 } mscale(float f)98 void mscale (float f) 99 { 100 Mscale = 30.f * f; 101 } 102 103 // Set the initial trackball origin, translation and rotation. 104 105 void origin (float x, float y, float z); 106 107 void translate (float *v); 108 109 void rotate (float *axis, float theta); 110 111 // Call this from draw() to calculate the view transformation. 112 113 void calculate (float a[16]); 114 115 /* Call this when the geometry changes to force a redraw(), 116 or just call redraw(). */ 117 set_changed()118 void set_changed () 119 { 120 changed = TRUE; 121 } 122 123 /* Call idle_redraw(TRUE) to force a redraw() every time idle() is 124 called. It is initially FALSE. */ 125 idle_redraw(int b)126 void idle_redraw (int b) 127 { 128 idle_redraw_ = b; 129 } 130 // XYZ Trans; // total translation 131 132 private: 133 void init (); 134 135 int handle (int); 136 137 void idle (); 138 139 QUAT Spin; // quaternion incremental rotation 140 141 float Tbsize; // fraction of window to fill with trackball 142 143 float Mscale; // mouse movement multiplier 144 145 ulong Event; // mouse button and modifier keys 146 147 int Mx; // place where mouse is now 148 149 int My; 150 151 int Oldx; // place where mouse used to be 152 153 int Oldy; 154 155 int spinning; // if the user did a SPIN 156 157 int changed; // if the view changed 158 159 int idle_redraw_; // call redraw() in any case 160 161 void trackball (); 162 163 void window_to_tb (float mx, float my, float *x, float *y); 164 165 float tb_project_to_sphere (float, float, float); 166 }; 167 168 #endif 169