38template<
typename DataType>
66 void bind()
const noexcept;
70 void draw(GLenum mode,
int first, std::size_t n_vertices)
const;
89template<
typename T, std::
size_t size>
110 template<
typename... Ts>
119 void update(
size_t index,
const Eigen::Vector3f& value);
164template<std::
size_t size>
187 template<
typename... Ts>
208 std::vector<
unsigned int>
data;
227 void clear() noexcept;
229 void to_gpu() const noexcept;
258 void clear() noexcept;
275template<typename DataType>
278 if constexpr (std::is_same_v<DataType, char>) {
280 }
else if constexpr (std::is_same_v<DataType, unsigned char>) {
281 return GL_UNSIGNED_BYTE;
282 }
else if constexpr (std::is_same_v<DataType, int>) {
284 }
else if constexpr (std::is_same_v<DataType, unsigned int>) {
285 return GL_UNSIGNED_INT;
286 }
else if constexpr (std::is_same_v<DataType, float>) {
288 }
else if constexpr (std::is_same_v<DataType, double>) {
297template<
typename T, std::
size_t size>
301 glGenBuffers(1, &(this->descriptor));
304template<
typename T, std::
size_t size>
309 other.descriptor = 0;
312template<
typename T, std::
size_t size>
315 if (this->descriptor != 0) {
316 glDeleteBuffers(1, &(this->descriptor));
320template<
typename T, std::
size_t size>
321template<
typename... Ts>
325 (std::is_same_v<
decltype(values), T> && ...),
326 "ArrayBuffer: all values to be appended must have the same type as T"
329 sizeof...(values) == size,
330 "ArrayBuffer: number of values to be appended must be same as size per vertex"
332 (this->data.push_back(values), ...);
335template<
typename T, std::
size_t size>
338 const GLintptr offset = index * 3 *
sizeof(float);
339 data[index * 3] = value.x();
340 data[index * 3 + 1] = value.y();
341 data[index * 3 + 2] = value.z();
343 glBufferSubData(GL_ARRAY_BUFFER, offset, 3 *
sizeof(
float), value.data());
346template<
typename T, std::
size_t size>
349 return this->data.size() / size;
352template<
typename T, std::
size_t size>
355 glBindBuffer(GL_ARRAY_BUFFER, this->descriptor);
358template<
typename T, std::
size_t size>
361 glBindBuffer(GL_ARRAY_BUFFER, 0);
364template<
typename T, std::
size_t size>
368 glVertexAttribPointer(
369 this->layout_location, size, data_type, GL_FALSE, size *
sizeof(T), (
void*)0
371 glEnableVertexAttribArray(this->layout_location);
374template<
typename T, std::
size_t size>
377 glDisableVertexAttribArray(this->layout_location);
380template<
typename T, std::
size_t size>
384 glBufferData(GL_ARRAY_BUFFER,
sizeof(T) * this->data.size(), this->data.data(), this->usage);
390template<std::
size_t size>
393 glGenBuffers(1, &(this->descriptor));
396template<std::
size_t size>
400 other.descriptor = 0;
403template<std::
size_t size>
406 if (this->descriptor != 0) {
407 glDeleteBuffers(1, &(this->descriptor));
411template<std::
size_t size>
414 return this->data.size() / size;
417template<std::
size_t size>
418template<
typename... Ts>
422 (std::is_same_v<
decltype(values),
unsigned int> && ...),
423 "ElementArrayBuffer: all values to be appended must be unsigned int"
426 sizeof...(values) == size,
427 "ElementArrayBuffer: number of values to be appended must be as same as size per vertex"
429 (this->data.push_back(values), ...);
432template<std::
size_t size>
435 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->descriptor);
438template<std::
size_t size>
441 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
444template<std::
size_t size>
449 GL_ELEMENT_ARRAY_BUFFER,
sizeof(
unsigned int) * this->data.size(), this->data.data(),
所有对 OpenGL 的封装类型和工具函数均在此命名空间中。
定义 gl.hpp:24
对 OpenGL 数组缓冲 (Array Buffer) 的封装。
定义 gl.hpp:91
ArrayBuffer(GLenum buffer_usage, unsigned int layout_location)
定义 gl.hpp:298
void update(size_t index, const Eigen::Vector3f &value)
更新指定位置的 3 个 float 数据,仅限 T = float 时使用,否则行为未定义。
定义 gl.hpp:336
ArrayBuffer(const ArrayBuffer &other)=delete
void release() const noexcept
定义 gl.hpp:359
unsigned int layout_location
定义 gl.hpp:145
void append(Ts... values)
定义 gl.hpp:322
void bind() const noexcept
定义 gl.hpp:353
~ArrayBuffer()
定义 gl.hpp:313
unsigned int usage
定义 gl.hpp:141
std::vector< T > data
定义 gl.hpp:147
std::size_t count() const
定义 gl.hpp:347
void to_gpu() const noexcept
定义 gl.hpp:381
void disable() const noexcept
定义 gl.hpp:375
ArrayBuffer & operator=(ArrayBuffer &other)=delete
unsigned int descriptor
定义 gl.hpp:136
void specify_vertex_attribute() const noexcept
定义 gl.hpp:365
void to_gpu() const
定义 gl.cpp:96
DrawableLineSet()
定义 gl.cpp:81
ElementArrayBuffer< 2 > lines
定义 gl.hpp:267
void clear() noexcept
定义 gl.cpp:90
ArrayBuffer< float, 3 > positions
定义 gl.hpp:265
VertexArrayObject VAO
定义 gl.hpp:263
DrawableLineSet & operator=(const DrawableLineSet &other)=delete
DrawableLineSet(const DrawableLineSet &other)=delete
void to_gpu() const noexcept
定义 gl.cpp:70
DrawableMesh(const DrawableMesh &other)=delete
ArrayBuffer< float, 3 > positions
定义 gl.hpp:234
ElementArrayBuffer< 2 > edges
定义 gl.hpp:238
DrawableMesh()
定义 gl.cpp:48
VertexArrayObject VAO
定义 gl.hpp:232
ElementArrayBuffer< 3 > triangles
定义 gl.hpp:240
DrawableMesh & operator=(const DrawableMesh &other)=delete
ArrayBuffer< float, 3 > normals
定义 gl.hpp:236
void clear() noexcept
定义 gl.cpp:62
对 OpenGL 索引数组缓冲 (Element Array Buffer) 的封装。
定义 gl.hpp:166
ElementArrayBuffer(const ElementArrayBuffer &other)=delete
std::size_t count() const
定义 gl.hpp:412
void bind() const noexcept
定义 gl.hpp:433
unsigned int descriptor
定义 gl.hpp:199
ElementArrayBuffer(unsigned int buffer_usage)
构造一个 EBO 对象。
定义 gl.hpp:391
void to_gpu() const noexcept
定义 gl.hpp:445
void append(Ts... values)
定义 gl.hpp:419
ElementArrayBuffer & operator=(ElementArrayBuffer &other)=delete
std::vector< unsigned int > data
内存中的索引数据副本。
定义 gl.hpp:208
unsigned int usage
定义 gl.hpp:201
void release() const noexcept
定义 gl.hpp:439
~ElementArrayBuffer()
调用 glDeleteBuffers 销毁该 EBO。
定义 gl.hpp:404
对 OpenGL 顶点数组对象 (Vertex Array Object) 的封装。
定义 gl.hpp:50
void bind() const noexcept
定义 gl.cpp:31
VertexArrayObject()
定义 gl.cpp:14
unsigned int descriptor
定义 gl.hpp:73
void draw(GLenum mode, int first, std::size_t n_vertices) const
定义 gl.cpp:41
void release() const noexcept
定义 gl.cpp:36