我相机的预览呈现为纹理。我有textureId,我正试图在FBO
上渲染纹理,所以我可以glReadPixels
。
当我实际读取像素时,它们看起来都是黑色的。
这是我目前正在使用的代码:
//called when the camera first connects to the texture
public void initBackBuffer()
{
// For use in getFramePixels()
intBuffer = ByteBuffer.allocateDirect(width *
height * 4)
.order(ByteOrder.nativeOrder())
.asIntBuffer();
IntBuffer framebuffer = IntBuffer.allocate(1);
glGenFramebuffers(1, framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.get(0));
int status = glGetError();
if(status != GL_NO_ERROR)
{
Log.e("RENDERER", status + "");
}
IntBuffer depthBufferName = IntBuffer.allocate(1);
glGenRenderbuffers(1, depthBufferName);
glBindRenderbuffer(GL_RENDERBUFFER, depthBufferName.get(0));
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER,
depthBufferName.get(0));
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
mTangoCameraTexture.getTextureId(), 0);
mOffscreenBuffer = framebuffer.get(0);
status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status != GL_FRAMEBUFFER_COMPLETE) {
if(status == GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT)
Log.e("RENDERER","GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT");
if(status == GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT)
Log.e("RENDERER", "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT");
Log.e("RENDERER", "FRAMEBUFFER ERROR:"+status);
}
glBindFramebuffer( GL_FRAMEBUFFER, 0 );
}
// called onFrameAvailable
public void getFramePixels() {
glBindFramebuffer(GL_FRAMEBUFFER, mOffscreenBuffer);
int status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status != GL_FRAMEBUFFER_COMPLETE)
{
Log.e(TAG, "FRAMEBUFFER STATUS:"+status);
}
glReadPixels(0, 0, width, height, GL_RGBA,
GL_UNSIGNED_BYTE,
intBuffer.clear());
StringBuilder str = new StringBuilder();
for(int i = 0; i < intBuffer.capacity(); i++)
{
str.append(intBuffer.get(););
}
Log.d(TAG, "IMAGE:" + str.toString());
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
如果我在屏幕上显示纹理,我就能看到它 - 所以不会实际绑定到相机的纹理。
修改:由于某种原因GL_INVALID_OPERATION
发生了glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D, getTextureId(), 0);
。
来自文档:
如果零绑定到目标,则生成GL_INVALID_OPERATION。 如果textarget和纹理不兼容,则生成GL_INVALID_OPERATION。
我的textureId
是1
。
答案 0 :(得分:0)
更仔细地阅读文档。您的纹理ID可能为1,但您当前绑定到目标的内容可能实际为0.如果您不打电话glBindTexture(GL_TEXTURE_2D, mTangoCameraTexture.getTextureId());
,则GL_TEXTURE_2D
(目标)是您设置的任何内容最后,我倾向于说可能是0。
如果您发现围绕glReadPixels()
存在性能问题,则应考虑将其与像素缓冲区对象一起使用。