cgv
Loading...
Searching...
No Matches
vertex_buffer.cxx
1#include "vertex_buffer.h"
2
3namespace cgv {
4namespace render {
5
7{
8 this->type = type;
9 this->usage = usage;
10}
11
13{
14 if (ctx_ptr) {
15 if (ctx_ptr->make_current()) {
17 ctx_ptr = nullptr;
18 }
19 }
20}
21
23{
24 return size_in_bytes;
25}
26
27void vertex_buffer::bind(const context& ctx, VertexBufferType type) const
28{
29 ctx.vertex_buffer_bind(*this, type == VBT_UNDEF ? this->type : type);
30}
31
33{
34 ctx.vertex_buffer_unbind(*this, type == VBT_UNDEF ? this->type : type);
35}
36
37void vertex_buffer::unbind(const context& ctx, unsigned index) const { ctx.vertex_buffer_unbind(*this, this->type, index); }
38
39void vertex_buffer::unbind(const context& ctx, VertexBufferType type, unsigned index) const
40{
41 ctx.vertex_buffer_unbind(*this, type, index);
42}
43
44void vertex_buffer::bind(const context& ctx, unsigned index) const
45{
46 ctx.vertex_buffer_bind(*this, this->type, index);
47}
48
49void vertex_buffer::bind(const context& ctx, VertexBufferType type, unsigned index) const
50{
51 ctx.vertex_buffer_bind(*this, type, index);
52}
53
54bool vertex_buffer::create(const context& ctx, size_t size_in_bytes)
55{
56 this->size_in_bytes = size_in_bytes;
57 return ctx.vertex_buffer_create(*this, 0, size_in_bytes);
58}
59
61{
62 return handle != nullptr;
63}
64
65bool vertex_buffer::resize(const context& ctx, size_t size_in_bytes)
66{
67 if(this->size_in_bytes != size_in_bytes) {
68 this->size_in_bytes = size_in_bytes;
69 return ctx.vertex_buffer_resize(*this, 0, size_in_bytes);
70 }
71 return true;
72}
73
74bool vertex_buffer::create_or_resize(const context& ctx, size_t size_in_bytes)
75{
76 return !is_created() ? create(ctx, size_in_bytes) : resize(ctx, size_in_bytes);
77}
78
79bool vertex_buffer::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
80{
81 return ctx.vertex_buffer_copy(*this, src_offset_in_bytes, dst, dst_offset_in_bytes, size_in_bytes);
82}
83
85{
86 if (handle) {
87 ctx.vertex_buffer_destruct(*this);
88 size_in_bytes = 0;
89 handle = nullptr;
90 }
91}
92
93} // namespace render
94} // namespace cgv
base class for all drawables, which is independent of the used rendering API.
Definition context.h:668
virtual bool make_current() const =0
make the current context current if possible
const context * ctx_ptr
keep pointer to my context
Definition context.h:363
VertexBufferType type
buffer type defaults to VBT_VERTICES
Definition context.h:511
VertexBufferUsage usage
usage defaults to VBU_STATIC_DRAW
Definition context.h:513
a vertex buffer is an unstructured memory block on the GPU.
size_t get_size_in_bytes() const
Retrieves the current size of the buffer in bytes.
vertex_buffer(VertexBufferType type=VBT_VERTICES, VertexBufferUsage usage=VBU_STATIC_DRAW)
Construct a vertex buffer of the given type and usage.
bool create_or_resize(const context &ctx, size_t size_in_bytes)
Convenience wrapper to either create() or resize() the buffer.
bool resize(const context &ctx, size_t size_in_bytes)
resize vertex buffer to size size given in bytes clearing all data
~vertex_buffer()
calls the destruct method if necessary
bool create(const context &ctx, size_t size_in_bytes)
create empty vertex buffer of size size given in bytes
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
Copy bytes between different vertex_buffer instances.
void unbind(const context &ctx, VertexBufferType type=VBT_UNDEF) const
Unbind buffer from the appropriate target.
bool is_created() const override
Check whether the vertex buffer has been created.
void destruct(const context &ctx)
destruct the render buffer
void bind(const context &ctx, VertexBufferType type=VBT_UNDEF) const
Bind buffer to appropriate target.
VertexBufferUsage
Provides vertex buffer usage hints as defined in OpenGL.
Definition context.h:485
VertexBufferType
Provides vertex buffer types to allow implicit binding.
Definition context.h:472
@ VBT_UNDEF
The buffer has no type.
Definition context.h:473
this header is dependency free
Definition print.h:11