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

indexedmeshgeometry.cpp 8.02 KB
Newer Older
1
2
3
4
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
6
7
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
// 
11
12
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
13
14
15
16
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
17
// 
18
19
20
21
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// 
// ================================================================================================

#include "indexedmeshgeometry.h"

#include "tgt/assert.h"
#include "tgt/logmanager.h"
#include "tgt/buffer.h"
#include "tgt/vertexarrayobject.h"

#include <algorithm>
#include <list>
#include <utility>

namespace campvis {

    const std::string IndexedMeshGeometry::loggerCat_ = "CAMPVis.core.datastructures.IndexedMeshGeometry";


    IndexedMeshGeometry::IndexedMeshGeometry(
        const std::vector<uint16_t>& indices, 
        const std::vector<tgt::vec3>& vertices, 
        const std::vector<tgt::vec3>& textureCoordinates /*= std::vector<tgt::vec3>()*/, 
        const std::vector<tgt::vec4>& colors /*= std::vector<tgt::vec4>()*/, 
        const std::vector<tgt::vec3>& normals /*= std::vector<tgt::vec3>() */)
        : GeometryData()
        , _indices(indices)
        , _vertices(vertices)
        , _textureCoordinates(textureCoordinates)
        , _colors(colors)
        , _normals(normals)
        , _indicesBuffer(0)
    {
        tgtAssert(textureCoordinates.empty() || textureCoordinates.size() == vertices.size(), "Texture coordinates vector must be either empty or of the same size as the vertex vector.");
        tgtAssert(colors.empty() || colors.size() == vertices.size(), "Colors vector must be either empty or of the same size as the vertex vector.");
        tgtAssert(normals.empty() || normals.size() == vertices.size(), "Normals vector must be either empty or of the same size as the vertex vector.");

    }

61
62
63
64
65
66
67
68
69
70
71
    IndexedMeshGeometry::IndexedMeshGeometry(const IndexedMeshGeometry& rhs)
        : GeometryData(rhs)
        , _indices(rhs._indices)
        , _vertices(rhs._vertices)
        , _textureCoordinates(rhs._textureCoordinates)
        , _colors(rhs._colors)
        , _normals(rhs._normals)
        , _indicesBuffer(0)
    {

    }
72

73
    IndexedMeshGeometry::~IndexedMeshGeometry() {
74
75
        deleteIndicesBuffer();
    }
76

77
78
79
80
81
    IndexedMeshGeometry& IndexedMeshGeometry::operator=(const IndexedMeshGeometry& rhs) {
        if (this == &rhs)
            return *this;

        GeometryData::operator=(rhs);
82
83
84
85
86
        _indices = rhs._indices;
        _vertices = rhs._vertices;
        _textureCoordinates = rhs._textureCoordinates;
        _colors = rhs._colors;
        _normals = rhs._normals;
87
88
89
90
91

        // delete old VBOs and null pointers
        deleteIndicesBuffer();

        return *this;
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    }

    IndexedMeshGeometry* IndexedMeshGeometry::clone() const {
        return new IndexedMeshGeometry(_indices, _vertices, _textureCoordinates, _colors, _normals);
    }

    size_t IndexedMeshGeometry::getLocalMemoryFootprint() const {
        size_t sum = 0;

        if (_indicesBuffer != 0)
            sum += sizeof(tgt::BufferObject);
        if (_verticesBuffer != 0)
            sum += sizeof(tgt::BufferObject);
        if (_texCoordsBuffer != 0)
            sum += sizeof(tgt::BufferObject);
        if (_colorsBuffer != 0)
            sum += sizeof(tgt::BufferObject);
        if (_normalsBuffer != 0)
            sum += sizeof(tgt::BufferObject);

        return sizeof(*this) + sum + (sizeof(size_t) * _indices.size()) + (sizeof(tgt::vec3) * (_vertices.size() + _textureCoordinates.size() + _normals.size())) + (sizeof(tgt::vec4) * _colors.size());
    }

    size_t IndexedMeshGeometry::getVideoMemoryFootprint() const {
        return GeometryData::getVideoMemoryFootprint() + (_indicesBuffer == 0 ? 0 : _indicesBuffer->getBufferSize());
    }


    void IndexedMeshGeometry::render(GLenum mode) const {
121
122
123
        if (_indices.empty())
            return;

124
        createGLBuffers();
125
        if (_buffersDirty) {
126
127
128
129
130
131
            LERROR("Cannot render without initialized OpenGL buffers.");
            return;
        }

        tgt::VertexArrayObject vao;
        if (_verticesBuffer)
132
            vao.setVertexAttributePointer(0, _verticesBuffer);
133
        if (_texCoordsBuffer)
134
            vao.setVertexAttributePointer(1, _texCoordsBuffer);
135
        if (_colorsBuffer)
136
            vao.setVertexAttributePointer(2, _colorsBuffer);
137
        if (_normalsBuffer)
138
            vao.setVertexAttributePointer(3, _normalsBuffer);
139
        vao.bindIndexBuffer(_indicesBuffer);
140
141
142
143
144
145

        glDrawElements(mode, static_cast<GLsizei>(_indices.size()), GL_UNSIGNED_SHORT, 0);
        LGL_ERROR;
    }

    void IndexedMeshGeometry::createGLBuffers() const {
146
147
148
        if (_buffersDirty) {
            deleteBuffers();
            deleteIndicesBuffer();
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

            try {
                _indicesBuffer = new tgt::BufferObject(tgt::BufferObject::ELEMENT_ARRAY_BUFFER, tgt::BufferObject::USAGE_STATIC_DRAW);
                _indicesBuffer->data(&_indices.front(), _indices.size() * sizeof(uint16_t), tgt::BufferObject::UNSIGNED_SHORT, 1);

                _verticesBuffer = new tgt::BufferObject(tgt::BufferObject::ARRAY_BUFFER, tgt::BufferObject::USAGE_STATIC_DRAW);
                _verticesBuffer->data(&_vertices.front(), _vertices.size() * sizeof(tgt::vec3), tgt::BufferObject::FLOAT, 3);

                if (! _textureCoordinates.empty()) {
                    _texCoordsBuffer = new tgt::BufferObject(tgt::BufferObject::ARRAY_BUFFER, tgt::BufferObject::USAGE_STATIC_DRAW);
                    _texCoordsBuffer->data(&_textureCoordinates.front(), _textureCoordinates.size() * sizeof(tgt::vec3), tgt::BufferObject::FLOAT, 3);
                }
                if (! _colors.empty()) {
                    _colorsBuffer = new tgt::BufferObject(tgt::BufferObject::ARRAY_BUFFER, tgt::BufferObject::USAGE_STATIC_DRAW);
                    _colorsBuffer->data(&_colors.front(), _colors.size() * sizeof(tgt::vec4), tgt::BufferObject::FLOAT, 4);
                }
                if (! _normals.empty()) {
                    _normalsBuffer = new tgt::BufferObject(tgt::BufferObject::ARRAY_BUFFER, tgt::BufferObject::USAGE_STATIC_DRAW);
                    _normalsBuffer->data(&_normals.front(), _normals.size() * sizeof(tgt::vec3), tgt::BufferObject::FLOAT, 3);
                }
            }
            catch (tgt::Exception& e) {
                LERROR("Error creating OpenGL Buffer objects: " << e.what());
172
                _buffersDirty = true;
173
174
175
176
                return;
            }

            LGL_ERROR;
177
            _buffersDirty = false;
178
179
        }
    }
180
181
182
183
184
185
186

    tgt::Bounds IndexedMeshGeometry::getWorldBounds() const {
        tgt::Bounds toReturn;
        for (std::vector<tgt::vec3>::const_iterator it = _vertices.begin(); it != _vertices.end(); ++it)
            toReturn.addPoint(*it);
        return toReturn;
    }
187
188
189
190
191
    
    void IndexedMeshGeometry::deleteIndicesBuffer() const {
        delete _indicesBuffer;
        _indicesBuffer = 0;
    }
192

193
194
195
196
197
198
199
200
201
202
203
204
205
    bool IndexedMeshGeometry::hasTextureCoordinates() const {
        return ! _textureCoordinates.empty();
    }

    void IndexedMeshGeometry::applyTransformationToVertices(const tgt::mat4& t) {
        for (size_t i = 0; i < _vertices.size(); ++i) {
            tgt::vec4 tmp = t * tgt::vec4(_vertices[i], 1.f);
            _vertices[i] = tmp.xyz() / tmp.w;
        }

        _buffersDirty = true;
    }

206

207
}