sKit  0.0.9
camera.cpp
Go to the documentation of this file.
1 #include "camera.hpp"
2 
3 #include <glm/ext/matrix_clip_space.hpp>
4 #include <glm/ext/matrix_transform.hpp>
5 
6 sKit::Camera::Camera(float width, float height) {
7  projectionMatrix_ =
8  glm::perspective<float>(glm::radians(fov_), width / height, near_, far_);
9 
10  viewMatrix_ = glm::lookAt(pos_, viewOrigin_, up_);
11 }
12 
13 auto sKit::Camera::getProjectionMatrix() -> glm::mat4 {
14  return projectionMatrix_;
15 }
16 
17 auto sKit::Camera::getViewMatrix() -> glm::mat4 { return viewMatrix_; }
18 
19 auto sKit::Camera::getPos() -> glm::vec3 { return pos_; };
20 
21 auto sKit::Camera::setPos(glm::vec3 pos) -> void {
22  pos_ = pos;
23  viewMatrix_ = glm::lookAt(pos_, viewOrigin_, up_);
24 }
25 
26 auto sKit::Camera::update(SDL_Event *event) -> void {
27  if (event->type == SDL_EVENT_MOUSE_WHEEL) {
28  auto newPos = pos_ + (viewOrigin_ - pos_) * event->wheel.y * 0.1f;
29  setPos(newPos);
30  }
31  if (event->type == SDL_EVENT_MOUSE_MOTION) {
32  glm::vec2 mpos;
33  auto flags = SDL_GetMouseState(&mpos.x, &mpos.y);
34  glm::vec2 dPos = (mpos - lastMousePos_);
35  if (flags == SDL_BUTTON_LMASK) { // drag
36  auto roty =
37  glm::rotate(glm::mat4(1), glm::radians(-dPos.x), glm::vec3(0, 1, 0));
38  auto rot = glm::rotate(roty, glm::radians(-dPos.y), glm::vec3(1, 0, 0));
39  setPos(rot * glm::vec4(pos_, 1));
40  }
41  SDL_GetMouseState(&lastMousePos_.x, &lastMousePos_.y);
42  }
43 };
sKit::Camera::Camera
Camera()=default
sKit::Camera::update
auto update(SDL_Event *event) -> void
Definition: camera.cpp:26
camera.hpp
sKit::Camera::setPos
auto setPos(glm::vec3 pos) -> void
Definition: camera.cpp:21
sKit::Camera::getProjectionMatrix
auto getProjectionMatrix() -> glm::mat4
Definition: camera.cpp:13
sKit::Camera::getPos
auto getPos() -> glm::vec3
Definition: camera.cpp:19
sKit::Camera::getViewMatrix
auto getViewMatrix() -> glm::mat4
Definition: camera.cpp:17