Sign in to follow this  
Followers 0
theHOUSE16

OpenGL
[SOLVED]Switching to new framebuffer and back causes only clearcolor to render

2 posts in this topic

I'm trying to write a deferred renderer with sdl and opengl and am having trouble with the initial switching framebuffers part. Before i tried to switch framebuffers, when i was still foward rendering my scene looked like this. Its a little crude but it gets the point across.
[attachment=22724: figure.jpg]
On the left is the camera and at the very front of screenspace(z= -1) is the quad that i would texture with the output from my first pass. Behind that is the suzanne model. So when i foward render i get this(the color is the texture coords, i havent tried applying a texture yet):
[attachment=22725:Screen Shot 2014-07-19 at 5.44.40 AM.png]
and if i move the quad to z = -1.1 i get this:
[attachment=22726:Screen Shot 2014-07-20 at 1.17.29 AM.png]
Just like you would expect. The problem is as soon as i add glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); before rendering suzanne it only renders the clearcolor and that is all. I do switch back to the default framebuffer for the quad render and the framebuffer status is 0x8cd5(complete). 

 

Here's my code:

 

init

const char* vertex_shader = getVShader();
	const char* fragment_shader = getFShader();
    const char* ss_v_source = getSSVS();
	const char* ss_f_source = getSSFS();

    
    SDL_GetWindowDisplayMode(window, &current);
    init_camera();
    
    glClearColor(0.95f, 0.95f, 0.95f, 0.0f);
	glEnable (GL_DEPTH_TEST);
	glDepthFunc (GL_LESS);
    
    head_node = create_graph("Data/Scene/test.grf");
    
	unsigned int vs = glCreateShader (GL_VERTEX_SHADER);
	glShaderSource (vs, 1, &vertex_shader, NULL);
	glCompileShader (vs);
    check_shader(vs);
	unsigned int fs = glCreateShader (GL_FRAGMENT_SHADER);
	glShaderSource (fs, 1, &fragment_shader, NULL);
	glCompileShader (fs);
    check_shader(fs);
    
	shader_program = glCreateProgram ();
	glAttachShader (shader_program, fs);
	glAttachShader (shader_program, vs);
	glLinkProgram (shader_program);
    
    
    
    //init ss quad
    glGenBuffers(1, &ss_vbo);
    glBindBuffer(GL_ARRAY_BUFFER, ss_vbo);
    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(float), &ss_quad_pos, GL_STATIC_DRAW);
    glGenBuffers(1, &ss_st);
    glBindBuffer(GL_ARRAY_BUFFER, ss_st);
    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(float), &ss_quad_st, GL_STATIC_DRAW);
    glGenVertexArrays(1, &ss_vao);
    glBindVertexArray(ss_vao);
    glBindBuffer(GL_ARRAY_BUFFER, ss_vbo);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, ss_st);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
    glEnableVertexAttribArray(1);
    //set up ss shader
    ss_v_shader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(ss_v_shader, 1, &ss_v_source, NULL);
    glCompileShader(ss_v_shader);
    check_shader(ss_v_shader);
    ss_f_shader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(ss_f_shader, 1, &ss_f_source, NULL);
    glCompileShader(ss_f_shader);
    check_shader(ss_f_shader);
    ss_program = glCreateProgram();
    glAttachShader(ss_program, ss_v_shader);
    glAttachShader(ss_program, ss_f_shader);
    glLinkProgram(ss_program);
    
    //fbo set up
    glGenFramebuffers(1, &framebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
    //generate and bind texture for normals
    glGenTextures(1, &normals_tex);
    glBindTexture(GL_TEXTURE_2D, normals_tex);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, window_data.width, window_data.height, 0, GL_RGB, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    //attach to fbo
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, normals_tex, 0);
    
    //check fbo status
    GLenum  status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    printf("FrameBuffer status: 0x%x\n", status);
    
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

main loop

render_scene();
deferred();
SDL_GL_SwapWindow(window);

render_scene

glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

   proj_matrix = perspective(70.0f, (float)current.w/(float)current.h, 0.1f, 1000.0f);
    view_matrix = lookAt(vec3(camera.position[0],camera.position[1],camera.position[2]), vec3(camera.target[0],camera.target[1],camera.target[2]), vec3(camera.up[0],camera.up[1],camera.up[2]));
    
    mat4 mvp_matrix = proj_matrix * view_matrix * curr->transform;
    mat4 world_matrix = curr->transform;
    GLint mvp_id = glGetUniformLocation(shader_program,"mvp");
    glUseProgram(shader_program);
    glUniformMatrix4fv(mvp_id, 1, GL_FALSE, &mvp_matrix[0][0]);
    GLint world_matrix_id = glGetUniformLocation(shader_program,"world_pos");
    glUseProgram(shader_program);
    glUniformMatrix4fv(world_matrix_id, 1, GL_FALSE, &world_matrix[0][0]);
    
    glUseProgram (shader_program);
    glBindVertexArray (curr->mesh->vao);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, curr->mesh->ibo);
    glDrawElements(GL_TRIANGLES, 3 * curr->mesh->f_count, GL_UNSIGNED_INT, (void*)0);

and deferred

    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    glClear(GL_COLOR_BUFFER_BIT);
    
    glUseProgram(ss_program);
    
    glBindVertexArray(ss_vao);
    glDrawArrays(GL_TRIANGLES, 0, 6);

I havent actually taken the output from the geometry pass and tried to use it in the deferred function which is why any uniforms and all of that kind of stuff is missing. At this point i'm only trying to figure out why switching to another framebuffer and back is causing problems. What am i missing?

Edited by theHOUSE16
0

Share this post


Link to post
Share on other sites

I think i've solved it but i'm not exactly sure why it works. If i bind the default framebuffer AND glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) before i bind the bind my second framebuffer and glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) that so that i can draw to it, then it renders the quad and not the monkey like it should but otherwise it only renders the clearcolor. It seems to be something dealing with glclear.

Edited by theHOUSE16
0

Share this post


Link to post
Share on other sites

It was because i wasnt using GL_DEPTH_BUFFER_BIT when i cleared the default buffer originally. 

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0