1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include <GL/glew.h>
#include "FBO.h"
#include "mesh.h"
#include "Vertex_buffer_object.h"
#include "loader.h"
#include <iostream>
FBO::FBO(const Mesh& m): Object(m)
{
this->initTex();
this->initFBO();
this->mesh = m;
}
FBO::~FBO()
{
//dtor
}
void FBO::initTex()
{
Texture* tex = new Texture("data/test.png");
textId = tex->genTexture();
}
void FBO::initFBO()
{
glGenFramebuffers(1, &_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, _fbo);
// attach a texture to FBO color attachment point
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textId, 0);
checkFramebufferStatus();
}
void FBO::draw()
{
// set the rendering destination to FBO
glBindFramebuffer(GL_FRAMEBUFFER, _fbo);
// clear buffer
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
std::cout << "creating VBO mode" << std::endl;
Object* vbo = new Vertex_buffer_object(this->mesh);
vbo->translate(Vec3(8, 0, 0));
vbo->color(Vec3(0, 1, 0));
vbo->rotate(-90, Vec3(1, 0, 0));
vbo->draw();
std::cout << "\t\t\tend." << std::endl;
// back to normal window-system-provided framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0); // unbind
}
void FBO::checkFramebufferStatus()
{
// check FBO status
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
switch(status)
{
case GL_FRAMEBUFFER_COMPLETE:
std::cout << "Framebuffer complete." << std::endl;
break;
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
std::cout << "[ERROR] Framebuffer incomplete: Attachment is NOT complete." << std::endl;
break;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
std::cout << "[ERROR] Framebuffer incomplete: No image is attached to FBO." << std::endl;
break;
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
std::cout << "[ERROR] Framebuffer incomplete: Draw buffer." << std::endl;
break;
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
std::cout << "[ERROR] Framebuffer incomplete: Read buffer." << std::endl;
break;
case GL_FRAMEBUFFER_UNSUPPORTED:
std::cout << "[ERROR] Framebuffer incomplete: Unsupported by FBO implementation." << std::endl;
break;
default:
std::cout << "[ERROR] Framebuffer incomplete: Unknown error." << std::endl;
break;
}
}
|