Skip to main content
Up and Coming
December 29, 2015

[0.8 PC GL] copy texture to TextureSet

hi all,

back in the 0.5 days before textureSets, one could simply set one's own textureID to eyeTex.OGL.TexId and call it a day. Could anyone please offer a code snippet/workflow description of how to do something similar with 0.8 in OpenGL?

what I tried:
- glBindTexture (my own texture (GLuint handle) with a combined left/right image)
- get ovrGLTexture* tex = (ovrGLTexture*)&pTextureSet->Textures[pTextureSet->CurrentIndex]
- glCopyTexSubImage2D (target tex->OGL.TexID)

regards

    30 replies

    kojack
    Honored Contributor
    January 7, 2016
    "ccrooks" wrote:
    @kojack For OGRE OpenGL specifically I was just hacking on this the past two days.

    I got it to work (in OGRE 1.9) by linking directly against the OpenGL render system, and utilizing Ogre::GLTextureBuffer, which can take an OpenGL texture id value (provided by the Oculus SDK), and acts as a render buffer, from which you can call getRenderTarget(), and proceed with the normal OGRE render-to-texture workflow (add viewport with a camera, call update() on the viewport, etc...).

    I just hacked it in, but it *appears* to be working.

    Cool, I'll give it a shot tomorrow.
    Author: Oculus Monitor , Auto Oculus Touch , Forum Dark Mode , Phantom Touch Remover , X-Plane Fixer Hardware: Threadripper 1950x, MSI Gaming Trio 2080TI, Asrock X399 Taich Headsets: Wrap 1200VR, DK1, DK2, CV1, Rift-S, GearVR, Go, Quest, Quest 2, Reverb G2, Quest 3
    kojack
    Honored Contributor
    January 7, 2016
    Using a GL3PlusTextureBuffer to inject the oculus texture id I was able to get 1 frame to render to the rift directly. But no more than that. Many opengl warnings and errors showed up in the log.

    I still can't find a good way to copy textures opengl that works. Ogre has blit methods for texture to texture, but they are for full ogre textures, not external opengl texture ids.
    6:30am, enough debugging for now, time for bed.
    Author: Oculus Monitor , Auto Oculus Touch , Forum Dark Mode , Phantom Touch Remover , X-Plane Fixer Hardware: Threadripper 1950x, MSI Gaming Trio 2080TI, Asrock X399 Taich Headsets: Wrap 1200VR, DK1, DK2, CV1, Rift-S, GearVR, Go, Quest, Quest 2, Reverb G2, Quest 3
    Inspiring
    January 8, 2016
    "kojack" wrote:
    I still can't find a good way to copy textures opengl that works. Ogre has blit methods for texture to texture, but they are for full ogre textures, not external opengl texture ids.
    6:30am, enough debugging for now, time for bed.


    Why not using GLext_frame_buffer_object to make a glBlitFramebuffer?
    Inspiring
    January 8, 2016
    "thewhiteambit" wrote:
    "kojack" wrote:
    I still can't find a good way to copy textures opengl that works. Ogre has blit methods for texture to texture, but they are for full ogre textures, not external opengl texture ids.
    6:30am, enough debugging for now, time for bed.


    Why not using GLext_frame_buffer_object to make a glBlitFramebuffer?


    I don't know much about Ogre, but the impression I've gotten from the thread is that it doesn't seem to want to let you do low level stuff. This isn't too surprising since it has multiple rendering back-ends. I'm all for object oriented abstractions, but they should always let you get down to the level of 'give me the fucking texture ID so I can call the copy functions myself'.
    Brad Davis - Developer for High Fidelity Co-author of Oculus Rift in Action
    kojack
    Honored Contributor
    January 8, 2016
    "jherico" wrote:
    I'm all for object oriented abstractions, but they should always let you get down to the level of 'give me the fucking texture ID so I can call the copy functions myself'.

    It does give me the texture id. Ogre doesn't want to deal with external (made by oculus) textures but it lets me access the low level details of it's own texture. I can do the same thing in directx11, Ogre gives me the dx11 device context and texture pointer, I can easily copy to the oculus texture. But every copy attempt I've made in opengl fails with vague error results. Googling around doesn't show me anything useful that works.

    I'm not a gl programmer, I'm not used to the way it works (I've used directx for 18 years, teach directx, etc. But I prefer to work one level up from that). So I might be doing something wrong.
    Author: Oculus Monitor , Auto Oculus Touch , Forum Dark Mode , Phantom Touch Remover , X-Plane Fixer Hardware: Threadripper 1950x, MSI Gaming Trio 2080TI, Asrock X399 Taich Headsets: Wrap 1200VR, DK1, DK2, CV1, Rift-S, GearVR, Go, Quest, Quest 2, Reverb G2, Quest 3
    Inspiring
    January 9, 2016
    "kojack" wrote:
    But every copy attempt I've made in opengl fails with vague error results. Googling around doesn't show me anything useful that works.

    I'm not a gl programmer, I'm not used to the way it works (I've used directx for 18 years, teach directx, etc. But I prefer to work one level up from that). So I might be doing something wrong.


    Can you post the code you're using to try to do the copy?
    Brad Davis - Developer for High Fidelity Co-author of Oculus Rift in Action
    kojack
    Honored Contributor
    January 16, 2016
    Yay, got opengl working now. (No, I haven't struggled with this for the last week, I've been busy with other things. Just started back on it an hour ago).

    I'm using glBlitFramebuffer.
    Ogre's GL3+ renderer provides two temporary pre-made FBOs, I attach the Ogre render target to one FBO, the Oculus texture to the other FBO, then blit between them.


    Some of the code I'm using:
    // fboMan is pointing to the Ogre frame buffer object manager.
    // Set one fbo as read, the other as write.
    glBindFramebuffer(GL_READ_FRAMEBUFFER, fboMan->getTemporaryFBO(0));
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboMan->getTemporaryFBO(1));

    // Left Eye
    // srcid is the Ogre render target texture.
    // dstid is the Oculus texture from the texture set.
    glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, srcid, 0);
    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, dstid, 0);
    // Do the copy.
    glBlitFramebuffer(0, 0, g_idealTextureSize.w - 1, g_idealTextureSize.h - 1, 0, 0, g_idealTextureSize.w - 1, g_idealTextureSize.h - 1, GL_COLOR_BUFFER_BIT, GL_NEAREST);

    // Right Eye
    // srcid2 is the Ogre render target texture.
    // dstid2 is the Oculus texture from the texture set.
    glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, srcid2, 0);
    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, dstid2, 0);
    // Do the copy.
    glBlitFramebuffer(0, 0, g_idealTextureSize.w - 1, g_idealTextureSize.h - 1, 0, 0, g_idealTextureSize.w - 1, g_idealTextureSize.h - 1, GL_COLOR_BUFFER_BIT, GL_NEAREST);


    Don't forget to the cycle the current texture index in the texture sets, if you don't the glBlitFramebuffer will fail (at least it did for me).

    Of course you'll need to deal with the FBO creation yourself, Ogre does that for me.
    Author: Oculus Monitor , Auto Oculus Touch , Forum Dark Mode , Phantom Touch Remover , X-Plane Fixer Hardware: Threadripper 1950x, MSI Gaming Trio 2080TI, Asrock X399 Taich Headsets: Wrap 1200VR, DK1, DK2, CV1, Rift-S, GearVR, Go, Quest, Quest 2, Reverb G2, Quest 3
    Participant
    January 18, 2016
    "kojack" wrote:
    Yay, got opengl working now. (No, I haven't struggled with this for the last week, I've been busy with other things. Just started back on it an hour ago).

    I'm using glBlitFramebuffer.
    Ogre's GL3+ renderer provides two temporary pre-made FBOs, I attach the Ogre render target to one FBO, the Oculus texture to the other FBO, then blit between them.


    Some of the code I'm using:
    // fboMan is pointing to the Ogre frame buffer object manager.
    // Set one fbo as read, the other as write.
    glBindFramebuffer(GL_READ_FRAMEBUFFER, fboMan->getTemporaryFBO(0));
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboMan->getTemporaryFBO(1));

    // Left Eye
    // srcid is the Ogre render target texture.
    // dstid is the Oculus texture from the texture set.
    glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, srcid, 0);
    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, dstid, 0);
    // Do the copy.
    glBlitFramebuffer(0, 0, g_idealTextureSize.w - 1, g_idealTextureSize.h - 1, 0, 0, g_idealTextureSize.w - 1, g_idealTextureSize.h - 1, GL_COLOR_BUFFER_BIT, GL_NEAREST);

    // Right Eye
    // srcid2 is the Ogre render target texture.
    // dstid2 is the Oculus texture from the texture set.
    glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, srcid2, 0);
    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, dstid2, 0);
    // Do the copy.
    glBlitFramebuffer(0, 0, g_idealTextureSize.w - 1, g_idealTextureSize.h - 1, 0, 0, g_idealTextureSize.w - 1, g_idealTextureSize.h - 1, GL_COLOR_BUFFER_BIT, GL_NEAREST);


    Don't forget to the cycle the current texture index in the texture sets, if you don't the glBlitFramebuffer will fail (at least it did for me).

    Of course you'll need to deal with the FBO creation yourself, Ogre does that for me.


    does dstid mean that tex->OGL.TextId
    which is generated by "CreateSwapTextureSetGL" ?
    kojack
    Honored Contributor
    January 18, 2016
    "godjilla" wrote:
    does dstid mean that tex->OGL.TextId
    which is generated by "CreateSwapTextureSetGL" ?

    Yep.
    // Cycle through textures
    g_textureSet[0]->CurrentIndex = (g_textureSet[0]->CurrentIndex + 1) % g_textureSet[0]->TextureCount;
    g_textureSet[1]->CurrentIndex = (g_textureSet[0]->CurrentIndex + 1) % g_textureSet[0]->TextureCount;
    // Get current texture ids
    GLuint dstid = ((ovrGLTexture *)g_textureSet[0]->Textures)[g_textureSet[0]->CurrentIndex].OGL.TexId;
    GLuint dstid2 = ((ovrGLTexture *)g_textureSet[1]->Textures)[g_textureSet[1]->CurrentIndex].OGL.TexId;
    Author: Oculus Monitor , Auto Oculus Touch , Forum Dark Mode , Phantom Touch Remover , X-Plane Fixer Hardware: Threadripper 1950x, MSI Gaming Trio 2080TI, Asrock X399 Taich Headsets: Wrap 1200VR, DK1, DK2, CV1, Rift-S, GearVR, Go, Quest, Quest 2, Reverb G2, Quest 3
    New Participant
    February 17, 2016
    Hey this was a huge help to me. Big thanks. I was trying to write directly into the texture specified in the ovr layers as well. Now my code works!