mirror of
https://github.com/allemangD/toddcox-visualize.git
synced 2025-11-10 12:02:47 -05:00
Add useful ImGui windows
This commit is contained in:
245
main.cpp
245
main.cpp
@@ -7,92 +7,117 @@
|
|||||||
|
|
||||||
#include "gldebug.hpp"
|
#include "gldebug.hpp"
|
||||||
|
|
||||||
void show_overlay() {
|
void show_overlay(float* clear_color) {
|
||||||
ImGuiWindowFlags window_flags = ImGuiWindowFlags_None
|
static std::string gl_vendor = (const char *) glGetString(GL_VENDOR);
|
||||||
| ImGuiWindowFlags_NoDecoration
|
static std::string gl_renderer = (const char *) glGetString(GL_RENDERER);
|
||||||
| ImGuiWindowFlags_AlwaysAutoResize
|
static std::string gl_version = (const char *) glGetString(GL_VERSION);
|
||||||
| ImGuiWindowFlags_NoSavedSettings
|
static std::string glsl_version = (const char *) glGetString(GL_SHADING_LANGUAGE_VERSION);
|
||||||
| ImGuiWindowFlags_NoFocusOnAppearing
|
|
||||||
| ImGuiWindowFlags_NoNav
|
ImGuiWindowFlags window_flags =
|
||||||
| ImGuiWindowFlags_NoMove;
|
ImGuiWindowFlags_AlwaysAutoResize |
|
||||||
|
ImGuiWindowFlags_NoSavedSettings |
|
||||||
|
ImGuiWindowFlags_NoFocusOnAppearing |
|
||||||
|
ImGuiWindowFlags_NoNav |
|
||||||
|
ImGuiWindowFlags_NoBringToFrontOnFocus |
|
||||||
|
ImGuiWindowFlags_NoMove;
|
||||||
|
|
||||||
ImGuiStyle &style = ImGui::GetStyle();
|
ImGuiStyle &style = ImGui::GetStyle();
|
||||||
const auto PAD = style.DisplaySafeAreaPadding;
|
const auto PAD = style.DisplaySafeAreaPadding;
|
||||||
const auto *viewport = ImGui::GetMainViewport();
|
auto window_pos = PAD;
|
||||||
const auto work_pos = viewport->WorkPos;
|
|
||||||
const auto work_size = viewport->WorkSize;
|
ImGui::SetNextWindowPos(window_pos, ImGuiCond_Always);
|
||||||
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);
|
ImGui::SetNextWindowBgAlpha(0.35f * style.Alpha);
|
||||||
if (ImGui::Begin("Graphics Information", nullptr, window_flags)) {
|
ImGui::SetNextWindowCollapsed(true, ImGuiCond_Appearing);
|
||||||
ImGui::BeginTable("graphics", 2, ImGuiTableFlags_BordersInnerV);
|
ImGui::Begin("Graphics Information", nullptr, window_flags);
|
||||||
ImGui::TableNextRow();
|
ImGui::Text("GL Vendor | %s", gl_vendor.c_str());
|
||||||
ImGui::TableNextColumn();
|
ImGui::Text("GL Renderer | %s", gl_renderer.c_str());
|
||||||
ImGui::Text("GL Vendor");
|
ImGui::Text("GL Version | %s", gl_version.c_str());
|
||||||
ImGui::TableNextColumn();
|
ImGui::Text("GLSL Version | %s", glsl_version.c_str());
|
||||||
ImGui::Text("%s", glGetString(GL_VENDOR));
|
|
||||||
ImGui::TableNextRow();
|
auto v2 = ImGui::GetWindowSize();
|
||||||
ImGui::TableNextColumn();
|
window_pos.y += v2.y + PAD.y;
|
||||||
ImGui::Text("GL Renderer");
|
ImGui::End();
|
||||||
ImGui::TableNextColumn();
|
|
||||||
ImGui::Text("%s", glGetString(GL_RENDERER));
|
ImGui::SetNextWindowPos(window_pos, ImGuiCond_Always);
|
||||||
ImGui::TableNextRow();
|
ImGui::SetNextWindowBgAlpha(0.35f * style.Alpha);
|
||||||
ImGui::TableNextColumn();
|
ImGui::Begin("Controls", nullptr, window_flags);
|
||||||
ImGui::Text("GL Version");
|
|
||||||
ImGui::TableNextColumn();
|
ImGuiIO &io = ImGui::GetIO();
|
||||||
ImGui::Text("%s", glGetString(GL_VERSION));
|
ImGui::Text("FPS | %.2f", io.Framerate);
|
||||||
ImGui::TableNextRow();
|
|
||||||
ImGui::TableNextColumn();
|
ImGui::Separator();
|
||||||
ImGui::Text("GLSL Version");
|
|
||||||
ImGui::TableNextColumn();
|
ImGui::ColorEdit3("Background", state.bg, ImGuiColorEditFlags_Float);
|
||||||
ImGui::Text("%s", glGetString(GL_SHADING_LANGUAGE_VERSION));
|
ImGui::SliderFloat("Alpha", &state.fg[3], 0.0f, 1.0f, "%.2f");
|
||||||
ImGui::EndTable();
|
|
||||||
ImGui::End();
|
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::StyleColorsDark();
|
||||||
|
|
||||||
ImGui_ImplGlfw_InitForOpenGL(window, true);
|
ImGuiStyle &style = ImGui::GetStyle();
|
||||||
ImGui_ImplOpenGL3_Init(glsl_version);
|
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)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
@@ -100,20 +125,78 @@ int main() {
|
|||||||
ImGui_ImplOpenGL3_NewFrame();
|
ImGui_ImplOpenGL3_NewFrame();
|
||||||
ImGui_ImplGlfw_NewFrame();
|
ImGui_ImplGlfw_NewFrame();
|
||||||
ImGui::NewFrame();
|
ImGui::NewFrame();
|
||||||
show_overlay();
|
show_overlay(state);
|
||||||
ImGui::Render();
|
ImGui::Render();
|
||||||
|
|
||||||
int display_w, display_h;
|
int display_w, display_h;
|
||||||
glfwGetFramebufferSize(window, &display_w, &display_h);
|
glfwGetFramebufferSize(window, &display_w, &display_h);
|
||||||
glViewport(0, 0, 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,
|
glClearColor(state.bg[0], state.bg[1], state.bg[2], state.bg[3]);
|
||||||
clear_color.w);
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
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);
|
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_ImplOpenGL3_Shutdown();
|
||||||
ImGui_ImplGlfw_Shutdown();
|
ImGui_ImplGlfw_Shutdown();
|
||||||
ImGui::DestroyContext();
|
ImGui::DestroyContext();
|
||||||
@@ -121,5 +204,5 @@ int main() {
|
|||||||
glfwDestroyWindow(window);
|
glfwDestroyWindow(window);
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
|
|
||||||
exit(EXIT_SUCCESS);
|
return exit_code;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user