Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "extensions.h"
- #include "bufferObject.h"
- #include "../debug/errors.h"
- #include "../math/matrix.h"
- #include "../core/core.h"
- #include <cstdlib>
- //-----------------------------------------------------------------------------------------------------------------------
- /*
- * Frame buffer/ render buffer class
- * ---------------------------------
- *
- */
- namespace {
- bool using_fbo = false;
- void checkIsFBOAvailable(){
- if (glGenFramebuffersEXT)
- using_fbo = true;
- else
- using_fbo = false;
- }
- bool checkFBOStatus(){
- // check FBO status
- GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
- //using_fbo = false;
- #if defined (CORE_DUMPING) && defined (CORE_DEBUG)
- switch(status)
- {
- //case GL_FRAMEBUFFER_COMPLETE:
- case GL_FRAMEBUFFER_COMPLETE_EXT:
- //fprintf(_STDOUT, "Framebuffer complete. \n" ); fflush(_STDOUT);
- return true;
- //break;
- //case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
- case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
- fprintf(_STDOUT, "[ERROR] Framebuffer incomplete: Attachment is NOT complete." ); fflush(_STDOUT);
- return false;
- //case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
- case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
- fprintf(_STDOUT, "[ERROR] Framebuffer incomplete: No image is attached to FBO." ); fflush(_STDOUT);
- return false;
- //case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
- case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
- fprintf(_STDOUT, "[ERROR] Framebuffer incomplete: Attached images have different dimensions." ); fflush(_STDOUT);
- return false;
- //case GL_FRAMEBUFFER_INCOMPLETE_FORMATS:
- case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
- fprintf(_STDOUT, "[ERROR] Framebuffer incomplete: Color attached images have different internal formats." ); fflush(_STDOUT);
- return false;
- //case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
- case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
- fprintf(_STDOUT, "[ERROR] Framebuffer incomplete: Draw buffer." ); fflush(_STDOUT);
- return false;
- //case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
- case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
- fprintf(_STDOUT, "[ERROR] Framebuffer incomplete: Read buffer." ); fflush(_STDOUT);
- return false;
- //case GL_FRAMEBUFFER_UNSUPPORTED:
- case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
- fprintf(_STDOUT, "[ERROR] Unsupported by FBO implementation." ); fflush(_STDOUT);
- return false;
- default:
- fprintf(_STDOUT, "[ERROR] Unknown error." ); fflush(_STDOUT);
- return false;
- }
- #else
- if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
- return false;
- #endif
- //checkIsFBOAvailable();
- return true;
- }
- }
- FWrender::fbo::fbo(int colorMapNum, int depthMap, int stencilMap, bool _isCreateMipmap)
- {
- this->isGenerateMipmap = _isCreateMipmap;
- this->renderColor = (unsigned int)(colorMapNum)%15;
- this->renderDepth = (depthMap != 0);
- this->renderStencil = (stencilMap != 0);
- this->tex = new FWrender::texture*[colorMapNum];
- for (int i=0; i<colorMapNum; i++){
- this->tex[i] = new FWrender::texture();
- this->tex[i]->props.textureFormat = FWrender::TFM_RGBA;
- this->tex[i]->props.dataFormat = FWrender::TDT_UNSIGNED_BYTE;
- this->tex[i]->props.generateMipmap = _isCreateMipmap;
- if (_isCreateMipmap){
- this->tex[i]->props.magFilter = FWrender::TF_LINEAR;
- this->tex[i]->props.minFilter = FWrender::TF_LINEAR_MIPMAP_LINEAR;
- }
- this->tex[i]->setSize(RENDERER_RENDERBUFFER_SIZE, RENDERER_RENDERBUFFER_SIZE);
- this->tex[i]->buildEmpty();
- }
- if (this->renderDepth){
- this->depthTex = new FWrender::texture();
- this->depthTex->props.depthMode = 1;
- this->depthTex->props.textureFormat = FWrender::TFM_DEPTH_COMPONENT;
- this->depthTex->props.dataFormat = FWrender::TDT_UNSIGNED_INT;
- this->depthTex->setSize(RENDERER_RENDERBUFFER_SIZE, RENDERER_RENDERBUFFER_SIZE);
- this->depthTex->buildEmpty();
- }
- // render stencil
- this->drawBufferList = new GLuint[this->renderColor];
- #if defined (CORE_DUMPING) && defined (CORE_DEBUG)
- fprintf(_STDOUT, "FBO: Generating %d color, %d depth, %d stencil map(s) \n", this->renderColor, this->renderDepth, this->renderStencil);
- fflush(_STDOUT);
- #endif
- checkIsFBOAvailable();
- if (using_fbo)
- init();
- else
- FWcore::releaseFw(1);
- }
- void FWrender::fbo::init(){
- this->fbo_id = 0;
- this->rbo_id = NULL;
- this->rbo_DepthID = 0;
- this->rbo_StencilID = 0;
- //unsigned int spp = FWrender::getSamplePerPixel();
- unsigned int spp = 1;
- //GLuint attachment;
- //if (!this->renderColor) return;
- glGenFramebuffersEXT(1, (GLuint*)&fbo_id);
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, (GLuint)fbo_id);
- if (this->renderColor) {
- this->rbo_id = new int [this->renderColor];
- for (int i=0; i<this->renderColor; i++) {
- glGenRenderbuffersEXT(1, (GLuint*)&rbo_id[i]);
- glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, (GLuint)rbo_id[i]);
- // attach depth component due to depth test.
- if (spp>1) glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT, spp, GL_RGBA8, this->tex[i]->props.sizeX, this->tex[i]->props.sizeY);
- if (i)
- if (spp>1)
- glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT, spp, GL_DEPTH_COMPONENT32, this->tex[i]->props.sizeX, this->tex[i]->props.sizeY);
- else
- glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT32, this->tex[i]->props.sizeX, this->tex[i]->props.sizeY);
- if (i) glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
- glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT+i, GL_TEXTURE_2D, this->tex[i]->getID(), 0);
- this->drawBufferList[i] = GL_COLOR_ATTACHMENT0_EXT+i;
- //glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT+i, GL_RENDERBUFFER_EXT, (GLuint)rbo_id[i]);
- if (i) glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, (GLuint)rbo_id[i]);
- bool status = checkFBOStatus(); using_fbo = status;
- if (!status) FWcore::releaseFw(1);
- //glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
- }
- }
- if (this->renderDepth) {
- //this->rbo_DepthID = 0;
- glGenRenderbuffersEXT(1, (GLuint*)&this->rbo_DepthID);
- glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, (GLuint)this->rbo_DepthID);
- //glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, this->depthTex->props.sizeX, this->depthTex->props.sizeY);
- if (spp>1)
- glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT, spp, GL_DEPTH_COMPONENT32, this->depthTex->props.sizeX, this->depthTex->props.sizeY);
- else
- glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT32, this->depthTex->props.sizeX, this->depthTex->props.sizeY);
- glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
- glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, (GLuint)this->depthTex->getID(), 0);
- //glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, (GLuint)this->rbo_DepthID);
- }
- if (this->renderColor)
- glDrawBuffers(this->renderColor, this->drawBufferList);
- else{
- glDrawBuffer(GL_NONE);
- glReadBuffer(GL_NONE);
- }
- bool status = checkFBOStatus(); using_fbo = status;
- if (!status) FWcore::releaseFw(1);
- glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
- }
- FWrender::fbo::~fbo(){
- if (!using_fbo) return;
- glDeleteFramebuffersEXT(1, (GLuint*)&fbo_id);
- glDeleteRenderbuffersEXT(1, (GLuint*)&rbo_id);
- //if (this->tex) delete this->tex; // hm
- // TODO: DESTRUCT
- }
- void FWrender::fbo::bind(){
- //glPixelTransferf(GL_DEPTH_SCALE, 10);
- //int e = glGetError();
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, this->fbo_id);
- //int e = glGetError();
- }
- void FWrender::fbo::unbind(){
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
- if (this->isGenerateMipmap) for (int i=0; i<this->renderColor; i++){
- this->tex[i]->bind();
- glGenerateMipmapEXT(GL_TEXTURE_2D);
- this->tex[i]->unbind();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement