Visualizer/src/Application.cpp

196 lines
4.4 KiB
C++
Raw Normal View History

2021-12-20 04:31:44 +01:00
#include "Application.hpp"
#include <stdexcept>
#include <sstream>
2021-12-20 04:50:23 +01:00
#include <glad/glad.h>
2021-12-20 04:31:44 +01:00
#include <GLFW/glfw3.h>
2021-12-21 15:22:47 +01:00
#include "imgui.h"
#include "backends/imgui_impl_glfw.h"
#include "backends/imgui_impl_opengl3.h"
2021-12-20 04:31:44 +01:00
2021-12-21 14:54:22 +01:00
#ifdef NDEBUG
#define FULLSCREEN
#endif
2021-12-20 04:31:44 +01:00
Application::~Application()
{
2021-12-21 15:22:47 +01:00
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
2021-12-21 20:13:54 +01:00
if (grid != nullptr)
delete grid;
2021-12-21 00:51:18 +01:00
2021-12-20 04:31:44 +01:00
if (window != nullptr)
{
glfwDestroyWindow(window);
window = nullptr;
}
glfwTerminate();
}
void Application::Init(int width, int height, const std::string& title)
{
2021-12-20 04:50:23 +01:00
// Initialize GLFW
2021-12-21 00:51:18 +01:00
if (window == nullptr)
2021-12-20 04:31:44 +01:00
glfwInit();
2021-12-21 14:54:22 +01:00
int windowWidth = width, windowHeight = height;
GLFWmonitor* monitor = NULL;
#ifdef FULLSCREEN
monitor = glfwGetPrimaryMonitor();
2021-12-20 23:12:34 +01:00
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
glfwWindowHint(GLFW_RED_BITS, mode->redBits);
glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
2021-12-21 14:54:22 +01:00
windowWidth = mode->width;
windowHeight = mode->height;
#endif
2021-12-20 04:50:23 +01:00
// Create GLFW window
2021-12-21 14:54:22 +01:00
window = glfwCreateWindow(windowWidth, windowHeight, title.c_str(), monitor, NULL);
2021-12-20 04:31:44 +01:00
if (window == nullptr)
{
const char* errorbuf;
int errorcode = glfwGetError(&errorbuf);
glfwTerminate();
std::stringstream errorstream;
errorstream << "Failed to create GLFWwindow (" << errorcode << "): \n" << errorbuf << std::endl;
throw std::runtime_error(errorstream.str());
}
glfwMakeContextCurrent(window);
2021-12-20 04:50:23 +01:00
// Set up OpenGL
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
glfwDestroyWindow(window);
window = nullptr;
glfwTerminate();
throw std::runtime_error("Failed to initialize GLAD");
}
2021-12-21 14:54:22 +01:00
glViewport(0, 0, windowWidth, windowHeight);
2021-12-21 00:51:18 +01:00
glEnable(GL_DEPTH_TEST);
2021-12-20 04:50:23 +01:00
// Register GLFW callbacks
2021-12-21 00:51:18 +01:00
glfwSetFramebufferSizeCallback(window,
2021-12-20 04:50:23 +01:00
[](GLFWwindow* window, int width, int height)
{
2021-12-21 18:14:01 +01:00
WindowData* data = (WindowData*)glfwGetWindowUserPointer(window);
float aspectRatio = (float)width / (float)height;
*(data->camera) = Camera(100.0f, aspectRatio);
data->camera->Move(glm::vec3(0.0f, 0.0f, -4.0f));
*(data->orthoCam) = OrthogonalCamera(-3.0f * aspectRatio, 3.0f * aspectRatio, -3.0f, 3.0f);
2021-12-20 04:50:23 +01:00
glViewport(0, 0, width, height);
}
);
2021-12-20 19:09:52 +01:00
2021-12-20 23:12:34 +01:00
glfwSetKeyCallback(window,
[](GLFWwindow* window, int key, int scancode, int action, int mods)
{
// Close window when pressing ESC
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
{
glfwSetWindowShouldClose(window, true);
}
}
);
2021-12-21 18:14:01 +01:00
glfwSetWindowUserPointer(window, &data);
2021-12-21 15:22:47 +01:00
// Set up ImGui
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 460 core");
ImGui::StyleColorsDark();
2021-12-21 18:14:01 +01:00
float aspectRatio = (float)windowWidth / (float)windowHeight;
camera = Camera(100.0f, aspectRatio);
camera.Move(glm::vec3(0.0f, 0.0f, -4.0f));
orthoCam = OrthogonalCamera(-3.0f * aspectRatio, 3.0f * aspectRatio, -3.0f, 3.0f);
activeCamera = &camera;
2021-12-21 20:13:54 +01:00
grid = new Grid(glm::vec2(5.0f, 4.0f), 25, 20);
2021-12-21 15:22:47 +01:00
cubePosition = glm::vec3(0.0f);
cubeOrientation = glm::vec3(0.0f);
cubeScale = glm::vec3(1.0f);
2021-12-21 18:14:01 +01:00
data.camera = &camera;
data.orthoCam = &orthoCam;
2021-12-21 20:13:54 +01:00
glfwWindowHint(GLFW_SAMPLES, 4);
glEnable(GL_CULL_FACE);
glEnable(GL_MULTISAMPLE);
2021-12-20 04:31:44 +01:00
}
void Application::Launch()
{
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
2021-12-21 20:13:54 +01:00
grid->SetPosition(cubePosition);
grid->SetRotation(cubeOrientation);
grid->SetScale(cubeScale);
2021-12-21 14:54:22 +01:00
2021-12-20 16:58:47 +01:00
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
2021-12-21 00:51:18 +01:00
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2021-12-20 04:50:23 +01:00
2021-12-21 15:22:47 +01:00
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
2021-12-21 20:13:54 +01:00
grid->Draw(*activeCamera);
2021-12-20 19:09:52 +01:00
2021-12-21 15:22:47 +01:00
ImGui::Begin("Debug");
2021-12-21 18:14:01 +01:00
2021-12-21 20:13:54 +01:00
if (ImGui::CollapsingHeader("Grid"))
2021-12-21 18:14:01 +01:00
{
ImGui::SliderFloat3("Position", &(cubePosition[0]), -2.0f, 2.0f);
ImGui::SliderFloat3("Orientation", &(cubeOrientation[0]), 0.0f, glm::two_pi<float>());
ImGui::SliderFloat3("Scale", &(cubeScale[0]), 0.0f, 2.0f);
}
if (ImGui::CollapsingHeader("Camera"))
{
ImGui::Columns(2);
ImGui::Text("Projection: ");
ImGui::NextColumn();
if (ImGui::Button((activeCamera == &camera) ? "Perspective" : "Orthographic"))
{
if (activeCamera == &camera)
activeCamera = &orthoCam;
else
activeCamera = &camera;
}
}
2021-12-21 15:22:47 +01:00
ImGui::End();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
2021-12-20 04:31:44 +01:00
glfwSwapBuffers(window);
}
}