Advertisement
WeltEnSTurm

Untitled

Oct 20th, 2014
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.90 KB | None | 0 0
  1. module ws.gl.gbuffer;
  2.  
  3. import
  4.     std.string,
  5.     ws.gl.gl;
  6.  
  7.  
  8. class GBuffer {
  9.  
  10.     private {
  11.         GLuint fbo;
  12.         GLuint textures[NUM];
  13.         GLuint depth;
  14.     }
  15.  
  16.     this(int w, int h){
  17.         // Create the FBO
  18.         glGenFramebuffersEXT(1, &fbo);
  19.         glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
  20.    
  21.         // Create the gbuffer textures
  22.         glGenTextures(textures.length, textures.ptr);
  23.         for(uint i = 0; i < textures.length; i++){
  24.             glBindTexture(GL_TEXTURE_2D, textures[i]);
  25.             glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB32F, w, h);
  26.             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  27.             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  28.             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  29.             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  30.             glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0);
  31.         }
  32.         // depth
  33.         glGenTextures(1, &depth);
  34.         glBindTexture(GL_TEXTURE_2D, depth);
  35.         glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT32F, w, h);
  36.         glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depth, 0);
  37.  
  38.         GLenum[] drawBuffers;
  39.         for(int i=0; i<NUM; i++)
  40.             drawBuffers ~= GL_COLOR_ATTACHMENT0+i;
  41.         glDrawBuffers(cast(uint)drawBuffers.length, drawBuffers.ptr);
  42.         GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  43.         if(status != GL_FRAMEBUFFER_COMPLETE)
  44.             throw new Exception("FB error %s".format(status));
  45.         // restore default FBO
  46.         glBindFramebuffer(GL_FRAMEBUFFER, 0);
  47.     }
  48.  
  49.     void destroy(){
  50.         if(fbo)
  51.             glDeleteFramebuffers(1, &fbo);
  52.         if(textures[0])
  53.             glDeleteTextures(textures.length, textures.ptr);
  54.         if(depth)
  55.             glDeleteRenderbuffers(1, &depth);
  56.     }
  57.  
  58.     void setRead(int type){
  59.         glReadBuffer(GL_COLOR_ATTACHMENT0 + type);
  60.     }
  61.    
  62.     void bindWrite(){
  63.         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
  64.     }
  65.  
  66.     void bindRead(){
  67.         glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
  68.     }
  69.  
  70.     enum: int {
  71.         DIFFUSE, NUM
  72.     };
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement