Commit 7105c87d authored by Sadman Kazi's avatar Sadman Kazi 🎸

Add mouse controls for camera

parent d8191ba9
Pipeline #92 passed with stage
in 2 minutes and 34 seconds
......@@ -4,6 +4,7 @@
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtx/rotate_vector.hpp>
#include <glm/geometric.hpp> // glm::cross
#include "input.h"
#define SPEED 0.001f
......@@ -16,12 +17,14 @@
class Camera
{
public:
Camera(const glm::vec3& pos, float fov, float aspect, float zNear, float zFar) : m_position(pos) {
Camera(const glm::f32vec3& pos, float fov, float aspect, float zNear, float zFar) : m_position(pos) {
m_perspective = glm::perspective(fov, aspect, zNear, zFar);
//assuming that the vertical axis is y
m_forward = glm::vec3(0, 0, 1);
m_up = glm::vec3(0, 1, 0);
m_forward = glm::f32vec3(0, 0, 1);
m_up = glm::f32vec3(0, 1, 0);
m_mouse_pos = Input::getMousePos();
}
inline glm::mat4 getViewProjection() const {
......@@ -29,27 +32,36 @@ public:
}
inline void input() {
// Check if the mouse moved
glm::vec2 new_mouse_pos = Input::getMousePos();
int delta_x = new_mouse_pos.x - m_mouse_pos.x;
if (DEBUG_MOVEMENT) {
std::cout << "Delta X: " << delta_x << std::endl;
}
m_forward.x = glm::cos((float)delta_x/100);
m_forward.z = glm::sin((float)delta_x/100);
//moving the camera
if (Input::getKey(SDLK_w)) {
m_position.z += SPEED;
m_position += (SPEED * m_forward);
if (DEBUG_MOVEMENT) {
std::cout << "Pressed W" << std::endl;
}
}
if (Input::getKey(SDLK_s)) {
m_position.z -= SPEED;
m_position -= (SPEED * m_forward);
if (DEBUG_MOVEMENT) {
std::cout << "Pressed S" << std::endl;
}
}
if (Input::getKey(SDLK_a)) {
m_position.x += SPEED;
m_position -= (SPEED * glm::cross(m_forward, m_up));
if (DEBUG_MOVEMENT) {
std::cout << "Pressed A" << std::endl;
}
}
if (Input::getKey(SDLK_d)) {
m_position.x -= SPEED;
m_position += (SPEED * glm::cross(m_forward, m_up));
if (DEBUG_MOVEMENT) {
std::cout << "Pressed D" << std::endl;
}
......@@ -69,9 +81,11 @@ public:
private:
glm::mat4 m_perspective;
glm::vec3 m_position;
glm::vec3 m_forward;
glm::vec3 m_up;
glm::f32vec3 m_position;
glm::f32vec3 m_forward;
glm::f32vec3 m_up;
glm::vec2 m_mouse_pos;
};
#endif //CAMERA_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment