vertexarrayobject.h 6.33 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifndef VERTEXATTRIBUTE_H__
#define VERTEXATTRIBUTE_H__

#include "tgt/exception.h"
#include "tgt/tgt_gl.h"

#include <string>
#include <vector>
#include <map>

namespace tgt {
    class BufferObject;
    class Shader;
    class VertexArrayObject;

    /**
     * Class encapsulating an OpenGL Vertex Attribute state.
     * \see http://www.opengl.org/wiki/Vertex_Array_Object
     */
    class VertexAttribute {
        friend class VertexArrayObject;

    public:
        /**
         * Destructor
         */
        ~VertexAttribute();

    protected:
        /**
         * Creates an OpenGL Vertex Attribute for the data in \a BufferObject and binds it to the uniform
         * named \a uniformName in the given shader.
         * 
         * \note    VertexAttributes shall only be created by VertexArrayObject's member functions, hence the
         *          constructor is protected.
         *          
         * \param   index         Index of the generic vertex attribute to be modified.
         * \param   bufferObject  BufferObject to bind
         */
40
        VertexAttribute(GLuint index, BufferObject* bufferObject, GLsizei stride, size_t offset);
41

42
43
        GLuint _index;                      ///< Index of the generic vertex attribute to be modified.
        GLsizei _stride;                    ///< Specifies the byte offset between consecutive generic vertex attributes.
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
        size_t _offset;                     ///< Offset in bytes to the first element in the buffer.

        BufferObject* _bufferObject;        ///< BufferObject that is bound to this VertexAttribute.

        static const std::string loggerCat_;
    };

// ================================================================================================

    /**
     * Wrapper class for OpenGL Vertex Array Objects.
     * 
     * \note    Because all member methods require a valid locked OpenGL context, we have 
     *          implicit thread-safety.
     */
    class VertexArrayObject {
    public:
        /**
         * Enumeration of different VertexAttribute types.
         * Every added VertexAttribute may have the UnspecifiedAttribute, then its index cannot be
         * re-acquired by getVertexAttributeIndexByType(). In contrast, each other attribute types
         * can only be assigned to one single VertexAttribute which then can be re-acquired by 
         * getVertexAttributeIndexByType().
         */
        enum AttributeType {
            UnspecifiedAttribute,
            VerticesAttribute,
            NormalsAttribute,
            TextureCoordinatesAttribute,
            ColorsAttribute
        };

        VertexArrayObject(bool autoBind = true) throw (tgt::Exception);

        /**
         * Destructor, deletes the whole VAO.
         */
        ~VertexArrayObject();

        /**
         * Binds this VertexArrayObject to the current OpenGL context.
         */
        void bind();

        /**
         * Unbinds this VertexArrayObject from the current OpenGL context.
         */
        void unbind();

        /**
         * Add a VertexAttribute to this VertexArrayObject.
         * A VertexAttribute refers to a BufferObject and defines where to find the (geometry, normal, ...) data
         * in it.
         * \param   attributeType 
         * \param   bufferObject 
         * \param   stride 
         * \param   offset 
         * \return  
         */
103
        size_t addVertexAttribute(AttributeType attributeType, BufferObject* bufferObject, GLsizei stride = 0, size_t offset = 0, bool enable = true);
104
105
106
107
108
109
110
111

        /**
         *
         * \param index 
         * \param bufferObject 
         * \param stride 
         * \param offset 
         */
112
        void updateVertexAttribute(size_t index, BufferObject* bufferObject, GLsizei stride = 0, size_t offset = 0);
113
114
115
116
117
118
119

        /**
         * Enables the VertexAttribute with the given index in the OpenGL client state.
         * \param   index   Index of the VertexAttribute to enable.
         */
        void enableVertexAttribute(size_t index);

120
121
122
123
124
        /**
         * Enables all VertexAttributes of this VAO.
         */
        void enableAllVertexAttributes();

125
126
127
128
129
130
        /**
         * Disables the VertexAttribute with the given index in the OpenGL client state.
         * \param   index   Index of the VertexAttribute to disable.
         */
        void disableVertexAttribute(size_t index);

131
132
133
134
135
        /**
         * Disables all VertexAttributes of this VAO.
         */
        void disableAllVertexAttributes();

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
        /**
         * Returns the index of the VertexAttribute which was previously created with the given type.
         * \note    A VertexAttribute of type \a type must have been created beforehand. Otherwise
         *          this method will raise an error and just return 0 (which is also a perfectly 
         *          valid index).
         * \param   type    Type of the VertexAttribute to search for. Must not be UnspecifiedAttribute, must have been created beforehand!
         * \return  The index of the VertexAttribute, which was created with the given type. If no such
         *          VA was created, this method raises an error and returns 0 as default value (which is
         *          also a perfect valid return value!).
         */
        size_t getVertexAttributeIndexByType(AttributeType type) const;

    protected:
        /**
         * Initializes all static members.
         */
152
        static void initStaticMembers();
153

154
        GLuint _id;                                         ///< Id of the internal OpenGL handle to the VAO.
155
        std::vector<VertexAttribute> _attributes;           ///< List of all VertexAttributes of this VAO.
156
        std::vector<bool> _enabledAttributes;               ///< List of the enabled VertexAttributes.
157
158
159
160
161
162
163
164
165
166
        std::map<AttributeType, size_t> _attributeTypeMap;  ///< Map of the AttributeTypes to their indices in the VertexAttributes list.

        static bool _initialized;                           ///< Flag whether the static members are initialized;
        static size_t _currentlyBoundVertexArray;           ///< Id of the VertexArrayObject which was bound most recently. 0 if the most recent bind call was unbind().

        static const std::string loggerCat_;
    };
}

#endif // VERTEXATTRIBUTE_H__