Advertisement
Guest User

RenderTargetClass

a guest
May 19th, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. import org.lwjgl.opengl.ARBFramebufferObject;
  2. import org.lwjgl.opengl.GL11;
  3. import org.lwjgl.opengl.GL14;
  4.  
  5.  
  6. public class RenderTarget {
  7.    
  8.     int colorTextureID, framebufferID, depthRenderBufferID,
  9.         width,height;
  10.    
  11.     public RenderTarget(int width, int height){
  12.         this.width= width;
  13.         this.height = height;
  14.         // init our fbo
  15.         framebufferID = ARBFramebufferObject.glGenFramebuffers();                                           // create a new framebuffer
  16.         colorTextureID = GL11.glGenTextures();                                              // and a new texture used as a color buffer
  17.         depthRenderBufferID = ARBFramebufferObject.glGenRenderbuffers();                                    // And finally a new depthbuffer
  18.  
  19.         ARBFramebufferObject.glBindFramebuffer(ARBFramebufferObject.GL_FRAMEBUFFER, framebufferID);                         // switch to the new framebuffer
  20.  
  21.         // initialize color texture
  22.         GL11.glBindTexture(GL11.GL_TEXTURE_2D, colorTextureID);                                 // Bind the colorbuffer texture
  23.         GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);               // make it linear filterd
  24.         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
  25.         ARBFramebufferObject.glFramebufferTexture2D(ARBFramebufferObject.GL_FRAMEBUFFER ,
  26.                 ARBFramebufferObject.GL_COLOR_ATTACHMENT0 ,GL11.GL_TEXTURE_2D, colorTextureID, 0); // attach it to the framebuffer
  27.        
  28.        
  29.  
  30.         // initialize depth renderbuffer
  31.         ARBFramebufferObject.glBindRenderbuffer(ARBFramebufferObject.GL_RENDERBUFFER , depthRenderBufferID);                // bind the depth renderbuffer
  32.         ARBFramebufferObject.glRenderbufferStorage(ARBFramebufferObject.GL_RENDERBUFFER , GL14.GL_DEPTH_COMPONENT24,  width, height);   // get the data space for it
  33.         ARBFramebufferObject.glFramebufferRenderbuffer(ARBFramebufferObject.GL_FRAMEBUFFER ,
  34.                 ARBFramebufferObject.GL_DEPTH_ATTACHMENT ,
  35.                 ARBFramebufferObject.GL_RENDERBUFFER , depthRenderBufferID); // bind it to the renderbuffer
  36.  
  37.         ARBFramebufferObject.glBindFramebuffer(ARBFramebufferObject.GL_FRAMEBUFFER , 0);                                    // Swithch back to normal framebuffer rendering
  38.        
  39.        
  40.         completenessCheck();
  41.     }
  42.    
  43.     private void completenessCheck(){
  44.         int framebuffer = ARBFramebufferObject.glCheckFramebufferStatus( ARBFramebufferObject.GL_FRAMEBUFFER  );
  45.         switch ( framebuffer ) {
  46.             case ARBFramebufferObject.GL_FRAMEBUFFER_COMPLETE :
  47.                 break;
  48.             case ARBFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT :
  49.                 throw new RuntimeException( "FrameBuffer: " + framebufferID
  50.                         + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT  exception" );
  51.             case ARBFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT :
  52.                 throw new RuntimeException( "FrameBuffer: " + framebufferID
  53.                         + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT  exception" );
  54.             case ARBFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER :
  55.                 throw new RuntimeException( "FrameBuffer: " + framebufferID
  56.                         + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER  exception" );
  57.             case ARBFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER :
  58.                 throw new RuntimeException( "FrameBuffer: " + framebufferID
  59.                         + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER  exception" );
  60.             default:
  61.                 throw new RuntimeException( "Unexpected reply from glCheckFramebufferStatusEXT: " + framebuffer );
  62.         }
  63.     }
  64.    
  65.     public void bind(){
  66.         ARBFramebufferObject.glBindFramebuffer(ARBFramebufferObject.GL_FRAMEBUFFER , framebufferID);   
  67.         GL11.glPushAttrib(GL11.GL_VIEWPORT_BIT);
  68.         GL11.glViewport( 0, 0, width, height);
  69.         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
  70.     }
  71.    
  72.     public void unBind(){
  73.         ARBFramebufferObject.glBindFramebuffer(ARBFramebufferObject.GL_FRAMEBUFFER , 0);
  74.         GL11.glPopAttrib();
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement