RenderBuffer.hh 5.72 KB
Newer Older
1
2
3
4
5
/***********************************************************************
 * Copyright 2011-2012 Computer Graphics Group RWTH Aachen University. *
 * All rights reserved.                                                *
 * Distributed under the terms of the MIT License (see LICENSE.TXT).   *
 **********************************************************************/
Robert Menzel's avatar
Robert Menzel committed
6

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

Robert Menzel's avatar
Robert Menzel committed
10
/**
Robert Menzel's avatar
Robert Menzel committed
11
 * An OpenGL RenderBuffer that can be used with FBOs.
Robert Menzel's avatar
Robert Menzel committed
12
13
14
15
 *
 * 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
16
17
 */

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

20
#include <ACGL/Base/Macros.hh>
Robert Menzel's avatar
Robert Menzel committed
21
22
#include <ACGL/OpenGL/GL.hh>
#include <ACGL/OpenGL/Tools.hh>
23
#include <ACGL/OpenGL/Debug.hh>
24
#include <ACGL/Math/Math.hh>
Robert Menzel's avatar
Robert Menzel committed
25
26

namespace ACGL{
Robert Menzel's avatar
Robert Menzel committed
27
namespace OpenGL{
Robert Menzel's avatar
Robert Menzel committed
28
29
30

class RenderBuffer
{
31
32
    ACGL_NOT_COPYABLE(RenderBuffer)

33
34
35
    // ========================================================================================================= \/
    // ============================================================================================ CONSTRUCTORS \/
    // ========================================================================================================= \/
Robert Menzel's avatar
Robert Menzel committed
36
37
38
public:
    RenderBuffer(
        GLenum _internalFormat)
39
    :   mObjectName(0),
Robert Menzel's avatar
Robert Menzel committed
40
        mInternalFormat(_internalFormat),
41
        mWidth(0),
Robert Menzel's avatar
Robert Menzel committed
42
        mHeight(0)
Robert Menzel's avatar
Robert Menzel committed
43
    {
Robert Menzel's avatar
Robert Menzel committed
44
        mObjectName = 0;
45
        glGenRenderbuffers(1, &mObjectName);
Robert Menzel's avatar
Robert Menzel committed
46
47
48
49
    }

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

54
55
56
57
58
59
60
61
62
63
64
65
66
67
    // ===================================================================================================== \/
    // =========================================================================================== KHR_DEBUG \/
    // ===================================================================================================== \/
public:
    // Sets and gets a label visible inside of a OpenGL debugger if KHR_debug is supported at runtime *and*
    // if ACGL_OPENGL_DEBUGGER_SUPPORT was defined during compile time. Does nothing otherwise!
#ifdef ACGL_OPENGL_DEBUGGER_SUPPORT
    void setObjectLabel( const std::string &_label ) { setObjectLabelT<GL_RENDERBUFFER>(getObjectName(),_label); }
    std::string getObjectLabel() { return getObjectLabelT<GL_RENDERBUFFER>(getObjectName()); }
#else
    void setObjectLabel( const std::string & ) {}
    std::string getObjectLabel() { return ""; }
#endif

68
69
70
    // ==================================================================================================== \/
    // ============================================================================================ GETTERS \/
    // ==================================================================================================== \/
Robert Menzel's avatar
Robert Menzel committed
71
public:
72
    inline GLuint  getObjectName     (void) const { return mObjectName;     }
Robert Menzel's avatar
Robert Menzel committed
73
74
75
    inline GLenum  getInternalFormat (void) const { return mInternalFormat; }
    inline GLsizei getWidth          (void) const { return mWidth;          }
    inline GLsizei getHeight         (void) const { return mHeight;         }
76
    inline glm::uvec2 getSize        (void) const { return glm::uvec2( mWidth, mHeight ); }
Robert Menzel's avatar
Robert Menzel committed
77

78
79
80
81
82
    // ==================================================================================================== \/
    // ============================================================================================ METHODS \/
    // ==================================================================================================== \/
public:
    //! Get the actual number of samples
83
84
85
#ifdef ACGL_OPENGL_ES
    inline int_t getSamples(void) const { return 1; }
#else
86
87
88
89
90
91
92
    inline int_t getSamples(void) const
    {
        glBindRenderbuffer(GL_RENDERBUFFER, mObjectName);
        GLint samples;
        glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
        return (int_t)samples;
    }
93
#endif
94

95
96
97
    // ===================================================================================================== \/
    // ============================================================================================ WRAPPERS \/
    // ===================================================================================================== \/
98
public:    
99
100
101
    //! Bind the renderbuffer
    inline void bind(void) const
    {
102
        glBindRenderbuffer(GL_RENDERBUFFER, mObjectName);
103
104
105
106
107
    }

    //! Set texture size and NULL data
    inline void setSize(
        GLsizei _width,
108
109
        GLsizei _height,
        GLsizei _samples = 0)
110
111
112
    {
        mWidth = _width;
        mHeight = _height;
Robert Menzel's avatar
Robert Menzel committed
113
        // the sample count will not get saved as the real samples returned by GL might differ this number anyway!
114
        glBindRenderbuffer(GL_RENDERBUFFER, mObjectName);
115
116

#if (ACGL_OPENGL_VERSION >= 30)
Robert Menzel's avatar
Robert Menzel committed
117
118
        glRenderbufferStorageMultisample(GL_RENDERBUFFER, _samples, mInternalFormat, mWidth, mHeight);
#else // OpenGL ES, as Desktop GL didn't have renderbuffers pre 3.0 anyway
119
        glRenderbufferStorage(GL_RENDERBUFFER, mInternalFormat, mWidth, mHeight);
120
121
#endif // OpenGL >= 3.0

122
123
    }

124
125
126
    // =================================================================================================== \/
    // ============================================================================================ FIELDS \/
    // =================================================================================================== \/
Robert Menzel's avatar
Robert Menzel committed
127
protected:
128
    GLuint  mObjectName;
Robert Menzel's avatar
Robert Menzel committed
129
130
131
132
133
    GLenum  mInternalFormat;
    GLsizei mWidth;
    GLsizei mHeight;
};

Robert Menzel's avatar
Robert Menzel committed
134
ACGL_SMARTPOINTER_TYPEDEFS(RenderBuffer)
Robert Menzel's avatar
Robert Menzel committed
135
136

} // OpenGL
Robert Menzel's avatar
Robert Menzel committed
137
138
} // ACGL

139
#endif // ACGL_OPENGL_OBJECTS_RENDERBUFFER_HH