Created
August 1, 2020 04:53
-
-
Save sjhalayka/6cd70980ce38e2cb0aa64a46c4abc8b0 to your computer and use it in GitHub Desktop.
Bare bones GLFW + Dear Imgui
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <GL/glew.h> | |
#include <GLFW/glfw3.h> | |
#include "dear imgui/imgui.h" | |
#include "dear imgui/imgui_impl_glfw.h" | |
#include "dear imgui/imgui_impl_opengl3.h" | |
// Automatically link in the GLFW and GLEW libraries if compiling on MSVC++ | |
#ifdef _MSC_VER | |
#pragma comment(lib, "glew32") | |
#pragma comment(lib, "glfw3") // https://github.com/glfw/glfw/releases/download/3.3.2/glfw-3.3.2.bin.WIN64.zip | |
#endif | |
#include <stdio.h> | |
#if defined(_MSC_VER) && (_MSC_VER >= 1900) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) | |
#pragma comment(lib, "legacy_stdio_definitions") | |
#endif | |
GLFWwindow* main_window; | |
bool lmb_down = false; | |
bool mmb_down = false; | |
bool rmb_down = false; | |
GLuint mouse_x = 0; | |
GLuint mouse_y = 0; | |
static void glfw_error_callback(int error, const char* description) | |
{ | |
fprintf(stderr, "Glfw Error %d: %s\n", error, description); | |
} | |
void cursor_position_callback(GLFWwindow* window, double xpos, double ypos) | |
{ | |
int prev_mouse_x = mouse_x; | |
int prev_mouse_y = mouse_y; | |
mouse_x = xpos; | |
mouse_y = ypos; | |
if (ImGui::IsAnyWindowHovered()) | |
return; | |
int mouse_delta_x = mouse_x - prev_mouse_x; | |
int mouse_delta_y = prev_mouse_y - mouse_y; | |
if (true == lmb_down && (0 != mouse_delta_x || 0 != mouse_delta_y)) | |
{ | |
} | |
else if (true == rmb_down && (0 != mouse_delta_y)) | |
{ | |
} | |
} | |
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) | |
{ | |
if (ImGui::IsAnyWindowHovered()) | |
return; | |
if (GLFW_MOUSE_BUTTON_LEFT == button) | |
{ | |
if (action == GLFW_PRESS) | |
lmb_down = true; | |
else | |
lmb_down = false; | |
} | |
else if (GLFW_MOUSE_BUTTON_MIDDLE == button) | |
{ | |
if (action == GLFW_PRESS) | |
mmb_down = true; | |
else | |
mmb_down = false; | |
} | |
else if (GLFW_MOUSE_BUTTON_RIGHT == button) | |
{ | |
if (action == GLFW_PRESS) | |
rmb_down = true; | |
else | |
rmb_down = false; | |
} | |
} | |
void generate_cancel_button_func(void) | |
{ | |
} | |
int main(int, char**) | |
{ | |
// Setup window | |
glfwSetErrorCallback(glfw_error_callback); | |
if (!glfwInit()) | |
return 1; | |
// Decide GL+GLSL versions | |
// GL 3.0 + GLSL 130 | |
const char* glsl_version = "#version 430 core"; | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); | |
GLFWmonitor* monitor = glfwGetPrimaryMonitor(); | |
const GLFWvidmode* mode = glfwGetVideoMode(monitor); | |
// Create window with graphics context | |
main_window = glfwCreateWindow(mode->width, mode->height, "Julia 4D 3 Multithreaded", NULL, NULL); | |
if (main_window == NULL) | |
return 1; | |
glfwMakeContextCurrent(main_window); | |
glfwSwapInterval(1); // Enable vsync | |
bool err = glewInit() != GLEW_OK; | |
if (err) | |
{ | |
fprintf(stderr, "Failed to initialize OpenGL loader!\n"); | |
return 1; | |
} | |
// Setup Dear ImGui context | |
IMGUI_CHECKVERSION(); | |
ImGui::CreateContext(); | |
ImGuiIO& io = ImGui::GetIO(); | |
io.ConfigFlags |= ImGuiConfigFlags_NavEnableSetMousePos; // Enable Keyboard Controls | |
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls | |
io.WantCaptureMouse = true; | |
glfwSetCursorPosCallback(main_window, cursor_position_callback); | |
glfwSetMouseButtonCallback(main_window, mouse_button_callback); | |
// Setup Dear ImGui style | |
ImGui::StyleColorsDark(); | |
//ImGui::StyleColorsClassic(); | |
// Setup Platform/Renderer bindings | |
ImGui_ImplGlfw_InitForOpenGL(main_window, true); | |
ImGui_ImplOpenGL3_Init(glsl_version); | |
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); | |
// Main loop | |
while (!glfwWindowShouldClose(main_window)) | |
{ | |
glfwPollEvents(); | |
// Start the Dear ImGui frame | |
ImGui_ImplOpenGL3_NewFrame(); | |
ImGui_ImplGlfw_NewFrame(); | |
ImGui::NewFrame(); | |
{ | |
ImGui::SetNextWindowSize(ImVec2(400,500)); | |
ImGui::Begin("Controls"); // Create a window called "Hello, world!" and append into it. | |
if (ImGui::Button("Generate mesh")) // Buttons return true when clicked (most widgets return true when edited/activated) | |
generate_cancel_button_func(); | |
ImGui::End(); | |
} | |
// Rendering | |
ImGui::Render(); | |
int display_w, display_h; | |
glfwGetFramebufferSize(main_window, &display_w, &display_h); | |
glViewport(0, 0, display_w, display_h); | |
glClearColor(1, 0.5f, 0, 1); | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glDisable(GL_BLEND); | |
const GLfloat black[] = { 0.0f, 0.0f, 0.0f, 0.0f }; | |
static const GLfloat one = 1.0f; | |
static const GLenum draw_buffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 }; | |
glViewport(0, 0, display_w, display_h); | |
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); | |
glfwSwapBuffers(main_window); | |
} | |
// Cleanup | |
ImGui_ImplOpenGL3_Shutdown(); | |
ImGui_ImplGlfw_Shutdown(); | |
ImGui::DestroyContext(); | |
glfwDestroyWindow(main_window); | |
glfwTerminate(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment