Render to texture using FBO - result texture is white

I’m trying to render to texture using a FBO. I’m not sure what I’m doing wrong, but when rendering the resulting texture is all white. Below is some of my code. Please tell me if you see what I’m doing wrong. Am I missing something?


// render code

renderToTexture.activate();
GL11.glPushAttrib(GL11.GL_VIEWPORT_BIT);
GL11.glViewport(0, 0, width, height);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

renderNoise();

p1.render();
p2.render();
ball.render();

renderToTexture.deactivate();
GL11.glPopAttrib();

SlickCallable.enterSafeBlock();
int rtt = renderToTexture.getTexture();

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, rtt);

glBegin(GL_QUADS);
glNormal3d(0, 0, 1);
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(1, 0);
glVertex2f(width, 0);
glTexCoord2f(1, 1);
glVertex2f(width, height);
glTexCoord2f(0, 1);
glVertex2f(0, height);
glEnd();

glDisable(GL_TEXTURE_2D);
SlickCallable.leaveSafeBlock();

// fbo helper class
public class FBORenderer {
    private int fbo;
    private int rbo;
    private int texture;
    private int height;
    private int width;
    private ByteBuffer byteBuffer;

    public FBORenderer(int w, int h) {
        fbo = ARBFramebufferObject.glGenFramebuffers();
        texture = GL11.glGenTextures();
        width = w;
        height = h;

        activate();

        byteBuffer = BufferUtils.createByteBuffer(4 * 4 * width * height);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA, GL11.GL_FLOAT, byteBuffer);

        ARBFramebufferObject.glFramebufferTexture2D(ARBFramebufferObject.GL_DRAW_FRAMEBUFFER,
                ARBFramebufferObject.GL_COLOR_ATTACHMENT0, GL11.GL_TEXTURE_2D, texture, 0);

        deactivate();
    }

    public void activate(){
        ARBFramebufferObject.glBindFramebuffer(ARBFramebufferObject.GL_DRAW_FRAMEBUFFER, fbo);
    }

    public void deactivate() {
        ARBFramebufferObject.glBindFramebuffer(ARBFramebufferObject.GL_DRAW_FRAMEBUFFER, 0);
        ARBFramebufferObject.glBindRenderbuffer(ARBFramebufferObject.GL_RENDERBUFFER, 0);
    }

    public int getTexture() {
        return texture;
    }
}

I’m trying to get FBO working too (but in OpenGL ES 2.0). I have not mush luck too :clue:. For me, only glClear works with my FBO.

From my point of view, what you are doing wrong :


byteBuffer = BufferUtils.createByteBuffer(4 * 4 * width * height);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA, GL11.GL_FLOAT, byteBuffer);

==>


GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA, GL11.GL_FLOAT, null);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D,GL11.GL_TEXTURE_MAG_FILTER,GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D,GL11.GL_TEXTURE_MIN_FILTER,GL11.GL_NEAREST);

You don’t have to allocate system memory and you have to define texture filter.
GL11.GL_FLOAT… why ?

ARBFramebufferObject.glBindRenderbuffer(ARBFramebufferObject.GL_RENDERBUFFER, 0);

I see why you change back the render buffer to the default one since you haven’t change it before.

width, height are power of 2 ? (you can have problem with some drivers).

ps : move this post to OpenGL section ?

On deactivation you don’t need to bind the renderbuffer to 0. A renderbuffer is a fake texture that is attached to an FBO when an FBO needs someplace to render to but you don’t want to use a texture. This leads me to my next point, right now your FBO isn’t complete. It does not have everything set up properly so when you render into it, everything is ignored.

Off the top of my head, you need to attach a depth texture or a depth renderbuffer or disable depth tests, you also need to correctly configure the read and draw buffers with glReadBuffer and glDrawBuffer(). I don’t remember the details of this, so you’ll want to read the OpenGL manual.

After you’ve finished creating the fbo and attaching textures and renderbuffers, you should check its completeness with glCheckFramebufferCompleteness() or some similarly named function and use the returned error code to debug what you need to do.

Thanks to both of you!

The render texture stuff was just left overs from some trial and error. sorry :slight_smile:

@Bonbon-Chan you were right about the texture filter. After adding that I got it working! Anyhow, I couldn’t manage to call glTexImage2D with null as the last argument. If I try to create a smaller bytebuffer I get an error telling me that the buffer’s size is x but must be y.