Search code examples
javaopenglrenderinglwjglframebuffer

Can't draw to FrameBufferObject, then draw to frameBuffer, in LWJGL


My problem is that I have drawn a small quad to a Frame Buffer Object and am trying to draw it to the framebuffer on the window. This is my java code:

import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.opengl.GL11.*;
import java.nio.ByteBuffer;
import org.lwjgl.opengl.*;

public class Main
{
    private static int fboID;
    private static int texID;
    private static int depthBuff;

    public static void main(String[] args)
    {
        try
        {
            Display.setDisplayMode(new DisplayMode(640, 480));
            Display.create();
        } catch (Exception e)
        {
            e.printStackTrace();
            System.exit(1);
        }

        glEnable(GL_TEXTURE_2D);
        initFramebuffer();
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 640, 480, 0, 1, -1);
        glMatrixMode(GL_MODELVIEW);

        while (!Display.isCloseRequested())
        {
            glBindTexture(GL_TEXTURE_2D, texID);
            glBegin(GL_QUADS);

            glTexCoord2f(0, 0);
            glVertex2i(10, 10);

            glTexCoord2f(0, 1);
            glVertex2i(10, 470);

            glTexCoord2f(1, 1);
            glVertex2i(630, 470);

            glTexCoord2f(1, 0);
            glVertex2i(630, 10);

            glEnd();
            glBindTexture(GL_TEXTURE_2D, 0);

            Display.update();
            Display.sync(60);
        }
    }

    public static void initFramebuffer()
    {
        fboID = glGenFramebuffers();
        glBindFramebuffer(GL_FRAMEBUFFER, fboID);

        texID = glGenTextures();
        glBindTexture(GL_TEXTURE_2D, texID);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 640, 480, 0, GL_RGBA, GL_UNSIGNED_BYTE, (ByteBuffer) null);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

        depthBuff = glGenRenderbuffers();
        glBindRenderbuffer(GL_RENDERBUFFER, depthBuff);
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 640, 480);
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuff);

        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texID, 0);

        if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
            System.out.println("GOD DAMT");

        glBindFramebuffer(GL_FRAMEBUFFER, fboID);
        {
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            glOrtho(0, 640, 480, 0, 1, -1);
            glMatrixMode(GL_MODELVIEW);

            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glViewport(0, 0, 640, 480);

            //glLoadIdentity();

            glBegin(GL_QUADS);
            glColor3f(1, 0, 0);
            glVertex2i(0, 0);
            glVertex2i(100, 0);
            glVertex2i(100, 100);
            glVertex2i(0, 100);
            glEnd();
        }
        glBindFramebuffer(GL_FRAMEBUFFER, 0);

    }
}

Whenever I run this code I get nothing but a black screen. What am I doing wrong?


Solution

  • I'm not tooooo familar with OpenGL but

    Instead of

        glEnable(GL_TEXTURE_2D);
        initFramebuffer();
    

    you should set texturing enabled after initializing fb:

        initFramebuffer();
        glEnable(GL_TEXTURE_2D);
    

    this is caused by glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); which seems to render uninitialized (non-fb texture) in front of your fb texture box, which causes blank screen