1
\$\begingroup\$

I recently added framebuffer rendering to my game and rendering to the FBO works (verified with glGetTexImage), but when I go to render a quad to show the result nothing is drawn to the screen. I'm using OpenGL 3.3 Core, so I'm drawing using vertex buffers for the quad and I have two simple pass-through shaders to display the quad. I apologize for the amount of code that follows, but I felt it was all necessary for anyone who ends up looking at this.

Quad generation:

// create vertices
// Vertex(position, normal, tex coord)
quadVertices.Add( Vertex( Vector3( -1.0f,  1.0f, 0.0f ), Vector3(), Vector2( 0.0f, 1.0f ) ) );
quadVertices.Add( Vertex( Vector3( -1.0f, -1.0f, 0.0f ), Vector3(), Vector2( 0.0f, 0.0f ) ) );
quadVertices.Add( Vertex( Vector3(  1.0f, -1.0f, 0.0f ), Vector3(), Vector2( 1.0f, 0.0f ) ) );
quadVertices.Add( Vertex( Vector3(  1.0f,  1.0f, 0.0f ), Vector3(), Vector2( 1.0f, 1.0f ) ) );
quadVertices.SendData(); // gl*Buffer* methods wrapper

// create indices (quadIndices is a buffer of 16-bit integers)
quadIndices.Add( 0 );
quadIndices.Add( 1 );
quadIndices.Add( 2 );
quadIndices.Add( 0 );
quadIndices.Add( 2 );
quadIndices.Add( 3 );
quadIndices.SendData();

Quad rendering:

glDisable( GL_DEPTH_TEST );
glDisable( GL_CULL_FACE );

// bind the texture
glActiveTexture( GL_TEXTURE0 );
glBindTexture( GL_TEXTURE_2D, framebuffer.GetColorHandle() );
_shader.Seti( "texture0", 0 );

 // projection matrix is glm::ortho( -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f );
_shader.Set( "proj", _projection );

// get attributes
int vertex   = _shader.GetAttribLoc( "vertex" );
int texCoord = _shader.GetAttribLoc( "texCoordV" );

// bind buffers
quadVertices.Bind();
quadIndices.Bind();

// enable arrays and point to data
glEnableVertexAttribArray( vertex );
glVertexAttribPointer    ( vertex,   3, GL_FLOAT, GL_FALSE, sizeof( Vertex ), (void*)( 0 ) );
glEnableVertexAttribArray( texCoord );
glVertexAttribPointer    ( texCoord, 2, GL_FLOAT, GL_FALSE, sizeof( Vertex ), (void*)( sizeof( Vector3 ) * 2 ) );

// draw!
glDrawElements( GL_TRIANGLES, quadIndices.Size(), GL_UNSIGNED_SHORT, 0 );

// disable arrays
glDisableVertexAttribArray( vertex   );
glDisableVertexAttribArray( texCoord );

// unbind everything
quadVertices.Unbind();
quadIndices.Unbind();
glBindTexture( GL_TEXTURE_2D, 0 );

glEnable( GL_CULL_FACE );
glEnable( GL_DEPTH_TEST );

Vertex shader:

#version 330 core

in  vec3 vertex;
in  vec2 texCoordV;
out vec2 texCoordF;

uniform mat4 proj;

void main()
{
    texCoordF = texCoordV;
    gl_Position = proj * vec4( vertex, 1.0 );
}

Fragment shader:

#version 330 core

layout(location=0) out vec4 fragColor;

in vec2 texCoordF;

uniform sampler2D texture0;

void main()
{
    fragColor = texture( texture0, texCoordF );
}
\$\endgroup\$
0
\$\begingroup\$

Maybe the projection matrix is wrong, but that should not matter. When rendering a fullscreen quad, you can discard the projection entirely.

Basically your vertex shader should be:

#version 330 core

in  vec3 vertex;
in  vec2 texCoordV;
out vec2 texCoordF;

void main()
{
    texCoordF   = texCoordV;
    gl_Position = vec4(vertex, 1.0);
}

The important bit is that you submit the vertexes in device coordinates (from [-1,-1] to [1,1]) but you are doing that already.

\$\endgroup\$
  • \$\begingroup\$ Okay, this draws the quad, but somehow the quad is being affected by the matrices in the scene being rendered to it? As in I can move around my actual scene, and I also move around the rendered quad at the same time. \$\endgroup\$ – fastinvsqrt Jul 30 '14 at 16:05
  • \$\begingroup\$ I forgot to call glUseProgram for the pass-through shader... \$\endgroup\$ – fastinvsqrt Jul 30 '14 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.