dependencies/third-party/imgui/src/examples/example_glfw_opengl3/main.cpp
Go to the documentation of this file.
1 // dear imgui: standalone example application for GLFW + OpenGL 3, using programmable pipeline
2 // If you are new to dear imgui, see examples/README.txt and documentation at the top of imgui.cpp.
3 // (GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan/Metal graphics context creation, etc.)
4 
5 #include "imgui.h"
6 #include "imgui_impl_glfw.h"
7 #include "imgui_impl_opengl3.h"
8 #include <stdio.h>
9 
10 // About Desktop OpenGL function loaders:
11 // Modern desktop OpenGL doesn't have a standard portable header file to load OpenGL function pointers.
12 // Helper libraries are often used for this purpose! Here we are supporting a few common ones (gl3w, glew, glad).
13 // You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own.
14 #if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W)
15 #include <GL/gl3w.h> // Initialize with gl3wInit()
16 #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW)
17 #include <GL/glew.h> // Initialize with glewInit()
18 #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD)
19 #include <glad/glad.h> // Initialize with gladLoadGL()
20 #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2)
21 #define GLFW_INCLUDE_NONE // GLFW including OpenGL headers causes ambiguity or multiple definition errors.
22 #include <glbinding/Binding.h> // Initialize with glbinding::Binding::initialize()
23 #include <glbinding/gl/gl.h>
24 using namespace gl;
25 #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3)
26 #define GLFW_INCLUDE_NONE // GLFW including OpenGL headers causes ambiguity or multiple definition errors.
27 #include <glbinding/glbinding.h>// Initialize with glbinding::initialize()
28 #include <glbinding/gl/gl.h>
29 using namespace gl;
30 #else
31 #include IMGUI_IMPL_OPENGL_LOADER_CUSTOM
32 #endif
33 
34 // Include glfw3.h after our OpenGL definitions
35 #include <GLFW/glfw3.h>
36 
37 // [Win32] Our example includes a copy of glfw3.lib pre-compiled with VS2010 to maximize ease of testing and compatibility with old VS compilers.
38 // To link with VS2010-era libraries, VS2015+ requires linking with legacy_stdio_definitions.lib, which we do using this pragma.
39 // Your own project should not be affected, as you are likely to link with a newer binary of GLFW that is adequate for your version of Visual Studio.
40 #if defined(_MSC_VER) && (_MSC_VER >= 1900) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS)
41 #pragma comment(lib, "legacy_stdio_definitions")
42 #endif
43 
44 static void glfw_error_callback(int error, const char* description)
45 {
46  fprintf(stderr, "Glfw Error %d: %s\n", error, description);
47 }
48 
49 int main(int, char**)
50 {
51  // Setup window
53  if (!glfwInit())
54  return 1;
55 
56  // Decide GL+GLSL versions
57 #if __APPLE__
58  // GL 3.2 + GLSL 150
59  const char* glsl_version = "#version 150";
64 #else
65  // GL 3.0 + GLSL 130
66  const char* glsl_version = "#version 130";
69  //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
70  //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only
71 #endif
72 
73  // Create window with graphics context
74  GLFWwindow* window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+OpenGL3 example", NULL, NULL);
75  if (window == NULL)
76  return 1;
77  glfwMakeContextCurrent(window);
78  glfwSwapInterval(1); // Enable vsync
79 
80  // Initialize OpenGL loader
81 #if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W)
82  bool err = gl3wInit() != 0;
83 #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW)
84  bool err = glewInit() != GLEW_OK;
85 #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD)
86  bool err = gladLoadGL() == 0;
87 #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2)
88  bool err = false;
89  glbinding::Binding::initialize();
90 #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3)
91  bool err = false;
92  glbinding::initialize([](const char* name) { return (glbinding::ProcAddress)glfwGetProcAddress(name); });
93 #else
94  bool err = false; // If you use IMGUI_IMPL_OPENGL_LOADER_CUSTOM, your loader is likely to requires some form of initialization.
95 #endif
96  if (err)
97  {
98  fprintf(stderr, "Failed to initialize OpenGL loader!\n");
99  return 1;
100  }
101 
102  // Setup Dear ImGui context
105  ImGuiIO& io = ImGui::GetIO(); (void)io;
106  //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
107  //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
108 
109  // Setup Dear ImGui style
111  //ImGui::StyleColorsClassic();
112 
113  // Setup Platform/Renderer bindings
114  ImGui_ImplGlfw_InitForOpenGL(window, true);
115  ImGui_ImplOpenGL3_Init(glsl_version);
116 
117  // Load Fonts
118  // - 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.
119  // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
120  // - 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).
121  // - 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.
122  // - Read 'docs/FONTS.txt' for more instructions and details.
123  // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
124  //io.Fonts->AddFontDefault();
125  //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
126  //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
127  //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
128  //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
129  //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
130  //IM_ASSERT(font != NULL);
131 
132  // Our state
133  bool show_demo_window = true;
134  bool show_another_window = false;
135  ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
136 
137  // Main loop
138  while (!glfwWindowShouldClose(window))
139  {
140  // Poll and handle events (inputs, window resize, etc.)
141  // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
142  // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
143  // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
144  // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
145  glfwPollEvents();
146 
147  // Start the Dear ImGui frame
150  ImGui::NewFrame();
151 
152  // 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!).
153  if (show_demo_window)
155 
156  // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
157  {
158  static float f = 0.0f;
159  static int counter = 0;
160 
161  ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
162 
163  ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
164  ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
165  ImGui::Checkbox("Another Window", &show_another_window);
166 
167  ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
168  ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
169 
170  if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
171  counter++;
172  ImGui::SameLine();
173  ImGui::Text("counter = %d", counter);
174 
175  ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
176  ImGui::End();
177  }
178 
179  // 3. Show another simple window.
181  {
182  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)
183  ImGui::Text("Hello from another window!");
184  if (ImGui::Button("Close Me"))
185  show_another_window = false;
186  ImGui::End();
187  }
188 
189  // Rendering
190  ImGui::Render();
191  int display_w, display_h;
192  glfwGetFramebufferSize(window, &display_w, &display_h);
193  glViewport(0, 0, display_w, display_h);
197 
198  glfwSwapBuffers(window);
199  }
200 
201  // Cleanup
205 
206  glfwDestroyWindow(window);
207  glfwTerminate();
208 
209  return 0;
210 }
glfwSwapInterval
GLFWAPI void glfwSwapInterval(int interval)
Sets the swap interval for the current context.
name
GLuint const GLchar * name
Definition: glcorearb.h:3055
ImGui_ImplGlfw_InitForOpenGL
bool ImGui_ImplGlfw_InitForOpenGL(GLFWwindow *window, bool install_callbacks)
Definition: imgui_impl_glfw.cpp:224
glfwMakeContextCurrent
GLFWAPI void glfwMakeContextCurrent(GLFWwindow *window)
Makes the context of the specified window current for the calling thread.
imgui_impl_opengl3.h
glfwTerminate
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
NULL
NULL
Definition: test_security_zap.cpp:405
ImVec4::z
float z
Definition: imgui.h:223
ImGui_ImplOpenGL3_RenderDrawData
void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData *draw_data)
Definition: imgui_impl_opengl3.cpp:285
glfwPollEvents
GLFWAPI void glfwPollEvents(void)
Processes all pending events.
ImGui::GetDrawData
IMGUI_API ImDrawData * GetDrawData()
Definition: imgui.cpp:3293
GLFW_OPENGL_PROFILE
#define GLFW_OPENGL_PROFILE
Definition: glfw3.h:655
ImGui::Button
IMGUI_API bool Button(const char *label, const ImVec2 &size=ImVec2(0, 0))
Definition: imgui_widgets.cpp:678
glfw3.h
The header of the GLFW 3 API.
ImGui_ImplGlfw_Shutdown
void ImGui_ImplGlfw_Shutdown()
Definition: imgui_impl_glfw.cpp:234
glfw_error_callback
static void glfw_error_callback(int error, const char *description)
Definition: dependencies/third-party/imgui/src/examples/example_glfw_opengl3/main.cpp:44
setup.description
description
Definition: compatibility_tests/v2.5.0/setup.py:61
ImGui::Render
IMGUI_API void Render()
Definition: imgui.cpp:4222
GLFWwindow
struct GLFWwindow GLFWwindow
Opaque window object.
Definition: glfw3.h:782
imgui.h
ImGui::Text
IMGUI_API void Text(const char *fmt,...) IM_FMTARGS(1)
Definition: imgui_widgets.cpp:247
glViewport
#define glViewport
Definition: gl3w.h:672
ImGui_ImplOpenGL3_Shutdown
void ImGui_ImplOpenGL3_Shutdown()
Definition: imgui_impl_opengl3.cpp:221
ImGui::SameLine
IMGUI_API void SameLine(float offset_from_start_x=0.0f, float spacing=-1.0f)
Definition: imgui.cpp:6923
ImGui_ImplOpenGL3_Init
bool ImGui_ImplOpenGL3_Init(const char *glsl_version)
Definition: imgui_impl_opengl3.cpp:153
imgui_impl_glfw.h
gl3wInit
int gl3wInit(void)
Definition: gl3w.c:111
ImGui::GetIO
IMGUI_API ImGuiIO & GetIO()
Definition: imgui.cpp:3286
glClear
#define glClear
Definition: gl3w.h:639
error
Definition: cJSON.c:88
glfwSetErrorCallback
GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun)
Sets the error callback.
show_another_window
static bool show_another_window
Definition: dependencies/third-party/imgui/src/examples/example_glut_opengl2/main.cpp:23
GLFW_CONTEXT_VERSION_MINOR
#define GLFW_CONTEXT_VERSION_MINOR
Definition: glfw3.h:650
ImGui::End
IMGUI_API void End()
Definition: imgui.cpp:6007
ImVec4::y
float y
Definition: imgui.h:223
glfwSwapBuffers
GLFWAPI void glfwSwapBuffers(GLFWwindow *window)
Swaps the front and back buffers of the specified window.
GLFW_OPENGL_FORWARD_COMPAT
#define GLFW_OPENGL_FORWARD_COMPAT
Definition: glfw3.h:653
ImGui::ShowDemoWindow
IMGUI_API void ShowDemoWindow(bool *p_open=NULL)
Definition: imgui_demo.cpp:208
GLFW_CONTEXT_VERSION_MAJOR
#define GLFW_CONTEXT_VERSION_MAJOR
Definition: glfw3.h:649
ImGui_ImplGlfw_NewFrame
void ImGui_ImplGlfw_NewFrame()
Definition: imgui_impl_glfw.cpp:345
gl3w.h
ImGui::SliderFloat
IMGUI_API bool SliderFloat(const char *label, float *v, float v_min, float v_max, const char *format="%.3f", float power=1.0f)
Definition: imgui_widgets.cpp:2652
ImGui::DestroyContext
IMGUI_API void DestroyContext(ImGuiContext *ctx=NULL)
Definition: imgui.cpp:3276
err
static UPB_NORETURN void err(tarjan *t)
Definition: ruby/ext/google/protobuf_c/upb.c:5856
glClearColor
#define glClearColor
Definition: gl3w.h:640
clear_color
static ImVec4 clear_color
Definition: dependencies/third-party/imgui/src/examples/example_glut_opengl2/main.cpp:24
GL_COLOR_BUFFER_BIT
#define GL_COLOR_BUFFER_BIT
Definition: glcorearb.h:98
glfwInit
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
glfwGetProcAddress
GLFWAPI GLFWglproc glfwGetProcAddress(const char *procname)
Returns the address of the specified function for the current context.
main
int main(int, char **)
Definition: dependencies/third-party/imgui/src/examples/example_glfw_opengl3/main.cpp:49
ImGui::Checkbox
IMGUI_API bool Checkbox(const char *label, bool *v)
Definition: imgui_widgets.cpp:1008
ImGui::CreateContext
IMGUI_API ImGuiContext * CreateContext(ImFontAtlas *shared_font_atlas=NULL)
Definition: imgui.cpp:3267
ImVec4::x
float x
Definition: imgui.h:223
void
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
glfwDestroyWindow
GLFWAPI void glfwDestroyWindow(GLFWwindow *window)
Destroys the specified window and its context.
ImGui::ColorEdit3
IMGUI_API bool ColorEdit3(const char *label, float col[3], ImGuiColorEditFlags flags=0)
Definition: imgui_widgets.cpp:4242
GL_TRUE
#define GL_TRUE
Definition: glcorearb.h:101
ImGui::NewFrame
IMGUI_API void NewFrame()
Definition: imgui.cpp:3691
ImGui::Begin
IMGUI_API bool Begin(const char *name, bool *p_open=NULL, ImGuiWindowFlags flags=0)
Definition: imgui.cpp:5397
ImVec4::w
float w
Definition: imgui.h:223
glfwCreateWindow
GLFWAPI GLFWwindow * glfwCreateWindow(int width, int height, const char *title, GLFWmonitor *monitor, GLFWwindow *share)
Creates a window and its associated context.
ImGuiIO
Definition: imgui.h:1414
glfwWindowHint
GLFWAPI void glfwWindowHint(int hint, int value)
Sets the specified window hint to the desired value.
ImVec4
Definition: imgui.h:221
ImGui_ImplOpenGL3_NewFrame
void ImGui_ImplOpenGL3_NewFrame()
Definition: imgui_impl_opengl3.cpp:226
glfwWindowShouldClose
GLFWAPI int glfwWindowShouldClose(GLFWwindow *window)
Checks the close flag of the specified window.
f
GLfloat f
Definition: glcorearb.h:3964
glfwGetFramebufferSize
GLFWAPI void glfwGetFramebufferSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the framebuffer of the specified window.
GLFW_OPENGL_CORE_PROFILE
#define GLFW_OPENGL_CORE_PROFILE
Definition: glfw3.h:668
show_demo_window
static bool show_demo_window
Definition: dependencies/third-party/imgui/src/examples/example_glut_opengl2/main.cpp:22
ImGui::StyleColorsDark
IMGUI_API void StyleColorsDark(ImGuiStyle *dst=NULL)
Definition: imgui_draw.cpp:178
IMGUI_CHECKVERSION
#define IMGUI_CHECKVERSION()
Definition: imgui.h:64


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:55