Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.lwjgl.opengl.ARBFramebufferObject;
- import org.lwjgl.opengl.GL11;
- import org.lwjgl.opengl.GL14;
- public class RenderTarget {
- int colorTextureID, framebufferID, depthRenderBufferID,
- width,height;
- public RenderTarget(int width, int height){
- this.width= width;
- this.height = height;
- // init our fbo
- framebufferID = ARBFramebufferObject.glGenFramebuffers(); // create a new framebuffer
- colorTextureID = GL11.glGenTextures(); // and a new texture used as a color buffer
- depthRenderBufferID = ARBFramebufferObject.glGenRenderbuffers(); // And finally a new depthbuffer
- ARBFramebufferObject.glBindFramebuffer(ARBFramebufferObject.GL_FRAMEBUFFER, framebufferID); // switch to the new framebuffer
- // initialize color texture
- GL11.glBindTexture(GL11.GL_TEXTURE_2D, colorTextureID); // Bind the colorbuffer texture
- GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); // make it linear filterd
- GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA16, width, height, 0,GL11.GL_RGBA, GL11.GL_INT, (java.nio.ByteBuffer) null); // Create the texture data
- ARBFramebufferObject.glFramebufferTexture2D(ARBFramebufferObject.GL_FRAMEBUFFER ,
- ARBFramebufferObject.GL_COLOR_ATTACHMENT0 ,GL11.GL_TEXTURE_2D, colorTextureID, 0); // attach it to the framebuffer
- // initialize depth renderbuffer
- ARBFramebufferObject.glBindRenderbuffer(ARBFramebufferObject.GL_RENDERBUFFER , depthRenderBufferID); // bind the depth renderbuffer
- ARBFramebufferObject.glRenderbufferStorage(ARBFramebufferObject.GL_RENDERBUFFER , GL14.GL_DEPTH_COMPONENT24, width, height); // get the data space for it
- ARBFramebufferObject.glFramebufferRenderbuffer(ARBFramebufferObject.GL_FRAMEBUFFER ,
- ARBFramebufferObject.GL_DEPTH_ATTACHMENT ,
- ARBFramebufferObject.GL_RENDERBUFFER , depthRenderBufferID); // bind it to the renderbuffer
- ARBFramebufferObject.glBindFramebuffer(ARBFramebufferObject.GL_FRAMEBUFFER , 0); // Swithch back to normal framebuffer rendering
- completenessCheck();
- }
- private void completenessCheck(){
- int framebuffer = ARBFramebufferObject.glCheckFramebufferStatus( ARBFramebufferObject.GL_FRAMEBUFFER );
- switch ( framebuffer ) {
- case ARBFramebufferObject.GL_FRAMEBUFFER_COMPLETE :
- break;
- case ARBFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT :
- throw new RuntimeException( "FrameBuffer: " + framebufferID
- + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT exception" );
- case ARBFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT :
- throw new RuntimeException( "FrameBuffer: " + framebufferID
- + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT exception" );
- case ARBFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER :
- throw new RuntimeException( "FrameBuffer: " + framebufferID
- + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER exception" );
- case ARBFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER :
- throw new RuntimeException( "FrameBuffer: " + framebufferID
- + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER exception" );
- default:
- throw new RuntimeException( "Unexpected reply from glCheckFramebufferStatusEXT: " + framebuffer );
- }
- }
- public void bind(){
- ARBFramebufferObject.glBindFramebuffer(ARBFramebufferObject.GL_FRAMEBUFFER , framebufferID);
- GL11.glPushAttrib(GL11.GL_VIEWPORT_BIT);
- GL11.glViewport( 0, 0, width, height);
- GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
- }
- public void unBind(){
- ARBFramebufferObject.glBindFramebuffer(ARBFramebufferObject.GL_FRAMEBUFFER , 0);
- GL11.glPopAttrib();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement