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

On Wed, Oct 12, 2022 at 9:25 AM [email protected] <
[email protected]> wrote:

> 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 ....
>
> Il giorno martedì 11 ottobre 2022 alle 15:13:45 UTC+2 [email protected]
> ha scritto:
>
>> 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
>>
>> On Tue, Oct 11, 2022 at 9:08 AM Jeremy Moles <[email protected]> wrote:
>> >
>> > 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 [email protected]
>> > <[email protected]> wrote:
>> > >
>> > > UP!
>> > >
>> > > can anyone help me?
>> > >
>> > > Il giorno venerdì 7 ottobre 2022 alle 11:12:33 UTC+2
>> [email protected] ha scritto:
>> > >>
>> > >> 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
>> > >>
>> > >>
>> > > --
>> > > 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 [email protected].
>> > > To view this discussion on the web visit
>> https://groups.google.com/d/msgid/osg-users/bfa6a367-4d2c-4e29-b296-612d40165fd0n%40googlegroups.com.
>>
>>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/osg-users/61e45788-88df-408b-8bcf-e61492b99242n%40googlegroups.com
> <https://groups.google.com/d/msgid/osg-users/61e45788-88df-408b-8bcf-e61492b99242n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/osg-users/CAHX4H_Ya9BH0kN%3Dk4CTtNa_sZAJiqxMY6PGTHDhku_o8N98WNQ%40mail.gmail.com.

Reply via email to