2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

indexedmeshgeometry.cpp 8.97 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
        _pickingInformation = rhs._pickingInformation;
88
89
90
91
92

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

        return *this;
93
94
    }

95
96
97
98
99
100
101
102
103
104
    const std::vector<tgt::col4>& IndexedMeshGeometry::getPickingInformation() const {
        return _pickingInformation;
    }

    void IndexedMeshGeometry::setPickingInformation(const std::vector<tgt::col4>& pickingInformation) {
        tgtAssert(pickingInformation.size() == 0 || pickingInformation.size() == _vertices.size(), "Number of picking informations does not match number of vertices!");
        _pickingInformation = pickingInformation;
        _buffersDirty = true;
    }

105
    IndexedMeshGeometry* IndexedMeshGeometry::clone() const {
106
107
108
        IndexedMeshGeometry* toReturn = new IndexedMeshGeometry(_indices, _vertices, _textureCoordinates, _colors, _normals);
        toReturn->setPickingInformation(_pickingInformation);
        return toReturn;
109
110
111
112
    }

    size_t IndexedMeshGeometry::getLocalMemoryFootprint() const {
        size_t sum = 0;
113
114
115
116
        for (size_t i = 0; i < NUM_BUFFERS; ++i) {
            if (_buffers[i] != nullptr)
                sum += sizeof(tgt::BufferObject);
        }
117
118
119
120
121
122
123
124
125
126

        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 {
127
128
129
        if (_indices.empty())
            return;

130
        createGLBuffers();
131
        if (_buffersDirty) {
132
133
134
135
136
137
            LERROR("Cannot render without initialized OpenGL buffers.");
            return;
        }

        tgt::VertexArrayObject vao;
        if (_verticesBuffer)
138
            vao.setVertexAttributePointer(0, _verticesBuffer);
139
        if (_texCoordsBuffer)
140
            vao.setVertexAttributePointer(1, _texCoordsBuffer);
141
        if (_colorsBuffer)
142
            vao.setVertexAttributePointer(2, _colorsBuffer);
143
        if (_normalsBuffer)
144
            vao.setVertexAttributePointer(3, _normalsBuffer);
145
146
        if (_pickingBuffer)
            vao.setVertexAttributePointer(4, _pickingBuffer);
147
        vao.bindIndexBuffer(_indicesBuffer);
148
149
150
151
152
153

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

    void IndexedMeshGeometry::createGLBuffers() const {
154
155
156
        if (_buffersDirty) {
            deleteBuffers();
            deleteIndicesBuffer();
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176

            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);
                }
177
178
179
180
                if (! _pickingInformation.empty()) {
                    _pickingBuffer = new tgt::BufferObject(tgt::BufferObject::ARRAY_BUFFER, tgt::BufferObject::USAGE_STATIC_DRAW);
                    _pickingBuffer->data(&_pickingInformation.front(), _pickingInformation.size() * sizeof(tgt::col4), tgt::BufferObject::UNSIGNED_BYTE, 4);
                }
181
182
183
            }
            catch (tgt::Exception& e) {
                LERROR("Error creating OpenGL Buffer objects: " << e.what());
184
                _buffersDirty = true;
185
186
187
188
                return;
            }

            LGL_ERROR;
189
            _buffersDirty = false;
190
191
        }
    }
192
193
194
195
196
197
198

    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;
    }
199
200
201
202
203
    
    void IndexedMeshGeometry::deleteIndicesBuffer() const {
        delete _indicesBuffer;
        _indicesBuffer = 0;
    }
204

205
206
207
208
    bool IndexedMeshGeometry::hasTextureCoordinates() const {
        return ! _textureCoordinates.empty();
    }

209
210
211
212
    bool IndexedMeshGeometry::hasPickingInformation() const {
        return _pickingInformation.empty();
    }

213
214
215
216
217
218
219
220
221
    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;
    }

222
}