Visualizer/src/Application.cpp

191 lines
4.1 KiB
C++
Raw Normal View History

2021-12-20 03:31:44 +00:00
#include "Application.hpp"
2021-12-24 14:45:55 +00:00
#include <iostream>
2021-12-20 03:31:44 +00:00
#include <stdexcept>
#include <sstream>
2021-12-20 03:50:23 +00:00
#include <glad/glad.h>
2021-12-20 03:31:44 +00:00
#include <GLFW/glfw3.h>
2021-12-21 14:22:47 +00:00
#include "imgui.h"
#include "backends/imgui_impl_glfw.h"
#include "backends/imgui_impl_opengl3.h"
2021-12-20 03:31:44 +00:00
2021-12-21 13:54:22 +00:00
#ifdef NDEBUG
#define FULLSCREEN
#endif
2021-12-20 03:31:44 +00:00
Application::~Application()
2021-12-24 15:22:16 +00:00
{
}
void Application::Quit()
2021-12-20 03:31:44 +00:00
{
2021-12-21 14:22:47 +00:00
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
2021-12-24 15:22:16 +00:00
if (window != nullptr)
2021-12-20 03:31:44 +00:00
{
2021-12-25 02:15:50 +00:00
delete topology;
2021-12-20 03:31:44 +00:00
glfwDestroyWindow(window);
window = nullptr;
}
glfwTerminate();
}
void Application::Init(int width, int height, const std::string& title)
{
2021-12-20 03:50:23 +00:00
// Initialize GLFW
2021-12-20 23:51:18 +00:00
if (window == nullptr)
2021-12-20 03:31:44 +00:00
glfwInit();
2021-12-21 13:54:22 +00:00
int windowWidth = width, windowHeight = height;
GLFWmonitor* monitor = NULL;
#ifdef FULLSCREEN
monitor = glfwGetPrimaryMonitor();
2021-12-20 22:12:34 +00: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 13:54:22 +00:00
windowWidth = mode->width;
windowHeight = mode->height;
#endif
2021-12-20 03:50:23 +00:00
// Create GLFW window
2021-12-21 13:54:22 +00:00
window = glfwCreateWindow(windowWidth, windowHeight, title.c_str(), monitor, NULL);
2021-12-20 03:31:44 +00: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 03:50:23 +00:00
// Set up OpenGL
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
glfwDestroyWindow(window);
window = nullptr;
glfwTerminate();
throw std::runtime_error("Failed to initialize GLAD");
}
2021-12-21 13:54:22 +00:00
glViewport(0, 0, windowWidth, windowHeight);
2021-12-20 23:51:18 +00:00
glEnable(GL_DEPTH_TEST);
2021-12-20 03:50:23 +00:00
// Register GLFW callbacks
2021-12-20 23:51:18 +00:00
glfwSetFramebufferSizeCallback(window,
2021-12-20 03:50:23 +00:00
[](GLFWwindow* window, int width, int height)
{
2021-12-21 17:14:01 +00:00
WindowData* data = (WindowData*)glfwGetWindowUserPointer(window);
float aspectRatio = (float)width / (float)height;
2021-12-22 14:45:52 +00:00
data->camera->Update(100.0f, aspectRatio, 0.01f, 100.0f);
2021-12-21 17:14:01 +00:00
2021-12-20 03:50:23 +00:00
glViewport(0, 0, width, height);
}
);
2021-12-20 18:09:52 +00:00
2021-12-20 22:12:34 +00: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 17:14:01 +00:00
glfwSetWindowUserPointer(window, &data);
2021-12-21 14:22:47 +00: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 17:14:01 +00:00
float aspectRatio = (float)windowWidth / (float)windowHeight;
2021-12-22 18:19:53 +00:00
camera = OrbitingCamera(glm::vec3(0.0f, 0.0f, 0.0f), 6.0f);
2021-12-24 14:14:35 +00:00
camera.Update(100.0f, aspectRatio, 0.01f, 100.0f);
2021-12-25 15:51:55 +00:00
pitch = 45.0f;
yaw = 90.0f;
distance = 6.0f;
2021-12-24 22:34:01 +00:00
2021-12-25 02:15:50 +00:00
data.camera = &camera;
2021-12-24 14:45:55 +00:00
2021-12-25 14:01:15 +00:00
topology = new Topology(glm::vec2(15.0f, 7.5f), glm::uvec2(200, 100));
2021-12-25 02:15:50 +00:00
glm::uvec2 size = topology->GetSize();
2021-12-22 14:45:52 +00:00
2021-12-25 02:15:50 +00:00
float* pixels = topology->GetTopology();
for (unsigned int y = 0; y < size.y; y++)
{
for (unsigned int x = 0; x < size.x; x++)
{
2021-12-25 15:51:55 +00:00
pixels[y * size.x + x] = 0.5f + (cos(x * glm::two_pi<float>() / ((float)size.x * 0.5f)) - cos(y * glm::pi<float>() / ((float)size.y * 0.5f))) * 0.5f;
2021-12-25 02:15:50 +00:00
}
}
2021-12-21 17:14:01 +00:00
2021-12-25 02:15:50 +00:00
topology->MakeTexture();
2021-12-21 17:14:01 +00:00
2021-12-21 19:13:54 +00:00
glfwWindowHint(GLFW_SAMPLES, 4);
2021-12-22 14:45:52 +00:00
// glEnable(GL_CULL_FACE);
2021-12-21 19:13:54 +00:00
glEnable(GL_MULTISAMPLE);
2021-12-20 03:31:44 +00:00
}
void Application::Launch()
{
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
2021-12-25 02:15:50 +00:00
2021-12-25 15:51:55 +00:00
camera.SetPosition(pitch, yaw, distance);
2021-12-22 18:19:53 +00:00
2021-12-20 15:58:47 +00:00
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
2021-12-20 23:51:18 +00:00
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2021-12-20 03:50:23 +00:00
2021-12-25 02:15:50 +00:00
camera.Draw(*topology);
2021-12-21 14:22:47 +00:00
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::Begin("Debug");
2021-12-21 17:14:01 +00:00
if (ImGui::CollapsingHeader("Camera"))
{
2021-12-22 18:19:53 +00:00
ImGui::SliderFloat("Yaw", &yaw, 0.0f, 360.0f);
ImGui::SliderFloat("Pitch", &pitch, 1.0f, 179.0f);
2021-12-25 15:51:55 +00:00
ImGui::SliderFloat("Distance", &distance, 1.0f, 14.0f);
2021-12-21 17:14:01 +00:00
}
2021-12-21 14:22:47 +00:00
ImGui::End();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
2021-12-20 03:31:44 +00:00
glfwSwapBuffers(window);
}
}