From 24b701d95718b16db8cb724100c26f563016e7ac Mon Sep 17 00:00:00 2001 From: Lauchmelder Date: Thu, 18 Nov 2021 15:41:56 +0100 Subject: [PATCH] added ImGui --- .gitmodules | 3 ++ src/CMakeLists.txt | 20 ++++++++++--- src/Camera.cpp | 2 +- src/Model.cpp | 1 + src/main.cpp | 72 +++++++++++++++++++++++++++++++++++++--------- vendor/imgui | 1 + 6 files changed, 81 insertions(+), 18 deletions(-) create mode 160000 vendor/imgui diff --git a/.gitmodules b/.gitmodules index 2562ef5..53c06bb 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "vendor/glm"] path = vendor/glm url = git@github.com:g-truc/glm +[submodule "vendor/imgui"] + path = vendor/imgui + url = git@github.com:ocornut/imgui.git diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b4b77a9..98a0018 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,16 +1,28 @@ add_executable(orbitals "main.cpp" "Model.cpp" "Shader.cpp" "Camera.cpp" "Orbital.cpp") -target_sources(orbitals PRIVATE - ${CMAKE_SOURCE_DIR}/vendor/glad/src/glad.c -) - +# Add GLFW, GLM, GLAD and ImGui include directories to target target_include_directories(orbitals PRIVATE glfw glm ${CMAKE_SOURCE_DIR}/vendor/glad/include + ${CMAKE_SOURCE_DIR}/vendor/imgui ) +# Link to glfw and glm (why?) target_link_libraries(orbitals PRIVATE glfw glm +) + +# Find imgui base source files +file(GLOB IMGUI_SOURCES + ${CMAKE_SOURCE_DIR}/vendor/imgui/*.cpp +) + +# Add sources from 3rd-party libraries (glad & imgui & imgui backends) +target_sources(orbitals PRIVATE + ${CMAKE_SOURCE_DIR}/vendor/glad/src/glad.c + ${IMGUI_SOURCES} + ${CMAKE_SOURCE_DIR}/vendor/imgui/backends/imgui_impl_glfw.cpp + ${CMAKE_SOURCE_DIR}/vendor/imgui/backends/imgui_impl_opengl3.cpp ) \ No newline at end of file diff --git a/src/Camera.cpp b/src/Camera.cpp index 1e7d1cb..26bc230 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -3,7 +3,7 @@ #include Camera::Camera() : - viewMatrix(1.0f), position(0.0f), front({0.0f, 0.0f, -1.0f}), up({0.0f, 1.0f, 0.0f}), yawPitchRoll(0.0f) + viewMatrix(1.0f), position(0.0f), front({ 0.0f, 0.0f, -1.0f }), up({ 0.0f, 1.0f, 0.0f }), yawPitchRoll({ -90.0f, 0.0f, 0.0f }) { } diff --git a/src/Model.cpp b/src/Model.cpp index 2705ece..accf9e1 100644 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -30,6 +30,7 @@ void Model::Draw() { glBindVertexArray(vao); glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0); + glBindVertexArray(0); } void Model::CreateVAO() diff --git a/src/main.cpp b/src/main.cpp index d6e7f98..7f049aa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include "Orbital.hpp" #include "Shader.hpp" @@ -27,12 +29,15 @@ void ProcessInput(GLFWwindow* window); int main(int argc, char** argv) { + // Initialize GLFW and let it know what OpenGL version/profile we're using glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + // Create a new window + // TODO: Magic numbers kinda bad GLFWwindow* window = glfwCreateWindow(1200, 800, "Orbital Demonstation", NULL, NULL); if (window == NULL) { @@ -40,30 +45,49 @@ int main(int argc, char** argv) return -1; } + // Make window context and set callbacks for resizing and keyboard/mouse events glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, OnFramebufferResize); glfwSetCursorPosCallback(window, OnMouseMoved); glfwSetKeyCallback(window, OnKeyPressed); + // Let GLFW trap the cursor (needed for proper camera movement) glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + // Load OpenGL functions from driver if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cerr << "Failed to initialize GLAD" << std::endl; return -1; } + // Set up ImGui + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); + + ImGui_ImplGlfw_InitForOpenGL(window, true); + ImGui_ImplOpenGL3_Init("#version 460 core"); + + ImGui::StyleColorsDark(); + + // Create some orbital and set up its transformation matrix + // TODO: the matrix should probably be part of Model Orbital orbital(2, 1); glm::mat4 modelMatrix = glm::mat4(1.0f); modelMatrix = glm::rotate(modelMatrix, glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f)); modelMatrix = glm::scale(modelMatrix, glm::vec3(3.0f)); + + // Set up a camera + // TODO: should the projection matrix be part of the camera? Camera camera; camera.SetPosition(glm::vec3(0.0f, 0.0f, 3.0f)); glm::mat4 projectionMatrix = glm::perspective(glm::radians(110.0f), 1200.0f / 800.0f, 0.1f, 100.0f); + // Write some shaders to display the orbitals (too lazy to put them in files) Shader shader( R"( #version 460 core @@ -97,46 +121,68 @@ int main(int argc, char** argv) )" ); + // Data that we want to be able to access from anywhere UserData data = { - &projectionMatrix, - &camera, - 0.0, - 0.0, 0.0, - false, - false + &projectionMatrix, // Projection matrix + &camera, // The camera object + 0.0, // Duration of the last frame + 0.0, 0.0, // Mouse position of the last frame + false, // Has the mouse moved before + false // Is the cursor enabled }; glfwSetWindowUserPointer(window, &data); + // Set up a timer to calculate frametimes std::chrono::system_clock::time_point start = std::chrono::system_clock::now(); + // Set viewport and depth buffer glViewport(0, 0, 1200, 800); glEnable(GL_DEPTH_TEST); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable(GL_BLEND); while (!glfwWindowShouldClose(window)) { + // Calculate frametime std::chrono::duration framedelta = std::chrono::duration_cast>(std::chrono::system_clock::now() - start); data.frametime = framedelta.count(); start = std::chrono::system_clock::now(); + // Handle events glfwPollEvents(); ProcessInput(window); - // modelMatrix = glm::rotate(modelMatrix, glm::radians(1.0f), glm::vec3(1.0f, 0.8f, -0.2f)); - - glClearColor(0.0f, 0.0f, 0.1f, 0.0f); + // Clear screen + glClearColor(0.0f, 0.0f, 0.05f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + // Start new ImGui Frame + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + + // Use our shader and set the matrices shader.Bind(); shader.SetMatrix("model", &modelMatrix[0][0]); shader.SetMatrix("view", camera.GetViewMatrix()); shader.SetMatrix("projection", &projectionMatrix[0][0]); - // triangle.Draw(); + + // Finally draw the orbital orbital.Draw(); + ImGui::Begin("Test Window"); + ImGui::Button("Test"); + ImGui::End(); + + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + + // Update swapchain glfwSwapBuffers(window); } + // cleanup + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); + glfwDestroyWindow(window); return 0; @@ -152,7 +198,7 @@ void OnFramebufferResize(GLFWwindow* window, int width, int height) void OnMouseMoved(GLFWwindow* window, double xpos, double ypos) { UserData* data = (UserData*)glfwGetWindowUserPointer(window); - float sensitivity = 5.0f; + float sensitivity = 6.5f; if (!data->mouseMovedBefore) { diff --git a/vendor/imgui b/vendor/imgui new file mode 160000 index 0000000..764f9e6 --- /dev/null +++ b/vendor/imgui @@ -0,0 +1 @@ +Subproject commit 764f9e606d106591072a8e5a79d4e801c4c699fb