Tatooine
vertexbuffer.h
Go to the documentation of this file.
1#ifndef TATOOINE_GL_VERTEXBUFFER_H
2#define TATOOINE_GL_VERTEXBUFFER_H
3//==============================================================================
9#include <tatooine/tensor.h>
10#include <tatooine/tuple.h>
11
12#include <initializer_list>
13#include <iostream>
14#include <vector>
15//==============================================================================
16namespace tatooine::gl {
17//==============================================================================
18template <typename... Ts>
20 : public buffer<GL_ARRAY_BUFFER,
21 std::conditional_t<sizeof...(Ts) == 1, head_t<Ts...>,
22 tuple<Ts...> > > {
23 public:
25 GL_ARRAY_BUFFER,
26 std::conditional_t<sizeof...(Ts) == 1, head_t<Ts...>, tuple<Ts...> > >;
27 using this_type = vertexbuffer<Ts...>;
28 using typename parent_type::value_type;
29
30 static constexpr auto data_size = parent_type::data_size;
31
33
34 static constexpr auto num_attributes = sizeof...(Ts);
35 static constexpr std::array<GLsizei, num_attributes> num_components{
36 static_cast<GLsizei>(tatooine::tensor_num_components<Ts>)...};
37 static constexpr std::array<GLenum, num_attributes> types{
38 value_type_v<Ts>...};
39 static constexpr std::array<std::size_t, num_attributes> offsets =
41
42 //----------------------------------------------------------------------------
44 vertexbuffer(vertexbuffer const& other) : parent_type{other} {}
45 vertexbuffer(vertexbuffer&& other) noexcept : parent_type{other} {}
46
47 auto operator=(this_type const& other) -> auto& {
49 return *this;
50 }
51
52 auto operator=(this_type&& other) noexcept -> auto& {
53 parent_type::operator=(std::move(other));
54 return *this;
55 }
56
57 vertexbuffer(std::size_t n, buffer_usage usage = default_usage)
58 : parent_type(n, usage) {}
59 vertexbuffer(std::size_t n, value_type const& initial,
61 : parent_type(n, initial, usage) {}
62 vertexbuffer(std::vector<value_type> const& data, buffer_usage usage = default_usage)
63 : parent_type(data, usage) {}
64 vertexbuffer(std::initializer_list<value_type>&& list)
65 : parent_type(std::move(list), default_usage) {}
66
67 void push_back(Ts const&... ts) {
68 if constexpr (num_attributes == 1) {
70 } else {
72 }
73 }
74
75 //============================================================================
76 static constexpr void activate_attributes() {
77 for (unsigned int i = 0; i < num_attributes; i++) {
80 data_size, (void*)offsets[i]);
81 }
82 }
83 //----------------------------------------------------------------------------
84 private:
85 template <std::convertible_to<GLboolean>... Normalized, std::size_t... Is>
86 static constexpr auto activate_attributes(
87 std::index_sequence<Is...>, Normalized... normalized) -> void {
88 static_assert(sizeof...(Normalized) == sizeof...(Is));
89 static_assert(sizeof...(Normalized) == num_attributes);
90 (
91 [&](std::size_t i, GLboolean normalized) {
93 gl::vertex_attrib_pointer(static_cast<GLuint>(i), num_components[i], types[i], normalized,
94 data_size, (void*)(offsets[i]));
95 }(Is, static_cast<GLboolean>(normalized)),
96 ...);
97 }
98 //----------------------------------------------------------------------------
99 public:
100 template <std::convertible_to<GLboolean>... Normalized>
101 static constexpr void activate_attributes(Normalized... normalized) {
102 activate_attributes(std::make_index_sequence<num_attributes>{},
103 normalized...);
104 }
105 //----------------------------------------------------------------------------
106 static constexpr void deactivate_attributes() {
107 for (unsigned int i = 0; i < num_attributes; i++)
109 }
110};
111
112//==============================================================================
113} // namespace tatooine::gl
114//==============================================================================
115
116#endif
buffer base class for each of the OpenGL buffer types
Definition: buffer.h:405
std::conditional_t< sizeof...(Ts)==1, head_t< Ts... >, tuple< Ts... > > value_type
Definition: buffer.h:416
Definition: vertexbuffer.h:22
static constexpr std::array< GLsizei, num_attributes > num_components
Definition: vertexbuffer.h:35
static constexpr void activate_attributes()
Definition: vertexbuffer.h:76
vertexbuffer(std::size_t n, buffer_usage usage=default_usage)
Definition: vertexbuffer.h:57
static constexpr void deactivate_attributes()
Definition: vertexbuffer.h:106
static constexpr std::array< std::size_t, num_attributes > offsets
Definition: vertexbuffer.h:39
vertexbuffer(std::vector< value_type > const &data, buffer_usage usage=default_usage)
Definition: vertexbuffer.h:62
auto operator=(this_type const &other) -> auto &
Definition: vertexbuffer.h:47
static constexpr auto activate_attributes(std::index_sequence< Is... >, Normalized... normalized) -> void
Definition: vertexbuffer.h:86
static buffer_usage const default_usage
Definition: vertexbuffer.h:32
static constexpr auto num_attributes
Definition: vertexbuffer.h:34
vertexbuffer(buffer_usage usage=default_usage)
Definition: vertexbuffer.h:43
static constexpr auto data_size
Definition: vertexbuffer.h:30
vertexbuffer(vertexbuffer const &other)
Definition: vertexbuffer.h:44
static constexpr std::array< GLenum, num_attributes > types
Definition: vertexbuffer.h:37
auto operator=(this_type &&other) noexcept -> auto &
Definition: vertexbuffer.h:52
static constexpr void activate_attributes(Normalized... normalized)
Definition: vertexbuffer.h:101
vertexbuffer(vertexbuffer &&other) noexcept
Definition: vertexbuffer.h:45
void push_back(Ts const &... ts)
Definition: vertexbuffer.h:67
vertexbuffer(std::size_t n, value_type const &initial, buffer_usage usage=default_usage)
Definition: vertexbuffer.h:59
vertexbuffer(std::initializer_list< value_type > &&list)
Definition: vertexbuffer.h:64
Definition: ansiformat.h:6
DLL_API auto vertex_attrib_pointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer) -> void
DLL_API auto disable_vertex_attrib_array(GLuint index) -> void
DLL_API auto enable_vertex_attrib_array(GLuint index) -> void
buffer_usage
Definition: buffer_usage.h:8
typename head< Ts... >::type head_t
Definition: utility.h:38
static constexpr auto gen(size_t, size_t)
Definition: vbohelpers.h:18
Definition: tuple.h:11
type_list_push_back< this_type, T > push_back
Definition: type_list.h:254