View Full Version : 2D Lighting using a FrameBuffer

06-22-2016, 12:45 PM
I'm trying to accomplish a 2D lighting effect that takes an ambient light level and attaches scene lights like campfires or torches, etc.

To tie all of these lights together, I'm trying to render them all to a Frame Buffer and then render the Frame Buffer to the screen.

Right now, i'm just trying to render a single image to the Frame Buffer and then render the Frame Buffer to the screen but the image is not being rendered and instead the screen is flashing with a black image.

I'm using the JOGL OpenGL java library but most of this is pure OpenGL.

Please let me know if anything here jumps out to you as a problem:

GL2 gl = drawable.getGL().getGL2();
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f );

// for(int i = 0; i < displayList.size(); i++)
// displayList.get(i).display(this, drawable);

//Generate frame buffer
IntBuffer frameBufferReturn = IntBuffer.allocate(1);
gl.glGenFramebuffers(1, frameBufferReturn);

int frameBufferID = frameBufferReturn.get();

Texture texture = light.getTexture();

//Bind frame buffer and draw
gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, frameBufferID);
gl.glFramebufferTexture2D(GL2.GL_FRAMEBUFFER, GL2.GL_COLOR_ATTACHMENT0, GL2.GL_TEXTURE_2D, texture.getTarget(), 0);


TextureCoords coords = texture.getImageTexCoords();

gl.glTexCoord2d(coords.left(), coords.top()); gl.glVertex2d(0, 0); //Top Left
gl.glTexCoord2d(coords.right(), coords.top()); gl.glVertex2d(256, 0); //Top Right
gl.glTexCoord2d(coords.right(), coords.bottom()); gl.glVertex2d(256, 256); //Bottom Right
gl.glTexCoord2d(coords.left(), coords.bottom()); gl.glVertex2d(0, 256); //Bottom Left

// gl.glColor3d(1,1,1);


gl.glRenderbufferStorage(GL2.GL_RENDERBUFFER, GL2.GL_RGB10_A2, 1280, 720);

gl.glDeleteFramebuffers(1, frameBufferReturn);