Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module ws.gl.gbuffer;
- import
- std.string,
- ws.gl.gl;
- class GBuffer {
- private {
- GLuint fbo;
- GLuint textures[NUM];
- GLuint depth;
- }
- this(int w, int h){
- // Create the FBO
- glGenFramebuffersEXT(1, &fbo);
- glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
- // Create the gbuffer textures
- glGenTextures(textures.length, textures.ptr);
- for(uint i = 0; i < textures.length; i++){
- glBindTexture(GL_TEXTURE_2D, textures[i]);
- glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB32F, w, h);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0);
- }
- // depth
- glGenTextures(1, &depth);
- glBindTexture(GL_TEXTURE_2D, depth);
- glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT32F, w, h);
- glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depth, 0);
- GLenum[] drawBuffers;
- for(int i=0; i<NUM; i++)
- drawBuffers ~= GL_COLOR_ATTACHMENT0+i;
- glDrawBuffers(cast(uint)drawBuffers.length, drawBuffers.ptr);
- GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
- if(status != GL_FRAMEBUFFER_COMPLETE)
- throw new Exception("FB error %s".format(status));
- // restore default FBO
- glBindFramebuffer(GL_FRAMEBUFFER, 0);
- }
- void destroy(){
- if(fbo)
- glDeleteFramebuffers(1, &fbo);
- if(textures[0])
- glDeleteTextures(textures.length, textures.ptr);
- if(depth)
- glDeleteRenderbuffers(1, &depth);
- }
- void setRead(int type){
- glReadBuffer(GL_COLOR_ATTACHMENT0 + type);
- }
- void bindWrite(){
- glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
- }
- void bindRead(){
- glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
- }
- enum: int {
- DIFFUSE, NUM
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement