buffer.h 4.03 KB
Newer Older
1
2
3
4
5
#ifndef BUFFER_H__
#define BUFFER_H__

#include "tgt/exception.h"
#include "tgt/tgt_gl.h"
6
#include "tgt/types.h"
7
8
9
10
11
12

#include <set>

namespace tgt {
    class VertexAttribute;

13
    class TGT_API BufferObject {
14
15
    public:
        enum TargetType {
16
17
            ARRAY_BUFFER = GL_ARRAY_BUFFER,
            ELEMENT_ARRAY_BUFFER = GL_ELEMENT_ARRAY_BUFFER
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
        };

        enum UsageType {
            USAGE_STATIC_DRAW = GL_STATIC_DRAW,
            USAGE_STATIC_READ = GL_STATIC_READ,
            USAGE_STATIC_COPY = GL_STATIC_COPY,

            USAGE_DYNAMIC_DRAW = GL_DYNAMIC_DRAW,
            USAGE_DYNAMIC_READ = GL_DYNAMIC_READ,
            USAGE_DYNAMIC_COPY = GL_DYNAMIC_COPY,

            USAGE_STREAM_DRAW = GL_STREAM_DRAW,
            USAGE_STREAM_READ = GL_STREAM_READ,
            USAGE_STREAM_COPY = GL_STREAM_COPY
        };

        enum BaseType {
            BYTE = GL_BYTE,
            UNSIGNED_BYTE = GL_UNSIGNED_BYTE,
            SHORT = GL_SHORT,
            UNSIGNED_SHORT = GL_UNSIGNED_SHORT,
            INT = GL_INT,
            UNSIGNED_INT = GL_UNSIGNED_INT,
            FLOAT = GL_FLOAT,
            DOUBLE = GL_DOUBLE
        };

        /**
         * Creates a new OpenGL buffer object and generates an ID for it.
47
48
         * \param   target      Target type of buffer object.
         * \param   usage       Usage of data.
49
50
         * \throw   tgt::Exception when OpenGL buffer generation failed.
         */
51
        BufferObject(TargetType target, UsageType usage) throw (tgt::Exception);
52
53
54
55
56
57
58

        /**
         * Disposes and deletes the OpenGL buffer object.
         */
        virtual ~BufferObject();


59
60
61
62
        /**
         * Gets the base data type in the buffer.
         * \return  _baseType
         */
63
64
        BaseType getBaseType() const;

65
66
67
68
69
70
        /**
         * Gets the target type of this buffer
         * \return  _targetType
         */
        TargetType getTargetType() const;

71
72
73
74
        /**
         * Gets the number of elements per element (1, 2, 3 or 4).
         * \return  _elementSize
         */
75
76
        size_t getElementSize() const;

77
78
79
80
81
        /**
         * Gets the buffer size in bytes on the GPU.
         * \return  _size
         */
        size_t getBufferSize() const;
82

83
84
85
86
87
88
        /**
         * Gets the OpenGL id of this buffer object;
         * \return _id
         */
        GLuint getId() const { return _id; };

89
90
91
92
        /**
         * Binds the buffer object to the current OpenGL context.
         * \param target    Target type of buffer object
         */
93
        void bind();
94
95
96

        /**
         * Binds the buffer, reserves space on the OpenGL device and initializes it with the given data in \a data.
97
98
99
100
         * \param data          Pointer to host memory containing the data.
         * \param numBytes      Size of \a data in bytes.
         * \param baseType      Base data type
         * \param elementSize   Number of elements per element (must be 1, 2, 3 or 4)
101
         */
102
        void data(const void* data, size_t numBytes, BaseType baseType, size_t elementSize);
103
104
105
106
107
108
109

        /**
         * Updates a subset of the buffer's data.
         * \param offset    Offset where to start writing in buffer's data in bytes.
         * \param data      Pointer to host memory containing the data.
         * \param numBytes  Size of \a data in bytes.
         */
110
        void subdata(size_t offset, const void* data, size_t numBytes);
111
112
113
114
115


    protected:
        GLuint _id;                         ///< OpenGL ID of this buffer

116
117
        TargetType _targetType;             ///< Target type of buffer object
        UsageType _usageType;               ///< Usage type of data.
118
119
        BaseType _baseType;                 ///< Base data type
        size_t _elementSize;                ///< Number of elements per element *lol* (must be 1, 2, 3 or 4)
120
121
122
123
124
125
126

        size_t _size;                       ///< Buffer size in bytes
        size_t _numElements;                ///< Number of elements in this buffer
    };
}

#endif // BUFFER_H__