From 2f17710adb3ae4c349085f7aae34f124222d7ec7 Mon Sep 17 00:00:00 2001 From: David Allemang Date: Mon, 7 Feb 2022 21:45:09 -0500 Subject: [PATCH] Initial Commit Basic window with OpenGL, glad, GLFW, ImGui, and Eigen. Dependencies are populated with FetchContent, not submodules. --- .gitignore | 48 +++++++++++++++++ CMakeLists.txt | 14 +++++ External/eigen.cmake | 9 ++++ External/glad.cmake | 9 ++++ External/glfw.cmake | 10 ++++ External/imgui.cmake | 22 ++++++++ main.cpp | 123 +++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 235 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 External/eigen.cmake create mode 100644 External/glad.cmake create mode 100644 External/glfw.cmake create mode 100644 External/imgui.cmake create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..702c390 --- /dev/null +++ b/.gitignore @@ -0,0 +1,48 @@ +# C/C++ +*.d +*.slo +*.lo +*.o +*.obj +*.gch +*.pch +*.so +*.dylib +*.dll +*.mod +*.smod +*.lai +*.la +*.a +*.lib +*.exe +*.out +*.app + +# CMake +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps + +# JetBrains +.idea/ +cmake-build-*/ +*.iws +out/ +.idea_modules/ +atlassian-ide-plugin.xml +.idea/replstate.xml +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties +.idea/httpRequests +.idea/caches/build_file_checksums.ser diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..9b65e0c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.21) +project(toddcox-visualize) + +set(CMAKE_CXX_STANDARD 17) + +include(FetchContent) + +include(External/glfw.cmake) +include(External/imgui.cmake) +include(External/eigen.cmake) +include(External/glad.cmake) + +add_executable(vis main.cpp) +target_link_libraries(vis glfw glad imgui eigen) diff --git a/External/eigen.cmake b/External/eigen.cmake new file mode 100644 index 0000000..78bd394 --- /dev/null +++ b/External/eigen.cmake @@ -0,0 +1,9 @@ +FetchContent_Declare( + eigen + GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git + GIT_TAG 3.4 +) +set(EIGEN_BUILD_DOC OFF CACHE INTERNAL "") +set(BUILD_TESTING OFF CACHE INTERNAL "") +set(EIGEN_BUILD_PKGCONFIG OFF CACHE INTERNAL "") +FetchContent_MakeAvailable(eigen) diff --git a/External/glad.cmake b/External/glad.cmake new file mode 100644 index 0000000..dd71ac1 --- /dev/null +++ b/External/glad.cmake @@ -0,0 +1,9 @@ +FetchContent_Declare( + glad + GIT_REPOSITORY https://github.com/Dav1dde/glad.git + GIT_TAG v0.1.36 +) +set(GLAD_PROFILE "core" CACHE INTERNAL "OpenGL profile") +set(GLAD_API "gl=4.6" CACHE INTERNAL "API type/version pairs, like \"gl=3.2,gles=\", no version means latest") +set(GLAD_GENERATOR "c" CACHE INTERNAL "Language to generate the binding for") +FetchContent_MakeAvailable(glad) diff --git a/External/glfw.cmake b/External/glfw.cmake new file mode 100644 index 0000000..8e88164 --- /dev/null +++ b/External/glfw.cmake @@ -0,0 +1,10 @@ +FetchContent_Declare( + glfw + GIT_REPOSITORY https://github.com/glfw/glfw.git + GIT_TAG 3.3.6 +) +set(GLFW_BUILD_EXAMPLES OFF CACHE INTERNAL "") +set(GLFW_BUILD_TESTS OFF CACHE INTERNAL "") +set(GLFW_BUILD_DOCS OFF CACHE INTERNAL "") +set(GLFW_INSTALL OFF CACHE INTERNAL "") +FetchContent_MakeAvailable(glfw) diff --git a/External/imgui.cmake b/External/imgui.cmake new file mode 100644 index 0000000..d28c60f --- /dev/null +++ b/External/imgui.cmake @@ -0,0 +1,22 @@ +FetchContent_Declare( + imgui + GIT_REPOSITORY https://github.com/ocornut/imgui + GIT_TAG v1.86 +) +FetchContent_MakeAvailable(imgui) + +add_library( + imgui + ${imgui_SOURCE_DIR}/backends/imgui_impl_glfw.cpp + ${imgui_SOURCE_DIR}/backends/imgui_impl_glfw.h + ${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.cpp + ${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.h + ${imgui_SOURCE_DIR}/imgui.cpp + ${imgui_SOURCE_DIR}/imgui.h + ${imgui_SOURCE_DIR}/imgui_demo.cpp + ${imgui_SOURCE_DIR}/imgui_draw.cpp + ${imgui_SOURCE_DIR}/imgui_tables.cpp + ${imgui_SOURCE_DIR}/imgui_widgets.cpp +) +target_include_directories(imgui PUBLIC ${imgui_SOURCE_DIR}) +target_link_libraries(imgui PRIVATE glfw) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..cd2ae0c --- /dev/null +++ b/main.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include + +void show_overlay() { + ImGuiWindowFlags window_flags = ImGuiWindowFlags_None + | ImGuiWindowFlags_NoDecoration + | ImGuiWindowFlags_AlwaysAutoResize + | ImGuiWindowFlags_NoSavedSettings + | ImGuiWindowFlags_NoFocusOnAppearing + | ImGuiWindowFlags_NoNav + | ImGuiWindowFlags_NoMove; + ImGuiStyle &style = ImGui::GetStyle(); + const auto PAD = style.DisplaySafeAreaPadding; + const auto *viewport = ImGui::GetMainViewport(); + const auto work_pos = viewport->WorkPos; + const auto work_size = viewport->WorkSize; + const auto window_pos = ImVec2( + work_pos.x + PAD.x, + work_pos.y + PAD.y + ); + const auto window_pos_pivot = ImVec2( + 0.0f, + 0.0f + ); + ImGui::SetNextWindowPos( + window_pos, + ImGuiCond_Always, + window_pos_pivot + ); + ImGui::SetNextWindowBgAlpha(0.35f * style.Alpha); + if (ImGui::Begin("Graphics Information", nullptr, window_flags)) { + ImGui::BeginTable("graphics", 2, ImGuiTableFlags_BordersInnerV); + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + ImGui::Text("GL Vendor"); + ImGui::TableNextColumn(); + ImGui::Text("%s", glGetString(GL_VENDOR)); + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + ImGui::Text("GL Renderer"); + ImGui::TableNextColumn(); + ImGui::Text("%s", glGetString(GL_RENDERER)); + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + ImGui::Text("GL Version"); + ImGui::TableNextColumn(); + ImGui::Text("%s", glGetString(GL_VERSION)); + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + ImGui::Text("GLSL Version"); + ImGui::TableNextColumn(); + ImGui::Text("%s", glGetString(GL_SHADING_LANGUAGE_VERSION)); + ImGui::EndTable(); + ImGui::End(); + } +} + +int main() { + if (!glfwInit()) { + std::cerr << "GLFW :: failed initialization" << std::endl; + exit(EXIT_FAILURE); + } + + const char *glsl_version = "#version 130"; + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + auto *window = glfwCreateWindow(1280, 720, "Hello OpenGL", nullptr, nullptr); + if (!window) { + std::cerr << "GLFW :: failed to create window" << std::endl; + exit(EXIT_FAILURE); + } + + glfwMakeContextCurrent(window); + glfwSwapInterval(1); + gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); + + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO &io = ImGui::GetIO(); + + ImGui::StyleColorsDark(); + + ImGui_ImplGlfw_InitForOpenGL(window, true); + ImGui_ImplOpenGL3_Init(glsl_version); + + io.Fonts->AddFontDefault(); + + ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); + + while (!glfwWindowShouldClose(window)) { + glfwPollEvents(); + + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + show_overlay(); + ImGui::Render(); + + int display_w, display_h; + glfwGetFramebufferSize(window, &display_w, &display_h); + glViewport(0, 0, display_w, display_h); + glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, + clear_color.w); + glClear(GL_COLOR_BUFFER_BIT); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + + glfwSwapBuffers(window); + } + + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); + + glfwDestroyWindow(window); + glfwTerminate(); + + exit(EXIT_SUCCESS); +}