//=========================================================================== // SISL - SINTEF Spline Library, version 4.5.0. // Definition and interrogation of NURBS curves and surfaces. // // Copyright (C) 2000-2005, 2010 SINTEF ICT, Applied Mathematics, Norway. // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation version 2 of the License. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., // 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. // // Contact information: E-mail: tor.dokken@sintef.no // SINTEF ICT, Department of Applied Mathematics, // P.O. Box 124 Blindern, // 0314 Oslo, Norway. // // Other licenses are also available for this software, notably licenses // for: // - Building commercial software. // - Building software whose source code you wish to keep private. //=========================================================================== 00033 #ifndef MICROSOFT 00034 # include <unistd.h> 00035 #endif 00036 00037 #ifndef PI 00038 # define PI 3.1415926536 00039 #endif 00040 #include "mouse.h" 00041 #include "transfutils.h" 00042 #include "aux2.h" 00043 00044 // 00045 // All casting to double in calls to sqrt are just because of aCC on tor. 00046 // Doing this by using temporary variables instead... 00047 // 00048 00049 00050 00051 00052 00053 00054 // 00055 // 990831: These are used for storage of flags, states, old mouse- 00056 // positions etc. 00057 // 00058 GLboolean allow_zrot=GL_TRUE; 00059 int last_x=0, last_y=0, last_x0=0, last_y0=0; 00060 bool mouse_movement=false; // Mouse-movement routines set this. 00061 00062 00063 00064 00065 00066 00067 // 00068 // 040326: An attempt to make a "draw cursor into framebuffer" subroutine, 00069 // in order to get the cursor into movie sequences produced with 00070 // 'sfviewer1'. 00071 // 00072 00073 float curx=0.0, cury=0.0, curz=0.0; 00074 00075 void draw_cursor(int x, int y) 00076 { 00077 GLint viewport[4]; 00078 GLdouble modelview[16]; 00079 GLdouble projection[16]; 00080 GLfloat winX, winY, winZ; 00081 GLdouble posX, posY, posZ; 00082 00083 glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); 00084 glGetDoublev( GL_PROJECTION_MATRIX, projection ); 00085 glGetIntegerv( GL_VIEWPORT, viewport ); 00086 00087 winX = (float)x; 00088 winY = (float)viewport[3] - (float)y; 00089 glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ ); 00090 00091 gluUnProject(winX, winY, winZ, 00092 modelview, projection, viewport, &posX, &posY, &posZ); 00093 00094 curx=posX; 00095 cury=posY; 00096 curz=posZ; 00097 } 00098 00099 00100 00101 00102 00103 00104 int transversal_rotation(int x, int y, int last_xx, int last_yy) 00105 { 00106 int viewp[4]; 00107 00108 glGetIntegerv(GL_VIEWPORT, viewp); 00109 if ((fabs(((x-last_xx)*(x-(viewp[0]+viewp[2])*0.5)+ 00110 (y-last_yy)*(y-(viewp[1]+viewp[3])*0.5))/ 00111 sqrt((double)(SQR(x-last_xx)+SQR(y-last_yy)))/ 00112 sqrt((double)(SQR(x-(viewp[0]+viewp[2])*0.5)+ 00113 SQR(y-(viewp[1]+viewp[3])*0.5)))) > 00114 cos(50.0/180.0*PI)) || 00115 (sqrt((x-(viewp[0]+viewp[2])*0.5)*(x-(viewp[0]+viewp[2])*0.5)+ 00116 (y-(viewp[1]+viewp[3])*0.5)*(y-(viewp[1]+viewp[3])*0.5)) < 00117 0.2*sqrt((double)SQR(viewp[2]-viewp[0])+SQR(viewp[3]-viewp[1]))) || 00118 (!allow_zrot)) 00119 { 00120 /* puts("radielt");*/ 00121 return 0; 00122 } 00123 else 00124 { 00125 /* puts("transversalt");*/ 00126 return 1; 00127 } 00128 // draw_cursor(x, y); 00129 } 00130 00131 00132 00133 00134 00135 00136 // 00137 // 990831: Would have liked to send other parameters too, but 00138 // glut won't allow that... 00139 // 00140 void MouseRotate(int x, int y) 00141 { 00142 int viewp[4]; 00143 //const double sensitiveness=0.1; 00144 // 030129: Reducing sensitivity slightly. 00145 //const double sensitiveness=0.05; 00146 // 030302: Reducing sensitivity slightly more, for demo. 00147 const double sensitiveness=0.02; 00148 00149 mouse_movement = mouse_movement || ((x!=last_x) || (y!=last_y)); 00150 00151 //puts("MouseRotate"); 00152 glGetIntegerv(GL_VIEWPORT, viewp); 00153 00154 draw_cursor(x, y); 00155 00156 if (!transversal_rotation(x, y, last_x, last_y)) 00157 rotate((x-last_x)*sensitiveness, (y-last_y)*sensitiveness, 0.0); 00158 else 00159 { 00160 int s; 00161 00162 if (atan2(y-(viewp[1]+viewp[3])*0.5, 00163 x-(viewp[0]+viewp[2])*0.5)> 00164 atan2(last_y-(viewp[1]+viewp[3])*0.5, 00165 last_x-(viewp[0]+viewp[2])*0.5)) 00166 s=-1; 00167 else 00168 s=1; 00169 00170 rotate(0.0, 0.0, 00171 s*sqrt((double)(SQR(x-last_x)+SQR(y-last_y)))*sensitiveness); 00172 } 00173 00174 last_x0=last_x; 00175 last_y0=last_y; 00176 last_x=x; 00177 last_y=y; 00178 } 00179 00180 00181 00182 00183 00184 00185 void MouseZoom(int x, int y) 00186 { 00187 scale(1.0+(y-last_y)*0.01, 1.0+(y-last_y)*0.01, 1.0+(y-last_y)*0.01); 00188 last_x=x; 00189 last_y=y; 00190 draw_cursor(x, y); 00191 } 00192 00193 00194 00195 00196 00197 00198 void MouseTranslate(int x, int y) 00199 { 00200 translate(xtrans+(x-last_x)*0.01, ytrans-(y-last_y)*0.01, ztrans); 00201 last_x=x; 00202 last_y=y; 00203 draw_cursor(x, y); 00204 } 00205 00206 00207 00208 00209 00210 00211 void Mouse(int butt, int state, int x, int y) 00212 { 00213 // 00214 // 990824: Forsoek paa aa emulere midterste tast vha. begge de to 00215 // paa PC... 00216 // 00217 #ifdef MIDDLE_EMU 00218 static int left_down=0, right_down=0; 00219 #endif 00220 00221 /* 00222 printf("butt=%d state=%d x=%d y=%d " 00223 "last_x=%d last_y=%d last_x=%d last_y=%d\n", 00224 butt, state, x, y, last_x, last_y, last_x0, last_y0); 00225 #ifdef MIDDLE_EMU 00226 printf("Left down=%d, right down=%d\n", left_down, right_down); 00227 #endif 00228 */ 00229 00230 if (state==GLUT_DOWN) 00231 { 00232 /* A fresh start, all "last" info to be discarded... */ 00233 last_x=x; 00234 last_y=y; 00235 last_x0=x; 00236 last_y0=y; 00237 00238 #ifdef MIDDLE_EMU 00239 // 00240 // 990824: Forsoek paa aa emulere midterste tast vha. begge de to 00241 // paa PC... 00242 // 00243 if (butt==GLUT_RIGHT_BUTTON) 00244 { 00245 if (left_down) 00246 butt=GLUT_MIDDLE_BUTTON; 00247 right_down=1; 00248 } 00249 else 00250 if (butt==GLUT_LEFT_BUTTON) 00251 { 00252 if (right_down) 00253 butt=GLUT_MIDDLE_BUTTON; 00254 left_down=1; 00255 } 00256 00257 /* if (((left_down) && (butt==GLUT_RIGHT_BUTTON)) || 00258 ((right_down) && (butt==GLUT_LEFT_BUTTON))) 00259 { 00260 butt=GLUT_MIDDLE_BUTTON; 00261 left_down=right_down=1; 00262 }*/ 00263 #endif 00264 00265 switch (butt) 00266 { 00267 case GLUT_LEFT_BUTTON: 00268 xrot=0; 00269 yrot=0; 00270 zrot=0; 00271 #ifndef QTMODE 00272 glutMotionFunc(MouseRotate); 00273 #else 00274 // 030917: setting global variable so that new qt event handler 00275 // knows what to do. 00276 extern bool enableMouseRotation; 00277 enableMouseRotation=true; 00278 #endif 00279 break; 00280 case GLUT_MIDDLE_BUTTON: 00281 #ifndef QTMODE 00282 glutMotionFunc(MouseZoom); 00283 #else 00284 // 030918: setting global variable so that new qt event handler 00285 // knows what to do. 00286 extern bool enableMouseZoom; 00287 enableMouseZoom=true; 00288 #endif 00289 break; 00290 case GLUT_RIGHT_BUTTON: 00291 #ifndef QTMODE 00292 glutMotionFunc(MouseTranslate); 00293 #else 00294 // 030918: setting global variable so that new qt event handler 00295 // knows what to do. 00296 extern bool enableMouseTranslate; 00297 enableMouseTranslate=true; 00298 #endif 00299 break; 00300 } 00301 } 00302 00303 if (state==GLUT_UP) 00304 { 00305 00306 #ifdef MIDDLE_EMU 00307 if (butt==GLUT_LEFT_BUTTON) 00308 left_down=0; 00309 if (butt==GLUT_RIGHT_BUTTON) 00310 right_down=0; 00311 #endif 00312 #ifdef QTMODE 00313 extern bool enableMouseRotation, enableMouseZoom, enableMouseTranslate; 00314 enableMouseRotation=false; 00315 enableMouseZoom=false; 00316 enableMouseTranslate=false; 00317 #endif 00318 00319 // 00320 // 020410: Introduced 'spin_speed', was 1 before, setting to 2. 00321 // There seems to be some confusion here as to whether 00322 // the ?rot-variables are ints or floats?! 00323 // 040220: Reducing to 1, seems to be very unwieldy with the new 00324 // GF4 card...(?!) 00325 // 040505: Nå med fx5900, 1.0 ser ut til å være alt for mye for 00326 // endel bview2-greier. 00327 // 00328 //const double spin_speed=2.0; 00329 //const double spin_speed=1.0; 00330 const double spin_speed=0.1; 00331 00332 // 00333 // 991123: This was a stupid thing to do! We don't need to 00334 // disable this one! It's only active while the button 00335 // is pressed down, anyway! 00336 // 00337 // glutMotionFunc(MouseDummy); 00338 if ((butt==GLUT_LEFT_BUTTON) && 00339 ((x-last_x0)*(x-last_x0)+(y-last_y0)*(y-last_y0)>4)) 00340 { 00341 if (!transversal_rotation(x, y, last_x0, last_y0)) 00342 { 00343 xrot=(y-last_y0)*spin_speed; 00344 yrot=(x-last_x0)*spin_speed; 00345 /* zrot=0;*/ 00346 } 00347 else 00348 { 00349 int s; 00350 int viewp[4]; 00351 00352 glGetIntegerv(GL_VIEWPORT, viewp); 00353 if (atan2(y-(viewp[1]+viewp[3])*0.5, 00354 x-(viewp[0]+viewp[2])*0.5)> 00355 atan2(last_y0-(viewp[1]+viewp[3])*0.5, 00356 last_x0-(viewp[0]+viewp[2])*0.5)) 00357 s=-1; 00358 else 00359 s=1; 00360 00361 /* xrot=0; 00362 yrot=0;*/ 00363 zrot=(s*(int)(sqrt((double)(SQR(y-last_y0)+SQR(x-last_x0))))* 00364 spin_speed); 00365 } 00366 } 00367 } 00368 00369 /* 00370 printf("butt=%d state=%d x=%d y=%d " 00371 "last_x=%d last_y=%d last_x=%d last_y=%d\n", 00372 butt, state, x, y, last_x, last_y, last_x0, last_y0); 00373 #ifdef MIDDLE_EMU 00374 // printf("Left down=%d, right down=%d\n", left_down, right_down); 00375 #endif 00376 puts(" "); 00377 */ 00378 draw_cursor(x, y); 00379 }
Generated on Tue Sep 21 17:28:53 2010 for SISL by  doxygen 1.6.3