I'm running in a small opengl issue and I was wondering if someone could help me. Basically Im trying to get a fbo up and running, I manage to draw something on the texture but it seems to me to be rendering only one pixel on the whole texture.
Heres the normal render without framebuffer: http://puu.sh/dbMJK/332f60d5dc.png
And with: http://puu.sh/dbMRK/e77134c646.png
vertex shader for the frame buffer:
in vec3 in_Vertex;
in vec2 in_TexCoord0;
out vec2 coordTexture;
void main() {
coordTexture = in_TexCoord0;
gl_Position = vec4(in_Vertex, 1.0);
}
fargment shader:
in vec2 coordTexture;
uniform sampler2D tex;
out vec4 out_Color;
void main() {
out_Color = texture(tex, coordTexture);
}
Heres are the functions where I take care of the FBO for now:
GLuint Game::createTexture(int w, int h, bool isDepth){
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
int i = glGetError();
if (i != 0)
{
printf("DUCK YOU create texture error: %s\n", glewGetErrorString(i));
}
glBindTexture(GL_TEXTURE_2D, 0);
return textureID;
}
void Game::createFBO(){
renderTexture = createTexture(800, 600, false);
glGenFramebuffers(1, &FBO);
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
GLuint depthrenderbuffer;
glGenRenderbuffers(1, &depthrenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 800, 600);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer);
// GL_COLOR_ATTACHMENT0
// GL_DEPTH_ATTACHMENT
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, renderTexture, 0);
// Set the list of draw buffers.
GLenum DrawBuffers[1] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(1, DrawBuffers); // "1" is the size of DrawBuffers
int i = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (i != GL_FRAMEBUFFER_COMPLETE)
{
printf("DUCK YOU FRAMEBUFFER BUFFED YOU: %i\n", i);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void Game::windowLoop(sf::Window *window){
static const GLfloat g_quad_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
};
GLuint quad_vertexbuffer;
glGenBuffers(1, &quad_vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_quad_vertex_buffer_data), g_quad_vertex_buffer_data, GL_STATIC_DRAW);
bool running = true;
while (running){
// handle events
sf::Event event;
while (window->pollEvent(event)){
if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
// end the program
running = false;
}
else if (event.type == sf::Event::Resized){
// adjust the viewport when the window is resized
glViewport(0, 0, event.size.width, event.size.height);
}
}
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glEnable(GL_DEPTH_TEST);
glClearColor(0.3f, 0.2f, 0.4f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gameLoop(window);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glUseProgram(screenShader.getProgramID());
glDisable(GL_DEPTH_TEST);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, renderTexture);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
glUseProgram(0);
window->display();
}
}
You do not supply texture coordinates for your quad rendering. Also your code comments suggest that you set the attribute locations explicitely via the layout
qualifier in the GL code, but you do not do that, so the mapping between the attribute variables and locations are undefined or at least not clear from the code parts you pasted.