ArrayBuffer.hh 7.36 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_ARRAYBUFFER_HH
#define ACGL_OPENGL_OBJECTS_ARRAYBUFFER_HH
Robert Menzel's avatar
Robert Menzel committed
8

Robert Menzel's avatar
Robert Menzel committed
9
10
#include <ACGL/ACGL.hh>

11
#include <ACGL/Base/Macros.hh>
Robert Menzel's avatar
Robert Menzel committed
12
13
#include <ACGL/OpenGL/GL.hh>
#include <ACGL/OpenGL/Tools.hh>
Robert Menzel's avatar
Robert Menzel committed
14
15
16

#include <string>
#include <vector>
Robert Menzel's avatar
Robert Menzel committed
17
#include <tr1/memory>
Robert Menzel's avatar
Robert Menzel committed
18
19

namespace ACGL{
Robert Menzel's avatar
Robert Menzel committed
20
namespace OpenGL{
Robert Menzel's avatar
Robert Menzel committed
21
22
23

class ArrayBuffer
{
24
    ACGL_NOT_COPYABLE(ArrayBuffer)
25

Robert Menzel's avatar
Robert Menzel committed
26
27
28
29
30
31
32
33
    // ==================================================================================================== \/
    // ============================================================================================ STRUCTS \/
    // ==================================================================================================== \/
public:
    //! Each attribute has a dimension (#components, e.g. normal with x/y/z => 3) and an offset in the stride (in bytes)
    struct Attribute
    {
        std::string name;
34
35
36
37
        GLenum      type;
        GLint       dimension;
        GLint       offset;
        GLboolean   normalized;
Robert Menzel's avatar
Robert Menzel committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    };

    // ===================================================================================================== \/
    // ============================================================================================ TYPEDEFS \/
    // ===================================================================================================== \/
public:
    typedef std::vector< Attribute > AttributeVec;

    // ========================================================================================================= \/
    // ============================================================================================ CONSTRUCTORS \/
    // ========================================================================================================= \/
public:
    ArrayBuffer(
        GLenum _usage = GL_STATIC_DRAW,
        GLenum _mode = GL_TRIANGLES)
    :   mContext(0),
        mUsage(_usage),
        mMode(_mode),
        mElements(0),
        mStride(0),
        mAttributes()
    {
        glGenBuffers(1, &mContext);
61
62
63
        if (openGLCriticalErrorOccured() ) {
            ACGL::Utils::error() << "could not generate array buffer!" << std::endl;
        }
Robert Menzel's avatar
Robert Menzel committed
64
65
66
67
    }

    virtual ~ArrayBuffer(void)
    {
68
69
        // buffer 0 will get ignored by OpenGL
        glDeleteBuffers(1, &mContext);
Robert Menzel's avatar
Robert Menzel committed
70
71
72
73
74
75
    }

    // ==================================================================================================== \/
    // ============================================================================================ GETTERS \/
    // ==================================================================================================== \/
public:
76
    inline       GLuint        getContext    (void) const { return mContext;    }
Robert Menzel's avatar
Robert Menzel committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    inline       GLenum        getUsage      (void) const { return mUsage;      }
    inline       GLenum        getMode       (void) const { return mMode;       }
    inline       GLsizei       getElements   (void) const { return mElements;   }
    inline       GLsizei       getStride     (void) const { return mStride;     }
    inline const AttributeVec& getAttributes (void) const { return mAttributes; }

    // ==================================================================================================== \/
    // ============================================================================================ METHODS \/
    // ==================================================================================================== \/
public:
    int_t getAttributeIndexByName(const std::string& _nameInArray) const;

    inline void attachAttribute(
        const std::string& _name,
        GLenum _type,
92
93
        GLint _dimension,
        GLboolean _normalized = GL_FALSE)
Robert Menzel's avatar
Robert Menzel committed
94
95
96
97
98
    {
        Attribute attribute = {
            _name,
            _type,
            _dimension,
99
100
            mStride,
            _normalized};
Robert Menzel's avatar
Robert Menzel committed
101
        mStride += getGLTypeSize(_type) * _dimension;
Robert Menzel's avatar
Robert Menzel committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
        mAttributes.push_back(attribute);
    }

    inline void removeAttributes(void)
    {
        mStride = 0;
        mAttributes.clear();
    }

    // ===================================================================================================== \/
    // ============================================================================================ WRAPPERS \/
    // ===================================================================================================== \/
public:
    void render(void) const;

    void draw(void) const
    {
        glDrawArrays(mMode, 0, mElements);
    }

122
123
124
125
126
    void draw(GLint _first, GLsizei _count) const
    {
        glDrawArrays(mMode, _first, _count);
    }

Robert Menzel's avatar
Robert Menzel committed
127
128
129
130
131
132
    //! Bind this buffer
    inline void bind(void) const
    {
        glBindBuffer(GL_ARRAY_BUFFER, mContext);
    }

133
    inline void setAttributePointer(AttributeVec::size_type _indexInArray, GLuint _indexInShader) const
Robert Menzel's avatar
Robert Menzel committed
134
135
136
137
138
    {
        glVertexAttribPointer(
            _indexInShader,
            mAttributes[_indexInArray].dimension,
            mAttributes[_indexInArray].type,
139
            mAttributes[_indexInArray].normalized,
Robert Menzel's avatar
Robert Menzel committed
140
141
142
143
            mStride,
            reinterpret_cast<GLvoid*>(mAttributes[_indexInArray].offset));
    }

144
    inline bool setAttributePointer(const std::string& _nameInArray, GLuint _indexInShader) const
Robert Menzel's avatar
Robert Menzel committed
145
146
147
148
149
    {
        int_t index = getAttributeIndexByName(_nameInArray);
        if(index == -1)
            return false;

150
        setAttributePointer(index, _indexInShader);
Robert Menzel's avatar
Robert Menzel committed
151
152
153
154
        return true;
    }

    //! Set data for this buffer
155
156
    //! Note: The function is not const, because it changes the corresponding GPU data
    inline void setData(const GLvoid* _pData)
Robert Menzel's avatar
Robert Menzel committed
157
    {
158
        glBindBuffer(GL_ARRAY_BUFFER, mContext);
Robert Menzel's avatar
Robert Menzel committed
159
160
161
162
163
164
165
166
        glBufferData(
            GL_ARRAY_BUFFER,
            mStride * mElements,
            _pData,
            mUsage);
    }

    //! Set data for this buffer and change its size
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
    inline void setData(
        const GLvoid* _pData,
        GLsizei _elements)
    {
        mElements = _elements;
        glBindBuffer(GL_ARRAY_BUFFER, mContext);
        glBufferData(
            GL_ARRAY_BUFFER,
            mStride * mElements,
            _pData,
            mUsage);
    }

    //! Set data for this buffer and change its size
    inline void setData(
        const GLvoid* _pData,
        GLsizei _elements,
        GLenum _usage,
        GLenum _mode)
Robert Menzel's avatar
Robert Menzel committed
186
187
    {
        mElements = _elements;
188
189
        mUsage = _usage;
        mMode = _mode;
190
        glBindBuffer(GL_ARRAY_BUFFER, mContext);
Robert Menzel's avatar
Robert Menzel committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
        glBufferData(
            GL_ARRAY_BUFFER,
            mStride * mElements,
            _pData,
            mUsage);
    }

    // =================================================================================================== \/
    // ============================================================================================ FIELDS \/
    // =================================================================================================== \/
protected:
    GLuint       mContext;
    GLenum       mUsage;
    GLenum       mMode;
    GLsizei      mElements;
    GLsizei      mStride;
    AttributeVec mAttributes;
};

210
ACGL_SHARED_TYPEDEF(ArrayBuffer)
Robert Menzel's avatar
Robert Menzel committed
211
212

} // OpenGL
Robert Menzel's avatar
Robert Menzel committed
213
214
} // ACGL

215
#endif // ACGL_OPENGL_OBJECTS_ARRAYBUFFER_HH