Currently job artifacts in CI/CD pipelines on LRZ GitLab never expire. Starting from Wed 26.1.2022 the default expiration time will be 30 days (GitLab default). Currently existing artifacts in already completed jobs will not be affected by the change. The latest artifacts for all jobs in the latest successful pipelines will be kept. More information: https://gitlab.lrz.de/help/user/admin_area/settings/continuous_integration.html#default-artifacts-expiration

vertexarrayobject.cpp 6.15 KB
Newer Older
schultezub's avatar
schultezub committed
1
#include "vertexarrayobject.h"
2
3
4
5
6
7
8
9
10

#include "tgt/assert.h"
#include "tgt/buffer.h"
#include "tgt/shadermanager.h"

namespace tgt {

    const std::string VertexAttribute::loggerCat_ = "tgt.VertexAttribute";

11
    VertexAttribute::VertexAttribute(GLuint index, BufferObject* bufferObject, GLsizei stride, size_t offset)
12
13
14
15
16
17
18
19
20
        : _index(index)
        , _stride(stride)
        , _offset(offset)
        , _bufferObject(bufferObject)
    {
        tgtAssert(_bufferObject != 0, "BufferObject must not be 0.");

        _bufferObject->bindToVertexAttribute(this);
        // Todo: implement normalized flag if needed
21
        _bufferObject->bind();
22
        glVertexAttribPointer(_index, static_cast<GLint>(_bufferObject->getElementSize()), _bufferObject->getBaseType(), false, _stride, reinterpret_cast<void*>(_offset));
23
24
25
26
27
28
29
30
    }

    VertexAttribute::~VertexAttribute() {
        _bufferObject->unbindFromVertexAttribute(this);
    }

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

31
32
33
    size_t VertexArrayObject::_currentlyBoundVertexArray = 0;
    bool VertexArrayObject::_initialized = true;

34
35
    const std::string VertexArrayObject::loggerCat_ = "tgt.VertexArrayObject";

schultezub's avatar
schultezub committed
36
    VertexArrayObject::VertexArrayObject(bool autoBind) throw (tgt::Exception)
37
        : _id(0)
38
        , _enabledAttributes(16, false)
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
    {
        glGenVertexArrays(1, &_id);
        if (_id == 0) {
            throw new tgt::Exception("Could not create OpenGL Vertex Array.");
        }

        if (autoBind)
            bind();
    }

    VertexArrayObject::~VertexArrayObject() {
        glDeleteVertexArrays(1, &_id);
    }

    void VertexArrayObject::bind() {
        if (!_initialized)
            initStaticMembers();

57
        //if (_currentlyBoundVertexArray != _id) {
58
            glBindVertexArray(_id);
59
60
//             _currentlyBoundVertexArray = _id;
//         }
61
62
63
64
65
66
    }

    void VertexArrayObject::unbind() {
        if (!_initialized)
            initStaticMembers();

67
//        if (_currentlyBoundVertexArray != 0) {
68
            glBindVertexArray(0);
69
70
//             _currentlyBoundVertexArray = 0;
//         }
71
72
    }

73
    void VertexArrayObject::bindIndexBuffer(BufferObject* bufferObject) {
74
75
76
77
78
79
        tgtAssert(bufferObject->getTargetType() == BufferObject::ELEMENT_ARRAY_BUFFER, "Buffer needs to have target type ELEMENT_ARRAY_BUFFER!");

        bind();
        bufferObject->bind();
    }

80
    size_t VertexArrayObject::addVertexAttribute(AttributeType attributeType, BufferObject* bufferObject, GLsizei stride /*= 0*/, size_t offset /*= 0*/, bool enableNow /*= true*/) {
81
82
83
84
85
86
87
88
89
        if (_attributes.size() > 16) {
            // TODO:    The better way would be to check glGet(GL_MAX_VERTEX_ATTRIBS), but the standard says 16 is the minimum
            //          number to be supported and that should be enough and I currently feel lazy. If you're reading this, 
            //          feel free to improve this check...
            LERROR("Could not add VertexAttribute: Tried to add more Vertex Attributes than supported.");
            return 0;
        }

        tgtAssert(
90
            attributeType != UnspecifiedAttribute || _attributeTypeMap.find(attributeType) != _attributeTypeMap.end(), 
91
92
93
94
95
            "Tried to add two VertexAttributes with the same type. This is currently not supported.");

        // bind and create VertexAttribute
        bind();
        size_t index = _attributes.size();
96
        _attributes.push_back(VertexAttribute(static_cast<GLuint>(index), bufferObject, stride, offset));
97
98
99
100
101
102
103
104
105
106
107
108

        // add to attribute-type map
        if (attributeType != UnspecifiedAttribute)
            _attributeTypeMap.insert(std::make_pair(attributeType, index));

        // enable if wanted
        if (enableNow)
            enableVertexAttribute(index);

        return index;
    }

109
    void VertexArrayObject::updateVertexAttribute(size_t index, BufferObject* bufferObject, GLsizei stride /*= 0*/, size_t offset /*= 0*/) {
110
111
112
        tgtAssert(index < _attributes.size(), "Index out of bounds.");

        bind();
113
        _attributes[index] = VertexAttribute(static_cast<GLuint>(index), bufferObject, stride, offset);
114
115
116
117
118
    }

    void VertexArrayObject::enableVertexAttribute(size_t index) {
        tgtAssert(index < _enabledAttributes.size(), "Index out of bounds.");

119
        bind();
120
        glEnableVertexAttribArray(static_cast<GLuint>(index));
121
122
123
        _enabledAttributes[index] = true;
    }

124
125
126
127
128
    void VertexArrayObject::enableAllVertexAttributes() {
        for (size_t i = 0; i < _attributes.size(); ++i)
            enableVertexAttribute(i);
    }

129
130
131
    void VertexArrayObject::disableVertexAttribute(size_t index) {
        tgtAssert(index < _enabledAttributes.size(), "Index out of bounds.");

132
        bind();
133
        glDisableVertexAttribArray(static_cast<GLuint>(index));
134
135
136
        _enabledAttributes[index] = false;
    }

137
138
139
140
141
    void VertexArrayObject::disableAllVertexAttributes() {
        for (size_t i = 0; i < _attributes.size(); ++i)
            disableVertexAttribute(i);
    }

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
    void VertexArrayObject::initStaticMembers() {
        // TODO:    The better way would be to us glGet(GL_MAX_VERTEX_ATTRIBS) as dimension, but the standard says 16 
        //          is the minimum number to be supported and that should be enough and I currently feel lazy. If 
        //          you're reading this, feel free to improve this allocation...
        _currentlyBoundVertexArray = 0;
        _initialized = true;
    }

    size_t VertexArrayObject::getVertexAttributeIndexByType(AttributeType type) const {
        tgtAssert(type != UnspecifiedAttribute, "Type most not be UnspecifiedAttribute, those attributes are not tracked.");

        std::map<AttributeType, size_t>::const_iterator it = _attributeTypeMap.find(type);
        if (it == _attributeTypeMap.end()) {
            tgtAssert(false, "Could not find a VertexAttribute with the given type. Returning 0 as default value.");
            LERROR("Could not find a VertexAttribute with the given type. Returning 0 as default value.");
            return 0;
        }
        return it->second;
    }


163

164
}