问题描述:

Image with two cubes is when its using BLIT Call - Test Code

Image with one Cube is when its not Using BLIT CALL - Test Code

Folks,

Please follow the attached pictures & Code as below..

Struggling with this from last 2 days..

Facing an issue while using FBO in below code, I am able to attach & render to FBo successfully, BUT some how not able to use that as a texture (color & depth buffers) in the same program, MY glReadpixel call after rendering to FBO Shows me all Zero's data...

When generated FBO(color,depth) is used in GLSL- shader code , which is called from Draw2() function displays a blend white cube without texture..

please suggest..., below is the complete code... , I am creating an FBO & using 2 shaders one for Normal rendering & other one uses the color & depth from FBO processed earlier..

Here is MY SHADER CODE

 init_gl()

{

This Fragment Shader Code is for normal rendering used in Program under draw()

 static const char *fragment_shader_source =

"precision mediump float; \n"

" \n"

"varying vec4 vVaryingColor; \n"

" \n"

"void main() \n"

"{ \n"

" gl_FragColor = vVaryingColor; \n"

"} \n";

This Fragment Shader Code is used in Program2 under draw2()

 static const char *fragment_shader_source2 =

"precision mediump float; \n"

" \n"

"varying vec4 vVaryingColor; \n"

"uniform sampler2D color; \n"

"uniform sampler2D depth; \n"

" \n"

"void main() \n"

"{ \n"

"float r = pow(texture2D(color, gl_FragCoord.xy / vec2(256, 256)).a, 128.0); \n"

"float g = texture2D(depth, gl_FragCoord.xy / vec2(256, 256)).a;\n"

"float b = pow(texture2D(depth, gl_FragCoord.xy / vec2(256, 256)).a, 128.0); \n"

"gl_FragColor = vec4(r, g, b, 1.); \n"

"} \n";

MY FIRST DRAWING ON FBO

 static void draw(uint32_t i)

{

EGL_CHECK(glEnable(GL_DEPTH_TEST));

glEnable(GL_TEXTURE_2D);

EGL_CHECK(glDepthFunc(GL_ALWAYS));

EGL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, fboInfo.id));

EGL_CHECK(glUseProgram(program));

//CUBE DRAWING

Doing Read Pixel here, Its returning all zero's

 GLubyte *pixels = malloc(4 * 256 * 256);

EGL_CHECK(glReadBuffer(GL_COLOR_ATTACHMENT0));

EGL_CHECK(glReadPixels(0, 0, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, pixels));

//**TO TEST my FBO , IF ITS HAVING SOME RENDER DATA**

EGL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0));

glBindFramebuffer(GL_READ_FRAMEBUFFER, fboInfo.id);

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);

glBlitFramebuffer(0, 0, 256, 256, 0, 0, 256, 256, GL_COLOR_BUFFER_BIT, GL_NEAREST);

glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);

//TEST CODE ONLY

glBindTexture(GL_TEXTURE_2D, fboInfo.id);

glGenerateMipmap(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, 0);

draw2(i);

}

2nd DRAWING WITHOUT FBO (BUT this uses FRAG SHADER WHICH CAN ACCESS FBO data color,depth)

 static void draw2(uint32_t i)

{

glUseProgram(program2);

//Draw Same Kind of Cube , Which we draw in draw() function

}

These are the functions to create FBO (color,depth)

 GLuint createTexture2D(const int w, const int h, GLint internalFormat, GLenum format, GLenum type)

{

GLuint textureIdX;

EGL_CHECK(glGenTextures(1, &textureIdX));

EGL_CHECK(glBindTexture(GL_TEXTURE_2D, textureIdX));

EGL_CHECK(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));

EGL_CHECK(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));

EGL_CHECK(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));

EGL_CHECK(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));

if (GL_DEPTH_COMPONENT == format) {

EGL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE));

EGL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY));

}

EGL_CHECK(glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, format, type, 0));

EGL_CHECK(glBindTexture(GL_TEXTURE_2D, 0));

return textureIdX;

}

int createFBO(void)

{

int result = 0;

unsigned int fb = 0;

fboInfo.color = createTexture2D(WINDOW_WIDTH, WINDOW_HEIGHT, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);

fboInfo.depth = createTexture2D(WINDOW_WIDTH, WINDOW_HEIGHT, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_FLOAT);

EGL_CHECK(glGenFramebuffers(1, &fboInfo.id));

EGL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, fboInfo.id));

EGL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fboInfo.color, 0));

EGL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fboInfo.depth, 0));

int data = EGL_CHECK(glCheckFramebufferStatus(GL_FRAMEBUFFER));

if (GL_FRAMEBUFFER_COMPLETE == glCheckFramebufferStatus(GL_FRAMEBUFFER)) {

printf("FBO %d set up successfully\n", fboInfo.id);

result = 1;

}

else {

printf("FBO %d NOT set up properly!\n", fboInfo.id);

}

EGL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0));

return result;

}

This is how my FBO Struct Looks Like

typedef struct {

GLuint id;

GLuint color;

GLuint depth;

}FBOInfo;

My MAIN function looks a below where I create FBO & then call my draw function & swap the buffers..

 unsigned long i = 0;

int ret = init_gl();

ret = createFBO();

draw(i);

EGL_CHECK(eglSwapBuffers(eglDisplay, eglSurface));

网友答案:

Since this is quite a lot of code, and you suggested that the problem is with the FBO setup, I focused on that section. I did spot a critical problem in that part of the code where you set up the attachments:

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fboInfo.depth, 0);

If you call glGetError() after this, you should see an error returned, because GL_RENDERBUFFER is not a valid value for the 3rd argument. The only valid arguments are GL_TEXTURE_2D and the GL_TEXTURE_CUBE_MAP_* values. You have to use GL_TEXTURE_2D in this case.

It might actually be better to use a renderbuffer for the depth buffer if you never use it as a texture. But to do that, you'll have to use all the corresponding calls:

  • Create the id with glGenRenderbuffers.
  • Bind it with glBindRenderbuffer.
  • Allocated it with glRenderbufferStorage.
  • Attach it to the FBO with glFramebufferRenderbuffer.
相关阅读:
Top