Direct Graphical Models  v.1.7.0
CameraControl.cpp
1 #include "CameraControl.h"
2 
3 namespace DirectGraphicalModels { namespace vis
4 {
5  // Constructor
6  CCameraControl::CCameraControl(GLFWwindow * window, float theta, float phi, float radius, float turnSpeed, float scrollSpeed, float panSpeed)
7  : CTrackballCamera(theta, phi, radius)
8  , m_mouseEvent(mouseEvent::none)
9  , m_turnSpeed(turnSpeed)
10  , m_scrollSpeed(scrollSpeed)
11  , m_panSpeed(panSpeed)
12  {
13  glfwSetWindowUserPointer(window, this);
14  glfwSetMouseButtonCallback(window, [](GLFWwindow *window, int button, int action, int mods) {
15  static_cast<CCameraControl *>(glfwGetWindowUserPointer(window))->mouseButtonCallback(button, action, mods);
16  });
17  glfwSetCursorPosCallback(window, [](GLFWwindow *window, double x, double y) {
18  static_cast<CCameraControl *>(glfwGetWindowUserPointer(window))->cursorPosCallback(x, y);
19  });
20  glfwSetScrollCallback(window, [](GLFWwindow *window, double x, double y) {
21  static_cast<CCameraControl *>(glfwGetWindowUserPointer(window))->scrollCallback(x, y);
22  });
23  }
24 
25  // ------------------------------------- Callbacks -------------------------------------
26  void CCameraControl::mouseButtonCallback(int button, int action, int mods)
27  {
28  if (button == GLFW_MOUSE_BUTTON_LEFT) {
29  if (action == GLFW_PRESS) m_mouseEvent = mouseEvent::start_turn;
30  if (action == GLFW_RELEASE) m_mouseEvent = mouseEvent::none;
31  }
32  if (button == GLFW_MOUSE_BUTTON_MIDDLE) {
33  if (action == GLFW_PRESS) m_mouseEvent = mouseEvent::start_pan;
34  if (action == GLFW_RELEASE) m_mouseEvent = mouseEvent::none;
35  }
36  }
37 
38  void CCameraControl::cursorPosCallback(double x, double y)
39  {
40  if (m_mouseEvent == mouseEvent::none) return;
41 
42  float dx = m_mouseLastPos.x - static_cast<float>(x); // dTheta
43  float dy = m_mouseLastPos.y - static_cast<float>(y); // dPhi
44  m_mouseLastPos.x = static_cast<float>(x);
45  m_mouseLastPos.y = static_cast<float>(y);
46 
47  switch (m_mouseEvent) {
49  case mouseEvent::turn: rotate(dx * m_turnSpeed, dy * m_turnSpeed); break;
51  case mouseEvent::pan: pan(-dx * m_panSpeed, dy * m_panSpeed); break;
52  case mouseEvent::none: break;
53  }
54  }
55 
56  void CCameraControl::scrollCallback(double x, double y)
57  {
58  zoom(static_cast<float>(y) * m_scrollSpeed);
59  }
60 } }
void cursorPosCallback(double x, double y)
Trackball camera control class.
Definition: CameraControl.h:16
CCameraControl(GLFWwindow *window, float theta=0.0f, float phi=-glm::pi< float >()/2, float radius=2.4f, float turnSpeed=0.004f, float scrollSpeed=0.33f, float panSpeed=0.01f)
Constructor.
void mouseButtonCallback(int button, int action, int mods)