Sign in to follow this  
GameGeazer

FBO only renders to the first target

Recommended Posts

Hey, I'm trying to render to multiple targets using a frame buffer object, but as of now only the first target is being rendered to. If I render either of the other two buffers to the screen it's a white wash.

 

It does work for the first and as far as I can tell I'm not treating the other two targets any differently, so I'm having a bit of trouble deciphering where the root of this problem is. If anyone could point me in the right direction that would be great!

 

Oh and I'm using the fixed function pipeline as a placeholder to draw the texture since it was a quick solution.

 

Renderer:

public class DeferredRenderer {

    public static final int POSITION_BUFFER_BINDING = 0;
    public static final int NORMAL_BUFFER_BINDING = 1;
    public static final int DIFFUSE_BUFFER_BINDING = 2;
    public static final int SCREEN_BUFFER_BINDING = 3;

    private final Texture positionBuffer;
    private final Texture normalBuffer;
    private final Texture diffuseBuffer;

    private final Texture screenTexture;

    private final FBO fbo;
    private final RBO fboDepthBuffer;

    public DeferredRenderer(final int width, final int height) throws GraphicsException{

        GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glEnable(GL11.GL_CULL_FACE); 

        positionBuffer = new Texture(width, height, POSITION_BUFFER_BINDING, OGLColorType.RGBA16F);
        normalBuffer = new Texture(width, height, NORMAL_BUFFER_BINDING, OGLColorType.RGBA16F);
        diffuseBuffer = new Texture(width, height, DIFFUSE_BUFFER_BINDING, OGLColorType.RGBA16F);

        screenTexture = new Texture(width, height, SCREEN_BUFFER_BINDING, OGLColorType.RGBA8);

        fbo = new FBO();
        fboDepthBuffer = new RBO(width, height, AttachmentType.DEPTH_ATTACHMENT);

        fbo.bind();
        fboDepthBuffer.bind();

        positionBuffer.attachToFBO(AttachmentType.COLOR_ATTACHMENT0);
        normalBuffer.attachToFBO(AttachmentType.COLOR_ATTACHMENT1);
        diffuseBuffer.attachToFBO(AttachmentType.COLOR_ATTACHMENT2);

        final IntBuffer buffer = BufferUtils.createIntBuffer(3);
        buffer.put(0, AttachmentType.COLOR_ATTACHMENT0.ID);
        buffer.put(1, AttachmentType.COLOR_ATTACHMENT1.ID);
        buffer.put(2, AttachmentType.COLOR_ATTACHMENT2.ID);

        buffer.rewind();
        // Creates an array of buffers that the fragment shader will output to
        GL20.glDrawBuffers(buffer);

        if(!fbo.checkForErrors()) {
            throw new GraphicsException("Could not compile FBO");
        }

        fboDepthBuffer.unbind();
        fbo.unbind();
    }

    public void beginDrawing() {

        fbo.bind();

        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

    }

    public void endDrawing() {

        fbo.unbind();

        diffuseBuffer.bind();
        GL11.glBegin(GL11.GL_QUADS);

        GL11.glTexCoord2f(0.0f, 0.0f);
        GL11.glVertex3f(-1.0f, -1.0f, 0.0f); // The bottom left corner

        GL11.glTexCoord2f(0.0f, 1.0f);
        GL11.glVertex3f(-1.0f, 1.0f, 0.0f); // The top left corner

        GL11.glTexCoord2f(1.0f, 1.0f);
        GL11.glVertex3f(1.0f, 1.0f, 0.0f); // The top right corner

        GL11.glTexCoord2f(1.0f, 0.0f);
        GL11.glVertex3f(1.0f, -1.0f, 0.0f); // The bottom right corner

        GL11.glEnd();

        diffuseBuffer.unbind();
    }

    public void destroy() {

        positionBuffer.destroy();
        normalBuffer.destroy();
        diffuseBuffer.destroy();
    }
}

Vertex shader:

#version 330 core
uniform mat4 u_projectionMatrix;
uniform mat4 u_viewMatrix;
uniform mat4 u_modelMatrix;

in vec4 in_Position;
in vec3 in_Normal;
in vec2 in_TextureCoord;

out vec4 pass_Position;
out vec4 pass_Normal;
out vec2 pass_TextureCoord;


void main(void) {

    mat4 mvp = u_projectionMatrix * u_viewMatrix * u_modelMatrix;

    pass_Position = mvp * in_Position;
    pass_Normal = vec4(normalize(mat3(mvp) * in_Normal), 1);

	pass_TextureCoord = in_TextureCoord;

	gl_Position = mvp * in_Position;
}

Fragment shader:

 

(I know I'm not passing the correct values, I just want to fix this problem and pass data through all the buffers before continuing on)

#version 330 core

uniform sampler2D texture_diffuse;

in vec4 pass_Position;
in vec4 pass_Normal;
in vec2 pass_TextureCoord;


void main(void) {

	vec4 texel = texture(texture_diffuse, pass_TextureCoord);

	gl_FragData[0] = texel;
	gl_FragData[1] = pass_Normal;
	gl_FragData[2] = texel;
}
Edited by GameGeezer

Share this post


Link to post
Share on other sites

Figured it out, In a tutorial I used the author treated the attachment Ids like they were relative: COLOR_ATTACHMENT1 = COLOR_ATTACHMENT0 + 1. They aren't relative to each other for some reason in lwjgl so I just had to change the Id's.

Edited by GameGeezer

Share this post


Link to post
Share on other sites

. They aren't relative to each other for some reason in lwjgl 
They do are from what I've used in LWJGL 2.9.2 and LWJGL 3. They take the values directly from OGL headers.

 

This is from LWJGL 3:

GL_COLOR_ATTACHMENT0        = 0x8CE0, // Thats 36064
GL_COLOR_ATTACHMENT1        = 0x8CE1, // 36065
GL_COLOR_ATTACHMENT2        = 0x8CE2, // 36066
... etc

Share this post


Link to post
Share on other sites

Oh I just assumed since incrementing from ATTACHMENT0 was the root of my error that they couldn't be. Must have been another small bug that accidently got tossed out along with that code.

Edited by GameGeezer

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