Skip to content

Commit

Permalink
added glm and updated imgui style (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Husenap authored Apr 10, 2021
1 parent 6c89f14 commit 08fa305
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 47 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ option(${PROJECT_NAME}_BUILD_TESTS

include("thirdparty/dubu_log.cmake")
include("thirdparty/dubu_window.cmake")
include("thirdparty/glm.cmake")
add_subdirectory("thirdparty/glad")
include("thirdparty/imgui.cmake")

Expand Down
1 change: 1 addition & 0 deletions dubu_opengl_app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ add_library(${target_name} STATIC ${src_files})
target_link_libraries(${target_name}
dubu_log
dubu_window
glm
glad
imgui
compiler_features
Expand Down
161 changes: 121 additions & 40 deletions dubu_opengl_app/src/dubu_opengl_app/AppBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,14 @@ AppBase::AppBase(const CreateInfo& createInfo)
}

void AppBase::Run() {
mWindow = std::move(std::make_unique<dubu::window::GLFWWindow>(
dubu::window::GLFWWindow::CreateInfo{
.width = mCreateInfo.width,
.height = mCreateInfo.height,
.title = mCreateInfo.appName,
.api = dubu::window::GLFWWindow::Api::OpenGL,
}));

glfwMakeContextCurrent(mWindow->GetGLFWHandle());
if (!gladLoadGL(reinterpret_cast<GLADloadfunc>(glfwGetProcAddress))) {
DUBU_LOG_FATAL("Failed to init glad");
}

IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
InitWindow();

ImGui::StyleColorsDark();

ImGuiStyle& style = ImGui::GetStyle();
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {
style.WindowRounding = 0.0f;
style.Colors[ImGuiCol_WindowBg].w = 1.0f;
}

ImGui_ImplGlfw_InitForOpenGL(mWindow->GetGLFWHandle(), true);
ImGui_ImplOpenGL3_Init("#version 130");

auto resizeToken = mWindow->RegisterListener<dubu::window::EventResize>(
[](const auto& e) { glViewport(0, 0, e.width, e.height); });
auto keyPressToken = mWindow->RegisterListener<dubu::window::EventKeyPress>(
[&](const auto& e) {
if (e.key == GLFW_KEY_ESCAPE) {
glfwSetWindowShouldClose(mWindow->GetGLFWHandle(), GLFW_TRUE);
}
});

glViewport(0, 0, mCreateInfo.width, mCreateInfo.height);
glfwSwapInterval(mCreateInfo.swapInterval);
InitImGui();

Init();

ImGuiIO& io = ImGui::GetIO();

while (!mWindow->ShouldClose()) {
mWindow->PollEvents();

Expand Down Expand Up @@ -85,4 +49,121 @@ void AppBase::Run() {
ImGui::DestroyContext();
}

void AppBase::InitWindow() {
mWindow = std::move(std::make_unique<dubu::window::GLFWWindow>(
dubu::window::GLFWWindow::CreateInfo{
.width = mCreateInfo.width,
.height = mCreateInfo.height,
.title = mCreateInfo.appName,
.api = dubu::window::GLFWWindow::Api::OpenGL,
}));

glfwMakeContextCurrent(mWindow->GetGLFWHandle());
if (!gladLoadGL(reinterpret_cast<GLADloadfunc>(glfwGetProcAddress))) {
DUBU_LOG_FATAL("Failed to init glad");
}

glViewport(0, 0, mCreateInfo.width, mCreateInfo.height);
glfwSwapInterval(mCreateInfo.swapInterval);

mResizeToken = mWindow->RegisterListener<dubu::window::EventResize>(
[](const auto& e) { glViewport(0, 0, e.width, e.height); });
mKeyPressToken = mWindow->RegisterListener<dubu::window::EventKeyPress>(
[&](const auto& e) {
if (e.key == GLFW_KEY_ESCAPE) {
glfwSetWindowShouldClose(mWindow->GetGLFWHandle(), GLFW_TRUE);
}
});
}

void AppBase::InitImGui() {
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;

ImGui_ImplGlfw_InitForOpenGL(mWindow->GetGLFWHandle(), true);
ImGui_ImplOpenGL3_Init("#version 130");

ImGuiStyle& style = ImGui::GetStyle();
ImVec4* colors = style.Colors;

colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
colors[ImGuiCol_TextDisabled] = ImVec4(0.40f, 0.40f, 0.40f, 1.00f);
colors[ImGuiCol_WindowBg] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);
colors[ImGuiCol_ChildBg] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);
colors[ImGuiCol_PopupBg] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);
colors[ImGuiCol_Border] = ImVec4(0.12f, 0.12f, 0.12f, 0.71f);
colors[ImGuiCol_BorderShadow] = ImVec4(1.00f, 1.00f, 1.00f, 0.06f);
colors[ImGuiCol_FrameBg] = ImVec4(0.42f, 0.42f, 0.42f, 0.54f);
colors[ImGuiCol_FrameBgHovered] = ImVec4(0.42f, 0.42f, 0.42f, 0.40f);
colors[ImGuiCol_FrameBgActive] = ImVec4(0.56f, 0.56f, 0.56f, 0.67f);
colors[ImGuiCol_TitleBg] = ImVec4(0.19f, 0.19f, 0.19f, 1.00f);
colors[ImGuiCol_TitleBgActive] = ImVec4(0.22f, 0.22f, 0.22f, 1.00f);
colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.17f, 0.17f, 0.17f, 0.90f);
colors[ImGuiCol_MenuBarBg] = ImVec4(0.33f, 0.33f, 0.33f, 0.75f);
colors[ImGuiCol_ScrollbarBg] = ImVec4(0.24f, 0.24f, 0.24f, 0.53f);
colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.41f, 0.41f, 0.41f, 1.00f);
colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.52f, 0.52f, 0.52f, 1.00f);
colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.76f, 0.76f, 0.76f, 1.00f);
colors[ImGuiCol_CheckMark] = ImVec4(0.65f, 0.65f, 0.65f, 1.00f);
colors[ImGuiCol_SliderGrab] = ImVec4(0.52f, 0.52f, 0.52f, 1.00f);
colors[ImGuiCol_SliderGrabActive] = ImVec4(0.64f, 0.64f, 0.64f, 1.00f);
colors[ImGuiCol_Button] = ImVec4(0.54f, 0.54f, 0.54f, 0.35f);
colors[ImGuiCol_ButtonHovered] = ImVec4(0.52f, 0.52f, 0.52f, 0.59f);
colors[ImGuiCol_ButtonActive] = ImVec4(0.76f, 0.76f, 0.76f, 1.00f);
colors[ImGuiCol_Header] = ImVec4(0.38f, 0.38f, 0.38f, 1.00f);
colors[ImGuiCol_HeaderHovered] = ImVec4(0.47f, 0.47f, 0.47f, 1.00f);
colors[ImGuiCol_HeaderActive] = ImVec4(0.76f, 0.76f, 0.76f, 0.77f);
colors[ImGuiCol_Separator] = ImVec4(0.00f, 0.00f, 0.00f, 0.14f);
colors[ImGuiCol_SeparatorHovered] = ImVec4(0.70f, 0.67f, 0.60f, 0.29f);
colors[ImGuiCol_SeparatorActive] = ImVec4(0.70f, 0.67f, 0.60f, 0.67f);
colors[ImGuiCol_ResizeGrip] = ImVec4(0.99f, 0.78f, 0.61f, 1.00f);
colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.78f, 0.62f, 0.48f, 1.00f);
colors[ImGuiCol_ResizeGripActive] = ImVec4(0.86f, 0.68f, 0.53f, 1.00f);
colors[ImGuiCol_Tab] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);
colors[ImGuiCol_TabHovered] = ImVec4(0.40f, 0.40f, 0.40f, 1.00f);
colors[ImGuiCol_TabActive] = ImVec4(0.33f, 0.33f, 0.33f, 1.00f);
colors[ImGuiCol_TabUnfocused] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);
colors[ImGuiCol_TabUnfocusedActive] = ImVec4(0.33f, 0.33f, 0.33f, 1.00f);
colors[ImGuiCol_DockingPreview] = ImVec4(1.00f, 0.37f, 0.64f, 1.00f);
colors[ImGuiCol_DockingEmptyBg] = ImVec4(0.75f, 0.28f, 0.47f, 1.00f);
colors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f);
colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.72f, 0.30f, 1.00f);
colors[ImGuiCol_PlotHistogram] = ImVec4(0.68f, 0.84f, 0.51f, 1.00f);
colors[ImGuiCol_PlotHistogramHovered] = ImVec4(0.48f, 0.59f, 0.42f, 1.00f);
colors[ImGuiCol_TextSelectedBg] = ImVec4(0.73f, 0.73f, 0.73f, 0.35f);
colors[ImGuiCol_DragDropTarget] = ImVec4(0.51f, 0.83f, 0.98f, 1.00f);
colors[ImGuiCol_NavHighlight] = ImVec4(0.38f, 0.62f, 0.73f, 1.00f);
colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f);
colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f);
colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f);

style.PopupRounding = 3.f;

style.WindowPadding = ImVec2(2.f, 2.f);
style.FramePadding = ImVec2(4.f, 4.f);
style.ItemSpacing = ImVec2(3.f, 3.f);
style.ItemInnerSpacing = ImVec2(3.f, 3.f);

style.ScrollbarSize = 18.f;

style.WindowBorderSize = 1.f;
style.ChildBorderSize = 1.f;
style.PopupBorderSize = 1.f;
style.FrameBorderSize = 0.f;

style.WindowRounding = 2.f;
style.ChildRounding = 2.f;
style.FrameRounding = 2.f;
style.ScrollbarRounding = 2.f;
style.GrabRounding = 3.f;

style.TabBorderSize = 0.f;
style.TabRounding = 3.f;

style.WindowMenuButtonPosition = ImGuiDir_Right;
}

} // namespace dubu::opengl_app
8 changes: 8 additions & 0 deletions dubu_opengl_app/src/dubu_opengl_app/AppBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ class AppBase {
std::unique_ptr<dubu::window::GLFWWindow> mWindow;

private:
void InitWindow();
void InitImGui();

void DrawDockSpace();

CreateInfo mCreateInfo;

dubu::event::Token mResizeToken;
dubu::event::Token mKeyPressToken;
};

} // namespace dubu::opengl_app
15 changes: 8 additions & 7 deletions example/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <dubu_opengl_app/dubu_opengl_app.hpp>
#include <glad/gl.h>
#include <glm/glm.hpp>
#include <imgui/imgui.h>

constexpr int WIDTH = 2048;
constexpr int HEIGHT = 2048;
constexpr glm::ivec2 SIZE{2048, 2048};

class App : public dubu::opengl_app::AppBase {
public:
Expand Down Expand Up @@ -50,8 +50,8 @@ class App : public dubu::opengl_app::AppBase {
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA32F,
WIDTH,
HEIGHT,
SIZE.x,
SIZE.y,
0,
GL_RGBA,
GL_FLOAT,
Expand All @@ -63,7 +63,7 @@ class App : public dubu::opengl_app::AppBase {
virtual void Update() override {
glUseProgram(mProgram);
glUniform1f(0, mOffset);
glDispatchCompute(WIDTH, HEIGHT, 1);
glDispatchCompute(SIZE.x, SIZE.y, 1);

glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);

Expand All @@ -73,11 +73,12 @@ class App : public dubu::opengl_app::AppBase {
ImVec2 offset = regionMin;
ImVec2 regionSize =
ImVec2(regionMax.x - regionMin.x, regionMax.y - regionMin.y);
ImVec2 imageSize = {WIDTH, HEIGHT};
ImVec2 imageSize = {static_cast<float>(SIZE.x),
static_cast<float>(SIZE.y)};

float regionRatio = regionSize.x / regionSize.y;
float imageRatio =
static_cast<float>(WIDTH) / static_cast<float>(HEIGHT);
static_cast<float>(SIZE.x) / static_cast<float>(SIZE.y);

if (regionRatio > imageRatio) {
imageSize.x *= regionSize.y / imageSize.y;
Expand Down
10 changes: 10 additions & 0 deletions thirdparty/glm.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
message("-- External Project: glm")
include(FetchContent)

FetchContent_Declare(
glm
GIT_REPOSITORY https://github.com/g-truc/glm.git
GIT_TAG 761a842a595f58acfc01cc5c7760ff351feaaece
)

FetchContent_MakeAvailable(glm)

0 comments on commit 08fa305

Please sign in to comment.