├── CMakeLists.txt ├── GlfwOcctView.cpp ├── GlfwOcctView.h ├── GlfwOcctWindow.cpp ├── GlfwOcctWindow.h ├── LICENSE ├── README.md ├── imgui ├── imconfig.h ├── imgui.cpp ├── imgui.h ├── imgui_demo.cpp ├── imgui_draw.cpp ├── imgui_impl_glfw.cpp ├── imgui_impl_glfw.h ├── imgui_impl_opengl3.cpp ├── imgui_impl_opengl3.h ├── imgui_impl_opengl3_loader.h ├── imgui_internal.h ├── imgui_tables.cpp ├── imgui_widgets.cpp ├── imstb_rectpack.h ├── imstb_textedit.h └── imstb_truetype.h ├── main.cpp ├── occt-imgui.png └── premake5.lua /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | 3 | # Project configuration 4 | project(OcctImgui LANGUAGES CXX) 5 | 6 | # Set output directories 7 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}) 8 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE}) 9 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE}) 10 | 11 | # Include directories 12 | find_package(glfw3 REQUIRED) 13 | find_package(OpenCASCADE REQUIRED) 14 | 15 | file(GLOB_RECURSE SOURCES 16 | "${CMAKE_SOURCE_DIR}/*.cpp" 17 | "${CMAKE_SOURCE_DIR}/*.h" 18 | ) 19 | 20 | # Exclude CMake-generated files from the SOURCES list 21 | list(FILTER SOURCES EXCLUDE REGEX ".*CMakeFiles.*") 22 | 23 | # Add executable target 24 | add_executable(OcctImgui ${SOURCES}) 25 | 26 | # Link libraries 27 | target_link_libraries(OcctImgui 28 | PRIVATE TKernel TKMath TKG2d TKG3d TKGeomBase TKGeomAlgo TKBRep TKTopAlgo TKPrim TKMesh TKService TKOpenGl TKV3d 29 | glfw 30 | ) 31 | 32 | target_compile_options(OcctImgui PRIVATE 33 | $<$:-g> 34 | $<$:-O3> 35 | ) 36 | 37 | target_link_directories(OcctImgui PRIVATE 38 | $<$:${DEBUG_LIBS}> 39 | $<$:${RELEASE_LIBS}> 40 | ) 41 | 42 | # Debug environment variables (for Windows) 43 | if(WIN32) 44 | set(DEBUG_ENVS "path=%path%;D:/OpenCASCADE-7.7.0/opencascade-7.7.0/win64/vc14/bind") 45 | set(RELEASE_ENVS "path=%path%;D:/OpenCASCADE-7.7.0/opencascade-7.7.0/win64/vc14/bin") 46 | 47 | if(CMAKE_BUILD_TYPE STREQUAL "Debug") 48 | set_target_properties(OcctImgui PROPERTIES VS_DEBUGGER_ENVIRONMENT "${DEBUG_ENVS}") 49 | elseif(CMAKE_BUILD_TYPE STREQUAL "Release") 50 | set_target_properties(OcctImgui PROPERTIES VS_DEBUGGER_ENVIRONMENT "${RELEASE_ENVS}") 51 | endif() 52 | endif() 53 | 54 | -------------------------------------------------------------------------------- /GlfwOcctView.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // 3 | // Copyright(c) 2023 Shing Liu 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files(the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions : 11 | // 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #include "GlfwOcctView.h" 24 | 25 | #include "imgui/imgui_impl_glfw.h" 26 | #include "imgui/imgui_impl_opengl3.h" 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | #include 40 | 41 | #include 42 | 43 | namespace 44 | { 45 | //! Convert GLFW mouse button into Aspect_VKeyMouse. 46 | static Aspect_VKeyMouse mouseButtonFromGlfw(int theButton) 47 | { 48 | switch (theButton) 49 | { 50 | case GLFW_MOUSE_BUTTON_LEFT: return Aspect_VKeyMouse_LeftButton; 51 | case GLFW_MOUSE_BUTTON_RIGHT: return Aspect_VKeyMouse_RightButton; 52 | case GLFW_MOUSE_BUTTON_MIDDLE: return Aspect_VKeyMouse_MiddleButton; 53 | } 54 | return Aspect_VKeyMouse_NONE; 55 | } 56 | 57 | //! Convert GLFW key modifiers into Aspect_VKeyFlags. 58 | static Aspect_VKeyFlags keyFlagsFromGlfw(int theFlags) 59 | { 60 | Aspect_VKeyFlags aFlags = Aspect_VKeyFlags_NONE; 61 | if ((theFlags & GLFW_MOD_SHIFT) != 0) 62 | { 63 | aFlags |= Aspect_VKeyFlags_SHIFT; 64 | } 65 | if ((theFlags & GLFW_MOD_CONTROL) != 0) 66 | { 67 | aFlags |= Aspect_VKeyFlags_CTRL; 68 | } 69 | if ((theFlags & GLFW_MOD_ALT) != 0) 70 | { 71 | aFlags |= Aspect_VKeyFlags_ALT; 72 | } 73 | if ((theFlags & GLFW_MOD_SUPER) != 0) 74 | { 75 | aFlags |= Aspect_VKeyFlags_META; 76 | } 77 | return aFlags; 78 | } 79 | } 80 | 81 | // ================================================================ 82 | // Function : GlfwOcctView 83 | // Purpose : 84 | // ================================================================ 85 | GlfwOcctView::GlfwOcctView() 86 | { 87 | } 88 | 89 | // ================================================================ 90 | // Function : ~GlfwOcctView 91 | // Purpose : 92 | // ================================================================ 93 | GlfwOcctView::~GlfwOcctView() 94 | { 95 | } 96 | 97 | // ================================================================ 98 | // Function : toView 99 | // Purpose : 100 | // ================================================================ 101 | GlfwOcctView* GlfwOcctView::toView(GLFWwindow* theWin) 102 | { 103 | return static_cast(glfwGetWindowUserPointer(theWin)); 104 | } 105 | 106 | // ================================================================ 107 | // Function : errorCallback 108 | // Purpose : 109 | // ================================================================ 110 | void GlfwOcctView::errorCallback(int theError, const char* theDescription) 111 | { 112 | Message::DefaultMessenger()->Send(TCollection_AsciiString("Error") + theError + ": " + theDescription, Message_Fail); 113 | } 114 | 115 | // ================================================================ 116 | // Function : run 117 | // Purpose : 118 | // ================================================================ 119 | void GlfwOcctView::run() 120 | { 121 | initWindow(800, 600, "OCCT IMGUI"); 122 | initViewer(); 123 | initDemoScene(); 124 | if (myView.IsNull()) 125 | { 126 | return; 127 | } 128 | 129 | myView->MustBeResized(); 130 | myOcctWindow->Map(); 131 | initGui(); 132 | mainloop(); 133 | cleanup(); 134 | } 135 | 136 | // ================================================================ 137 | // Function : initWindow 138 | // Purpose : 139 | // ================================================================ 140 | void GlfwOcctView::initWindow(int theWidth, int theHeight, const char* theTitle) 141 | { 142 | glfwSetErrorCallback(GlfwOcctView::errorCallback); 143 | glfwInit(); 144 | const bool toAskCoreProfile = true; 145 | if (toAskCoreProfile) 146 | { 147 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 148 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 149 | #if defined (__APPLE__) 150 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 151 | #endif 152 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 153 | //glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, true); 154 | //glfwWindowHint(GLFW_DECORATED, GL_FALSE); 155 | } 156 | myOcctWindow = new GlfwOcctWindow(theWidth, theHeight, theTitle); 157 | glfwSetWindowUserPointer(myOcctWindow->getGlfwWindow(), this); 158 | 159 | // window callback 160 | glfwSetWindowSizeCallback(myOcctWindow->getGlfwWindow(), GlfwOcctView::onResizeCallback); 161 | glfwSetFramebufferSizeCallback(myOcctWindow->getGlfwWindow(), GlfwOcctView::onFBResizeCallback); 162 | 163 | // mouse callback 164 | glfwSetScrollCallback(myOcctWindow->getGlfwWindow(), GlfwOcctView::onMouseScrollCallback); 165 | glfwSetMouseButtonCallback(myOcctWindow->getGlfwWindow(), GlfwOcctView::onMouseButtonCallback); 166 | glfwSetCursorPosCallback(myOcctWindow->getGlfwWindow(), GlfwOcctView::onMouseMoveCallback); 167 | } 168 | 169 | // ================================================================ 170 | // Function : initViewer 171 | // Purpose : 172 | // ================================================================ 173 | void GlfwOcctView::initViewer() 174 | { 175 | if (myOcctWindow.IsNull() 176 | || myOcctWindow->getGlfwWindow() == nullptr) 177 | { 178 | return; 179 | } 180 | 181 | Handle(OpenGl_GraphicDriver) aGraphicDriver 182 | = new OpenGl_GraphicDriver(myOcctWindow->GetDisplay(), Standard_False); 183 | aGraphicDriver->SetBuffersNoSwap(Standard_True); 184 | 185 | Handle(V3d_Viewer) aViewer = new V3d_Viewer(aGraphicDriver); 186 | aViewer->SetDefaultLights(); 187 | aViewer->SetLightOn(); 188 | aViewer->SetDefaultTypeOfView(V3d_PERSPECTIVE); 189 | aViewer->ActivateGrid(Aspect_GT_Rectangular, Aspect_GDM_Lines); 190 | myView = aViewer->CreateView(); 191 | //myView->SetImmediateUpdate(Standard_False); 192 | myView->SetWindow(myOcctWindow, myOcctWindow->NativeGlContext()); 193 | myView->ChangeRenderingParams().ToShowStats = Standard_True; 194 | 195 | myContext = new AIS_InteractiveContext(aViewer); 196 | 197 | Handle(AIS_ViewCube) aCube = new AIS_ViewCube(); 198 | aCube->SetSize(55); 199 | aCube->SetFontHeight(12); 200 | aCube->SetAxesLabels("", "", ""); 201 | aCube->SetTransformPersistence(new Graphic3d_TransformPers(Graphic3d_TMF_TriedronPers, Aspect_TOTP_LEFT_LOWER, Graphic3d_Vec2i(100, 100))); 202 | aCube->SetViewAnimation(this->ViewAnimation()); 203 | aCube->SetFixedAnimationLoop(false); 204 | myContext->Display(aCube, false); 205 | } 206 | 207 | void GlfwOcctView::initGui() 208 | { 209 | IMGUI_CHECKVERSION(); 210 | ImGui::CreateContext(); 211 | 212 | ImGuiIO& aIO = ImGui::GetIO(); 213 | aIO.ConfigFlags |= ImGuiConfigFlags_DockingEnable; 214 | 215 | ImGui_ImplGlfw_InitForOpenGL(myOcctWindow->getGlfwWindow(), Standard_True); 216 | ImGui_ImplOpenGL3_Init("#version 330"); 217 | 218 | // Setup Dear ImGui style. 219 | //ImGui::StyleColorsClassic(); 220 | } 221 | 222 | void GlfwOcctView::renderGui() 223 | { 224 | ImGuiIO& aIO = ImGui::GetIO(); 225 | 226 | ImGui_ImplOpenGL3_NewFrame(); 227 | ImGui_ImplGlfw_NewFrame(); 228 | 229 | ImGui::NewFrame(); 230 | 231 | ImGui::ShowDemoWindow(); 232 | 233 | // Hello IMGUI. 234 | ImGui::Begin("Hello"); 235 | ImGui::Text("Hello ImGui!"); 236 | ImGui::Text("Hello OpenCASCADE!"); 237 | ImGui::Button("OK"); 238 | ImGui::SameLine(); 239 | ImGui::Button("Cancel"); 240 | ImGui::End(); 241 | 242 | ImGui::Render(); 243 | 244 | ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); 245 | 246 | glfwSwapBuffers(myOcctWindow->getGlfwWindow()); 247 | } 248 | 249 | // ================================================================ 250 | // Function : initDemoScene 251 | // Purpose : 252 | // ================================================================ 253 | void GlfwOcctView::initDemoScene() 254 | { 255 | if (myContext.IsNull()) 256 | { 257 | return; 258 | } 259 | 260 | myView->TriedronDisplay(Aspect_TOTP_LEFT_LOWER, Quantity_NOC_GOLD, 0.08, V3d_WIREFRAME); 261 | 262 | gp_Ax2 anAxis; 263 | anAxis.SetLocation(gp_Pnt(0.0, 0.0, 0.0)); 264 | Handle(AIS_Shape) aBox = new AIS_Shape(BRepPrimAPI_MakeBox(anAxis, 50, 50, 50).Shape()); 265 | myContext->Display(aBox, AIS_Shaded, 0, false); 266 | anAxis.SetLocation(gp_Pnt(25.0, 125.0, 0.0)); 267 | Handle(AIS_Shape) aCone = new AIS_Shape(BRepPrimAPI_MakeCone(anAxis, 25, 0, 50).Shape()); 268 | myContext->Display(aCone, AIS_Shaded, 0, false); 269 | 270 | TCollection_AsciiString aGlInfo; 271 | { 272 | TColStd_IndexedDataMapOfStringString aRendInfo; 273 | myView->DiagnosticInformation(aRendInfo, Graphic3d_DiagnosticInfo_Basic); 274 | for (TColStd_IndexedDataMapOfStringString::Iterator aValueIter(aRendInfo); aValueIter.More(); aValueIter.Next()) 275 | { 276 | if (!aGlInfo.IsEmpty()) { aGlInfo += "\n"; } 277 | aGlInfo += TCollection_AsciiString(" ") + aValueIter.Key() + ": " + aValueIter.Value(); 278 | } 279 | } 280 | Message::DefaultMessenger()->Send(TCollection_AsciiString("OpenGL info:\n") + aGlInfo, Message_Info); 281 | } 282 | 283 | // ================================================================ 284 | // Function : handleViewRedraw 285 | // Purpose : 286 | // ================================================================ 287 | void GlfwOcctView::handleViewRedraw(const Handle(AIS_InteractiveContext)& theCtx, 288 | const Handle(V3d_View)& theView) 289 | { 290 | AIS_ViewController::handleViewRedraw(theCtx, theView); 291 | myToWaitEvents = !myToAskNextFrame; 292 | } 293 | 294 | // ================================================================ 295 | // Function : mainloop 296 | // Purpose : 297 | // ================================================================ 298 | void GlfwOcctView::mainloop() 299 | { 300 | while (!glfwWindowShouldClose(myOcctWindow->getGlfwWindow())) 301 | { 302 | // glfwPollEvents() for continuous rendering (immediate return if there are no new events) 303 | // and glfwWaitEvents() for rendering on demand (something actually happened in the viewer) 304 | if (myToWaitEvents) 305 | { 306 | glfwWaitEvents(); 307 | } 308 | else 309 | { 310 | glfwPollEvents(); 311 | } 312 | if (!myView.IsNull()) 313 | { 314 | myView->InvalidateImmediate(); // redraw view even if it wasn't modified 315 | FlushViewEvents(myContext, myView, Standard_True); 316 | 317 | renderGui(); 318 | } 319 | } 320 | } 321 | 322 | // ================================================================ 323 | // Function : cleanup 324 | // Purpose : 325 | // ================================================================ 326 | void GlfwOcctView::cleanup() 327 | { 328 | // Cleanup IMGUI. 329 | ImGui_ImplOpenGL3_Shutdown(); 330 | ImGui_ImplGlfw_Shutdown(); 331 | ImGui::DestroyContext(); 332 | 333 | if (!myView.IsNull()) 334 | { 335 | myView->Remove(); 336 | } 337 | if (!myOcctWindow.IsNull()) 338 | { 339 | myOcctWindow->Close(); 340 | } 341 | 342 | glfwTerminate(); 343 | } 344 | 345 | // ================================================================ 346 | // Function : onResize 347 | // Purpose : 348 | // ================================================================ 349 | void GlfwOcctView::onResize(int theWidth, int theHeight) 350 | { 351 | if (theWidth != 0 352 | && theHeight != 0 353 | && !myView.IsNull()) 354 | { 355 | myView->Window()->DoResize(); 356 | myView->MustBeResized(); 357 | myView->Invalidate(); 358 | FlushViewEvents(myContext, myView, true); 359 | renderGui(); 360 | } 361 | } 362 | 363 | // ================================================================ 364 | // Function : onMouseScroll 365 | // Purpose : 366 | // ================================================================ 367 | void GlfwOcctView::onMouseScroll(double theOffsetX, double theOffsetY) 368 | { 369 | ImGuiIO& aIO = ImGui::GetIO(); 370 | if (!myView.IsNull() && !aIO.WantCaptureMouse) 371 | { 372 | UpdateZoom(Aspect_ScrollDelta(myOcctWindow->CursorPosition(), int(theOffsetY * 8.0))); 373 | } 374 | } 375 | 376 | // ================================================================ 377 | // Function : onMouseButton 378 | // Purpose : 379 | // ================================================================ 380 | void GlfwOcctView::onMouseButton(int theButton, int theAction, int theMods) 381 | { 382 | ImGuiIO& aIO = ImGui::GetIO(); 383 | if (myView.IsNull() || aIO.WantCaptureMouse) 384 | { 385 | return; 386 | } 387 | 388 | const Graphic3d_Vec2i aPos = myOcctWindow->CursorPosition(); 389 | if (theAction == GLFW_PRESS) 390 | { 391 | PressMouseButton(aPos, mouseButtonFromGlfw(theButton), keyFlagsFromGlfw(theMods), false); 392 | } 393 | else 394 | { 395 | ReleaseMouseButton(aPos, mouseButtonFromGlfw(theButton), keyFlagsFromGlfw(theMods), false); 396 | } 397 | } 398 | 399 | // ================================================================ 400 | // Function : onMouseMove 401 | // Purpose : 402 | // ================================================================ 403 | void GlfwOcctView::onMouseMove(int thePosX, int thePosY) 404 | { 405 | if (myView.IsNull()) 406 | { 407 | return; 408 | } 409 | 410 | ImGuiIO& aIO = ImGui::GetIO(); 411 | if (aIO.WantCaptureMouse) 412 | { 413 | //myView->Redraw(); 414 | } 415 | else 416 | { 417 | const Graphic3d_Vec2i aNewPos(thePosX, thePosY); 418 | UpdateMousePosition(aNewPos, PressedMouseButtons(), LastMouseFlags(), Standard_False); 419 | } 420 | } 421 | -------------------------------------------------------------------------------- /GlfwOcctView.h: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // 3 | // Copyright(c) 2023 Shing Liu 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files(the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions : 11 | // 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #ifndef _GlfwOcctView_Header 24 | #define _GlfwOcctView_Header 25 | 26 | #include "GlfwOcctWindow.h" 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | //! Sample class creating 3D Viewer within GLFW window. 33 | class GlfwOcctView : protected AIS_ViewController 34 | { 35 | public: 36 | //! Default constructor. 37 | GlfwOcctView(); 38 | 39 | //! Destructor. 40 | ~GlfwOcctView(); 41 | 42 | //! Main application entry point. 43 | void run(); 44 | 45 | private: 46 | 47 | //! Create GLFW window. 48 | void initWindow(int theWidth, int theHeight, const char* theTitle); 49 | 50 | //! Create 3D Viewer. 51 | void initViewer(); 52 | 53 | //! Init ImGui. 54 | void initGui(); 55 | 56 | //! Render ImGUI. 57 | void renderGui(); 58 | 59 | //! Fill 3D Viewer with a DEMO items. 60 | void initDemoScene(); 61 | 62 | //! Application event loop. 63 | void mainloop(); 64 | 65 | //! Clean up before . 66 | void cleanup(); 67 | 68 | //! Handle view redraw. 69 | void handleViewRedraw(const Handle(AIS_InteractiveContext)& theCtx, 70 | const Handle(V3d_View)& theView) override; 71 | 72 | //! @name GLWF callbacks 73 | private: 74 | //! Window resize event. 75 | void onResize(int theWidth, int theHeight); 76 | 77 | //! Mouse scroll event. 78 | void onMouseScroll(double theOffsetX, double theOffsetY); 79 | 80 | //! Mouse click event. 81 | void onMouseButton(int theButton, int theAction, int theMods); 82 | 83 | //! Mouse move event. 84 | void onMouseMove(int thePosX, int thePosY); 85 | 86 | //! @name GLWF callbacks (static functions) 87 | private: 88 | 89 | //! GLFW callback redirecting messages into Message::DefaultMessenger(). 90 | static void errorCallback(int theError, const char* theDescription); 91 | 92 | //! Wrapper for glfwGetWindowUserPointer() returning this class instance. 93 | static GlfwOcctView* toView(GLFWwindow* theWin); 94 | 95 | //! Window resize callback. 96 | static void onResizeCallback(GLFWwindow* theWin, int theWidth, int theHeight) 97 | { 98 | toView(theWin)->onResize(theWidth, theHeight); 99 | } 100 | 101 | //! Frame-buffer resize callback. 102 | static void onFBResizeCallback(GLFWwindow* theWin, int theWidth, int theHeight) 103 | { 104 | toView(theWin)->onResize(theWidth, theHeight); 105 | } 106 | 107 | //! Mouse scroll callback. 108 | static void onMouseScrollCallback(GLFWwindow* theWin, double theOffsetX, double theOffsetY) 109 | { 110 | toView(theWin)->onMouseScroll(theOffsetX, theOffsetY); 111 | } 112 | 113 | //! Mouse click callback. 114 | static void onMouseButtonCallback(GLFWwindow* theWin, int theButton, int theAction, int theMods) 115 | { 116 | toView(theWin)->onMouseButton(theButton, theAction, theMods); 117 | } 118 | 119 | //! Mouse move callback. 120 | static void onMouseMoveCallback(GLFWwindow* theWin, double thePosX, double thePosY) 121 | { 122 | toView(theWin)->onMouseMove((int)thePosX, (int)thePosY); 123 | } 124 | 125 | private: 126 | 127 | Handle(GlfwOcctWindow) myOcctWindow; 128 | Handle(V3d_View) myView; 129 | Handle(AIS_InteractiveContext) myContext; 130 | bool myToWaitEvents = true; 131 | 132 | }; 133 | 134 | #endif // _GlfwOcctView_Header 135 | -------------------------------------------------------------------------------- /GlfwOcctWindow.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // 3 | // Copyright(c) 2023 Shing Liu 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files(the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions : 11 | // 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #include "GlfwOcctWindow.h" 24 | 25 | #if defined (__APPLE__) 26 | #undef Handle // avoid name collisions in macOS headers 27 | #define GLFW_EXPOSE_NATIVE_COCOA 28 | #define GLFW_EXPOSE_NATIVE_NSGL 29 | #elif defined (_WIN32) 30 | #define GLFW_EXPOSE_NATIVE_WIN32 31 | #define GLFW_EXPOSE_NATIVE_WGL 32 | #else 33 | #define GLFW_EXPOSE_NATIVE_X11 34 | #define GLFW_EXPOSE_NATIVE_GLX 35 | #endif 36 | 37 | #include 38 | #include 39 | 40 | // ================================================================ 41 | // Function : GlfwOcctWindow 42 | // Purpose : 43 | // ================================================================ 44 | GlfwOcctWindow::GlfwOcctWindow(int theWidth, int theHeight, const TCollection_AsciiString& theTitle) 45 | : myGlfwWindow(glfwCreateWindow(theWidth, theHeight, theTitle.ToCString(), NULL, NULL)), 46 | myXLeft(0), 47 | myYTop(0), 48 | myXRight(0), 49 | myYBottom(0) 50 | { 51 | if (myGlfwWindow != nullptr) 52 | { 53 | int aWidth = 0, aHeight = 0; 54 | glfwGetWindowPos(myGlfwWindow, &myXLeft, &myYTop); 55 | glfwGetWindowSize(myGlfwWindow, &aWidth, &aHeight); 56 | myXRight = myXLeft + aWidth; 57 | myYBottom = myYTop + aHeight; 58 | 59 | #if !defined(_WIN32) && !defined(__APPLE__) 60 | myDisplay = new Aspect_DisplayConnection((Aspect_XDisplay*)glfwGetX11Display()); 61 | #endif 62 | } 63 | } 64 | 65 | // ================================================================ 66 | // Function : Close 67 | // Purpose : 68 | // ================================================================ 69 | void GlfwOcctWindow::Close() 70 | { 71 | if (myGlfwWindow != nullptr) 72 | { 73 | glfwDestroyWindow(myGlfwWindow); 74 | myGlfwWindow = nullptr; 75 | } 76 | } 77 | 78 | // ================================================================ 79 | // Function : NativeHandle 80 | // Purpose : 81 | // ================================================================ 82 | Aspect_Drawable GlfwOcctWindow::NativeHandle() const 83 | { 84 | #if defined (__APPLE__) 85 | return (Aspect_Drawable)glfwGetCocoaWindow(myGlfwWindow); 86 | #elif defined (_WIN32) 87 | return (Aspect_Drawable)glfwGetWin32Window(myGlfwWindow); 88 | #else 89 | return (Aspect_Drawable)glfwGetX11Window(myGlfwWindow); 90 | #endif 91 | } 92 | 93 | // ================================================================ 94 | // Function : NativeGlContext 95 | // Purpose : 96 | // ================================================================ 97 | Aspect_RenderingContext GlfwOcctWindow::NativeGlContext() const 98 | { 99 | #if defined (__APPLE__) 100 | return (NSOpenGLContext*)glfwGetNSGLContext(myGlfwWindow); 101 | #elif defined (_WIN32) 102 | return glfwGetWGLContext(myGlfwWindow); 103 | #else 104 | return glfwGetGLXContext(myGlfwWindow); 105 | #endif 106 | } 107 | 108 | // ================================================================ 109 | // Function : IsMapped 110 | // Purpose : 111 | // ================================================================ 112 | Standard_Boolean GlfwOcctWindow::IsMapped() const 113 | { 114 | return glfwGetWindowAttrib(myGlfwWindow, GLFW_VISIBLE) != 0; 115 | } 116 | 117 | // ================================================================ 118 | // Function : Map 119 | // Purpose : 120 | // ================================================================ 121 | void GlfwOcctWindow::Map() const 122 | { 123 | glfwShowWindow(myGlfwWindow); 124 | } 125 | 126 | // ================================================================ 127 | // Function : Unmap 128 | // Purpose : 129 | // ================================================================ 130 | void GlfwOcctWindow::Unmap() const 131 | { 132 | glfwHideWindow(myGlfwWindow); 133 | } 134 | 135 | // ================================================================ 136 | // Function : DoResize 137 | // Purpose : 138 | // ================================================================ 139 | Aspect_TypeOfResize GlfwOcctWindow::DoResize() 140 | { 141 | if (glfwGetWindowAttrib(myGlfwWindow, GLFW_VISIBLE) == 1) 142 | { 143 | int anXPos = 0, anYPos = 0, aWidth = 0, aHeight = 0; 144 | glfwGetWindowPos(myGlfwWindow, &anXPos, &anYPos); 145 | glfwGetWindowSize(myGlfwWindow, &aWidth, &aHeight); 146 | myXLeft = anXPos; 147 | myXRight = anXPos + aWidth; 148 | myYTop = anYPos; 149 | myYBottom = anYPos + aHeight; 150 | } 151 | return Aspect_TOR_UNKNOWN; 152 | } 153 | 154 | // ================================================================ 155 | // Function : CursorPosition 156 | // Purpose : 157 | // ================================================================ 158 | Graphic3d_Vec2i GlfwOcctWindow::CursorPosition() const 159 | { 160 | Graphic3d_Vec2d aPos; 161 | glfwGetCursorPos(myGlfwWindow, &aPos.x(), &aPos.y()); 162 | return Graphic3d_Vec2i((int)aPos.x(), (int)aPos.y()); 163 | } 164 | -------------------------------------------------------------------------------- /GlfwOcctWindow.h: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // 3 | // Copyright(c) 2023 Shing Liu 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files(the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions : 11 | // 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #ifndef _GlfwOcctWindow_Header 24 | #define _GlfwOcctWindow_Header 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | struct GLFWwindow; 33 | 34 | //! GLFWwindow wrapper implementing Aspect_Window interface. 35 | class GlfwOcctWindow : public Aspect_Window 36 | { 37 | DEFINE_STANDARD_RTTI_INLINE(GlfwOcctWindow, Aspect_Window) 38 | public: 39 | //! Main constructor. 40 | GlfwOcctWindow(int theWidth, int theHeight, const TCollection_AsciiString& theTitle); 41 | 42 | //! Close the window. 43 | virtual ~GlfwOcctWindow() { Close(); } 44 | 45 | //! Close the window. 46 | void Close(); 47 | 48 | //! Return X Display connection. 49 | const Handle(Aspect_DisplayConnection)& GetDisplay() const { return myDisplay; } 50 | 51 | //! Return GLFW window. 52 | GLFWwindow* getGlfwWindow() { return myGlfwWindow; } 53 | 54 | //! Return native OpenGL context. 55 | Aspect_RenderingContext NativeGlContext() const; 56 | 57 | //! Return cursor position. 58 | Graphic3d_Vec2i CursorPosition() const; 59 | 60 | public: 61 | 62 | //! Returns native Window handle 63 | virtual Aspect_Drawable NativeHandle() const Standard_OVERRIDE; 64 | 65 | //! Returns parent of native Window handle. 66 | virtual Aspect_Drawable NativeParentHandle() const Standard_OVERRIDE { return 0; } 67 | 68 | //! Applies the resizing to the window 69 | virtual Aspect_TypeOfResize DoResize() Standard_OVERRIDE; 70 | 71 | //! Returns True if the window is opened and False if the window is closed. 72 | virtual Standard_Boolean IsMapped() const Standard_OVERRIDE; 73 | 74 | //! Apply the mapping change to the window and returns TRUE if the window is mapped at screen. 75 | virtual Standard_Boolean DoMapping() const Standard_OVERRIDE { return Standard_True; } 76 | 77 | //! Opens the window . 78 | virtual void Map() const Standard_OVERRIDE; 79 | 80 | //! Closes the window . 81 | virtual void Unmap() const Standard_OVERRIDE; 82 | 83 | virtual void Position(Standard_Integer& theX1, Standard_Integer& theY1, 84 | Standard_Integer& theX2, Standard_Integer& theY2) const Standard_OVERRIDE 85 | { 86 | theX1 = myXLeft; 87 | theX2 = myXRight; 88 | theY1 = myYTop; 89 | theY2 = myYBottom; 90 | } 91 | 92 | //! Returns The Window RATIO equal to the physical WIDTH/HEIGHT dimensions. 93 | virtual Standard_Real Ratio() const Standard_OVERRIDE 94 | { 95 | return Standard_Real(myXRight - myXLeft) / Standard_Real(myYBottom - myYTop); 96 | } 97 | 98 | //! Return window size. 99 | virtual void Size(Standard_Integer& theWidth, Standard_Integer& theHeight) const Standard_OVERRIDE 100 | { 101 | theWidth = myXRight - myXLeft; 102 | theHeight = myYBottom - myYTop; 103 | } 104 | 105 | virtual Aspect_FBConfig NativeFBConfig() const Standard_OVERRIDE { return NULL; } 106 | 107 | protected: 108 | Handle(Aspect_DisplayConnection) myDisplay; 109 | GLFWwindow* myGlfwWindow; 110 | Standard_Integer myXLeft; 111 | Standard_Integer myYTop; 112 | Standard_Integer myXRight; 113 | Standard_Integer myYBottom; 114 | }; 115 | 116 | #endif // _GlfwOcctWindow_Header 117 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Shing Liu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OcctImgui 2 | OpenCASCADE + GLFW + IMGUI Sample. 3 | 4 | ![occt imgui](occt-imgui.png "opencascade imgui") 5 | 6 | https://tracker.dev.opencascade.org/view.php?id=33485 7 | 8 | ## OpenCASCADE 9 | https://dev.opencascade.org/ 10 | 11 | https://github.com/Open-Cascade-SAS/OCCT 12 | 13 | Open CASCADE Technology (OCCT) a software 14 | development platform providing services for 3D surface and solid modeling, CAD 15 | data exchange, and visualization. Most of OCCT functionality is available in 16 | the form of C++ libraries. OCCT can be best applied in development of software 17 | dealing with 3D modeling (CAD), manufacturing / measuring (CAM) or numerical 18 | simulation (CAE). 19 | 20 | ## IMGUI 21 | https://github.com/ocornut/imgui 22 | 23 | Dear ImGui is a bloat-free graphical user interface library for C++. It outputs optimized vertex buffers that you can render anytime in your 3D-pipeline-enabled application. It is fast, portable, renderer agnostic, and self-contained (no external dependencies). 24 | 25 | Dear ImGui is designed to enable fast iterations and to empower programmers to create content creation tools and visualization / debug tools (as opposed to UI for the average end-user). It favors simplicity and productivity toward this goal and lacks certain features commonly found in more high-level libraries. 26 | 27 | Dear ImGui is particularly suited to integration in game engines (for tooling), real-time 3D applications, fullscreen applications, embedded applications, or any applications on console platforms where operating system features are non-standard. 28 | 29 | ## GLFW 30 | https://github.com/glfw/glfw 31 | 32 | GLFW is an Open Source, multi-platform library for OpenGL, OpenGL ES and Vulkan application development. It provides a simple, platform-independent API for creating windows, contexts and surfaces, reading input, handling events, etc. 33 | 34 | GLFW natively supports Windows, macOS and Linux and other Unix-like systems. On Linux both X11 and Wayland are supported. 35 | 36 | GLFW is licensed under the zlib/libpng license. 37 | 38 | ## Build 39 | Use Premake5 to build OcctImgui or with CMake: 40 | 41 | ``` 42 | cmake -DCMAKE_CXX_STANDARD=17 .. 43 | ``` 44 | 45 | 46 | -------------------------------------------------------------------------------- /imgui/imconfig.h: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // DEAR IMGUI COMPILE-TIME OPTIONS 3 | // Runtime options (clipboard callbacks, enabling various features, etc.) can generally be set via the ImGuiIO structure. 4 | // You can use ImGui::SetAllocatorFunctions() before calling ImGui::CreateContext() to rewire memory allocation functions. 5 | //----------------------------------------------------------------------------- 6 | // A) You may edit imconfig.h (and not overwrite it when updating Dear ImGui, or maintain a patch/rebased branch with your modifications to it) 7 | // B) or '#define IMGUI_USER_CONFIG "my_imgui_config.h"' in your project and then add directives in your own file without touching this template. 8 | //----------------------------------------------------------------------------- 9 | // You need to make sure that configuration settings are defined consistently _everywhere_ Dear ImGui is used, which include the imgui*.cpp 10 | // files but also _any_ of your code that uses Dear ImGui. This is because some compile-time options have an affect on data structures. 11 | // Defining those options in imconfig.h will ensure every compilation unit gets to see the same data structure layouts. 12 | // Call IMGUI_CHECKVERSION() from your .cpp file to verify that the data structures your files are using are matching the ones imgui.cpp is using. 13 | //----------------------------------------------------------------------------- 14 | 15 | #pragma once 16 | 17 | //---- Define assertion handler. Defaults to calling assert(). 18 | // If your macro uses multiple statements, make sure is enclosed in a 'do { .. } while (0)' block so it can be used as a single statement. 19 | //#define IM_ASSERT(_EXPR) MyAssert(_EXPR) 20 | //#define IM_ASSERT(_EXPR) ((void)(_EXPR)) // Disable asserts 21 | 22 | //---- Define attributes of all API symbols declarations, e.g. for DLL under Windows 23 | // Using Dear ImGui via a shared library is not recommended, because of function call overhead and because we don't guarantee backward nor forward ABI compatibility. 24 | // DLL users: heaps and globals are not shared across DLL boundaries! You will need to call SetCurrentContext() + SetAllocatorFunctions() 25 | // for each static/DLL boundary you are calling from. Read "Context and Memory Allocators" section of imgui.cpp for more details. 26 | //#define IMGUI_API __declspec( dllexport ) 27 | //#define IMGUI_API __declspec( dllimport ) 28 | 29 | //---- Don't define obsolete functions/enums/behaviors. Consider enabling from time to time after updating to clean your code of obsolete function/names. 30 | //#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS 31 | //#define IMGUI_DISABLE_OBSOLETE_KEYIO // 1.87: disable legacy io.KeyMap[]+io.KeysDown[] in favor io.AddKeyEvent(). This will be folded into IMGUI_DISABLE_OBSOLETE_FUNCTIONS in a few versions. 32 | 33 | //---- Disable all of Dear ImGui or don't implement standard windows/tools. 34 | // It is very strongly recommended to NOT disable the demo windows and debug tool during development. They are extremely useful in day to day work. Please read comments in imgui_demo.cpp. 35 | //#define IMGUI_DISABLE // Disable everything: all headers and source files will be empty. 36 | //#define IMGUI_DISABLE_DEMO_WINDOWS // Disable demo windows: ShowDemoWindow()/ShowStyleEditor() will be empty. 37 | //#define IMGUI_DISABLE_DEBUG_TOOLS // Disable metrics/debugger and other debug tools: ShowMetricsWindow(), ShowDebugLogWindow() and ShowStackToolWindow() will be empty (this was called IMGUI_DISABLE_METRICS_WINDOW before 1.88). 38 | 39 | //---- Don't implement some functions to reduce linkage requirements. 40 | //#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS // [Win32] Don't implement default clipboard handler. Won't use and link with OpenClipboard/GetClipboardData/CloseClipboard etc. (user32.lib/.a, kernel32.lib/.a) 41 | //#define IMGUI_ENABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] [Default with Visual Studio] Implement default IME handler (require imm32.lib/.a, auto-link for Visual Studio, -limm32 on command-line for MinGW) 42 | //#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] [Default with non-Visual Studio compilers] Don't implement default IME handler (won't require imm32.lib/.a) 43 | //#define IMGUI_DISABLE_WIN32_FUNCTIONS // [Win32] Won't use and link with any Win32 function (clipboard, IME). 44 | //#define IMGUI_ENABLE_OSX_DEFAULT_CLIPBOARD_FUNCTIONS // [OSX] Implement default OSX clipboard handler (need to link with '-framework ApplicationServices', this is why this is not the default). 45 | //#define IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS // Don't implement ImFormatString/ImFormatStringV so you can implement them yourself (e.g. if you don't want to link with vsnprintf) 46 | //#define IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS // Don't implement ImFabs/ImSqrt/ImPow/ImFmod/ImCos/ImSin/ImAcos/ImAtan2 so you can implement them yourself. 47 | //#define IMGUI_DISABLE_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite and ImFileHandle at all (replace them with dummies) 48 | //#define IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite and ImFileHandle so you can implement them yourself if you don't want to link with fopen/fclose/fread/fwrite. This will also disable the LogToTTY() function. 49 | //#define IMGUI_DISABLE_DEFAULT_ALLOCATORS // Don't implement default allocators calling malloc()/free() to avoid linking with them. You will need to call ImGui::SetAllocatorFunctions(). 50 | //#define IMGUI_DISABLE_SSE // Disable use of SSE intrinsics even if available 51 | 52 | //---- Include imgui_user.h at the end of imgui.h as a convenience 53 | //#define IMGUI_INCLUDE_IMGUI_USER_H 54 | 55 | //---- Pack colors to BGRA8 instead of RGBA8 (to avoid converting from one to another) 56 | //#define IMGUI_USE_BGRA_PACKED_COLOR 57 | 58 | //---- Use 32-bit for ImWchar (default is 16-bit) to support unicode planes 1-16. (e.g. point beyond 0xFFFF like emoticons, dingbats, symbols, shapes, ancient languages, etc...) 59 | //#define IMGUI_USE_WCHAR32 60 | 61 | //---- Avoid multiple STB libraries implementations, or redefine path/filenames to prioritize another version 62 | // By default the embedded implementations are declared static and not available outside of Dear ImGui sources files. 63 | //#define IMGUI_STB_TRUETYPE_FILENAME "my_folder/stb_truetype.h" 64 | //#define IMGUI_STB_RECT_PACK_FILENAME "my_folder/stb_rect_pack.h" 65 | //#define IMGUI_STB_SPRINTF_FILENAME "my_folder/stb_sprintf.h" // only used if IMGUI_USE_STB_SPRINTF is defined. 66 | //#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION 67 | //#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION 68 | //#define IMGUI_DISABLE_STB_SPRINTF_IMPLEMENTATION // only disabled if IMGUI_USE_STB_SPRINTF is defined. 69 | 70 | //---- Use stb_sprintf.h for a faster implementation of vsnprintf instead of the one from libc (unless IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS is defined) 71 | // Compatibility checks of arguments and formats done by clang and GCC will be disabled in order to support the extra formats provided by stb_sprintf.h. 72 | //#define IMGUI_USE_STB_SPRINTF 73 | 74 | //---- Use FreeType to build and rasterize the font atlas (instead of stb_truetype which is embedded by default in Dear ImGui) 75 | // Requires FreeType headers to be available in the include path. Requires program to be compiled with 'misc/freetype/imgui_freetype.cpp' (in this repository) + the FreeType library (not provided). 76 | // On Windows you may use vcpkg with 'vcpkg install freetype --triplet=x64-windows' + 'vcpkg integrate install'. 77 | //#define IMGUI_ENABLE_FREETYPE 78 | 79 | //---- Use FreeType+lunasvg library to render OpenType SVG fonts (SVGinOT) 80 | // Requires lunasvg headers to be available in the include path + program to be linked with the lunasvg library (not provided). 81 | // Only works in combination with IMGUI_ENABLE_FREETYPE. 82 | // (implementation is based on Freetype's rsvg-port.c which is licensed under CeCILL-C Free Software License Agreement) 83 | //#define IMGUI_ENABLE_FREETYPE_LUNASVG 84 | 85 | //---- Use stb_truetype to build and rasterize the font atlas (default) 86 | // The only purpose of this define is if you want force compilation of the stb_truetype backend ALONG with the FreeType backend. 87 | //#define IMGUI_ENABLE_STB_TRUETYPE 88 | 89 | //---- Define constructor and implicit cast operators to convert back<>forth between your math types and ImVec2/ImVec4. 90 | // This will be inlined as part of ImVec2 and ImVec4 class declarations. 91 | /* 92 | #define IM_VEC2_CLASS_EXTRA \ 93 | constexpr ImVec2(const MyVec2& f) : x(f.x), y(f.y) {} \ 94 | operator MyVec2() const { return MyVec2(x,y); } 95 | 96 | #define IM_VEC4_CLASS_EXTRA \ 97 | constexpr ImVec4(const MyVec4& f) : x(f.x), y(f.y), z(f.z), w(f.w) {} \ 98 | operator MyVec4() const { return MyVec4(x,y,z,w); } 99 | */ 100 | //---- ...Or use Dear ImGui's own very basic math operators. 101 | //#define IMGUI_DEFINE_MATH_OPERATORS 102 | 103 | //---- Use 32-bit vertex indices (default is 16-bit) is one way to allow large meshes with more than 64K vertices. 104 | // Your renderer backend will need to support it (most example renderer backends support both 16/32-bit indices). 105 | // Another way to allow large meshes while keeping 16-bit indices is to handle ImDrawCmd::VtxOffset in your renderer. 106 | // Read about ImGuiBackendFlags_RendererHasVtxOffset for details. 107 | //#define ImDrawIdx unsigned int 108 | 109 | //---- Override ImDrawCallback signature (will need to modify renderer backends accordingly) 110 | //struct ImDrawList; 111 | //struct ImDrawCmd; 112 | //typedef void (*MyImDrawCallback)(const ImDrawList* draw_list, const ImDrawCmd* cmd, void* my_renderer_user_data); 113 | //#define ImDrawCallback MyImDrawCallback 114 | 115 | //---- Debug Tools: Macro to break in Debugger (we provide a default implementation of this in the codebase) 116 | // (use 'Metrics->Tools->Item Picker' to pick widgets with the mouse and break into them for easy debugging.) 117 | //#define IM_DEBUG_BREAK IM_ASSERT(0) 118 | //#define IM_DEBUG_BREAK __debugbreak() 119 | 120 | //---- Debug Tools: Enable slower asserts 121 | //#define IMGUI_DEBUG_PARANOID 122 | 123 | //---- Tip: You can add extra functions within the ImGui:: namespace from anywhere (e.g. your own sources/header files) 124 | /* 125 | namespace ImGui 126 | { 127 | void MyFunction(const char* name, MyMatrix44* mtx); 128 | } 129 | */ 130 | -------------------------------------------------------------------------------- /imgui/imgui_impl_glfw.h: -------------------------------------------------------------------------------- 1 | // dear imgui: Platform Backend for GLFW 2 | // This needs to be used along with a Renderer (e.g. OpenGL3, Vulkan, WebGPU..) 3 | // (Info: GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.) 4 | // (Requires: GLFW 3.1+. Prefer GLFW 3.3+ for full feature support.) 5 | 6 | // Implemented features: 7 | // [X] Platform: Clipboard support. 8 | // [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen/Pen (Windows only). 9 | // [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy GLFW_KEY_* values will also be supported unless IMGUI_DISABLE_OBSOLETE_KEYIO is set] 10 | // [X] Platform: Gamepad support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. 11 | // [x] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange' (note: the resizing cursors requires GLFW 3.4+). 12 | // [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'. 13 | 14 | // Issues: 15 | // [ ] Platform: Multi-viewport support: ParentViewportID not honored, and so io.ConfigViewportsNoDefaultParent has no effect (minor). 16 | 17 | // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. 18 | // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. 19 | // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. 20 | // Read online: https://github.com/ocornut/imgui/tree/master/docs 21 | 22 | #pragma once 23 | #include "imgui.h" // IMGUI_IMPL_API 24 | #ifndef IMGUI_DISABLE 25 | 26 | struct GLFWwindow; 27 | struct GLFWmonitor; 28 | 29 | IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForOpenGL(GLFWwindow* window, bool install_callbacks); 30 | IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForVulkan(GLFWwindow* window, bool install_callbacks); 31 | IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForOther(GLFWwindow* window, bool install_callbacks); 32 | IMGUI_IMPL_API void ImGui_ImplGlfw_Shutdown(); 33 | IMGUI_IMPL_API void ImGui_ImplGlfw_NewFrame(); 34 | 35 | // GLFW callbacks install 36 | // - When calling Init with 'install_callbacks=true': ImGui_ImplGlfw_InstallCallbacks() is called. GLFW callbacks will be installed for you. They will chain-call user's previously installed callbacks, if any. 37 | // - When calling Init with 'install_callbacks=false': GLFW callbacks won't be installed. You will need to call individual function yourself from your own GLFW callbacks. 38 | IMGUI_IMPL_API void ImGui_ImplGlfw_InstallCallbacks(GLFWwindow* window); 39 | IMGUI_IMPL_API void ImGui_ImplGlfw_RestoreCallbacks(GLFWwindow* window); 40 | 41 | // GFLW callbacks options: 42 | // - Set 'chain_for_all_windows=true' to enable chaining callbacks for all windows (including secondary viewports created by backends or by user) 43 | IMGUI_IMPL_API void ImGui_ImplGlfw_SetCallbacksChainForAllWindows(bool chain_for_all_windows); 44 | 45 | // GLFW callbacks (individual callbacks to call yourself if you didn't install callbacks) 46 | IMGUI_IMPL_API void ImGui_ImplGlfw_WindowFocusCallback(GLFWwindow* window, int focused); // Since 1.84 47 | IMGUI_IMPL_API void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered); // Since 1.84 48 | IMGUI_IMPL_API void ImGui_ImplGlfw_CursorPosCallback(GLFWwindow* window, double x, double y); // Since 1.87 49 | IMGUI_IMPL_API void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods); 50 | IMGUI_IMPL_API void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset); 51 | IMGUI_IMPL_API void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); 52 | IMGUI_IMPL_API void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c); 53 | IMGUI_IMPL_API void ImGui_ImplGlfw_MonitorCallback(GLFWmonitor* monitor, int event); 54 | 55 | #endif // #ifndef IMGUI_DISABLE 56 | -------------------------------------------------------------------------------- /imgui/imgui_impl_opengl3.cpp: -------------------------------------------------------------------------------- 1 | // dear imgui: Renderer Backend for modern OpenGL with shaders / programmatic pipeline 2 | // - Desktop GL: 2.x 3.x 4.x 3 | // - Embedded GL: ES 2.0 (WebGL 1.0), ES 3.0 (WebGL 2.0) 4 | // This needs to be used along with a Platform Backend (e.g. GLFW, SDL, Win32, custom..) 5 | 6 | // Implemented features: 7 | // [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID! 8 | // [X] Renderer: Large meshes support (64k+ vertices) with 16-bit indices (Desktop OpenGL only). 9 | // [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'. 10 | 11 | // About WebGL/ES: 12 | // - You need to '#define IMGUI_IMPL_OPENGL_ES2' or '#define IMGUI_IMPL_OPENGL_ES3' to use WebGL or OpenGL ES. 13 | // - This is done automatically on iOS, Android and Emscripten targets. 14 | // - For other targets, the define needs to be visible from the imgui_impl_opengl3.cpp compilation unit. If unsure, define globally or in imconfig.h. 15 | 16 | // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. 17 | // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. 18 | // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. 19 | // Read online: https://github.com/ocornut/imgui/tree/master/docs 20 | 21 | // CHANGELOG 22 | // (minor and older changes stripped away, please see git history for details) 23 | // 2023-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface. 24 | // 2023-06-20: OpenGL: Fixed erroneous use glGetIntegerv(GL_CONTEXT_PROFILE_MASK) on contexts lower than 3.2. (#6539, #6333) 25 | // 2023-05-09: OpenGL: Support for glBindSampler() backup/restore on ES3. (#6375) 26 | // 2023-04-18: OpenGL: Restore front and back polygon mode separately when supported by context. (#6333) 27 | // 2023-03-23: OpenGL: Properly restoring "no shader program bound" if it was the case prior to running the rendering function. (#6267, #6220, #6224) 28 | // 2023-03-15: OpenGL: Fixed GL loader crash when GL_VERSION returns NULL. (#6154, #4445, #3530) 29 | // 2023-03-06: OpenGL: Fixed restoration of a potentially deleted OpenGL program, by calling glIsProgram(). (#6220, #6224) 30 | // 2022-11-09: OpenGL: Reverted use of glBufferSubData(), too many corruptions issues + old issues seemingly can't be reproed with Intel drivers nowadays (revert 2021-12-15 and 2022-05-23 changes). 31 | // 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11. 32 | // 2022-09-27: OpenGL: Added ability to '#define IMGUI_IMPL_OPENGL_DEBUG'. 33 | // 2022-05-23: OpenGL: Reworking 2021-12-15 "Using buffer orphaning" so it only happens on Intel GPU, seems to cause problems otherwise. (#4468, #4825, #4832, #5127). 34 | // 2022-05-13: OpenGL: Fixed state corruption on OpenGL ES 2.0 due to not preserving GL_ELEMENT_ARRAY_BUFFER_BINDING and vertex attribute states. 35 | // 2021-12-15: OpenGL: Using buffer orphaning + glBufferSubData(), seems to fix leaks with multi-viewports with some Intel HD drivers. 36 | // 2021-08-23: OpenGL: Fixed ES 3.0 shader ("#version 300 es") use normal precision floats to avoid wobbly rendering at HD resolutions. 37 | // 2021-08-19: OpenGL: Embed and use our own minimal GL loader (imgui_impl_opengl3_loader.h), removing requirement and support for third-party loader. 38 | // 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX). 39 | // 2021-06-25: OpenGL: Use OES_vertex_array extension on Emscripten + backup/restore current state. 40 | // 2021-06-21: OpenGL: Destroy individual vertex/fragment shader objects right after they are linked into the main shader. 41 | // 2021-05-24: OpenGL: Access GL_CLIP_ORIGIN when "GL_ARB_clip_control" extension is detected, inside of just OpenGL 4.5 version. 42 | // 2021-05-19: OpenGL: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement) 43 | // 2021-04-06: OpenGL: Don't try to read GL_CLIP_ORIGIN unless we're OpenGL 4.5 or greater. 44 | // 2021-02-18: OpenGL: Change blending equation to preserve alpha in output buffer. 45 | // 2021-01-03: OpenGL: Backup, setup and restore GL_STENCIL_TEST state. 46 | // 2020-10-23: OpenGL: Backup, setup and restore GL_PRIMITIVE_RESTART state. 47 | // 2020-10-15: OpenGL: Use glGetString(GL_VERSION) instead of glGetIntegerv(GL_MAJOR_VERSION, ...) when the later returns zero (e.g. Desktop GL 2.x) 48 | // 2020-09-17: OpenGL: Fix to avoid compiling/calling glBindSampler() on ES or pre 3.3 context which have the defines set by a loader. 49 | // 2020-07-10: OpenGL: Added support for glad2 OpenGL loader. 50 | // 2020-05-08: OpenGL: Made default GLSL version 150 (instead of 130) on OSX. 51 | // 2020-04-21: OpenGL: Fixed handling of glClipControl(GL_UPPER_LEFT) by inverting projection matrix. 52 | // 2020-04-12: OpenGL: Fixed context version check mistakenly testing for 4.0+ instead of 3.2+ to enable ImGuiBackendFlags_RendererHasVtxOffset. 53 | // 2020-03-24: OpenGL: Added support for glbinding 2.x OpenGL loader. 54 | // 2020-01-07: OpenGL: Added support for glbinding 3.x OpenGL loader. 55 | // 2019-10-25: OpenGL: Using a combination of GL define and runtime GL version to decide whether to use glDrawElementsBaseVertex(). Fix building with pre-3.2 GL loaders. 56 | // 2019-09-22: OpenGL: Detect default GL loader using __has_include compiler facility. 57 | // 2019-09-16: OpenGL: Tweak initialization code to allow application calling ImGui_ImplOpenGL3_CreateFontsTexture() before the first NewFrame() call. 58 | // 2019-05-29: OpenGL: Desktop GL only: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag. 59 | // 2019-04-30: OpenGL: Added support for special ImDrawCallback_ResetRenderState callback to reset render state. 60 | // 2019-03-29: OpenGL: Not calling glBindBuffer more than necessary in the render loop. 61 | // 2019-03-15: OpenGL: Added a GL call + comments in ImGui_ImplOpenGL3_Init() to detect uninitialized GL function loaders early. 62 | // 2019-03-03: OpenGL: Fix support for ES 2.0 (WebGL 1.0). 63 | // 2019-02-20: OpenGL: Fix for OSX not supporting OpenGL 4.5, we don't try to read GL_CLIP_ORIGIN even if defined by the headers/loader. 64 | // 2019-02-11: OpenGL: Projecting clipping rectangles correctly using draw_data->FramebufferScale to allow multi-viewports for retina display. 65 | // 2019-02-01: OpenGL: Using GLSL 410 shaders for any version over 410 (e.g. 430, 450). 66 | // 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window. 67 | // 2018-11-13: OpenGL: Support for GL 4.5's glClipControl(GL_UPPER_LEFT) / GL_CLIP_ORIGIN. 68 | // 2018-08-29: OpenGL: Added support for more OpenGL loaders: glew and glad, with comments indicative that any loader can be used. 69 | // 2018-08-09: OpenGL: Default to OpenGL ES 3 on iOS and Android. GLSL version default to "#version 300 ES". 70 | // 2018-07-30: OpenGL: Support for GLSL 300 ES and 410 core. Fixes for Emscripten compilation. 71 | // 2018-07-10: OpenGL: Support for more GLSL versions (based on the GLSL version string). Added error output when shaders fail to compile/link. 72 | // 2018-06-08: Misc: Extracted imgui_impl_opengl3.cpp/.h away from the old combined GLFW/SDL+OpenGL3 examples. 73 | // 2018-06-08: OpenGL: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle. 74 | // 2018-05-25: OpenGL: Removed unnecessary backup/restore of GL_ELEMENT_ARRAY_BUFFER_BINDING since this is part of the VAO state. 75 | // 2018-05-14: OpenGL: Making the call to glBindSampler() optional so 3.2 context won't fail if the function is a nullptr pointer. 76 | // 2018-03-06: OpenGL: Added const char* glsl_version parameter to ImGui_ImplOpenGL3_Init() so user can override the GLSL version e.g. "#version 150". 77 | // 2018-02-23: OpenGL: Create the VAO in the render function so the setup can more easily be used with multiple shared GL context. 78 | // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplSdlGL3_RenderDrawData() in the .h file so you can call it yourself. 79 | // 2018-01-07: OpenGL: Changed GLSL shader version from 330 to 150. 80 | // 2017-09-01: OpenGL: Save and restore current bound sampler. Save and restore current polygon mode. 81 | // 2017-05-01: OpenGL: Fixed save and restore of current blend func state. 82 | // 2017-05-01: OpenGL: Fixed save and restore of current GL_ACTIVE_TEXTURE. 83 | // 2016-09-05: OpenGL: Fixed save and restore of current scissor rectangle. 84 | // 2016-07-29: OpenGL: Explicitly setting GL_UNPACK_ROW_LENGTH to reduce issues because SDL changes it. (#752) 85 | 86 | //---------------------------------------- 87 | // OpenGL GLSL GLSL 88 | // version version string 89 | //---------------------------------------- 90 | // 2.0 110 "#version 110" 91 | // 2.1 120 "#version 120" 92 | // 3.0 130 "#version 130" 93 | // 3.1 140 "#version 140" 94 | // 3.2 150 "#version 150" 95 | // 3.3 330 "#version 330 core" 96 | // 4.0 400 "#version 400 core" 97 | // 4.1 410 "#version 410 core" 98 | // 4.2 420 "#version 410 core" 99 | // 4.3 430 "#version 430 core" 100 | // ES 2.0 100 "#version 100" = WebGL 1.0 101 | // ES 3.0 300 "#version 300 es" = WebGL 2.0 102 | //---------------------------------------- 103 | 104 | #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) 105 | #define _CRT_SECURE_NO_WARNINGS 106 | #endif 107 | 108 | #include "imgui.h" 109 | #ifndef IMGUI_DISABLE 110 | #include "imgui_impl_opengl3.h" 111 | #include 112 | #include // intptr_t 113 | #if defined(__APPLE__) 114 | #include 115 | #endif 116 | 117 | // Clang/GCC warnings with -Weverything 118 | #if defined(__clang__) 119 | #pragma clang diagnostic push 120 | #pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast 121 | #pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness 122 | #pragma clang diagnostic ignored "-Wunused-macros" // warning: macro is not used 123 | #pragma clang diagnostic ignored "-Wnonportable-system-include-path" 124 | #pragma clang diagnostic ignored "-Wcast-function-type" // warning: cast between incompatible function types (for loader) 125 | #endif 126 | #if defined(__GNUC__) 127 | #pragma GCC diagnostic push 128 | #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind 129 | #pragma GCC diagnostic ignored "-Wunknown-warning-option" // warning: unknown warning group 'xxx' 130 | #pragma GCC diagnostic ignored "-Wcast-function-type" // warning: cast between incompatible function types (for loader) 131 | #endif 132 | 133 | // GL includes 134 | #if defined(IMGUI_IMPL_OPENGL_ES2) 135 | #if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) 136 | #include // Use GL ES 2 137 | #else 138 | #include // Use GL ES 2 139 | #endif 140 | #if defined(__EMSCRIPTEN__) 141 | #ifndef GL_GLEXT_PROTOTYPES 142 | #define GL_GLEXT_PROTOTYPES 143 | #endif 144 | #include 145 | #endif 146 | #elif defined(IMGUI_IMPL_OPENGL_ES3) 147 | #if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) 148 | #include // Use GL ES 3 149 | #else 150 | #include // Use GL ES 3 151 | #endif 152 | #elif !defined(IMGUI_IMPL_OPENGL_LOADER_CUSTOM) 153 | // Modern desktop OpenGL doesn't have a standard portable header file to load OpenGL function pointers. 154 | // Helper libraries are often used for this purpose! Here we are using our own minimal custom loader based on gl3w. 155 | // In the rest of your app/engine, you can use another loader of your choice (gl3w, glew, glad, glbinding, glext, glLoadGen, etc.). 156 | // If you happen to be developing a new feature for this backend (imgui_impl_opengl3.cpp): 157 | // - You may need to regenerate imgui_impl_opengl3_loader.h to add new symbols. See https://github.com/dearimgui/gl3w_stripped 158 | // - You can temporarily use an unstripped version. See https://github.com/dearimgui/gl3w_stripped/releases 159 | // Changes to this backend using new APIs should be accompanied by a regenerated stripped loader version. 160 | #define IMGL3W_IMPL 161 | #include "imgui_impl_opengl3_loader.h" 162 | #endif 163 | 164 | // Vertex arrays are not supported on ES2/WebGL1 unless Emscripten which uses an extension 165 | #ifndef IMGUI_IMPL_OPENGL_ES2 166 | #define IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY 167 | #elif defined(__EMSCRIPTEN__) 168 | #define IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY 169 | #define glBindVertexArray glBindVertexArrayOES 170 | #define glGenVertexArrays glGenVertexArraysOES 171 | #define glDeleteVertexArrays glDeleteVertexArraysOES 172 | #define GL_VERTEX_ARRAY_BINDING GL_VERTEX_ARRAY_BINDING_OES 173 | #endif 174 | 175 | // Desktop GL 2.0+ has glPolygonMode() which GL ES and WebGL don't have. 176 | #ifdef GL_POLYGON_MODE 177 | #define IMGUI_IMPL_HAS_POLYGON_MODE 178 | #endif 179 | 180 | // Desktop GL 3.2+ has glDrawElementsBaseVertex() which GL ES and WebGL don't have. 181 | #if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) && defined(GL_VERSION_3_2) 182 | #define IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET 183 | #endif 184 | 185 | // Desktop GL 3.3+ and GL ES 3.0+ have glBindSampler() 186 | #if !defined(IMGUI_IMPL_OPENGL_ES2) && (defined(IMGUI_IMPL_OPENGL_ES3) || defined(GL_VERSION_3_3)) 187 | #define IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER 188 | #endif 189 | 190 | // Desktop GL 3.1+ has GL_PRIMITIVE_RESTART state 191 | #if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) && defined(GL_VERSION_3_1) 192 | #define IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART 193 | #endif 194 | 195 | // Desktop GL use extension detection 196 | #if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) 197 | #define IMGUI_IMPL_OPENGL_MAY_HAVE_EXTENSIONS 198 | #endif 199 | 200 | // [Debugging] 201 | //#define IMGUI_IMPL_OPENGL_DEBUG 202 | #ifdef IMGUI_IMPL_OPENGL_DEBUG 203 | #include 204 | #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) // Call with error check 205 | #else 206 | #define GL_CALL(_CALL) _CALL // Call without error check 207 | #endif 208 | 209 | // OpenGL Data 210 | struct ImGui_ImplOpenGL3_Data 211 | { 212 | GLuint GlVersion; // Extracted at runtime using GL_MAJOR_VERSION, GL_MINOR_VERSION queries (e.g. 320 for GL 3.2) 213 | char GlslVersionString[32]; // Specified by user or detected based on compile time GL settings. 214 | bool GlProfileIsES2; 215 | bool GlProfileIsES3; 216 | bool GlProfileIsCompat; 217 | GLint GlProfileMask; 218 | GLuint FontTexture; 219 | GLuint ShaderHandle; 220 | GLint AttribLocationTex; // Uniforms location 221 | GLint AttribLocationProjMtx; 222 | GLuint AttribLocationVtxPos; // Vertex attributes location 223 | GLuint AttribLocationVtxUV; 224 | GLuint AttribLocationVtxColor; 225 | unsigned int VboHandle, ElementsHandle; 226 | GLsizeiptr VertexBufferSize; 227 | GLsizeiptr IndexBufferSize; 228 | bool HasClipOrigin; 229 | bool UseBufferSubData; 230 | 231 | ImGui_ImplOpenGL3_Data() { memset((void*)this, 0, sizeof(*this)); } 232 | }; 233 | 234 | // Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts 235 | // It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts. 236 | static ImGui_ImplOpenGL3_Data* ImGui_ImplOpenGL3_GetBackendData() 237 | { 238 | return ImGui::GetCurrentContext() ? (ImGui_ImplOpenGL3_Data*)ImGui::GetIO().BackendRendererUserData : nullptr; 239 | } 240 | 241 | // Forward Declarations 242 | static void ImGui_ImplOpenGL3_InitPlatformInterface(); 243 | static void ImGui_ImplOpenGL3_ShutdownPlatformInterface(); 244 | 245 | // OpenGL vertex attribute state (for ES 1.0 and ES 2.0 only) 246 | #ifndef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY 247 | struct ImGui_ImplOpenGL3_VtxAttribState 248 | { 249 | GLint Enabled, Size, Type, Normalized, Stride; 250 | GLvoid* Ptr; 251 | 252 | void GetState(GLint index) 253 | { 254 | glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &Enabled); 255 | glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_SIZE, &Size); 256 | glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_TYPE, &Type); 257 | glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &Normalized); 258 | glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &Stride); 259 | glGetVertexAttribPointerv(index, GL_VERTEX_ATTRIB_ARRAY_POINTER, &Ptr); 260 | } 261 | void SetState(GLint index) 262 | { 263 | glVertexAttribPointer(index, Size, Type, (GLboolean)Normalized, Stride, Ptr); 264 | if (Enabled) glEnableVertexAttribArray(index); else glDisableVertexAttribArray(index); 265 | } 266 | }; 267 | #endif 268 | 269 | // Functions 270 | bool ImGui_ImplOpenGL3_Init(const char* glsl_version) 271 | { 272 | ImGuiIO& io = ImGui::GetIO(); 273 | IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!"); 274 | 275 | // Initialize our loader 276 | #if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) && !defined(IMGUI_IMPL_OPENGL_LOADER_CUSTOM) 277 | if (imgl3wInit() != 0) 278 | { 279 | fprintf(stderr, "Failed to initialize OpenGL loader!\n"); 280 | return false; 281 | } 282 | #endif 283 | 284 | // Setup backend capabilities flags 285 | ImGui_ImplOpenGL3_Data* bd = IM_NEW(ImGui_ImplOpenGL3_Data)(); 286 | io.BackendRendererUserData = (void*)bd; 287 | io.BackendRendererName = "imgui_impl_opengl3"; 288 | 289 | // Query for GL version (e.g. 320 for GL 3.2) 290 | #if defined(IMGUI_IMPL_OPENGL_ES2) 291 | // GLES 2 292 | bd->GlVersion = 200; 293 | bd->GlProfileIsES2 = true; 294 | #else 295 | // Desktop or GLES 3 296 | GLint major = 0; 297 | GLint minor = 0; 298 | glGetIntegerv(GL_MAJOR_VERSION, &major); 299 | glGetIntegerv(GL_MINOR_VERSION, &minor); 300 | if (major == 0 && minor == 0) 301 | { 302 | // Query GL_VERSION in desktop GL 2.x, the string will start with "." 303 | const char* gl_version = (const char*)glGetString(GL_VERSION); 304 | sscanf(gl_version, "%d.%d", &major, &minor); 305 | } 306 | bd->GlVersion = (GLuint)(major * 100 + minor * 10); 307 | #if defined(GL_CONTEXT_PROFILE_MASK) 308 | if (bd->GlVersion >= 320) 309 | glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &bd->GlProfileMask); 310 | bd->GlProfileIsCompat = (bd->GlProfileMask & GL_CONTEXT_COMPATIBILITY_PROFILE_BIT) != 0; 311 | #endif 312 | 313 | #if defined(IMGUI_IMPL_OPENGL_ES3) 314 | bd->GlProfileIsES3 = true; 315 | #endif 316 | 317 | bd->UseBufferSubData = false; 318 | /* 319 | // Query vendor to enable glBufferSubData kludge 320 | #ifdef _WIN32 321 | if (const char* vendor = (const char*)glGetString(GL_VENDOR)) 322 | if (strncmp(vendor, "Intel", 5) == 0) 323 | bd->UseBufferSubData = true; 324 | #endif 325 | */ 326 | #endif 327 | 328 | #ifdef IMGUI_IMPL_OPENGL_DEBUG 329 | printf("GlVersion = %d\nGlProfileIsCompat = %d\nGlProfileMask = 0x%X\nGlProfileIsES2 = %d, GlProfileIsES3 = %d\nGL_VENDOR = '%s'\nGL_RENDERER = '%s'\n", bd->GlVersion, bd->GlProfileIsCompat, bd->GlProfileMask, bd->GlProfileIsES2, bd->GlProfileIsES3, (const char*)glGetString(GL_VENDOR), (const char*)glGetString(GL_RENDERER)); // [DEBUG] 330 | #endif 331 | 332 | #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET 333 | if (bd->GlVersion >= 320) 334 | io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. 335 | #endif 336 | io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional) 337 | 338 | // Store GLSL version string so we can refer to it later in case we recreate shaders. 339 | // Note: GLSL version is NOT the same as GL version. Leave this to nullptr if unsure. 340 | if (glsl_version == nullptr) 341 | { 342 | #if defined(IMGUI_IMPL_OPENGL_ES2) 343 | glsl_version = "#version 100"; 344 | #elif defined(IMGUI_IMPL_OPENGL_ES3) 345 | glsl_version = "#version 300 es"; 346 | #elif defined(__APPLE__) 347 | glsl_version = "#version 150"; 348 | #else 349 | glsl_version = "#version 130"; 350 | #endif 351 | } 352 | IM_ASSERT((int)strlen(glsl_version) + 2 < IM_ARRAYSIZE(bd->GlslVersionString)); 353 | strcpy(bd->GlslVersionString, glsl_version); 354 | strcat(bd->GlslVersionString, "\n"); 355 | 356 | // Make an arbitrary GL call (we don't actually need the result) 357 | // IF YOU GET A CRASH HERE: it probably means the OpenGL function loader didn't do its job. Let us know! 358 | GLint current_texture; 359 | glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_texture); 360 | 361 | // Detect extensions we support 362 | bd->HasClipOrigin = (bd->GlVersion >= 450); 363 | #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_EXTENSIONS 364 | GLint num_extensions = 0; 365 | glGetIntegerv(GL_NUM_EXTENSIONS, &num_extensions); 366 | for (GLint i = 0; i < num_extensions; i++) 367 | { 368 | const char* extension = (const char*)glGetStringi(GL_EXTENSIONS, i); 369 | if (extension != nullptr && strcmp(extension, "GL_ARB_clip_control") == 0) 370 | bd->HasClipOrigin = true; 371 | } 372 | #endif 373 | 374 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) 375 | ImGui_ImplOpenGL3_InitPlatformInterface(); 376 | 377 | return true; 378 | } 379 | 380 | void ImGui_ImplOpenGL3_Shutdown() 381 | { 382 | ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); 383 | IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?"); 384 | ImGuiIO& io = ImGui::GetIO(); 385 | 386 | ImGui_ImplOpenGL3_ShutdownPlatformInterface(); 387 | ImGui_ImplOpenGL3_DestroyDeviceObjects(); 388 | io.BackendRendererName = nullptr; 389 | io.BackendRendererUserData = nullptr; 390 | io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasViewports); 391 | IM_DELETE(bd); 392 | } 393 | 394 | void ImGui_ImplOpenGL3_NewFrame() 395 | { 396 | ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); 397 | IM_ASSERT(bd != nullptr && "Did you call ImGui_ImplOpenGL3_Init()?"); 398 | 399 | if (!bd->ShaderHandle) 400 | ImGui_ImplOpenGL3_CreateDeviceObjects(); 401 | } 402 | 403 | static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_width, int fb_height, GLuint vertex_array_object) 404 | { 405 | ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); 406 | 407 | // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, polygon fill 408 | glEnable(GL_BLEND); 409 | glBlendEquation(GL_FUNC_ADD); 410 | glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); 411 | glDisable(GL_CULL_FACE); 412 | glDisable(GL_DEPTH_TEST); 413 | glDisable(GL_STENCIL_TEST); 414 | glEnable(GL_SCISSOR_TEST); 415 | #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART 416 | if (bd->GlVersion >= 310) 417 | glDisable(GL_PRIMITIVE_RESTART); 418 | #endif 419 | #ifdef IMGUI_IMPL_HAS_POLYGON_MODE 420 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); 421 | #endif 422 | 423 | // Support for GL 4.5 rarely used glClipControl(GL_UPPER_LEFT) 424 | #if defined(GL_CLIP_ORIGIN) 425 | bool clip_origin_lower_left = true; 426 | if (bd->HasClipOrigin) 427 | { 428 | GLenum current_clip_origin = 0; glGetIntegerv(GL_CLIP_ORIGIN, (GLint*)¤t_clip_origin); 429 | if (current_clip_origin == GL_UPPER_LEFT) 430 | clip_origin_lower_left = false; 431 | } 432 | #endif 433 | 434 | // Setup viewport, orthographic projection matrix 435 | // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps. 436 | GL_CALL(glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height)); 437 | float L = draw_data->DisplayPos.x; 438 | float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x; 439 | float T = draw_data->DisplayPos.y; 440 | float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y; 441 | #if defined(GL_CLIP_ORIGIN) 442 | if (!clip_origin_lower_left) { float tmp = T; T = B; B = tmp; } // Swap top and bottom if origin is upper left 443 | #endif 444 | const float ortho_projection[4][4] = 445 | { 446 | { 2.0f/(R-L), 0.0f, 0.0f, 0.0f }, 447 | { 0.0f, 2.0f/(T-B), 0.0f, 0.0f }, 448 | { 0.0f, 0.0f, -1.0f, 0.0f }, 449 | { (R+L)/(L-R), (T+B)/(B-T), 0.0f, 1.0f }, 450 | }; 451 | glUseProgram(bd->ShaderHandle); 452 | glUniform1i(bd->AttribLocationTex, 0); 453 | glUniformMatrix4fv(bd->AttribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]); 454 | 455 | #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER 456 | if (bd->GlVersion >= 330 || bd->GlProfileIsES3) 457 | glBindSampler(0, 0); // We use combined texture/sampler state. Applications using GL 3.3 and GL ES 3.0 may set that otherwise. 458 | #endif 459 | 460 | (void)vertex_array_object; 461 | #ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY 462 | glBindVertexArray(vertex_array_object); 463 | #endif 464 | 465 | // Bind vertex/index buffers and setup attributes for ImDrawVert 466 | GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, bd->VboHandle)); 467 | GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bd->ElementsHandle)); 468 | GL_CALL(glEnableVertexAttribArray(bd->AttribLocationVtxPos)); 469 | GL_CALL(glEnableVertexAttribArray(bd->AttribLocationVtxUV)); 470 | GL_CALL(glEnableVertexAttribArray(bd->AttribLocationVtxColor)); 471 | GL_CALL(glVertexAttribPointer(bd->AttribLocationVtxPos, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, pos))); 472 | GL_CALL(glVertexAttribPointer(bd->AttribLocationVtxUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, uv))); 473 | GL_CALL(glVertexAttribPointer(bd->AttribLocationVtxColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, col))); 474 | } 475 | 476 | // OpenGL3 Render function. 477 | // Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL state explicitly. 478 | // This is in order to be able to run within an OpenGL engine that doesn't do so. 479 | void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) 480 | { 481 | // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates) 482 | int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x); 483 | int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y); 484 | if (fb_width <= 0 || fb_height <= 0) 485 | return; 486 | 487 | ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); 488 | 489 | // Backup GL state 490 | GLenum last_active_texture; glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint*)&last_active_texture); 491 | glActiveTexture(GL_TEXTURE0); 492 | GLuint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&last_program); 493 | GLuint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*)&last_texture); 494 | #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER 495 | GLuint last_sampler; if (bd->GlVersion >= 330 || bd->GlProfileIsES3) { glGetIntegerv(GL_SAMPLER_BINDING, (GLint*)&last_sampler); } else { last_sampler = 0; } 496 | #endif 497 | GLuint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, (GLint*)&last_array_buffer); 498 | #ifndef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY 499 | // This is part of VAO on OpenGL 3.0+ and OpenGL ES 3.0+. 500 | GLint last_element_array_buffer; glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &last_element_array_buffer); 501 | ImGui_ImplOpenGL3_VtxAttribState last_vtx_attrib_state_pos; last_vtx_attrib_state_pos.GetState(bd->AttribLocationVtxPos); 502 | ImGui_ImplOpenGL3_VtxAttribState last_vtx_attrib_state_uv; last_vtx_attrib_state_uv.GetState(bd->AttribLocationVtxUV); 503 | ImGui_ImplOpenGL3_VtxAttribState last_vtx_attrib_state_color; last_vtx_attrib_state_color.GetState(bd->AttribLocationVtxColor); 504 | #endif 505 | #ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY 506 | GLuint last_vertex_array_object; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, (GLint*)&last_vertex_array_object); 507 | #endif 508 | #ifdef IMGUI_IMPL_HAS_POLYGON_MODE 509 | GLint last_polygon_mode[2]; glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode); 510 | #endif 511 | GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport); 512 | GLint last_scissor_box[4]; glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box); 513 | GLenum last_blend_src_rgb; glGetIntegerv(GL_BLEND_SRC_RGB, (GLint*)&last_blend_src_rgb); 514 | GLenum last_blend_dst_rgb; glGetIntegerv(GL_BLEND_DST_RGB, (GLint*)&last_blend_dst_rgb); 515 | GLenum last_blend_src_alpha; glGetIntegerv(GL_BLEND_SRC_ALPHA, (GLint*)&last_blend_src_alpha); 516 | GLenum last_blend_dst_alpha; glGetIntegerv(GL_BLEND_DST_ALPHA, (GLint*)&last_blend_dst_alpha); 517 | GLenum last_blend_equation_rgb; glGetIntegerv(GL_BLEND_EQUATION_RGB, (GLint*)&last_blend_equation_rgb); 518 | GLenum last_blend_equation_alpha; glGetIntegerv(GL_BLEND_EQUATION_ALPHA, (GLint*)&last_blend_equation_alpha); 519 | GLboolean last_enable_blend = glIsEnabled(GL_BLEND); 520 | GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE); 521 | GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST); 522 | GLboolean last_enable_stencil_test = glIsEnabled(GL_STENCIL_TEST); 523 | GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST); 524 | #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART 525 | GLboolean last_enable_primitive_restart = (bd->GlVersion >= 310) ? glIsEnabled(GL_PRIMITIVE_RESTART) : GL_FALSE; 526 | #endif 527 | 528 | // Setup desired GL state 529 | // Recreate the VAO every time (this is to easily allow multiple GL contexts to be rendered to. VAO are not shared among GL contexts) 530 | // The renderer would actually work without any VAO bound, but then our VertexAttrib calls would overwrite the default one currently bound. 531 | GLuint vertex_array_object = 0; 532 | #ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY 533 | GL_CALL(glGenVertexArrays(1, &vertex_array_object)); 534 | #endif 535 | ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object); 536 | 537 | // Will project scissor/clipping rectangles into framebuffer space 538 | ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports 539 | ImVec2 clip_scale = draw_data->FramebufferScale; // (1,1) unless using retina display which are often (2,2) 540 | 541 | // Render command lists 542 | for (int n = 0; n < draw_data->CmdListsCount; n++) 543 | { 544 | const ImDrawList* cmd_list = draw_data->CmdLists[n]; 545 | 546 | // Upload vertex/index buffers 547 | // - OpenGL drivers are in a very sorry state nowadays.... 548 | // During 2021 we attempted to switch from glBufferData() to orphaning+glBufferSubData() following reports 549 | // of leaks on Intel GPU when using multi-viewports on Windows. 550 | // - After this we kept hearing of various display corruptions issues. We started disabling on non-Intel GPU, but issues still got reported on Intel. 551 | // - We are now back to using exclusively glBufferData(). So bd->UseBufferSubData IS ALWAYS FALSE in this code. 552 | // We are keeping the old code path for a while in case people finding new issues may want to test the bd->UseBufferSubData path. 553 | // - See https://github.com/ocornut/imgui/issues/4468 and please report any corruption issues. 554 | const GLsizeiptr vtx_buffer_size = (GLsizeiptr)cmd_list->VtxBuffer.Size * (int)sizeof(ImDrawVert); 555 | const GLsizeiptr idx_buffer_size = (GLsizeiptr)cmd_list->IdxBuffer.Size * (int)sizeof(ImDrawIdx); 556 | if (bd->UseBufferSubData) 557 | { 558 | if (bd->VertexBufferSize < vtx_buffer_size) 559 | { 560 | bd->VertexBufferSize = vtx_buffer_size; 561 | GL_CALL(glBufferData(GL_ARRAY_BUFFER, bd->VertexBufferSize, nullptr, GL_STREAM_DRAW)); 562 | } 563 | if (bd->IndexBufferSize < idx_buffer_size) 564 | { 565 | bd->IndexBufferSize = idx_buffer_size; 566 | GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, bd->IndexBufferSize, nullptr, GL_STREAM_DRAW)); 567 | } 568 | GL_CALL(glBufferSubData(GL_ARRAY_BUFFER, 0, vtx_buffer_size, (const GLvoid*)cmd_list->VtxBuffer.Data)); 569 | GL_CALL(glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, idx_buffer_size, (const GLvoid*)cmd_list->IdxBuffer.Data)); 570 | } 571 | else 572 | { 573 | GL_CALL(glBufferData(GL_ARRAY_BUFFER, vtx_buffer_size, (const GLvoid*)cmd_list->VtxBuffer.Data, GL_STREAM_DRAW)); 574 | GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, idx_buffer_size, (const GLvoid*)cmd_list->IdxBuffer.Data, GL_STREAM_DRAW)); 575 | } 576 | 577 | for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) 578 | { 579 | const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i]; 580 | if (pcmd->UserCallback != nullptr) 581 | { 582 | // User callback, registered via ImDrawList::AddCallback() 583 | // (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.) 584 | if (pcmd->UserCallback == ImDrawCallback_ResetRenderState) 585 | ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object); 586 | else 587 | pcmd->UserCallback(cmd_list, pcmd); 588 | } 589 | else 590 | { 591 | // Project scissor/clipping rectangles into framebuffer space 592 | ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y); 593 | ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y); 594 | if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y) 595 | continue; 596 | 597 | // Apply scissor/clipping rectangle (Y is inverted in OpenGL) 598 | GL_CALL(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))); 599 | 600 | // Bind texture, Draw 601 | GL_CALL(glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->GetTexID())); 602 | #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET 603 | if (bd->GlVersion >= 320) 604 | GL_CALL(glDrawElementsBaseVertex(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx)), (GLint)pcmd->VtxOffset)); 605 | else 606 | #endif 607 | GL_CALL(glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx)))); 608 | } 609 | } 610 | } 611 | 612 | // Destroy the temporary VAO 613 | #ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY 614 | GL_CALL(glDeleteVertexArrays(1, &vertex_array_object)); 615 | #endif 616 | 617 | // Restore modified GL state 618 | // This "glIsProgram()" check is required because if the program is "pending deletion" at the time of binding backup, it will have been deleted by now and will cause an OpenGL error. See #6220. 619 | if (last_program == 0 || glIsProgram(last_program)) glUseProgram(last_program); 620 | glBindTexture(GL_TEXTURE_2D, last_texture); 621 | #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER 622 | if (bd->GlVersion >= 330 || bd->GlProfileIsES3) 623 | glBindSampler(0, last_sampler); 624 | #endif 625 | glActiveTexture(last_active_texture); 626 | #ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY 627 | glBindVertexArray(last_vertex_array_object); 628 | #endif 629 | glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer); 630 | #ifndef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY 631 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, last_element_array_buffer); 632 | last_vtx_attrib_state_pos.SetState(bd->AttribLocationVtxPos); 633 | last_vtx_attrib_state_uv.SetState(bd->AttribLocationVtxUV); 634 | last_vtx_attrib_state_color.SetState(bd->AttribLocationVtxColor); 635 | #endif 636 | glBlendEquationSeparate(last_blend_equation_rgb, last_blend_equation_alpha); 637 | glBlendFuncSeparate(last_blend_src_rgb, last_blend_dst_rgb, last_blend_src_alpha, last_blend_dst_alpha); 638 | if (last_enable_blend) glEnable(GL_BLEND); else glDisable(GL_BLEND); 639 | if (last_enable_cull_face) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE); 640 | if (last_enable_depth_test) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); 641 | if (last_enable_stencil_test) glEnable(GL_STENCIL_TEST); else glDisable(GL_STENCIL_TEST); 642 | if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST); 643 | #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART 644 | if (bd->GlVersion >= 310) { if (last_enable_primitive_restart) glEnable(GL_PRIMITIVE_RESTART); else glDisable(GL_PRIMITIVE_RESTART); } 645 | #endif 646 | 647 | #ifdef IMGUI_IMPL_HAS_POLYGON_MODE 648 | // Desktop OpenGL 3.0 and OpenGL 3.1 had separate polygon draw modes for front-facing and back-facing faces of polygons 649 | if (bd->GlVersion <= 310 || bd->GlProfileIsCompat) 650 | { 651 | glPolygonMode(GL_FRONT, (GLenum)last_polygon_mode[0]); 652 | glPolygonMode(GL_BACK, (GLenum)last_polygon_mode[1]); 653 | } 654 | else 655 | { 656 | glPolygonMode(GL_FRONT_AND_BACK, (GLenum)last_polygon_mode[0]); 657 | } 658 | #endif // IMGUI_IMPL_HAS_POLYGON_MODE 659 | 660 | glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]); 661 | glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]); 662 | (void)bd; // Not all compilation paths use this 663 | } 664 | 665 | bool ImGui_ImplOpenGL3_CreateFontsTexture() 666 | { 667 | ImGuiIO& io = ImGui::GetIO(); 668 | ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); 669 | 670 | // Build texture atlas 671 | unsigned char* pixels; 672 | int width, height; 673 | io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bit (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory. 674 | 675 | // Upload texture to graphics system 676 | // (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling) 677 | GLint last_texture; 678 | GL_CALL(glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture)); 679 | GL_CALL(glGenTextures(1, &bd->FontTexture)); 680 | GL_CALL(glBindTexture(GL_TEXTURE_2D, bd->FontTexture)); 681 | GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); 682 | GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); 683 | #ifdef GL_UNPACK_ROW_LENGTH // Not on WebGL/ES 684 | GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, 0)); 685 | #endif 686 | GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels)); 687 | 688 | // Store our identifier 689 | io.Fonts->SetTexID((ImTextureID)(intptr_t)bd->FontTexture); 690 | 691 | // Restore state 692 | GL_CALL(glBindTexture(GL_TEXTURE_2D, last_texture)); 693 | 694 | return true; 695 | } 696 | 697 | void ImGui_ImplOpenGL3_DestroyFontsTexture() 698 | { 699 | ImGuiIO& io = ImGui::GetIO(); 700 | ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); 701 | if (bd->FontTexture) 702 | { 703 | glDeleteTextures(1, &bd->FontTexture); 704 | io.Fonts->SetTexID(0); 705 | bd->FontTexture = 0; 706 | } 707 | } 708 | 709 | // If you get an error please report on github. You may try different GL context version or GLSL version. See GL<>GLSL version table at the top of this file. 710 | static bool CheckShader(GLuint handle, const char* desc) 711 | { 712 | ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); 713 | GLint status = 0, log_length = 0; 714 | glGetShaderiv(handle, GL_COMPILE_STATUS, &status); 715 | glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &log_length); 716 | if ((GLboolean)status == GL_FALSE) 717 | fprintf(stderr, "ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to compile %s! With GLSL: %s\n", desc, bd->GlslVersionString); 718 | if (log_length > 1) 719 | { 720 | ImVector buf; 721 | buf.resize((int)(log_length + 1)); 722 | glGetShaderInfoLog(handle, log_length, nullptr, (GLchar*)buf.begin()); 723 | fprintf(stderr, "%s\n", buf.begin()); 724 | } 725 | return (GLboolean)status == GL_TRUE; 726 | } 727 | 728 | // If you get an error please report on GitHub. You may try different GL context version or GLSL version. 729 | static bool CheckProgram(GLuint handle, const char* desc) 730 | { 731 | ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); 732 | GLint status = 0, log_length = 0; 733 | glGetProgramiv(handle, GL_LINK_STATUS, &status); 734 | glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &log_length); 735 | if ((GLboolean)status == GL_FALSE) 736 | fprintf(stderr, "ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to link %s! With GLSL %s\n", desc, bd->GlslVersionString); 737 | if (log_length > 1) 738 | { 739 | ImVector buf; 740 | buf.resize((int)(log_length + 1)); 741 | glGetProgramInfoLog(handle, log_length, nullptr, (GLchar*)buf.begin()); 742 | fprintf(stderr, "%s\n", buf.begin()); 743 | } 744 | return (GLboolean)status == GL_TRUE; 745 | } 746 | 747 | bool ImGui_ImplOpenGL3_CreateDeviceObjects() 748 | { 749 | ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); 750 | 751 | // Backup GL state 752 | GLint last_texture, last_array_buffer; 753 | glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture); 754 | glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer); 755 | #ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY 756 | GLint last_vertex_array; 757 | glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array); 758 | #endif 759 | 760 | // Parse GLSL version string 761 | int glsl_version = 130; 762 | sscanf(bd->GlslVersionString, "#version %d", &glsl_version); 763 | 764 | const GLchar* vertex_shader_glsl_120 = 765 | "uniform mat4 ProjMtx;\n" 766 | "attribute vec2 Position;\n" 767 | "attribute vec2 UV;\n" 768 | "attribute vec4 Color;\n" 769 | "varying vec2 Frag_UV;\n" 770 | "varying vec4 Frag_Color;\n" 771 | "void main()\n" 772 | "{\n" 773 | " Frag_UV = UV;\n" 774 | " Frag_Color = Color;\n" 775 | " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n" 776 | "}\n"; 777 | 778 | const GLchar* vertex_shader_glsl_130 = 779 | "uniform mat4 ProjMtx;\n" 780 | "in vec2 Position;\n" 781 | "in vec2 UV;\n" 782 | "in vec4 Color;\n" 783 | "out vec2 Frag_UV;\n" 784 | "out vec4 Frag_Color;\n" 785 | "void main()\n" 786 | "{\n" 787 | " Frag_UV = UV;\n" 788 | " Frag_Color = Color;\n" 789 | " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n" 790 | "}\n"; 791 | 792 | const GLchar* vertex_shader_glsl_300_es = 793 | "precision highp float;\n" 794 | "layout (location = 0) in vec2 Position;\n" 795 | "layout (location = 1) in vec2 UV;\n" 796 | "layout (location = 2) in vec4 Color;\n" 797 | "uniform mat4 ProjMtx;\n" 798 | "out vec2 Frag_UV;\n" 799 | "out vec4 Frag_Color;\n" 800 | "void main()\n" 801 | "{\n" 802 | " Frag_UV = UV;\n" 803 | " Frag_Color = Color;\n" 804 | " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n" 805 | "}\n"; 806 | 807 | const GLchar* vertex_shader_glsl_410_core = 808 | "layout (location = 0) in vec2 Position;\n" 809 | "layout (location = 1) in vec2 UV;\n" 810 | "layout (location = 2) in vec4 Color;\n" 811 | "uniform mat4 ProjMtx;\n" 812 | "out vec2 Frag_UV;\n" 813 | "out vec4 Frag_Color;\n" 814 | "void main()\n" 815 | "{\n" 816 | " Frag_UV = UV;\n" 817 | " Frag_Color = Color;\n" 818 | " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n" 819 | "}\n"; 820 | 821 | const GLchar* fragment_shader_glsl_120 = 822 | "#ifdef GL_ES\n" 823 | " precision mediump float;\n" 824 | "#endif\n" 825 | "uniform sampler2D Texture;\n" 826 | "varying vec2 Frag_UV;\n" 827 | "varying vec4 Frag_Color;\n" 828 | "void main()\n" 829 | "{\n" 830 | " gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV.st);\n" 831 | "}\n"; 832 | 833 | const GLchar* fragment_shader_glsl_130 = 834 | "uniform sampler2D Texture;\n" 835 | "in vec2 Frag_UV;\n" 836 | "in vec4 Frag_Color;\n" 837 | "out vec4 Out_Color;\n" 838 | "void main()\n" 839 | "{\n" 840 | " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n" 841 | "}\n"; 842 | 843 | const GLchar* fragment_shader_glsl_300_es = 844 | "precision mediump float;\n" 845 | "uniform sampler2D Texture;\n" 846 | "in vec2 Frag_UV;\n" 847 | "in vec4 Frag_Color;\n" 848 | "layout (location = 0) out vec4 Out_Color;\n" 849 | "void main()\n" 850 | "{\n" 851 | " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n" 852 | "}\n"; 853 | 854 | const GLchar* fragment_shader_glsl_410_core = 855 | "in vec2 Frag_UV;\n" 856 | "in vec4 Frag_Color;\n" 857 | "uniform sampler2D Texture;\n" 858 | "layout (location = 0) out vec4 Out_Color;\n" 859 | "void main()\n" 860 | "{\n" 861 | " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n" 862 | "}\n"; 863 | 864 | // Select shaders matching our GLSL versions 865 | const GLchar* vertex_shader = nullptr; 866 | const GLchar* fragment_shader = nullptr; 867 | if (glsl_version < 130) 868 | { 869 | vertex_shader = vertex_shader_glsl_120; 870 | fragment_shader = fragment_shader_glsl_120; 871 | } 872 | else if (glsl_version >= 410) 873 | { 874 | vertex_shader = vertex_shader_glsl_410_core; 875 | fragment_shader = fragment_shader_glsl_410_core; 876 | } 877 | else if (glsl_version == 300) 878 | { 879 | vertex_shader = vertex_shader_glsl_300_es; 880 | fragment_shader = fragment_shader_glsl_300_es; 881 | } 882 | else 883 | { 884 | vertex_shader = vertex_shader_glsl_130; 885 | fragment_shader = fragment_shader_glsl_130; 886 | } 887 | 888 | // Create shaders 889 | const GLchar* vertex_shader_with_version[2] = { bd->GlslVersionString, vertex_shader }; 890 | GLuint vert_handle = glCreateShader(GL_VERTEX_SHADER); 891 | glShaderSource(vert_handle, 2, vertex_shader_with_version, nullptr); 892 | glCompileShader(vert_handle); 893 | CheckShader(vert_handle, "vertex shader"); 894 | 895 | const GLchar* fragment_shader_with_version[2] = { bd->GlslVersionString, fragment_shader }; 896 | GLuint frag_handle = glCreateShader(GL_FRAGMENT_SHADER); 897 | glShaderSource(frag_handle, 2, fragment_shader_with_version, nullptr); 898 | glCompileShader(frag_handle); 899 | CheckShader(frag_handle, "fragment shader"); 900 | 901 | // Link 902 | bd->ShaderHandle = glCreateProgram(); 903 | glAttachShader(bd->ShaderHandle, vert_handle); 904 | glAttachShader(bd->ShaderHandle, frag_handle); 905 | glLinkProgram(bd->ShaderHandle); 906 | CheckProgram(bd->ShaderHandle, "shader program"); 907 | 908 | glDetachShader(bd->ShaderHandle, vert_handle); 909 | glDetachShader(bd->ShaderHandle, frag_handle); 910 | glDeleteShader(vert_handle); 911 | glDeleteShader(frag_handle); 912 | 913 | bd->AttribLocationTex = glGetUniformLocation(bd->ShaderHandle, "Texture"); 914 | bd->AttribLocationProjMtx = glGetUniformLocation(bd->ShaderHandle, "ProjMtx"); 915 | bd->AttribLocationVtxPos = (GLuint)glGetAttribLocation(bd->ShaderHandle, "Position"); 916 | bd->AttribLocationVtxUV = (GLuint)glGetAttribLocation(bd->ShaderHandle, "UV"); 917 | bd->AttribLocationVtxColor = (GLuint)glGetAttribLocation(bd->ShaderHandle, "Color"); 918 | 919 | // Create buffers 920 | glGenBuffers(1, &bd->VboHandle); 921 | glGenBuffers(1, &bd->ElementsHandle); 922 | 923 | ImGui_ImplOpenGL3_CreateFontsTexture(); 924 | 925 | // Restore modified GL state 926 | glBindTexture(GL_TEXTURE_2D, last_texture); 927 | glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer); 928 | #ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY 929 | glBindVertexArray(last_vertex_array); 930 | #endif 931 | 932 | return true; 933 | } 934 | 935 | void ImGui_ImplOpenGL3_DestroyDeviceObjects() 936 | { 937 | ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); 938 | if (bd->VboHandle) { glDeleteBuffers(1, &bd->VboHandle); bd->VboHandle = 0; } 939 | if (bd->ElementsHandle) { glDeleteBuffers(1, &bd->ElementsHandle); bd->ElementsHandle = 0; } 940 | if (bd->ShaderHandle) { glDeleteProgram(bd->ShaderHandle); bd->ShaderHandle = 0; } 941 | ImGui_ImplOpenGL3_DestroyFontsTexture(); 942 | } 943 | 944 | //-------------------------------------------------------------------------------------------------------- 945 | // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT 946 | // This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously. 947 | // If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first.. 948 | //-------------------------------------------------------------------------------------------------------- 949 | 950 | static void ImGui_ImplOpenGL3_RenderWindow(ImGuiViewport* viewport, void*) 951 | { 952 | if (!(viewport->Flags & ImGuiViewportFlags_NoRendererClear)) 953 | { 954 | ImVec4 clear_color = ImVec4(0.0f, 0.0f, 0.0f, 1.0f); 955 | glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w); 956 | glClear(GL_COLOR_BUFFER_BIT); 957 | } 958 | ImGui_ImplOpenGL3_RenderDrawData(viewport->DrawData); 959 | } 960 | 961 | static void ImGui_ImplOpenGL3_InitPlatformInterface() 962 | { 963 | ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); 964 | platform_io.Renderer_RenderWindow = ImGui_ImplOpenGL3_RenderWindow; 965 | } 966 | 967 | static void ImGui_ImplOpenGL3_ShutdownPlatformInterface() 968 | { 969 | ImGui::DestroyPlatformWindows(); 970 | } 971 | 972 | //----------------------------------------------------------------------------- 973 | 974 | #if defined(__GNUC__) 975 | #pragma GCC diagnostic pop 976 | #endif 977 | #if defined(__clang__) 978 | #pragma clang diagnostic pop 979 | #endif 980 | 981 | #endif // #ifndef IMGUI_DISABLE 982 | -------------------------------------------------------------------------------- /imgui/imgui_impl_opengl3.h: -------------------------------------------------------------------------------- 1 | // dear imgui: Renderer Backend for modern OpenGL with shaders / programmatic pipeline 2 | // - Desktop GL: 2.x 3.x 4.x 3 | // - Embedded GL: ES 2.0 (WebGL 1.0), ES 3.0 (WebGL 2.0) 4 | // This needs to be used along with a Platform Backend (e.g. GLFW, SDL, Win32, custom..) 5 | 6 | // Implemented features: 7 | // [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID! 8 | // [X] Renderer: Large meshes support (64k+ vertices) with 16-bit indices (Desktop OpenGL only). 9 | // [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'. 10 | 11 | // About WebGL/ES: 12 | // - You need to '#define IMGUI_IMPL_OPENGL_ES2' or '#define IMGUI_IMPL_OPENGL_ES3' to use WebGL or OpenGL ES. 13 | // - This is done automatically on iOS, Android and Emscripten targets. 14 | // - For other targets, the define needs to be visible from the imgui_impl_opengl3.cpp compilation unit. If unsure, define globally or in imconfig.h. 15 | 16 | // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. 17 | // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. 18 | // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. 19 | // Read online: https://github.com/ocornut/imgui/tree/master/docs 20 | 21 | // About GLSL version: 22 | // The 'glsl_version' initialization parameter should be nullptr (default) or a "#version XXX" string. 23 | // On computer platform the GLSL version default to "#version 130". On OpenGL ES 3 platform it defaults to "#version 300 es" 24 | // Only override if your GL version doesn't handle this GLSL version. See GLSL version table at the top of imgui_impl_opengl3.cpp. 25 | 26 | #pragma once 27 | #include "imgui.h" // IMGUI_IMPL_API 28 | #ifndef IMGUI_DISABLE 29 | 30 | // Backend API 31 | IMGUI_IMPL_API bool ImGui_ImplOpenGL3_Init(const char* glsl_version = nullptr); 32 | IMGUI_IMPL_API void ImGui_ImplOpenGL3_Shutdown(); 33 | IMGUI_IMPL_API void ImGui_ImplOpenGL3_NewFrame(); 34 | IMGUI_IMPL_API void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data); 35 | 36 | // (Optional) Called by Init/NewFrame/Shutdown 37 | IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateFontsTexture(); 38 | IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyFontsTexture(); 39 | IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateDeviceObjects(); 40 | IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects(); 41 | 42 | // Specific OpenGL ES versions 43 | //#define IMGUI_IMPL_OPENGL_ES2 // Auto-detected on Emscripten 44 | //#define IMGUI_IMPL_OPENGL_ES3 // Auto-detected on iOS/Android 45 | 46 | // You can explicitly select GLES2 or GLES3 API by using one of the '#define IMGUI_IMPL_OPENGL_LOADER_XXX' in imconfig.h or compiler command-line. 47 | #if !defined(IMGUI_IMPL_OPENGL_ES2) \ 48 | && !defined(IMGUI_IMPL_OPENGL_ES3) 49 | 50 | // Try to detect GLES on matching platforms 51 | #if defined(__APPLE__) 52 | #include 53 | #endif 54 | #if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) || (defined(__ANDROID__)) 55 | #define IMGUI_IMPL_OPENGL_ES3 // iOS, Android -> GL ES 3, "#version 300 es" 56 | #elif defined(__EMSCRIPTEN__) || defined(__amigaos4__) 57 | #define IMGUI_IMPL_OPENGL_ES2 // Emscripten -> GL ES 2, "#version 100" 58 | #else 59 | // Otherwise imgui_impl_opengl3_loader.h will be used. 60 | #endif 61 | 62 | #endif 63 | 64 | #endif // #ifndef IMGUI_DISABLE 65 | -------------------------------------------------------------------------------- /imgui/imgui_impl_opengl3_loader.h: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // About imgui_impl_opengl3_loader.h: 3 | // 4 | // We embed our own OpenGL loader to not require user to provide their own or to have to use ours, 5 | // which proved to be endless problems for users. 6 | // Our loader is custom-generated, based on gl3w but automatically filtered to only include 7 | // enums/functions that we use in our imgui_impl_opengl3.cpp source file in order to be small. 8 | // 9 | // YOU SHOULD NOT NEED TO INCLUDE/USE THIS DIRECTLY. THIS IS USED BY imgui_impl_opengl3.cpp ONLY. 10 | // THE REST OF YOUR APP SHOULD USE A DIFFERENT GL LOADER: ANY GL LOADER OF YOUR CHOICE. 11 | // 12 | // IF YOU GET BUILD ERRORS IN THIS FILE (commonly macro redefinitions or function redefinitions): 13 | // IT LIKELY MEANS THAT YOU ARE BUILDING 'imgui_impl_opengl3.cpp' OR INCUDING 'imgui_impl_opengl3_loader.h' 14 | // IN THE SAME COMPILATION UNIT AS ONE OF YOUR FILE WHICH IS USING A THIRD-PARTY OPENGL LOADER. 15 | // (e.g. COULD HAPPEN IF YOU ARE DOING A UNITY/JUMBO BUILD, OR INCLUDING .CPP FILES FROM OTHERS) 16 | // YOU SHOULD NOT BUILD BOTH IN THE SAME COMPILATION UNIT. 17 | // BUT IF YOU REALLY WANT TO, you can '#define IMGUI_IMPL_OPENGL_LOADER_CUSTOM' and imgui_impl_opengl3.cpp 18 | // WILL NOT BE USING OUR LOADER, AND INSTEAD EXPECT ANOTHER/YOUR LOADER TO BE AVAILABLE IN THE COMPILATION UNIT. 19 | // 20 | // Regenerate with: 21 | // python gl3w_gen.py --output ../imgui/backends/imgui_impl_opengl3_loader.h --ref ../imgui/backends/imgui_impl_opengl3.cpp ./extra_symbols.txt 22 | // 23 | // More info: 24 | // https://github.com/dearimgui/gl3w_stripped 25 | // https://github.com/ocornut/imgui/issues/4445 26 | //----------------------------------------------------------------------------- 27 | 28 | /* 29 | * This file was generated with gl3w_gen.py, part of imgl3w 30 | * (hosted at https://github.com/dearimgui/gl3w_stripped) 31 | * 32 | * This is free and unencumbered software released into the public domain. 33 | * 34 | * Anyone is free to copy, modify, publish, use, compile, sell, or 35 | * distribute this software, either in source code form or as a compiled 36 | * binary, for any purpose, commercial or non-commercial, and by any 37 | * means. 38 | * 39 | * In jurisdictions that recognize copyright laws, the author or authors 40 | * of this software dedicate any and all copyright interest in the 41 | * software to the public domain. We make this dedication for the benefit 42 | * of the public at large and to the detriment of our heirs and 43 | * successors. We intend this dedication to be an overt act of 44 | * relinquishment in perpetuity of all present and future rights to this 45 | * software under copyright law. 46 | * 47 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 48 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 49 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 50 | * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 51 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 52 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 53 | * OTHER DEALINGS IN THE SOFTWARE. 54 | */ 55 | 56 | #ifndef __gl3w_h_ 57 | #define __gl3w_h_ 58 | 59 | // Adapted from KHR/khrplatform.h to avoid including entire file. 60 | #ifndef __khrplatform_h_ 61 | typedef float khronos_float_t; 62 | typedef signed char khronos_int8_t; 63 | typedef unsigned char khronos_uint8_t; 64 | typedef signed short int khronos_int16_t; 65 | typedef unsigned short int khronos_uint16_t; 66 | #ifdef _WIN64 67 | typedef signed long long int khronos_intptr_t; 68 | typedef signed long long int khronos_ssize_t; 69 | #else 70 | typedef signed long int khronos_intptr_t; 71 | typedef signed long int khronos_ssize_t; 72 | #endif 73 | 74 | #if defined(_MSC_VER) && !defined(__clang__) 75 | typedef signed __int64 khronos_int64_t; 76 | typedef unsigned __int64 khronos_uint64_t; 77 | #elif (defined(__clang__) || defined(__GNUC__)) && (__cplusplus < 201100) 78 | #include 79 | typedef int64_t khronos_int64_t; 80 | typedef uint64_t khronos_uint64_t; 81 | #else 82 | typedef signed long long khronos_int64_t; 83 | typedef unsigned long long khronos_uint64_t; 84 | #endif 85 | #endif // __khrplatform_h_ 86 | 87 | #ifndef __gl_glcorearb_h_ 88 | #define __gl_glcorearb_h_ 1 89 | #ifdef __cplusplus 90 | extern "C" { 91 | #endif 92 | /* 93 | ** Copyright 2013-2020 The Khronos Group Inc. 94 | ** SPDX-License-Identifier: MIT 95 | ** 96 | ** This header is generated from the Khronos OpenGL / OpenGL ES XML 97 | ** API Registry. The current version of the Registry, generator scripts 98 | ** used to make the header, and the header can be found at 99 | ** https://github.com/KhronosGroup/OpenGL-Registry 100 | */ 101 | #if defined(_WIN32) && !defined(APIENTRY) && !defined(__CYGWIN__) && !defined(__SCITECH_SNAP__) 102 | #ifndef WIN32_LEAN_AND_MEAN 103 | #define WIN32_LEAN_AND_MEAN 1 104 | #endif 105 | #include 106 | #endif 107 | #ifndef APIENTRY 108 | #define APIENTRY 109 | #endif 110 | #ifndef APIENTRYP 111 | #define APIENTRYP APIENTRY * 112 | #endif 113 | #ifndef GLAPI 114 | #define GLAPI extern 115 | #endif 116 | /* glcorearb.h is for use with OpenGL core profile implementations. 117 | ** It should should be placed in the same directory as gl.h and 118 | ** included as . 119 | ** 120 | ** glcorearb.h includes only APIs in the latest OpenGL core profile 121 | ** implementation together with APIs in newer ARB extensions which 122 | ** can be supported by the core profile. It does not, and never will 123 | ** include functionality removed from the core profile, such as 124 | ** fixed-function vertex and fragment processing. 125 | ** 126 | ** Do not #include both and either of or 127 | ** in the same source file. 128 | */ 129 | /* Generated C header for: 130 | * API: gl 131 | * Profile: core 132 | * Versions considered: .* 133 | * Versions emitted: .* 134 | * Default extensions included: glcore 135 | * Additional extensions included: _nomatch_^ 136 | * Extensions removed: _nomatch_^ 137 | */ 138 | #ifndef GL_VERSION_1_0 139 | typedef void GLvoid; 140 | typedef unsigned int GLenum; 141 | 142 | typedef khronos_float_t GLfloat; 143 | typedef int GLint; 144 | typedef int GLsizei; 145 | typedef unsigned int GLbitfield; 146 | typedef double GLdouble; 147 | typedef unsigned int GLuint; 148 | typedef unsigned char GLboolean; 149 | typedef khronos_uint8_t GLubyte; 150 | #define GL_COLOR_BUFFER_BIT 0x00004000 151 | #define GL_FALSE 0 152 | #define GL_TRUE 1 153 | #define GL_TRIANGLES 0x0004 154 | #define GL_ONE 1 155 | #define GL_SRC_ALPHA 0x0302 156 | #define GL_ONE_MINUS_SRC_ALPHA 0x0303 157 | #define GL_FRONT 0x0404 158 | #define GL_BACK 0x0405 159 | #define GL_FRONT_AND_BACK 0x0408 160 | #define GL_POLYGON_MODE 0x0B40 161 | #define GL_CULL_FACE 0x0B44 162 | #define GL_DEPTH_TEST 0x0B71 163 | #define GL_STENCIL_TEST 0x0B90 164 | #define GL_VIEWPORT 0x0BA2 165 | #define GL_BLEND 0x0BE2 166 | #define GL_SCISSOR_BOX 0x0C10 167 | #define GL_SCISSOR_TEST 0x0C11 168 | #define GL_UNPACK_ROW_LENGTH 0x0CF2 169 | #define GL_PACK_ALIGNMENT 0x0D05 170 | #define GL_TEXTURE_2D 0x0DE1 171 | #define GL_UNSIGNED_BYTE 0x1401 172 | #define GL_UNSIGNED_SHORT 0x1403 173 | #define GL_UNSIGNED_INT 0x1405 174 | #define GL_FLOAT 0x1406 175 | #define GL_RGBA 0x1908 176 | #define GL_FILL 0x1B02 177 | #define GL_VENDOR 0x1F00 178 | #define GL_RENDERER 0x1F01 179 | #define GL_VERSION 0x1F02 180 | #define GL_EXTENSIONS 0x1F03 181 | #define GL_LINEAR 0x2601 182 | #define GL_TEXTURE_MAG_FILTER 0x2800 183 | #define GL_TEXTURE_MIN_FILTER 0x2801 184 | typedef void (APIENTRYP PFNGLPOLYGONMODEPROC) (GLenum face, GLenum mode); 185 | typedef void (APIENTRYP PFNGLSCISSORPROC) (GLint x, GLint y, GLsizei width, GLsizei height); 186 | typedef void (APIENTRYP PFNGLTEXPARAMETERIPROC) (GLenum target, GLenum pname, GLint param); 187 | typedef void (APIENTRYP PFNGLTEXIMAGE2DPROC) (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *pixels); 188 | typedef void (APIENTRYP PFNGLCLEARPROC) (GLbitfield mask); 189 | typedef void (APIENTRYP PFNGLCLEARCOLORPROC) (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha); 190 | typedef void (APIENTRYP PFNGLDISABLEPROC) (GLenum cap); 191 | typedef void (APIENTRYP PFNGLENABLEPROC) (GLenum cap); 192 | typedef void (APIENTRYP PFNGLFLUSHPROC) (void); 193 | typedef void (APIENTRYP PFNGLPIXELSTOREIPROC) (GLenum pname, GLint param); 194 | typedef void (APIENTRYP PFNGLREADPIXELSPROC) (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void *pixels); 195 | typedef GLenum (APIENTRYP PFNGLGETERRORPROC) (void); 196 | typedef void (APIENTRYP PFNGLGETINTEGERVPROC) (GLenum pname, GLint *data); 197 | typedef const GLubyte *(APIENTRYP PFNGLGETSTRINGPROC) (GLenum name); 198 | typedef GLboolean (APIENTRYP PFNGLISENABLEDPROC) (GLenum cap); 199 | typedef void (APIENTRYP PFNGLVIEWPORTPROC) (GLint x, GLint y, GLsizei width, GLsizei height); 200 | #ifdef GL_GLEXT_PROTOTYPES 201 | GLAPI void APIENTRY glPolygonMode (GLenum face, GLenum mode); 202 | GLAPI void APIENTRY glScissor (GLint x, GLint y, GLsizei width, GLsizei height); 203 | GLAPI void APIENTRY glTexParameteri (GLenum target, GLenum pname, GLint param); 204 | GLAPI void APIENTRY glTexImage2D (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *pixels); 205 | GLAPI void APIENTRY glClear (GLbitfield mask); 206 | GLAPI void APIENTRY glClearColor (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha); 207 | GLAPI void APIENTRY glDisable (GLenum cap); 208 | GLAPI void APIENTRY glEnable (GLenum cap); 209 | GLAPI void APIENTRY glFlush (void); 210 | GLAPI void APIENTRY glPixelStorei (GLenum pname, GLint param); 211 | GLAPI void APIENTRY glReadPixels (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void *pixels); 212 | GLAPI GLenum APIENTRY glGetError (void); 213 | GLAPI void APIENTRY glGetIntegerv (GLenum pname, GLint *data); 214 | GLAPI const GLubyte *APIENTRY glGetString (GLenum name); 215 | GLAPI GLboolean APIENTRY glIsEnabled (GLenum cap); 216 | GLAPI void APIENTRY glViewport (GLint x, GLint y, GLsizei width, GLsizei height); 217 | #endif 218 | #endif /* GL_VERSION_1_0 */ 219 | #ifndef GL_VERSION_1_1 220 | typedef khronos_float_t GLclampf; 221 | typedef double GLclampd; 222 | #define GL_TEXTURE_BINDING_2D 0x8069 223 | typedef void (APIENTRYP PFNGLDRAWELEMENTSPROC) (GLenum mode, GLsizei count, GLenum type, const void *indices); 224 | typedef void (APIENTRYP PFNGLBINDTEXTUREPROC) (GLenum target, GLuint texture); 225 | typedef void (APIENTRYP PFNGLDELETETEXTURESPROC) (GLsizei n, const GLuint *textures); 226 | typedef void (APIENTRYP PFNGLGENTEXTURESPROC) (GLsizei n, GLuint *textures); 227 | #ifdef GL_GLEXT_PROTOTYPES 228 | GLAPI void APIENTRY glDrawElements (GLenum mode, GLsizei count, GLenum type, const void *indices); 229 | GLAPI void APIENTRY glBindTexture (GLenum target, GLuint texture); 230 | GLAPI void APIENTRY glDeleteTextures (GLsizei n, const GLuint *textures); 231 | GLAPI void APIENTRY glGenTextures (GLsizei n, GLuint *textures); 232 | #endif 233 | #endif /* GL_VERSION_1_1 */ 234 | #ifndef GL_VERSION_1_3 235 | #define GL_TEXTURE0 0x84C0 236 | #define GL_ACTIVE_TEXTURE 0x84E0 237 | typedef void (APIENTRYP PFNGLACTIVETEXTUREPROC) (GLenum texture); 238 | #ifdef GL_GLEXT_PROTOTYPES 239 | GLAPI void APIENTRY glActiveTexture (GLenum texture); 240 | #endif 241 | #endif /* GL_VERSION_1_3 */ 242 | #ifndef GL_VERSION_1_4 243 | #define GL_BLEND_DST_RGB 0x80C8 244 | #define GL_BLEND_SRC_RGB 0x80C9 245 | #define GL_BLEND_DST_ALPHA 0x80CA 246 | #define GL_BLEND_SRC_ALPHA 0x80CB 247 | #define GL_FUNC_ADD 0x8006 248 | typedef void (APIENTRYP PFNGLBLENDFUNCSEPARATEPROC) (GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorAlpha, GLenum dfactorAlpha); 249 | typedef void (APIENTRYP PFNGLBLENDEQUATIONPROC) (GLenum mode); 250 | #ifdef GL_GLEXT_PROTOTYPES 251 | GLAPI void APIENTRY glBlendFuncSeparate (GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorAlpha, GLenum dfactorAlpha); 252 | GLAPI void APIENTRY glBlendEquation (GLenum mode); 253 | #endif 254 | #endif /* GL_VERSION_1_4 */ 255 | #ifndef GL_VERSION_1_5 256 | typedef khronos_ssize_t GLsizeiptr; 257 | typedef khronos_intptr_t GLintptr; 258 | #define GL_ARRAY_BUFFER 0x8892 259 | #define GL_ELEMENT_ARRAY_BUFFER 0x8893 260 | #define GL_ARRAY_BUFFER_BINDING 0x8894 261 | #define GL_ELEMENT_ARRAY_BUFFER_BINDING 0x8895 262 | #define GL_STREAM_DRAW 0x88E0 263 | typedef void (APIENTRYP PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer); 264 | typedef void (APIENTRYP PFNGLDELETEBUFFERSPROC) (GLsizei n, const GLuint *buffers); 265 | typedef void (APIENTRYP PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers); 266 | typedef void (APIENTRYP PFNGLBUFFERDATAPROC) (GLenum target, GLsizeiptr size, const void *data, GLenum usage); 267 | typedef void (APIENTRYP PFNGLBUFFERSUBDATAPROC) (GLenum target, GLintptr offset, GLsizeiptr size, const void *data); 268 | #ifdef GL_GLEXT_PROTOTYPES 269 | GLAPI void APIENTRY glBindBuffer (GLenum target, GLuint buffer); 270 | GLAPI void APIENTRY glDeleteBuffers (GLsizei n, const GLuint *buffers); 271 | GLAPI void APIENTRY glGenBuffers (GLsizei n, GLuint *buffers); 272 | GLAPI void APIENTRY glBufferData (GLenum target, GLsizeiptr size, const void *data, GLenum usage); 273 | GLAPI void APIENTRY glBufferSubData (GLenum target, GLintptr offset, GLsizeiptr size, const void *data); 274 | #endif 275 | #endif /* GL_VERSION_1_5 */ 276 | #ifndef GL_VERSION_2_0 277 | typedef char GLchar; 278 | typedef khronos_int16_t GLshort; 279 | typedef khronos_int8_t GLbyte; 280 | typedef khronos_uint16_t GLushort; 281 | #define GL_BLEND_EQUATION_RGB 0x8009 282 | #define GL_VERTEX_ATTRIB_ARRAY_ENABLED 0x8622 283 | #define GL_VERTEX_ATTRIB_ARRAY_SIZE 0x8623 284 | #define GL_VERTEX_ATTRIB_ARRAY_STRIDE 0x8624 285 | #define GL_VERTEX_ATTRIB_ARRAY_TYPE 0x8625 286 | #define GL_VERTEX_ATTRIB_ARRAY_POINTER 0x8645 287 | #define GL_BLEND_EQUATION_ALPHA 0x883D 288 | #define GL_VERTEX_ATTRIB_ARRAY_NORMALIZED 0x886A 289 | #define GL_FRAGMENT_SHADER 0x8B30 290 | #define GL_VERTEX_SHADER 0x8B31 291 | #define GL_COMPILE_STATUS 0x8B81 292 | #define GL_LINK_STATUS 0x8B82 293 | #define GL_INFO_LOG_LENGTH 0x8B84 294 | #define GL_CURRENT_PROGRAM 0x8B8D 295 | #define GL_UPPER_LEFT 0x8CA2 296 | typedef void (APIENTRYP PFNGLBLENDEQUATIONSEPARATEPROC) (GLenum modeRGB, GLenum modeAlpha); 297 | typedef void (APIENTRYP PFNGLATTACHSHADERPROC) (GLuint program, GLuint shader); 298 | typedef void (APIENTRYP PFNGLCOMPILESHADERPROC) (GLuint shader); 299 | typedef GLuint (APIENTRYP PFNGLCREATEPROGRAMPROC) (void); 300 | typedef GLuint (APIENTRYP PFNGLCREATESHADERPROC) (GLenum type); 301 | typedef void (APIENTRYP PFNGLDELETEPROGRAMPROC) (GLuint program); 302 | typedef void (APIENTRYP PFNGLDELETESHADERPROC) (GLuint shader); 303 | typedef void (APIENTRYP PFNGLDETACHSHADERPROC) (GLuint program, GLuint shader); 304 | typedef void (APIENTRYP PFNGLDISABLEVERTEXATTRIBARRAYPROC) (GLuint index); 305 | typedef void (APIENTRYP PFNGLENABLEVERTEXATTRIBARRAYPROC) (GLuint index); 306 | typedef GLint (APIENTRYP PFNGLGETATTRIBLOCATIONPROC) (GLuint program, const GLchar *name); 307 | typedef void (APIENTRYP PFNGLGETPROGRAMIVPROC) (GLuint program, GLenum pname, GLint *params); 308 | typedef void (APIENTRYP PFNGLGETPROGRAMINFOLOGPROC) (GLuint program, GLsizei bufSize, GLsizei *length, GLchar *infoLog); 309 | typedef void (APIENTRYP PFNGLGETSHADERIVPROC) (GLuint shader, GLenum pname, GLint *params); 310 | typedef void (APIENTRYP PFNGLGETSHADERINFOLOGPROC) (GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog); 311 | typedef GLint (APIENTRYP PFNGLGETUNIFORMLOCATIONPROC) (GLuint program, const GLchar *name); 312 | typedef void (APIENTRYP PFNGLGETVERTEXATTRIBIVPROC) (GLuint index, GLenum pname, GLint *params); 313 | typedef void (APIENTRYP PFNGLGETVERTEXATTRIBPOINTERVPROC) (GLuint index, GLenum pname, void **pointer); 314 | typedef GLboolean (APIENTRYP PFNGLISPROGRAMPROC) (GLuint program); 315 | typedef void (APIENTRYP PFNGLLINKPROGRAMPROC) (GLuint program); 316 | typedef void (APIENTRYP PFNGLSHADERSOURCEPROC) (GLuint shader, GLsizei count, const GLchar *const*string, const GLint *length); 317 | typedef void (APIENTRYP PFNGLUSEPROGRAMPROC) (GLuint program); 318 | typedef void (APIENTRYP PFNGLUNIFORM1IPROC) (GLint location, GLint v0); 319 | typedef void (APIENTRYP PFNGLUNIFORMMATRIX4FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); 320 | typedef void (APIENTRYP PFNGLVERTEXATTRIBPOINTERPROC) (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer); 321 | #ifdef GL_GLEXT_PROTOTYPES 322 | GLAPI void APIENTRY glBlendEquationSeparate (GLenum modeRGB, GLenum modeAlpha); 323 | GLAPI void APIENTRY glAttachShader (GLuint program, GLuint shader); 324 | GLAPI void APIENTRY glCompileShader (GLuint shader); 325 | GLAPI GLuint APIENTRY glCreateProgram (void); 326 | GLAPI GLuint APIENTRY glCreateShader (GLenum type); 327 | GLAPI void APIENTRY glDeleteProgram (GLuint program); 328 | GLAPI void APIENTRY glDeleteShader (GLuint shader); 329 | GLAPI void APIENTRY glDetachShader (GLuint program, GLuint shader); 330 | GLAPI void APIENTRY glDisableVertexAttribArray (GLuint index); 331 | GLAPI void APIENTRY glEnableVertexAttribArray (GLuint index); 332 | GLAPI GLint APIENTRY glGetAttribLocation (GLuint program, const GLchar *name); 333 | GLAPI void APIENTRY glGetProgramiv (GLuint program, GLenum pname, GLint *params); 334 | GLAPI void APIENTRY glGetProgramInfoLog (GLuint program, GLsizei bufSize, GLsizei *length, GLchar *infoLog); 335 | GLAPI void APIENTRY glGetShaderiv (GLuint shader, GLenum pname, GLint *params); 336 | GLAPI void APIENTRY glGetShaderInfoLog (GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog); 337 | GLAPI GLint APIENTRY glGetUniformLocation (GLuint program, const GLchar *name); 338 | GLAPI void APIENTRY glGetVertexAttribiv (GLuint index, GLenum pname, GLint *params); 339 | GLAPI void APIENTRY glGetVertexAttribPointerv (GLuint index, GLenum pname, void **pointer); 340 | GLAPI GLboolean APIENTRY glIsProgram (GLuint program); 341 | GLAPI void APIENTRY glLinkProgram (GLuint program); 342 | GLAPI void APIENTRY glShaderSource (GLuint shader, GLsizei count, const GLchar *const*string, const GLint *length); 343 | GLAPI void APIENTRY glUseProgram (GLuint program); 344 | GLAPI void APIENTRY glUniform1i (GLint location, GLint v0); 345 | GLAPI void APIENTRY glUniformMatrix4fv (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); 346 | GLAPI void APIENTRY glVertexAttribPointer (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer); 347 | #endif 348 | #endif /* GL_VERSION_2_0 */ 349 | #ifndef GL_VERSION_3_0 350 | typedef khronos_uint16_t GLhalf; 351 | #define GL_MAJOR_VERSION 0x821B 352 | #define GL_MINOR_VERSION 0x821C 353 | #define GL_NUM_EXTENSIONS 0x821D 354 | #define GL_FRAMEBUFFER_SRGB 0x8DB9 355 | #define GL_VERTEX_ARRAY_BINDING 0x85B5 356 | typedef void (APIENTRYP PFNGLGETBOOLEANI_VPROC) (GLenum target, GLuint index, GLboolean *data); 357 | typedef void (APIENTRYP PFNGLGETINTEGERI_VPROC) (GLenum target, GLuint index, GLint *data); 358 | typedef const GLubyte *(APIENTRYP PFNGLGETSTRINGIPROC) (GLenum name, GLuint index); 359 | typedef void (APIENTRYP PFNGLBINDVERTEXARRAYPROC) (GLuint array); 360 | typedef void (APIENTRYP PFNGLDELETEVERTEXARRAYSPROC) (GLsizei n, const GLuint *arrays); 361 | typedef void (APIENTRYP PFNGLGENVERTEXARRAYSPROC) (GLsizei n, GLuint *arrays); 362 | #ifdef GL_GLEXT_PROTOTYPES 363 | GLAPI const GLubyte *APIENTRY glGetStringi (GLenum name, GLuint index); 364 | GLAPI void APIENTRY glBindVertexArray (GLuint array); 365 | GLAPI void APIENTRY glDeleteVertexArrays (GLsizei n, const GLuint *arrays); 366 | GLAPI void APIENTRY glGenVertexArrays (GLsizei n, GLuint *arrays); 367 | #endif 368 | #endif /* GL_VERSION_3_0 */ 369 | #ifndef GL_VERSION_3_1 370 | #define GL_VERSION_3_1 1 371 | #define GL_PRIMITIVE_RESTART 0x8F9D 372 | #endif /* GL_VERSION_3_1 */ 373 | #ifndef GL_VERSION_3_2 374 | #define GL_VERSION_3_2 1 375 | typedef struct __GLsync *GLsync; 376 | typedef khronos_uint64_t GLuint64; 377 | typedef khronos_int64_t GLint64; 378 | #define GL_CONTEXT_COMPATIBILITY_PROFILE_BIT 0x00000002 379 | #define GL_CONTEXT_PROFILE_MASK 0x9126 380 | typedef void (APIENTRYP PFNGLDRAWELEMENTSBASEVERTEXPROC) (GLenum mode, GLsizei count, GLenum type, const void *indices, GLint basevertex); 381 | typedef void (APIENTRYP PFNGLGETINTEGER64I_VPROC) (GLenum target, GLuint index, GLint64 *data); 382 | #ifdef GL_GLEXT_PROTOTYPES 383 | GLAPI void APIENTRY glDrawElementsBaseVertex (GLenum mode, GLsizei count, GLenum type, const void *indices, GLint basevertex); 384 | #endif 385 | #endif /* GL_VERSION_3_2 */ 386 | #ifndef GL_VERSION_3_3 387 | #define GL_VERSION_3_3 1 388 | #define GL_SAMPLER_BINDING 0x8919 389 | typedef void (APIENTRYP PFNGLBINDSAMPLERPROC) (GLuint unit, GLuint sampler); 390 | #ifdef GL_GLEXT_PROTOTYPES 391 | GLAPI void APIENTRY glBindSampler (GLuint unit, GLuint sampler); 392 | #endif 393 | #endif /* GL_VERSION_3_3 */ 394 | #ifndef GL_VERSION_4_1 395 | typedef void (APIENTRYP PFNGLGETFLOATI_VPROC) (GLenum target, GLuint index, GLfloat *data); 396 | typedef void (APIENTRYP PFNGLGETDOUBLEI_VPROC) (GLenum target, GLuint index, GLdouble *data); 397 | #endif /* GL_VERSION_4_1 */ 398 | #ifndef GL_VERSION_4_3 399 | typedef void (APIENTRY *GLDEBUGPROC)(GLenum source,GLenum type,GLuint id,GLenum severity,GLsizei length,const GLchar *message,const void *userParam); 400 | #endif /* GL_VERSION_4_3 */ 401 | #ifndef GL_VERSION_4_5 402 | #define GL_CLIP_ORIGIN 0x935C 403 | typedef void (APIENTRYP PFNGLGETTRANSFORMFEEDBACKI_VPROC) (GLuint xfb, GLenum pname, GLuint index, GLint *param); 404 | typedef void (APIENTRYP PFNGLGETTRANSFORMFEEDBACKI64_VPROC) (GLuint xfb, GLenum pname, GLuint index, GLint64 *param); 405 | #endif /* GL_VERSION_4_5 */ 406 | #ifndef GL_ARB_bindless_texture 407 | typedef khronos_uint64_t GLuint64EXT; 408 | #endif /* GL_ARB_bindless_texture */ 409 | #ifndef GL_ARB_cl_event 410 | struct _cl_context; 411 | struct _cl_event; 412 | #endif /* GL_ARB_cl_event */ 413 | #ifndef GL_ARB_clip_control 414 | #define GL_ARB_clip_control 1 415 | #endif /* GL_ARB_clip_control */ 416 | #ifndef GL_ARB_debug_output 417 | typedef void (APIENTRY *GLDEBUGPROCARB)(GLenum source,GLenum type,GLuint id,GLenum severity,GLsizei length,const GLchar *message,const void *userParam); 418 | #endif /* GL_ARB_debug_output */ 419 | #ifndef GL_EXT_EGL_image_storage 420 | typedef void *GLeglImageOES; 421 | #endif /* GL_EXT_EGL_image_storage */ 422 | #ifndef GL_EXT_direct_state_access 423 | typedef void (APIENTRYP PFNGLGETFLOATI_VEXTPROC) (GLenum pname, GLuint index, GLfloat *params); 424 | typedef void (APIENTRYP PFNGLGETDOUBLEI_VEXTPROC) (GLenum pname, GLuint index, GLdouble *params); 425 | typedef void (APIENTRYP PFNGLGETPOINTERI_VEXTPROC) (GLenum pname, GLuint index, void **params); 426 | typedef void (APIENTRYP PFNGLGETVERTEXARRAYINTEGERI_VEXTPROC) (GLuint vaobj, GLuint index, GLenum pname, GLint *param); 427 | typedef void (APIENTRYP PFNGLGETVERTEXARRAYPOINTERI_VEXTPROC) (GLuint vaobj, GLuint index, GLenum pname, void **param); 428 | #endif /* GL_EXT_direct_state_access */ 429 | #ifndef GL_NV_draw_vulkan_image 430 | typedef void (APIENTRY *GLVULKANPROCNV)(void); 431 | #endif /* GL_NV_draw_vulkan_image */ 432 | #ifndef GL_NV_gpu_shader5 433 | typedef khronos_int64_t GLint64EXT; 434 | #endif /* GL_NV_gpu_shader5 */ 435 | #ifndef GL_NV_vertex_buffer_unified_memory 436 | typedef void (APIENTRYP PFNGLGETINTEGERUI64I_VNVPROC) (GLenum value, GLuint index, GLuint64EXT *result); 437 | #endif /* GL_NV_vertex_buffer_unified_memory */ 438 | #ifdef __cplusplus 439 | } 440 | #endif 441 | #endif 442 | 443 | #ifndef GL3W_API 444 | #define GL3W_API 445 | #endif 446 | 447 | #ifndef __gl_h_ 448 | #define __gl_h_ 449 | #endif 450 | 451 | #ifdef __cplusplus 452 | extern "C" { 453 | #endif 454 | 455 | #define GL3W_OK 0 456 | #define GL3W_ERROR_INIT -1 457 | #define GL3W_ERROR_LIBRARY_OPEN -2 458 | #define GL3W_ERROR_OPENGL_VERSION -3 459 | 460 | typedef void (*GL3WglProc)(void); 461 | typedef GL3WglProc (*GL3WGetProcAddressProc)(const char *proc); 462 | 463 | /* gl3w api */ 464 | GL3W_API int imgl3wInit(void); 465 | GL3W_API int imgl3wInit2(GL3WGetProcAddressProc proc); 466 | GL3W_API int imgl3wIsSupported(int major, int minor); 467 | GL3W_API GL3WglProc imgl3wGetProcAddress(const char *proc); 468 | 469 | /* gl3w internal state */ 470 | union GL3WProcs { 471 | GL3WglProc ptr[59]; 472 | struct { 473 | PFNGLACTIVETEXTUREPROC ActiveTexture; 474 | PFNGLATTACHSHADERPROC AttachShader; 475 | PFNGLBINDBUFFERPROC BindBuffer; 476 | PFNGLBINDSAMPLERPROC BindSampler; 477 | PFNGLBINDTEXTUREPROC BindTexture; 478 | PFNGLBINDVERTEXARRAYPROC BindVertexArray; 479 | PFNGLBLENDEQUATIONPROC BlendEquation; 480 | PFNGLBLENDEQUATIONSEPARATEPROC BlendEquationSeparate; 481 | PFNGLBLENDFUNCSEPARATEPROC BlendFuncSeparate; 482 | PFNGLBUFFERDATAPROC BufferData; 483 | PFNGLBUFFERSUBDATAPROC BufferSubData; 484 | PFNGLCLEARPROC Clear; 485 | PFNGLCLEARCOLORPROC ClearColor; 486 | PFNGLCOMPILESHADERPROC CompileShader; 487 | PFNGLCREATEPROGRAMPROC CreateProgram; 488 | PFNGLCREATESHADERPROC CreateShader; 489 | PFNGLDELETEBUFFERSPROC DeleteBuffers; 490 | PFNGLDELETEPROGRAMPROC DeleteProgram; 491 | PFNGLDELETESHADERPROC DeleteShader; 492 | PFNGLDELETETEXTURESPROC DeleteTextures; 493 | PFNGLDELETEVERTEXARRAYSPROC DeleteVertexArrays; 494 | PFNGLDETACHSHADERPROC DetachShader; 495 | PFNGLDISABLEPROC Disable; 496 | PFNGLDISABLEVERTEXATTRIBARRAYPROC DisableVertexAttribArray; 497 | PFNGLDRAWELEMENTSPROC DrawElements; 498 | PFNGLDRAWELEMENTSBASEVERTEXPROC DrawElementsBaseVertex; 499 | PFNGLENABLEPROC Enable; 500 | PFNGLENABLEVERTEXATTRIBARRAYPROC EnableVertexAttribArray; 501 | PFNGLFLUSHPROC Flush; 502 | PFNGLGENBUFFERSPROC GenBuffers; 503 | PFNGLGENTEXTURESPROC GenTextures; 504 | PFNGLGENVERTEXARRAYSPROC GenVertexArrays; 505 | PFNGLGETATTRIBLOCATIONPROC GetAttribLocation; 506 | PFNGLGETERRORPROC GetError; 507 | PFNGLGETINTEGERVPROC GetIntegerv; 508 | PFNGLGETPROGRAMINFOLOGPROC GetProgramInfoLog; 509 | PFNGLGETPROGRAMIVPROC GetProgramiv; 510 | PFNGLGETSHADERINFOLOGPROC GetShaderInfoLog; 511 | PFNGLGETSHADERIVPROC GetShaderiv; 512 | PFNGLGETSTRINGPROC GetString; 513 | PFNGLGETSTRINGIPROC GetStringi; 514 | PFNGLGETUNIFORMLOCATIONPROC GetUniformLocation; 515 | PFNGLGETVERTEXATTRIBPOINTERVPROC GetVertexAttribPointerv; 516 | PFNGLGETVERTEXATTRIBIVPROC GetVertexAttribiv; 517 | PFNGLISENABLEDPROC IsEnabled; 518 | PFNGLISPROGRAMPROC IsProgram; 519 | PFNGLLINKPROGRAMPROC LinkProgram; 520 | PFNGLPIXELSTOREIPROC PixelStorei; 521 | PFNGLPOLYGONMODEPROC PolygonMode; 522 | PFNGLREADPIXELSPROC ReadPixels; 523 | PFNGLSCISSORPROC Scissor; 524 | PFNGLSHADERSOURCEPROC ShaderSource; 525 | PFNGLTEXIMAGE2DPROC TexImage2D; 526 | PFNGLTEXPARAMETERIPROC TexParameteri; 527 | PFNGLUNIFORM1IPROC Uniform1i; 528 | PFNGLUNIFORMMATRIX4FVPROC UniformMatrix4fv; 529 | PFNGLUSEPROGRAMPROC UseProgram; 530 | PFNGLVERTEXATTRIBPOINTERPROC VertexAttribPointer; 531 | PFNGLVIEWPORTPROC Viewport; 532 | } gl; 533 | }; 534 | 535 | GL3W_API extern union GL3WProcs imgl3wProcs; 536 | 537 | /* OpenGL functions */ 538 | #define glActiveTexture imgl3wProcs.gl.ActiveTexture 539 | #define glAttachShader imgl3wProcs.gl.AttachShader 540 | #define glBindBuffer imgl3wProcs.gl.BindBuffer 541 | #define glBindSampler imgl3wProcs.gl.BindSampler 542 | #define glBindTexture imgl3wProcs.gl.BindTexture 543 | #define glBindVertexArray imgl3wProcs.gl.BindVertexArray 544 | #define glBlendEquation imgl3wProcs.gl.BlendEquation 545 | #define glBlendEquationSeparate imgl3wProcs.gl.BlendEquationSeparate 546 | #define glBlendFuncSeparate imgl3wProcs.gl.BlendFuncSeparate 547 | #define glBufferData imgl3wProcs.gl.BufferData 548 | #define glBufferSubData imgl3wProcs.gl.BufferSubData 549 | #define glClear imgl3wProcs.gl.Clear 550 | #define glClearColor imgl3wProcs.gl.ClearColor 551 | #define glCompileShader imgl3wProcs.gl.CompileShader 552 | #define glCreateProgram imgl3wProcs.gl.CreateProgram 553 | #define glCreateShader imgl3wProcs.gl.CreateShader 554 | #define glDeleteBuffers imgl3wProcs.gl.DeleteBuffers 555 | #define glDeleteProgram imgl3wProcs.gl.DeleteProgram 556 | #define glDeleteShader imgl3wProcs.gl.DeleteShader 557 | #define glDeleteTextures imgl3wProcs.gl.DeleteTextures 558 | #define glDeleteVertexArrays imgl3wProcs.gl.DeleteVertexArrays 559 | #define glDetachShader imgl3wProcs.gl.DetachShader 560 | #define glDisable imgl3wProcs.gl.Disable 561 | #define glDisableVertexAttribArray imgl3wProcs.gl.DisableVertexAttribArray 562 | #define glDrawElements imgl3wProcs.gl.DrawElements 563 | #define glDrawElementsBaseVertex imgl3wProcs.gl.DrawElementsBaseVertex 564 | #define glEnable imgl3wProcs.gl.Enable 565 | #define glEnableVertexAttribArray imgl3wProcs.gl.EnableVertexAttribArray 566 | #define glFlush imgl3wProcs.gl.Flush 567 | #define glGenBuffers imgl3wProcs.gl.GenBuffers 568 | #define glGenTextures imgl3wProcs.gl.GenTextures 569 | #define glGenVertexArrays imgl3wProcs.gl.GenVertexArrays 570 | #define glGetAttribLocation imgl3wProcs.gl.GetAttribLocation 571 | #define glGetError imgl3wProcs.gl.GetError 572 | #define glGetIntegerv imgl3wProcs.gl.GetIntegerv 573 | #define glGetProgramInfoLog imgl3wProcs.gl.GetProgramInfoLog 574 | #define glGetProgramiv imgl3wProcs.gl.GetProgramiv 575 | #define glGetShaderInfoLog imgl3wProcs.gl.GetShaderInfoLog 576 | #define glGetShaderiv imgl3wProcs.gl.GetShaderiv 577 | #define glGetString imgl3wProcs.gl.GetString 578 | #define glGetStringi imgl3wProcs.gl.GetStringi 579 | #define glGetUniformLocation imgl3wProcs.gl.GetUniformLocation 580 | #define glGetVertexAttribPointerv imgl3wProcs.gl.GetVertexAttribPointerv 581 | #define glGetVertexAttribiv imgl3wProcs.gl.GetVertexAttribiv 582 | #define glIsEnabled imgl3wProcs.gl.IsEnabled 583 | #define glIsProgram imgl3wProcs.gl.IsProgram 584 | #define glLinkProgram imgl3wProcs.gl.LinkProgram 585 | #define glPixelStorei imgl3wProcs.gl.PixelStorei 586 | #define glPolygonMode imgl3wProcs.gl.PolygonMode 587 | #define glReadPixels imgl3wProcs.gl.ReadPixels 588 | #define glScissor imgl3wProcs.gl.Scissor 589 | #define glShaderSource imgl3wProcs.gl.ShaderSource 590 | #define glTexImage2D imgl3wProcs.gl.TexImage2D 591 | #define glTexParameteri imgl3wProcs.gl.TexParameteri 592 | #define glUniform1i imgl3wProcs.gl.Uniform1i 593 | #define glUniformMatrix4fv imgl3wProcs.gl.UniformMatrix4fv 594 | #define glUseProgram imgl3wProcs.gl.UseProgram 595 | #define glVertexAttribPointer imgl3wProcs.gl.VertexAttribPointer 596 | #define glViewport imgl3wProcs.gl.Viewport 597 | 598 | #ifdef __cplusplus 599 | } 600 | #endif 601 | 602 | #endif 603 | 604 | #ifdef IMGL3W_IMPL 605 | #ifdef __cplusplus 606 | extern "C" { 607 | #endif 608 | 609 | #include 610 | 611 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 612 | 613 | #if defined(_WIN32) 614 | #ifndef WIN32_LEAN_AND_MEAN 615 | #define WIN32_LEAN_AND_MEAN 1 616 | #endif 617 | #include 618 | 619 | static HMODULE libgl; 620 | typedef PROC(__stdcall* GL3WglGetProcAddr)(LPCSTR); 621 | static GL3WglGetProcAddr wgl_get_proc_address; 622 | 623 | static int open_libgl(void) 624 | { 625 | libgl = LoadLibraryA("opengl32.dll"); 626 | if (!libgl) 627 | return GL3W_ERROR_LIBRARY_OPEN; 628 | wgl_get_proc_address = (GL3WglGetProcAddr)GetProcAddress(libgl, "wglGetProcAddress"); 629 | return GL3W_OK; 630 | } 631 | 632 | static void close_libgl(void) { FreeLibrary(libgl); } 633 | static GL3WglProc get_proc(const char *proc) 634 | { 635 | GL3WglProc res; 636 | res = (GL3WglProc)wgl_get_proc_address(proc); 637 | if (!res) 638 | res = (GL3WglProc)GetProcAddress(libgl, proc); 639 | return res; 640 | } 641 | #elif defined(__APPLE__) 642 | #include 643 | 644 | static void *libgl; 645 | static int open_libgl(void) 646 | { 647 | libgl = dlopen("/System/Library/Frameworks/OpenGL.framework/OpenGL", RTLD_LAZY | RTLD_LOCAL); 648 | if (!libgl) 649 | return GL3W_ERROR_LIBRARY_OPEN; 650 | return GL3W_OK; 651 | } 652 | 653 | static void close_libgl(void) { dlclose(libgl); } 654 | 655 | static GL3WglProc get_proc(const char *proc) 656 | { 657 | GL3WglProc res; 658 | *(void **)(&res) = dlsym(libgl, proc); 659 | return res; 660 | } 661 | #else 662 | #include 663 | 664 | static void *libgl; 665 | static GL3WglProc (*glx_get_proc_address)(const GLubyte *); 666 | 667 | static int open_libgl(void) 668 | { 669 | libgl = dlopen("libGL.so.1", RTLD_LAZY | RTLD_LOCAL); 670 | if (!libgl) 671 | return GL3W_ERROR_LIBRARY_OPEN; 672 | *(void **)(&glx_get_proc_address) = dlsym(libgl, "glXGetProcAddressARB"); 673 | return GL3W_OK; 674 | } 675 | 676 | static void close_libgl(void) { dlclose(libgl); } 677 | 678 | static GL3WglProc get_proc(const char *proc) 679 | { 680 | GL3WglProc res; 681 | res = glx_get_proc_address((const GLubyte *)proc); 682 | if (!res) 683 | *(void **)(&res) = dlsym(libgl, proc); 684 | return res; 685 | } 686 | #endif 687 | 688 | static struct { int major, minor; } version; 689 | 690 | static int parse_version(void) 691 | { 692 | if (!glGetIntegerv) 693 | return GL3W_ERROR_INIT; 694 | glGetIntegerv(GL_MAJOR_VERSION, &version.major); 695 | glGetIntegerv(GL_MINOR_VERSION, &version.minor); 696 | if (version.major == 0 && version.minor == 0) 697 | { 698 | // Query GL_VERSION in desktop GL 2.x, the string will start with "." 699 | if (const char* gl_version = (const char*)glGetString(GL_VERSION)) 700 | sscanf(gl_version, "%d.%d", &version.major, &version.minor); 701 | } 702 | if (version.major < 2) 703 | return GL3W_ERROR_OPENGL_VERSION; 704 | return GL3W_OK; 705 | } 706 | 707 | static void load_procs(GL3WGetProcAddressProc proc); 708 | 709 | int imgl3wInit(void) 710 | { 711 | int res = open_libgl(); 712 | if (res) 713 | return res; 714 | atexit(close_libgl); 715 | return imgl3wInit2(get_proc); 716 | } 717 | 718 | int imgl3wInit2(GL3WGetProcAddressProc proc) 719 | { 720 | load_procs(proc); 721 | return parse_version(); 722 | } 723 | 724 | int imgl3wIsSupported(int major, int minor) 725 | { 726 | if (major < 2) 727 | return 0; 728 | if (version.major == major) 729 | return version.minor >= minor; 730 | return version.major >= major; 731 | } 732 | 733 | GL3WglProc imgl3wGetProcAddress(const char *proc) { return get_proc(proc); } 734 | 735 | static const char *proc_names[] = { 736 | "glActiveTexture", 737 | "glAttachShader", 738 | "glBindBuffer", 739 | "glBindSampler", 740 | "glBindTexture", 741 | "glBindVertexArray", 742 | "glBlendEquation", 743 | "glBlendEquationSeparate", 744 | "glBlendFuncSeparate", 745 | "glBufferData", 746 | "glBufferSubData", 747 | "glClear", 748 | "glClearColor", 749 | "glCompileShader", 750 | "glCreateProgram", 751 | "glCreateShader", 752 | "glDeleteBuffers", 753 | "glDeleteProgram", 754 | "glDeleteShader", 755 | "glDeleteTextures", 756 | "glDeleteVertexArrays", 757 | "glDetachShader", 758 | "glDisable", 759 | "glDisableVertexAttribArray", 760 | "glDrawElements", 761 | "glDrawElementsBaseVertex", 762 | "glEnable", 763 | "glEnableVertexAttribArray", 764 | "glFlush", 765 | "glGenBuffers", 766 | "glGenTextures", 767 | "glGenVertexArrays", 768 | "glGetAttribLocation", 769 | "glGetError", 770 | "glGetIntegerv", 771 | "glGetProgramInfoLog", 772 | "glGetProgramiv", 773 | "glGetShaderInfoLog", 774 | "glGetShaderiv", 775 | "glGetString", 776 | "glGetStringi", 777 | "glGetUniformLocation", 778 | "glGetVertexAttribPointerv", 779 | "glGetVertexAttribiv", 780 | "glIsEnabled", 781 | "glIsProgram", 782 | "glLinkProgram", 783 | "glPixelStorei", 784 | "glPolygonMode", 785 | "glReadPixels", 786 | "glScissor", 787 | "glShaderSource", 788 | "glTexImage2D", 789 | "glTexParameteri", 790 | "glUniform1i", 791 | "glUniformMatrix4fv", 792 | "glUseProgram", 793 | "glVertexAttribPointer", 794 | "glViewport", 795 | }; 796 | 797 | GL3W_API union GL3WProcs imgl3wProcs; 798 | 799 | static void load_procs(GL3WGetProcAddressProc proc) 800 | { 801 | size_t i; 802 | for (i = 0; i < ARRAY_SIZE(proc_names); i++) 803 | imgl3wProcs.ptr[i] = proc(proc_names[i]); 804 | } 805 | 806 | #ifdef __cplusplus 807 | } 808 | #endif 809 | #endif 810 | -------------------------------------------------------------------------------- /imgui/imstb_rectpack.h: -------------------------------------------------------------------------------- 1 | // [DEAR IMGUI] 2 | // This is a slightly modified version of stb_rect_pack.h 1.01. 3 | // Grep for [DEAR IMGUI] to find the changes. 4 | // 5 | // stb_rect_pack.h - v1.01 - public domain - rectangle packing 6 | // Sean Barrett 2014 7 | // 8 | // Useful for e.g. packing rectangular textures into an atlas. 9 | // Does not do rotation. 10 | // 11 | // Before #including, 12 | // 13 | // #define STB_RECT_PACK_IMPLEMENTATION 14 | // 15 | // in the file that you want to have the implementation. 16 | // 17 | // Not necessarily the awesomest packing method, but better than 18 | // the totally naive one in stb_truetype (which is primarily what 19 | // this is meant to replace). 20 | // 21 | // Has only had a few tests run, may have issues. 22 | // 23 | // More docs to come. 24 | // 25 | // No memory allocations; uses qsort() and assert() from stdlib. 26 | // Can override those by defining STBRP_SORT and STBRP_ASSERT. 27 | // 28 | // This library currently uses the Skyline Bottom-Left algorithm. 29 | // 30 | // Please note: better rectangle packers are welcome! Please 31 | // implement them to the same API, but with a different init 32 | // function. 33 | // 34 | // Credits 35 | // 36 | // Library 37 | // Sean Barrett 38 | // Minor features 39 | // Martins Mozeiko 40 | // github:IntellectualKitty 41 | // 42 | // Bugfixes / warning fixes 43 | // Jeremy Jaussaud 44 | // Fabian Giesen 45 | // 46 | // Version history: 47 | // 48 | // 1.01 (2021-07-11) always use large rect mode, expose STBRP__MAXVAL in public section 49 | // 1.00 (2019-02-25) avoid small space waste; gracefully fail too-wide rectangles 50 | // 0.99 (2019-02-07) warning fixes 51 | // 0.11 (2017-03-03) return packing success/fail result 52 | // 0.10 (2016-10-25) remove cast-away-const to avoid warnings 53 | // 0.09 (2016-08-27) fix compiler warnings 54 | // 0.08 (2015-09-13) really fix bug with empty rects (w=0 or h=0) 55 | // 0.07 (2015-09-13) fix bug with empty rects (w=0 or h=0) 56 | // 0.06 (2015-04-15) added STBRP_SORT to allow replacing qsort 57 | // 0.05: added STBRP_ASSERT to allow replacing assert 58 | // 0.04: fixed minor bug in STBRP_LARGE_RECTS support 59 | // 0.01: initial release 60 | // 61 | // LICENSE 62 | // 63 | // See end of file for license information. 64 | 65 | ////////////////////////////////////////////////////////////////////////////// 66 | // 67 | // INCLUDE SECTION 68 | // 69 | 70 | #ifndef STB_INCLUDE_STB_RECT_PACK_H 71 | #define STB_INCLUDE_STB_RECT_PACK_H 72 | 73 | #define STB_RECT_PACK_VERSION 1 74 | 75 | #ifdef STBRP_STATIC 76 | #define STBRP_DEF static 77 | #else 78 | #define STBRP_DEF extern 79 | #endif 80 | 81 | #ifdef __cplusplus 82 | extern "C" { 83 | #endif 84 | 85 | typedef struct stbrp_context stbrp_context; 86 | typedef struct stbrp_node stbrp_node; 87 | typedef struct stbrp_rect stbrp_rect; 88 | 89 | typedef int stbrp_coord; 90 | 91 | #define STBRP__MAXVAL 0x7fffffff 92 | // Mostly for internal use, but this is the maximum supported coordinate value. 93 | 94 | STBRP_DEF int stbrp_pack_rects (stbrp_context *context, stbrp_rect *rects, int num_rects); 95 | // Assign packed locations to rectangles. The rectangles are of type 96 | // 'stbrp_rect' defined below, stored in the array 'rects', and there 97 | // are 'num_rects' many of them. 98 | // 99 | // Rectangles which are successfully packed have the 'was_packed' flag 100 | // set to a non-zero value and 'x' and 'y' store the minimum location 101 | // on each axis (i.e. bottom-left in cartesian coordinates, top-left 102 | // if you imagine y increasing downwards). Rectangles which do not fit 103 | // have the 'was_packed' flag set to 0. 104 | // 105 | // You should not try to access the 'rects' array from another thread 106 | // while this function is running, as the function temporarily reorders 107 | // the array while it executes. 108 | // 109 | // To pack into another rectangle, you need to call stbrp_init_target 110 | // again. To continue packing into the same rectangle, you can call 111 | // this function again. Calling this multiple times with multiple rect 112 | // arrays will probably produce worse packing results than calling it 113 | // a single time with the full rectangle array, but the option is 114 | // available. 115 | // 116 | // The function returns 1 if all of the rectangles were successfully 117 | // packed and 0 otherwise. 118 | 119 | struct stbrp_rect 120 | { 121 | // reserved for your use: 122 | int id; 123 | 124 | // input: 125 | stbrp_coord w, h; 126 | 127 | // output: 128 | stbrp_coord x, y; 129 | int was_packed; // non-zero if valid packing 130 | 131 | }; // 16 bytes, nominally 132 | 133 | 134 | STBRP_DEF void stbrp_init_target (stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes); 135 | // Initialize a rectangle packer to: 136 | // pack a rectangle that is 'width' by 'height' in dimensions 137 | // using temporary storage provided by the array 'nodes', which is 'num_nodes' long 138 | // 139 | // You must call this function every time you start packing into a new target. 140 | // 141 | // There is no "shutdown" function. The 'nodes' memory must stay valid for 142 | // the following stbrp_pack_rects() call (or calls), but can be freed after 143 | // the call (or calls) finish. 144 | // 145 | // Note: to guarantee best results, either: 146 | // 1. make sure 'num_nodes' >= 'width' 147 | // or 2. call stbrp_allow_out_of_mem() defined below with 'allow_out_of_mem = 1' 148 | // 149 | // If you don't do either of the above things, widths will be quantized to multiples 150 | // of small integers to guarantee the algorithm doesn't run out of temporary storage. 151 | // 152 | // If you do #2, then the non-quantized algorithm will be used, but the algorithm 153 | // may run out of temporary storage and be unable to pack some rectangles. 154 | 155 | STBRP_DEF void stbrp_setup_allow_out_of_mem (stbrp_context *context, int allow_out_of_mem); 156 | // Optionally call this function after init but before doing any packing to 157 | // change the handling of the out-of-temp-memory scenario, described above. 158 | // If you call init again, this will be reset to the default (false). 159 | 160 | 161 | STBRP_DEF void stbrp_setup_heuristic (stbrp_context *context, int heuristic); 162 | // Optionally select which packing heuristic the library should use. Different 163 | // heuristics will produce better/worse results for different data sets. 164 | // If you call init again, this will be reset to the default. 165 | 166 | enum 167 | { 168 | STBRP_HEURISTIC_Skyline_default=0, 169 | STBRP_HEURISTIC_Skyline_BL_sortHeight = STBRP_HEURISTIC_Skyline_default, 170 | STBRP_HEURISTIC_Skyline_BF_sortHeight 171 | }; 172 | 173 | 174 | ////////////////////////////////////////////////////////////////////////////// 175 | // 176 | // the details of the following structures don't matter to you, but they must 177 | // be visible so you can handle the memory allocations for them 178 | 179 | struct stbrp_node 180 | { 181 | stbrp_coord x,y; 182 | stbrp_node *next; 183 | }; 184 | 185 | struct stbrp_context 186 | { 187 | int width; 188 | int height; 189 | int align; 190 | int init_mode; 191 | int heuristic; 192 | int num_nodes; 193 | stbrp_node *active_head; 194 | stbrp_node *free_head; 195 | stbrp_node extra[2]; // we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2' 196 | }; 197 | 198 | #ifdef __cplusplus 199 | } 200 | #endif 201 | 202 | #endif 203 | 204 | ////////////////////////////////////////////////////////////////////////////// 205 | // 206 | // IMPLEMENTATION SECTION 207 | // 208 | 209 | #ifdef STB_RECT_PACK_IMPLEMENTATION 210 | #ifndef STBRP_SORT 211 | #include 212 | #define STBRP_SORT qsort 213 | #endif 214 | 215 | #ifndef STBRP_ASSERT 216 | #include 217 | #define STBRP_ASSERT assert 218 | #endif 219 | 220 | #ifdef _MSC_VER 221 | #define STBRP__NOTUSED(v) (void)(v) 222 | #define STBRP__CDECL __cdecl 223 | #else 224 | #define STBRP__NOTUSED(v) (void)sizeof(v) 225 | #define STBRP__CDECL 226 | #endif 227 | 228 | enum 229 | { 230 | STBRP__INIT_skyline = 1 231 | }; 232 | 233 | STBRP_DEF void stbrp_setup_heuristic(stbrp_context *context, int heuristic) 234 | { 235 | switch (context->init_mode) { 236 | case STBRP__INIT_skyline: 237 | STBRP_ASSERT(heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight || heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight); 238 | context->heuristic = heuristic; 239 | break; 240 | default: 241 | STBRP_ASSERT(0); 242 | } 243 | } 244 | 245 | STBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context *context, int allow_out_of_mem) 246 | { 247 | if (allow_out_of_mem) 248 | // if it's ok to run out of memory, then don't bother aligning them; 249 | // this gives better packing, but may fail due to OOM (even though 250 | // the rectangles easily fit). @TODO a smarter approach would be to only 251 | // quantize once we've hit OOM, then we could get rid of this parameter. 252 | context->align = 1; 253 | else { 254 | // if it's not ok to run out of memory, then quantize the widths 255 | // so that num_nodes is always enough nodes. 256 | // 257 | // I.e. num_nodes * align >= width 258 | // align >= width / num_nodes 259 | // align = ceil(width/num_nodes) 260 | 261 | context->align = (context->width + context->num_nodes-1) / context->num_nodes; 262 | } 263 | } 264 | 265 | STBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes) 266 | { 267 | int i; 268 | 269 | for (i=0; i < num_nodes-1; ++i) 270 | nodes[i].next = &nodes[i+1]; 271 | nodes[i].next = NULL; 272 | context->init_mode = STBRP__INIT_skyline; 273 | context->heuristic = STBRP_HEURISTIC_Skyline_default; 274 | context->free_head = &nodes[0]; 275 | context->active_head = &context->extra[0]; 276 | context->width = width; 277 | context->height = height; 278 | context->num_nodes = num_nodes; 279 | stbrp_setup_allow_out_of_mem(context, 0); 280 | 281 | // node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly) 282 | context->extra[0].x = 0; 283 | context->extra[0].y = 0; 284 | context->extra[0].next = &context->extra[1]; 285 | context->extra[1].x = (stbrp_coord) width; 286 | context->extra[1].y = (1<<30); 287 | context->extra[1].next = NULL; 288 | } 289 | 290 | // find minimum y position if it starts at x1 291 | static int stbrp__skyline_find_min_y(stbrp_context *c, stbrp_node *first, int x0, int width, int *pwaste) 292 | { 293 | stbrp_node *node = first; 294 | int x1 = x0 + width; 295 | int min_y, visited_width, waste_area; 296 | 297 | STBRP__NOTUSED(c); 298 | 299 | STBRP_ASSERT(first->x <= x0); 300 | 301 | #if 0 302 | // skip in case we're past the node 303 | while (node->next->x <= x0) 304 | ++node; 305 | #else 306 | STBRP_ASSERT(node->next->x > x0); // we ended up handling this in the caller for efficiency 307 | #endif 308 | 309 | STBRP_ASSERT(node->x <= x0); 310 | 311 | min_y = 0; 312 | waste_area = 0; 313 | visited_width = 0; 314 | while (node->x < x1) { 315 | if (node->y > min_y) { 316 | // raise min_y higher. 317 | // we've accounted for all waste up to min_y, 318 | // but we'll now add more waste for everything we've visted 319 | waste_area += visited_width * (node->y - min_y); 320 | min_y = node->y; 321 | // the first time through, visited_width might be reduced 322 | if (node->x < x0) 323 | visited_width += node->next->x - x0; 324 | else 325 | visited_width += node->next->x - node->x; 326 | } else { 327 | // add waste area 328 | int under_width = node->next->x - node->x; 329 | if (under_width + visited_width > width) 330 | under_width = width - visited_width; 331 | waste_area += under_width * (min_y - node->y); 332 | visited_width += under_width; 333 | } 334 | node = node->next; 335 | } 336 | 337 | *pwaste = waste_area; 338 | return min_y; 339 | } 340 | 341 | typedef struct 342 | { 343 | int x,y; 344 | stbrp_node **prev_link; 345 | } stbrp__findresult; 346 | 347 | static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int width, int height) 348 | { 349 | int best_waste = (1<<30), best_x, best_y = (1 << 30); 350 | stbrp__findresult fr; 351 | stbrp_node **prev, *node, *tail, **best = NULL; 352 | 353 | // align to multiple of c->align 354 | width = (width + c->align - 1); 355 | width -= width % c->align; 356 | STBRP_ASSERT(width % c->align == 0); 357 | 358 | // if it can't possibly fit, bail immediately 359 | if (width > c->width || height > c->height) { 360 | fr.prev_link = NULL; 361 | fr.x = fr.y = 0; 362 | return fr; 363 | } 364 | 365 | node = c->active_head; 366 | prev = &c->active_head; 367 | while (node->x + width <= c->width) { 368 | int y,waste; 369 | y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste); 370 | if (c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) { // actually just want to test BL 371 | // bottom left 372 | if (y < best_y) { 373 | best_y = y; 374 | best = prev; 375 | } 376 | } else { 377 | // best-fit 378 | if (y + height <= c->height) { 379 | // can only use it if it first vertically 380 | if (y < best_y || (y == best_y && waste < best_waste)) { 381 | best_y = y; 382 | best_waste = waste; 383 | best = prev; 384 | } 385 | } 386 | } 387 | prev = &node->next; 388 | node = node->next; 389 | } 390 | 391 | best_x = (best == NULL) ? 0 : (*best)->x; 392 | 393 | // if doing best-fit (BF), we also have to try aligning right edge to each node position 394 | // 395 | // e.g, if fitting 396 | // 397 | // ____________________ 398 | // |____________________| 399 | // 400 | // into 401 | // 402 | // | | 403 | // | ____________| 404 | // |____________| 405 | // 406 | // then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned 407 | // 408 | // This makes BF take about 2x the time 409 | 410 | if (c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) { 411 | tail = c->active_head; 412 | node = c->active_head; 413 | prev = &c->active_head; 414 | // find first node that's admissible 415 | while (tail->x < width) 416 | tail = tail->next; 417 | while (tail) { 418 | int xpos = tail->x - width; 419 | int y,waste; 420 | STBRP_ASSERT(xpos >= 0); 421 | // find the left position that matches this 422 | while (node->next->x <= xpos) { 423 | prev = &node->next; 424 | node = node->next; 425 | } 426 | STBRP_ASSERT(node->next->x > xpos && node->x <= xpos); 427 | y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste); 428 | if (y + height <= c->height) { 429 | if (y <= best_y) { 430 | if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) { 431 | best_x = xpos; 432 | //STBRP_ASSERT(y <= best_y); [DEAR IMGUI] 433 | best_y = y; 434 | best_waste = waste; 435 | best = prev; 436 | } 437 | } 438 | } 439 | tail = tail->next; 440 | } 441 | } 442 | 443 | fr.prev_link = best; 444 | fr.x = best_x; 445 | fr.y = best_y; 446 | return fr; 447 | } 448 | 449 | static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context *context, int width, int height) 450 | { 451 | // find best position according to heuristic 452 | stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height); 453 | stbrp_node *node, *cur; 454 | 455 | // bail if: 456 | // 1. it failed 457 | // 2. the best node doesn't fit (we don't always check this) 458 | // 3. we're out of memory 459 | if (res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) { 460 | res.prev_link = NULL; 461 | return res; 462 | } 463 | 464 | // on success, create new node 465 | node = context->free_head; 466 | node->x = (stbrp_coord) res.x; 467 | node->y = (stbrp_coord) (res.y + height); 468 | 469 | context->free_head = node->next; 470 | 471 | // insert the new node into the right starting point, and 472 | // let 'cur' point to the remaining nodes needing to be 473 | // stiched back in 474 | 475 | cur = *res.prev_link; 476 | if (cur->x < res.x) { 477 | // preserve the existing one, so start testing with the next one 478 | stbrp_node *next = cur->next; 479 | cur->next = node; 480 | cur = next; 481 | } else { 482 | *res.prev_link = node; 483 | } 484 | 485 | // from here, traverse cur and free the nodes, until we get to one 486 | // that shouldn't be freed 487 | while (cur->next && cur->next->x <= res.x + width) { 488 | stbrp_node *next = cur->next; 489 | // move the current node to the free list 490 | cur->next = context->free_head; 491 | context->free_head = cur; 492 | cur = next; 493 | } 494 | 495 | // stitch the list back in 496 | node->next = cur; 497 | 498 | if (cur->x < res.x + width) 499 | cur->x = (stbrp_coord) (res.x + width); 500 | 501 | #ifdef _DEBUG 502 | cur = context->active_head; 503 | while (cur->x < context->width) { 504 | STBRP_ASSERT(cur->x < cur->next->x); 505 | cur = cur->next; 506 | } 507 | STBRP_ASSERT(cur->next == NULL); 508 | 509 | { 510 | int count=0; 511 | cur = context->active_head; 512 | while (cur) { 513 | cur = cur->next; 514 | ++count; 515 | } 516 | cur = context->free_head; 517 | while (cur) { 518 | cur = cur->next; 519 | ++count; 520 | } 521 | STBRP_ASSERT(count == context->num_nodes+2); 522 | } 523 | #endif 524 | 525 | return res; 526 | } 527 | 528 | static int STBRP__CDECL rect_height_compare(const void *a, const void *b) 529 | { 530 | const stbrp_rect *p = (const stbrp_rect *) a; 531 | const stbrp_rect *q = (const stbrp_rect *) b; 532 | if (p->h > q->h) 533 | return -1; 534 | if (p->h < q->h) 535 | return 1; 536 | return (p->w > q->w) ? -1 : (p->w < q->w); 537 | } 538 | 539 | static int STBRP__CDECL rect_original_order(const void *a, const void *b) 540 | { 541 | const stbrp_rect *p = (const stbrp_rect *) a; 542 | const stbrp_rect *q = (const stbrp_rect *) b; 543 | return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed); 544 | } 545 | 546 | STBRP_DEF int stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects) 547 | { 548 | int i, all_rects_packed = 1; 549 | 550 | // we use the 'was_packed' field internally to allow sorting/unsorting 551 | for (i=0; i < num_rects; ++i) { 552 | rects[i].was_packed = i; 553 | } 554 | 555 | // sort according to heuristic 556 | STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_height_compare); 557 | 558 | for (i=0; i < num_rects; ++i) { 559 | if (rects[i].w == 0 || rects[i].h == 0) { 560 | rects[i].x = rects[i].y = 0; // empty rect needs no space 561 | } else { 562 | stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h); 563 | if (fr.prev_link) { 564 | rects[i].x = (stbrp_coord) fr.x; 565 | rects[i].y = (stbrp_coord) fr.y; 566 | } else { 567 | rects[i].x = rects[i].y = STBRP__MAXVAL; 568 | } 569 | } 570 | } 571 | 572 | // unsort 573 | STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_original_order); 574 | 575 | // set was_packed flags and all_rects_packed status 576 | for (i=0; i < num_rects; ++i) { 577 | rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL); 578 | if (!rects[i].was_packed) 579 | all_rects_packed = 0; 580 | } 581 | 582 | // return the all_rects_packed status 583 | return all_rects_packed; 584 | } 585 | #endif 586 | 587 | /* 588 | ------------------------------------------------------------------------------ 589 | This software is available under 2 licenses -- choose whichever you prefer. 590 | ------------------------------------------------------------------------------ 591 | ALTERNATIVE A - MIT License 592 | Copyright (c) 2017 Sean Barrett 593 | Permission is hereby granted, free of charge, to any person obtaining a copy of 594 | this software and associated documentation files (the "Software"), to deal in 595 | the Software without restriction, including without limitation the rights to 596 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 597 | of the Software, and to permit persons to whom the Software is furnished to do 598 | so, subject to the following conditions: 599 | The above copyright notice and this permission notice shall be included in all 600 | copies or substantial portions of the Software. 601 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 602 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 603 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 604 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 605 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 606 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 607 | SOFTWARE. 608 | ------------------------------------------------------------------------------ 609 | ALTERNATIVE B - Public Domain (www.unlicense.org) 610 | This is free and unencumbered software released into the public domain. 611 | Anyone is free to copy, modify, publish, use, compile, sell, or distribute this 612 | software, either in source code form or as a compiled binary, for any purpose, 613 | commercial or non-commercial, and by any means. 614 | In jurisdictions that recognize copyright laws, the author or authors of this 615 | software dedicate any and all copyright interest in the software to the public 616 | domain. We make this dedication for the benefit of the public at large and to 617 | the detriment of our heirs and successors. We intend this dedication to be an 618 | overt act of relinquishment in perpetuity of all present and future rights to 619 | this software under copyright law. 620 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 621 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 622 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 623 | AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 624 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 625 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 626 | ------------------------------------------------------------------------------ 627 | */ 628 | -------------------------------------------------------------------------------- /imgui/imstb_textedit.h: -------------------------------------------------------------------------------- 1 | // [DEAR IMGUI] 2 | // This is a slightly modified version of stb_textedit.h 1.14. 3 | // Those changes would need to be pushed into nothings/stb: 4 | // - Fix in stb_textedit_discard_redo (see https://github.com/nothings/stb/issues/321) 5 | // - Fix in stb_textedit_find_charpos to handle last line (see https://github.com/ocornut/imgui/issues/6000 + #6783) 6 | // Grep for [DEAR IMGUI] to find the changes. 7 | 8 | // stb_textedit.h - v1.14 - public domain - Sean Barrett 9 | // Development of this library was sponsored by RAD Game Tools 10 | // 11 | // This C header file implements the guts of a multi-line text-editing 12 | // widget; you implement display, word-wrapping, and low-level string 13 | // insertion/deletion, and stb_textedit will map user inputs into 14 | // insertions & deletions, plus updates to the cursor position, 15 | // selection state, and undo state. 16 | // 17 | // It is intended for use in games and other systems that need to build 18 | // their own custom widgets and which do not have heavy text-editing 19 | // requirements (this library is not recommended for use for editing large 20 | // texts, as its performance does not scale and it has limited undo). 21 | // 22 | // Non-trivial behaviors are modelled after Windows text controls. 23 | // 24 | // 25 | // LICENSE 26 | // 27 | // See end of file for license information. 28 | // 29 | // 30 | // DEPENDENCIES 31 | // 32 | // Uses the C runtime function 'memmove', which you can override 33 | // by defining STB_TEXTEDIT_memmove before the implementation. 34 | // Uses no other functions. Performs no runtime allocations. 35 | // 36 | // 37 | // VERSION HISTORY 38 | // 39 | // 1.14 (2021-07-11) page up/down, various fixes 40 | // 1.13 (2019-02-07) fix bug in undo size management 41 | // 1.12 (2018-01-29) user can change STB_TEXTEDIT_KEYTYPE, fix redo to avoid crash 42 | // 1.11 (2017-03-03) fix HOME on last line, dragging off single-line textfield 43 | // 1.10 (2016-10-25) supress warnings about casting away const with -Wcast-qual 44 | // 1.9 (2016-08-27) customizable move-by-word 45 | // 1.8 (2016-04-02) better keyboard handling when mouse button is down 46 | // 1.7 (2015-09-13) change y range handling in case baseline is non-0 47 | // 1.6 (2015-04-15) allow STB_TEXTEDIT_memmove 48 | // 1.5 (2014-09-10) add support for secondary keys for OS X 49 | // 1.4 (2014-08-17) fix signed/unsigned warnings 50 | // 1.3 (2014-06-19) fix mouse clicking to round to nearest char boundary 51 | // 1.2 (2014-05-27) fix some RAD types that had crept into the new code 52 | // 1.1 (2013-12-15) move-by-word (requires STB_TEXTEDIT_IS_SPACE ) 53 | // 1.0 (2012-07-26) improve documentation, initial public release 54 | // 0.3 (2012-02-24) bugfixes, single-line mode; insert mode 55 | // 0.2 (2011-11-28) fixes to undo/redo 56 | // 0.1 (2010-07-08) initial version 57 | // 58 | // ADDITIONAL CONTRIBUTORS 59 | // 60 | // Ulf Winklemann: move-by-word in 1.1 61 | // Fabian Giesen: secondary key inputs in 1.5 62 | // Martins Mozeiko: STB_TEXTEDIT_memmove in 1.6 63 | // Louis Schnellbach: page up/down in 1.14 64 | // 65 | // Bugfixes: 66 | // Scott Graham 67 | // Daniel Keller 68 | // Omar Cornut 69 | // Dan Thompson 70 | // 71 | // USAGE 72 | // 73 | // This file behaves differently depending on what symbols you define 74 | // before including it. 75 | // 76 | // 77 | // Header-file mode: 78 | // 79 | // If you do not define STB_TEXTEDIT_IMPLEMENTATION before including this, 80 | // it will operate in "header file" mode. In this mode, it declares a 81 | // single public symbol, STB_TexteditState, which encapsulates the current 82 | // state of a text widget (except for the string, which you will store 83 | // separately). 84 | // 85 | // To compile in this mode, you must define STB_TEXTEDIT_CHARTYPE to a 86 | // primitive type that defines a single character (e.g. char, wchar_t, etc). 87 | // 88 | // To save space or increase undo-ability, you can optionally define the 89 | // following things that are used by the undo system: 90 | // 91 | // STB_TEXTEDIT_POSITIONTYPE small int type encoding a valid cursor position 92 | // STB_TEXTEDIT_UNDOSTATECOUNT the number of undo states to allow 93 | // STB_TEXTEDIT_UNDOCHARCOUNT the number of characters to store in the undo buffer 94 | // 95 | // If you don't define these, they are set to permissive types and 96 | // moderate sizes. The undo system does no memory allocations, so 97 | // it grows STB_TexteditState by the worst-case storage which is (in bytes): 98 | // 99 | // [4 + 3 * sizeof(STB_TEXTEDIT_POSITIONTYPE)] * STB_TEXTEDIT_UNDOSTATECOUNT 100 | // + sizeof(STB_TEXTEDIT_CHARTYPE) * STB_TEXTEDIT_UNDOCHARCOUNT 101 | // 102 | // 103 | // Implementation mode: 104 | // 105 | // If you define STB_TEXTEDIT_IMPLEMENTATION before including this, it 106 | // will compile the implementation of the text edit widget, depending 107 | // on a large number of symbols which must be defined before the include. 108 | // 109 | // The implementation is defined only as static functions. You will then 110 | // need to provide your own APIs in the same file which will access the 111 | // static functions. 112 | // 113 | // The basic concept is that you provide a "string" object which 114 | // behaves like an array of characters. stb_textedit uses indices to 115 | // refer to positions in the string, implicitly representing positions 116 | // in the displayed textedit. This is true for both plain text and 117 | // rich text; even with rich text stb_truetype interacts with your 118 | // code as if there was an array of all the displayed characters. 119 | // 120 | // Symbols that must be the same in header-file and implementation mode: 121 | // 122 | // STB_TEXTEDIT_CHARTYPE the character type 123 | // STB_TEXTEDIT_POSITIONTYPE small type that is a valid cursor position 124 | // STB_TEXTEDIT_UNDOSTATECOUNT the number of undo states to allow 125 | // STB_TEXTEDIT_UNDOCHARCOUNT the number of characters to store in the undo buffer 126 | // 127 | // Symbols you must define for implementation mode: 128 | // 129 | // STB_TEXTEDIT_STRING the type of object representing a string being edited, 130 | // typically this is a wrapper object with other data you need 131 | // 132 | // STB_TEXTEDIT_STRINGLEN(obj) the length of the string (ideally O(1)) 133 | // STB_TEXTEDIT_LAYOUTROW(&r,obj,n) returns the results of laying out a line of characters 134 | // starting from character #n (see discussion below) 135 | // STB_TEXTEDIT_GETWIDTH(obj,n,i) returns the pixel delta from the xpos of the i'th character 136 | // to the xpos of the i+1'th char for a line of characters 137 | // starting at character #n (i.e. accounts for kerning 138 | // with previous char) 139 | // STB_TEXTEDIT_KEYTOTEXT(k) maps a keyboard input to an insertable character 140 | // (return type is int, -1 means not valid to insert) 141 | // STB_TEXTEDIT_GETCHAR(obj,i) returns the i'th character of obj, 0-based 142 | // STB_TEXTEDIT_NEWLINE the character returned by _GETCHAR() we recognize 143 | // as manually wordwrapping for end-of-line positioning 144 | // 145 | // STB_TEXTEDIT_DELETECHARS(obj,i,n) delete n characters starting at i 146 | // STB_TEXTEDIT_INSERTCHARS(obj,i,c*,n) insert n characters at i (pointed to by STB_TEXTEDIT_CHARTYPE*) 147 | // 148 | // STB_TEXTEDIT_K_SHIFT a power of two that is or'd in to a keyboard input to represent the shift key 149 | // 150 | // STB_TEXTEDIT_K_LEFT keyboard input to move cursor left 151 | // STB_TEXTEDIT_K_RIGHT keyboard input to move cursor right 152 | // STB_TEXTEDIT_K_UP keyboard input to move cursor up 153 | // STB_TEXTEDIT_K_DOWN keyboard input to move cursor down 154 | // STB_TEXTEDIT_K_PGUP keyboard input to move cursor up a page 155 | // STB_TEXTEDIT_K_PGDOWN keyboard input to move cursor down a page 156 | // STB_TEXTEDIT_K_LINESTART keyboard input to move cursor to start of line // e.g. HOME 157 | // STB_TEXTEDIT_K_LINEEND keyboard input to move cursor to end of line // e.g. END 158 | // STB_TEXTEDIT_K_TEXTSTART keyboard input to move cursor to start of text // e.g. ctrl-HOME 159 | // STB_TEXTEDIT_K_TEXTEND keyboard input to move cursor to end of text // e.g. ctrl-END 160 | // STB_TEXTEDIT_K_DELETE keyboard input to delete selection or character under cursor 161 | // STB_TEXTEDIT_K_BACKSPACE keyboard input to delete selection or character left of cursor 162 | // STB_TEXTEDIT_K_UNDO keyboard input to perform undo 163 | // STB_TEXTEDIT_K_REDO keyboard input to perform redo 164 | // 165 | // Optional: 166 | // STB_TEXTEDIT_K_INSERT keyboard input to toggle insert mode 167 | // STB_TEXTEDIT_IS_SPACE(ch) true if character is whitespace (e.g. 'isspace'), 168 | // required for default WORDLEFT/WORDRIGHT handlers 169 | // STB_TEXTEDIT_MOVEWORDLEFT(obj,i) custom handler for WORDLEFT, returns index to move cursor to 170 | // STB_TEXTEDIT_MOVEWORDRIGHT(obj,i) custom handler for WORDRIGHT, returns index to move cursor to 171 | // STB_TEXTEDIT_K_WORDLEFT keyboard input to move cursor left one word // e.g. ctrl-LEFT 172 | // STB_TEXTEDIT_K_WORDRIGHT keyboard input to move cursor right one word // e.g. ctrl-RIGHT 173 | // STB_TEXTEDIT_K_LINESTART2 secondary keyboard input to move cursor to start of line 174 | // STB_TEXTEDIT_K_LINEEND2 secondary keyboard input to move cursor to end of line 175 | // STB_TEXTEDIT_K_TEXTSTART2 secondary keyboard input to move cursor to start of text 176 | // STB_TEXTEDIT_K_TEXTEND2 secondary keyboard input to move cursor to end of text 177 | // 178 | // Keyboard input must be encoded as a single integer value; e.g. a character code 179 | // and some bitflags that represent shift states. to simplify the interface, SHIFT must 180 | // be a bitflag, so we can test the shifted state of cursor movements to allow selection, 181 | // i.e. (STB_TEXTEDIT_K_RIGHT|STB_TEXTEDIT_K_SHIFT) should be shifted right-arrow. 182 | // 183 | // You can encode other things, such as CONTROL or ALT, in additional bits, and 184 | // then test for their presence in e.g. STB_TEXTEDIT_K_WORDLEFT. For example, 185 | // my Windows implementations add an additional CONTROL bit, and an additional KEYDOWN 186 | // bit. Then all of the STB_TEXTEDIT_K_ values bitwise-or in the KEYDOWN bit, 187 | // and I pass both WM_KEYDOWN and WM_CHAR events to the "key" function in the 188 | // API below. The control keys will only match WM_KEYDOWN events because of the 189 | // keydown bit I add, and STB_TEXTEDIT_KEYTOTEXT only tests for the KEYDOWN 190 | // bit so it only decodes WM_CHAR events. 191 | // 192 | // STB_TEXTEDIT_LAYOUTROW returns information about the shape of one displayed 193 | // row of characters assuming they start on the i'th character--the width and 194 | // the height and the number of characters consumed. This allows this library 195 | // to traverse the entire layout incrementally. You need to compute word-wrapping 196 | // here. 197 | // 198 | // Each textfield keeps its own insert mode state, which is not how normal 199 | // applications work. To keep an app-wide insert mode, update/copy the 200 | // "insert_mode" field of STB_TexteditState before/after calling API functions. 201 | // 202 | // API 203 | // 204 | // void stb_textedit_initialize_state(STB_TexteditState *state, int is_single_line) 205 | // 206 | // void stb_textedit_click(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y) 207 | // void stb_textedit_drag(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y) 208 | // int stb_textedit_cut(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) 209 | // int stb_textedit_paste(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE *text, int len) 210 | // void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXEDIT_KEYTYPE key) 211 | // 212 | // Each of these functions potentially updates the string and updates the 213 | // state. 214 | // 215 | // initialize_state: 216 | // set the textedit state to a known good default state when initially 217 | // constructing the textedit. 218 | // 219 | // click: 220 | // call this with the mouse x,y on a mouse down; it will update the cursor 221 | // and reset the selection start/end to the cursor point. the x,y must 222 | // be relative to the text widget, with (0,0) being the top left. 223 | // 224 | // drag: 225 | // call this with the mouse x,y on a mouse drag/up; it will update the 226 | // cursor and the selection end point 227 | // 228 | // cut: 229 | // call this to delete the current selection; returns true if there was 230 | // one. you should FIRST copy the current selection to the system paste buffer. 231 | // (To copy, just copy the current selection out of the string yourself.) 232 | // 233 | // paste: 234 | // call this to paste text at the current cursor point or over the current 235 | // selection if there is one. 236 | // 237 | // key: 238 | // call this for keyboard inputs sent to the textfield. you can use it 239 | // for "key down" events or for "translated" key events. if you need to 240 | // do both (as in Win32), or distinguish Unicode characters from control 241 | // inputs, set a high bit to distinguish the two; then you can define the 242 | // various definitions like STB_TEXTEDIT_K_LEFT have the is-key-event bit 243 | // set, and make STB_TEXTEDIT_KEYTOCHAR check that the is-key-event bit is 244 | // clear. STB_TEXTEDIT_KEYTYPE defaults to int, but you can #define it to 245 | // anything other type you wante before including. 246 | // 247 | // 248 | // When rendering, you can read the cursor position and selection state from 249 | // the STB_TexteditState. 250 | // 251 | // 252 | // Notes: 253 | // 254 | // This is designed to be usable in IMGUI, so it allows for the possibility of 255 | // running in an IMGUI that has NOT cached the multi-line layout. For this 256 | // reason, it provides an interface that is compatible with computing the 257 | // layout incrementally--we try to make sure we make as few passes through 258 | // as possible. (For example, to locate the mouse pointer in the text, we 259 | // could define functions that return the X and Y positions of characters 260 | // and binary search Y and then X, but if we're doing dynamic layout this 261 | // will run the layout algorithm many times, so instead we manually search 262 | // forward in one pass. Similar logic applies to e.g. up-arrow and 263 | // down-arrow movement.) 264 | // 265 | // If it's run in a widget that *has* cached the layout, then this is less 266 | // efficient, but it's not horrible on modern computers. But you wouldn't 267 | // want to edit million-line files with it. 268 | 269 | 270 | //////////////////////////////////////////////////////////////////////////// 271 | //////////////////////////////////////////////////////////////////////////// 272 | //// 273 | //// Header-file mode 274 | //// 275 | //// 276 | 277 | #ifndef INCLUDE_STB_TEXTEDIT_H 278 | #define INCLUDE_STB_TEXTEDIT_H 279 | 280 | //////////////////////////////////////////////////////////////////////// 281 | // 282 | // STB_TexteditState 283 | // 284 | // Definition of STB_TexteditState which you should store 285 | // per-textfield; it includes cursor position, selection state, 286 | // and undo state. 287 | // 288 | 289 | #ifndef STB_TEXTEDIT_UNDOSTATECOUNT 290 | #define STB_TEXTEDIT_UNDOSTATECOUNT 99 291 | #endif 292 | #ifndef STB_TEXTEDIT_UNDOCHARCOUNT 293 | #define STB_TEXTEDIT_UNDOCHARCOUNT 999 294 | #endif 295 | #ifndef STB_TEXTEDIT_CHARTYPE 296 | #define STB_TEXTEDIT_CHARTYPE int 297 | #endif 298 | #ifndef STB_TEXTEDIT_POSITIONTYPE 299 | #define STB_TEXTEDIT_POSITIONTYPE int 300 | #endif 301 | 302 | typedef struct 303 | { 304 | // private data 305 | STB_TEXTEDIT_POSITIONTYPE where; 306 | STB_TEXTEDIT_POSITIONTYPE insert_length; 307 | STB_TEXTEDIT_POSITIONTYPE delete_length; 308 | int char_storage; 309 | } StbUndoRecord; 310 | 311 | typedef struct 312 | { 313 | // private data 314 | StbUndoRecord undo_rec [STB_TEXTEDIT_UNDOSTATECOUNT]; 315 | STB_TEXTEDIT_CHARTYPE undo_char[STB_TEXTEDIT_UNDOCHARCOUNT]; 316 | short undo_point, redo_point; 317 | int undo_char_point, redo_char_point; 318 | } StbUndoState; 319 | 320 | typedef struct 321 | { 322 | ///////////////////// 323 | // 324 | // public data 325 | // 326 | 327 | int cursor; 328 | // position of the text cursor within the string 329 | 330 | int select_start; // selection start point 331 | int select_end; 332 | // selection start and end point in characters; if equal, no selection. 333 | // note that start may be less than or greater than end (e.g. when 334 | // dragging the mouse, start is where the initial click was, and you 335 | // can drag in either direction) 336 | 337 | unsigned char insert_mode; 338 | // each textfield keeps its own insert mode state. to keep an app-wide 339 | // insert mode, copy this value in/out of the app state 340 | 341 | int row_count_per_page; 342 | // page size in number of row. 343 | // this value MUST be set to >0 for pageup or pagedown in multilines documents. 344 | 345 | ///////////////////// 346 | // 347 | // private data 348 | // 349 | unsigned char cursor_at_end_of_line; // not implemented yet 350 | unsigned char initialized; 351 | unsigned char has_preferred_x; 352 | unsigned char single_line; 353 | unsigned char padding1, padding2, padding3; 354 | float preferred_x; // this determines where the cursor up/down tries to seek to along x 355 | StbUndoState undostate; 356 | } STB_TexteditState; 357 | 358 | 359 | //////////////////////////////////////////////////////////////////////// 360 | // 361 | // StbTexteditRow 362 | // 363 | // Result of layout query, used by stb_textedit to determine where 364 | // the text in each row is. 365 | 366 | // result of layout query 367 | typedef struct 368 | { 369 | float x0,x1; // starting x location, end x location (allows for align=right, etc) 370 | float baseline_y_delta; // position of baseline relative to previous row's baseline 371 | float ymin,ymax; // height of row above and below baseline 372 | int num_chars; 373 | } StbTexteditRow; 374 | #endif //INCLUDE_STB_TEXTEDIT_H 375 | 376 | 377 | //////////////////////////////////////////////////////////////////////////// 378 | //////////////////////////////////////////////////////////////////////////// 379 | //// 380 | //// Implementation mode 381 | //// 382 | //// 383 | 384 | 385 | // implementation isn't include-guarded, since it might have indirectly 386 | // included just the "header" portion 387 | #ifdef STB_TEXTEDIT_IMPLEMENTATION 388 | 389 | #ifndef STB_TEXTEDIT_memmove 390 | #include 391 | #define STB_TEXTEDIT_memmove memmove 392 | #endif 393 | 394 | 395 | ///////////////////////////////////////////////////////////////////////////// 396 | // 397 | // Mouse input handling 398 | // 399 | 400 | // traverse the layout to locate the nearest character to a display position 401 | static int stb_text_locate_coord(STB_TEXTEDIT_STRING *str, float x, float y) 402 | { 403 | StbTexteditRow r; 404 | int n = STB_TEXTEDIT_STRINGLEN(str); 405 | float base_y = 0, prev_x; 406 | int i=0, k; 407 | 408 | r.x0 = r.x1 = 0; 409 | r.ymin = r.ymax = 0; 410 | r.num_chars = 0; 411 | 412 | // search rows to find one that straddles 'y' 413 | while (i < n) { 414 | STB_TEXTEDIT_LAYOUTROW(&r, str, i); 415 | if (r.num_chars <= 0) 416 | return n; 417 | 418 | if (i==0 && y < base_y + r.ymin) 419 | return 0; 420 | 421 | if (y < base_y + r.ymax) 422 | break; 423 | 424 | i += r.num_chars; 425 | base_y += r.baseline_y_delta; 426 | } 427 | 428 | // below all text, return 'after' last character 429 | if (i >= n) 430 | return n; 431 | 432 | // check if it's before the beginning of the line 433 | if (x < r.x0) 434 | return i; 435 | 436 | // check if it's before the end of the line 437 | if (x < r.x1) { 438 | // search characters in row for one that straddles 'x' 439 | prev_x = r.x0; 440 | for (k=0; k < r.num_chars; ++k) { 441 | float w = STB_TEXTEDIT_GETWIDTH(str, i, k); 442 | if (x < prev_x+w) { 443 | if (x < prev_x+w/2) 444 | return k+i; 445 | else 446 | return k+i+1; 447 | } 448 | prev_x += w; 449 | } 450 | // shouldn't happen, but if it does, fall through to end-of-line case 451 | } 452 | 453 | // if the last character is a newline, return that. otherwise return 'after' the last character 454 | if (STB_TEXTEDIT_GETCHAR(str, i+r.num_chars-1) == STB_TEXTEDIT_NEWLINE) 455 | return i+r.num_chars-1; 456 | else 457 | return i+r.num_chars; 458 | } 459 | 460 | // API click: on mouse down, move the cursor to the clicked location, and reset the selection 461 | static void stb_textedit_click(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y) 462 | { 463 | // In single-line mode, just always make y = 0. This lets the drag keep working if the mouse 464 | // goes off the top or bottom of the text 465 | if( state->single_line ) 466 | { 467 | StbTexteditRow r; 468 | STB_TEXTEDIT_LAYOUTROW(&r, str, 0); 469 | y = r.ymin; 470 | } 471 | 472 | state->cursor = stb_text_locate_coord(str, x, y); 473 | state->select_start = state->cursor; 474 | state->select_end = state->cursor; 475 | state->has_preferred_x = 0; 476 | } 477 | 478 | // API drag: on mouse drag, move the cursor and selection endpoint to the clicked location 479 | static void stb_textedit_drag(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y) 480 | { 481 | int p = 0; 482 | 483 | // In single-line mode, just always make y = 0. This lets the drag keep working if the mouse 484 | // goes off the top or bottom of the text 485 | if( state->single_line ) 486 | { 487 | StbTexteditRow r; 488 | STB_TEXTEDIT_LAYOUTROW(&r, str, 0); 489 | y = r.ymin; 490 | } 491 | 492 | if (state->select_start == state->select_end) 493 | state->select_start = state->cursor; 494 | 495 | p = stb_text_locate_coord(str, x, y); 496 | state->cursor = state->select_end = p; 497 | } 498 | 499 | ///////////////////////////////////////////////////////////////////////////// 500 | // 501 | // Keyboard input handling 502 | // 503 | 504 | // forward declarations 505 | static void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state); 506 | static void stb_text_redo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state); 507 | static void stb_text_makeundo_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length); 508 | static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length); 509 | static void stb_text_makeundo_replace(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length); 510 | 511 | typedef struct 512 | { 513 | float x,y; // position of n'th character 514 | float height; // height of line 515 | int first_char, length; // first char of row, and length 516 | int prev_first; // first char of previous row 517 | } StbFindState; 518 | 519 | // find the x/y location of a character, and remember info about the previous row in 520 | // case we get a move-up event (for page up, we'll have to rescan) 521 | static void stb_textedit_find_charpos(StbFindState *find, STB_TEXTEDIT_STRING *str, int n, int single_line) 522 | { 523 | StbTexteditRow r; 524 | int prev_start = 0; 525 | int z = STB_TEXTEDIT_STRINGLEN(str); 526 | int i=0, first; 527 | 528 | if (n == z && single_line) { 529 | // special case if it's at the end (may not be needed?) 530 | STB_TEXTEDIT_LAYOUTROW(&r, str, 0); 531 | find->y = 0; 532 | find->first_char = 0; 533 | find->length = z; 534 | find->height = r.ymax - r.ymin; 535 | find->x = r.x1; 536 | return; 537 | } 538 | 539 | // search rows to find the one that straddles character n 540 | find->y = 0; 541 | 542 | for(;;) { 543 | STB_TEXTEDIT_LAYOUTROW(&r, str, i); 544 | if (n < i + r.num_chars) 545 | break; 546 | if (i + r.num_chars == z && z > 0 && STB_TEXTEDIT_GETCHAR(str, z - 1) != STB_TEXTEDIT_NEWLINE) // [DEAR IMGUI] special handling for last line 547 | break; // [DEAR IMGUI] 548 | prev_start = i; 549 | i += r.num_chars; 550 | find->y += r.baseline_y_delta; 551 | if (i == z) // [DEAR IMGUI] 552 | { 553 | r.num_chars = 0; // [DEAR IMGUI] 554 | break; // [DEAR IMGUI] 555 | } 556 | } 557 | 558 | find->first_char = first = i; 559 | find->length = r.num_chars; 560 | find->height = r.ymax - r.ymin; 561 | find->prev_first = prev_start; 562 | 563 | // now scan to find xpos 564 | find->x = r.x0; 565 | for (i=0; first+i < n; ++i) 566 | find->x += STB_TEXTEDIT_GETWIDTH(str, first, i); 567 | } 568 | 569 | #define STB_TEXT_HAS_SELECTION(s) ((s)->select_start != (s)->select_end) 570 | 571 | // make the selection/cursor state valid if client altered the string 572 | static void stb_textedit_clamp(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) 573 | { 574 | int n = STB_TEXTEDIT_STRINGLEN(str); 575 | if (STB_TEXT_HAS_SELECTION(state)) { 576 | if (state->select_start > n) state->select_start = n; 577 | if (state->select_end > n) state->select_end = n; 578 | // if clamping forced them to be equal, move the cursor to match 579 | if (state->select_start == state->select_end) 580 | state->cursor = state->select_start; 581 | } 582 | if (state->cursor > n) state->cursor = n; 583 | } 584 | 585 | // delete characters while updating undo 586 | static void stb_textedit_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int len) 587 | { 588 | stb_text_makeundo_delete(str, state, where, len); 589 | STB_TEXTEDIT_DELETECHARS(str, where, len); 590 | state->has_preferred_x = 0; 591 | } 592 | 593 | // delete the section 594 | static void stb_textedit_delete_selection(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) 595 | { 596 | stb_textedit_clamp(str, state); 597 | if (STB_TEXT_HAS_SELECTION(state)) { 598 | if (state->select_start < state->select_end) { 599 | stb_textedit_delete(str, state, state->select_start, state->select_end - state->select_start); 600 | state->select_end = state->cursor = state->select_start; 601 | } else { 602 | stb_textedit_delete(str, state, state->select_end, state->select_start - state->select_end); 603 | state->select_start = state->cursor = state->select_end; 604 | } 605 | state->has_preferred_x = 0; 606 | } 607 | } 608 | 609 | // canoncialize the selection so start <= end 610 | static void stb_textedit_sortselection(STB_TexteditState *state) 611 | { 612 | if (state->select_end < state->select_start) { 613 | int temp = state->select_end; 614 | state->select_end = state->select_start; 615 | state->select_start = temp; 616 | } 617 | } 618 | 619 | // move cursor to first character of selection 620 | static void stb_textedit_move_to_first(STB_TexteditState *state) 621 | { 622 | if (STB_TEXT_HAS_SELECTION(state)) { 623 | stb_textedit_sortselection(state); 624 | state->cursor = state->select_start; 625 | state->select_end = state->select_start; 626 | state->has_preferred_x = 0; 627 | } 628 | } 629 | 630 | // move cursor to last character of selection 631 | static void stb_textedit_move_to_last(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) 632 | { 633 | if (STB_TEXT_HAS_SELECTION(state)) { 634 | stb_textedit_sortselection(state); 635 | stb_textedit_clamp(str, state); 636 | state->cursor = state->select_end; 637 | state->select_start = state->select_end; 638 | state->has_preferred_x = 0; 639 | } 640 | } 641 | 642 | #ifdef STB_TEXTEDIT_IS_SPACE 643 | static int is_word_boundary( STB_TEXTEDIT_STRING *str, int idx ) 644 | { 645 | return idx > 0 ? (STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str,idx-1) ) && !STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str, idx) ) ) : 1; 646 | } 647 | 648 | #ifndef STB_TEXTEDIT_MOVEWORDLEFT 649 | static int stb_textedit_move_to_word_previous( STB_TEXTEDIT_STRING *str, int c ) 650 | { 651 | --c; // always move at least one character 652 | while( c >= 0 && !is_word_boundary( str, c ) ) 653 | --c; 654 | 655 | if( c < 0 ) 656 | c = 0; 657 | 658 | return c; 659 | } 660 | #define STB_TEXTEDIT_MOVEWORDLEFT stb_textedit_move_to_word_previous 661 | #endif 662 | 663 | #ifndef STB_TEXTEDIT_MOVEWORDRIGHT 664 | static int stb_textedit_move_to_word_next( STB_TEXTEDIT_STRING *str, int c ) 665 | { 666 | const int len = STB_TEXTEDIT_STRINGLEN(str); 667 | ++c; // always move at least one character 668 | while( c < len && !is_word_boundary( str, c ) ) 669 | ++c; 670 | 671 | if( c > len ) 672 | c = len; 673 | 674 | return c; 675 | } 676 | #define STB_TEXTEDIT_MOVEWORDRIGHT stb_textedit_move_to_word_next 677 | #endif 678 | 679 | #endif 680 | 681 | // update selection and cursor to match each other 682 | static void stb_textedit_prep_selection_at_cursor(STB_TexteditState *state) 683 | { 684 | if (!STB_TEXT_HAS_SELECTION(state)) 685 | state->select_start = state->select_end = state->cursor; 686 | else 687 | state->cursor = state->select_end; 688 | } 689 | 690 | // API cut: delete selection 691 | static int stb_textedit_cut(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) 692 | { 693 | if (STB_TEXT_HAS_SELECTION(state)) { 694 | stb_textedit_delete_selection(str,state); // implicitly clamps 695 | state->has_preferred_x = 0; 696 | return 1; 697 | } 698 | return 0; 699 | } 700 | 701 | // API paste: replace existing selection with passed-in text 702 | static int stb_textedit_paste_internal(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE *text, int len) 703 | { 704 | // if there's a selection, the paste should delete it 705 | stb_textedit_clamp(str, state); 706 | stb_textedit_delete_selection(str,state); 707 | // try to insert the characters 708 | if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, len)) { 709 | stb_text_makeundo_insert(state, state->cursor, len); 710 | state->cursor += len; 711 | state->has_preferred_x = 0; 712 | return 1; 713 | } 714 | // note: paste failure will leave deleted selection, may be restored with an undo (see https://github.com/nothings/stb/issues/734 for details) 715 | return 0; 716 | } 717 | 718 | #ifndef STB_TEXTEDIT_KEYTYPE 719 | #define STB_TEXTEDIT_KEYTYPE int 720 | #endif 721 | 722 | // API key: process a keyboard input 723 | static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_KEYTYPE key) 724 | { 725 | retry: 726 | switch (key) { 727 | default: { 728 | int c = STB_TEXTEDIT_KEYTOTEXT(key); 729 | if (c > 0) { 730 | STB_TEXTEDIT_CHARTYPE ch = (STB_TEXTEDIT_CHARTYPE) c; 731 | 732 | // can't add newline in single-line mode 733 | if (c == '\n' && state->single_line) 734 | break; 735 | 736 | if (state->insert_mode && !STB_TEXT_HAS_SELECTION(state) && state->cursor < STB_TEXTEDIT_STRINGLEN(str)) { 737 | stb_text_makeundo_replace(str, state, state->cursor, 1, 1); 738 | STB_TEXTEDIT_DELETECHARS(str, state->cursor, 1); 739 | if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) { 740 | ++state->cursor; 741 | state->has_preferred_x = 0; 742 | } 743 | } else { 744 | stb_textedit_delete_selection(str,state); // implicitly clamps 745 | if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) { 746 | stb_text_makeundo_insert(state, state->cursor, 1); 747 | ++state->cursor; 748 | state->has_preferred_x = 0; 749 | } 750 | } 751 | } 752 | break; 753 | } 754 | 755 | #ifdef STB_TEXTEDIT_K_INSERT 756 | case STB_TEXTEDIT_K_INSERT: 757 | state->insert_mode = !state->insert_mode; 758 | break; 759 | #endif 760 | 761 | case STB_TEXTEDIT_K_UNDO: 762 | stb_text_undo(str, state); 763 | state->has_preferred_x = 0; 764 | break; 765 | 766 | case STB_TEXTEDIT_K_REDO: 767 | stb_text_redo(str, state); 768 | state->has_preferred_x = 0; 769 | break; 770 | 771 | case STB_TEXTEDIT_K_LEFT: 772 | // if currently there's a selection, move cursor to start of selection 773 | if (STB_TEXT_HAS_SELECTION(state)) 774 | stb_textedit_move_to_first(state); 775 | else 776 | if (state->cursor > 0) 777 | --state->cursor; 778 | state->has_preferred_x = 0; 779 | break; 780 | 781 | case STB_TEXTEDIT_K_RIGHT: 782 | // if currently there's a selection, move cursor to end of selection 783 | if (STB_TEXT_HAS_SELECTION(state)) 784 | stb_textedit_move_to_last(str, state); 785 | else 786 | ++state->cursor; 787 | stb_textedit_clamp(str, state); 788 | state->has_preferred_x = 0; 789 | break; 790 | 791 | case STB_TEXTEDIT_K_LEFT | STB_TEXTEDIT_K_SHIFT: 792 | stb_textedit_clamp(str, state); 793 | stb_textedit_prep_selection_at_cursor(state); 794 | // move selection left 795 | if (state->select_end > 0) 796 | --state->select_end; 797 | state->cursor = state->select_end; 798 | state->has_preferred_x = 0; 799 | break; 800 | 801 | #ifdef STB_TEXTEDIT_MOVEWORDLEFT 802 | case STB_TEXTEDIT_K_WORDLEFT: 803 | if (STB_TEXT_HAS_SELECTION(state)) 804 | stb_textedit_move_to_first(state); 805 | else { 806 | state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor); 807 | stb_textedit_clamp( str, state ); 808 | } 809 | break; 810 | 811 | case STB_TEXTEDIT_K_WORDLEFT | STB_TEXTEDIT_K_SHIFT: 812 | if( !STB_TEXT_HAS_SELECTION( state ) ) 813 | stb_textedit_prep_selection_at_cursor(state); 814 | 815 | state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor); 816 | state->select_end = state->cursor; 817 | 818 | stb_textedit_clamp( str, state ); 819 | break; 820 | #endif 821 | 822 | #ifdef STB_TEXTEDIT_MOVEWORDRIGHT 823 | case STB_TEXTEDIT_K_WORDRIGHT: 824 | if (STB_TEXT_HAS_SELECTION(state)) 825 | stb_textedit_move_to_last(str, state); 826 | else { 827 | state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor); 828 | stb_textedit_clamp( str, state ); 829 | } 830 | break; 831 | 832 | case STB_TEXTEDIT_K_WORDRIGHT | STB_TEXTEDIT_K_SHIFT: 833 | if( !STB_TEXT_HAS_SELECTION( state ) ) 834 | stb_textedit_prep_selection_at_cursor(state); 835 | 836 | state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor); 837 | state->select_end = state->cursor; 838 | 839 | stb_textedit_clamp( str, state ); 840 | break; 841 | #endif 842 | 843 | case STB_TEXTEDIT_K_RIGHT | STB_TEXTEDIT_K_SHIFT: 844 | stb_textedit_prep_selection_at_cursor(state); 845 | // move selection right 846 | ++state->select_end; 847 | stb_textedit_clamp(str, state); 848 | state->cursor = state->select_end; 849 | state->has_preferred_x = 0; 850 | break; 851 | 852 | case STB_TEXTEDIT_K_DOWN: 853 | case STB_TEXTEDIT_K_DOWN | STB_TEXTEDIT_K_SHIFT: 854 | case STB_TEXTEDIT_K_PGDOWN: 855 | case STB_TEXTEDIT_K_PGDOWN | STB_TEXTEDIT_K_SHIFT: { 856 | StbFindState find; 857 | StbTexteditRow row; 858 | int i, j, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0; 859 | int is_page = (key & ~STB_TEXTEDIT_K_SHIFT) == STB_TEXTEDIT_K_PGDOWN; 860 | int row_count = is_page ? state->row_count_per_page : 1; 861 | 862 | if (!is_page && state->single_line) { 863 | // on windows, up&down in single-line behave like left&right 864 | key = STB_TEXTEDIT_K_RIGHT | (key & STB_TEXTEDIT_K_SHIFT); 865 | goto retry; 866 | } 867 | 868 | if (sel) 869 | stb_textedit_prep_selection_at_cursor(state); 870 | else if (STB_TEXT_HAS_SELECTION(state)) 871 | stb_textedit_move_to_last(str, state); 872 | 873 | // compute current position of cursor point 874 | stb_textedit_clamp(str, state); 875 | stb_textedit_find_charpos(&find, str, state->cursor, state->single_line); 876 | 877 | for (j = 0; j < row_count; ++j) { 878 | float x, goal_x = state->has_preferred_x ? state->preferred_x : find.x; 879 | int start = find.first_char + find.length; 880 | 881 | if (find.length == 0) 882 | break; 883 | 884 | // [DEAR IMGUI] 885 | // going down while being on the last line shouldn't bring us to that line end 886 | if (STB_TEXTEDIT_GETCHAR(str, find.first_char + find.length - 1) != STB_TEXTEDIT_NEWLINE) 887 | break; 888 | 889 | // now find character position down a row 890 | state->cursor = start; 891 | STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor); 892 | x = row.x0; 893 | for (i=0; i < row.num_chars; ++i) { 894 | float dx = STB_TEXTEDIT_GETWIDTH(str, start, i); 895 | #ifdef STB_TEXTEDIT_GETWIDTH_NEWLINE 896 | if (dx == STB_TEXTEDIT_GETWIDTH_NEWLINE) 897 | break; 898 | #endif 899 | x += dx; 900 | if (x > goal_x) 901 | break; 902 | ++state->cursor; 903 | } 904 | stb_textedit_clamp(str, state); 905 | 906 | state->has_preferred_x = 1; 907 | state->preferred_x = goal_x; 908 | 909 | if (sel) 910 | state->select_end = state->cursor; 911 | 912 | // go to next line 913 | find.first_char = find.first_char + find.length; 914 | find.length = row.num_chars; 915 | } 916 | break; 917 | } 918 | 919 | case STB_TEXTEDIT_K_UP: 920 | case STB_TEXTEDIT_K_UP | STB_TEXTEDIT_K_SHIFT: 921 | case STB_TEXTEDIT_K_PGUP: 922 | case STB_TEXTEDIT_K_PGUP | STB_TEXTEDIT_K_SHIFT: { 923 | StbFindState find; 924 | StbTexteditRow row; 925 | int i, j, prev_scan, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0; 926 | int is_page = (key & ~STB_TEXTEDIT_K_SHIFT) == STB_TEXTEDIT_K_PGUP; 927 | int row_count = is_page ? state->row_count_per_page : 1; 928 | 929 | if (!is_page && state->single_line) { 930 | // on windows, up&down become left&right 931 | key = STB_TEXTEDIT_K_LEFT | (key & STB_TEXTEDIT_K_SHIFT); 932 | goto retry; 933 | } 934 | 935 | if (sel) 936 | stb_textedit_prep_selection_at_cursor(state); 937 | else if (STB_TEXT_HAS_SELECTION(state)) 938 | stb_textedit_move_to_first(state); 939 | 940 | // compute current position of cursor point 941 | stb_textedit_clamp(str, state); 942 | stb_textedit_find_charpos(&find, str, state->cursor, state->single_line); 943 | 944 | for (j = 0; j < row_count; ++j) { 945 | float x, goal_x = state->has_preferred_x ? state->preferred_x : find.x; 946 | 947 | // can only go up if there's a previous row 948 | if (find.prev_first == find.first_char) 949 | break; 950 | 951 | // now find character position up a row 952 | state->cursor = find.prev_first; 953 | STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor); 954 | x = row.x0; 955 | for (i=0; i < row.num_chars; ++i) { 956 | float dx = STB_TEXTEDIT_GETWIDTH(str, find.prev_first, i); 957 | #ifdef STB_TEXTEDIT_GETWIDTH_NEWLINE 958 | if (dx == STB_TEXTEDIT_GETWIDTH_NEWLINE) 959 | break; 960 | #endif 961 | x += dx; 962 | if (x > goal_x) 963 | break; 964 | ++state->cursor; 965 | } 966 | stb_textedit_clamp(str, state); 967 | 968 | state->has_preferred_x = 1; 969 | state->preferred_x = goal_x; 970 | 971 | if (sel) 972 | state->select_end = state->cursor; 973 | 974 | // go to previous line 975 | // (we need to scan previous line the hard way. maybe we could expose this as a new API function?) 976 | prev_scan = find.prev_first > 0 ? find.prev_first - 1 : 0; 977 | while (prev_scan > 0 && STB_TEXTEDIT_GETCHAR(str, prev_scan - 1) != STB_TEXTEDIT_NEWLINE) 978 | --prev_scan; 979 | find.first_char = find.prev_first; 980 | find.prev_first = prev_scan; 981 | } 982 | break; 983 | } 984 | 985 | case STB_TEXTEDIT_K_DELETE: 986 | case STB_TEXTEDIT_K_DELETE | STB_TEXTEDIT_K_SHIFT: 987 | if (STB_TEXT_HAS_SELECTION(state)) 988 | stb_textedit_delete_selection(str, state); 989 | else { 990 | int n = STB_TEXTEDIT_STRINGLEN(str); 991 | if (state->cursor < n) 992 | stb_textedit_delete(str, state, state->cursor, 1); 993 | } 994 | state->has_preferred_x = 0; 995 | break; 996 | 997 | case STB_TEXTEDIT_K_BACKSPACE: 998 | case STB_TEXTEDIT_K_BACKSPACE | STB_TEXTEDIT_K_SHIFT: 999 | if (STB_TEXT_HAS_SELECTION(state)) 1000 | stb_textedit_delete_selection(str, state); 1001 | else { 1002 | stb_textedit_clamp(str, state); 1003 | if (state->cursor > 0) { 1004 | stb_textedit_delete(str, state, state->cursor-1, 1); 1005 | --state->cursor; 1006 | } 1007 | } 1008 | state->has_preferred_x = 0; 1009 | break; 1010 | 1011 | #ifdef STB_TEXTEDIT_K_TEXTSTART2 1012 | case STB_TEXTEDIT_K_TEXTSTART2: 1013 | #endif 1014 | case STB_TEXTEDIT_K_TEXTSTART: 1015 | state->cursor = state->select_start = state->select_end = 0; 1016 | state->has_preferred_x = 0; 1017 | break; 1018 | 1019 | #ifdef STB_TEXTEDIT_K_TEXTEND2 1020 | case STB_TEXTEDIT_K_TEXTEND2: 1021 | #endif 1022 | case STB_TEXTEDIT_K_TEXTEND: 1023 | state->cursor = STB_TEXTEDIT_STRINGLEN(str); 1024 | state->select_start = state->select_end = 0; 1025 | state->has_preferred_x = 0; 1026 | break; 1027 | 1028 | #ifdef STB_TEXTEDIT_K_TEXTSTART2 1029 | case STB_TEXTEDIT_K_TEXTSTART2 | STB_TEXTEDIT_K_SHIFT: 1030 | #endif 1031 | case STB_TEXTEDIT_K_TEXTSTART | STB_TEXTEDIT_K_SHIFT: 1032 | stb_textedit_prep_selection_at_cursor(state); 1033 | state->cursor = state->select_end = 0; 1034 | state->has_preferred_x = 0; 1035 | break; 1036 | 1037 | #ifdef STB_TEXTEDIT_K_TEXTEND2 1038 | case STB_TEXTEDIT_K_TEXTEND2 | STB_TEXTEDIT_K_SHIFT: 1039 | #endif 1040 | case STB_TEXTEDIT_K_TEXTEND | STB_TEXTEDIT_K_SHIFT: 1041 | stb_textedit_prep_selection_at_cursor(state); 1042 | state->cursor = state->select_end = STB_TEXTEDIT_STRINGLEN(str); 1043 | state->has_preferred_x = 0; 1044 | break; 1045 | 1046 | 1047 | #ifdef STB_TEXTEDIT_K_LINESTART2 1048 | case STB_TEXTEDIT_K_LINESTART2: 1049 | #endif 1050 | case STB_TEXTEDIT_K_LINESTART: 1051 | stb_textedit_clamp(str, state); 1052 | stb_textedit_move_to_first(state); 1053 | if (state->single_line) 1054 | state->cursor = 0; 1055 | else while (state->cursor > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) != STB_TEXTEDIT_NEWLINE) 1056 | --state->cursor; 1057 | state->has_preferred_x = 0; 1058 | break; 1059 | 1060 | #ifdef STB_TEXTEDIT_K_LINEEND2 1061 | case STB_TEXTEDIT_K_LINEEND2: 1062 | #endif 1063 | case STB_TEXTEDIT_K_LINEEND: { 1064 | int n = STB_TEXTEDIT_STRINGLEN(str); 1065 | stb_textedit_clamp(str, state); 1066 | stb_textedit_move_to_first(state); 1067 | if (state->single_line) 1068 | state->cursor = n; 1069 | else while (state->cursor < n && STB_TEXTEDIT_GETCHAR(str, state->cursor) != STB_TEXTEDIT_NEWLINE) 1070 | ++state->cursor; 1071 | state->has_preferred_x = 0; 1072 | break; 1073 | } 1074 | 1075 | #ifdef STB_TEXTEDIT_K_LINESTART2 1076 | case STB_TEXTEDIT_K_LINESTART2 | STB_TEXTEDIT_K_SHIFT: 1077 | #endif 1078 | case STB_TEXTEDIT_K_LINESTART | STB_TEXTEDIT_K_SHIFT: 1079 | stb_textedit_clamp(str, state); 1080 | stb_textedit_prep_selection_at_cursor(state); 1081 | if (state->single_line) 1082 | state->cursor = 0; 1083 | else while (state->cursor > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) != STB_TEXTEDIT_NEWLINE) 1084 | --state->cursor; 1085 | state->select_end = state->cursor; 1086 | state->has_preferred_x = 0; 1087 | break; 1088 | 1089 | #ifdef STB_TEXTEDIT_K_LINEEND2 1090 | case STB_TEXTEDIT_K_LINEEND2 | STB_TEXTEDIT_K_SHIFT: 1091 | #endif 1092 | case STB_TEXTEDIT_K_LINEEND | STB_TEXTEDIT_K_SHIFT: { 1093 | int n = STB_TEXTEDIT_STRINGLEN(str); 1094 | stb_textedit_clamp(str, state); 1095 | stb_textedit_prep_selection_at_cursor(state); 1096 | if (state->single_line) 1097 | state->cursor = n; 1098 | else while (state->cursor < n && STB_TEXTEDIT_GETCHAR(str, state->cursor) != STB_TEXTEDIT_NEWLINE) 1099 | ++state->cursor; 1100 | state->select_end = state->cursor; 1101 | state->has_preferred_x = 0; 1102 | break; 1103 | } 1104 | } 1105 | } 1106 | 1107 | ///////////////////////////////////////////////////////////////////////////// 1108 | // 1109 | // Undo processing 1110 | // 1111 | // @OPTIMIZE: the undo/redo buffer should be circular 1112 | 1113 | static void stb_textedit_flush_redo(StbUndoState *state) 1114 | { 1115 | state->redo_point = STB_TEXTEDIT_UNDOSTATECOUNT; 1116 | state->redo_char_point = STB_TEXTEDIT_UNDOCHARCOUNT; 1117 | } 1118 | 1119 | // discard the oldest entry in the undo list 1120 | static void stb_textedit_discard_undo(StbUndoState *state) 1121 | { 1122 | if (state->undo_point > 0) { 1123 | // if the 0th undo state has characters, clean those up 1124 | if (state->undo_rec[0].char_storage >= 0) { 1125 | int n = state->undo_rec[0].insert_length, i; 1126 | // delete n characters from all other records 1127 | state->undo_char_point -= n; 1128 | STB_TEXTEDIT_memmove(state->undo_char, state->undo_char + n, (size_t) (state->undo_char_point*sizeof(STB_TEXTEDIT_CHARTYPE))); 1129 | for (i=0; i < state->undo_point; ++i) 1130 | if (state->undo_rec[i].char_storage >= 0) 1131 | state->undo_rec[i].char_storage -= n; // @OPTIMIZE: get rid of char_storage and infer it 1132 | } 1133 | --state->undo_point; 1134 | STB_TEXTEDIT_memmove(state->undo_rec, state->undo_rec+1, (size_t) (state->undo_point*sizeof(state->undo_rec[0]))); 1135 | } 1136 | } 1137 | 1138 | // discard the oldest entry in the redo list--it's bad if this 1139 | // ever happens, but because undo & redo have to store the actual 1140 | // characters in different cases, the redo character buffer can 1141 | // fill up even though the undo buffer didn't 1142 | static void stb_textedit_discard_redo(StbUndoState *state) 1143 | { 1144 | int k = STB_TEXTEDIT_UNDOSTATECOUNT-1; 1145 | 1146 | if (state->redo_point <= k) { 1147 | // if the k'th undo state has characters, clean those up 1148 | if (state->undo_rec[k].char_storage >= 0) { 1149 | int n = state->undo_rec[k].insert_length, i; 1150 | // move the remaining redo character data to the end of the buffer 1151 | state->redo_char_point += n; 1152 | STB_TEXTEDIT_memmove(state->undo_char + state->redo_char_point, state->undo_char + state->redo_char_point-n, (size_t) ((STB_TEXTEDIT_UNDOCHARCOUNT - state->redo_char_point)*sizeof(STB_TEXTEDIT_CHARTYPE))); 1153 | // adjust the position of all the other records to account for above memmove 1154 | for (i=state->redo_point; i < k; ++i) 1155 | if (state->undo_rec[i].char_storage >= 0) 1156 | state->undo_rec[i].char_storage += n; 1157 | } 1158 | // now move all the redo records towards the end of the buffer; the first one is at 'redo_point' 1159 | // [DEAR IMGUI] 1160 | size_t move_size = (size_t)((STB_TEXTEDIT_UNDOSTATECOUNT - state->redo_point - 1) * sizeof(state->undo_rec[0])); 1161 | const char* buf_begin = (char*)state->undo_rec; (void)buf_begin; 1162 | const char* buf_end = (char*)state->undo_rec + sizeof(state->undo_rec); (void)buf_end; 1163 | IM_ASSERT(((char*)(state->undo_rec + state->redo_point)) >= buf_begin); 1164 | IM_ASSERT(((char*)(state->undo_rec + state->redo_point + 1) + move_size) <= buf_end); 1165 | STB_TEXTEDIT_memmove(state->undo_rec + state->redo_point+1, state->undo_rec + state->redo_point, move_size); 1166 | 1167 | // now move redo_point to point to the new one 1168 | ++state->redo_point; 1169 | } 1170 | } 1171 | 1172 | static StbUndoRecord *stb_text_create_undo_record(StbUndoState *state, int numchars) 1173 | { 1174 | // any time we create a new undo record, we discard redo 1175 | stb_textedit_flush_redo(state); 1176 | 1177 | // if we have no free records, we have to make room, by sliding the 1178 | // existing records down 1179 | if (state->undo_point == STB_TEXTEDIT_UNDOSTATECOUNT) 1180 | stb_textedit_discard_undo(state); 1181 | 1182 | // if the characters to store won't possibly fit in the buffer, we can't undo 1183 | if (numchars > STB_TEXTEDIT_UNDOCHARCOUNT) { 1184 | state->undo_point = 0; 1185 | state->undo_char_point = 0; 1186 | return NULL; 1187 | } 1188 | 1189 | // if we don't have enough free characters in the buffer, we have to make room 1190 | while (state->undo_char_point + numchars > STB_TEXTEDIT_UNDOCHARCOUNT) 1191 | stb_textedit_discard_undo(state); 1192 | 1193 | return &state->undo_rec[state->undo_point++]; 1194 | } 1195 | 1196 | static STB_TEXTEDIT_CHARTYPE *stb_text_createundo(StbUndoState *state, int pos, int insert_len, int delete_len) 1197 | { 1198 | StbUndoRecord *r = stb_text_create_undo_record(state, insert_len); 1199 | if (r == NULL) 1200 | return NULL; 1201 | 1202 | r->where = pos; 1203 | r->insert_length = (STB_TEXTEDIT_POSITIONTYPE) insert_len; 1204 | r->delete_length = (STB_TEXTEDIT_POSITIONTYPE) delete_len; 1205 | 1206 | if (insert_len == 0) { 1207 | r->char_storage = -1; 1208 | return NULL; 1209 | } else { 1210 | r->char_storage = state->undo_char_point; 1211 | state->undo_char_point += insert_len; 1212 | return &state->undo_char[r->char_storage]; 1213 | } 1214 | } 1215 | 1216 | static void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) 1217 | { 1218 | StbUndoState *s = &state->undostate; 1219 | StbUndoRecord u, *r; 1220 | if (s->undo_point == 0) 1221 | return; 1222 | 1223 | // we need to do two things: apply the undo record, and create a redo record 1224 | u = s->undo_rec[s->undo_point-1]; 1225 | r = &s->undo_rec[s->redo_point-1]; 1226 | r->char_storage = -1; 1227 | 1228 | r->insert_length = u.delete_length; 1229 | r->delete_length = u.insert_length; 1230 | r->where = u.where; 1231 | 1232 | if (u.delete_length) { 1233 | // if the undo record says to delete characters, then the redo record will 1234 | // need to re-insert the characters that get deleted, so we need to store 1235 | // them. 1236 | 1237 | // there are three cases: 1238 | // there's enough room to store the characters 1239 | // characters stored for *redoing* don't leave room for redo 1240 | // characters stored for *undoing* don't leave room for redo 1241 | // if the last is true, we have to bail 1242 | 1243 | if (s->undo_char_point + u.delete_length >= STB_TEXTEDIT_UNDOCHARCOUNT) { 1244 | // the undo records take up too much character space; there's no space to store the redo characters 1245 | r->insert_length = 0; 1246 | } else { 1247 | int i; 1248 | 1249 | // there's definitely room to store the characters eventually 1250 | while (s->undo_char_point + u.delete_length > s->redo_char_point) { 1251 | // should never happen: 1252 | if (s->redo_point == STB_TEXTEDIT_UNDOSTATECOUNT) 1253 | return; 1254 | // there's currently not enough room, so discard a redo record 1255 | stb_textedit_discard_redo(s); 1256 | } 1257 | r = &s->undo_rec[s->redo_point-1]; 1258 | 1259 | r->char_storage = s->redo_char_point - u.delete_length; 1260 | s->redo_char_point = s->redo_char_point - u.delete_length; 1261 | 1262 | // now save the characters 1263 | for (i=0; i < u.delete_length; ++i) 1264 | s->undo_char[r->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u.where + i); 1265 | } 1266 | 1267 | // now we can carry out the deletion 1268 | STB_TEXTEDIT_DELETECHARS(str, u.where, u.delete_length); 1269 | } 1270 | 1271 | // check type of recorded action: 1272 | if (u.insert_length) { 1273 | // easy case: was a deletion, so we need to insert n characters 1274 | STB_TEXTEDIT_INSERTCHARS(str, u.where, &s->undo_char[u.char_storage], u.insert_length); 1275 | s->undo_char_point -= u.insert_length; 1276 | } 1277 | 1278 | state->cursor = u.where + u.insert_length; 1279 | 1280 | s->undo_point--; 1281 | s->redo_point--; 1282 | } 1283 | 1284 | static void stb_text_redo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) 1285 | { 1286 | StbUndoState *s = &state->undostate; 1287 | StbUndoRecord *u, r; 1288 | if (s->redo_point == STB_TEXTEDIT_UNDOSTATECOUNT) 1289 | return; 1290 | 1291 | // we need to do two things: apply the redo record, and create an undo record 1292 | u = &s->undo_rec[s->undo_point]; 1293 | r = s->undo_rec[s->redo_point]; 1294 | 1295 | // we KNOW there must be room for the undo record, because the redo record 1296 | // was derived from an undo record 1297 | 1298 | u->delete_length = r.insert_length; 1299 | u->insert_length = r.delete_length; 1300 | u->where = r.where; 1301 | u->char_storage = -1; 1302 | 1303 | if (r.delete_length) { 1304 | // the redo record requires us to delete characters, so the undo record 1305 | // needs to store the characters 1306 | 1307 | if (s->undo_char_point + u->insert_length > s->redo_char_point) { 1308 | u->insert_length = 0; 1309 | u->delete_length = 0; 1310 | } else { 1311 | int i; 1312 | u->char_storage = s->undo_char_point; 1313 | s->undo_char_point = s->undo_char_point + u->insert_length; 1314 | 1315 | // now save the characters 1316 | for (i=0; i < u->insert_length; ++i) 1317 | s->undo_char[u->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u->where + i); 1318 | } 1319 | 1320 | STB_TEXTEDIT_DELETECHARS(str, r.where, r.delete_length); 1321 | } 1322 | 1323 | if (r.insert_length) { 1324 | // easy case: need to insert n characters 1325 | STB_TEXTEDIT_INSERTCHARS(str, r.where, &s->undo_char[r.char_storage], r.insert_length); 1326 | s->redo_char_point += r.insert_length; 1327 | } 1328 | 1329 | state->cursor = r.where + r.insert_length; 1330 | 1331 | s->undo_point++; 1332 | s->redo_point++; 1333 | } 1334 | 1335 | static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length) 1336 | { 1337 | stb_text_createundo(&state->undostate, where, 0, length); 1338 | } 1339 | 1340 | static void stb_text_makeundo_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length) 1341 | { 1342 | int i; 1343 | STB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, length, 0); 1344 | if (p) { 1345 | for (i=0; i < length; ++i) 1346 | p[i] = STB_TEXTEDIT_GETCHAR(str, where+i); 1347 | } 1348 | } 1349 | 1350 | static void stb_text_makeundo_replace(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length) 1351 | { 1352 | int i; 1353 | STB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, old_length, new_length); 1354 | if (p) { 1355 | for (i=0; i < old_length; ++i) 1356 | p[i] = STB_TEXTEDIT_GETCHAR(str, where+i); 1357 | } 1358 | } 1359 | 1360 | // reset the state to default 1361 | static void stb_textedit_clear_state(STB_TexteditState *state, int is_single_line) 1362 | { 1363 | state->undostate.undo_point = 0; 1364 | state->undostate.undo_char_point = 0; 1365 | state->undostate.redo_point = STB_TEXTEDIT_UNDOSTATECOUNT; 1366 | state->undostate.redo_char_point = STB_TEXTEDIT_UNDOCHARCOUNT; 1367 | state->select_end = state->select_start = 0; 1368 | state->cursor = 0; 1369 | state->has_preferred_x = 0; 1370 | state->preferred_x = 0; 1371 | state->cursor_at_end_of_line = 0; 1372 | state->initialized = 1; 1373 | state->single_line = (unsigned char) is_single_line; 1374 | state->insert_mode = 0; 1375 | state->row_count_per_page = 0; 1376 | } 1377 | 1378 | // API initialize 1379 | static void stb_textedit_initialize_state(STB_TexteditState *state, int is_single_line) 1380 | { 1381 | stb_textedit_clear_state(state, is_single_line); 1382 | } 1383 | 1384 | #if defined(__GNUC__) || defined(__clang__) 1385 | #pragma GCC diagnostic push 1386 | #pragma GCC diagnostic ignored "-Wcast-qual" 1387 | #endif 1388 | 1389 | static int stb_textedit_paste(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE const *ctext, int len) 1390 | { 1391 | return stb_textedit_paste_internal(str, state, (STB_TEXTEDIT_CHARTYPE *) ctext, len); 1392 | } 1393 | 1394 | #if defined(__GNUC__) || defined(__clang__) 1395 | #pragma GCC diagnostic pop 1396 | #endif 1397 | 1398 | #endif//STB_TEXTEDIT_IMPLEMENTATION 1399 | 1400 | /* 1401 | ------------------------------------------------------------------------------ 1402 | This software is available under 2 licenses -- choose whichever you prefer. 1403 | ------------------------------------------------------------------------------ 1404 | ALTERNATIVE A - MIT License 1405 | Copyright (c) 2017 Sean Barrett 1406 | Permission is hereby granted, free of charge, to any person obtaining a copy of 1407 | this software and associated documentation files (the "Software"), to deal in 1408 | the Software without restriction, including without limitation the rights to 1409 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 1410 | of the Software, and to permit persons to whom the Software is furnished to do 1411 | so, subject to the following conditions: 1412 | The above copyright notice and this permission notice shall be included in all 1413 | copies or substantial portions of the Software. 1414 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1415 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1416 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1417 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1418 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 1419 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 1420 | SOFTWARE. 1421 | ------------------------------------------------------------------------------ 1422 | ALTERNATIVE B - Public Domain (www.unlicense.org) 1423 | This is free and unencumbered software released into the public domain. 1424 | Anyone is free to copy, modify, publish, use, compile, sell, or distribute this 1425 | software, either in source code form or as a compiled binary, for any purpose, 1426 | commercial or non-commercial, and by any means. 1427 | In jurisdictions that recognize copyright laws, the author or authors of this 1428 | software dedicate any and all copyright interest in the software to the public 1429 | domain. We make this dedication for the benefit of the public at large and to 1430 | the detriment of our heirs and successors. We intend this dedication to be an 1431 | overt act of relinquishment in perpetuity of all present and future rights to 1432 | this software under copyright law. 1433 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1434 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1435 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1436 | AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 1437 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 1438 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 1439 | ------------------------------------------------------------------------------ 1440 | */ 1441 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // 3 | // Copyright(c) 2023 Shing Liu 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files(the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions : 11 | // 12 | // The above copyright notice and this permission notice shall be included in all 13 | // copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | // SOFTWARE. 22 | 23 | #include "GlfwOcctView.h" 24 | 25 | int main(int, char**) 26 | { 27 | GlfwOcctView anApp; 28 | 29 | try 30 | { 31 | anApp.run(); 32 | } 33 | catch (const std::runtime_error& theError) 34 | { 35 | std::cerr << theError.what() << std::endl; 36 | return EXIT_FAILURE; 37 | } 38 | 39 | return EXIT_SUCCESS; 40 | } 41 | -------------------------------------------------------------------------------- /occt-imgui.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eryar/OcctImgui/c26d84d70c365a4077a26911b7d3df79188cd901/occt-imgui.png -------------------------------------------------------------------------------- /premake5.lua: -------------------------------------------------------------------------------- 1 | workspace "OcctImgui" 2 | configurations {"Debug", "Release"} 3 | system "Windows" 4 | platforms {"Win64"} 5 | architecture "X64" 6 | language "C++" 7 | 8 | project "OcctImgui" 9 | kind "ConsoleApp" 10 | language "C++" 11 | targetdir "build/bin/%{cfg.buildcfg}" 12 | objdir "build/obj/%{cfg.buildcfg}" 13 | 14 | files { "**.h", "**.cpp"} 15 | 16 | -- Header files. 17 | includedirs 18 | { 19 | "D:/OpenCASCADE-7.7.0/opencascade-7.7.0/inc", 20 | "D:/glfw-3.3.8/include" 21 | } 22 | 23 | -- Library files. 24 | links 25 | { 26 | "TKernel", "TKMath", "TKG2d", "TKG3d", "TKGeomBase", "TKGeomAlgo", "TKBRep", "TKTopAlgo", "TKPrim", "TKMesh", "TKService", "TKOpenGl", "TKV3d", 27 | "glfw3" 28 | } 29 | 30 | filter "configurations:Debug" 31 | defines { "DEBUG" } 32 | symbols "On" 33 | 34 | libdirs 35 | { 36 | "D:/OpenCASCADE-7.7.0/opencascade-7.7.0/win64/vc14/libd", 37 | "D:/glfw-3.3.8/libd" 38 | } 39 | 40 | debugenvs 41 | { 42 | "path=%path%;D:/OpenCASCADE-7.7.0/opencascade-7.7.0/win64/vc14/bind" 43 | } 44 | 45 | filter "configurations:Release" 46 | defines { "NDEBUG" } 47 | symbols "Off" 48 | optimize "On" 49 | libdirs 50 | { 51 | "D:/OpenCASCADE-7.7.0/opencascade-7.7.0/win64/vc14/lib", 52 | "D:/glfw-3.3.8/lib" 53 | } 54 | 55 | debugenvs 56 | { 57 | "path=%path%;D:/OpenCASCADE-7.7.0/opencascade-7.7.0/win64/vc14/bin" 58 | } --------------------------------------------------------------------------------