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

  • Similar Content

    • By Freezee
      Hi, I'm 16 years old (nearly 17) french guy who loves coding, and pizzas, and music, and a lot of other things...
      I started learning some programming languages 6 years ago, and always failed to achieve something with them. Then I decided to re-try Java 2 years ago, and it went pretty well. So well that from this time I did not stopped programming. I really started to dig into C++ a year ago because I wanted lower level programming skills, with one specific goal: create games. Unfortunately I always overestimate myself and my ideas, and I've not been able to create a single real game because of my lack of experience in that specific domain. So I'm looking for a 3D FPS game project (multiplayer would be great too) to see how that kind of project is managed, and to finally be able to create something. I would like for once to work with other people on the same thing, I think it could really help me to help back the others. I have a lot of free time right now and I'm ready to spend some (if not a lot) into a project.
      I learned a lot of C++ features when I started, but I feel like I'm missing a lot of other features and I want to learn them on something useful.
      I really prefer not working on a project with a pre-used game engine (GM, UE, Unity, ...) because for me the most interesting part is what happens at the lowest programming level of a game. I learned basics of modern OpenGL so if there is a graphical engine to improve, I can work on it. I'm also very interested into working on the game engine structure, and on implementing a scripting language if it's needed. If the game is multiplayer, I will not guarantee that I could really work on that (because I really don't know a lot about networking) but I'll try my best to continue learning things and maybe work on that too.
      If you're interested, feel free to contact me on Discord: Freezee#2283. If you don't have Discord, reply back a way to contact you
    • By Jon Alma
      Some time ago I implemented a particle system using billboarding techniques to ensure that the particles are always facing the viewer.  These billboards are always centered on one 3d coordinate.
      I would like to build on this and use billboarding as the basis for things like laser bolts and gunshots.  Here the difference is that instead of a single point particle I now have to draw a billboard between two points - the start and end of the laser bolt for example.  I appreciate that having two end points places limits on how much the billboard can be rotated to face the viewer, but I'm looking to code a best effort solution.  For the moment I am struggling to work out how to do this or find any tutorials / code examples that explain how to draw a billboard between two points ... can anyone help?
      Thanks.
    • By Sagaceil
      It's always better to fight with a bro.
    • By recp
      Hi,
      I'm working on new asset importer (https://github.com/recp/assetkit) based on COLLADA specs, the question is not about COLLADA directly
      also I'm working on a new renderer to render (https://github.com/recp/libgk) imported document.
      In the future I'll spend more time on this renderer of course, currently rendering imported (implemented parts) is enough for me
      assetkit imports COLLADA document (it will support glTF too),
      importing scene, geometries, effects/materials, 2d textures and rendering them seems working
      My actual confusion is about shaders. COLLADA has COMMON profile and GLSL... profiles,
      GLSL profile provides shaders for effects so I don't need to wory about them just compile, link, group them before render

      The problem occours in COMMON profile because I need to write shaders,
      Actually I wrote them for basic matrials and another version for 2d texture
      I would like to create multiple program but I am not sure how to split this this shader into smaller ones,

      Basic material version (only colors):
      https://github.com/recp/libgk/blob/master/src/default/shader/gk_default.frag
      Texture version:
      https://gist.github.com/recp/b0368c74c35d9d6912f524624bfbf5a3
      I used subroutines to bind materials, actually I liked it,
      In scene graph every node can have different program, and it switches between them if parentNode->program != node->program
      (I'll do scene graph optimizations e.g.  view frustum culling, grouping shaders... later)

      I'm going to implement transparency but I'm considering to create separate shaders,
      because default shader is going to be branching hell
      I can't generate shader for every node because I don't know how many node can be exist, there is no limit.
      I don't know how to write a good uber-shader for different cases:

      Here material struct:
      struct Material { ColorOrTexture emission; ColorOrTexture ambient; ColorOrTexture specular; ColorOrTexture reflective; ColorOrTexture transparent; ColorOrTexture diffuse; float shininess; float reflectivEyety; float transparency; float indexOfRefraction; }; ColorOrTexture could be color or 2d texture, if there would be single colorOrTex then I could split into two programs,
      Also I'm going to implement transparency, I am not sure how many program that I needed

      I'm considering to maintain a few default shaders for COMMON profile,
      1-no-texture, 2-one of colorOrTexture contains texture, 3-........

      Any advices in general or about how to optimize/split (if I need) these shaders which I provied as link?
      What do you think the shaders I wrote, I would like to write them without branching if posible,
      I hope I don't need to write 50+ or 100+ shaders, and 100+ default programs

      PS: These default shaders should render any document, they are not specific, they are general purpose...
             I'm compiling and linking default shaders when app launched

      Thanks
    • By CircleOfLight97
      Hi guys,
      I would like to contribute to a game project as a developer (open source possibly). I have some experiences in C/C++ in game development (perso projects). I don't know either unreal or unity but I have some knowledges in opengl, glsl and shading theory as I had some courses at university regarding to that. I have some knowledges in maths and basic in physics. I know a little how to use blender to do modelling, texturing and simple game assets (no characters, no animation no skinning/rigging). I have no game preferences but I like aventure game, dungeon crawler, platformers, randomly generated things. I know these kind of projects involve a lot of time and I'd be really to work on it but if there are no cleary defined specific design goals/stories/gameplay mechanics I would like to not be part of it x) and I would rather prefer a smaller but well defined project to work on that a huge and not 'finishable' one.
      CircleOfLight97
  • Popular Now