Page 1 of 1

Get OpenGLgadget() context number?

Posted: Thu Jan 22, 2026 2:51 pm
by naf
The setter

Code: Select all

#PB_OpenGL_SetContext
is useful, but there is no getter, like maybe #PB_OpenGL_GetContext.

Code: Select all

GetGadgetAttribute(openGLgadgetNum, #PB_OpenGL_SetContext )
only returns 0, not working.
The same question came when using wegGL / webView: viewtopic.php?p=636229#p636229
In practice, the first opened OpenGLgadget() gets context 0, the second one has context = 1, and so on. It works. But becomes unusable when the user has the freedom to dynamically create and and close windows and their OpenGLgadget()'s. The context number is needed after dynamic creation of an OpenGLgadget().
Is there a quick workaround by importing something from a library and skipping the standard GetGadgetAttribute() ?
Thanks. (PB v.6.21 on linux Zorin with Wayland, msi GP72 2QE Leopard Pro)

Re: Get OpenGLgadget() context number?

Posted: Fri Jan 23, 2026 8:30 am
by pjay
I've used this code, but only tested on Windows:

Code: Select all

Procedure glGetCurrentContext()
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows : ProcedureReturn wglGetCurrentContext_()
    CompilerCase #PB_OS_Linux : ProcedureReturn glxGetCurrentContext_()
    CompilerCase #PB_OS_MacOS : ProcedureReturn CGLGetCurrentContext_()
  CompilerEndSelect
EndProcedure
 
OpenWindow(0,0,0,1280,480,"")
OpenGLGadget(0,0,0,640,480) : Debug glGetCurrentContext()
OpenGLGadget(1,640,0,640,480) : Debug glGetCurrentContext()

SetGadgetAttribute(0, #PB_OpenGL_SetContext, 1) : Debug glGetCurrentContext()
SetGadgetAttribute(1, #PB_OpenGL_SetContext, 1) : Debug glGetCurrentContext()

Re: Get OpenGLgadget() context number?

Posted: Wed Jan 28, 2026 12:28 pm
by naf
Nice trick! The glX functions are for the X11server though, don't work with wayland. I find no equivalent for wayland, or I would need to use the restricted ES (for Embedded Systems), with EGL.
I will try using a single context, hidden, resizing it before each draw to an offscreen framebuffer/renderbuffer, and finally transfering the image to the screen as an image gadget. Anyone is welcome to try it before me. See how much the frame rate is impacted.
For Wayland, you can use EGL as an alternative to glXGetCurrentContext(). Specifically, using libGLESv2 instead of libGL can help avoid issues related to glX calls. GitHub
X11 and Wayland differ significantly in how they manage OpenGL contexts. X11 uses a client-server model where applications send rendering commands to the X server, which handles the OpenGL context. In contrast, Wayland applications manage their own OpenGL contexts directly, communicating with the compositor for final display. This direct approach in Wayland reduces latency and improves performance by eliminating the need for an intermediary server. glukhov.org abhik.ai

Re: Get OpenGLgadget() context number?

Posted: Wed Jan 28, 2026 8:00 pm
by mk-soft
As I know, the OpenGLGadget does not work under Wayland because Wayland does not have an interface to OpenGL ...