Created camera movement in the Input class, next step: cursor control
This commit is contained in:
parent
ef90718135
commit
c64b7b4bde
@ -5,6 +5,9 @@
|
|||||||
|
|
||||||
#include <util/log.hpp>
|
#include <util/log.hpp>
|
||||||
|
|
||||||
|
#include <Renderer/Renderer.hpp>
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
bool Input::shouldClose() {
|
bool Input::shouldClose() {
|
||||||
return glfwWindowShouldClose(in);
|
return glfwWindowShouldClose(in);
|
||||||
@ -18,10 +21,10 @@ Input::Input(INPUT_PTR in) : in(in) {
|
|||||||
glfwSetWindowUserPointer(in, this);
|
glfwSetWindowUserPointer(in, this);
|
||||||
|
|
||||||
glfwSetWindowSizeCallback(in, [](GLFWwindow* in, int width, int height) {
|
glfwSetWindowSizeCallback(in, [](GLFWwindow* in, int width, int height) {
|
||||||
Log::info("Event Received: Resize to %dx%d\n", width, height);
|
Log::info("Event Received:Resize to %dx%d\n", width, height);
|
||||||
Input* input = reinterpret_cast<Input*>(glfwGetWindowUserPointer(in));
|
Input* input = reinterpret_cast<Input*>(glfwGetWindowUserPointer(in));
|
||||||
input->queue.push(InputEvent{
|
input->queue.push(InputEvent{
|
||||||
.tag = InputEvent::Tag::RESIZE,
|
.tag = InputEvent::Tag::eRESIZE,
|
||||||
.resize = {
|
.resize = {
|
||||||
.width = width,
|
.width = width,
|
||||||
.height = height,
|
.height = height,
|
||||||
@ -32,17 +35,17 @@ Input::Input(INPUT_PTR in) : in(in) {
|
|||||||
glfwSetKeyCallback(in, [](GLFWwindow* in, int key, int scancode, int action, int mods) {
|
glfwSetKeyCallback(in, [](GLFWwindow* in, int key, int scancode, int action, int mods) {
|
||||||
int state = (action == GLFW_PRESS || action == GLFW_REPEAT);
|
int state = (action == GLFW_PRESS || action == GLFW_REPEAT);
|
||||||
static const char* action_strings[] = { "RELEASE", "PRESS", "REPEAT" };
|
static const char* action_strings[] = { "RELEASE", "PRESS", "REPEAT" };
|
||||||
Log::info("Event Received: Key: \"%s\" %s\n", glfwGetKeyName(key, scancode), action_strings[action]);
|
Log::info("Event Received:Key:\"%s\" %s\n", glfwGetKeyName(key, scancode), action_strings[action]);
|
||||||
Input* input = reinterpret_cast<Input*>(glfwGetWindowUserPointer(in));
|
Input* input = reinterpret_cast<Input*>(glfwGetWindowUserPointer(in));
|
||||||
InputModifierBit i_mods = NONE;
|
InputModifierBit i_mods = eNONE;
|
||||||
|
|
||||||
if(mods &= GLFW_MOD_SHIFT)
|
if(mods &= GLFW_MOD_SHIFT)
|
||||||
i_mods |= SHIFT;
|
i_mods |= eSHIFT;
|
||||||
if (mods &= GLFW_MOD_CONTROL)
|
if (mods &= GLFW_MOD_CONTROL)
|
||||||
i_mods |= CNTRL;
|
i_mods |= eCNTRL;
|
||||||
|
|
||||||
input->queue.push(InputEvent{
|
input->queue.push(InputEvent{
|
||||||
.tag = InputEvent::Tag::KEY,
|
.tag = InputEvent::Tag::eKEY,
|
||||||
.key = {
|
.key = {
|
||||||
.key = key,
|
.key = key,
|
||||||
.state = state,
|
.state = state,
|
||||||
@ -55,7 +58,7 @@ Input::Input(INPUT_PTR in) : in(in) {
|
|||||||
Input* input = reinterpret_cast<Input*>(glfwGetWindowUserPointer(in));
|
Input* input = reinterpret_cast<Input*>(glfwGetWindowUserPointer(in));
|
||||||
|
|
||||||
input->queue.push(InputEvent{
|
input->queue.push(InputEvent{
|
||||||
.tag = InputEvent::Tag::CURSOR,
|
.tag = InputEvent::Tag::eCURSOR,
|
||||||
.pos = {
|
.pos = {
|
||||||
.x = x,
|
.x = x,
|
||||||
.y = y,
|
.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>());
|
||||||
|
}
|
||||||
|
|||||||
@ -8,9 +8,9 @@
|
|||||||
#include <util/int.hpp>
|
#include <util/int.hpp>
|
||||||
|
|
||||||
enum InputModifierBit {
|
enum InputModifierBit {
|
||||||
NONE = 0x00,
|
eNONE = 0x00,
|
||||||
SHIFT = 0x01,
|
eSHIFT = 0x01,
|
||||||
CNTRL = 0x02,
|
eCNTRL = 0x02,
|
||||||
};
|
};
|
||||||
|
|
||||||
constexpr InputModifierBit operator & (const InputModifierBit a, const InputModifierBit b) {
|
constexpr InputModifierBit operator & (const InputModifierBit a, const InputModifierBit b) {
|
||||||
@ -32,11 +32,11 @@ constexpr u32 operator ~ (InputModifierBit a) {
|
|||||||
|
|
||||||
struct InputEvent {
|
struct InputEvent {
|
||||||
enum Tag {
|
enum Tag {
|
||||||
EXIT,
|
eEXIT,
|
||||||
RESIZE,
|
eRESIZE,
|
||||||
CURSOR,
|
eCURSOR,
|
||||||
KEY,
|
eKEY,
|
||||||
BUTTON,
|
eBUTTON,
|
||||||
} tag;
|
} tag;
|
||||||
|
|
||||||
union {
|
union {
|
||||||
@ -54,6 +54,8 @@ struct InputEvent {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct Renderer;
|
||||||
|
|
||||||
struct Input {
|
struct Input {
|
||||||
Input(INPUT_PTR in);
|
Input(INPUT_PTR in);
|
||||||
|
|
||||||
@ -63,5 +65,7 @@ struct Input {
|
|||||||
|
|
||||||
std::queue<InputEvent> queue;
|
std::queue<InputEvent> queue;
|
||||||
|
|
||||||
|
void handleMovementKeys(Renderer& ren);
|
||||||
|
|
||||||
bool shouldClose();
|
bool shouldClose();
|
||||||
};
|
};
|
||||||
@ -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) {
|
Window::Window(const std::string& title, u32 width, u32 height) : width(width), height(height) {
|
||||||
glfwSetErrorCallback(win_err_callback);
|
glfwSetErrorCallback(win_err_callback);
|
||||||
|
|
||||||
|
|
||||||
if (!glfwInit()) {
|
if (!glfwInit()) {
|
||||||
Log::error("Failed to initialize GLFW\n");
|
Log::error("Failed to initialize GLFW\n");
|
||||||
return;
|
return;
|
||||||
|
|||||||
@ -7,6 +7,9 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
try {
|
try {
|
||||||
Window win(argv[0], 256, 512);
|
Window win(argv[0], 256, 512);
|
||||||
@ -16,26 +19,25 @@ int main(int argc, char* argv[]) {
|
|||||||
while (!in->shouldClose()) {
|
while (!in->shouldClose()) {
|
||||||
Timer frame_timer;
|
Timer frame_timer;
|
||||||
in->poll();
|
in->poll();
|
||||||
|
in->handleMovementKeys(ren);
|
||||||
|
|
||||||
while (in->queue.size()) {
|
while (in->queue.size()) {
|
||||||
/* take event from front of queue, then process it */
|
/* take event from front of queue, then process it */
|
||||||
const auto& event = in->queue.front();
|
const auto& event = in->queue.front();
|
||||||
in->queue.pop();
|
in->queue.pop();
|
||||||
switch (event.tag) {
|
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);
|
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
|
/* 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
|
* automatically marked out-of-date, and recreation will be triggered in our code
|
||||||
*/
|
*/
|
||||||
break;
|
break;
|
||||||
case InputEvent::Tag::KEY:
|
case InputEvent::Tag::eEXIT:
|
||||||
Log::info("Event Processed: Button 0x%x %d\n", event.key.key, event.key.state);
|
|
||||||
break;
|
|
||||||
case InputEvent::Tag::EXIT:
|
|
||||||
win.close();
|
win.close();
|
||||||
break;
|
break;
|
||||||
case InputEvent::Tag::CURSOR:
|
case InputEvent::Tag::eCURSOR:
|
||||||
case InputEvent::Tag::BUTTON:
|
break;
|
||||||
|
case InputEvent::Tag::eBUTTON:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user