Add ImGui in Debug Mode,

improvement of input handling
This commit is contained in:
4VRDriver
2020-09-10 15:51:12 +02:00
parent 650c814e20
commit 146392ae22
30 changed files with 41008 additions and 61 deletions

View File

@@ -18,6 +18,7 @@ target_link_libraries(
stb
${GLFW3_LIBRARY}
${OPENGL_LIBRARIES}
imgui
)
target_compile_options(Fall-Fever PRIVATE -Wall -Wextra -pedantic)

View File

@@ -34,22 +34,22 @@ void Camera::lookForward() {
viewMatrix = glm::lookAt(position, position + frontVec, upVec);
}
void Camera::updatePositionFromKeyboardInput(bool *actionCameraRegister, float deltaTime) {
void Camera::updatePositionFromKeyboardInput(bool *cameraActionRegister, float deltaTime) {
glm::vec3 frontVecWithoutY = glm::vec3(frontVec.x, 0.0f, frontVec.z);
glm::vec3 deltaPos = glm::vec3(0.0f, 0.0f, 0.0f);
if(actionCameraRegister[cameraForward])
if(cameraActionRegister[cameraForward])
deltaPos += speed * deltaTime * glm::normalize(frontVecWithoutY);
if(actionCameraRegister[cameraBackward])
if(cameraActionRegister[cameraBackward])
deltaPos -= speed * deltaTime * glm::normalize(frontVecWithoutY);
if(actionCameraRegister[cameraLeft])
if(cameraActionRegister[cameraLeft])
deltaPos -= speed * deltaTime * glm::normalize(glm::cross(frontVec, upVec));
if(actionCameraRegister[cameraRight])
if(cameraActionRegister[cameraRight])
deltaPos += speed * deltaTime * glm::normalize(glm::cross(frontVec, upVec));
if(actionCameraRegister[cameraUp])
if(cameraActionRegister[cameraUp])
deltaPos += speed * deltaTime * upVec;
if(actionCameraRegister[cameraDown])
if(cameraActionRegister[cameraDown])
deltaPos -= speed * deltaTime * upVec;
position += deltaPos;

View File

@@ -1,10 +1,17 @@
#include <iostream>
#include <vector>
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#ifdef _DEBUG
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#endif
#ifdef __linux__
#include <unistd.h>
#endif
@@ -23,21 +30,44 @@ Controller::Controller() {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef _DEBUG
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
glfwSetErrorCallback(error_callback);
#ifndef _DEBUG
glfwWindowHint(GLFW_MAXIMIZED, GL_TRUE);
#endif
gameWindow = new Window();
gameEventHandler = new EventHandler(gameWindow->getGLFWwindow());
camera = new Camera(90.0f, gameWindow->getWindowWidth(), gameWindow->getWindowHeight());
#ifdef _DEBUG
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
glfwSetErrorCallback(error_callback);
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO(); (void)io;
// Setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(gameWindow->getGLFWwindow(), true);
ImGui_ImplOpenGL3_Init("#version 150");
// Setup Dear ImGui style
ImGui::StyleColorsDark();
#endif
}
Controller::~Controller() {
#ifdef _DEBUG
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
#endif
delete gameWindow;
delete gameEventHandler;
delete camera;
glfwTerminate();
}
void Controller::run() {
@@ -48,21 +78,24 @@ void Controller::run() {
std::vector<Entity> scene;
Model model_backpack("res/models/backpack.ffo");
//Model model_plant("res/models/plant.obj");
//Model model_container("res/models/container.ffo");
//Model model_backpack("res/models/backpack.ffo");
//Model model_plant("res/models/plant.ffo");
Model model_container("res/models/container.ffo");
Model model_cube("res/models/cube.ffo");
Model model_dragon("res/models/dragon.ffo");
//Model model_sphere("res/models/sphere.ffo");
Entity backpack(&model_backpack, &shaderProgram);
//Entity backpack(&model_backpack, &shaderProgram);
//Entity sphere(&model_sphere, &shaderProgram);
//Entity container(&model_container, &shaderProgram);
Entity container(&model_container, &shaderProgram);
Entity dragon(&model_dragon, &shaderProgram);
//Entity plant(&model_plant, &shaderProgram);
Entity lightSource(&model_cube, &lightProgram);
lightSource.translate(glm::vec3(-5.0f, 1.0f, 0.0f));
lightSource.scale(0.2f);
lightSource.setScale(0.2f);
//plant.scale(5.0f);
dragon.setScale(0.2f);
glm::vec3 lightColor = glm::vec3(1.0f, 1.0f, 1.0f);
glm::vec3 diffuseColor = lightColor * glm::vec3(1.0f);
@@ -86,7 +119,7 @@ void Controller::run() {
shaderProgram.setUniform("u_material.shininess", 32.0f);
shaderProgram.unbind();
scene.push_back(backpack);
scene.push_back(dragon);
scene.push_back(lightSource);
camera->translate(glm::vec3(0.0f, 0.0f, 7.5f));
@@ -96,14 +129,6 @@ void Controller::run() {
// Timing
limit_framerate();
#ifdef _DEBUG
static uint8_t frameCount = 250;
if(frameCount++ == 255) {
std::cout << "FPS: " << 1/deltaTime << std::endl;
frameCount = 0;
}
#endif
// Update game
// ...
@@ -117,6 +142,10 @@ void Controller::run() {
it->draw(camera->getViewProj(), camera->getPosition());
}
#ifdef _DEBUG
renderImGui(&scene[0]);
#endif
glfwSwapBuffers(gameWindow->getGLFWwindow());
// Update window size
@@ -127,7 +156,10 @@ void Controller::run() {
gameEventHandler->handleEvents();
camera->updatePositionFromKeyboardInput(gameEventHandler->getCameraActionRegister(), deltaTime);
camera->updateDirectionFromMouseInput(gameEventHandler->getCursorDeltaX(), gameEventHandler->getCursorDeltaY());
if(gameWindow->getMouseIsCatched())
camera->updateDirectionFromMouseInput(gameEventHandler->getCursorDeltaX(), gameEventHandler->getCursorDeltaY());
gameWindow->handleActionRegister(gameEventHandler->getWindowActionRegister());
}
}
@@ -163,3 +195,33 @@ void Controller::updateWindowSize() {
camera->updateAspectRatio(gameWindow->getWindowWidth(), gameWindow->getWindowHeight());
gameEventHandler->setFirstMouseInput(1);
}
#ifdef _DEBUG
void Controller::renderImGui(Entity *entity) {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
// render your GUI
ImGui::Begin("Object Modifier");
static float rotation = 0.0;
ImGui::SliderFloat("Rotation", &rotation, 0, 2 * M_PI);
static float translation[] = {0.0f, 0.0f};
ImGui::SliderFloat2("Position", translation, -3.0, 3.0);
static float color[4] = { 1.0f,1.0f,1.0f,1.0f };
entity->setPosition(glm::vec3(translation[0], 0.0f, translation[1]));
//entity->setRotation()
// color picker
ImGui::ColorEdit3("Color", color);
ImGui::End();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
#endif

View File

@@ -7,8 +7,10 @@
#include "Camera.h"
#include "ShaderProgram.h"
#include "VertexBuffer.h"
#include "Entity.h"
#include "defines.h"
class Controller {
public:
@@ -25,6 +27,8 @@ private:
void updateWindowSize();
void renderImGui(Entity *entity);
Window *gameWindow;
EventHandler *gameEventHandler;
Camera *camera;

View File

@@ -11,6 +11,7 @@ Entity::Entity(Model *model, ShaderProgram *shaderProgram)
}
void Entity::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition) {
shaderProgram->bind();
glm::mat4 modelViewProj = viewProjMatrix * modelMatrix;
@@ -27,18 +28,50 @@ void Entity::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition) {
model->draw(shaderProgram);
shaderProgram->unbind();
}
void Entity::translate(glm::vec3 vector) {
position += vector;
modelMatrix = glm::translate(modelMatrix, vector);
updateModelMatrix();
}
void Entity::rotate(glm::vec3 axis, float radians) {
modelMatrix = glm::rotate(modelMatrix, radians, axis);
glm::mat4 rotationMatrix(1.0f);
rotationMatrix = glm::rotate(rotationMatrix, radians, axis);
// Rotate orientation vector
orientation = glm::normalize(glm::vec3(rotationMatrix * glm::vec4(orientation, 1.0)));
updateModelMatrix();
}
void Entity::scale(float scaleFactor) {
void Entity::setPosition(glm::vec3 position) {
this->position = position;
updateModelMatrix();
}
void Entity::setScale(float scaleFactor) {
modelScale = scaleFactor;
modelMatrix = glm::scale(modelMatrix, glm::vec3(scaleFactor, scaleFactor, scaleFactor));
updateModelMatrix();
}
void Entity::updateModelMatrix() {
glm::mat4 newModelMatrix(1.0f);
// Translate * Rotate * Scale * vertex_vec;
// First scaling, then rotation, then translation
// Translate
newModelMatrix = glm::translate(newModelMatrix, position);
// Rotate
glm::vec3 const up(0.0f, 0.0f, 1.0f);
//newModelMatrix = glm::rotate(newModelMatrix, , )
// Scale
newModelMatrix = glm::scale(newModelMatrix, glm::vec3(modelScale, modelScale, modelScale));
modelMatrix = newModelMatrix;
}

View File

@@ -15,15 +15,18 @@ public:
void translate(glm::vec3 vector);
void rotate(glm::vec3 axis, float radians);
void scale(float scaleFactor);
void setPosition(glm::vec3 position);
void setOrientiation(glm::vec3 orientation);
void setScale(float scaleFactor);
glm::vec3 getPosition() { return position; }
glm::mat4 getModelMatrix() { return modelMatrix; }
private:
void updateModelMatrix();
// May be used later...
uint32_t id;

View File

@@ -2,7 +2,11 @@
#include <iostream>
bool EventHandler::wireFrameMode = 0;
bool EventHandler::cameraActionRegister[CAMERA_ACTION_NUM_ITEMS] = {0};
bool EventHandler::windowActionRegister[WINDOW_ACTION_NUM_ITEMS] = {0};
bool EventHandler::firstMouseInput = 1;
EventHandler::EventHandler(GLFWwindow *window)
: window(window) {
@@ -19,7 +23,7 @@ void EventHandler::handleEvents() {
glfwPollEvents();
for(int i=0; i<CAMERA_ACTION_NUM_ITEMS; i++) {
actionCameraRegister[i] = 0;
cameraActionRegister[i] = 0;
}
updateMouseDelta();
@@ -28,32 +32,31 @@ void EventHandler::handleEvents() {
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
actionCameraRegister[cameraForward] = 1;
cameraActionRegister[cameraForward] = 1;
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
actionCameraRegister[cameraBackward] = 1;
cameraActionRegister[cameraBackward] = 1;
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
actionCameraRegister[cameraUp] = 1;
cameraActionRegister[cameraUp] = 1;
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
actionCameraRegister[cameraDown] = 1;
cameraActionRegister[cameraDown] = 1;
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
actionCameraRegister[cameraLeft] = 1;
cameraActionRegister[cameraLeft] = 1;
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
actionCameraRegister[cameraRight] = 1;
cameraActionRegister[cameraRight] = 1;
}
void EventHandler::key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
// Silence warnings of unused variables.
(void)window; (void)scancode; (void)mods;
(void)window; (void)scancode; (void)mods; (void)key; (void)action;
if (key == GLFW_KEY_O && action == GLFW_PRESS) {
wireFrameMode = !wireFrameMode;
if(wireFrameMode) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
} else {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
if(key==GLFW_KEY_O && action == GLFW_PRESS)
windowActionRegister[wireFrameToggle] = 1;
if(key==GLFW_KEY_LEFT_CONTROL && action == GLFW_PRESS) {
windowActionRegister[mouseCatchToggle] = 1;
firstMouseInput = 1;
}
}
void EventHandler::updateMouseDelta() {

View File

@@ -13,7 +13,9 @@ public:
void handleEvents();
bool * getCameraActionRegister() { return actionCameraRegister; }
bool * getCameraActionRegister() { return cameraActionRegister; }
bool * getWindowActionRegister() { return windowActionRegister; }
float getCursorDeltaX() { return deltaCursorPosX; }
float getCursorDeltaY() { return deltaCursorPosY; }
@@ -27,12 +29,11 @@ private:
void updateMouseDelta();
bool actionCameraRegister[CAMERA_ACTION_NUM_ITEMS] = {0};
static bool cameraActionRegister[CAMERA_ACTION_NUM_ITEMS];
static bool windowActionRegister[WINDOW_ACTION_NUM_ITEMS];
GLFWwindow *window;
static bool wireFrameMode;
float lastCursorPosX = 0.0f;
float lastCursorPosY = 0.0f;
float deltaCursorPosX = 0.0f;
@@ -40,6 +41,6 @@ private:
float mouseSensitivity = 0.08f;
bool firstMouseInput = 1;
static bool firstMouseInput;
};

View File

@@ -2,6 +2,7 @@
#include <glad/glad.h>
#include "Window.h"
#include "eventActions.h"
#include "defines.h"
@@ -28,9 +29,10 @@ Window::Window() {
glEnable(GL_DEPTH_TEST);
// Disable mouse cursor
#ifndef _DEBUG
setGrabbedCursor(1);
#ifdef _DEBUG
mouseCatched = false;
#endif
setCatchedCursor(mouseCatched);
#ifdef _DEBUG
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
@@ -63,7 +65,7 @@ bool Window::checkWindowWasResized() {
return 1;
}
void Window::setGrabbedCursor(bool value) {
void Window::setCatchedCursor(bool value) {
if(value) {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
} else {
@@ -71,6 +73,25 @@ void Window::setGrabbedCursor(bool value) {
}
}
void Window::handleActionRegister(bool *windowActionRegister) {
if(windowActionRegister[wireFrameToggle]) {
windowActionRegister[wireFrameToggle] = 0;
wireFrameMode = !wireFrameMode;
if(wireFrameMode) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
} else {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
}
if(windowActionRegister[mouseCatchToggle]) {
windowActionRegister[mouseCatchToggle] = 0;
mouseCatched = !mouseCatched;
setCatchedCursor(mouseCatched);
}
}
// This function is called when the window gets resized (currently not used)
void Window::framebuffer_size_callback(GLFWwindow* window, int width, int height) {
(void)window;

View File

@@ -14,18 +14,25 @@ public:
int getWindowWidth() { return width; }
int getWindowHeight() { return height; }
// Beware: side effect!
bool getMouseIsCatched() { return mouseCatched; }
// side effect!
bool checkWindowWasResized();
void handleActionRegister(bool *windowActionRegister);
private:
static void framebuffer_size_callback(GLFWwindow* window, int width, int height);
static void openGLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam);
void setGrabbedCursor(bool value);
void setCatchedCursor(bool value);
GLFWwindow *window;
int posX, posY;
int width, height;
bool mouseCatched = true;
bool wireFrameMode = false;
};

View File

@@ -1,4 +1,4 @@
enum cameraActions{
enum cameraActions {
cameraUp,
cameraDown,
cameraForward,
@@ -7,3 +7,9 @@ enum cameraActions{
cameraRight,
CAMERA_ACTION_NUM_ITEMS
};
enum windowActions {
wireFrameToggle,
mouseCatchToggle,
WINDOW_ACTION_NUM_ITEMS
};