Commit 7ebcfa22 authored by Sadman Kazi's avatar Sadman Kazi 🎸

Added camera movement with WASD

parent b03ef97a
......@@ -4,6 +4,9 @@
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtx/rotate_vector.hpp>
#include "input.h"
#define SPEED 0.001f
class Camera
{
......@@ -20,6 +23,22 @@ public:
return m_perspective * glm::lookAt(m_position, m_position + m_forward, m_up);
}
inline void input() {
//moving the camera
if (Input::getKey(SDLK_w)) {
m_position.z += SPEED;
}
if (Input::getKey(SDLK_s)) {
m_position.z -= SPEED;
}
if (Input::getKey(SDLK_a)) {
m_position.x += SPEED;
}
if (Input::getKey(SDLK_d)) {
m_position.x -= SPEED;
}
}
inline void rotate(float angle, glm::vec3& axis) {
m_forward = glm::rotate(m_forward, angle, axis);
m_up = glm::rotate(m_up, angle, axis);
......
......@@ -54,12 +54,13 @@ Game::~Game()
}
void Game::input() {
m_camera->input();
}
void Game::render() {
m_shader->bind();
m_mesh->draw();
if (Input::getKeyDown(SDLK_UP)) {
/*if (Input::getKeyDown(SDLK_UP)) {
std::cout << "You have pressed up!" << std::endl;
m_camera->translate(glm::vec3(0.0f, 0.0f, 0.1f));
}
......@@ -73,6 +74,7 @@ void Game::render() {
else if (Input::getMouseUp(SDL_BUTTON_LEFT)) {
std::cout << "You released the left mouse button!" << std::endl;
}
*/
}
void Game::update() {
......@@ -85,5 +87,5 @@ void Game::update() {
m_transform->getRot().y = sinCounter;
//transform->GetRot().z = sinCounter;
m_shader->setUniformMat4("transform", m_camera->getViewProjection() * m_transform->getModel());
m_shader->setUniformMat4("transform", m_transform->getProjectedModel(m_camera));
}
\ No newline at end of file
......@@ -12,6 +12,9 @@ but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#ifndef INPUT_H_INCLUDED
#define INPUT_H_INCLUDED
#include <sdl/SDL.h> //required for getting the keyboard inputs
#include <vector>
#include <glm/glm.hpp>
......@@ -40,3 +43,5 @@ private:
static std::vector<Uint8> downMouse; //store the mouse buttons in the current frame
static std::vector<Uint8> upMouse; //store the mouse buttons released in the current frame
};
#endif INPUT_H_INCLUDED
\ No newline at end of file
......@@ -3,6 +3,7 @@
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#include "camera.h"
class Transform
{
......@@ -26,6 +27,10 @@ public:
return posMatrix * rotMatrix * scaleMatrix;
}
inline glm::mat4 getProjectedModel(Camera* camera) const {
return camera->getViewProjection() * getModel();
}
//getters, returns by reference
inline glm::vec3& getPos() { return m_pos; }
inline glm::vec3& getRot() { return m_rot; }
......@@ -42,4 +47,4 @@ public:
private:
glm::vec3 m_pos, m_rot, m_scale; //m_ refers to member variables
};
#endif
\ No newline at end of file
#endif CAMERA_H_INCLUDED
\ No newline at end of file
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