Carna  Version 3.3.2
ManagedMesh.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 - 2015 Leonid Kostrykin
3  *
4  * Chair of Medical Engineering (mediTEC)
5  * RWTH Aachen University
6  * Pauwelsstr. 20
7  * 52074 Aachen
8  * Germany
9  *
10  */
11 
12 #ifndef MANAGEDMESH_H_6014714286
13 #define MANAGEDMESH_H_6014714286
14 
15 #include <Carna/base/Mesh.h>
18 #include <memory>
19 #include <vector>
20 
25 namespace Carna
26 {
27 
28 namespace base
29 {
30 
31 
32 
33 // ----------------------------------------------------------------------------------
34 // ManagedMeshBase
35 // ----------------------------------------------------------------------------------
36 
56 class CARNA_LIB ManagedMeshBase : public GeometryFeature
57 {
58 
60 
61  struct Details;
62  const std::unique_ptr< Details > pimpl;
63 
64 protected:
65 
66  friend class GeometryFeature;
67  friend class ManagedMeshInterface;
68 
72  ManagedMeshBase( unsigned int primitiveType, const VertexAttributes& va );
73 
77  virtual ~ManagedMeshBase();
78 
82  virtual VertexBufferBase* loadVertexBuffer() = 0;
83 
87  virtual IndexBufferBase* loadIndexBuffer() = 0;
88 
92  void acquireMesh();
93 
97  void releaseMesh();
98 
102  const MeshBase& mesh() const;
103 
104 public:
105 
110 
111  virtual bool controlsSameVideoResource( const GeometryFeature& ) const override;
112 
117  const unsigned int primitiveType;
118 
123 
124  virtual ManagedMeshInterface* acquireVideoResource() override;
125 
126 }; // ManagedMeshBase
127 
128 
129 
130 // ----------------------------------------------------------------------------------
131 // ManagedMesh
132 // ----------------------------------------------------------------------------------
133 
146 template< typename VertexType, typename IndexType >
148 {
149 
150  const std::vector< VertexType > vertices;
151  const std::vector< IndexType > indices;
152 
153  ManagedMesh( unsigned int primitiveType
154  , const VertexType* vertices
155  , const std::size_t vertexCount
156  , const IndexType* indices
157  , const std::size_t indexCount );
158 
159 protected:
160 
161  virtual VertexBufferBase* loadVertexBuffer() override;
162 
163  virtual IndexBufferBase* loadIndexBuffer() override;
164 
165 public:
166 
167  typedef VertexType Vertex;
168  typedef IndexType Index;
169 
174  ( unsigned int primitiveType
175  , const VertexType* vertices
176  , const std::size_t vertexCount
177  , const IndexType* indices
178  , const std::size_t indexCount );
179 
180 }; // ManagedMesh
181 
182 
183 template< typename VertexType, typename IndexType >
185  ( unsigned int primitiveType
186  , const VertexType* vertices
187  , const std::size_t vertexCount
188  , const IndexType* indices
189  , const std::size_t indexCount )
190  : ManagedMeshBase( primitiveType, Vertex::attributes )
191  , vertices( vertices, vertices + vertexCount )
192  , indices ( indices, indices + indexCount )
193 {
194 }
195 
196 
197 template< typename VertexType, typename IndexType >
199 {
201  vb->copy( &vertices.front(), vertices.size() );
202  return vb;
203 }
204 
205 
206 template< typename VertexType, typename IndexType >
208 {
210  ib->copy( &indices.front(), indices.size() );
211  return ib;
212 }
213 
214 
215 template< typename VertexType, typename IndexType >
217  ( unsigned int primitiveType
218  , const VertexType* vertices
219  , const std::size_t vertexCount
220  , const IndexType* indices
221  , const std::size_t indexCount )
222 {
223  return *new ManagedMesh< Vertex, Index >( primitiveType, vertices, vertexCount, indices, indexCount );
224 }
225 
226 
227 
228 } // namespace Carna :: base
229 
230 } // namespace Carna
231 
232 #endif // MANAGEDMESH_H_6014714286
Specializes VertexBufferBase for particular VertexType.
Definition: VertexBuffer.h:75
void copy(const IndexType *indicesPtr, const std::size_t indicesCount)
Copies indicesCount indices referenced by indicesPtr to the maintained index buffer object...
Definition: IndexBuffer.h:240
ManagedMeshInterface ManagedInterface
Defines the type to be used for interfacing the video resource.
Definition: ManagedMesh.h:109
void copy(const Vertex *verticesPtr, const std::size_t verticesCount)
Copies verticesCount vertices referenced by verticesPtr to the maintained vertex buffer object...
Definition: VertexBuffer.h:95
IndexType Index
Holds the element type of the index buffer.
Definition: ManagedMesh.h:168
virtual VertexBufferBase * loadVertexBuffer() override
Creates OpenGL vertex buffer object and fills it with data.
Definition: ManagedMesh.h:198
Defines Carna::base::GeometryFeature.
Defines Carna::base::Mesh.
Represents "components" that are aggregated by Geometry objects. Closer description is given here...
Maintains GL_ARRAY_BUFFER object that holds vertices. This class realizes the RAII-idiom.
Definition: VertexBuffer.h:42
virtual IndexBufferBase * loadIndexBuffer() override
Creates OpenGL index buffer object and fills it with data.
Definition: ManagedMesh.h:207
Represents MeshBase object whose lifetime is managed by instances of this class. This is a format-ind...
Definition: ManagedMesh.h:56
Defines Carna::base::ManagedMeshInterface.
Interfaces the MeshBase that a ManagedMeshBase represents.
VertexType Vertex
Holds the element type of the vertex buffer.
Definition: ManagedMesh.h:167
Format-independent abstract Mesh base class. Each mesh consists of a VertexBuffer, an IndexBuffer and a vertex array. This class realizes the RAII-idiom w.r.t. the vertex array.
Definition: Mesh.h:63
Specializes IndexBufferBase for particular IndexType.
Definition: IndexBuffer.h:204
Implements MeshBase class for particular VertexType and IndexType.
Definition: ManagedMesh.h:147
const unsigned int primitiveType
Holds the primitive type, like GL_TRIANGLES, that should be used when rendering this mesh...
Definition: ManagedMesh.h:117
std::vector< VertexAttribute > VertexAttributes
Defines VertexAttribute vector.
const VertexAttributes vertexAttributes
Holds the vertex format of the vertices contained by the vertex buffer.
Definition: ManagedMesh.h:122
#define NON_COPYABLE
Features class it is placed in as non-copyable.
Definition: noncopyable.h:109
Maintains GL_ELEMENT_ARRAY_BUFFER object that holds the vertex indices, that interconnect the vertice...
Definition: IndexBuffer.h:43
static ManagedMesh< VertexType, IndexType > & create(unsigned int primitiveType, const VertexType *vertices, const std::size_t vertexCount, const IndexType *indices, const std::size_t indexCount)
Instantiates. Call release when you do not need the object any longer.
Definition: ManagedMesh.h:217