pgl
PrimitiveOpenGL3Dprimitivelibrary
controller.h
Go to the documentation of this file.
1 
22 #ifndef PGL_CONTROLLER_H_
23 #define PGL_CONTROLLER_H_
24 
25 #include "pgl.h"
26 
27 namespace pgl {
28 
31 {
32  public:
33  Vector3 center;
34  double azimuth, elevation, distance;
35 
36  protected:
37  Vector3 old_center_;
38  double old_azimuth_, old_elevation_, old_distance_, old_xpos_, old_ypos_;
39 
40  enum {modeNone, modeAngle, modeDistance, modeCenter} mode_;
41 
42  public:
43  OrbitController(Camera *_camera) : Controller(_camera), center{0, 0, 0}, azimuth(60*M_PI/180), elevation(35*M_PI/180), distance(2), mode_(modeNone)
44  {
45  apply();
46  }
47 
48  void view(double _azimuth, double _elevation, double _distance)
49  {
50  azimuth = _azimuth;
51  elevation = _elevation;
52  distance = _distance;
53 
54  apply();
55  }
56 
57  void click(int button, int action, int /*mods*/, double xpos, double ypos)
58  {
59  if (action == PGL_PRESS)
60  {
61  old_xpos_ = xpos;
62  old_ypos_ = ypos;
63  old_center_ = center;
64  old_azimuth_ = azimuth;
65  old_elevation_ = elevation;
66  old_distance_ = distance;
67 
68  if (button == PGL_MOUSE_BUTTON_LEFT) mode_ = modeAngle;
69  if (button == PGL_MOUSE_BUTTON_MIDDLE) mode_ = modeDistance;
70  if (button == PGL_MOUSE_BUTTON_RIGHT) mode_ = modeCenter;
71  }
72  else
73  mode_ = modeNone;
74  }
75 
76  void scroll(double /*xoffset*/, double yoffset)
77  {
78  distance *= pow(sqrt(2), -yoffset);
79  apply();
80  }
81 
82  void motion(double xpos, double ypos)
83  {
84  switch (mode_)
85  {
86  case modeAngle:
87  azimuth = old_azimuth_ - 0.005*(xpos-old_xpos_);
88  elevation = old_elevation_ + 0.005*(ypos-old_ypos_);
89  break;
90  case modeDistance:
91  distance = old_distance_ + 0.02*(ypos-old_ypos_);
92  break;
93  case modeCenter:
94  center = old_center_ + Rotation({0, 0, -azimuth})*(Vector3({old_xpos_-xpos, ypos-old_ypos_, 0})*0.02);
95  break;
96  default:
97  break;
98  }
99 
100  apply();
101  }
102 
103  void apply()
104  {
105  camera->transform = Transform({-0.5*M_PI+elevation, 0, 0}, {0, 0, -distance})*Rotation({0, 0, -azimuth})*Translation(-center);
106  }
107 };
108 
109 }
110 
111 #endif // PGL_CONTROLLER_H_
Camera * camera
Camera to move.
Definition: pgl.h:277
Camera Controller which orbits around a center.
Definition: controller.h:30
Definition: controller.h:27
Transform with zero translation.
Definition: math.h:282
Transform transform
Camera position.
Definition: pgl.h:227
Camera controller.
Definition: pgl.h:274
void scroll(double, double yoffset)
Scroll wheel handler.
Definition: controller.h:76
3-component vector.
Definition: math.h:43
void click(int button, int action, int, double xpos, double ypos)
Click handler.
Definition: controller.h:57
void motion(double xpos, double ypos)
Mouse motion handler.
Definition: controller.h:82
Transform with identity rotation.
Definition: math.h:289
Homogeneous coordinate transform.
Definition: math.h:164
Defines camera position and frustum.
Definition: pgl.h:224