简体   繁体   English

android从GL_TEXTURE_EXTERNAL_OES读取像素

[英]android read pixels from GL_TEXTURE_EXTERNAL_OES

I am trying to read pixels/data from an OpenGL texture which is bound to GL_TEXTURE_EXTERNAL_OES. 我试图从一个绑定到GL_TEXTURE_EXTERNAL_OES的OpenGL纹理中读取像素/数据。

The reason for binding the texture to that target is because in order get live camera feed on android a SurfaceTexture needs to be created from an OpenGL texture which is bound to GL_TEXTURE_EXTERNAL_OES. 将纹理绑定到该目标的原因是因为为了获得Android上的实时相机源,需要从绑定到GL_TEXTURE_EXTERNAL_OES的OpenGL纹理创建SurfaceTexture。

Since android uses OpenGL ES I can't use glGetTexImage() to read the image data. 由于android使用OpenGL ES,我无法使用glGetTexImage()来读取图像数据。

Therefore I am binding the target to an FBO and then reading it using readPixels(). 因此,我将目标绑定到FBO,然后使用readPixels()读取它。 This is my code: 这是我的代码:

    GLuint framebuffer;
    glGenFramebuffers(1, &framebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);

    //Attach 2D texture to this FBO
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_EXTERNAL_OES, cameraTexture, 0);
    status("glFramebufferTexture2D() returned error %d", glGetError());

However I am getting error 1282 (GL_INVALID_OPERATION) for some reason. 但是由于某种原因我收到错误1282(GL_INVALID_OPERATION)。

I think this might be the problem: 我想这可能是问题所在:

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_EXTERNAL_OES, cameraTexture, 0);

You should not attach the cameraTexture to the framebuffer, instead you should generate a new texture in the format of GL_TEXTURE_2D 你不应该将cameraTexture附加到帧缓冲区,而应该生成格式为GL_TEXTURE_2D的新纹理。

glGenTextures(1, mTextureHandle, 0);

glBindTexture(GL_TEXTURE_2D, mTextureHandle[0]);

...

cameraTexture is the one you get from SurfaceTexture, and it is the source used for rendering. cameraTexture是您从SurfaceTexture获得的,它是用于渲染的源。 This new texture is the one you should render to (which could be used later in the rendering pipeline). 这个新纹理是你应该渲染的纹理(可以在以后的渲染管道中使用)。 Then do this: 然后这样做:

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, mTextureHandle[0], 0);

cameraTexture is drawn to the framebuffer's attached texture, using a simple shader program to draw, bind the cameraTexture when the shader program is in use: cameraTexture被绘制到framebuffer的附加纹理,使用简单的着色器程序绘制,在着色器程序使用时绑定cameraTexture:

glBindTexture(GL_TEXTURE_EXTERNAL_OES, cameraTexture); 

The GL_TEXTURE_EXTERNAL_OES texture target is usually in a YUV color space and glReadPixels() requires the target to be RGB. GL_TEXTURE_EXTERNAL_OES纹理目标通常位于YUV颜色空间中,glReadPixels()要求目标为RGB。 It probably does not do the color space conversion automatically. 它可能不会自动进行色彩空间转换。 However, you could do the conversion in your own fragment shader which renders RGB into another texture and then use glReadPixels() to read that. 但是,您可以在自己的片段着色器中进行转换,该着色器将RGB渲染为另一个纹理,然后使用glReadPixels()来读取它。

texture for YUV420 to RGB conversion in OpenGL ES 在OpenGL ES中进行YUV420到RGB转换的纹理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android 如何为 GL_TEXTURE_EXTERNAL_OES 生成 mipmap - Android How to generate mipmap for GL_TEXTURE_EXTERNAL_OES 从GL_TEXTURE_EXTERNAL_OES将GLES纹理绑定到GL_TEXTURE_2D - Bind GLES texture to GL_TEXTURE_2D from GL_TEXTURE_EXTERNAL_OES 错误:在Android Studio上找不到符号变量GL_TEXTURE_EXTERNAL_OES + OpenCV - Error: cannot find symbol variable GL_TEXTURE_EXTERNAL_OES + OpenCV on Android Studio Android glTexSubImage2D 与 GL_TEXTURE_EXTERNAL_OES 问题(OpenGL + OpenCV + CameraX) - Android glTexSubImage2D with GL_TEXTURE_EXTERNAL_OES problem (OpenGL + OpenCV + CameraX) GL_TEXTURE_EXTERNAL_OES在OpenGL ES 3.0 NDK上不起作用 - GL_TEXTURE_EXTERNAL_OES Not working on OpenGL ES 3.0 NDK GL_TEXTURE_2D 和 GL_TEXTURE_EXTERNAL_OES 有什么区别 - What's the difference between GL_TEXTURE_2D and GL_TEXTURE_EXTERNAL_OES GLES2.0:通过 glEGLImageTargetTexture2DOES 使用 GL_TEXTURE_EXTERNAL_OES - GLES2.0: Use GL_TEXTURE_EXTERNAL_OES via glEGLImageTargetTexture2DOES 具有GL_HALF_FLOAT_OES或GL_FLOAT的纹理目标使帧缓冲区GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT在Galaxy S4(Android 4.2)上 - Texture target with GL_HALF_FLOAT_OES or GL_FLOAT makes framebuffer GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT on Galaxy S4 (Android 4.2) Android OpenGL ES读取纹理像素 - Android OpenGL ES read texture pixels 是否可以将2个opengl EXTERNAL_OES纹理组合在一起? - Is it possible to combine 2 opengl EXTERNAL_OES texture together?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM