-
-
Notifications
You must be signed in to change notification settings - Fork 11.5k
Examples: add Gtk3/OpenGL3 example #2032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # | ||
| # You will need GTK+ 3.0 : | ||
| # Linux: | ||
| # apt-get install libgtk-3-dev | ||
| # | ||
|
|
||
| #CXX = g++ | ||
| #CXX = clang++ | ||
|
|
||
| EXE = example_gtk3_opengl3 | ||
| SOURCES = main.cpp | ||
| SOURCES += ../imgui_impl_gtk3.cpp ../imgui_impl_opengl3.cpp | ||
| SOURCES += ../../imgui.cpp ../../imgui_demo.cpp ../../imgui_draw.cpp ../../imgui_widgets.cpp | ||
| SOURCES += ../libs/gl3w/GL/gl3w.c | ||
| OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES)))) | ||
|
|
||
| UNAME_S := $(shell uname -s) | ||
|
|
||
|
|
||
| ifeq ($(UNAME_S), Linux) #LINUX | ||
| ECHO_MESSAGE = "Linux" | ||
| LIBS = -lGL `pkg-config --libs gtk+-3.0 epoxy` -ldl | ||
|
|
||
| CXXFLAGS = -I../ -I../../ -I../libs/gl3w `pkg-config --cflags gtk+-3.0` | ||
| CXXFLAGS += -Wall -Wformat -Wno-deprecated-declarations -Wno-parentheses | ||
| CFLAGS = $(CXXFLAGS) | ||
| endif | ||
|
|
||
| %.o:%.cpp | ||
| $(CXX) $(CXXFLAGS) -c -o $@ $< | ||
|
|
||
| %.o:../%.cpp | ||
| $(CXX) $(CXXFLAGS) -c -o $@ $< | ||
|
|
||
| %.o:../../%.cpp | ||
| $(CXX) $(CXXFLAGS) -c -o $@ $< | ||
|
|
||
| %.o:../libs/gl3w/GL/%.c | ||
| $(CC) $(CFLAGS) -c -o $@ $< | ||
|
|
||
| all: $(EXE) | ||
| @echo Build complete for $(ECHO_MESSAGE) | ||
|
|
||
| $(EXE): $(OBJS) | ||
| $(CXX) -o $@ $^ $(CXXFLAGS) $(LIBS) | ||
|
|
||
| clean: | ||
| rm -f $(EXE) $(OBJS) |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| // ImGui - standalone example application for GLFW + OpenGL 3, using programmable pipeline | ||
| // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp. | ||
| // (GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.) | ||
| // (GL3W is a helper library to access OpenGL functions since there is no standard header to access modern OpenGL functions easily. Alternatives are GLEW, Glad, etc.) | ||
|
|
||
| #include "imgui.h" | ||
| #include "imgui_impl_gtk3.h" | ||
| #include "imgui_impl_opengl3.h" | ||
| #include <stdio.h> | ||
|
|
||
| // About OpenGL function loaders: modern OpenGL doesn't have a standard header file and requires individual function pointers to be loaded manually. | ||
| // Helper libraries are often used for this purpose! Here we are supporting a few common ones: gl3w, glew, glad. | ||
| // You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own. | ||
| #if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) | ||
| #include <GL/gl3w.h> // Initialize with gl3wInit() | ||
| #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) | ||
| #include <GL/glew.h> // Initialize with glewInit() | ||
| #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) | ||
| #include <glad/glad.h> // Initialize with gladLoadGL() | ||
| #else | ||
| #include IMGUI_IMPL_OPENGL_LOADER_CUSTOM | ||
| #endif | ||
|
|
||
| #include <gtk/gtk.h> | ||
|
|
||
| static gboolean render_area(GtkGLArea* area, GdkGLContext* context) | ||
| { | ||
| static bool show_demo_window = true; | ||
| static bool show_another_window = false; | ||
| static ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); | ||
|
|
||
| // Start the Dear ImGui frame | ||
| ImGui_ImplOpenGL3_NewFrame(); | ||
| ImGui_ImplGtk3_NewFrame(); | ||
| ImGui::NewFrame(); | ||
|
|
||
| // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!). | ||
| if (show_demo_window) | ||
| ImGui::ShowDemoWindow(&show_demo_window); | ||
|
|
||
| // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window. | ||
| { | ||
| static float f = 0.0f; | ||
| static int counter = 0; | ||
|
|
||
| ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it. | ||
|
|
||
| ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too) | ||
| ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state | ||
| ImGui::Checkbox("Another Window", &show_another_window); | ||
|
|
||
| ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f | ||
| ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color | ||
|
|
||
| if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated) | ||
| counter++; | ||
| ImGui::SameLine(); | ||
| ImGui::Text("counter = %d", counter); | ||
|
|
||
| ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); | ||
| ImGui::End(); | ||
| } | ||
|
|
||
| // 3. Show another simple window. | ||
| if (show_another_window) | ||
| { | ||
| ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked) | ||
| ImGui::Text("Hello from another window!"); | ||
| if (ImGui::Button("Close Me")) | ||
| show_another_window = false; | ||
| ImGui::End(); | ||
| } | ||
|
|
||
| // Rendering | ||
| ImGui::Render(); | ||
| glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w); | ||
| glClear(GL_COLOR_BUFFER_BIT); | ||
| ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); | ||
|
|
||
| // Queue another rendering request. | ||
| gtk_gl_area_queue_render(area); | ||
|
|
||
| return TRUE; | ||
| } | ||
|
|
||
| static void realize_area(GtkGLArea* area) | ||
| { | ||
| // Setup Dear ImGui binding | ||
| IMGUI_CHECKVERSION(); | ||
| ImGui::CreateContext(); | ||
| ImGuiIO& io = ImGui::GetIO(); (void)io; | ||
| //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls | ||
| //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls | ||
|
|
||
| ImGui_ImplGtk3_Init(GTK_WIDGET(area), true); | ||
| ImGui_ImplOpenGL3_Init("#version 130"); | ||
|
|
||
| // Setup style | ||
| ImGui::StyleColorsDark(); | ||
| //ImGui::StyleColorsClassic(); | ||
|
|
||
| // Load Fonts | ||
| // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them. | ||
| // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple. | ||
| // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit). | ||
| // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call. | ||
| // - Read 'misc/fonts/README.txt' for more instructions and details. | ||
| // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ ! | ||
| //io.Fonts->AddFontDefault(); | ||
| //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f); | ||
| //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f); | ||
| //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f); | ||
| //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f); | ||
| //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese()); | ||
| //IM_ASSERT(font != NULL); | ||
| } | ||
|
|
||
| static void unrealize_area(GtkGLArea* area) | ||
| { | ||
| gtk_gl_area_make_current(area); | ||
|
|
||
| // Cleanup | ||
| ImGui_ImplOpenGL3_Shutdown(); | ||
| ImGui_ImplGtk3_Shutdown(); | ||
| ImGui::DestroyContext(); | ||
| } | ||
|
|
||
| int main(int argc, char** argv) | ||
| { | ||
| gtk_init(&argc, &argv); | ||
|
|
||
| // Create window with graphics context | ||
| GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL); | ||
| gtk_window_set_title(GTK_WINDOW(window), "Dear ImGui GTK+3 & OpenGL3 example"); | ||
| g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL); | ||
| gtk_window_resize(GTK_WINDOW(window), 1280, 720); | ||
|
|
||
| GtkWidget* gl_area = gtk_gl_area_new(); | ||
| gtk_gl_area_set_auto_render(GTK_GL_AREA(gl_area), TRUE); | ||
| g_signal_connect(gl_area, "realize", G_CALLBACK(realize_area), NULL); | ||
| g_signal_connect(gl_area, "unrealize", G_CALLBACK(unrealize_area), NULL); | ||
| g_signal_connect(gl_area, "render", G_CALLBACK(render_area), NULL); | ||
| gtk_container_add(GTK_CONTAINER(window), gl_area); | ||
|
|
||
| gtk_widget_show_all(window); | ||
|
|
||
| gtk_gl_area_make_current(GTK_GL_AREA(gl_area)); | ||
|
|
||
| // Initialize OpenGL loader | ||
| #if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) | ||
| bool err = gl3wInit() != 0; | ||
| #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) | ||
| bool err = glewInit() != GLEW_OK; | ||
| #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) | ||
| bool err = gladLoadGL() != 0; | ||
| #endif | ||
| if (err) | ||
| { | ||
| fprintf(stderr, "Failed to initialize OpenGL loader!\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| // Main loop | ||
| gtk_main(); | ||
|
|
||
| return 0; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.