added movement

This commit is contained in:
Jay Robson 2024-01-22 00:20:54 +11:00
parent 68af17b81a
commit 310f85a57e
7 changed files with 194 additions and 2 deletions

93
src/graphics/camera.cpp Normal file
View File

@ -0,0 +1,93 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "camera.hpp"
#include "keyboard.hpp"
#include <glm/matrix.hpp>
#include <glm/vec3.hpp>
#include <glm/ext/matrix_transform.hpp>
using namespace sim::graphics;
static double yaw = 0, pitch = 0;
static double x = 0, y = 0, z = 0;
void camera::rotate(double y, double p)
{
yaw += y * 0.1;
pitch += p * 0.1;
if(pitch < -90) pitch = -90;
if(pitch > 90) pitch = 90;
}
void camera::move(double xoff, double yoff, double zoff)
{
x += xoff;
y += yoff;
z += zoff;
}
void camera::update()
{
double xoff = 0, yoff = 0, zoff = 0;
glm::vec<3, double> off(0, 0, 0);
if(keyboard::is_pressed(GLFW_KEY_SPACE))
{
off.y -= 1;
}
if(keyboard::is_pressed(GLFW_KEY_LEFT_SHIFT))
{
off.y += 1;
}
if(keyboard::is_pressed(GLFW_KEY_W))
{
off.z += 1;
}
if(keyboard::is_pressed(GLFW_KEY_S))
{
off.z -= 1;
}
if(keyboard::is_pressed(GLFW_KEY_A))
{
off.x += 1;
}
if(keyboard::is_pressed(GLFW_KEY_D))
{
off.x -= 1;
}
double angle = -glm::radians<double>(yaw);
glm::mat<2, 2, double> mat = {
std::cos(angle), std::sin(angle),
-std::sin(angle), std::cos(angle)
};
glm::vec<2, double> rotated = glm::vec<2, double>(off.x, off.z) * mat;
y += off.y * 0.1;
x += rotated.x * 0.1;
z += rotated.y * 0.1;
}
glm::mat4 camera::get()
{
glm::mat4 mat(1);
mat = glm::rotate(mat, (float)glm::radians(pitch), glm::vec3(1, 0, 0));
mat = glm::rotate(mat, (float)glm::radians(yaw), glm::vec3(0, 1, 0));
mat = glm::translate(mat, glm::vec3(x, y, z));
return mat;
}

15
src/graphics/camera.hpp Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include <glm/matrix.hpp>
namespace sim::graphics::camera
{
glm::mat4 get();
void rotate(double pitch, double yaw);
void move(double x, double y, double z);
void update();
};

View File

@ -2,18 +2,48 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <unordered_map>
#include "keyboard.hpp"
#include "window.hpp"
#include "resize.hpp"
#include "camera.hpp"
using namespace sim::graphics;
static std::unordered_map<int, bool> pressed;
static void cb_keypress(GLFWwindow* win, int key, int sc, int action, int mods)
{
if(key == GLFW_KEY_F11 && action == GLFW_RELEASE)
{
resize::toggle_fullscreen();
}
if(action == GLFW_PRESS)
{
pressed[key] = true;
}
if(action == GLFW_RELEASE)
{
pressed[key] = false;
}
}
bool keyboard::is_pressed(int key)
{
auto it = pressed.find(key);
if(it == pressed.end())
{
return false;
}
else
{
return it->second;
}
}
void keyboard::init()

View File

@ -5,6 +5,7 @@ namespace sim::graphics::keyboard
{
void init();
bool is_pressed(int key);
};

34
src/graphics/mouse.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "mouse.hpp"
#include "window.hpp"
#include "camera.hpp"
using namespace sim::graphics;
static double xpos = 0, ypos = 0;
static void cb_cursor_pos(GLFWwindow* win, double x, double y)
{
camera::rotate(x - xpos, y - ypos);
xpos = x;
ypos = y;
}
void mouse::get(double& x, double& y)
{
x = xpos;
y = ypos;
}
void mouse::init()
{
GLFWwindow* win = window::get_window();
glfwSetCursorPosCallback(win, cb_cursor_pos);
glfwSetInputMode(win, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPos(win, 0, 0);
}

11
src/graphics/mouse.hpp Normal file
View File

@ -0,0 +1,11 @@
#pragma once
namespace sim::graphics::mouse
{
void init();
void get(double& x, double& y);
};

View File

@ -11,6 +11,8 @@
#include "model.hpp"
#include "arrays.hpp"
#include "keyboard.hpp"
#include "mouse.hpp"
#include "camera.hpp"
#include "resize.hpp"
#include "window.hpp"
#include "shader.hpp"
@ -58,8 +60,10 @@ void window::create()
glDebugMessageCallback(cb_debug_message, nullptr);
keyboard::init();
mouse::init();
resize::init();
font::init();
shader::init_program();
Model.alloc();
@ -80,10 +84,14 @@ void window::loop()
0, 0, 0, 0
};
glm::mat4 mat_model = glm::mat4(1.0f);
camera::update();
glm::mat4 mat_model = camera::get();
double mouse_x, mouse_y;
mouse::get(mouse_x, mouse_y);
mat_model = glm::translate(mat_model, glm::vec3(0.0f, 0.0f, -5.0f));
// mat_model = glm::scale(mat_model, glm::vec3(1.0f) * 0.05f);
mat_model = glm::rotate(mat_model, float(M_PI * 0.125), glm::vec3(1, 1, 1));
glm::mat4 mat_projection = glm::perspective(float(M_PI * 0.25), (float)resize::get_aspect(), 0.1f, 100.f);