Author Topic: Create OpenGL context on Linux, MacOS and Windows  (Read 1862 times)

0 Members and 1 Guest are viewing this topic.

Offline radiolistenerTopic starter

  • Super Contributor
  • ***
  • Posts: 3981
  • Country: ua
Create OpenGL context on Linux, MacOS and Windows
« on: April 13, 2023, 07:53:15 am »
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, CreateWindowEx
2) Select proper pixelFormat for requested DepthBits/StencilBits/MultisampleSize with ChoosePixelFormat
3) Assign selected pixelFormat to a window with SetPixelFormat
4) Create OpenGL context for the window with wglCreateContextAttribsARB or wglCreateContext

Linux:
1) Select proper visual for for requested DepthBits/StencilBits/MultisampleSize with glXChooseFBConfig, glXChooseVisual
2) Create window for selected Visual with XCreateWindow with passing visual as argument
3) Create OpenGL context for the window with  glXCreateContextAttribsARB or glXCreateContext and passing visial or FBConfig as argument

MacOS:
1) Create window
2) Select proper pixelFormat for requested DepthBits/StencilBits/MultisampleSize with aglChoosePixelFormat
3) Create OpenGL context for the window with aglCreateContext and passing pixelFormat as argument


So, 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?
 

Offline cantata.tech

  • Regular Contributor
  • *
  • Posts: 75
  • Country: au
Re: Create OpenGL context on Linux, MacOS and Windows
« Reply #1 on: April 13, 2023, 08:42:47 am »
You haven't stated what Programming language you want the answer in.

Do we have to guess ?
 

Offline radiolistenerTopic starter

  • Super Contributor
  • ***
  • Posts: 3981
  • Country: ua
Re: Create OpenGL context on Linux, MacOS and Windows
« Reply #2 on: April 13, 2023, 09:42:13 am »
You haven't stated what Programming language you want the answer in.

Do we have to guess ?

Programming 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 :)
« Last Edit: April 13, 2023, 09:52:47 am by radiolistener »
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6836
  • Country: fi
    • My home page and email address
Re: Create OpenGL context on Linux, MacOS and Windows
« Reply #3 on: April 13, 2023, 01:29:46 pm »
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.

 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 15254
  • Country: fr
Re: Create OpenGL context on Linux, MacOS and Windows
« Reply #4 on: April 13, 2023, 07:48:09 pm »
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
 

Offline radiolistenerTopic starter

  • Super Contributor
  • ***
  • Posts: 3981
  • Country: ua
Re: Create OpenGL context on Linux, MacOS and Windows
« Reply #5 on: April 14, 2023, 02:52:49 am »
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.

But 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...
 

Offline radiolistenerTopic starter

  • Super Contributor
  • ***
  • Posts: 3981
  • Country: ua
Re: Create OpenGL context on Linux, MacOS and Windows
« Reply #6 on: April 14, 2023, 03:06:24 am »
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

It 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...
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 15254
  • Country: fr
Re: Create OpenGL context on Linux, MacOS and Windows
« Reply #7 on: April 14, 2023, 03:22:16 am »
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.
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6836
  • Country: fi
    • My home page and email address
Re: Create OpenGL context on Linux, MacOS and Windows
« Reply #8 on: April 14, 2023, 03:58:49 am »
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.

But 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...
Window 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.
 

Offline radiolistenerTopic starter

  • Super Contributor
  • ***
  • Posts: 3981
  • Country: ua
Re: Create OpenGL context on Linux, MacOS and Windows
« Reply #9 on: April 18, 2023, 12:38:10 pm »
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?
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6836
  • Country: fi
    • My home page and email address
Re: Create OpenGL context on Linux, MacOS and Windows
« Reply #10 on: April 18, 2023, 03:02:52 pm »
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?
Sure, some windowing toolkits do this for OpenGL and multimedia player widgets.  See e.g. GtkGLArea in Gtk+3.  It is common for an apparently single window to actually contain undecorated borderless child windows with X11.
 
The following users thanked this post: SiliconWizard, radiolistener


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf