cgv
attribute_array_binding.h
1 #pragma once
2 
3 #include "element_traits.h"
4 
5 #include "lib_begin.h"
6 
7 namespace cgv {
8  namespace render {
9 
10  class CGV_API vertex_buffer;
11 
20 {
21  int get_attribute_location(const context& ctx, const shader_program& prog, const std::string& attr_name) const;
22 public:
25  static bool is_global_array_enabled(const context& ctx, int loc);
28  static bool enable_global_array(const context& ctx, int loc);
30  static bool disable_global_array(const context& ctx, int loc);
32  static bool set_global_attribute_array(const context& ctx, int loc, const vertex_buffer& vbo, type_descriptor td, size_t size, size_t offset, unsigned stride = 0);
34  template <typename T>
35  static bool set_global_attribute_array(const context& ctx, int loc, const T& array) {
36  return ctx.set_attribute_array_void(0, loc, array_descriptor_traits<T>::get_type_descriptor(array), 0, array_descriptor_traits<T>::get_address(array), array_descriptor_traits<T>::get_nr_elements(array));
37  }
39  template <typename T>
40  static bool set_global_attribute_array(const context& ctx, int loc, const T* array_ptr, size_t nr_elements, unsigned stride = 0) {
41  return ctx.set_attribute_array_void(0, loc, element_descriptor_traits<T>::get_type_descriptor(*array_ptr), 0, array_ptr, nr_elements, stride);
42  }
44  static bool set_global_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 = 0);
46  static bool set_global_element_array(const context& ctx, const vertex_buffer& vbe);
53  bool create(const context& ctx);
55  void destruct(const context& ctx);
57  bool enable(context& ctx);
59  bool disable(context& ctx);
61  bool enable_array(const context& ctx, int loc);
63  bool is_array_enabled(const context& ctx, int loc) const;
65  bool disable_array(const context& ctx, int loc);
67  template <typename T>
68  bool set_attribute_array(const context& ctx, int loc, const T& array) {
69  return ctx.set_attribute_array_void(this, loc, array_descriptor_traits<T>::get_type_descriptor(array), 0, array_descriptor_traits<T>::get_address(array), array_descriptor_traits<T>::get_nr_elements(array));
70  }
72  template <typename T>
73  bool set_attribute_array(const context& ctx, int loc, const T* value_ptr, unsigned nr_elements, unsigned stride = 0) {
74  return ctx.set_attribute_array_void(this, loc, get_element_type(*value_ptr), 0, value_ptr, nr_elements, stride);
75  }
77  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 = 0);
79  template <typename T>
80  bool bind_attribute_array(const context& ctx, const shader_program& prog, const std::string& attribute_name, const T& array) {
81  int loc = get_attribute_location(ctx, prog, attribute_name);
82  if (loc == -1)
83  return false;
84  return ctx.set_attribute_array_void(this, loc, array_descriptor_traits<T>::get_type_descriptor(array), 0, array_descriptor_traits<T>::get_address(array), array_descriptor_traits<T>::get_nr_elements(array));
85  }
87  template <typename T>
88  bool bind_attribute_array(const context& ctx, const shader_program& prog, const std::string& attribute_name, const T* value_ptr, unsigned nr_elements, unsigned stride = 0) {
89  int loc = get_attribute_location(ctx, prog, attribute_name);
90  if (loc == -1)
91  return false;
92  return ctx.set_attribute_array_void(this, loc, get_element_type(*value_ptr), 0, value_ptr, nr_elements, stride);
93  }
95  bool bind_attribute_array(const context& ctx,
96  const shader_program& prog, const std::string& attribute_name,
97  type_descriptor element_type, const vertex_buffer& vbo,
98  size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes = 0);
100  bool set_element_array(const context& ctx, const vertex_buffer& vbe);
101 };
102 
103  }
104 }
105 
106 #include <cgv/config/lib_end.h>
cgv::render::attribute_array_binding::bind_attribute_array
bool bind_attribute_array(const context &ctx, const shader_program &prog, const std::string &attribute_name, const T *value_ptr, unsigned nr_elements, unsigned stride=0)
convenience function that determines attribute location in program by name and then uses set_attribut...
Definition: attribute_array_binding.h:88
cgv::render::attribute_array_binding
Definition: attribute_array_binding.h:20
cgv::render::type_descriptor
compact type description of data that can be sent to the context; convertible to int
Definition: context.h:36
cgv::render::attribute_array_binding_base
base class for attribute_array_bindings
Definition: context.h:338
cgv::render::vertex_buffer
Definition: vertex_buffer.h:13
cgv::render::attribute_array_binding::set_attribute_array
bool set_attribute_array(const context &ctx, int loc, const T *value_ptr, unsigned nr_elements, unsigned stride=0)
set a vertex attribute to a single value or an array of values through the cgv::math::vec or std::vec...
Definition: attribute_array_binding.h:73
cgv::render::attribute_array_binding::set_attribute_array
bool set_attribute_array(const context &ctx, int loc, const T &array)
set vertex attribute location to given array and enable array
Definition: attribute_array_binding.h:68
cgv::render::shader_program
Definition: shader_program.h:25
cgv::render::attribute_array_binding::set_global_attribute_array
static bool set_global_attribute_array(const context &ctx, int loc, const T &array)
point array of vertex attribute at location loc to array array stored in CPU memory; in case of succe...
Definition: attribute_array_binding.h:35
cgv::render::attribute_array_binding::set_global_attribute_array
static bool set_global_attribute_array(const context &ctx, int loc, const T *array_ptr, size_t nr_elements, unsigned stride=0)
point array of vertex attribute at location loc to array with nr_elements elements pointed to by arra...
Definition: attribute_array_binding.h:40
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::render::context
Definition: context.h:525
cgv::render::attribute_array_binding::bind_attribute_array
bool bind_attribute_array(const context &ctx, const shader_program &prog, const std::string &attribute_name, const T &array)
convenience function that determines attribute location in program by name and then uses set_attribut...
Definition: attribute_array_binding.h:80