Advertisement
Guest User

Frame Buffer

a guest
Mar 9th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. package engine.renderer;
  2.  
  3. import java.nio.ByteBuffer;
  4.  
  5. import org.lwjgl.BufferUtils;
  6. import org.lwjgl.opengl.Display;
  7. import org.lwjgl.opengl.GL11;
  8. import org.lwjgl.opengl.GL14;
  9. import org.lwjgl.opengl.GL30;
  10. import org.lwjgl.opengl.GL32;
  11.  
  12. import engine.MainGameLoop;
  13. import engine.geometry.BasicShapes;
  14.  
  15. public class FrameBuffer {
  16.  
  17.     private int quadVAO;
  18.    
  19.     private int fbo;
  20.     private int colorTextureID;
  21.     private int depthTextureID;
  22.    
  23.     private int textureWidth;
  24.     private int textureHeight;
  25.    
  26.     public FrameBuffer(int w, int h){
  27.         quadVAO = MainGameLoop.theLoader.loadToVAO(BasicShapes.QUAD_VERTICES, BasicShapes.QUAD_TEXTURE_COORDS);
  28.         textureWidth = w;
  29.         textureHeight = h;
  30.         fbo = createFrameBuffer();
  31.         colorTextureID = createColorTextureAttachment(textureWidth, textureHeight);
  32.         depthTextureID = createDepthTextureAttachment(textureWidth, textureHeight);
  33.     }
  34.  
  35.     public int getColorTexture(){
  36.         return colorTextureID;
  37.     }
  38.    
  39.     public int getDepthTexure(){
  40.         return depthTextureID;
  41.     }
  42.    
  43.     public int getQuadVAO(){
  44.         return quadVAO;
  45.     }
  46.    
  47.     private int createFrameBuffer(){
  48.         int frameBuffer = GL30.glGenFramebuffers();
  49.         GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBuffer);
  50.         GL11.glDrawBuffer(GL30.GL_COLOR_ATTACHMENT0);
  51.         return frameBuffer;
  52.     }
  53.  
  54.     private int createColorTextureAttachment(int width, int height) {
  55.         int texture = GL11.glGenTextures();
  56.         GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
  57.         //HDR rendering: GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB16, width, height, 0, GL11.GL_RGBA, GL11.GL_FLOAT, (ByteBuffer) null);
  58.         //Normal rendering: GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, width, height, 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null);
  59.         GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB16, width, height, 0, GL11.GL_RGBA, GL11.GL_FLOAT, (ByteBuffer) null);
  60.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
  61.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
  62.         GL32.glFramebufferTexture(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, texture, 0);
  63.         return texture;
  64.     }
  65.  
  66.     private int createDepthTextureAttachment(int width, int height){
  67.         int texture = GL11.glGenTextures();
  68.         GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
  69.         GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL14.GL_DEPTH_COMPONENT32, width, height, 0, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, (ByteBuffer) null);
  70.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
  71.         GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
  72.         GL32.glFramebufferTexture(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, texture, 0);
  73.         return texture;
  74.     }
  75.    
  76.     public void bindFrameBuffer(){
  77.         GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
  78.         GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, fbo);
  79.         GL11.glViewport(0, 0, textureWidth, textureHeight);
  80.     }
  81.    
  82.     public void unbindCurrentFrameBuffer() {
  83.         GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
  84.         GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
  85.     }
  86.    
  87.     public void cleanUp(){
  88.         GL30.glDeleteFramebuffers(fbo);
  89.         GL11.glDeleteTextures(colorTextureID);
  90.         GL11.glDeleteTextures(depthTextureID);
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement