cgv
Loading...
Searching...
No Matches
vertex_buffer.h
1#pragma once
2
3#include <cgv/render/context.h>
4#include <cgv/render/element_traits.h>
5
6#include "lib_begin.h"
7
8namespace cgv {
9namespace render {
10
12class CGV_API vertex_buffer : public vertex_buffer_base
13{
14protected:
15 size_t size_in_bytes = 0;
16
17public:
40 void bind(const context& ctx, VertexBufferType type = VBT_UNDEF) const;
51 void unbind(const context& ctx, VertexBufferType type = VBT_UNDEF) const;
55 void unbind(const context& ctx, unsigned index) const;
63 void unbind(const context& ctx, VertexBufferType type, unsigned index) const;
73 void bind(const context& ctx, unsigned index) const;
83 void bind(const context& ctx, VertexBufferType type, unsigned index) const;
85 bool create(const context& ctx, size_t size_in_bytes);
87 template <typename T>
88 bool create(const context& ctx, const T& array)
89 {
90 size_in_bytes = array_descriptor_traits<T>::get_size(array);
91 return ctx.vertex_buffer_create(*this, array_descriptor_traits<T>::get_address(array), size_in_bytes);
92 }
93
94 template <typename T>
95 bool create(const context& ctx, const T* array_ptr, size_t nr_elements) {
96 size_in_bytes = nr_elements * sizeof(T);
97 return ctx.vertex_buffer_create(*this, array_ptr, size_in_bytes);
98 }
99
104 bool is_created() const override;
110 size_t get_size_in_bytes() const;
112 bool resize(const context& ctx, size_t size_in_bytes);
114 template <typename T, typename = std::enable_if_t<std::is_class<T>::value, bool>>
115 bool resize(const context& ctx, const T& array)
116 {
117 size_in_bytes = array_descriptor_traits<T>::get_size(array);
118 return ctx.vertex_buffer_resize(*this, array_descriptor_traits<T>::get_address(array), size_in_bytes);
119 }
120
121 template <typename T>
122 bool resize(const context& ctx, const T* array_ptr, size_t nr_elements)
123 {
124 size_in_bytes = nr_elements * sizeof(T);
125 return ctx.vertex_buffer_resize(*this, array_ptr, size_in_bytes);
126 }
127
134 bool create_or_resize(const context& ctx, size_t size_in_bytes);
143 template <typename T, typename = std::enable_if_t<std::is_class<T>::value, bool>>
144 bool create_or_resize(const context& ctx, const T& array)
145 {
146 return !is_created() ? create(ctx, array) : resize(ctx, array);
147 }
148
157 template <typename T> bool create_or_resize(const context& ctx, const T* array_ptr, size_t nr_elements)
158 {
159 return !is_created() ? create(ctx, array_ptr, nr_elements) : resize(ctx, array_ptr, nr_elements);
160 }
161
167 bool clear(const context& ctx) {
168 return ctx.vertex_buffer_clear(*this, 0, size_in_bytes);
169 }
170
171 template <typename T>
172 bool replace(const context& ctx, size_t buffer_offset_in_bytes, const T* array_ptr, size_t nr_elements)
173 {
174 return ctx.vertex_buffer_replace(*this, buffer_offset_in_bytes, nr_elements* sizeof(T), array_ptr);
175 }
176
186 bool copy(const context& ctx, size_t src_offset_in_bytes, size_t size_in_bytes, vertex_buffer& dst, size_t dst_offset_in_bytes) const;
197 template <typename T>
198 bool copy(const context& ctx, size_t src_offset_in_bytes, T* array_ptr, size_t nr_elements) const
199 {
200 return ctx.vertex_buffer_copy_back(*this, src_offset_in_bytes, sizeof(T)*nr_elements, array_ptr);
201 }
202
215 template <typename T, typename = std::enable_if_t<std::is_class<T>::value, bool>>
216 bool copy(const context& ctx, T& array, size_t src_offset_in_bytes = 0) const
217 {
218 const auto size_in_bytes = array_descriptor_traits<T>::get_size(array);
219 return ctx.vertex_buffer_copy_back(*this, src_offset_in_bytes, size_in_bytes,
221 }
222
223 void destruct(const context& ctx);
224};
225
226} // namespace render
227} // namespace cgv
228
229#include <cgv/config/lib_end.h>
base class for all drawables, which is independent of the used rendering API.
Definition context.h:670
base interface for a vertex buffer
Definition context.h:510
VertexBufferType type
buffer type defaults to VBT_VERTICES
Definition context.h:513
VertexBufferUsage usage
usage defaults to VBU_STATIC_DRAW
Definition context.h:515
a vertex buffer is an unstructured memory block on the GPU.
bool resize(const context &ctx, const T *array_ptr, size_t nr_elements)
resize vertex buffer and copy data from CPU array array_ptr into buffer memory
bool create(const context &ctx, const T *array_ptr, size_t nr_elements)
create vertex buffer and copy data from CPU array array_ptr into buffer memory
vertex_buffer(VertexBufferType type=VBT_VERTICES, VertexBufferUsage usage=VBU_STATIC_DRAW)
Construct a vertex buffer of the given type and usage.
bool copy(const context &ctx, T &array, size_t src_offset_in_bytes=0) const
Copy elements from the buffer into the CPU array.
bool create_or_resize(const context &ctx, const T *array_ptr, size_t nr_elements)
Convenience wrapper to either create() or resize() the buffer.
bool clear(const context &ctx)
Clear the entire buffer to zeros.
bool resize(const context &ctx, size_t size_in_bytes)
resize vertex buffer to size size given in bytes clearing all data
bool copy(const context &ctx, size_t src_offset_in_bytes, T *array_ptr, size_t nr_elements) const
Copy elements from the buffer into the CPU array.
bool create(const context &ctx, size_t size_in_bytes)
create empty vertex buffer of size size given in bytes
void unbind(const context &ctx, VertexBufferType type=VBT_UNDEF) const
Unbind buffer from the appropriate target.
bool create_or_resize(const context &ctx, const T &array)
Convenience wrapper to either create() or resize() the buffer.
bool is_created() const override
Check whether the vertex buffer has been created.
bool create(const context &ctx, const T &array)
create vertex buffer and copy data from CPU array array into buffer memory
bool resize(const context &ctx, const T &array)
resize vertex buffer and copy data from CPU array array into buffer memory
void bind(const context &ctx, VertexBufferType type=VBT_UNDEF) const
Bind buffer to appropriate target.
bool replace(const context &ctx, size_t buffer_offset_in_bytes, const T *array_ptr, size_t nr_elements)
replace part (starting at byte offset buffer_offset_in_bytes) or whole vertex buffer content from nr_...
namespace for api independent GPU programming
VertexBufferUsage
Provides vertex buffer usage hints as defined in OpenGL.
Definition context.h:487
@ VBU_STATIC_DRAW
Modified once and used many times; Modified by the application, and used as the source for GL drawing...
Definition context.h:494
VertexBufferType
Provides vertex buffer types to allow implicit binding.
Definition context.h:474
@ VBT_VERTICES
The buffer contains vertices and will be bound to GL_ARRAY_BUFFER.
Definition context.h:476
@ VBT_UNDEF
The buffer has no type.
Definition context.h:475
this header is dependency free
Definition print.h:11