Created camera movement in the Input class, next step: cursor control

This commit is contained in:
connellpaxton 2024-01-30 11:41:47 -05:00
parent ef90718135
commit c64b7b4bde
4 changed files with 77 additions and 24 deletions

View File

@ -5,6 +5,9 @@
#include <util/log.hpp>
#include <Renderer/Renderer.hpp>
#include <glm/glm.hpp>
bool Input::shouldClose() {
return glfwWindowShouldClose(in);
@ -21,7 +24,7 @@ Input::Input(INPUT_PTR in) : in(in) {
Log::info("Event Received:Resize to %dx%d\n", width, height);
Input* input = reinterpret_cast<Input*>(glfwGetWindowUserPointer(in));
input->queue.push(InputEvent{
.tag = InputEvent::Tag::RESIZE,
.tag = InputEvent::Tag::eRESIZE,
.resize = {
.width = width,
.height = height,
@ -34,15 +37,15 @@ Input::Input(INPUT_PTR in) : in(in) {
static const char* action_strings[] = { "RELEASE", "PRESS", "REPEAT" };
Log::info("Event Received:Key:\"%s\" %s\n", glfwGetKeyName(key, scancode), action_strings[action]);
Input* input = reinterpret_cast<Input*>(glfwGetWindowUserPointer(in));
InputModifierBit i_mods = NONE;
InputModifierBit i_mods = eNONE;
if(mods &= GLFW_MOD_SHIFT)
i_mods |= SHIFT;
i_mods |= eSHIFT;
if (mods &= GLFW_MOD_CONTROL)
i_mods |= CNTRL;
i_mods |= eCNTRL;
input->queue.push(InputEvent{
.tag = InputEvent::Tag::KEY,
.tag = InputEvent::Tag::eKEY,
.key = {
.key = key,
.state = state,
@ -55,7 +58,7 @@ Input::Input(INPUT_PTR in) : in(in) {
Input* input = reinterpret_cast<Input*>(glfwGetWindowUserPointer(in));
input->queue.push(InputEvent{
.tag = InputEvent::Tag::CURSOR,
.tag = InputEvent::Tag::eCURSOR,
.pos = {
.x = x,
.y = y,
@ -63,3 +66,48 @@ Input::Input(INPUT_PTR in) : in(in) {
});
});
}
void Input::handleMovementKeys(Renderer& ren) {
const auto forward = glm::vec3(glm::cos(ren.cam.phi), 0.0, glm::sin(ren.cam.phi));
const auto right = glm::cross(forward, glm::vec3(0.0, 1.0, 0.0));
const auto speed = glfwGetKey(in, GLFW_KEY_LEFT_SHIFT)? 2.0f : 1.0f;
if(glfwGetKey(in, GLFW_KEY_UP)) {
ren.cam.theta -= 0.01;
}
if(glfwGetKey(in, GLFW_KEY_UP)) {
ren.cam.theta -= 0.01;
}
if(glfwGetKey(in, GLFW_KEY_DOWN)) {
ren.cam.theta += 0.01;
}
if(glfwGetKey(in, GLFW_KEY_LEFT)) {
ren.cam.phi -= 0.03;
}
if(glfwGetKey(in, GLFW_KEY_RIGHT)) {
ren.cam.phi += 0.03;
}
/* move "forward" or "backward" */
if (glfwGetKey(in, GLFW_KEY_W)) {
ren.cam.pos += forward * 0.1f * speed;
}
if (glfwGetKey(in, GLFW_KEY_S)) {
ren.cam.pos += forward * -0.1f * speed;
}
/* move "left" or "right" */
if (glfwGetKey(in, GLFW_KEY_A)) {
ren.cam.pos -= right * 0.1f * speed;
}
if (glfwGetKey(in, GLFW_KEY_D)) {
ren.cam.pos += right * 0.1f * speed;
}
ren.cam.theta = glm::clamp(ren.cam.theta, 0.01f, glm::pi<float>() - 0.01f);
ren.cam.phi = glm::mod(ren.cam.phi, glm::two_pi<float>());
}

View File

@ -8,9 +8,9 @@
#include <util/int.hpp>
enum InputModifierBit {
NONE = 0x00,
SHIFT = 0x01,
CNTRL = 0x02,
eNONE = 0x00,
eSHIFT = 0x01,
eCNTRL = 0x02,
};
constexpr InputModifierBit operator & (const InputModifierBit a, const InputModifierBit b) {
@ -32,11 +32,11 @@ constexpr u32 operator ~ (InputModifierBit a) {
struct InputEvent {
enum Tag {
EXIT,
RESIZE,
CURSOR,
KEY,
BUTTON,
eEXIT,
eRESIZE,
eCURSOR,
eKEY,
eBUTTON,
} tag;
union {
@ -54,6 +54,8 @@ struct InputEvent {
};
};
struct Renderer;
struct Input {
Input(INPUT_PTR in);
@ -63,5 +65,7 @@ struct Input {
std::queue<InputEvent> queue;
void handleMovementKeys(Renderer& ren);
bool shouldClose();
};

View File

@ -16,7 +16,6 @@ static void win_err_callback(int code, const char* msg) {
Window::Window(const std::string& title, u32 width, u32 height) : width(width), height(height) {
glfwSetErrorCallback(win_err_callback);
if (!glfwInit()) {
Log::error("Failed to initialize GLFW\n");
return;

View File

@ -7,6 +7,9 @@
#include <iostream>
#include <GLFW/glfw3.h>
int main(int argc, char* argv[]) {
try {
Window win(argv[0], 256, 512);
@ -16,26 +19,25 @@ int main(int argc, char* argv[]) {
while (!in->shouldClose()) {
Timer frame_timer;
in->poll();
in->handleMovementKeys(ren);
while (in->queue.size()) {
/* take event from front of queue, then process it */
const auto& event = in->queue.front();
in->queue.pop();
switch (event.tag) {
case InputEvent::Tag::RESIZE:
case InputEvent::Tag::eRESIZE:
Log::info("Event Processed: Resized to %dx%d\n", event.resize.width, event.resize.height);
/* no need to have a resize() function in the renderer, b/c swapchain images will be
* automatically marked out-of-date, and recreation will be triggered in our code
*/
break;
case InputEvent::Tag::KEY:
Log::info("Event Processed: Button 0x%x %d\n", event.key.key, event.key.state);
break;
case InputEvent::Tag::EXIT:
case InputEvent::Tag::eEXIT:
win.close();
break;
case InputEvent::Tag::CURSOR:
case InputEvent::Tag::BUTTON:
case InputEvent::Tag::eCURSOR:
break;
case InputEvent::Tag::eBUTTON:
break;
}
}