來自我的相機的預覽呈現爲紋理。我有textureId,並試圖在FBO
上渲染紋理,所以我可以glReadPixels
。Android OpenGL ES讀取紋理像素
當我實際上讀取像素時,它們都顯示爲黑色。
這是我使用的那一刻代碼:
//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和texture不兼容,則會生成GL_INVALID_OPERATION。
我的textureId
是1
。