Advertisement
Guest User

FrameBuffer

a guest
Oct 27th, 2014
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.99 KB | None | 0 0
  1. public class FrameBuffer {
  2.  
  3.     public FrameBuffer(int width, int height, boolean hasDepth, boolean hasStencil) {
  4.         this.width = width;
  5.         this.height = height;
  6.  
  7.         if ((width + height) % 4 != 0) {
  8.             new Exception("Width + Height must be divisible by 4");
  9.         }
  10.  
  11.         colorTexture = GL11.glGenTextures();
  12.         GL11.glBindTexture(GL11.GL_TEXTURE_2D, colorTexture);
  13.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
  14.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
  15.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
  16.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
  17.  
  18.         GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer)null);
  19.  
  20.         frameBufferID = GL30.glGenFramebuffers();
  21.         GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBufferID);
  22.         GL30.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, GL11.GL_TEXTURE_2D, colorTexture, 0);
  23.  
  24.         if (hasDepth && hasStencil) {
  25.             int depthAndStencilBufferID = GL30.glGenRenderbuffers();
  26.             GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthAndStencilBufferID);
  27.             GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL30.GL_DEPTH24_STENCIL8, width, height);
  28.             GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_STENCIL_ATTACHMENT, GL30.GL_RENDERBUFFER, depthAndStencilBufferID);
  29.         }
  30.  
  31.         else if (hasDepth) {
  32.             int depthBufferID = GL30.glGenRenderbuffers();
  33.             GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthBufferID);
  34.             GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL30.GL_DEPTH_COMPONENT32F, width, height);
  35.             GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL30.GL_RENDERBUFFER, depthBufferID);
  36.         }
  37.  
  38.         else if (hasStencil) {
  39.             int stencilBufferID = GL30.glGenRenderbuffers();
  40.             GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, stencilBufferID);
  41.             GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL30.GL_STENCIL_INDEX16, width, height);
  42.             GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_STENCIL_ATTACHMENT, GL30.GL_RENDERBUFFER, stencilBufferID);
  43.         }
  44.  
  45.         int result = GL30.glCheckFramebufferStatus(GL30.GL_FRAMEBUFFER);
  46.         if (result != GL30.GL_FRAMEBUFFER_COMPLETE) {
  47.             if (result == GL30.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT) {
  48.                 new Exception("Frame Buffer Error: incomplete attachment").printStackTrace();
  49.             }
  50.             if (result == GL30.GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER) {
  51.                 new Exception("Frame Buffer Error: incomplete draw buffer").printStackTrace();
  52.             }
  53.             if (result == GL30.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT) {
  54.                 new Exception("Frame Buffer Error: missing attachment").printStackTrace();
  55.             }
  56.             if (result == GL30.GL_FRAMEBUFFER_UNSUPPORTED) {
  57.                 new Exception("Frame Buffer Error: unsupported combination of formats").printStackTrace();
  58.             }
  59.             if (result == GL30.GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE) {
  60.                 new Exception("Frame Buffer Error: incomplete multisample").printStackTrace();
  61.             }
  62.             if (result == GL30.GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER) {
  63.                 new Exception("Frame Buffer Error: incomplete read buffer").printStackTrace();
  64.             }
  65.  
  66.             new Exception("frame buffer couldn't be constructed: unknown error " + result).printStackTrace();
  67.         }
  68.         GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
  69.     }
  70.  
  71.     private int colorTexture;
  72.  
  73.     private int width, height;
  74.     private int frameBufferID;
  75.  
  76.     public void begin () {
  77.         GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBufferID);
  78.         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
  79.         GL11.glViewport(0, 0, width, height);
  80.     }
  81.  
  82.     public void end () {
  83.         GL11.glViewport(0, 0, RenderingEngine.getWidth(), RenderingEngine.getHeight());
  84.         GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
  85.     }
  86.  
  87.     public int getColorTexture () {
  88.         return colorTexture;
  89.     }
  90.  
  91.     public int getWidth () {
  92.         return width;
  93.     }
  94.  
  95.     public int getHeight () {
  96.         return height;
  97.     }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement