Skip to content

Commit bf074dd

Browse files
committed
Implement imgui demo menu
1 parent c360f50 commit bf074dd

4 files changed

Lines changed: 45 additions & 0 deletions

File tree

include/window.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Window {
2727
Window& operator=(const Window&) = delete;
2828

2929
bool ShouldClose() const { return glfwWindowShouldClose(window_m); }
30+
GLFWwindow* GetWindow() const { return window_m; }
3031
int GetWidth() const;
3132
int GetHeight() const;
3233
void PollEvents() const { glfwPollEvents(); }

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ target_link_libraries(renderer
2020
glfw
2121
glm::glm
2222
assimp
23+
imgui
2324
)

src/main.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
#include "shader.h"
1717
#include "window.h"
1818

19+
#include "imgui.h"
20+
#include "imgui_impl_glfw.h"
21+
#include "imgui_impl_opengl3.h"
22+
1923
int main(int argc, const char** const argv) {
2024
if (argc != 2) {
2125
std::cout << std::format("Usage: {} FILE\n", argv[0]);
@@ -29,6 +33,16 @@ int main(int argc, const char** const argv) {
2933
}
3034

3135
Window window({});
36+
// Setup Dear ImGui context
37+
IMGUI_CHECKVERSION();
38+
ImGui::CreateContext();
39+
ImGuiIO& io = ImGui::GetIO();
40+
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
41+
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
42+
43+
// Setup Platform/Renderer backends
44+
ImGui_ImplGlfw_InitForOpenGL(window.GetWindow(), true); // Second param install_callback=true will install GLFW callbacks and chain to existing ones.
45+
ImGui_ImplOpenGL3_Init();
3246

3347
Model model = Model(model_path);
3448
Shader shader = Shader("../shaders/vertex.vs", "../shaders/fragment.fs");
@@ -51,6 +65,10 @@ int main(int argc, const char** const argv) {
5165
while (!window.ShouldClose()) {
5266
// handling input
5367
window.ProcessInput(camera);
68+
ImGui_ImplOpenGL3_NewFrame();
69+
ImGui_ImplGlfw_NewFrame();
70+
ImGui::NewFrame();
71+
ImGui::ShowDemoWindow();
5472

5573
// Rendering
5674
// glClearColor(0.7f, 0.3f, 0.3f, 1.0f);
@@ -80,6 +98,9 @@ int main(int argc, const char** const argv) {
8098

8199
model.Draw(shader);
82100

101+
ImGui::Render();
102+
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
103+
83104
// swap buffers and poll for IO events
84105
window.SwapBuffers();
85106
window.PollEvents();
@@ -99,5 +120,9 @@ int main(int argc, const char** const argv) {
99120
past_time = std::chrono::steady_clock::now();
100121
}
101122
}
123+
ImGui_ImplOpenGL3_Shutdown();
124+
ImGui_ImplGlfw_Shutdown();
125+
ImGui::DestroyContext();
126+
102127
return 0;
103128
}

thirdparty/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,22 @@ set(ASSIMP_BUILD_SAMPLES OFF CACHE BOOL "" FORCE)
99
set(ASSIMP_INSTALL OFF CACHE BOOL "" FORCE)
1010
add_subdirectory(assimp)
1111

12+
# imgui setup
13+
add_library(imgui STATIC
14+
imgui/imgui.cpp
15+
imgui/imgui_demo.cpp
16+
imgui/imgui_draw.cpp
17+
imgui/imgui_tables.cpp
18+
imgui/imgui_widgets.cpp
19+
imgui/backends/imgui_impl_glfw.cpp
20+
imgui/backends/imgui_impl_opengl3.cpp
21+
)
22+
target_include_directories(imgui PUBLIC
23+
imgui
24+
imgui/backends)
25+
target_link_libraries(imgui
26+
PUBLIC
27+
glfw
28+
)
29+
1230
glad_add_library(glad_gl_core_33 SHARED API gl:core=3.3)

0 commit comments

Comments
 (0)