When binding an fbo do you need to call glFramebufferTexture2D every frame? - android

On each draw frame when I bind an FBO am I supposed to call
GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER,
GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D,
ResourceManager.windowTex[0], 0);
Are you only supposed to call this when you initialize your framebuffer or every frame? It doesn't draw for me unless I call it after glBindFramebuffer.

The attachment bindings are part of the FBO state. You only need to call glFramebufferTexture2D() once, while the FBO is bound. Those same textures will still be attached when you later bind the same FBO again.
One possible trap is that the texture object must have been created before it can be attached to an FBO. Generating the name alone is not enough, it needs to be bound at least once to create the actual texture object. For example, the following would be an error:
GLuint texId = 0;
glGenTextures(1, &texId);
GLuint fboId = 0;
glGenFramebuffers(1, &fboId);
glFramebufferTexture2D(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texId, 0);
glBindTexture(GL_TEXTURE_2D, texId);
glTexImage2D(...);
The texture object was not created in this case before being attached to the FBO. The following on the other hand is valid:
GLuint texId = 0;
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
glBindTexture(GL_TEXTURE_2D, 0);
GLuint fboId = 0;
glGenFramebuffers(1, &fboId);
glFramebufferTexture2D(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texId, 0);
glBindTexture(GL_TEXTURE_2D, texId);
glTexImage2D(...);
Note that the texture does not have to be currently bound when glFramebufferTexture2D() is called, and it's also not necessary for texture data to have been specified at that point. It just needs to have been bound at least once to trigger creation of the texture object.

Related

creating a framebuffer to take an image from external oes

I am trying to do multi pass rendering with framebuffer the issue is that the source is the camera so the texInput is not of type GL_TEXTURE_2D it is of type GL_TEXTURE_EXTERNAL_OES tho, if I use the function glTexImage2D it gives a black screen when I use that texture in the future after rendering to it without any errors glErros.
On the other hand if I don't use it(because it does not support GL_TEXTURE_EXTERNAL_OES) it gives me framebuffer status of NOT_COMPLETE
here is my code for creating the framebuffer and the texture that I want to associate to it.
fun prepareFBO() {
val intArray = IntArray(1)
glGenFramebuffers(1, intArray, 0)
fboId = intArray.first()
glGenTextures(1, intArray, 0)
texId = intArray.first()
glBindTexture(GL_TEXTURE_2D, texId)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,1080, 1920, 0, GL_RGBA, GL_UNSIGNED_BYTE, null)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glBindFramebuffer(GL_FRAMEBUFFER, fboId)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texId, 0)
val status = glCheckFramebufferStatus(GL_FRAMEBUFFER)
Log.e(TAG, "prepareFBO: ${status == GL_FRAMEBUFFER_COMPLETE}")
glBindFramebuffer(GL_FRAMEBUFFER, 0)
glBindTexture(GL_TEXTURE_2D, 0)
}
the rest is just normal glDrawArrays and glActiveTexture that already works fine when I draw directly to the default framebuffer, if needed please let me know and I will share them.
Thanks for help!
UPDATE:
I found that I need instead of using glTexImage2d I can use eglBindTexImage instead so the final code is:
fun prepareFBO() {
val intArray = IntArray(1)
glGenFramebuffers(1, intArray, 0)
fboId = intArray.first()
glBindFramebuffer(GL_FRAMEBUFFER, fboId)
glGenTextures(1, intArray, 0)
texId = intArray.first()
glBindTexture(GL_TEXTURE_EXTERNAL_OES, texId)
checkGlError("Error 1")
eglBindTexImage(eglGetCurrentDisplay(), windowSurface!!.eglSurface, EGL_BACK_BUFFER)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_EXTERNAL_OES, texId, 0)
val status = glCheckFramebufferStatus(GL_FRAMEBUFFER)
Log.e(TAG, "prepareFBO: is status complete? => ${status == GL_FRAMEBUFFER_COMPLETE}")
}
while this looks like it makes sense, still I am getting the status of the framebuffer as not complete :/

Load Texture From File problem Android NDK and OpenGL

I am trying to load an image through OpenGL and stbi_load on android ndk. The problem is that it is generating an invalid texture (equal to zero).
LoadTextureFromFile("/storage/emulated/0/Download/BPV/mic_close.jpg", &my_image_texture, &my_image_width, &my_image_height);
bool CGUI::LoadTextureFromFile(const char* filename, GLuint* out_texture, int* out_width, int* out_height)
{
// Load from file
int image_width = 0;
int image_height = 0;
unsigned char* image_data = stbi_load(filename, &image_width, &image_height, NULL, 4);
if (image_data == NULL)
return false;
// Create a OpenGL texture identifier
GLuint image_texture;
glGenTextures(1, &image_texture);
glBindTexture(GL_TEXTURE_2D, image_texture);
// Setup filtering parameters for display
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Upload pixels into texture
glPixelStorei(0x0CF2, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_width, image_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
stbi_image_free(image_data);
*out_texture = image_texture; // Receive zero
*out_width = image_width;
*out_height = image_height;
return true;
}
I use libGLESv2
GLuint image_texture;
glGenTextures(1, &image_texture);
...
*out_texture = image_texture; // Receive zero
glGenTextures() just allocates new object and practically never fails, save when called without active OpenGL context.
On Android platform, OpenGL rendering is usually done in dedicated working thread (different from GUI thread), so most likely you call the method from the wrong thread or before OpenGL context initialization (which cannot be deduced from the given code sample).

error 0x0502 when reading pixels to PBO from FBO

I was trying to use FBO and PBO together, read pixels from FBO to PBO, everyframe.
Codes are below:
//at the beginning:
//fbo
glGenFramebuffers(1, &m_fBO);
glBindFramebuffer(GL_FRAMEBUFFER, m_fBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texid, 0);
//pbo
glGenBuffersARB(1, &m_pbo);
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, m_pbo);
glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, DATA_SIZE, 0, GL_STREAM_READ_ARB);
//at each frame:
glBindFramebuffer(GL_FRAMEBUFFER, m_fBO);
Draw_some_stuff();
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, m_pbo);
glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, m_width, m_height, GL_RGBA, GL_UNSIGNED_BYTE, 0); //err occurs
Ptr = glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY_ARB);
Cpu_works_on_Ptr();
glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
It works fine on PC. But failed to running on my android phone, got an error 0x0502(GL_INVALID_OPERATION) right after glReadPixels.
I believe my codes are the same as the bottom answer at this link:
Reading the pixels values from the Frame Buffer Object (FBO) using Pixel Buffer Object (PBO)
Has anyone got a clue? Where did I do it wrong?

OpenGL ES2.0 glReadPixels() read data from renderbuffer through framebuffer

I am doing off-screen processing using opengl es2.0 on Android.
I created a renderbuffer, and attached it to a framebuffer FBO, after rendering to the FBO, I try to get the pixels from that FBO by getReadPixels() method. But I got nothing.
The code is shown below:
GLuint resultFBO;// FBO
GLuint rboId; //render buffer id
glGenRenderbuffers(1, &rboId);
glBindRenderbuffer(GL_RENDERBUFFER, rboId);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, image_width, image_height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glGenFramebuffers(1, &resultFBO);
glBindFramebuffer(GL_FRAMEBUFFER, resultFBO);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
GL_RENDERBUFFER, rboId);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status == GL_FRAMEBUFFER_COMPLETE) {
//LOGH("Single FBO setup successfully.");
} else {
LOGH("Problem in setup FBO texture: %d .", status);
}
//After render to the FBO
glBindFramebuffer(GL_FRAMEBUFFER, resultFBO);
glReadPixels(0, 0, w, h, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, imageSetData);
You can not glReadPixels() from GL_DEPTH_COMPONENT in OpenGL ES 2.0. Only from the color buffer. See API Reference here.
void glReadPixels( GLint x,
GLint y,
GLsizei width,
GLsizei height,
GLenum format,
GLenum type,
GLvoid * data);
format
Specifies the format of the pixel data. The following symbolic values are accepted: GL_ALPHA, GL_RGB, and GL_RGBA
Workaround1: If precision is not that important, you can write depth to one of the 8bit color channels instead.
Workaround2: You can write depth into the RGBA channels by packing the float into a vec4: See for example this SO thread.
Workaround3: You can try the OES_depth_texture extension and, if supported, render to a depth texture instead.

Failed to initialize FBO on Android SDK emulator device

So this is how FBO is initialized:
/**
* Initializes Renderbuffer.
*/
static GLuint init_renderbuffer(GLuint width, GLuint height, GLenum format) {
GLuint renderbuffer;
glGenRenderbuffers(1, &renderbuffer);
checkGlError("init_renderbuffer: glGenRenderbuffers");
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
checkGlError("init_renderbuffer: glBindRenderbuffer");
glRenderbufferStorage(GL_RENDERBUFFER, format, width, height);
checkGlError("init_renderbuffer: glRenderbufferStorage");
glBindRenderbuffer(GL_RENDERBUFFER, 0);
checkGlError("init_renderbuffer: glBindRenderbuffer");
return renderbuffer;
}
/**
* Initializes FBO.
*/
static void engine_init_fbo(struct engine* engine, GLuint width, GLuint height) {
// create renderable texture
glGenTextures(1, &engine->renderableTexture);
checkGlError("engine_init_fbo: glGenTextures");
glBindTexture(GL_TEXTURE_2D, engine->renderableTexture);
checkGlError("engine_init_fbo: glBindTexture");
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0);
checkGlError("engine_init_fbo: glTexImage2D");
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
checkGlError("engine_init_fbo: glTexParameteri");
// create render buffers
engine->depthRenderBuffer = init_renderbuffer(width, height, GL_DEPTH_COMPONENT16);
engine->stencilRenderBuffer = init_renderbuffer(width, height, GL_STENCIL_INDEX8);
LOGI("****************************** FBO: T: %d, D: %d, S: %d", engine->renderableTexture,
engine->depthRenderBuffer, engine->stencilRenderBuffer);
// create framebuffer object
glGenFramebuffers(1, &engine->framebufferObject);
checkGlError("engine_init_fbo: glGenFramebuffers");
glBindFramebuffer(GL_FRAMEBUFFER, engine->framebufferObject);
checkGlError("engine_init_fbo: glBindFramebuffer");
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, engine->renderableTexture, 0);
checkGlError("engine_init_fbo: glFramebufferTexture2D");
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, engine->depthRenderBuffer);
checkGlError("engine_init_fbo: glFramebufferRenderbuffer");
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, engine->stencilRenderBuffer);
checkGlError("engine_init_fbo: glFramebufferRenderbuffer");
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
checkGlError("engine_init_fbo: glCheckFramebufferStatus");
if(status != GL_FRAMEBUFFER_COMPLETE) {
switch(status) {
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
LOGI("****************************** engine_init_fbo: FBO error: FRAMEBUFFER_INCOMPLETE_ATTACHMENT");
break;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
LOGI("****************************** engine_init_fbo: FBO error: FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT");
break;
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
LOGI("****************************** engine_init_fbo: FBO error: FRAMEBUFFER_INCOMPLETE_DIMENSIONS");
break;
case GL_FRAMEBUFFER_UNSUPPORTED:
LOGI("****************************** engine_init_fbo: FBO error: FRAMEBUFFER_UNSUPPORTED");
break;
default:
LOGI("****************************** engine_init_fbo: Unknown FBO error");
}
}
else {
LOGI("****************************** engine_init_fbo: FBO has been successfully initialized");
}
glBindTexture(GL_TEXTURE_2D, 0);
checkGlError("engine_init_fbo: glBindTexture");
glBindFramebuffer(GL_FRAMEBUFFER, 0);
checkGlError("engine_init_fbo: glBindFramebuffer");
}
As you can see, everything here is done according to OpenGL ES standard: 2D texture with RGB565 internal format is used as color; 2 renderbuffers are used as depth and stencil surfaces; dimensions are 32x32.
It works fine on real device (Alcatel OT 918D, prints "FBO has been successfully initialized" in adb logcat), but fails with FRAMEBUFFER_UNSUPPORTED on virtual device.
My virtual device's CPU is atom, GPU emulation is enabled, OpenGL ES 2.0 works fine - checked with another applications (which do not use FBOs).
Entire project (based on native-activity sample from NDK), which can be compiled with ndk-build, is here:
https://docs.google.com/file/d/0Byy41LxMuTKUZVJxSUtBZDVDXzA/edit?usp=sharing
My question is: Is this a bug in android-sdk emulator? Because everything seems to meet the minimum requerements to the OpenGL ES 2.0 implementation.
Thank you!
EDIT:
Found the solution. It appears, that AVD does not support this configuration of attached objects. I had to remove stencil attachment to make it work.
EDIT2:
It appears to be the context creation problem. I did not require stencil bits, so probably AVD created context without stencil support, and Alcatel phone - with stencil.
Anyway, this is not a bug, but my mistake.

Categories

Resources