From 8ecdebf950d7d2458bc28c0bae2ad590f183e148 Mon Sep 17 00:00:00 2001 From: Don Pellegrino Date: Fri, 14 Aug 2009 21:17:17 +0000 Subject: Added zooming with the mouse wheel. The mouse wheel will zoom in or out by 10% of the screen pixels with the focus as the center of the window. --- diff --git a/src/Makefile.am b/src/Makefile.am index 38626c7..3bb95ee 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -7,6 +7,8 @@ exp004viz_SOURCES = \ controller/exp004processhits.c \ controller/exp004reshape.c \ controller/keyboard.c \ + controller/mousewheel.c \ + controller/performzoom.c \ controller/selection_from_db.c \ controller/selsave.c \ controller/set_ortho.c \ @@ -36,6 +38,8 @@ noinst_HEADERS = \ controller/exp004processhits.h \ controller/exp004reshape.h \ controller/keyboard.h \ + controller/mousewheel.h \ + controller/performzoom.h \ controller/selection_from_db.h \ controller/selsave.h \ controller/set_ortho.h \ diff --git a/src/controller/exp004mouse.c b/src/controller/exp004mouse.c index 17ff19c..cf09bef 100644 --- a/src/controller/exp004mouse.c +++ b/src/controller/exp004mouse.c @@ -1,6 +1,7 @@ #include "exp004mouse.h" #include "exp004processhits.h" #include "exp004reshape.h" +#include "performzoom.h" #include "set_ortho.h" #include "../view/exp004geometry.h" #include "../view/exp004state0.h" @@ -28,53 +29,19 @@ exp004mouse (int button, int state, int x, int y) if (x == S.selection.x || y == S.selection.y) return; - /* - * Convert the selection boundary from window coordinates to - * world coordinates. - */ - glMatrixMode (GL_MODELVIEW); - glLoadIdentity (); - GLdouble model[16]; - glGetDoublev (GL_MODELVIEW_MATRIX, model); - GLdouble projection[16]; - glGetDoublev (GL_PROJECTION_MATRIX, projection); GLint viewport[4]; glGetIntegerv (GL_VIEWPORT, viewport); - check_error (__FILE__, __LINE__); - - GLdouble start_position[3]; - gluUnProject (S.selection.x, - viewport[3] - S.selection.y, - 0, - model, - projection, - viewport, - &start_position[0], - &start_position[1], &start_position[2]); - - check_error (__FILE__, __LINE__); - - GLdouble end_position[3]; - gluUnProject (x, - viewport[3] - y, - 0, - model, - projection, - viewport, - &end_position[0], &end_position[1], &end_position[2]); - - check_error (__FILE__, __LINE__); - - S.zoom.active = true; - S.zoom.coords[0] = fmin (start_position[0], end_position[0]); - S.zoom.coords[1] = fmax (start_position[0], end_position[0]); - S.zoom.coords[2] = fmin (start_position[1], end_position[1]); - S.zoom.coords[3] = fmax (start_position[1], end_position[1]); - - exp004reshape (S.viewport.w, S.viewport.h); - - glutPostRedisplay (); + printf ("Zooming to: %i, %i, %i, %i\n", + S.selection.x, + viewport[3] - S.selection.y, + x, + viewport[3] - y); + + performzoom (S.selection.x, + viewport[3] - S.selection.y, + x, + viewport[3] - y); } /* diff --git a/src/controller/mousewheel.c b/src/controller/mousewheel.c new file mode 100644 index 0000000..c9ac742 --- a/dev/null +++ b/src/controller/mousewheel.c @@ -0,0 +1,45 @@ +#include "mousewheel.h" +#include "performzoom.h" +#include + +void +mousewheel (int button, int dir, int x, int y) +{ + /* + * Get the current coordinates, substract some fixed amount and + * then perform the zoom. + */ + GLint viewport[4]; + glGetIntegerv (GL_VIEWPORT, viewport); + + /* + * The step size could be either a fixed number of pixels or a percentage. + */ + // int step = 5; + int step = (viewport[3] - viewport[1]) * 0.10; + + /* + * Not that the focus of the zoom is currently the center of the + * window but could alternatively be the mouse pointer's position. + */ + + // Zoom in + if (dir > 0) + { + performzoom (step, + step, + viewport[3] - step, + viewport[3] - step); + } + + // Zoom out + else + { + performzoom (-step, + -step, + viewport[3] + step, + viewport[3] + step); + } + + return; +} diff --git a/src/controller/mousewheel.h b/src/controller/mousewheel.h new file mode 100644 index 0000000..a3944d0 --- a/dev/null +++ b/src/controller/mousewheel.h @@ -0,0 +1,11 @@ +#ifndef MOUSEWHEEL_H +#define MOUSEWHEEL_H + +/* + * GLUT callback to be called whenever the scroll wheel is scrolled. + * Based on code from Ashwin at + * http://stackoverflow.com/questions/14378/using-the-mouse-scrollwheel-in-glut/14444. + */ +void mousewheel (int, int, int, int); + +#endif // MOUSEWHEEL_H diff --git a/src/controller/performzoom.c b/src/controller/performzoom.c new file mode 100644 index 0000000..2fed1d2 --- a/dev/null +++ b/src/controller/performzoom.c @@ -0,0 +1,65 @@ +#include "performzoom.h" +#include "exp004reshape.h" +#include "../util/check_error.h" +#include "../view/exp004state0.h" +#include +#include + +/* + * A simple alias to make the code more readable. + */ +#define S exp004state0 + +void +performzoom (int x1, int y1, int x2, int y2) +{ + /* + * Convert the selection boundary from window coordinates to + * world coordinates. + */ + glMatrixMode (GL_MODELVIEW); + glLoadIdentity (); + GLdouble model[16]; + glGetDoublev (GL_MODELVIEW_MATRIX, model); + GLdouble projection[16]; + glGetDoublev (GL_PROJECTION_MATRIX, projection); + GLint viewport[4]; + glGetIntegerv (GL_VIEWPORT, viewport); + + check_error (__FILE__, __LINE__); + + GLdouble start_position[3]; + gluUnProject (x1, + y1, + 0, + model, + projection, + viewport, + &start_position[0], + &start_position[1], &start_position[2]); + + check_error (__FILE__, __LINE__); + + GLdouble end_position[3]; + gluUnProject (x2, + y2, + 0, + model, + projection, + viewport, + &end_position[0], &end_position[1], &end_position[2]); + + check_error (__FILE__, __LINE__); + + S.zoom.active = true; + S.zoom.coords[0] = fmin (start_position[0], end_position[0]); + S.zoom.coords[1] = fmax (start_position[0], end_position[0]); + S.zoom.coords[2] = fmin (start_position[1], end_position[1]); + S.zoom.coords[3] = fmax (start_position[1], end_position[1]); + + exp004reshape (S.viewport.w, S.viewport.h); + + glutPostRedisplay (); + + return; +} diff --git a/src/controller/performzoom.h b/src/controller/performzoom.h new file mode 100644 index 0000000..323cc20 --- a/dev/null +++ b/src/controller/performzoom.h @@ -0,0 +1,9 @@ +#ifndef PERFORMZOOM_H +#define PERFORMZOOM_H + +/* + * Perform a zoom operation. + */ +void performzoom (int x1, int y1, int x2, int y2); + +#endif // PERFORMZOOM_H diff --git a/src/view/exp004view.c b/src/view/exp004view.c index 92cb56e..434c016 100644 --- a/src/view/exp004view.c +++ b/src/view/exp004view.c @@ -2,10 +2,13 @@ #include "../controller/exp004mouse.h" #include "../controller/exp004reshape.h" #include "../controller/keyboard.h" +#include "../controller/mousewheel.h" #include "../db/dbconnect.h" #include "exp004init.h" #include "exp004state0.h" #include "exp004view.h" +#include +#include #include void @@ -45,6 +48,7 @@ exp004view (void) glutDisplayFunc (exp004display); glutKeyboardFunc (keyboard); glutMouseFunc (exp004mouse); + glutMouseWheelFunc (mousewheel); glutReshapeFunc (exp004reshape); return; -- cgit v0.8.3.1-22-g547a