Add useful ImGui windows

This commit is contained in:
David Allemang
2022-02-08 15:06:10 -05:00
committed by David Allemang
parent cb9a1b2794
commit 10a49a4172

245
main.cpp
View File

@@ -7,92 +7,117 @@
#include "gldebug.hpp"
void show_overlay() {
ImGuiWindowFlags window_flags = ImGuiWindowFlags_None
| ImGuiWindowFlags_NoDecoration
| ImGuiWindowFlags_AlwaysAutoResize
| ImGuiWindowFlags_NoSavedSettings
| ImGuiWindowFlags_NoFocusOnAppearing
| ImGuiWindowFlags_NoNav
| ImGuiWindowFlags_NoMove;
void show_overlay(float* clear_color) {
static std::string gl_vendor = (const char *) glGetString(GL_VENDOR);
static std::string gl_renderer = (const char *) glGetString(GL_RENDERER);
static std::string gl_version = (const char *) glGetString(GL_VERSION);
static std::string glsl_version = (const char *) glGetString(GL_SHADING_LANGUAGE_VERSION);
ImGuiWindowFlags window_flags =
ImGuiWindowFlags_AlwaysAutoResize |
ImGuiWindowFlags_NoSavedSettings |
ImGuiWindowFlags_NoFocusOnAppearing |
ImGuiWindowFlags_NoNav |
ImGuiWindowFlags_NoBringToFrontOnFocus |
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
);
auto window_pos = PAD;
ImGui::SetNextWindowPos(window_pos, ImGuiCond_Always);
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::SetNextWindowCollapsed(true, ImGuiCond_Appearing);
ImGui::Begin("Graphics Information", nullptr, window_flags);
ImGui::Text("GL Vendor | %s", gl_vendor.c_str());
ImGui::Text("GL Renderer | %s", gl_renderer.c_str());
ImGui::Text("GL Version | %s", gl_version.c_str());
ImGui::Text("GLSL Version | %s", glsl_version.c_str());
auto v2 = ImGui::GetWindowSize();
window_pos.y += v2.y + PAD.y;
ImGui::End();
ImGui::SetNextWindowPos(window_pos, ImGuiCond_Always);
ImGui::SetNextWindowBgAlpha(0.35f * style.Alpha);
ImGui::Begin("Controls", nullptr, window_flags);
ImGuiIO &io = ImGui::GetIO();
ImGui::Text("FPS | %.2f", io.Framerate);
ImGui::Separator();
ImGui::ColorEdit3("Background", state.bg, ImGuiColorEditFlags_Float);
ImGui::SliderFloat("Alpha", &state.fg[3], 0.0f, 1.0f, "%.2f");
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();
void set_style() {
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
ImGuiStyle &style = ImGui::GetStyle();
style.WindowRounding = 4;
style.FrameRounding = 2;
style.DisplaySafeAreaPadding.x = 10;
style.DisplaySafeAreaPadding.y = 10;
}
io.Fonts->AddFontDefault();
int run(GLFWwindow *window, ImGuiContext *context) {
State state;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
float points[]{
+0.5f, +0.5f, 0.0f, 1.0f,
+0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, +0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.0f, 1.0f,
};
unsigned int inds[]{
0, 1, 2,
1, 2, 3,
};
GLuint vao;
glCreateVertexArrays(1, &vao);
GLuint vbo;
glCreateBuffers(1, &vbo);
glNamedBufferData(vbo, sizeof(points), (void *) points, GL_STATIC_DRAW);
glEnableVertexArrayAttrib(vao, 0);
glVertexArrayVertexBuffer(vao, 0, vbo, 0, sizeof(float) * 4);
glVertexArrayAttribFormat(vao, 0, 4, GL_FLOAT, GL_FALSE, 0);
GLuint ibo;
glCreateBuffers(1, &ibo);
glGetError();
glNamedBufferData(ibo, sizeof(inds), (void *) inds, GL_STATIC_DRAW);
glVertexArrayElementBuffer(vao, ibo);
const char *vs_src = "#version 440\n"
"layout(location=0) in vec4 pos;"
"void main() {"
" gl_Position = pos;"
"}";
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vs_src, nullptr);
glCompileShader(vs);
const char *fs_src = "#version 440\n"
"layout(location=0) uniform vec4 ucol;"
"layout(location=0) out vec4 col;"
"void main() {"
" col = ucol;"
"}";
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fs_src, nullptr);
glCompileShader(fs);
GLuint pgm = glCreateProgram();
glAttachShader(pgm, vs);
glAttachShader(pgm, fs);
glLinkProgram(pgm);
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
@@ -100,20 +125,78 @@ int main() {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
show_overlay();
show_overlay(state);
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);
glClearColor(state.bg[0], state.bg[1], state.bg[2], state.bg[3]);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glUseProgram(pgm);
glBindVertexArray(vao);
glUniform4fv(0, 1, state.fg);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
glBindVertexArray(0);
glUseProgram(0);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &ibo);
glDeleteVertexArrays(1, &vao);
return EXIT_SUCCESS;
}
int main() {
if (!glfwInit()) {
std::cerr << "GLFW:Failed initialization" << std::endl;
return EXIT_FAILURE;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
auto *window = glfwCreateWindow(1280, 720, "Cosets Visualization", nullptr, nullptr);
if (!window) {
std::cerr << "GLFW:Failed to create window" << std::endl;
return EXIT_FAILURE;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
#ifndef NDEBUG
glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback(log_gl_debug_callback, nullptr);
glDebugMessageControl(
GL_DONT_CARE, GL_DEBUG_TYPE_OTHER,
GL_DEBUG_SEVERITY_NOTIFICATION,
0, nullptr, GL_FALSE
);
#endif
IMGUI_CHECKVERSION();
auto *context = ImGui::CreateContext();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 130");
set_style();
int exit_code = EXIT_SUCCESS;
try {
exit_code = run(window, context);
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
exit_code = EXIT_FAILURE;
}
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
@@ -121,5 +204,5 @@ int main() {
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
return exit_code;
}