StapleGL
Header-only C++20 OpenGL wrapper
Loading...
Searching...
No Matches
index_buffer.hpp
Go to the documentation of this file.
1
20#pragma once
21
22#include "gl_functions.hpp"
23#include <cstdint>
24#include <iostream>
25#include <span>
26
27namespace staplegl {
28
45public:
46 index_buffer() = default;
47
54 index_buffer(std::span<const std::uint32_t> indices) noexcept;
55
61
62 index_buffer(const index_buffer&) = delete;
63 auto operator=(const index_buffer&) -> index_buffer& = delete;
64
73 index_buffer(index_buffer&&) noexcept;
74
82 auto operator=(index_buffer&&) noexcept -> index_buffer&;
83
88 void bind() const;
89
94 void unbind() const;
95
101 [[nodiscard]] constexpr auto count() const -> std::int32_t;
102
108 [[nodiscard]] constexpr auto id() const -> std::uint32_t;
109
110private:
111 std::uint32_t m_id {};
112 std::int32_t m_count {};
113};
114
115inline index_buffer::index_buffer(std::span<const std::uint32_t> indices) noexcept
116 : m_count { static_cast<int32_t>(indices.size()) }
117{
118 glGenBuffers(1, &m_id);
119 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_id);
120
121 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
122 static_cast<ptrdiff_t>(indices.size_bytes()),
123 indices.data(),
124 GL_STATIC_DRAW);
125}
126
128{
129 if (m_id != 0) {
130 glDeleteBuffers(1, &m_id);
131 }
132}
133
135 : m_id { other.m_id }
136 , m_count { other.m_count }
137{
138 other.m_id = 0;
139}
140
141inline auto index_buffer::operator=(index_buffer&& other) noexcept -> index_buffer&
142{
143 if (this != &other) {
144 m_id = other.m_id;
145 m_count = other.m_count;
146
147 other.m_id = 0;
148 }
149
150 return *this;
151}
152
153inline void index_buffer::bind() const
154{
155 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_id);
156}
157
158inline void index_buffer::unbind() const
159{
160 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
161}
162
163constexpr auto index_buffer::count() const -> std::int32_t
164{
165 return m_count;
166}
167}
Element Buffer Object (EBO) wrapper.
index_buffer(const index_buffer &)=delete
constexpr auto count() const -> std::int32_t
Get the number of indices in the index buffer object.
void bind() const
Bind the index buffer object.
void unbind() const
Unbind the index buffer object.
auto operator=(const index_buffer &) -> index_buffer &=delete
~index_buffer()
Destroy the index buffer object.
Loads OpenGL functions.