geometrydata.h 9.04 KB
Newer Older
1
2
// ================================================================================================
// 
schultezub's avatar
schultezub committed
3
// This file is part of the CAMPVis Software Framework.
4
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
// 
schultezub's avatar
schultezub committed
11
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
12
// 
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
// 
// ================================================================================================

#ifndef GEOMETRYDATA_H__
#define GEOMETRYDATA_H__

28
#include "cgt/cgt_gl.h"
29
#include "cgt/bounds.h"
30
#include "core/datastructures/abstractdata.h"
31
#include <vector>
32

33
namespace cgt {
34
    class BufferObject;
35
    class GLCanvas;
36
37
}

schultezub's avatar
schultezub committed
38
namespace campvis {
39

40
    /**
41
     * Abstract base class for geometry data in CAMPVis.
42
     * 
43
44
45
46
47
48
49
50
51
52
53
     * GeometryData consists of a set of vertices (vec3) and optionally colors (vec4), texture 
     * coordinates (vec3) and/or normals (vec3) for each vertex.
     * GeometryData stores the geometry on the CPU side (local RAM) and takes care of transparently 
     * mapping it into GPU memory in order to render it using OpenGL. The mapping of vertex 
     * information to OpenGL vertex attributes is as follows:
     *  - Vertex positions: Vertex attribute 0
     *  - Vertex texture coordinates: Vertex attribute 1
     *  - Vertex colors: Vertex attribute 2
     *  - Vertex normals: Vertex attribute 3
     * 
     */    
54
    class CAMPVIS_CORE_API GeometryData : public AbstractData, public IHasWorldBounds {
55
    public:
56
57
58
59
60
        /**
         * Constructor
         * \param   vertexFeatures  List of features present for each vertex
         */
        explicit GeometryData();
61

62
63
64
65
        /**
         * Copy constructor
         * \param   rhs GeometryData to copy
         */
66
67
68
        GeometryData(const GeometryData& rhs);

        /**
69
         * Destructor, deletes VBOs/VAO if necessary.
70
         */
71
72
        virtual ~GeometryData();

73
74
75
76
77
        /**
         * Assignment operator.
         * \param   rhs GeometryData to assign to this.
         * \return  *this after assignment
         */
78
79
        GeometryData& operator=(const GeometryData& rhs);

80
        /// \see AbstractData::clone()
81
        virtual GeometryData* clone() const = 0;
82

83
84
85
86
        /**
         * Renders this GeometryData.
         * Must be called from a valid OpenGL context.
         */
87
        virtual void render(GLenum mode = GL_TRIANGLE_FAN) const = 0;
88
89
90
91
92
        
        /**
         * Returns the geometry extent in world coordinates.
         * \return  The geometry extent in world coordinates.
         */
93
        virtual cgt::Bounds getWorldBounds() const = 0;
94

95

96
97
98
99
100
101
        /**
         * Returns whether the geometry has texture coordinates.
         * \return  True if this geometry sets texture coordinates during rendering.
         */
        virtual bool hasTextureCoordinates() const = 0;

102
103
104
105
106
107
        /**
         * Returns whether this geometry has picking information.
         * \return  True if this geometry sets picking information during rendering.
         */
        virtual bool hasPickingInformation() const = 0;

108
109
110
111
        /**
         * Applies the transformation matrix \a t to each vertex of this geometry.
         * \param   t   Transformation matrix to apply
         */
112
        virtual void applyTransformationToVertices(const cgt::mat4& t) = 0;
113

114
        /**
115
116
117
         * Returns the Pointer to the OpenGL Buffer with the vertex positions.
         * May be 0 if not yet created.
         * \return  _verticesBuffer
118
         */
119
        const cgt::BufferObject* getVerticesBuffer() const;
120

121
122
123
124
125
        /**
         * Returns the Pointer to the OpenGL Buffer with the vertex texture coordinates.
         * May be 0 if none are present or not yet created.
         * \return  _texCoordsBuffer
         */
126
        const cgt::BufferObject* getTextureCoordinatesBuffer() const;
127

128
129
130
131
132
        /**
         * Returns the Pointer to the OpenGL Buffer with the vertex colors. 
         * May be 0 if none are present or not yet created.
         * \return  _colorsBuffer
         */
133
        const cgt::BufferObject* getColorsBuffer() const;
134

135
136
137
138
139
        /**
         * Returns the Pointer to the OpenGL Buffer with the vertex normals.
         * May be 0 if none are present or not yet created.
         * \return  _normalsBuffer
         */
140
        const cgt::BufferObject* getNormalsBuffer() const;
141

142
143
144
145
146
        /**
         * Returns the Pointer to the OpenGL Buffer with the vertex normals.
         * May be 0 if none are present or not yet created.
         * \return  _normalsBuffer
         */
147
        const cgt::BufferObject* getPickingBuffer() const;
148

149
150
151
        /// \see AbstractData::getVideoMemoryFootprint()
        virtual size_t getVideoMemoryFootprint() const;

152
    protected:
153
154
155
156
157
        /**
         * Deletes all OpenGL BufferObjects.
         */
        void deleteBuffers() const;

158

159
        // mutable to support const lazy initialization
160
        mutable bool _buffersDirty;             ///< Flag whether the buffers are dirty (i.e. need to be (re)initialized)
161

162
        enum { NUM_BUFFERS = 5 };               ///< Number of buffers in _buffers array
163
164
165

        union {
            struct {
166
167
168
169
170
                mutable cgt::BufferObject* _verticesBuffer;     ///< Pointer to the OpenGL Buffer with the vertex positions
                mutable cgt::BufferObject* _texCoordsBuffer;    ///< Pointer to the OpenGL Buffer with the vertex texture coordinates
                mutable cgt::BufferObject* _colorsBuffer;       ///< Pointer to the OpenGL Buffer with the vertex colors
                mutable cgt::BufferObject* _normalsBuffer;      ///< Pointer to the OpenGL Buffer with the vertex normals
                mutable cgt::BufferObject* _pickingBuffer;      ///< Pointer to the OpenGL Buffer with the picking information
171
172
            };

173
            mutable cgt::BufferObject* _buffers[NUM_BUFFERS];   ///< Array of all buffers
174
175
176
        };

    private:
177
178

        static const std::string loggerCat_;
179
180
    };

181
}
182

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
=== This is a draft for a new, generic GeometryData class design that manages arbitrary OpenGL buffers ====

class CAMPVIS_CORE_API GeometryDataBase {
public:
    /// Enumeration for defining semantics of stored buffer data
    enum ElementSemantic {
        VERTEX                  = 0,    ///< Vextex data
        TEXTURE_COORDINATE      = 1,    ///< Texture coordinate data
        COLOR                   = 2,    ///< Color data
        NORMAL                  = 3,    ///< Normal data
        PICKING_INFORMATION     = 4     ///< Picking information
    };

    /// Enumeration for defining the host data type of the element
    enum ElementHostType {
        UINT8,
        UINT16,
        UINT32,
        FLOAT,
        VEC2,
        VEC3,
        VEC4
    };
};

template<GeometryDataBase::ElementSemantic SEMANTIC>
struct GeometryDataTraits {};

template<>
struct GeometryDataTraits<GeometryDataBase::VERTEX> {
214
    typedef cgt::vec3 HostType;
215
216
217
218
219
220
};

class CAMPVIS_CORE_API DraftNewGeometryData {
public:
    template<GeometryDataBase::ElementSemantic SEMANTIC>
    const std::vector<typename GeometryDataTraits<SEMANTIC>::HostType>* getElementData() const;
221
222

    template<GeometryDataBase::ElementSemantic SEMANTIC>
223
    void setElementData(std::vector<typename GeometryDataTraits<SEMANTIC>::HostType>* elementData);
224

225
226
227
protected:
    std::vector<void*> _elementPointers;
};
228

229
230
231
232
233

template<GeometryDataBase::ElementSemantic SEMANTIC>
const std::vector<typename GeometryDataTraits<SEMANTIC>::HostType>* GeometryData::getElementData() const {
    if (_elementPointers.size() >= SEMANTIC) {
        return static_cast< std::vector<typename GeometryDataTraits<SEMANTIC>::HostType>* >(_elementPointers[SEMANTIC]);
234
235
    }

236
    return nullptr;
237
238
}

239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
template<GeometryDataBase::ElementSemantic SEMANTIC>
void GeometryData::setElementData(std::vector<typename GeometryDataTraits<SEMANTIC>::HostType>* elementData) {
    if (_elementPointers.size() < SEMANTIC + 1)
        _elementPointers.resize(SEMANTIC, nullptr);

    void* oldPtr = _elementPointers[SEMANTIC];
    if (oldPtr != elementData)
        delete static_cast< std::vector<typename GeometryDataTraits<SEMANTIC>::HostType>* >(oldPtr);

    _elementPointers[SEMANTIC] = elementData;
}

*/


254
#endif // GEOMETRYDATA_H__