Debian Codegraph @

avogadro/rendering/bufferobject.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_RENDERING_BUFFEROBJECT_H
7 #define AVOGADRO_RENDERING_BUFFEROBJECT_H
8
9 #include "avogadrorenderingexport.h"
10 #include <avogadro/core/avogadrocore.h>
11
12 #include "scene.h"
13
14 #include <string> // For member variables.
15 #include <vector> // For API.
16
17 namespace Avogadro {
18 namespace Rendering {
19
20 /**
21 * @class BufferObject bufferobject.h <avogadro/rendering/bufferobject.h>
22 * @brief Buffer object to store geometry/attribute data on the GPU.
23 * @author Marcus D. Hanwell
24 *
25 * This class creates GPU buffer object, and uploads the data to the GPU.
26 */
27
28 class AVOGADRORENDERING_EXPORT BufferObject
29 {
30 public:
31 enum ObjectType
32 {
33 ArrayBuffer,
34 ElementArrayBuffer
35 };
36
37 BufferObject(ObjectType type = ArrayBuffer);
38 ~BufferObject();
39
40 /** Get the type of the buffer object. */
41 ObjectType type() const;
42
43 /** Get the handle of the buffer object. */
44 Index handle() const;
45
46 /** Determine if the buffer object is ready to be used. */
47 bool ready() const { return m_dirty == false; }
48
49 /**
50 * Upload data to the buffer object. The BufferObject::type() must match
51 * @a type or be uninitialized.
52 *
53 * The ContainerT type must have tightly packed values of
54 * ContainerT::value_type accessible by reference via ContainerT::operator[].
55 * Additionally, the standard size() and empty() methods must be implemented.
56 * The std::vector and Avogadro::Core::Array classes are examples of such
57 * supported containers.
58 */
59 template <class ContainerT>
60 bool upload(const ContainerT& array, ObjectType type);
61
62 /** Bind the buffer object ready for rendering.
63 * @note Only one ARRAY_BUFFER and one ELEMENT_ARRAY_BUFFER may be bound at
64 * any time. */
65 bool bind();
66
67 /** Release the buffer. This should be done after rendering is complete. */
68 bool release();
69
70 /** Return a string describing errors. */
71 std::string error() const { return m_error; }
72
73 private:
74 bool uploadInternal(const void* buffer, size_t size, ObjectType objectType);
75
76 struct Private;
77 Private* d;
78 bool m_dirty;
79
80 std::string m_error;
81 };
82
83 template <class ContainerT>
84 inline bool BufferObject::upload(const ContainerT& array,
85 BufferObject::ObjectType objectType)
86 {
87 if (array.empty()) {
88 m_error = "Refusing to upload empty array.";
89 return false;
90 }
91 return uploadInternal(&array[0],
92 array.size() * sizeof(typename ContainerT::value_type),
93 objectType);
94 }
95
96 } // namespace Rendering
97 } // namespace Avogadro
98
99 #endif // AVOGADRO_RENDERING_BUFFEROBJECT_H