-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteract.cpp
More file actions
196 lines (154 loc) · 5.97 KB
/
interact.cpp
File metadata and controls
196 lines (154 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
////////////////////////////////////////////////////////////////////////
// All keyboard, mouse, and other interactions are implemented here.
// The single entry point, InitInteraction, sets up GLFW callbacks for
// various events that an interactive graphics program needs to
// handle.
//
#include "framework.h"
extern Scene scene; // Declared in framework.cpp, but used here.
// Some globals used for mouse handling.
double mouseX, mouseY;
bool shifted = false;
bool leftDown = false;
bool middleDown = false;
bool rightDown = false;
float spin, tilt, ry, front, back;
////////////////////////////////////////////////////////////////////////
// Function called to exit
void Quit(void *clientData)
{
glfwSetWindowShouldClose(scene.window, 1);
}
std::string ACTION[3] = {"Release", "Press", "Repeat"};
////////////////////////////////////////////////////////////////////////
// Called for keyboard actions.
void Keyboard(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (ImGui::GetIO().WantCaptureKeyboard) return;
if (action == GLFW_REPEAT) return; // Because keyboard autorepeat is evil.
printf("Keyboard %c(%d); S%d %s M%d\n", key, key, scancode, ACTION[action].c_str(), mods);
fflush(stdout);
// Track SHIFT/NO-SHIFT transitions. (The mods parameter should do this, but doesn't.)
if (key==GLFW_KEY_LEFT_SHIFT || key==GLFW_KEY_RIGHT_SHIFT)
shifted = !shifted;
// @@ Catch any key DOWN-transition you'd like in the following
// switch statement. Record any change of state in variables in
// the scene object.
if (action == GLFW_PRESS) {
switch(key) {
case GLFW_KEY_W:
scene.w_down = true;
break;
case GLFW_KEY_S:
scene.s_down = true;
break;
case GLFW_KEY_D:
scene.d_down = true;
break;
case GLFW_KEY_A:
scene.a_down = true;
break;
case GLFW_KEY_TAB:
scene.transformation_mode =! scene.transformation_mode;
break;
case GLFW_KEY_0: case GLFW_KEY_1: case GLFW_KEY_2: case GLFW_KEY_3: case GLFW_KEY_4:
case GLFW_KEY_5: case GLFW_KEY_6: case GLFW_KEY_7: case GLFW_KEY_8: case GLFW_KEY_9:
scene.mode = key-GLFW_KEY_0;
break;
case GLFW_KEY_ESCAPE: case GLFW_KEY_Q: // Escape and 'q' keys quit the application
exit(0); } }
else if (action == GLFW_RELEASE) {
switch (key) {
case GLFW_KEY_W:
scene.w_down = false;
break;
case GLFW_KEY_S:
scene.s_down = false;
break;
case GLFW_KEY_D:
scene.d_down = false;
break;
case GLFW_KEY_A:
scene.a_down = false;
break;
}
}
// @@ Catch any key UP-transitions you want here. Record any
// change of state in variables in the scene object.
fflush(stdout);
}
////////////////////////////////////////////////////////////////////////
// Called when a mouse button changes state.
void MouseButton(GLFWwindow* window, int button, int action, int mods)
{
if (ImGui::GetIO().WantCaptureMouse) return;
glfwGetCursorPos(window, &mouseX, &mouseY);
printf("MouseButton %d %d %d %f %f\n", button, action, mods, mouseX, mouseY);
// @@ Catch any mouse button UP-or-DOWN-transitions you want here.
// Record any change of state in variables in the scene object.
if (button == GLFW_MOUSE_BUTTON_LEFT) {
leftDown = (action == GLFW_PRESS); }
else if (button == GLFW_MOUSE_BUTTON_MIDDLE) {
middleDown = (action == GLFW_PRESS); }
else if (button == GLFW_MOUSE_BUTTON_RIGHT) {
rightDown = (action == GLFW_PRESS); }
}
////////////////////////////////////////////////////////////////////////
// Called by GLFW when a mouse moves (while a button is down)
void MouseMotion(GLFWwindow* window, double x, double y)
{
if (ImGui::GetIO().WantCaptureMouse) return;
// @@ Catch any mouse movement that occurs while any button is
// down. It is not reported here *which* button is down, but you
// should have recorded the DOWN-transition in a previous call to
// MouseButton.
// @@ The x,y parameters to this function record the position of
// the mouse. Usually you want to know only how much it has moved
// since the last call. Hence the following two lines (and this
// procedure's last two lines) calculate the change in the mouse
// position.
// Calculate the change in the mouse position
int dx = x-mouseX;
int dy = y-mouseY;
// @@ Please don't disable this shifted-left-button code.
if (leftDown && shifted) { // Rotate light position
scene.lightSpin += dx/3.0;
scene.lightTilt -= dy/3.0;
}
else if (leftDown) {
scene.spin += dx/3.0;
scene.tilt += dy/3.0;
if (scene.tilt > 90.0) scene.tilt = 90.0;
if (scene.tilt < -90.0) scene.tilt = -90.0;
}
if (middleDown) { }
if (rightDown) { }
// Record this position
mouseX = x;
mouseY = y;
}
void Scroll(GLFWwindow* window, double x, double y)
{
if (ImGui::GetIO().WantCaptureMouse) return;
printf("Scroll %f %f\n", x, y);
// Figure out the mouse action, and handle accordingly
// @@ Please don't disable this shifted-scroll-wheel code.
if (y>0.0 && shifted) { // Scroll light in
scene.lightDist = pow(scene.lightDist, 1.0f/1.02f); }
// @@ Please don't disable this shifted-scroll-wheel code.
else if (y<0.0 && shifted) { // Scroll light out
scene.lightDist = pow(scene.lightDist, 1.02f); }
else if (y>0.0) {
scene.zoom = pow(scene.zoom, 1.0f/1.02f);
}
else if (y<0.0) {
scene.zoom = pow(scene.zoom, 1.02f);
}
}
void InitInteraction()
{
glfwSetKeyCallback(scene.window, Keyboard);
glfwSetMouseButtonCallback(scene.window, MouseButton);
glfwSetCursorPosCallback(scene.window, MouseMotion);
glfwSetScrollCallback(scene.window, Scroll);
}