Share texture between OpenGL context and GraphicContext

130 views
Skip to first unread message

marco....@elmansrl.eu

unread,
Oct 7, 2022, 5:12:33 AM10/7/22
to OpenSceneGraph Users
Good morning everyone, 

 I have an application, based on a third party library, which generates a texture within an OpenGL context.

My goal is to have the texture available in the OSG GraphicContext.

I specify that I have the OpenGL context handle and the ID of the texture available.

What are the roads?

Thanks


Message has been deleted

Jeremy Moles

unread,
Oct 11, 2022, 9:08:12 AM10/11/22
to osg-...@googlegroups.com
Do you control the creation of the "other" context, the one NOT created by OSG?

Some quick googling revealed that the function "eglCreateContext" does
accept a "secondary" context, but you likely aren't using this inside
OSG directly unless you're building something for iOS/Android. I'll
try looking into it more later today, as I'm interested in the answer
myself.

Jeremy Moles, AlphaPixel LLC
https://alphapixeldev.com

On Tue, Oct 11, 2022 at 6:14 AM marco....@elmansrl.eu
<marco....@elmansrl.eu> wrote:
>
> UP!
>
> can anyone help me?
> --
> You received this message because you are subscribed to the Google Groups "OpenSceneGraph Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to osg-users+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/osg-users/bfa6a367-4d2c-4e29-b296-612d40165fd0n%40googlegroups.com.

Jeremy Moles

unread,
Oct 11, 2022, 9:13:45 AM10/11/22
to osg-...@googlegroups.com
So, it looks like osg::GraphicsContext::Traits has a member called
"sharedContext", and this is where you'll want to look. As I mentioned
previously, I may have more time to investigate later, but this should
get you going. There are more than a few examples inside the
"examples" directory that show how to work with the OSG Context/Traits
directly.

Jeremy Moles, AlphaPixel LLC
https://alphapixeldev.com

marco....@elmansrl.eu

unread,
Oct 12, 2022, 3:25:34 AM10/12/22
to OpenSceneGraph Users
First of all thanks for the reply, 

for the sharedContext, I tried to understand (scrolling through the osg examples) which is the best way to set it, but I didn't understand, so below I share my code.

1) starting from window handle, I initializated the traits...
HWND hwnd = (HWND)handle;
osg::ref_ptr<osg::Referenced> windata = new osgViewer::GraphicsWindowWin32::WindowData(hwnd);

traits = new osg::GraphicsContext::Traits;
    traits->readDISPLAY();
    if (traits->displayNum < 0) {
        traits->displayNum = 0;
    }

    traits->windowName = "ConsoleVTS - module 3D";
    traits->screenNum = 0;
    traits->x = x;
    traits->y = y;
    traits->width = width;
    traits->height = height;
    traits->alpha = ds->getMinimumNumAlphaBits();
    traits->stencil = ds->getMinimumNumStencilBits();
    traits->windowDecoration = false;
    traits->doubleBuffer = true;
    traits->sharedContext = 0;
    traits->sampleBuffers = ds->getMultiSamples();
    traits->samples = 4;
    traits->inheritedWindowData = windata;

    if (ds->getStereo()) {

        switch (ds->getStereoMode()) {

        case osg::DisplaySettings::QUAD_BUFFER:
            traits->quadBufferStereo = true;
            break;

        case(osg::DisplaySettings::VERTICAL_INTERLACE):
        case(osg::DisplaySettings::CHECKERBOARD):
        case(osg::DisplaySettings::HORIZONTAL_INTERLACE):
            traits->stencil = 8;
            break;

        default:
            break;
        }
    }

2) from traits I created graphic context:
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());

3) camera creation:
osg::ref_ptr <osg::Camera> camera = new osg::Camera;
camera->setGraphicsContext(gc);

Then, the problem is that as described by the code, osg will generate a GraphicContext with its own contextID. 

My problem is that, I have an OpenGL context (created externally to OSG) which has its own contextID and inside which I have a texture with an ID. Here, I would like to connect to this context through its ID and take the texture with OSG.

So, if this can be done with the Traits sharedContext, I don't understand how to set it up ....

SkillCheck

unread,
Oct 12, 2022, 8:15:35 AM10/12/22
to osg-...@googlegroups.com
Hello Marco,

I had almost exactly the same situation as you, the third party library that was involved was Qt, another team was using Qt to draw into an OpenGL Texture (generated with plain old glGenTextures), created in their own QOpenGLContext. The id was then passed to my OSG-based library and I would use that texture to map it onto a 3d object.

The solution I made was fairly straightforward, I don't know if it is the "correct" way to do it, but it worked/works. Anyway, basically just derive a class from osg::Texture2D which just has a member of the texture id, and then bind that in an override of the apply() method:

<code>
class Texture2DRef : public osg::Texture2D
{
public:
    explicit Texture2DRef( GLuint textureId = ~0 ) : osg::Texture2D(), mTextureId(textureId() {}
   Texture2DRef( Texture2DRef const & orig osg::CopyOp const & op = ... ) { ... }
    META_StateAttribute( ... )
    get/setTextureId(...) { ... }

    void apply( osg::State & state ) const override
    {
      if ( mTexture == ~0 ) {
        return;
      }
      glBindTexture( GL_TEXTURE_2D, mTextureId );
    }

private:
    GLuint mTextureId;
};
</code>

TBH, not sure if what I'm doing here is legal OpenGL. The logic for creating the (shared) contexts between Qt and OSG was developed by the end customer/integrator and complicated, but there was no guarantee that the contexts were shared or the same. I'm curious if anyone out there knows if this is a valid solution or possibly the case of different contexts was just never tested :(

HTH,
Chris

Trajce Nikolov NICK

unread,
Oct 17, 2022, 12:46:05 PM10/17/22
to osg-...@googlegroups.com
Hi!

If you have the handle of the texture created by 3rd party, you can create osg::Texture via osg::TextureObject. I dont have the code in front of me but something like this

osg::TextureObject* obj = new osg::TextureObject(handle);
obj->setAllocated(true);
osg::Texture2D* texture = new osg::Texture2D;
texture->setTextureObject(obj);

On Tue, Oct 11, 2022 at 1:14 PM marco....@elmansrl.eu <marco....@elmansrl.eu> wrote:
UP!

can anyone help me?

Il giorno venerdì 7 ottobre 2022 alle 11:12:33 UTC+2 marco....@elmansrl.eu ha scritto:

--
You received this message because you are subscribed to the Google Groups "OpenSceneGraph Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to osg-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/osg-users/bfa6a367-4d2c-4e29-b296-612d40165fd0n%40googlegroups.com.


--
trajce nikolov nick
Reply all
Reply to author
Forward
0 new messages