Commit b3157152 authored by Robert Menzel's avatar Robert Menzel

added functionality to clear FBOs

parent 7eea43ac
......@@ -47,14 +47,21 @@ class FrameBufferObject
// ============================================================================================ STRUCTS \/
// ==================================================================================================== \/
public:
//! An attachment can be a texture or a render buffer
/*struct Attachment
{
std::string name; // user defined name that matches the fragment shader out
ConstSharedTexture texture; // attached color texture, or:
ConstSharedRenderBuffer renderBuffer; // attached renderbuffer - only this or the texture should be set!
GLuint location; // the frag data location that maps to this attachment
};*/
//! defines a clear color per color buffer
struct ClearColor {
ClearColor() {
mType = Float;
mColor.floatColor[0] = mColor.floatColor[1] = mColor.floatColor[2] = mColor.floatColor[3] = 0.0f;
}
enum Type { Integer, UnsignedInteger, Float };
union Data {
GLint intColor[4];
GLuint uintColor[4];
GLfloat floatColor[4];
} mColor;
Type mType; // defines which of the data types of the union to use
};
//! An attachment can be a texture or a render buffer
struct Attachment
......@@ -64,6 +71,7 @@ public:
ConstSharedRenderBuffer renderBuffer; // attached renderbuffer - only this or the texture should be set!
GLuint location; // the frag data location that maps to this attachment
Image image; // in case of a texture the image will define which part of the texture to use
ClearColor clearColor; // clear color of the attachement
};
// ===================================================================================================== \/
......@@ -133,25 +141,25 @@ public:
*/
inline bool attachColorRenderBuffer(const std::string &_name, const ConstSharedRenderBuffer& _renderBuffer)
{
Attachment a = {_name, SharedTextureBase(), _renderBuffer, 0xFFFFFFFF, Image()};
Attachment a = {_name, SharedTextureBase(), _renderBuffer, 0xFFFFFFFF, Image(), ClearColor()};
return attachColorAttachment( a );
}
inline bool attachColorTexture(const std::string &_name, const ConstSharedTextureBase& _texture, const Image _image = Image() )
{
Attachment a = {_name, _texture, SharedRenderBuffer(), 0xFFFFFFFF, _image};
Attachment a = {_name, _texture, SharedRenderBuffer(), 0xFFFFFFFF, _image, ClearColor()};
return attachColorAttachment( a );
}
inline bool attachColorRenderBuffer(const std::string &_name, const ConstSharedRenderBuffer& _renderBuffer, GLuint _location )
{
Attachment a = {_name, SharedTextureBase(), _renderBuffer, _location, Image()};
Attachment a = {_name, SharedTextureBase(), _renderBuffer, _location, Image(), ClearColor()};
return attachColorAttachment( a );
}
inline bool attachColorTexture(const std::string &_name, const ConstSharedTextureBase& _texture, GLuint _location, const Image _image = Image() )
{
Attachment a = {_name, _texture, SharedRenderBuffer(), _location, _image};
Attachment a = {_name, _texture, SharedRenderBuffer(), _location, _image, ClearColor()};
return attachColorAttachment( a );
}
......@@ -243,6 +251,33 @@ public:
//! get the part of the framebuffer thats part of the current viewport
static SharedTextureData getImageData();
//
// clear buffer functions:
//
//! clear only the depth buffer:
void clearDepthBuffer();
//! clear one specific color buffer:
void clearBuffer( const std::string &_name );
//! clear all buffers, color and depth:
void clearBuffers();
//! sets the clear color for one buffer:
void setClearColor( const std::string &_name, const glm::vec4 &_color );
//! sets the clear color for one buffer:
void setClearColor( const std::string &_name, const glm::ivec4 &_color );
//! sets the clear color for one buffer:
void setClearColor( const std::string &_name, const glm::uvec4 &_color );
//! sets the clear color for all color buffers:
void setClearColor( const glm::vec4 &_color );
//! sets the clear color for all color buffers:
void setClearColor( const glm::ivec4 &_color );
//! sets the clear color for all color buffers:
void setClearColor( const glm::uvec4 &_color );
// =================================================================================================== \/
// ============================================================================================ FIELDS \/
// =================================================================================================== \/
......
......@@ -330,3 +330,123 @@ SharedTextureData FrameBufferObject::getImageData()
return FrameBufferObject::getImageData( currentViewport.getWidth(), currentViewport.getHeight(), currentViewport.getOffsetX(), currentViewport.getOffsetY() );
}
//! clear only the depth buffer:
void FrameBufferObject::clearDepthBuffer()
{
if (!mDepthAttachment.texture && !mDepthAttachment.renderBuffer) return;
bind(); // NOTE: if the old binding gets restored (removing this side effect), update clearBuffers() as it relies on it!
glClear( GL_DEPTH_BUFFER_BIT );
}
//! clear one specific color buffer:
void FrameBufferObject::clearBuffer( const std::string &_name )
{
int index = getColorAttachmentIndexByName( _name );
if (index == -1) {
ACGL::Utils::error() << "can't clear color buffer " << _name << " - attachment does not exist" << std::endl;
}
bind();
if (mColorAttachments[index].clearColor.mType == ClearColor::Float) {
glClearBufferfv( GL_COLOR, mColorAttachments[index].location, mColorAttachments[index].clearColor.mColor.floatColor);
} else if (mColorAttachments[index].clearColor.mType == ClearColor::Integer) {
glClearBufferiv( GL_COLOR, mColorAttachments[index].location, mColorAttachments[index].clearColor.mColor.intColor);
} else {
glClearBufferuiv( GL_COLOR, mColorAttachments[index].location, mColorAttachments[index].clearColor.mColor.uintColor);
}
}
//! clear all buffers, color and depth:
void FrameBufferObject::clearBuffers()
{
clearDepthBuffer(); // will also bind this FBO
for (unsigned int i = 0; i < mColorAttachments.size(); ++i) {
if (mColorAttachments[i].clearColor.mType == ClearColor::Float) {
glClearBufferfv( GL_COLOR, mColorAttachments[i].location, mColorAttachments[i].clearColor.mColor.floatColor);
} else if (mColorAttachments[i].clearColor.mType == ClearColor::Integer) {
glClearBufferiv( GL_COLOR, mColorAttachments[i].location, mColorAttachments[i].clearColor.mColor.intColor);
} else {
glClearBufferuiv( GL_COLOR, mColorAttachments[i].location, mColorAttachments[i].clearColor.mColor.uintColor);
}
}
}
//! sets the clear color for one buffer:
void FrameBufferObject::setClearColor( const std::string &_name, const glm::vec4 &_color )
{
int index = getColorAttachmentIndexByName( _name );
if (index == -1) {
ACGL::Utils::error() << "can't set clear color of " << _name << " - attachment does not exist" << std::endl;
}
mColorAttachments[index].clearColor.mType = ClearColor::Float;
mColorAttachments[index].clearColor.mColor.floatColor[0] = _color.r;
mColorAttachments[index].clearColor.mColor.floatColor[1] = _color.g;
mColorAttachments[index].clearColor.mColor.floatColor[2] = _color.b;
mColorAttachments[index].clearColor.mColor.floatColor[3] = _color.a;
}
//! sets the clear color for one buffer:
void FrameBufferObject::setClearColor( const std::string &_name, const glm::ivec4 &_color )
{
int index = getColorAttachmentIndexByName( _name );
if (index == -1) {
ACGL::Utils::error() << "can't set clear color of " << _name << " - attachment does not exist" << std::endl;
}
mColorAttachments[index].clearColor.mType = ClearColor::Integer;
mColorAttachments[index].clearColor.mColor.intColor[0] = _color.r;
mColorAttachments[index].clearColor.mColor.intColor[1] = _color.g;
mColorAttachments[index].clearColor.mColor.intColor[2] = _color.b;
mColorAttachments[index].clearColor.mColor.intColor[3] = _color.a;
}
//! sets the clear color for one buffer:
void FrameBufferObject::setClearColor( const std::string &_name, const glm::uvec4 &_color )
{
int index = getColorAttachmentIndexByName( _name );
if (index == -1) {
ACGL::Utils::error() << "can't set clear color of " << _name << " - attachment does not exist" << std::endl;
}
mColorAttachments[index].clearColor.mType = ClearColor::UnsignedInteger;
mColorAttachments[index].clearColor.mColor.uintColor[0] = _color.r;
mColorAttachments[index].clearColor.mColor.uintColor[1] = _color.g;
mColorAttachments[index].clearColor.mColor.uintColor[2] = _color.b;
mColorAttachments[index].clearColor.mColor.uintColor[3] = _color.a;
}
//! sets the clear color for all color buffers:
void FrameBufferObject::setClearColor( const glm::vec4 &_color )
{
for (unsigned int i = 0; i < mColorAttachments.size(); ++i) {
mColorAttachments[i].clearColor.mType = ClearColor::Float;
mColorAttachments[i].clearColor.mColor.floatColor[0] = _color.r;
mColorAttachments[i].clearColor.mColor.floatColor[1] = _color.g;
mColorAttachments[i].clearColor.mColor.floatColor[2] = _color.b;
mColorAttachments[i].clearColor.mColor.floatColor[3] = _color.a;
}
}
//! sets the clear color for all color buffers:
void FrameBufferObject::setClearColor( const glm::ivec4 &_color )
{
for (unsigned int i = 0; i < mColorAttachments.size(); ++i) {
mColorAttachments[i].clearColor.mType = ClearColor::Integer;
mColorAttachments[i].clearColor.mColor.intColor[0] = _color.r;
mColorAttachments[i].clearColor.mColor.intColor[1] = _color.g;
mColorAttachments[i].clearColor.mColor.intColor[2] = _color.b;
mColorAttachments[i].clearColor.mColor.intColor[3] = _color.a;
}
}
//! sets the clear color for all color buffers:
void FrameBufferObject::setClearColor( const glm::uvec4 &_color )
{
for (unsigned int i = 0; i < mColorAttachments.size(); ++i) {
mColorAttachments[i].clearColor.mType = ClearColor::UnsignedInteger;
mColorAttachments[i].clearColor.mColor.uintColor[0] = _color.r;
mColorAttachments[i].clearColor.mColor.uintColor[1] = _color.g;
mColorAttachments[i].clearColor.mColor.uintColor[2] = _color.b;
mColorAttachments[i].clearColor.mColor.uintColor[3] = _color.a;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment