Create OpenGL context on Linux, MacOS and Windows - Page 1
Forum section: electronics / general

author: radiolistener
author_rank: Super Contributor
timestamp: April 13, 2023, 07:53:15 am
content: I'm working on a cross platform library, it needs to initialize OpenGL context for a window. And I need to change FSAA setting on the fly.But I found that there is an issue to create OpenGL context with specific DepthBits/StencilBits/MultisampleSize. The problem here is that Windows and Linux platform needs a window which is initialized with specific DepthBits/StencilBits/MultisampleSize settings (pixelFormat for Windows and visual for Linux). And attempt to change pixelFormat/Visual for existing window leads to error on Windows and Linux platform.In short, create OpenGL context requires the following steps:Windows:1) Create window with RegisterClassEx, CreateWindowEx2) Select proper pixelFormat for requested DepthBits/StencilBits/MultisampleSize with ChoosePixelFormat3) Assign selected pixelFormat to a window with SetPixelFormat4) Create OpenGL context for the window with wglCreateContextAttribsARB or wglCreateContextLinux:1) Select proper visual for for requested DepthBits/StencilBits/MultisampleSize with glXChooseFBConfig, glXChooseVisual2) Create window for selected Visual with XCreateWindow with passing visual as argument3) Create OpenGL context for the window with glXCreateContextAttribsARB or glXCreateContext and passing visial or FBConfig as argumentMacOS:1) Create window2) Select proper pixelFormat for requested DepthBits/StencilBits/MultisampleSize with aglChoosePixelFormat3) Create OpenGL context for the window with aglCreateContext and passing pixelFormat as argumentSo, it looks that Linux and Windows needs to recreate window for a new MultisampleSize setting. This is bad, because it leads to the old window disappear and a new window appear. It breaks window position on the desktop and confuse user.Can someone help - is there any way to change pixelFormat/visual for the existing window on Linux and Windows platform with no need to recreate window?

author: cantata.tech
author_rank: Regular Contributor
timestamp: April 13, 2023, 08:42:47 am
content: You haven't stated what Programming language you want the answer in.Do we have to guess ?

author: radiolistener
author_rank: Super Contributor
timestamp: April 13, 2023, 09:42:13 am
content: Quote from: cantata.tech on April 13, 2023, 08:42:47 amProgramming language doesn't matter here. Code example in C is proffered for simplicity, but I don't care about it - you can write code examples in any language, I can read it with no problem in C, C#, Java, Python, Rust or any other. The question is about OpenGL context initialization which is the same for any language.I'm not sure, may be this question is too complicated for this forum, but I don't have idea where to ask for help with this, so I wrote it here. Who knows, may be someone have good knowledge about OpenGL initialization and can help.By the way I asked ChatGPT about this issue, it failed to solve it

author: Nominal Animal
author_rank: Super Contributor
timestamp: April 13, 2023, 01:29:46 pm
content: Create the new X window first, setting its properties based on the existing window (see XConfigureWindow). If you put it behind the existing window, you can delay destroying the old window until the new window has been mapped and fully drawn, in which case the replacement should be near seamless.

author: SiliconWizard
author_rank: Super Contributor
timestamp: April 13, 2023, 07:48:09 pm
content: Is there anything wrong with GLFW? Not that I don't like writing my own code too and use as few third-party libraries as possible. But getting that cross-platform may be pretty time-consuming.I've used GLFW successfully, it's relatively lightweight.Just making sure you're not missing something that could save you time. But if you still want to write your own stuff, having a look at the source code is probably going to help anyway.https://github.com/glfw/glfw

author: radiolistener
author_rank: Super Contributor
timestamp: April 14, 2023, 02:52:49 am
content: Quote from: Nominal Animal on April 13, 2023, 01:29:46 pmBut there are many things will be broken, for example window order in desktop window hierarchy, window focus, and other. It's hard to control all these things, and even more - the window may be shared with other UI code and it may not support reattach to a new window on the fly... Thats why I'm trying to find the way to reuse existing window...

author: radiolistener
author_rank: Super Contributor
timestamp: April 14, 2023, 03:06:24 am
content: Quote from: SiliconWizard on April 13, 2023, 07:48:09 pmIt doesn't provide you full control on how OpenGL context is created, so you're stick with that code which is hardcoded in GLFW and you can't do anything if GLFW implementation doesn't support it.By the way, recently I catch some strange issue with GLFW. I'm using Arch Linux with KDE Plasma and installed SDR++ for testing (SDRPlusPlus). It failed to initialize OpenGL with default glfw-wayland library. I have no idea why, it just show error, something about wrong sequence inside some library. I spent some time to google solution for this issue and after some time I found recommendation to remove glfw-wayland and install glfw-x11. I tried it and the issue is solved.So, if you're use GLFW, you're stick with it's hardcoded behavior and bugs and cannot control fine tuning settings...

author: SiliconWizard
author_rank: Super Contributor
timestamp: April 14, 2023, 03:22:16 am
content: I get that. Was just making sure.And yes not everything is perfectly smooth, although it's still a great library to start with. For instance, it fails to initialize via VNC, while I can use OpenGL via the same VNC connection in other apps.But again, maybe the source code can still help getting started with your own stuff.

author: Nominal Animal
author_rank: Super Contributor
timestamp: April 14, 2023, 03:58:49 am
content: Quote from: radiolistener on April 14, 2023, 02:52:49 amWindow order, hierarchy and focus is configurable via XConfigureWindow and EWMH. It may be hard, but it is your only option, because X11 does not support changing the Visual of an existing window.

author: radiolistener
author_rank: Super Contributor
timestamp: April 18, 2023, 12:38:10 pm
content: I have an idea to not create OpenGL context for the main window, but create sub-window which fill entire parent area and create OpenGL context for a sub-window. Then if pixelFormat change is required we can destroy sub-window and create new one, so the main window remains the same...Does someone tried it?

author: Nominal Animal
author_rank: Super Contributor
timestamp: April 18, 2023, 03:02:52 pm
content: Quote from: radiolistener on April 18, 2023, 12:38:10 pmSure, some windowing toolkits do this for OpenGL and multimedia player widgets. See e.g.GtkGLAreain Gtk+3. It is common for an apparently single window to actually contain undecorated borderless child windows with X11.