How to get texture data using textureID's in openGL

前端 未结 2 1883
不思量自难忘°
不思量自难忘° 2020-12-05 13:31

I\'m writing some code where all I have access to is a textureID to get access to the required texture. Is there any way that I can get access to the RGB values of this text

相关标签:
2条回答
  • 2020-12-05 14:19

    You are probably looking for glGetTexImage (see https://www.khronos.org/opengl/wiki/GLAPI/glGetTexImage for further information).

    Before using glGetTexImage, don't forget to use glBindTexture (https://www.khronos.org/opengl/wiki/GLAPI/glBindTexture) with your texture ID.

    0 讨论(0)
  • 2020-12-05 14:29

    In OpneGL a texture can be read by glGetTexImage/glGetnTexImage respectively the DSA version of the function glGetTextureImage.

    Another possibility is to attach the texture to a framebuffer and to read the pixel by glReadPixels. OpenGL ES does not offer a glGetTexImage, so this is the way to OpenGL ES.
    See opengl es 2.0 android c++ glGetTexImage alternative


    If you transfer the texture image to a Pixel Buffer Object, then you can even access the data via Buffer object mapping. See also OpenGL Pixel Buffer Object (PBO).
    You've to bind a buffer with the proper size to the target GL_PIXEL_PACK_BUFFER:

    // create buffer
    GLuint pbo;
    glGenBuffers(1, &pbo);
    glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo);
    glBufferData(GL_PIXEL_PACK_BUFFER, size_in_bytes, 0, GL_STATIC_READ);
    
    // get texture image
    glBindTexture(GL_TEXTURE_2D, texture_obj);
    glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, (void*)(0));
    
    // map pixel buffer
    void * data_ptr = glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
    
    // access the data
    // [...]
    
    glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
    
    0 讨论(0)
提交回复
热议问题