1#ifndef DANDELION_PLATFORM_GL_HPP
2#define DANDELION_PLATFORM_GL_HPP
40template<
typename DataType>
72 void draw(GLenum mode,
int first, std::size_t count);
74 unsigned int descriptor;
90template<
typename T, std::
size_t size>
109 template<
typename... Ts>
118 void update(
size_t index,
const Eigen::Vector3f& value);
134 unsigned int descriptor;
158template<std::
size_t size>
159struct ElementArrayBuffer
161 ElementArrayBuffer(
unsigned int buffer_usage);
162 ElementArrayBuffer(
const ElementArrayBuffer& other) =
delete;
163 ElementArrayBuffer& operator=(ElementArrayBuffer& other) =
delete;
166 ~ElementArrayBuffer();
168 template<
typename... Ts>
176 unsigned int descriptor;
179 std::vector<unsigned int>
data;
190 Material(
const Eigen::Vector3f& K_ambient = Eigen::Vector3f(1.0f, 1.0f, 1.0f),
191 const Eigen::Vector3f& K_diffuse = Eigen::Vector3f(0.5f, 0.5f, 0.5f),
192 const Eigen::Vector3f& K_specular = Eigen::Vector3f(0.0f, 0.0f, 0.0f),
223 Mesh(
const Mesh& other) =
delete;
226 Eigen::Vector3f
vertex(
size_t index)
const;
228 Eigen::Vector3f
normal(
size_t index)
const;
230 std::array<size_t, 2>
edge(
size_t index)
const;
232 std::array<size_t, 3>
face(
size_t index)
const;
243 void render(
const Shader& shader,
unsigned int element_flags,
bool face_shading =
true,
244 const Eigen::Vector3f& global_color = default_wireframe_color);
246 constexpr static unsigned int vertices_flag = 1u;
247 constexpr static unsigned int edges_flag = 1u << 1u;
248 constexpr static unsigned int faces_flag = 1u << 2u;
249 const static Eigen::Vector3f default_wireframe_color;
250 const static Eigen::Vector3f default_face_color;
251 const static Eigen::Vector3f highlight_wireframe_color;
252 const static Eigen::Vector3f highlight_face_color;
271 LineSet(
const std::string& name, Eigen::Vector3f color = GL::Mesh::default_wireframe_color);
274 LineSet(LineSet&& other);
278 void add_arrow(
const Eigen::Vector3f& from,
const Eigen::Vector3f& to);
280 void update_arrow(
size_t index,
const Eigen::Vector3f& from,
const Eigen::Vector3f& to);
282 void add_AABB(
const Eigen::Vector3f& p_min,
const Eigen::Vector3f& p_max);
308template<
typename DataType>
311 if constexpr (std::is_same_v<DataType, char>) {
313 }
else if constexpr (std::is_same_v<DataType, unsigned char>) {
314 return GL_UNSIGNED_BYTE;
315 }
else if constexpr (std::is_same_v<DataType, int>) {
317 }
else if constexpr (std::is_same_v<DataType, unsigned int>) {
318 return GL_UNSIGNED_INT;
319 }
else if constexpr (std::is_same_v<DataType, float>) {
321 }
else if constexpr (std::is_same_v<DataType, double>) {
330template<
typename T, std::
size_t size>
334 glGenBuffers(1, &(this->descriptor));
337template<
typename T, std::
size_t size>
340 data(std::move(other.data))
342 other.descriptor = 0;
345template<
typename T, std::
size_t size>
348 if (this->descriptor != 0) {
349 glDeleteBuffers(1, &(this->descriptor));
353template<
typename T, std::
size_t size>
354template<
typename... Ts>
357 static_assert((std::is_same_v<
decltype(values), T> && ...),
358 "ArrayBuffer: all values to be appended must have the same type as T");
359 static_assert(
sizeof...(values) == size,
360 "ArrayBuffer: number of values to be appended must be same as size per vertex");
361 (this->data.push_back(values), ...);
364template<
typename T, std::
size_t size>
367 const GLintptr offset = index * 3 *
sizeof(float);
368 data[index * 3] = value.x();
369 data[index * 3 + 1] = value.y();
370 data[index * 3 + 2] = value.z();
372 glBufferSubData(GL_ARRAY_BUFFER, offset, 3 *
sizeof(
float), value.data());
375template<
typename T, std::
size_t size>
378 return this->data.size() / size;
381template<
typename T, std::
size_t size>
384 glBindBuffer(GL_ARRAY_BUFFER, this->descriptor);
387template<
typename T, std::
size_t size>
390 glBindBuffer(GL_ARRAY_BUFFER, 0);
393template<
typename T, std::
size_t size>
397 glVertexAttribPointer(this->
layout_location, size, data_type, GL_FALSE, size *
sizeof(T),
402template<
typename T, std::
size_t size>
408template<
typename T, std::
size_t size>
412 glBufferData(GL_ARRAY_BUFFER,
sizeof(T) * this->data.size(), this->data.data(), this->usage);
418template<std::
size_t size>
419ElementArrayBuffer<size>::ElementArrayBuffer(
unsigned int buffer_usage) : usage(buffer_usage)
421 glGenBuffers(1, &(this->descriptor));
424template<std::
size_t size>
425ElementArrayBuffer<size>::ElementArrayBuffer(ElementArrayBuffer&& other)
426 : descriptor(other.descriptor), usage(other.usage),
data(std::move(other.
data))
428 other.descriptor = 0;
431template<std::
size_t size>
432ElementArrayBuffer<size>::~ElementArrayBuffer()
434 if (this->descriptor != 0) {
435 glDeleteBuffers(1, &(this->descriptor));
439template<std::
size_t size>
442 return this->
data.size() / size;
445template<std::
size_t size>
446template<
typename... Ts>
449 static_assert((std::is_same_v<
decltype(values),
unsigned int> && ...),
450 "ElementArrayBuffer: all values to be appended must be unsigned int");
452 sizeof...(values) == size,
453 "ElementArrayBuffer: number of values to be appended must be as same as size per vertex");
454 (this->
data.push_back(values), ...);
457template<std::
size_t size>
458void ElementArrayBuffer<size>::bind()
460 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->descriptor);
463template<std::
size_t size>
464void ElementArrayBuffer<size>::release()
466 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
469template<std::
size_t size>
470void ElementArrayBuffer<size>::to_gpu()
473 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
sizeof(
unsigned int) * this->data.size(),
474 this->data.data(), this->usage);
对 GLSL Shader 的简单封装。
定义 shader.hpp:24
这个文件定义了一些和渲染(离线渲染或场景预览)相关的常量、枚举等。
对 OpenGL 数组缓冲 (Array Buffer) 的封装。
定义 gl.hpp:92
ArrayBuffer(GLenum buffer_usage, unsigned int layout_location)
定义 gl.hpp:331
void update(size_t index, const Eigen::Vector3f &value)
更新指定位置的 size 个数据。
定义 gl.hpp:365
void specify_vertex_attribute()
定义 gl.hpp:394
unsigned int layout_location
定义 gl.hpp:143
void append(Ts... values)
定义 gl.hpp:355
void release()
定义 gl.hpp:388
~ArrayBuffer()
定义 gl.hpp:346
unsigned int usage
定义 gl.hpp:139
void disable()
定义 gl.hpp:403
std::size_t count() const
定义 gl.hpp:376
void to_gpu()
定义 gl.hpp:409
ArrayBuffer(ArrayBuffer &&other)
定义 gl.hpp:338
对 OpenGL 索引数组缓冲 (Element Array Buffer) 的封装。
定义 gl.hpp:160
std::size_t count() const
定义 gl.hpp:440
ElementArrayBuffer(ElementArrayBuffer &&other)
定义 gl.hpp:425
void append(Ts... values)
定义 gl.hpp:447
std::vector< unsigned int > data
定义 gl.hpp:179
void to_gpu()
定义 gl.cpp:281
void add_line_segment(const Eigen::Vector3f &a, const Eigen::Vector3f &b)
定义 gl.cpp:202
void add_AABB(const Eigen::Vector3f &p_min, const Eigen::Vector3f &p_max)
定义 gl.cpp:245
void render(const Shader &shader)
渲染该线条集。
定义 gl.cpp:289
Eigen::Vector3f line_color
定义 gl.hpp:296
void update_arrow(size_t index, const Eigen::Vector3f &from, const Eigen::Vector3f &to)
定义 gl.cpp:232
void add_arrow(const Eigen::Vector3f &from, const Eigen::Vector3f &to)
定义 gl.cpp:217
void clear()
定义 gl.cpp:275
LineSet(const LineSet &other)=delete
Eigen::Vector3f diffuse
定义 gl.hpp:197
float shininess
定义 gl.hpp:201
Eigen::Vector3f specular
定义 gl.hpp:199
Eigen::Vector3f ambient
定义 gl.hpp:195
std::array< size_t, 3 > face(size_t index) const
定义 gl.cpp:112
Material material
定义 gl.hpp:259
void render(const Shader &shader, unsigned int element_flags, bool face_shading=true, const Eigen::Vector3f &global_color=default_wireframe_color)
渲染这个 mesh。
定义 gl.cpp:137
Mesh(const Mesh &other)=delete
Eigen::Vector3f normal(size_t index) const
定义 gl.cpp:102
Eigen::Vector3f vertex(size_t index) const
定义 gl.cpp:97
std::array< size_t, 2 > edge(size_t index) const
定义 gl.cpp:107
对 OpenGL 顶点数组对象 (Vertex Array Object) 的封装。
定义 gl.hpp:52
~VertexArrayObject()
定义 gl.cpp:27
VertexArrayObject()
定义 gl.cpp:17
void release()
定义 gl.cpp:39
void draw(GLenum mode, int first, std::size_t count)
定义 gl.cpp:44