Basic input handling, Editor camera Movement

This commit is contained in:
Nigel Barink 2023-01-04 19:01:58 +01:00
parent d5a6ddb9d5
commit 13f67a7cdb
15 changed files with 382 additions and 152 deletions

View File

@ -7,7 +7,6 @@
std::vector<Asset> AssetManager::assets;
std::filesystem::path AssetManager::currentPath;
void AssetManager::Init()
{
assets = std::vector<Asset>();
@ -38,7 +37,6 @@ void AssetManager::setAssetPath(std::filesystem::path path)
currentPath = path;
}
YoggieEngine::Mesh* AssetManager::LoadFromAssetFile(const std::filesystem::path assetPath)
{
YoggieEngine::Mesh* imported = nullptr;
@ -147,4 +145,4 @@ YoggieEngine::Renderable* AssetManager::LoadFromSource(const std::filesystem::pa
return model->renderable;
}
}

View File

@ -6,9 +6,6 @@ class EditorWindow {
public:
EditorWindow(const std::string& name, ImGuiWindowFlags_ flags = ImGuiWindowFlags_None ) { ImGui::Begin(name.c_str(), false ,flags); }
~EditorWindow() { ImGui::End(); }
};

View File

@ -16,7 +16,6 @@ public:
io.ConfigFlags |= ImGuiConfigFlags_::ImGuiConfigFlags_DockingEnable;
io.Fonts->AddFontFromFileTTF("build/Debug/Fonts/Roboto-Regular.ttf", 18);
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window.GetGLFWHandle(), true);

View File

@ -151,14 +151,25 @@ public:
glm::mat4 transposed_view = glm::transpose(cam.ViewMatrix);
ImGuizmo::ViewManipulate(glm::value_ptr(transposed_view), 1, { ScreenSpaceMin.x,ScreenSpaceMin.y }, { 90,90 }, 0x22CCCCCC);
ImGuizmo::DrawGrid(glm::value_ptr(cam.ViewMatrix), glm::value_ptr(cam.ProjectionMatrix), glm::value_ptr(worldOrigin), 100.0f);
ImGuizmo::ViewManipulate(glm::value_ptr(transposed_view), 1, { ScreenSpaceMin.x,ScreenSpaceMin.y }, { 90,90 }, 0x22CCCCCC);
// Matrix is the model matrix we would want to manipulate
//ImGuizmo::Manipulate(glm::value_ptr(cam.ViewMatrix), glm::value_ptr(cam.ProjectionMatrix), ImGuizmo::TRANSLATE, ImGuizmo::WORLD, glm::value_ptr(cam.ViewMatrix));
if (ImGui::IsWindowFocused() )
{
isFocused = true;
}
else {
isFocused = false;
}
}
bool isFocused = false;
};

View File

@ -22,9 +22,10 @@ RendererConfig EditorSceneRendererConfig{
glm::vec3{0,0,0}, // Clear Color
true // Depth testing
};
glm::vec3 temp = glm::vec3(0);
Camera cam = Camera(glm::vec3(14.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90);
class Editor : public Application {
public:
Editor()
@ -35,6 +36,8 @@ public:
EditorGUIRenderer(AppWindow),
Selected((entt::entity)-1)
{
init_inputSystem(&AppWindow);
viewportRenderer.setCurrentFrameBuffer(*framebuffer);
}
@ -71,6 +74,74 @@ public:
{
Viewport sceneview = Viewport(*framebuffer, viewportRenderer.getCamera());
if (sceneview.isFocused)
{
const float movement_speed = 0.1f;
static float lastX = 400, lastY = 300;
const float sensitivity = 0.1;
static bool firstMouse = true;
if (MouseButtonPressed(YOGGIE_MOUSE_BUTTON_MIDDLE) ){
auto newX = getCursorPosX(&AppWindow);
auto newY = getCursorPosY(&AppWindow);
if (firstMouse)
{
lastX = newX;
lastY = newY;
firstMouse = false;
}
float xoffset = newX - lastX;
float yoffset = newY - lastY;
lastX = newX;
lastY = newY;
xoffset *= sensitivity;
yoffset *= sensitivity;
cam.yaw += xoffset;
cam.pitch += yoffset;
if (cam.pitch > 89.0f)
cam.pitch = 89.0f;
if (cam.pitch < -89.0f)
cam.pitch = -89.0f;
}
// Check for Camara movement input here!
if (keyIsPressed(YOGGIE_KEY_W)) {
cam.Position += cam.Front * movement_speed;
}
if (keyIsPressed(YOGGIE_KEY_A))
{
cam.Position -= cam.Right * movement_speed;
}
if (keyIsPressed(YOGGIE_KEY_S)) {
cam.Position -= cam.Front * movement_speed;
}
if (keyIsPressed(YOGGIE_KEY_D)) {
cam.Position += cam.Right * movement_speed;
}
}
}
{
@ -136,7 +207,7 @@ public:
lag += elapsed;
AppWindow.Poll();
cam.Update();
Physics.Step(elapsed);
ActiveScene.Update();
@ -170,6 +241,8 @@ private:
bool SimulatePhysics = true;
entt::entity Selected;
std::unique_ptr<Project> CurrentProject;
Scene ActiveScene;

View File

@ -6,8 +6,11 @@ namespace YoggieEngine {
Application::Application(const std::string& name)
: m_AppName(name)
{
// Initialize engine should possibly happen here
EngineInstrumentation::PerfomanceSamplerInit();
}

View File

@ -15,8 +15,7 @@ namespace YoggieEngine
}
}
int main(int argc, char** argv) {
int main(int argc, char** argv)
{
return YoggieEngine::entryPoint();
}

View File

@ -9,16 +9,25 @@ namespace YoggieEngine {
Camera::Camera(glm::vec3 position, glm::vec3 rotation, float zoom)
: Position(position), Rotation( rotation)
{
glm::vec3 WorldUp = glm::vec3(0.0f, 1.0f, 0.0f);
Front = glm::vec3(0.0f, 0.0f, 1.0f);
Right = glm::vec3(-1.0f, 0.0f, 0.0f);
Up = glm::vec3(0.0f, 1.0f, 0.0f);
Zoom = zoom;
ProjectionMatrix = glm::perspective(glm::radians(Zoom), (800.0f / 600.0f), 0.001f, 100.0f);
Update();
}
Camera::~Camera() {
}
void Camera::Update() {
glm::vec3 WorldUp = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3(direction);
float yaw = 180;
float pitch = 0;
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
direction.z = sin(glm::radians(yaw));
@ -32,17 +41,6 @@ namespace YoggieEngine {
Position,
Position + Front,
Up);
ProjectionMatrix = glm::perspective(glm::radians(Zoom), (800.0f / 600.0f), 0.001f, 100.0f);
}
Camera::~Camera() {
}
void Camera::Update() {
ViewMatrix = glm::lookAt(Position,Position + Front,Up);
}
}

View File

@ -9,15 +9,17 @@ namespace YoggieEngine {
void Update();
glm::vec3 Position;
glm::vec3 Rotation;
float yaw = 180;
float pitch = 0;
float Zoom;
glm::mat4 ViewMatrix;
glm::mat4 ProjectionMatrix;
glm::vec3 Position;
glm::vec3 Rotation;
private:
glm::vec3 Front;
glm::vec3 Right;
glm::vec3 Up;

View File

@ -1,115 +1,98 @@
#include <YoggieEngine.h>
#include "InputManager.h"
namespace YoggieEngine {
InputManager InputSystem;
void InputManager::PollEvents()
{
for (auto it = windows.begin(); it != windows.end(); ++it) {
auto window = *it;
window->Poll();
namespace YoggieEngine {
double cursor_pos_x;
double cursor_pos_y;
int key_status[YOGGIE_KEY_LAST];
int mouseButton_status[7];
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
void cursorPos_callback(GLFWwindow* window, double xpos, double ypos);
void cursorEnter_callback(GLFWwindow* window, int entered);
void mouseButton_callback(GLFWwindow* window, int button, int action, int mods);
void init_inputSystem(NativeWindow* window) {
cursor_pos_x = 0;
cursor_pos_y = 0;
glfwSetKeyCallback(window->GetGLFWHandle(), key_callback);
glfwSetMouseButtonCallback(window->GetGLFWHandle(), mouseButton_callback);
glfwSetCursorPosCallback(window->GetGLFWHandle(), cursorPos_callback);
glfwSetCursorEnterCallback(window->GetGLFWHandle(), cursorEnter_callback);
}
void InputManager::KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
void ReceiveInput() {
glfwPollEvents();
}
bool keyIsPressed(int key)
{
return (key_status[key] == GLFW_PRESS) || (key_status[key] == GLFW_REPEAT);
}
Event KeyEvent{};
KeyEvent.name = "KEY";
InputSystem.EmitEvent(KeyEvent);
bool MouseButtonPressed(int key)
{
return (mouseButton_status[key] == GLFW_PRESS ) || (mouseButton_status[key] == GLFW_REPEAT);
}
if (key == GLFW_KEY_A && action == GLFW_PRESS)
{
double getCursorPosX(NativeWindow* window)
{
glfwGetCursorPos(window->GetGLFWHandle(), &cursor_pos_x, nullptr);
return cursor_pos_x;
}
double getCursorPosY(NativeWindow* window)
{
glfwGetCursorPos(window->GetGLFWHandle(), nullptr, &cursor_pos_y);
return cursor_pos_y;
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
key_status[key] = action;
}
void mouseButton_callback(GLFWwindow* window, int button, int action, int mods)
{
auto& io = ImGui::GetIO();
io.AddMouseButtonEvent(button , action == GLFW_PRESS);
std::cout << "'a' key was pressed" << std::endl;
}
mouseButton_status[button] = action;
}
void InputManager::CursorPositionCallback(GLFWwindow* window, double x, double y)
void cursorPos_callback(GLFWwindow* window, double xpos, double ypos)
{
//std::cout << "Cursor Position x: " << x << ", y: " << y << std::endl;
Event CursorPosUpdate{};
CursorPosUpdate.name = "UPDATE::CURSOR:POSITION";
InputSystem.EmitEvent(CursorPosUpdate);
//std::cout << "Cursor moved!" << std::endl;
}
void InputManager::CursorEnterCallback(GLFWwindow* window, int entered)
void cursorEnter_callback(GLFWwindow* window, int entered)
{
if (entered) {
Event mouseEntered {};
mouseEntered.name = "Mouse Entered Window's confines!";
mouseEntered.argc = 0;
InputSystem.EmitEvent(mouseEntered);
// window is in focus
//std::cout << "Cursor Entered!" << std::endl;
}
else {
Event mouseLeft{};
mouseLeft.name = "Mouse Left Window's confines!";
mouseLeft.argc = 0;
InputSystem.EmitEvent(mouseLeft);
// window is not in focus
//std::cout << "Cursor Left!" << std::endl;
}
}
void InputManager::MouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
{
Event MouseButtonEvent{};
MouseButtonEvent.name = "MOUSEBUTTON";
InputSystem.EmitEvent(MouseButtonEvent);
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) {
std::cout << "Right mouse button was pressed!" << std::endl;
}
}
void InputManager::ScrollCallback(GLFWwindow* window, double xoffset, double yoffset)
{
std::cout << "Scroll: x: " << xoffset << ", y: " << yoffset << std::endl;
Event ScrollEvent{};
ScrollEvent.name = "SCROLL";
InputSystem.EmitEvent(ScrollEvent);
}
void InputManager::attach(BarinkWindow* window)
{
windows.push_back(window);
// Attach callbacks
glfwSetKeyCallback(window->windowptr(), KeyCallback);
glfwSetCursorPosCallback(window->windowptr(), CursorPositionCallback);
glfwSetCursorEnterCallback(window->windowptr(), CursorEnterCallback);
glfwSetMouseButtonCallback(window->windowptr(), MouseButtonCallback);
glfwSetScrollCallback(window->windowptr(), ScrollCallback);
this->Subscribe( (EventListener&)(*window));
}
InputManager::InputManager() : EventEmitter ()
{
windows = std::vector<BarinkWindow*>();
}
}

View File

@ -1,23 +1,15 @@
#pragma once
namespace YoggieEngine {
// Lets just take the dumb approach for now
class InputManager : EventEmitter {
public:
InputManager();
void init_inputSystem(NativeWindow* window);
void ReceiveInput();
bool keyIsPressed(int key);
bool MouseButtonPressed(int key);
void PollEvents();
void attach(BarinkWindow* window);
double getCursorPosX(NativeWindow* window);
double getCursorPosY(NativeWindow* window);
// GLFW Handlers
static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
static void CursorPositionCallback(GLFWwindow* window, double x, double y);
static void CursorEnterCallback(GLFWwindow* window, int entered);
static void MouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
static void ScrollCallback(GLFWwindow* window, double xoffset, double yoffset);
private:
std::vector<BarinkWindow*> windows;
};
extern InputManager InputSystem;
}
}

View File

@ -0,0 +1,170 @@
#pragma once
namespace YoggieEngine{
/*
* Based on GLFW
*/
/* The unknown key */
#define YOGGIE_KEY_UNKNOWN -1
/* Printable keys */
#define YOGGIE_KEY_SPACE 32
#define YOGGIE_KEY_APOSTROPHE 39 /* ' */
#define YOGGIE_KEY_COMMA 44 /* , */
#define YOGGIE_KEY_MINUS 45 /* - */
#define YOGGIE_KEY_PERIOD 46 /* . */
#define YOGGIE_KEY_SLASH 47 /* / */
#define YOGGIE_KEY_0 48
#define YOGGIE_KEY_1 49
#define YOGGIE_KEY_2 50
#define YOGGIE_KEY_3 51
#define YOGGIE_KEY_4 52
#define YOGGIE_KEY_5 53
#define YOGGIE_KEY_6 54
#define YOGGIE_KEY_7 55
#define YOGGIE_KEY_8 56
#define YOGGIE_KEY_9 57
#define YOGGIE_KEY_SEMICOLON 59 /* ; */
#define YOGGIE_KEY_EQUAL 61 /* = */
#define YOGGIE_KEY_A 65
#define YOGGIE_KEY_B 66
#define YOGGIE_KEY_C 67
#define YOGGIE_KEY_D 68
#define YOGGIE_KEY_E 69
#define YOGGIE_KEY_F 70
#define YOGGIE_KEY_G 71
#define YOGGIE_KEY_H 72
#define YOGGIE_KEY_I 73
#define YOGGIE_KEY_J 74
#define YOGGIE_KEY_K 75
#define YOGGIE_KEY_L 76
#define YOGGIE_KEY_M 77
#define YOGGIE_KEY_N 78
#define YOGGIE_KEY_O 79
#define YOGGIE_KEY_P 80
#define YOGGIE_KEY_Q 81
#define YOGGIE_KEY_R 82
#define YOGGIE_KEY_S 83
#define YOGGIE_KEY_T 84
#define YOGGIE_KEY_U 85
#define YOGGIE_KEY_V 86
#define YOGGIE_KEY_W 87
#define YOGGIE_KEY_X 88
#define YOGGIE_KEY_Y 89
#define YOGGIE_KEY_Z 90
#define YOGGIE_KEY_LEFT_BRACKET 91 /* [ */
#define YOGGIE_KEY_BACKSLASH 92 /* \ */
#define YOGGIE_KEY_RIGHT_BRACKET 93 /* ] */
#define YOGGIE_KEY_GRAVE_ACCENT 96 /* ` */
#define YOGGIE_KEY_WORLD_1 161 /* non-US #1 */
#define YOGGIE_KEY_WORLD_2 162 /* non-US #2 */
/* Function keys */
#define YOGGIE_KEY_ESCAPE 256
#define YOGGIE_KEY_ENTER 257
#define YOGGIE_KEY_TAB 258
#define YOGGIE_KEY_BACKSPACE 259
#define YOGGIE_KEY_INSERT 260
#define YOGGIE_KEY_DELETE 261
#define YOGGIE_KEY_RIGHT 262
#define YOGGIE_KEY_LEFT 263
#define YOGGIE_KEY_DOWN 264
#define YOGGIE_KEY_UP 265
#define YOGGIE_KEY_PAGE_UP 266
#define YOGGIE_KEY_PAGE_DOWN 267
#define YOGGIE_KEY_HOME 268
#define YOGGIE_KEY_END 269
#define YOGGIE_KEY_CAPS_LOCK 280
#define YOGGIE_KEY_SCROLL_LOCK 281
#define YOGGIE_KEY_NUM_LOCK 282
#define YOGGIE_KEY_PRINT_SCREEN 283
#define YOGGIE_KEY_PAUSE 284
#define YOGGIE_KEY_F1 290
#define YOGGIE_KEY_F2 291
#define YOGGIE_KEY_F3 292
#define YOGGIE_KEY_F4 293
#define YOGGIE_KEY_F5 294
#define YOGGIE_KEY_F6 295
#define YOGGIE_KEY_F7 296
#define YOGGIE_KEY_F8 297
#define YOGGIE_KEY_F9 298
#define YOGGIE_KEY_F10 299
#define YOGGIE_KEY_F11 300
#define YOGGIE_KEY_F12 301
#define YOGGIE_KEY_F13 302
#define YOGGIE_KEY_F14 303
#define YOGGIE_KEY_F15 304
#define YOGGIE_KEY_F16 305
#define YOGGIE_KEY_F17 306
#define YOGGIE_KEY_F18 307
#define YOGGIE_KEY_F19 308
#define YOGGIE_KEY_F20 309
#define YOGGIE_KEY_F21 310
#define YOGGIE_KEY_F22 311
#define YOGGIE_KEY_F23 312
#define YOGGIE_KEY_F24 313
#define YOGGIE_KEY_F25 314
#define YOGGIE_KEY_KP_0 320
#define YOGGIE_KEY_KP_1 321
#define YOGGIE_KEY_KP_2 322
#define YOGGIE_KEY_KP_3 323
#define YOGGIE_KEY_KP_4 324
#define YOGGIE_KEY_KP_5 325
#define YOGGIE_KEY_KP_6 326
#define YOGGIE_KEY_KP_7 327
#define YOGGIE_KEY_KP_8 328
#define YOGGIE_KEY_KP_9 329
#define YOGGIE_KEY_KP_DECIMAL 330
#define YOGGIE_KEY_KP_DIVIDE 331
#define YOGGIE_KEY_KP_MULTIPLY 332
#define YOGGIE_KEY_KP_SUBTRACT 333
#define YOGGIE_KEY_KP_ADD 334
#define YOGGIE_KEY_KP_ENTER 335
#define YOGGIE_KEY_KP_EQUAL 336
#define YOGGIE_KEY_LEFT_SHIFT 340
#define YOGGIE_KEY_LEFT_CONTROL 341
#define YOGGIE_KEY_LEFT_ALT 342
#define YOGGIE_KEY_LEFT_SUPER 343
#define YOGGIE_KEY_RIGHT_SHIFT 344
#define YOGGIE_KEY_RIGHT_CONTROL 345
#define YOGGIE_KEY_RIGHT_ALT 346
#define YOGGIE_KEY_RIGHT_SUPER 347
#define YOGGIE_KEY_MENU 348
#define YOGGIE_KEY_LAST YOGGIE_KEY_MENU
/*
* Modifier keys
*/
// If set one or more Shift keys were held down.
#define YOGGIE_MOD_SHIFT 0x0001
//If set one or more Control keys were held down.
#define YOGGIE_MOD_CONTROL 0x0002
// If set one or more Alt keys were held down.
#define YOGGIE_MOD_ALT 0x0004
// SuperKey(s) (if set, One or more superkeys is held down)
#define YOGGIE_MOD_SUPER 0x0008
// Capslock (if set, Capslock is enabeld)
#define YOGGIE_MOD_CAPS_LOCK 0x0010
// Numlock (if set, numlock is enabled)
#define YOGGIE_MOD_NUM_LOCK 0x0020
/*
* Mouse Keys
*/
#define YOGGIE_MOUSE_BUTTON_1 0
#define YOGGIE_MOUSE_BUTTON_2 1
#define YOGGIE_MOUSE_BUTTON_3 2
#define YOGGIE_MOUSE_BUTTON_4 3
#define YOGGIE_MOUSE_BUTTON_5 4
#define YOGGIE_MOUSE_BUTTON_6 5
#define YOGGIE_MOUSE_BUTTON_7 6
#define YOGGIE_MOUSE_BUTTON_8 7
#define YOGGIE_MOUSE_BUTTON_LAST YOGGIE_MOUSE_BUTTON_8
#define YOGGIE_MOUSE_BUTTON_LEFT YOGGIE_MOUSE_BUTTON_1
#define YOGGIE_MOUSE_BUTTON_RIGHT YOGGIE_MOUSE_BUTTON_2
#define YOGGIE_MOUSE_BUTTON_MIDDLE YOGGIE_MOUSE_BUTTON_3
}

View File

@ -8,24 +8,26 @@ namespace YoggieEngine {
}
void EngineInstrumentation::PerfomanceSamplerInit() {
//ES.frames = 0;
//EngineInstrumentation::lastSampleTime = GetPrecisionTime();
std::cout << "Initialize perf sampler" << std::endl;
/*EngineInstrumentation::frames = 0;
EngineInstrumentation::lastSampleTime = GetPrecisionTime();*/
}
void EngineInstrumentation::Update() {
/*
uint64_t MilliSecondsPast = GetPrecisionTime() - EngineInstrumentation::lastSampleTime;
/* uint64_t MilliSecondsPast = GetPrecisionTime() - EngineInstrumentation::lastSampleTime;
if (MilliSecondsPast >= 1000) {
ES.frameTime = (float)1000 / ES.frames;
ES.FPS = ES.frames;
ES.frames = 0;
//EngineInstrumentation::lastSampleTime = GetPrecisionTime();
}
*/
EngineInstrumentation::frameTime = (float)1000 / EngineInstrumentation::frames;
EngineInstrumentation::FPS = frames;
EngineInstrumentation::frames = 0;
EngineInstrumentation::lastSampleTime = GetPrecisionTime();
}*/
}

View File

@ -1,20 +1,11 @@
#pragma once
#include "YoggieEngine.h"
namespace YoggieEngine {
struct EngineStatistics {
float frameTime;
uint32_t verts;
uint32_t DC;
int64_t frames;
int64_t FPS;
};
class EngineInstrumentation {
public:
//static int64_t lastSampleTime;
static int64_t lastSampleTime;
static uint64_t GetPrecisionTime();
static void PerfomanceSamplerInit();
@ -22,6 +13,15 @@ namespace YoggieEngine {
static void Update();
static void ShowStats();
private:
static float frameTime;
static uint32_t verts;
static uint32_t DC;
static int64_t frames;
static int64_t FPS;
};
class PerfSampler {

View File

@ -46,6 +46,9 @@ extern "C"
#include "EventSystem/EventEmitter.h"
#include "EventSystem/EventListener.h"
#include "Input/Keyboard.h"
#include "Input/InputManager.h"
#include "Scene/Entity.h"
#include "Scene/Scene.h"
#include "Scene/Components.h"