| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | #include "imgui.h" |
| 56 | #ifndef IMGUI_DISABLE |
| 57 | #include "imgui_impl_opengl2.h" |
| 58 | #include <stdint.h> |
| 59 | |
| 60 | |
| 61 | #if defined(__clang__) |
| 62 | #pragma clang diagnostic push |
| 63 | #pragma clang diagnostic ignored "-Wunused-macros" |
| 64 | #pragma clang diagnostic ignored "-Wnonportable-system-include-path" |
| 65 | #endif |
| 66 | |
| 67 | |
| 68 | #if defined(_WIN32) && !defined(APIENTRY) |
| 69 | #define APIENTRY __stdcall |
| 70 | #endif |
| 71 | #if defined(_WIN32) && !defined(WINGDIAPI) |
| 72 | #define WINGDIAPI __declspec(dllimport) |
| 73 | #endif |
| 74 | #if defined(__APPLE__) |
| 75 | #define GL_SILENCE_DEPRECATION |
| 76 | #include <OpenGL/gl.h> |
| 77 | #else |
| 78 | #include <GL/gl.h> |
| 79 | #endif |
| 80 | |
| 81 | |
| 82 | |
| 83 | #ifdef IMGUI_IMPL_OPENGL_DEBUG |
| 84 | #include <stdio.h> |
| 85 | #define GL_CALL(_CALL) do { _CALL; GLenum gl_err = glGetError(); if (gl_err != 0) fprintf(stderr, "GL error 0x%x returned from '%s'.\n", gl_err, #_CALL); } while (0) |
| 86 | #else |
| 87 | #define GL_CALL(_CALL) _CALL |
| 88 | #endif |
| 89 | |
| 90 | |
| 91 | struct ImGui_ImplOpenGL2_Data |
| 92 | { |
| 93 | bool UseTexParameterToSetSampler; |
| 94 | GLuint NextSampler; |
| 95 | |
| 96 | ImGui_ImplOpenGL2_Data() { memset((void*)this, 0, sizeof(*this)); } |
| 97 | }; |
| 98 | |
| 99 | |
| 100 | |
| 101 | static ImGui_ImplOpenGL2_Data* ImGui_ImplOpenGL2_GetBackendData() |
| 102 | { |
| 103 | return ImGui::GetCurrentContext() ? (ImGui_ImplOpenGL2_Data*)ImGui::GetIO().BackendRendererUserData : nullptr; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | static void ImGui_ImplOpenGL2_InitMultiViewportSupport(); |
| 108 | static void ImGui_ImplOpenGL2_ShutdownMultiViewportSupport(); |
| 109 | |
| 110 | |
| 111 | void ImGui_ImplOpenGL2_NewFrame() |
| 112 | { |
| 113 | ImGui_ImplOpenGL2_Data* bd = ImGui_ImplOpenGL2_GetBackendData(); |
| 114 | IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplOpenGL2_Init()?"); |
| 115 | IM_UNUSED(bd); |
| 116 | } |
| 117 | |
| 118 | static void ImGui_ImplOpenGL2_SetupRenderState(ImDrawData* draw_data, int fb_width, int fb_height) |
| 119 | { |
| 120 | |
| 121 | ImGui_ImplOpenGL2_Data* bd = ImGui_ImplOpenGL2_GetBackendData(); |
| 122 | glEnable(GL_BLEND); |
| 123 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 124 | |
| 125 | glDisable(GL_CULL_FACE); |
| 126 | glDisable(GL_DEPTH_TEST); |
| 127 | glDisable(GL_STENCIL_TEST); |
| 128 | glDisable(GL_LIGHTING); |
| 129 | glDisable(GL_COLOR_MATERIAL); |
| 130 | glEnable(GL_SCISSOR_TEST); |
| 131 | glEnableClientState(GL_VERTEX_ARRAY); |
| 132 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 133 | glEnableClientState(GL_COLOR_ARRAY); |
| 134 | glDisableClientState(GL_NORMAL_ARRAY); |
| 135 | glEnable(GL_TEXTURE_2D); |
| 136 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
| 137 | glShadeModel(GL_SMOOTH); |
| 138 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
| 139 | bd->NextSampler = GL_LINEAR; |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | GL_CALL(glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height)); |
| 155 | glMatrixMode(GL_PROJECTION); |
| 156 | glPushMatrix(); |
| 157 | glLoadIdentity(); |
| 158 | glOrtho(draw_data->DisplayPos.x, draw_data->DisplayPos.x + draw_data->DisplaySize.x, draw_data->DisplayPos.y + draw_data->DisplaySize.y, draw_data->DisplayPos.y, -1.0f, +1.0f); |
| 159 | glMatrixMode(GL_MODELVIEW); |
| 160 | glPushMatrix(); |
| 161 | glLoadIdentity(); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | static void ImGui_ImplOpenGL2_DrawCallback_ResetRenderState(const ImDrawList*, const ImDrawCmd*) {} |
| 166 | static void ImGui_ImplOpenGL2_DrawCallback_SetSamplerLinear(const ImDrawList*, const ImDrawCmd*) { ImGui_ImplOpenGL2_Data* bd = ImGui_ImplOpenGL2_GetBackendData(); bd->UseTexParameterToSetSampler = true; bd->NextSampler = GL_LINEAR; } |
| 167 | static void ImGui_ImplOpenGL2_DrawCallback_SetSamplerNearest(const ImDrawList*, const ImDrawCmd*) { ImGui_ImplOpenGL2_Data* bd = ImGui_ImplOpenGL2_GetBackendData(); bd->UseTexParameterToSetSampler = true; bd->NextSampler = GL_NEAREST; } |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | void ImGui_ImplOpenGL2_RenderDrawData(ImDrawData* draw_data) |
| 173 | { |
| 174 | |
| 175 | ImGui_ImplOpenGL2_Data* bd = ImGui_ImplOpenGL2_GetBackendData(); |
| 176 | int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x); |
| 177 | int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y); |
| 178 | if (fb_width == 0 || fb_height == 0) |
| 179 | return; |
| 180 | |
| 181 | |
| 182 | |
| 183 | if (draw_data->Textures != nullptr) |
| 184 | for (ImTextureData* tex : *draw_data->Textures) |
| 185 | if (tex->Status != ImTextureStatus_OK) |
| 186 | ImGui_ImplOpenGL2_UpdateTexture(tex); |
| 187 | |
| 188 | |
| 189 | GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture); |
| 190 | GLint last_polygon_mode[2]; glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode); |
| 191 | GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport); |
| 192 | GLint last_scissor_box[4]; glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box); |
| 193 | GLint last_shade_model; glGetIntegerv(GL_SHADE_MODEL, &last_shade_model); |
| 194 | GLint last_tex_env_mode; glGetTexEnviv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &last_tex_env_mode); |
| 195 | glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT); |
| 196 | |
| 197 | |
| 198 | ImGui_ImplOpenGL2_SetupRenderState(draw_data, fb_width, fb_height); |
| 199 | |
| 200 | |
| 201 | ImVec2 clip_off = draw_data->DisplayPos; |
| 202 | ImVec2 clip_scale = draw_data->FramebufferScale; |
| 203 | |
| 204 | |
| 205 | for (const ImDrawList* draw_list : draw_data->CmdLists) |
| 206 | { |
| 207 | const ImDrawVert* vtx_buffer = draw_list->VtxBuffer.Data; |
| 208 | const ImDrawIdx* idx_buffer = draw_list->IdxBuffer.Data; |
| 209 | glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (const GLvoid*)((const char*)vtx_buffer + offsetof(ImDrawVert, pos))); |
| 210 | glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (const GLvoid*)((const char*)vtx_buffer + offsetof(ImDrawVert, uv))); |
| 211 | glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (const GLvoid*)((const char*)vtx_buffer + offsetof(ImDrawVert, col))); |
| 212 | |
| 213 | for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++) |
| 214 | { |
| 215 | const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i]; |
| 216 | if (pcmd->UserCallback) |
| 217 | { |
| 218 | |
| 219 | if (pcmd->UserCallback == ImGui_ImplOpenGL2_DrawCallback_ResetRenderState) |
| 220 | ImGui_ImplOpenGL2_SetupRenderState(draw_data, fb_width, fb_height); |
| 221 | else |
| 222 | pcmd->UserCallback(draw_list, pcmd); |
| 223 | } |
| 224 | else |
| 225 | { |
| 226 | |
| 227 | ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y); |
| 228 | ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y); |
| 229 | if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y) |
| 230 | continue; |
| 231 | |
| 232 | |
| 233 | glScissor((int)clip_min.x, (int)((float)fb_height - clip_max.y), (int)(clip_max.x - clip_min.x), (int)(clip_max.y - clip_min.y)); |
| 234 | |
| 235 | |
| 236 | GLuint pcmd_texture = (GLuint)(intptr_t)pcmd->GetTexID(); |
| 237 | glBindTexture(GL_TEXTURE_2D, pcmd_texture); |
| 238 | |
| 239 | |
| 240 | |
| 241 | if (bd->UseTexParameterToSetSampler) |
| 242 | { |
| 243 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, bd->NextSampler); |
| 244 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, bd->NextSampler); |
| 245 | } |
| 246 | |
| 247 | glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer + pcmd->IdxOffset); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | |
| 253 | glDisableClientState(GL_COLOR_ARRAY); |
| 254 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 255 | glDisableClientState(GL_VERTEX_ARRAY); |
| 256 | glBindTexture(GL_TEXTURE_2D, (GLuint)last_texture); |
| 257 | glMatrixMode(GL_MODELVIEW); |
| 258 | glPopMatrix(); |
| 259 | glMatrixMode(GL_PROJECTION); |
| 260 | glPopMatrix(); |
| 261 | glPopAttrib(); |
| 262 | glPolygonMode(GL_FRONT, (GLenum)last_polygon_mode[0]); glPolygonMode(GL_BACK, (GLenum)last_polygon_mode[1]); |
| 263 | glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]); |
| 264 | glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]); |
| 265 | glShadeModel(last_shade_model); |
| 266 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, last_tex_env_mode); |
| 267 | } |
| 268 | |
| 269 | void ImGui_ImplOpenGL2_UpdateTexture(ImTextureData* tex) |
| 270 | { |
| 271 | if (tex->Status == ImTextureStatus_WantCreate) |
| 272 | { |
| 273 | |
| 274 | |
| 275 | IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr); |
| 276 | IM_ASSERT(tex->Format == ImTextureFormat_RGBA32); |
| 277 | const void* pixels = tex->GetPixels(); |
| 278 | GLuint gl_texture_id = 0; |
| 279 | |
| 280 | |
| 281 | |
| 282 | GLint last_texture; |
| 283 | GL_CALL(glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture)); |
| 284 | GL_CALL(glGenTextures(1, &gl_texture_id)); |
| 285 | GL_CALL(glBindTexture(GL_TEXTURE_2D, gl_texture_id)); |
| 286 | GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); |
| 287 | GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); |
| 288 | GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)); |
| 289 | GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)); |
| 290 | GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, 0)); |
| 291 | GL_CALL(glPixelStorei(GL_UNPACK_ALIGNMENT, 1)); |
| 292 | GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->Width, tex->Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels)); |
| 293 | |
| 294 | |
| 295 | tex->SetTexID((ImTextureID)(intptr_t)gl_texture_id); |
| 296 | tex->SetStatus(ImTextureStatus_OK); |
| 297 | |
| 298 | |
| 299 | GL_CALL(glBindTexture(GL_TEXTURE_2D, last_texture)); |
| 300 | } |
| 301 | else if (tex->Status == ImTextureStatus_WantUpdates) |
| 302 | { |
| 303 | |
| 304 | |
| 305 | GLint last_texture; |
| 306 | GL_CALL(glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture)); |
| 307 | |
| 308 | GLuint gl_tex_id = (GLuint)(intptr_t)tex->TexID; |
| 309 | GL_CALL(glBindTexture(GL_TEXTURE_2D, gl_tex_id)); |
| 310 | GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, tex->Width)); |
| 311 | GL_CALL(glPixelStorei(GL_UNPACK_ALIGNMENT, 1)); |
| 312 | for (ImTextureRect& r : tex->Updates) |
| 313 | GL_CALL(glTexSubImage2D(GL_TEXTURE_2D, 0, r.x, r.y, r.w, r.h, GL_RGBA, GL_UNSIGNED_BYTE, tex->GetPixelsAt(r.x, r.y))); |
| 314 | GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, 0)); |
| 315 | GL_CALL(glBindTexture(GL_TEXTURE_2D, last_texture)); |
| 316 | tex->SetStatus(ImTextureStatus_OK); |
| 317 | } |
| 318 | else if (tex->Status == ImTextureStatus_WantDestroy) |
| 319 | { |
| 320 | GLuint gl_tex_id = (GLuint)(intptr_t)tex->TexID; |
| 321 | glDeleteTextures(1, &gl_tex_id); |
| 322 | |
| 323 | |
| 324 | tex->SetTexID(ImTextureID_Invalid); |
| 325 | tex->SetStatus(ImTextureStatus_Destroyed); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | bool ImGui_ImplOpenGL2_CreateDeviceObjects() |
| 330 | { |
| 331 | return true; |
| 332 | } |
| 333 | |
| 334 | void ImGui_ImplOpenGL2_DestroyDeviceObjects() |
| 335 | { |
| 336 | for (ImTextureData* tex : ImGui::GetPlatformIO().Textures) |
| 337 | if (tex->RefCount == 1) |
| 338 | { |
| 339 | tex->SetStatus(ImTextureStatus_WantDestroy); |
| 340 | ImGui_ImplOpenGL2_UpdateTexture(tex); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | bool ImGui_ImplOpenGL2_Init() |
| 345 | { |
| 346 | ImGuiIO& io = ImGui::GetIO(); |
| 347 | IMGUI_CHECKVERSION(); |
| 348 | IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!"); |
| 349 | |
| 350 | |
| 351 | ImGui_ImplOpenGL2_Data* bd = IM_NEW(ImGui_ImplOpenGL2_Data)(); |
| 352 | io.BackendRendererUserData = (void*)bd; |
| 353 | io.BackendRendererName = "imgui_impl_opengl2"; |
| 354 | io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; |
| 355 | io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; |
| 356 | |
| 357 | ImGui_ImplOpenGL2_InitMultiViewportSupport(); |
| 358 | |
| 359 | ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| 360 | platform_io.DrawCallback_ResetRenderState = ImGui_ImplOpenGL2_DrawCallback_ResetRenderState; |
| 361 | platform_io.DrawCallback_SetSamplerLinear = ImGui_ImplOpenGL2_DrawCallback_SetSamplerLinear; |
| 362 | platform_io.DrawCallback_SetSamplerNearest = ImGui_ImplOpenGL2_DrawCallback_SetSamplerNearest; |
| 363 | |
| 364 | return true; |
| 365 | } |
| 366 | |
| 367 | void ImGui_ImplOpenGL2_Shutdown() |
| 368 | { |
| 369 | ImGui_ImplOpenGL2_Data* bd = ImGui_ImplOpenGL2_GetBackendData(); |
| 370 | IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?"); |
| 371 | ImGuiIO& io = ImGui::GetIO(); |
| 372 | ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| 373 | |
| 374 | ImGui_ImplOpenGL2_ShutdownMultiViewportSupport(); |
| 375 | ImGui_ImplOpenGL2_DestroyDeviceObjects(); |
| 376 | |
| 377 | io.BackendRendererName = nullptr; |
| 378 | io.BackendRendererUserData = nullptr; |
| 379 | io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasTextures | ImGuiBackendFlags_RendererHasViewports); |
| 380 | platform_io.ClearRendererHandlers(); |
| 381 | IM_DELETE(bd); |
| 382 | } |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | static void ImGui_ImplOpenGL2_RenderWindow(ImGuiViewport* viewport, void*) |
| 391 | { |
| 392 | if (!(viewport->Flags & ImGuiViewportFlags_NoRendererClear)) |
| 393 | { |
| 394 | ImVec4 clear_color = ImVec4(0.0f, 0.0f, 0.0f, 1.0f); |
| 395 | glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w); |
| 396 | glClear(GL_COLOR_BUFFER_BIT); |
| 397 | } |
| 398 | ImGui_ImplOpenGL2_RenderDrawData(viewport->DrawData); |
| 399 | } |
| 400 | |
| 401 | static void ImGui_ImplOpenGL2_InitMultiViewportSupport() |
| 402 | { |
| 403 | ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| 404 | platform_io.Renderer_RenderWindow = ImGui_ImplOpenGL2_RenderWindow; |
| 405 | } |
| 406 | |
| 407 | static void ImGui_ImplOpenGL2_ShutdownMultiViewportSupport() |
| 408 | { |
| 409 | ImGui::DestroyPlatformWindows(); |
| 410 | } |
| 411 | |
| 412 | |
| 413 | |
| 414 | #if defined(__clang__) |
| 415 | #pragma clang diagnostic pop |
| 416 | #endif |
| 417 | |
| 418 | #endif |