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 {
9 namespace render {
10
12class CGV_API vertex_buffer : public vertex_buffer_base
13{
14protected:
15 size_t size_in_bytes;
16
17public:
35 void bind(context& ctx, VertexBufferType type = VBT_UNDEF) const;
46 void unbind(context& ctx, VertexBufferType type = VBT_UNDEF) const;
50 void unbind(context& ctx, unsigned index) const;
58 void unbind(context& ctx, VertexBufferType type, unsigned index) const;
68 void bind(context& ctx, unsigned index) const;
78 void bind(context& ctx, VertexBufferType type, unsigned index) const;
80 bool create(const context& ctx, size_t size_in_bytes);
82 template <typename T>
83 bool create(const context& ctx, const T& array)
84 {
85 size_in_bytes = array_descriptor_traits<T>::get_size(array);
86 return ctx.vertex_buffer_create(*this, array_descriptor_traits<T>::get_address(array), size_in_bytes);
87 }
89 template <typename T>
90 bool create(const context& ctx, const T* array_ptr, size_t nr_elements) {
91 size_in_bytes = nr_elements * sizeof(T);
92 return ctx.vertex_buffer_create(*this, array_ptr, size_in_bytes);
93 }
99 bool is_created() const override;
105 size_t get_size_in_bytes() const;
107 bool resize(const context& ctx, size_t size_in_bytes);
109 template <typename T, typename = std::enable_if_t<std::is_class<T>::value, bool>>
110 bool resize(const context& ctx, const T& array)
111 {
112 size_in_bytes = array_descriptor_traits<T>::get_size(array);
113 return ctx.vertex_buffer_resize(*this, array_descriptor_traits<T>::get_address(array), size_in_bytes);
114 }
116 template <typename T>
117 bool resize(const context& ctx, const T* array_ptr, size_t nr_elements)
118 {
119 size_in_bytes = nr_elements * sizeof(T);
120 return ctx.vertex_buffer_resize(*this, array_ptr, size_in_bytes);
121 }
129 bool create_or_resize(const context& ctx, size_t size_in_bytes);
138 template <typename T, typename = std::enable_if_t<std::is_class<T>::value, bool>>
139 bool create_or_resize(const context& ctx, const T& array)
140 {
141 return !is_created() ? create(ctx, array) : resize(ctx, array);
142 }
152 template <typename T> bool create_or_resize(const context& ctx, const T* array_ptr, size_t nr_elements)
153 {
154 return !is_created() ? create(ctx, array_ptr, nr_elements) : resize(ctx, array_ptr, nr_elements);
155 }
162 bool clear(const context& ctx) {
163 return ctx.vertex_buffer_clear(*this, 0, size_in_bytes);
164 }
166 template <typename T>
167 bool replace(const context& ctx, size_t buffer_offset_in_bytes, const T* array_ptr, size_t nr_elements)
168 {
169 return ctx.vertex_buffer_replace(*this, buffer_offset_in_bytes, nr_elements* sizeof(T), array_ptr);
170 }
181 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;
192 template <typename T>
193 bool copy(const context& ctx, size_t src_offset_in_bytes, T* array_ptr, size_t nr_elements) const
194 {
195 return ctx.vertex_buffer_copy_back(*this, src_offset_in_bytes, sizeof(T)*nr_elements, array_ptr);
196 }
210 template <typename T, typename = std::enable_if_t<std::is_class<T>::value, bool>>
211 bool copy(const context& ctx, T& array, size_t src_offset_in_bytes = 0) const
212 {
213 const auto size_in_bytes = array_descriptor_traits<T>::get_size(array);
214 return ctx.vertex_buffer_copy_back(*this, src_offset_in_bytes, size_in_bytes,
216 }
218 void destruct(const context& ctx);
219};
220
221 }
222}
223
224#include <cgv/config/lib_end.h>
base class for all drawables, which is independent of the used rendering API.
Definition context.h:627
base interface for a vertex buffer
Definition context.h:461
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
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 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_or_resize(const context &ctx, const T &array)
Convenience wrapper to either create() or resize() the buffer.
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
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_...
VertexBufferUsage
Provides vertex buffer usage hints as defined in OpenGL.
Definition context.h:438
@ VBU_STATIC_DRAW
Modified once and used many times; Modified by the application, and used as the source for GL drawing...
Definition context.h:445
VertexBufferType
Provides vertex buffer types to allow implicit binding.
Definition context.h:425
@ VBT_VERTICES
The buffer contains vertices and will be bound to GL_ARRAY_BUFFER.
Definition context.h:427
@ VBT_UNDEF
The buffer has no type.
Definition context.h:426
the cgv namespace
Definition print.h:11