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 size_in_bytes = nr_elements * sizeof(T);
119 return ctx.vertex_buffer_resize(*this, array_ptr, size_in_bytes);
120 }
128 bool create_or_resize(const context& ctx, size_t size_in_bytes);
137 template <typename T, typename = std::enable_if_t<std::is_class<T>::value, bool>>
138 bool create_or_resize(const context& ctx, const T& array)
139 {
140 return !is_created() ? create(ctx, array) : resize(ctx, array);
141 }
151 template <typename T> bool create_or_resize(const context& ctx, const T* array_ptr, size_t nr_elements)
152 {
153 return !is_created() ? create(ctx, array_ptr, nr_elements) : resize(ctx, array_ptr, nr_elements);
154 }
156 template <typename T>
157 bool replace(const context& ctx, size_t buffer_offset_in_bytes, const T* array_ptr, size_t nr_elements) {
158 return ctx.vertex_buffer_replace(*this, buffer_offset_in_bytes, nr_elements* sizeof(T), array_ptr);
159 }
170 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;
181 template <typename T>
182 bool copy(const context& ctx, size_t src_offset_in_bytes, T* array_ptr, size_t nr_elements) {
183 return ctx.vertex_buffer_copy_back(*this, src_offset_in_bytes, sizeof(T)*nr_elements, array_ptr);
184 }
198 template <typename T, typename = std::enable_if_t<std::is_class<T>::value, bool>>
199 bool copy(const context& ctx, T& array, size_t src_offset_in_bytes = 0)
200 {
201 const auto size_in_bytes = array_descriptor_traits<T>::get_size(array);
202 return ctx.vertex_buffer_copy_back(*this, src_offset_in_bytes, size_in_bytes,
204 }
206 void destruct(const context& ctx);
207};
208
209 }
210}
211
212#include <cgv/config/lib_end.h>
base class for all drawables, which is independent of the used rendering API.
Definition context.h:621
base interface for a vertex buffer
Definition context.h:450
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)
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 copy(const context &ctx, size_t src_offset_in_bytes, T *array_ptr, size_t nr_elements)
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:427
@ VBU_STATIC_DRAW
Modified once and used many times; Modified by the application, and used as the source for GL drawing...
Definition context.h:434
VertexBufferType
Provides vertex buffer types to allow implicit binding.
Definition context.h:414
@ VBT_VERTICES
The buffer contains vertices and will be bound to GL_ARRAY_BUFFER.
Definition context.h:416
@ VBT_UNDEF
The buffer has no type.
Definition context.h:415
the cgv namespace
Definition print.h:11