terravisor/source/main.cpp

141 lines
3.8 KiB
C++
Raw Normal View History

2024-08-12 17:56:29 -04:00
/*
* --------------------------------------------------------------------------
*
* ___________ ____ ____.__
* \__ ___/______________________\ \ / /|__| _________________
* | |_/ __ \_ __ \_ __ \__ \\ Y / | |/ ___/ _ \_ __ \
* | |\ ___/| | \/| | \// __ \\ / | |\___ ( <_> ) | \/
* |____| \___ >__| |__| (____ /\___/ |__/____ >____/|__|
* \/ \/ \/
*
* TerraVisor
* Dynamic Terrain Visualization
*
* --------------------------------------------------------------------------
* Author: Jack Christensen
* Version: 0.0.1
*
* Description:
* TerraVisor is a dynamic terrain visualization tool that uses real-world elevation
* data to render high-fidelity 3D landscapes. It leverages tessellation shaders
* to provide detailed and adaptive terrain rendering.
*
* License:
* This project is licensed under the GNU General Public License v3.0.
* See the COPYING file for more details.
*/
2024-08-10 19:12:21 -04:00
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
2024-08-11 21:20:17 -04:00
#include <chrono>
#include <thread>
2024-08-10 19:12:21 -04:00
2024-08-11 21:20:17 -04:00
#include <imgui.h>
2024-08-11 14:52:38 -04:00
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
2024-08-10 19:12:21 -04:00
#include "scene.h"
2024-08-13 16:25:56 -04:00
namespace {
2024-08-11 21:20:17 -04:00
const int kInitWindowWidth = 1280;
const int kInitWindowHeight = 720;
2024-08-13 16:25:56 -04:00
const int kTargetFps = 60;
const auto kFrameDuration = std::chrono::milliseconds(1000 / kTargetFps);
bool InitializeGlfw() {
2024-08-13 18:09:49 -04:00
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl;
return false;
}
return true;
2024-08-13 16:25:56 -04:00
}
2024-08-10 19:12:21 -04:00
2024-08-13 16:25:56 -04:00
GLFWwindow* CreateGlfwWindow() {
#ifdef _DEBUG
2024-08-13 18:09:49 -04:00
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
2024-08-13 16:25:56 -04:00
#endif
2024-08-12 21:40:28 -04:00
2024-08-13 18:09:49 -04:00
GLFWwindow* window = glfwCreateWindow(kInitWindowWidth, kInitWindowHeight, "TerraVisor", nullptr, nullptr);
if (!window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return nullptr;
}
2024-08-10 19:12:21 -04:00
2024-08-13 18:09:49 -04:00
glfwMakeContextCurrent(window);
return window;
2024-08-13 16:25:56 -04:00
}
bool InitializeGlew() {
2024-08-13 18:09:49 -04:00
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
std::cerr << "Failed to initialize GLEW" << std::endl;
return false;
}
return true;
2024-08-13 16:25:56 -04:00
}
2024-08-11 14:52:38 -04:00
2024-08-13 16:25:56 -04:00
void InitializeImGui(GLFWwindow* window) {
2024-08-13 18:09:49 -04:00
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 150");
2024-08-13 16:25:56 -04:00
}
void CleanupImGui() {
2024-08-13 18:09:49 -04:00
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
2024-08-13 16:25:56 -04:00
}
void MainLoop(GLFWwindow* window, Scene& scene) {
2024-08-13 18:09:49 -04:00
auto last_frame_time = std::chrono::high_resolution_clock::now();
2024-08-11 21:20:17 -04:00
2024-08-13 18:09:49 -04:00
while (!glfwWindowShouldClose(window)) {
auto frame_start = std::chrono::high_resolution_clock::now();
2024-08-11 21:20:17 -04:00
2024-08-13 18:09:49 -04:00
scene.Idle();
scene.Display(window);
glfwPollEvents();
2024-08-11 21:20:17 -04:00
2024-08-13 18:09:49 -04:00
auto frame_end = std::chrono::high_resolution_clock::now();
auto frame_duration = frame_end - frame_start;
2024-08-11 21:20:17 -04:00
2024-08-13 18:09:49 -04:00
if (frame_duration < kFrameDuration) {
std::this_thread::sleep_for(kFrameDuration - frame_duration);
}
2024-08-11 21:20:17 -04:00
2024-08-13 18:09:49 -04:00
auto current_frame_time = std::chrono::high_resolution_clock::now();
auto actual_frame_duration = std::chrono::duration_cast<std::chrono::milliseconds>(current_frame_time - last_frame_time).count();
2024-08-11 21:20:17 -04:00
2024-08-13 18:09:49 -04:00
last_frame_time = current_frame_time;
}
2024-08-13 16:25:56 -04:00
}
2024-08-10 19:12:21 -04:00
2024-08-13 16:25:56 -04:00
} // namespace
int main() {
2024-08-13 18:09:49 -04:00
if (!InitializeGlfw()) return -1;
2024-08-11 14:52:38 -04:00
2024-08-13 18:09:49 -04:00
GLFWwindow* window = CreateGlfwWindow();
if (!window) return -1;
2024-08-13 16:25:56 -04:00
2024-08-13 18:09:49 -04:00
if (!InitializeGlew()) return -1;
2024-08-13 16:25:56 -04:00
2024-08-13 18:09:49 -04:00
Scene scene(kInitWindowWidth, kInitWindowHeight);
scene.Init();
InitializeImGui(window);
2024-08-13 16:25:56 -04:00
2024-08-13 18:09:49 -04:00
MainLoop(window, scene);
2024-08-13 16:25:56 -04:00
2024-08-13 18:09:49 -04:00
CleanupImGui();
glfwDestroyWindow(window);
glfwTerminate();
2024-08-13 16:25:56 -04:00
2024-08-13 18:09:49 -04:00
return 0;
2024-08-10 19:12:21 -04:00
}