cgv
Loading...
Searching...
No Matches
attribute_array_manager.h
1#pragma once
2
3#include <cgv/render/context.h>
4#include <cgv/render/vertex_buffer.h>
5#include <cgv/render/attribute_array_binding.h>
6#include <cgv_gl/gl/gl_context.h>
7
8#include "gl/lib_begin.h"
9
10namespace cgv { // @<
11namespace render { // @<
12
15protected:
21 std::map<int, vertex_buffer*> vbos;
23 friend class renderer;
24public:
26 template <typename T>
27 bool set_indices(const context& ctx, const T& array) {
28 bool res;
29 vertex_buffer*& vbo_ptr = vbos[-1];
30 if(vbo_ptr) {
31 if(vbo_ptr->get_size_in_bytes() == array_descriptor_traits <T>::get_size(array))
32 res = vbo_ptr->replace(ctx, 0, array_descriptor_traits <T>::get_address(array), array_descriptor_traits < T>::get_nr_elements(array));
33 else {
34 vbo_ptr->destruct(ctx);
35 res = vbo_ptr->create(ctx, array);
36 }
37 } else {
38 vbo_ptr = new vertex_buffer(VBT_INDICES, default_usage);
39 res = vbo_ptr->create(ctx, array);
40 }
41 if(res)
42 res = ctx.set_element_array(&aab, vbo_ptr);
43 return res;
44 }
46 template <typename T>
47 bool set_indices(const context& ctx, const T* array, size_t count) {
48 bool res;
49 vertex_buffer*& vbo_ptr = vbos[-1];
50 if(vbo_ptr) {
51 if(vbo_ptr->get_size_in_bytes() == count * get_type_size(cgv::type::info::type_id<T>::get_id()))
52 res = vbo_ptr->replace(ctx, 0, array, count);
53 else {
54 vbo_ptr->destruct(ctx);
55 res = vbo_ptr->create(ctx, array, count);
56 }
57 } else {
58 vbo_ptr = new vertex_buffer(VBT_INDICES, default_usage);
59 res = vbo_ptr->create(ctx, array, count);
60 }
61 if(res)
62 res = ctx.set_element_array(&aab, vbo_ptr);
63 return res;
64 }
66 bool has_index_buffer() const;
68 void remove_indices(const context& ctx);
70 template <typename T>
71 bool set_attribute_array(const context& ctx, int loc, const T& array) {
72 bool res;
73 vertex_buffer*& vbo_ptr = vbos[loc];
74 if(vbo_ptr) {
75 if(vbo_ptr->get_size_in_bytes() == array_descriptor_traits <T>::get_size(array))
76 res = vbo_ptr->replace(ctx, 0, array_descriptor_traits <T>::get_address(array), array_descriptor_traits < T>::get_nr_elements(array));
77 else {
78 vbo_ptr->destruct(ctx);
79 res = vbo_ptr->create(ctx, array);
80 }
81 } else {
82 vbo_ptr = new vertex_buffer(VBT_VERTICES, default_usage);
83 res = vbo_ptr->create(ctx, array);
84 }
85 if(res)
86 res = ctx.set_attribute_array_void(&aab, loc, array_descriptor_traits <T>::get_type_descriptor(array), vbo_ptr, 0, array_descriptor_traits < T>::get_nr_elements(array));
87 return res;
88 }
90 template <typename T>
91 bool set_attribute_array(const context& ctx, int loc, const T* array_ptr, size_t nr_elements, unsigned stride) {
92 bool res;
93 vertex_buffer*& vbo_ptr = vbos[loc];
94 if(vbo_ptr) {
95 if(vbo_ptr->get_size_in_bytes() == nr_elements * sizeof(T))
96 res = vbo_ptr->replace(ctx, 0, array_ptr, nr_elements);
97 else {
98 vbo_ptr->destruct(ctx);
99 res = vbo_ptr->create(ctx, array_ptr, nr_elements);
100 }
101 } else {
102 vbo_ptr = new vertex_buffer(VBT_VERTICES, default_usage);
103 res = vbo_ptr->create(ctx, array_ptr, nr_elements);
104 }
105 if(res)
106 res = ctx.set_attribute_array_void(&aab, loc, type_descriptor(element_descriptor_traits<T>::get_type_descriptor(*array_ptr), true), vbo_ptr, 0, nr_elements);
107 return res;
108 }
110 bool set_attribute_array(const context& ctx, int loc, type_descriptor element_type, const vertex_buffer& vbo, size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes);
111
112 template <typename C, typename T>
113 bool set_composed_attribute_array(const context& ctx, int loc, const C* array_ptr, size_t nr_elements, const T& elem) {
114 bool res;
115 vertex_buffer*& vbo_ptr = vbos[loc];
116 if(vbo_ptr) {
117 if(vbo_ptr->get_size_in_bytes() == nr_elements * sizeof(C))
118 res = vbo_ptr->replace(ctx, 0, array_ptr, nr_elements);
119 else {
120 vbo_ptr->destruct(ctx);
121 res = vbo_ptr->create(ctx, array_ptr, nr_elements);
122 }
123 } else {
124 vbo_ptr = new vertex_buffer(VBT_VERTICES, default_usage);
125 res = vbo_ptr->create(ctx, array_ptr, nr_elements);
126 }
127 if(res)
128 res = ctx.set_attribute_array_void(&aab, loc,
129 type_descriptor(element_descriptor_traits<T>::get_type_descriptor(elem), true),
130 vbo_ptr,
131 reinterpret_cast<const void*>(reinterpret_cast<const cgv::type::uint8_type*>(&elem) - reinterpret_cast<const cgv::type::uint8_type*>(array_ptr)),
132 nr_elements, sizeof(C));
133 return res;
134 }
135 template <typename C, typename T>
136 bool ref_composed_attribute_array(const context& ctx, int loc, int loc_ref, const C* array_ptr, size_t nr_elements, const T& elem) {
137 vertex_buffer*& vbo_ptr = vbos[loc_ref];
138 if(!vbo_ptr)
139 return false;
140 return ctx.set_attribute_array_void(&aab, loc,
141 type_descriptor(element_descriptor_traits<T>::get_type_descriptor(elem), true),
142 vbo_ptr,
143 reinterpret_cast<const void*>(reinterpret_cast<const cgv::type::uint8_type*>(&elem) - reinterpret_cast<const cgv::type::uint8_type*>(array_ptr)),
144 nr_elements, sizeof(C));
145 }
146 void remove_attribute_array(const context& ctx, int loc);
147public:
149 attribute_array_manager(VertexBufferUsage _default_usage = VBU_STREAM_DRAW);
151 ~attribute_array_manager();
153 bool has_attribute(const context& ctx, int loc) const;
155 const vertex_buffer* get_buffer_ptr(int loc) const {
156 auto it = vbos.find(loc);
157 if(it != vbos.end())
158 return it->second;
159 return nullptr;
160 }
162 bool is_created() const;
164 bool init(context& ctx);
166 bool enable(context& ctx);
168 bool disable(context& ctx);
170 void destruct(const context& ctx);
171};
172
173}
174}
175
176#include <cgv/config/lib_end.h>
complete implementation of method actions that only call one method when entering a node
Definition action.h:113
the attribute_array_binding allows to define vertex attributes (i.e.
attribute array manager used to upload arrays to gpu
std::map< int, vertex_buffer * > vbos
store vertex buffers generated per attribute location
const vertex_buffer * get_buffer_ptr(int loc) const
returns the pointer to the vertex buffer as managed by this attribute array manager if specified and ...
attribute_array_binding aab
attribue array binding used to store array pointers
VertexBufferUsage default_usage
store default buffer usage
base class for all drawables, which is independent of the used rendering API.
Definition context.h:621
abstract base class for all renderers that handles a shader program and position / color attribute
Definition renderer.h:22
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.
bool create(const context &ctx, size_t size_in_bytes)
create empty vertex buffer of size size given in bytes
void destruct(const context &ctx)
destruct the render buffer
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
unsigned int get_type_size(TypeId tid)
function that returns the size of a type specified through TypeId
Definition type_id.cxx:18
the cgv namespace
Definition print.h:11
template with a static member function get_id() of type TypeId returning the TypeId of the template a...
Definition type_id.h:86