3 #include <cgv/render/context.h>
4 #include <cgv/render/shader_program.h>
5 #include <cgv/render/vertex_buffer.h>
6 #include <cgv/render/attribute_array_binding.h>
7 #include <cgv_gl/gl/gl_context.h>
9 #include "gl/lib_begin.h"
28 std::map<int, vertex_buffer*>
vbos;
33 bool set_indices(
const context& ctx,
const T& array)
39 res = vbo_ptr->
replace(ctx, 0, array_descriptor_traits <T>::get_address(array), array_descriptor_traits < T>::get_nr_elements(array));
42 res = vbo_ptr->
create(ctx, array);
46 vbo_ptr =
new vertex_buffer(VBT_INDICES, default_usage);
47 res = vbo_ptr->
create(ctx, array);
50 res = ctx.set_element_array(&aab, vbo_ptr);
55 bool set_indices(
const context& ctx,
const T* array,
size_t count)
58 vertex_buffer*& vbo_ptr = vbos[-1];
61 res = vbo_ptr->
replace(ctx, 0, array, count);
63 vbo_ptr->destruct(ctx);
64 res = vbo_ptr->create(ctx, array, count);
68 vbo_ptr =
new vertex_buffer(VBT_INDICES, default_usage);
69 res = vbo_ptr->create(ctx, array, count);
72 res = ctx.set_element_array(&aab, vbo_ptr);
76 bool has_index_buffer()
const;
78 void remove_indices(
const context& ctx);
81 bool set_attribute_array(
const context& ctx,
int loc,
const T& array) {
83 vertex_buffer*& vbo_ptr = vbos[loc];
85 if (vbo_ptr->get_size_in_bytes() == array_descriptor_traits <T>::get_size(array))
86 res = vbo_ptr->replace(ctx, 0, array_descriptor_traits <T>::get_address(array), array_descriptor_traits < T>::get_nr_elements(array));
88 vbo_ptr->destruct(ctx);
89 res = vbo_ptr->create(ctx, array);
93 vbo_ptr =
new vertex_buffer(VBT_VERTICES, default_usage);
94 res = vbo_ptr->create(ctx, array);
97 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));
101 template <
typename T>
102 bool set_attribute_array(
const context& ctx,
int loc,
const T* array_ptr,
size_t nr_elements,
unsigned stride) {
104 vertex_buffer*& vbo_ptr = vbos[loc];
106 if (vbo_ptr->get_size_in_bytes() == nr_elements *
sizeof(T))
107 res = vbo_ptr->replace(ctx, 0, array_ptr, nr_elements);
109 vbo_ptr->destruct(ctx);
110 res = vbo_ptr->create(ctx, array_ptr, nr_elements);
114 vbo_ptr =
new vertex_buffer(VBT_VERTICES, default_usage);
115 res = vbo_ptr->create(ctx, array_ptr, nr_elements);
118 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);
122 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);
124 template <
typename C,
typename T>
125 bool set_composed_attribute_array(
const context& ctx,
int loc,
const C* array_ptr,
size_t nr_elements,
const T& elem) {
127 vertex_buffer*& vbo_ptr = vbos[loc];
129 if (vbo_ptr->get_size_in_bytes() == nr_elements *
sizeof(C))
130 res = vbo_ptr->replace(ctx, 0, array_ptr, nr_elements);
132 vbo_ptr->destruct(ctx);
133 res = vbo_ptr->create(ctx, array_ptr, nr_elements);
137 vbo_ptr =
new vertex_buffer(VBT_VERTICES, default_usage);
138 res = vbo_ptr->create(ctx, array_ptr, nr_elements);
141 res = ctx.set_attribute_array_void(&aab, loc,
142 type_descriptor(element_descriptor_traits<T>::get_type_descriptor(elem),
true),
145 nr_elements,
sizeof(C));
148 template <
typename C,
typename T>
149 bool ref_composed_attribute_array(
const context& ctx,
int loc,
int loc_ref,
const C* array_ptr,
size_t nr_elements,
const T& elem) {
150 vertex_buffer*& vbo_ptr = vbos[loc_ref];
153 return ctx.set_attribute_array_void(&aab, loc,
154 type_descriptor(element_descriptor_traits<T>::get_type_descriptor(elem),
true),
157 nr_elements,
sizeof(C));
163 ~attribute_array_manager();
165 bool has_attribute(
const context& ctx,
int loc)
const;
167 bool init(context& ctx);
169 bool enable(context& ctx);
171 bool disable(context& ctx);
173 void destruct(
const context& ctx);
184 std::set<int> enabled_attribute_arrays;
203 bool has_aam()
const {
return aam_ptr != 0 && aam_ptr != &default_aam; }
206 return aam_ptr->
has_attribute(ctx, ref_prog().get_attribute_location(ctx, attr_name));
219 template <
typename T>
220 const T&
get_style()
const {
return *
static_cast<const T*
>(get_style_ptr()); }
228 template <
typename T>
229 bool set_attribute_array(
const context& ctx,
int loc,
const T& array) {
231 return aam_ptr->set_attribute_array(ctx, loc, array);
232 enabled_attribute_arrays.insert(loc);
235 template <
typename T>
236 bool set_attribute_array(
const context& ctx,
int loc,
const T* array_ptr,
size_t nr_elements,
unsigned stride) {
238 return aam_ptr->set_attribute_array(ctx, loc, array_ptr, nr_elements, stride);
239 enabled_attribute_arrays.insert(loc);
242 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);
244 template <
typename C,
typename T>
247 return aam_ptr->set_composed_attribute_array(ctx, loc, array_ptr, nr_elements, elem);
248 enabled_attribute_arrays.insert(loc);
252 template <
typename C,
typename T>
255 return aam_ptr->ref_composed_attribute_array(ctx, loc, loc_ref, array_ptr, nr_elements, elem);
256 enabled_attribute_arrays.insert(loc);
260 void draw_impl(
context& ctx,
PrimitiveType pt,
size_t start,
size_t count,
bool use_strips,
bool use_adjacency, uint32_t strip_restart_index);
262 void draw_impl_instanced(
context& ctx,
PrimitiveType type,
size_t start,
size_t count,
size_t instance_count,
bool use_strips,
bool use_adjacency, uint32_t strip_restart_index);
269 void manage_singleton(
context& ctx,
const std::string& renderer_name,
int& ref_count,
int ref_count_change);
275 DEPRECATED(
"deprecated, use enable_attribute_array_manager() paired with disable_attribute_manager instead().")
280 virtual
bool init(
context& ctx);
282 template <typename T>
283 void set_position(const
context& ctx, const T& position) { has_positions =
true; ref_prog().set_attribute(ctx, ref_prog().get_attribute_location(ctx,
"position"), position); }
285 template <
typename T>
286 void set_position_array(
const context& ctx,
const std::vector<T>& positions) { has_positions =
true; set_attribute_array(ctx, ref_prog().get_attribute_location(ctx,
"position"), positions); }
288 template <
typename T>
289 void set_position_array(
const context& ctx,
const T* positions,
size_t nr_elements,
unsigned stride_in_bytes = 0) { has_positions =
true; set_attribute_array(ctx, ref_prog().get_attribute_location(ctx,
"position"), positions, nr_elements, stride_in_bytes); }
293 template <
typename T>
294 void set_position_array(
const context& ctx,
const vertex_buffer& vbo,
size_t offset_in_bytes,
size_t nr_elements,
unsigned stride_in_bytes = 0) {
set_position_array(ctx,
type_descriptor(element_descriptor_traits<T>::get_type_descriptor(T()),
true), vbo, offset_in_bytes, nr_elements, stride_in_bytes); }
296 template <
typename T>
297 void set_color(
const context& ctx,
const T& color) { has_colors =
true; ref_prog().set_attribute(ctx, ref_prog().get_attribute_location(ctx,
"color"), color); }
299 template <
typename T>
300 void set_color_array(
const context& ctx,
const std::vector<T>& colors) { has_colors =
true; set_attribute_array(ctx, ref_prog().get_attribute_location(ctx,
"color"), colors); }
302 template <
typename T>
303 void set_color_array(
const context& ctx,
const T* colors,
size_t nr_elements,
unsigned stride_in_bytes = 0) { has_colors =
true; set_attribute_array(ctx, ref_prog().get_attribute_location(ctx,
"color"), colors, nr_elements, stride_in_bytes); }
305 void set_color_array(
const context& ctx,
type_descriptor element_type,
const vertex_buffer& vbo,
size_t offset_in_bytes,
size_t nr_elements,
unsigned stride_in_bytes = 0);
307 template <
typename T>
308 void set_color_array(
const context& ctx,
const vertex_buffer& vbo,
size_t offset_in_bytes,
size_t nr_elements,
unsigned stride_in_bytes = 0) {
set_color_array(ctx,
type_descriptor(element_descriptor_traits<T>::get_type_descriptor(T()),
true), vbo, offset_in_bytes, nr_elements, stride_in_bytes); }
319 template <
typename T>
321 this->indices = &indices.front();
322 index_buffer_ptr = 0;
323 index_count = indices.size();
325 if (!keep_on_cpu && aam_ptr)
326 return aam_ptr->set_indices(ctx, indices);
340 template <
typename T>
341 bool set_indices(
const context& ctx,
const T* indices,
size_t nr_indices,
bool keep_on_cpu =
false) {
342 this->indices = indices;
343 index_buffer_ptr = 0;
344 index_count = nr_indices;
346 if (!keep_on_cpu && aam_ptr)
347 return aam_ptr->set_indices(ctx, indices, nr_indices);
361 template <
typename T>
363 index_buffer_ptr = &vbo;
368 aam_ptr->remove_indices(ctx);
374 void remove_indices(
const context& ctx);
376 virtual bool validate_attributes(
const context& ctx)
const;
378 bool validate_and_enable(
context& ctx);
380 virtual bool enable(
context& ctx);
393 virtual void draw(
context& ctx,
size_t start,
size_t count,
394 bool use_strips =
false,
bool use_adjacency =
false, uint32_t strip_restart_index = -1);
396 virtual bool disable(
context& ctx);
406 virtual bool render(
context& ctx,
size_t start,
size_t count,
407 bool use_strips =
false,
bool use_adjacency =
false, uint32_t strip_restart_index = -1);
409 virtual void clear(
const context& ctx);
413 int get_vbo(
const context& ctx,
const std::string attr_name) {
416 int loc = ref_prog().get_attribute_location(ctx, attr_name);
417 auto it = aam_ptr->
vbos.find(loc);
418 if(it != aam_ptr->
vbos.end()) {
420 if(vbo_ptr->handle) {
421 return (
const int&)vbo_ptr->handle - 1;
426 return (
const int&)-1;
432 #include <cgv/config/lib_end.h>