//----------------------------------------------------------------------------//
void GLES2Texture::blitToMemory(void* targetData)
{
    /* OpenGL ES 3.1 or below doesn't support
       "glGetTexImage"/"glGetCompressedTexImage", so we need to emulate it
       using "glReadPixels". */

    GLint old_pack;
    glGetIntegerv(GL_PACK_ALIGNMENT, &old_pack);
    GLuint texture_framebuffer(0);
    GLint framebuffer_old(0), pack_alignment_old(0);
    glGenFramebuffers(1, &texture_framebuffer);
    GLenum framebuffer_target(0), framebuffer_param(0);
    framebuffer_param = GL_READ_FRAMEBUFFER_BINDING;
    framebuffer_target = GL_READ_FRAMEBUFFER;
    glGetIntegerv(framebuffer_param, &framebuffer_old);
    glBindFramebuffer(framebuffer_target, texture_framebuffer);
    glFramebufferTexture2D(framebuffer_target, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
                           d_ogltexture, 0);
    GLint read_buffer_old(0), pixel_pack_buffer_old(0);
    glGetIntegerv(GL_READ_BUFFER, &read_buffer_old);
    glReadBuffer(GL_COLOR_ATTACHMENT0);
    glGetIntegerv(GL_PIXEL_PACK_BUFFER_BINDING, &pixel_pack_buffer_old);
    glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
    glGetIntegerv(GL_PACK_ALIGNMENT, &pack_alignment_old);
    glPixelStorei(GL_PACK_ALIGNMENT, 1);
    glReadPixels(0, 0, static_cast<GLsizei>(d_dataSize.d_width),
                 static_cast<GLsizei>(d_dataSize.d_height), GL_RGBA, GL_UNSIGNED_BYTE,
                 targetData);
    glPixelStorei(GL_PACK_ALIGNMENT, pack_alignment_old);
    glBindBuffer(GL_PIXEL_PACK_BUFFER, pixel_pack_buffer_old);
    glReadBuffer(read_buffer_old);
    glBindFramebuffer(framebuffer_target, framebuffer_old);
    glDeleteFramebuffers(1, &texture_framebuffer);
}
Example #2
0
//----------------------------------------------------------------------------//
void OpenGL1Texture::blitToMemory(void* targetData)
{
    if (OpenGLInfo::getSingleton().isUsingOpenglEs())
    {
        /* OpenGL ES 3.1 or below doesn't support
        "glGetTexImage"/"glGetCompressedTexImage", so we need to emulate it
        using "glReadPixels". */

        GLint old_pack;
        glGetIntegerv(GL_PACK_ALIGNMENT, &old_pack);
        GLuint texture_framebuffer(0);
        GLint framebuffer_old(0), pack_alignment_old(0);
        glGenFramebuffers(1, &texture_framebuffer);
        GLenum framebuffer_target(0), framebuffer_param(0);
        if (OpenGLInfo::getSingleton()
            .isSeperateReadAndDrawFramebufferSupported())
        {
            framebuffer_param = GL_READ_FRAMEBUFFER_BINDING;
            framebuffer_target = GL_READ_FRAMEBUFFER;
        }
        else
        {
            framebuffer_param = GL_FRAMEBUFFER_BINDING;
            framebuffer_target = GL_FRAMEBUFFER;
        }
        glGetIntegerv(framebuffer_param, &framebuffer_old);
        glBindFramebuffer(framebuffer_target, texture_framebuffer);
        glFramebufferTexture2D(framebuffer_target, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, d_ogltexture, 0);
        GLint read_buffer_old(0), pixel_pack_buffer_old(0);
        if (OpenGLInfo::getSingleton().isReadBufferSupported())
        {
            glGetIntegerv(GL_READ_BUFFER, &read_buffer_old);
            glReadBuffer(GL_COLOR_ATTACHMENT0);
            glGetIntegerv(GL_PIXEL_PACK_BUFFER_BINDING, &pixel_pack_buffer_old);
            glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
        }
        glGetIntegerv(GL_PACK_ALIGNMENT, &pack_alignment_old);
        glPixelStorei(GL_PACK_ALIGNMENT, 1);
        glReadPixels(0, 0, static_cast<GLsizei>(d_dataSize.d_width),
            static_cast<GLsizei>(d_dataSize.d_height), GL_RGBA, GL_UNSIGNED_BYTE, targetData);
        glPixelStorei(GL_PACK_ALIGNMENT, pack_alignment_old);
        if (OpenGLInfo::getSingleton().isReadBufferSupported())
        {
            glBindBuffer(GL_PIXEL_PACK_BUFFER, pixel_pack_buffer_old);
            glReadBuffer(read_buffer_old);
        }
        glBindFramebuffer(framebuffer_target, framebuffer_old);
        glDeleteFramebuffers(1, &texture_framebuffer);

    }
    else // Desktop OpenGL
    {
        // save existing config
        GLuint old_tex;
        glGetIntegerv(GL_TEXTURE_BINDING_2D, reinterpret_cast<GLint*>(&old_tex));

        glBindTexture(GL_TEXTURE_2D, d_ogltexture);

        if (d_isCompressed)
        {
            glGetCompressedTexImage(GL_TEXTURE_2D, 0, targetData);
        }
        else
        {
            GLint old_pack;
            glGetIntegerv(GL_PACK_ALIGNMENT, &old_pack);

            glPixelStorei(GL_PACK_ALIGNMENT, 1);
            glGetTexImage(GL_TEXTURE_2D, 0, d_format, d_subpixelFormat, targetData);

            glPixelStorei(GL_PACK_ALIGNMENT, old_pack);
        }

        // restore previous config.
        glBindTexture(GL_TEXTURE_2D, old_tex);
    }
}