RenderBuffer.hh 4.69 KB
Newer Older
Robert Menzel's avatar
Robert Menzel committed
1 2 3 4 5
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University         //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

6 7
#ifndef ACGL_OPENGL_OBJECTS_RENDERBUFFER_HH
#define ACGL_OPENGL_OBJECTS_RENDERBUFFER_HH
Robert Menzel's avatar
Robert Menzel committed
8

Robert Menzel's avatar
Robert Menzel committed
9
/**
Robert Menzel's avatar
Robert Menzel committed
10
 * An OpenGL RenderBuffer that can be used with FBOs.
Robert Menzel's avatar
Robert Menzel committed
11 12 13 14
 *
 * A RenderBuffer is an alternative to a texture as a render target if the later
 * usage as a texture is not needed (e.g. as a depth attachment if only the
 * color attachments are needed from the offscreen renderpass).
Robert Menzel's avatar
Robert Menzel committed
15 16
 */

Robert Menzel's avatar
Robert Menzel committed
17 18
#include <ACGL/ACGL.hh>

19
#include <ACGL/Base/Macros.hh>
Robert Menzel's avatar
Robert Menzel committed
20 21 22
#include <ACGL/OpenGL/GL.hh>
#include <ACGL/OpenGL/Tools.hh>

Robert Menzel's avatar
Robert Menzel committed
23 24

namespace ACGL{
Robert Menzel's avatar
Robert Menzel committed
25
namespace OpenGL{
Robert Menzel's avatar
Robert Menzel committed
26 27 28

class RenderBuffer
{
29 30
    ACGL_NOT_COPYABLE(RenderBuffer)

31 32 33
    // ========================================================================================================= \/
    // ============================================================================================ CONSTRUCTORS \/
    // ========================================================================================================= \/
Robert Menzel's avatar
Robert Menzel committed
34 35 36
public:
    RenderBuffer(
        GLenum _internalFormat)
37
    :   mObjectName(0),
Robert Menzel's avatar
Robert Menzel committed
38
        mInternalFormat(_internalFormat),
39
        mWidth(0),
Robert Menzel's avatar
Robert Menzel committed
40
        mHeight(0)
Robert Menzel's avatar
Robert Menzel committed
41
    {
42
        glGenRenderbuffers(1, &mObjectName);
43 44 45 46
        if (openGLCriticalErrorOccured() ) {
            ACGL::Utils::error() << "could not generate renderbuffer!" << std::endl;
            return;
        }
Robert Menzel's avatar
Robert Menzel committed
47 48 49 50
    }

    virtual ~RenderBuffer(void)
    {
51
        // buffer 0 will get ignored by OpenGL
52
        glDeleteRenderbuffers(1, &mObjectName);
Robert Menzel's avatar
Robert Menzel committed
53 54
    }

55 56 57
    // ==================================================================================================== \/
    // ============================================================================================ GETTERS \/
    // ==================================================================================================== \/
Robert Menzel's avatar
Robert Menzel committed
58
public:
59
    inline GLuint  getObjectName     (void) const { return mObjectName;     }
Robert Menzel's avatar
Robert Menzel committed
60 61 62 63
    inline GLenum  getInternalFormat (void) const { return mInternalFormat; }
    inline GLsizei getWidth          (void) const { return mWidth;          }
    inline GLsizei getHeight         (void) const { return mHeight;         }

64 65 66 67 68 69 70 71 72 73 74 75 76
    // ==================================================================================================== \/
    // ============================================================================================ METHODS \/
    // ==================================================================================================== \/
public:
    //! Get the actual number of samples
    inline int_t getSamples(void) const
    {
        glBindRenderbuffer(GL_RENDERBUFFER, mObjectName);
        GLint samples;
        glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
        return (int_t)samples;
    }

77 78 79
    // ===================================================================================================== \/
    // ============================================================================================ WRAPPERS \/
    // ===================================================================================================== \/
80
public:    
81 82 83
    //! Bind the renderbuffer
    inline void bind(void) const
    {
84
        glBindRenderbuffer(GL_RENDERBUFFER, mObjectName);
85 86 87 88 89
    }

    //! Set texture size and NULL data
    inline void setSize(
        GLsizei _width,
90 91
        GLsizei _height,
        GLsizei _samples = 0)
92 93 94
    {
        mWidth = _width;
        mHeight = _height;
Robert Menzel's avatar
Robert Menzel committed
95
        // the sample count will not get saved as the real samples returned by GL might differ this number anyway!
96
        glBindRenderbuffer(GL_RENDERBUFFER, mObjectName);
97 98

#if (ACGL_OPENGL_VERSION >= 30)
Robert Menzel's avatar
Robert Menzel committed
99 100
        glRenderbufferStorageMultisample(GL_RENDERBUFFER, _samples, mInternalFormat, mWidth, mHeight);
#else // OpenGL ES, as Desktop GL didn't have renderbuffers pre 3.0 anyway
101
        glRenderbufferStorage(GL_RENDERBUFFER, mInternalFormat, mWidth, mHeight);
102 103
#endif // OpenGL >= 3.0

104 105
    }

106 107 108
    // =================================================================================================== \/
    // ============================================================================================ FIELDS \/
    // =================================================================================================== \/
Robert Menzel's avatar
Robert Menzel committed
109
protected:
110
    GLuint  mObjectName;
Robert Menzel's avatar
Robert Menzel committed
111 112 113 114 115
    GLenum  mInternalFormat;
    GLsizei mWidth;
    GLsizei mHeight;
};

Robert Menzel's avatar
Robert Menzel committed
116
ACGL_SMARTPOINTER_TYPEDEFS(RenderBuffer)
Robert Menzel's avatar
Robert Menzel committed
117 118

} // OpenGL
Robert Menzel's avatar
Robert Menzel committed
119 120
} // ACGL

121
#endif // ACGL_OPENGL_OBJECTS_RENDERBUFFER_HH