[osg-users] hook osg into other window

Sebastian Messerschmidt sebastian.messerschmidt at gmx.de
Mon Aug 1 01:25:03 PDT 2016

Hi Han
> Hi,
> Here is my problem.
> I am writing a plugin for a 3D modeling software , sketchup, and going to draw some 3D things into the OpenGL context. But it does not have any Api for customizing the rendering. So I have to hook my rendering stuffs into its render loop, which I do not have access.
Hasn't Sketchup some sort of plugin mechanism to allow for custom rendering?
> I can get the Windows handle HWND of the top window, maybe from that I can track to which window is doing rendering and the OpenGL context.
> So here is my question,
> 1. If I get the HWND of the rendering window, can I draw additional things into it? Even if I do not have the access to the render loop, and start my own loop.
You can in general obtain the window handle and setup the OSG viewer 
associated to it. I've attached some snippet which should illustrate this.
I think you need to obtain some control over rendering order to make 
this work.

osg::ref_ptr<osg::GraphicsContext::Traits> traits = new 
     RECT rect;
     auto hwnd = HWND(window_handle);
     if (!GetWindowRect(hwnd, &rect))
         std::cout << "Error getting window rect from handle. Aborting" 
<< std::endl;
         return false;
     //TODO: get  other traits from external  window handle:
     traits->x = 0;
     traits->y = 0;
     traits->width = rect.right - rect.left;
     traits->height = rect.bottom - rect.top;
     traits->windowDecoration = false;
     traits->doubleBuffer = true;
     traits->inheritedWindowData = new 
     traits->supportsResize = true;
     traits->sampleBuffers = mConfig.mNumSamples > 0 ? 1: 0;
     traits->samples = mConfig.mNumSamples;
     traits->depth = mConfig.mDepthBits;

     osg::ref_ptr<osg::GraphicsContext> gc = 
osg::GraphicsContext::createGraphicsContext( traits.get() );
     if (gc)
         osg::ref_ptr<osg::Camera> camera = new osg::Camera;
         camera->setViewport(new osg::Viewport(0,0, traits->width, 
double(traits->width/ traits->height), 1.0, 1000.0);

         std::cout << "Error creating context from traits" << std::endl;
         return false;

> 2. Another strategy, create another window and OpenGL context, that is overlaid on to existing window, but the overlay is transparent and do not interact with user.The new context only retrieves matrix from the existing one.
You can give it try if the first method fails. But you will have to make 
sure to resize/transform the overlay window according to the "parent"s 

> Thank you!
> Cheers,
> Han
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=68226#68226
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

More information about the osg-users mailing list