OpenGL FBO Returns Black Screen

1.1k views Asked by At

I'm having a bit of trouble with my frame buffers in my OpenGL C++ application. There are no errors thrown (I get GL_FRAMEBUFFER_COMPLETE when I call glCheckFramebufferStatus), however when I render my FBO texture all I see is a black screen. I've searched for numerous hours and redone it multiple times to no avail, so I'm finally turning to you guys for some help. Below is all the relevant code. I hope you guys can spot something that my tired eyes couldn't. Thank you.

Initialization code:

glGenFramebuffers(1, &m_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);

m_texture = new Texture(width, height);
//glBindTexture(GL_TEXTURE_2D, m_texture->GetID());

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_texture->GetID(), 0);

glGenRenderbuffers(1, &m_depth);
glBindRenderbuffer(GL_RENDERBUFFER, m_depth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depth);

GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
CheckError(status);

Texture init code:

unsigned char* imageData = fileName != "" ? stbi_load(fileName.c_str(), &width, &height, &numComponents, 4) : 0;

if (imageData == NULL && fileName != "")
    std::cerr << "Texture loading failed for texture: " << fileName << std::endl;

glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);

glBindTexture(GL_TEXTURE_2D, 0);

if (fileName != "") stbi_image_free(imageData);

Texture bind code:

glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, m_texture);

Framebuffer bind code:

glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
glViewport(0, 0, m_width, m_height);

Main code:

    fbo.BindFramebuffer();
    display.Clear(1.0f, 0.0f, 0.0f, 1.0f);

    fboShader.Bind();
    fboShader.Update(transform2, camera);
    texture.Bind(0);
    mesh2.Draw();

    display.Bind();
    display.Clear(0.0f, 0.0f, 0.9f, 1.0f);

    shader.Bind();
    shader.Update(transform, camera);
    fbo.BindFramebufferTexture(0);
    mesh3.Draw();

FBO shader code Fragment:

#version 330

varying vec2 texCoord0;
varying vec3 normal0;
varying vec3 position0;

uniform sampler2D diffuse;

out vec4 outputColor;

void main()
{
    outputColor = vec4(normal0, 1.0);
    //gl_FragColor = vec4(normal0, 1.0);    
}

Vertex:

#version 330

attribute vec3 position;
attribute vec2 texCoord;
attribute vec3 normal;

varying vec2 texCoord0;
varying vec3 normal0;
varying vec3 position0;

uniform mat4 transform;

void main() 
{
    gl_Position = transform * vec4(position, 1.0);  
    texCoord0 = texCoord;
    normal0 = (transform * vec4(normal, 0.0)).xyz;
    position0 = gl_Position.xyz;
}

What I should see is a box (mesh3) with a monkeyhead the color of its normals (mesh2) and red background on it, but all I get is black. I really appreciate any help!

0

There are 0 answers