Initial commit

This commit is contained in:
2026-09-27 01:33:35 +02:00
commit 3ac329a21b
1185 changed files with 543634 additions and 0 deletions

86
src/mesh.cpp Normal file
View File

@@ -0,0 +1,86 @@
#include <cstddef>
#include <utility>
#include "mesh.hpp"
namespace Fan
{
Rc<Mesh> Mesh::create(const std::vector<Vertex>& vertices,
const std::vector<GLuint>& indices/* = {}*/)
{
GLuint VBO{}, VAO{}, EBO{};
bool has_indices = !indices.empty();
glGenBuffers(1, &VBO);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// Upload vertex data
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vertices[0]),
&vertices[0], GL_STATIC_DRAW);
// Upload index data
if (has_indices) {
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(indices[0]),
&indices[0], GL_STATIC_DRAW);
}
// Define vertex data layout
GLsizei stride = sizeof(Vertex);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*)offsetof(Vertex, position));
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (void*)offsetof(Vertex, normal));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, stride, (void*)offsetof(Vertex, texture_coords));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
return make_rc<Mesh>(vertices, indices, has_indices, VBO, VAO, EBO);
}
Mesh::~Mesh()
{
glDeleteBuffers(1, &m_EBO);
glDeleteBuffers(1, &m_VBO);
glDeleteVertexArrays(1, &m_VAO);
}
Mesh::Mesh(Mesh&& other) noexcept
: m_vertices(std::move(other.m_vertices))
, m_indices(std::move(other.m_indices))
, m_has_indices(std::exchange(other.m_has_indices, false))
, m_VBO(std::exchange(other.m_VBO, 0))
, m_VAO(std::exchange(other.m_VAO, 0))
, m_EBO(std::exchange(other.m_EBO, 0))
{
}
Mesh& Mesh::operator=(Mesh&& other) noexcept
{
if (this == &other) { return *this; }
std::swap(m_vertices, other.m_vertices);
std::swap(m_indices, other.m_indices);
std::swap(m_has_indices, other.m_has_indices);
std::swap(m_VBO, other.m_VBO);
std::swap(m_VAO, other.m_VAO);
std::swap(m_EBO, other.m_EBO);
return *this;
}
void Mesh::draw() const
{
glBindVertexArray(m_VAO);
if (m_has_indices) {
glDrawElements(GL_TRIANGLES, m_indices.size(), GL_UNSIGNED_INT, 0);
} else {
glDrawArrays(GL_TRIANGLES, 0, m_vertices.size());
}
}
} // namespace Fan