cgv
Loading...
Searching...
No Matches
vertex_buffer.cxx
1#include "vertex_buffer.h"
2
3namespace cgv {
4 namespace render {
5
8{
9 type = _type;
10 usage = _usage;
11 size_in_bytes = 0;
12}
13
16{
17 if (ctx_ptr) {
18 if (ctx_ptr->make_current()) {
20 ctx_ptr = 0;
21 }
22 }
23}
24
26{
27 return size_in_bytes;
28}
29
31{
32 ctx.vertex_buffer_bind(*this, _type == VBT_UNDEF ? this->type : _type);
33}
34
36{
37 ctx.vertex_buffer_unbind(*this, _type == VBT_UNDEF ? this->type : _type);
38}
39
40void vertex_buffer::unbind(context& ctx, unsigned index) const { ctx.vertex_buffer_unbind(*this, this->type, index); }
41
42void vertex_buffer::unbind(context& ctx, VertexBufferType type, unsigned index) const
43{
44 ctx.vertex_buffer_unbind(*this, type, index);
45}
46
47void vertex_buffer::bind(context& ctx, unsigned index) const
48{
49 ctx.vertex_buffer_bind(*this, this->type, index);
50}
51
52void vertex_buffer::bind(context& ctx, VertexBufferType _type, unsigned index) const
53{
54 ctx.vertex_buffer_bind(*this, _type, index);
55}
56
58bool vertex_buffer::create(const context& ctx, size_t _size_in_bytes)
59{
60 size_in_bytes = _size_in_bytes;
61 return ctx.vertex_buffer_create(*this, 0, size_in_bytes);
62}
63
65{
66 return handle != 0;
67}
68
69bool vertex_buffer::resize(const context& ctx, size_t size_in_bytes)
70{
71 this->size_in_bytes = size_in_bytes;
72 return ctx.vertex_buffer_resize(*this, 0, size_in_bytes);
73}
74
75bool vertex_buffer::create_or_resize(const context& ctx, size_t size_in_bytes)
76{
77 return !is_created() ? create(ctx, size_in_bytes) : resize(ctx, size_in_bytes);
78}
79
80bool 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
81{
82 return ctx.vertex_buffer_copy(*this, src_offset_in_bytes, dst, dst_offset_in_bytes, size_in_bytes);
83}
84
87{
88 if (handle) {
89 ctx.vertex_buffer_destruct(*this);
90 size_in_bytes = 0;
91 handle = 0;
92 }
93}
94
95
96 }
97}
base class for all drawables, which is independent of the used rendering API.
Definition context.h:621
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:307
VertexBufferType type
buffer type defaults to VBT_VERTICES
Definition context.h:453
VertexBufferUsage usage
usage defaults to VBU_STATIC_DRAW
Definition context.h:455
a vertex buffer is an unstructured memory block on the GPU.
void bind(context &ctx, VertexBufferType type=VBT_UNDEF) const
Bind buffer to appropriate target.
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 from description of component format, where the default format specifies a color buffer wit...
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
void unbind(context &ctx, VertexBufferType type=VBT_UNDEF) const
Unbind buffer from the appropriate target.
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.
bool is_created() const override
Check whether the vertex buffer has been created.
void destruct(const context &ctx)
destruct the render buffer
VertexBufferUsage
Provides vertex buffer usage hints as defined in OpenGL.
Definition context.h:427
VertexBufferType
Provides vertex buffer types to allow implicit binding.
Definition context.h:414
@ VBT_UNDEF
The buffer has no type.
Definition context.h:415
the cgv namespace
Definition print.h:11