author | Don Pellegrino <don@drexel.edu> | 2009-06-13 21:51:03 (GMT) |
---|---|---|
committer | Don Pellegrino <don@drexel.edu> | 2009-06-13 21:51:03 (GMT) |
commit | f4abe0abb00210e9ef6b3277060634ba3db4f6d5 (patch) (unidiff) | |
tree | 3f245b82a6c9505b0b7741c348128d3189d02d27 | |
parent | 02ef5481e03d47c0bafcae531f8e53a9dea4a7b8 (diff) | |
download | exp005-f4abe0abb00210e9ef6b3277060634ba3db4f6d5.zip exp005-f4abe0abb00210e9ef6b3277060634ba3db4f6d5.tar.gz exp005-f4abe0abb00210e9ef6b3277060634ba3db4f6d5.tar.bz2 |
Added selection by bounding box with mouse.
-rw-r--r-- | src/controller/exp004mouse.c | 165 |
1 files changed, 98 insertions, 67 deletions
diff --git a/src/controller/exp004mouse.c b/src/controller/exp004mouse.c index 11117bf..8e8f986 100644 --- a/src/controller/exp004mouse.c +++ b/src/controller/exp004mouse.c | |||
@@ -3,87 +3,118 @@ | |||
3 | #include "../view/exp004geometry.h" | 3 | #include "../view/exp004geometry.h" |
4 | #include "../view/exp004state0.h" | 4 | #include "../view/exp004state0.h" |
5 | #include "../util/check_error.h" | 5 | #include "../util/check_error.h" |
6 | #include "../util/pick_convert.h" | ||
6 | #include <GL/glut.h> | 7 | #include <GL/glut.h> |
7 | #include <stdio.h> | 8 | #include <stdio.h> |
8 | 9 | ||
9 | #define SIZE 500 | 10 | /* |
10 | #define N 3 | 11 | * A simple alias to make the code more readable. |
12 | */ | ||
13 | #define S exp004state0 | ||
11 | 14 | ||
12 | void | 15 | void |
13 | exp004mouse (int button, int state, int x, int y) | 16 | exp004mouse (int button, int state, int x, int y) |
14 | { | 17 | { |
15 | 18 | if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) | |
16 | if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) | ||
17 | { | 19 | { |
18 | |||
19 | /* | ||
20 | * "Specify the array to be used for the returned hit records | ||
21 | * with glSelectBuffer () [Redbook]." | ||
22 | */ | ||
23 | GLuint select_buf[SIZE]; | ||
24 | glSelectBuffer (SIZE, select_buf); | ||
25 | |||
26 | /* | 20 | /* |
27 | * "Enter selection mode by specifying GL_SELECT with | 21 | * Complete a selection if one was started and not cancelled. |
28 | * glRenderMode () [Redbook]." | ||
29 | */ | 22 | */ |
30 | glRenderMode (GL_SELECT); | 23 | if (S.selecting == true) |
24 | { | ||
25 | |||
26 | /* | ||
27 | * "Specify the array to be used for the returned hit records | ||
28 | * with glSelectBuffer () [Redbook]." | ||
29 | */ | ||
30 | GLuint select_buf[ROWS]; | ||
31 | glSelectBuffer (ROWS, select_buf); | ||
32 | |||
33 | /* | ||
34 | * "Enter selection mode by specifying GL_SELECT with | ||
35 | * glRenderMode () [Redbook]." | ||
36 | */ | ||
37 | glRenderMode (GL_SELECT); | ||
38 | |||
39 | /* | ||
40 | * "Initialize the name stack using glInitNames () and glPush | ||
41 | * Names () [Redbook]." | ||
42 | */ | ||
43 | glInitNames (); | ||
44 | glPushName (0); | ||
45 | |||
46 | /* | ||
47 | * "Define the viewing volume you want to use for selection. | ||
48 | * Usually this is different from the viewing volume you | ||
49 | * originally used to draw the scene, so you probably want to | ||
50 | * save and then restore the current transformation state with | ||
51 | * glPushMatrix () and glPopMatrix () [Redbook]." | ||
52 | */ | ||
53 | glMatrixMode (GL_PROJECTION); | ||
54 | glPushMatrix (); | ||
55 | glLoadIdentity (); | ||
56 | |||
57 | GLint viewport[4]; | ||
58 | glGetIntegerv (GL_VIEWPORT, viewport); | ||
59 | |||
60 | /* | ||
61 | gluPickMatrix ((GLdouble) x, | ||
62 | (GLdouble) (viewport[3] - y), | ||
63 | N, | ||
64 | N, | ||
65 | viewport); | ||
66 | */ | ||
31 | 67 | ||
32 | /* | 68 | double c_x = 0.0; |
33 | * "Initialize the name stack using glInitNames () and glPush | 69 | double c_y = 0.0; |
34 | * Names () [Redbook]." | 70 | double w = 0.0; |
35 | */ | 71 | double h = 0.0; |
36 | glInitNames (); | 72 | pick_convert (S.select_x, S.select_y, x, y, |
37 | glPushName (0); | 73 | &c_x, &c_y, &w, &h); |
38 | |||
39 | /* | ||
40 | * "Define the viewing volume you want to use for selection. | ||
41 | * Usually this is different from the viewing volume you | ||
42 | * originally used to draw the scene, so you probably want to | ||
43 | * save and then restore the current transformation state with | ||
44 | * glPushMatrix () and glPopMatrix () [Redbook]." | ||
45 | */ | ||
46 | glMatrixMode (GL_PROJECTION); | ||
47 | glPushMatrix (); | ||
48 | glLoadIdentity (); | ||
49 | 74 | ||
50 | GLint viewport[4]; | 75 | gluPickMatrix (c_x, |
51 | glGetIntegerv (GL_VIEWPORT, viewport); | 76 | (GLdouble)viewport[3] - c_y, |
77 | w, | ||
78 | h, | ||
79 | viewport); | ||
80 | |||
81 | gluOrtho2D(S.ortho.min_x, | ||
82 | S.ortho.max_x, | ||
83 | S.ortho.min_y, | ||
84 | S.ortho.max_y); | ||
85 | |||
86 | /* | ||
87 | * "Alternately issue primitive drawing commands and commands to | ||
88 | * manipulate the name stack so that each primitive of interest | ||
89 | * has appropriate names assigned [Redbook]." | ||
90 | */ | ||
91 | exp004geometry (GL_SELECT); | ||
92 | |||
93 | glMatrixMode (GL_PROJECTION); | ||
94 | glPopMatrix (); | ||
95 | glutSwapBuffers (); | ||
96 | |||
97 | /* | ||
98 | * "Exit selection mode and process the returned selection data | ||
99 | * (the hit records) [Redbook]." | ||
100 | */ | ||
101 | GLint hits = glRenderMode (GL_RENDER); | ||
102 | check_error (__FILE__, __LINE__); | ||
103 | |||
104 | /* "process hits from selection mode rendering [Angel,2008]." */ | ||
105 | exp004processhits (hits, select_buf); | ||
106 | |||
107 | /* "normal render [Angel,2008]." */ | ||
108 | glutPostRedisplay (); | ||
109 | } | ||
52 | 110 | ||
53 | gluPickMatrix ((GLdouble) x, | 111 | } |
54 | (GLdouble) (viewport[3] - y), | ||
55 | N, | ||
56 | N, | ||
57 | viewport); | ||
58 | |||
59 | gluOrtho2D(exp004state0.ortho.min_x, | ||
60 | exp004state0.ortho.max_x, | ||
61 | exp004state0.ortho.min_y, | ||
62 | exp004state0.ortho.max_y); | ||
63 | |||
64 | /* | ||
65 | * "Alternately issue primitive drawing commands and commands to | ||
66 | * manipulate the name stack so that each primitive of interest | ||
67 | * has appropriate names assigned [Redbook]." | ||
68 | */ | ||
69 | exp004geometry (GL_SELECT); | ||
70 | |||
71 | glMatrixMode (GL_PROJECTION); | ||
72 | glPopMatrix (); | ||
73 | glutSwapBuffers (); | ||
74 | 112 | ||
75 | /* | 113 | if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) |
76 | * "Exit selection mode and process the returned selection data | 114 | { |
77 | * (the hit records) [Redbook]." | 115 | S.selecting = true; |
78 | */ | 116 | S.select_x = x; |
79 | GLint hits = glRenderMode (GL_RENDER); | 117 | S.select_y = y; |
80 | check_error (__FILE__, __LINE__); | ||
81 | |||
82 | /* "process hits from selection mode rendering [Angel,2008]." */ | ||
83 | exp004processhits (hits, select_buf); | ||
84 | |||
85 | /* "normal render [Angel,2008]." */ | ||
86 | glutPostRedisplay (); | ||
87 | } | 118 | } |
88 | 119 | ||
89 | return; | 120 | return; |