PDA

View Full Version : Render to Texture using FBOs not working



Cloudy McStrife
11-14-2015, 05:05 AM
Hey guys.
I'm currently trying to implement Deferred Shading in my Engine. The problem is that I can't render to a texture using GL_COLOR_ATTACHMENT0.
I know there must be a tiny error in my code which I can't see.
I hope you guys can help me.

Initializing Frambuffer texture etc.



GLuint textur;
glGenTextures(1, &textur);
glBindTexture(GL_TEXTURE_2D, textur);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 256, 256, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);

glGenerateMipmap(GL_TEXTURE_2D);

GLuint fbo;
glGenFramebuffers(1, &fbo);

glBindFramebuffer(GL_FRAMEBUFFER, fbo);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textur, 0/*mipmap level*/);

GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};

glDrawBuffers(1, DrawBuffers);

GLuint rbo;
glGenRenderbuffers(1, &rbo);
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 800, 600);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);

if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout << "Incomplete Framebuffer" << std::endl;
else
std::cout << "Complete Framebuffer" << std::endl;

glBindFramebuffer( GL_FRAMEBUFFER, 0 );


Every Frame:


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBindFramebuffer(GL_FRAMEBUFFER, fbo);

glEnable(GL_DEPTH_TEST);

update(deltaTime);

glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClearColor(1.0,0.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0,0,800,600);
vao.bind(); //binds the quads vao
glDisable(GL_DEPTH_TEST);
screen_program->use_program();

glActiveTexture(GL_TEXTURE5);
glBindTexture(GL_TEXTURE_2D, textur);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);


Notes:
My Quad is rendered succesfully but without any texture.
So I don't think there are any problems with my shaderprogram and/or vbo/vaos

Thank you in advance for your answer :)
I hope you can help me