changed format to screen fbo for a more explicit and safe one

This commit is contained in:
Anselme 2015-12-08 00:09:14 +01:00
parent 80a4bac43b
commit 6f258f295d
3 changed files with 30 additions and 25 deletions

View File

@ -18,7 +18,7 @@ class ForwardModule : public Module
public: public:
ForwardModule() : ForwardModule() :
shaderSources(NULL), shaderSources(NULL),
renderTarget(FrameBuffer::getScreen()) renderTarget(FrameBuffer::screen)
{} {}
virtual void renderGL(Camera* myCamera, Scene* scene); virtual void renderGL(Camera* myCamera, Scene* scene);
@ -47,7 +47,7 @@ private:
ShaderSource* shaderSources; ShaderSource* shaderSources;
std::vector<Shader*> shaders; std::vector<Shader*> shaders;
std::vector<unsigned int> flags; std::vector<unsigned int> flags;
FrameBuffer* renderTarget; const FrameBuffer* renderTarget;
void lightPass(Camera* myCamera, Scene* scene, Light* light, unsigned int type); void lightPass(Camera* myCamera, Scene* scene, Light* light, unsigned int type);
}; };

View File

@ -2,12 +2,11 @@
#include "texture.h" #include "texture.h"
#include "glassert.h" #include "glassert.h"
FrameBuffer::FrameBuffer(bool isNULL) const FrameBuffer* FrameBuffer::screen = new FrameBuffer(0);
FrameBuffer::FrameBuffer()
{ {
if(isNULL) glAssert(glGenFramebuffers(1, &fbo));
fbo = 0;
else
glAssert(glGenFramebuffers(1, &fbo));
} }
FrameBuffer::~FrameBuffer() FrameBuffer::~FrameBuffer()
@ -18,32 +17,34 @@ FrameBuffer::~FrameBuffer()
void FrameBuffer::addTexture(Texture* tex, GLenum attachment) void FrameBuffer::addTexture(Texture* tex, GLenum attachment)
{ {
textures.push_back(tex); if(fbo != 0)
bindFBO(); {
glAssert(glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, tex->getTarget(), tex->getId(), 0)); textures.push_back(tex);
attachments.push_back(attachment); bindFBO();
glAssert(glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, tex->getTarget(), tex->getId(), 0));
attachments.push_back(attachment);
}
} }
void FrameBuffer::initGL() void FrameBuffer::initGL()
{ {
bindFBO(); if(fbo != 0)
glAssert(glDrawBuffers(attachments.size(), attachments.data())); {
bindFBO();
glAssert(glDrawBuffers(attachments.size(), attachments.data()));
}
} }
void FrameBuffer::bindFBO() void FrameBuffer::bindFBO() const
{ {
glAssert(glBindFramebuffer(GL_FRAMEBUFFER, fbo)); glAssert(glBindFramebuffer(GL_FRAMEBUFFER, fbo));
} }
Texture* FrameBuffer::getTexture(int texId) Texture* FrameBuffer::getTexture(int texId)
{ {
return textures[texId]; if(fbo != 0)
return textures[texId];
else
return NULL;
} }
FrameBuffer* FrameBuffer::getScreen()
{
static FrameBuffer* screen = NULL;
if(screen == NULL)
screen = new FrameBuffer(true);
return screen;
}

View File

@ -8,19 +8,23 @@ class Texture;
class FrameBuffer class FrameBuffer
{ {
private:
FrameBuffer(int id) : fbo(id) {}
protected: protected:
GLuint fbo; GLuint fbo;
std::vector<Texture*> textures; std::vector<Texture*> textures;
std::vector<GLuint> attachments; std::vector<GLuint> attachments;
public: public:
FrameBuffer(bool isNULL = false); FrameBuffer();
~FrameBuffer(); ~FrameBuffer();
void addTexture(Texture* tex, GLenum attachment); void addTexture(Texture* tex, GLenum attachment);
void initGL(); void initGL();
void bindFBO(); void bindFBO() const;
Texture* getTexture(int texId); Texture* getTexture(int texId);
static FrameBuffer* getScreen(); static const FrameBuffer* screen;
}; };
#endif // FRAMEBUFFER_H #endif // FRAMEBUFFER_H