openscenegraph / OpenSceneGraph

OpenSceneGraph git repository
http://www.openscenegraph.org
Other
3.25k stars 1.42k forks source link

setInheritedWindowPixelFormat trait ignored #1177

Closed personne13 closed 1 year ago

personne13 commented 1 year ago

Hello,

I have a problem when using the library in a particular setup. In my application, the OpenGL context is created using an external tool, and I want to send this context to OSG (using WindowData in inheritedWindowData trait, I set it using Windows API), so it can create a texture that I can render with native OpenGL calls. My highest achievement doing so was to encounter an error when OSG tried to create the context with the message error:GraphicsWindowWin32::setWindow() - Unable to set the inherited window pixel format.

I do not specifically want OSG to override the pixel format, so I tried setting the setInheritedWindowPixelFormat to true when passing the Traits, but it seems completely ignored. When looking in the code I did not find any usage of that boolean. Am I misunderstanding something, or am I trying to do something that does not make sense ?

PS: I also found this thread speaking about the subject: https://osg-users.openscenegraph.narkive.com/dg7ul3th/mfc-graphicswindowwin32-question-or-possible-bug-and-if-it-is-a-bug-having-a-solution

EDIT: I think this behavior has been induced by a bad revert, there seem to be a bug in MFC_OSG.cpp (the method Render is commented):

// For some reason this has to be here to avoid issue:
    // if you have multiple OSG windows up
    // and you exit one then all stop rendering

Maybe he means that OSG renders on all screens, as no context has been found, as the original context could not be created.

EDIT2: This boolean was preventing to execute the exact piece of code that raises me the error in version 2.5.3, it has been removed for some reasons

personne13 commented 1 year ago

Alright, I figured out what I was doing wrong: I'm gonna post it here for future readers: The GraphicsWindowWin32 can get an existing OpenGL context, but I had to carefully set up the Traits of the OSG context, so the pixel format OSG tries to create matches the pixel format of the existing context. So I had to set up the red, green, blue (to 4), and double buffering, so the call to the Windows API "SetPixelFormat" does not return any error, so OSG-created context is valid. The logs could have been clearer, I had to modify the source of OSG so the pixelFormatIndex that OSG was trying to set is displayed.

N.B: I still had to use the "GraphicsWindowEmbedded" for my offscreen rendering, to prevent real calls to wglMakeCurrent methods (which created twinkling in my final image, because of frequent context changes).

Hope it will help someone, I struggled big on that one.

EDIT: the setInheritedWindowPixelFormat trait is pointless in the code, maybe it would be a great idea to remove it.