engine/include/resources/mesh.hpp

70 lines
1.4 KiB
C++
Raw Normal View History

2022-12-20 23:51:04 +00:00
#pragma once
2023-01-06 16:45:39 +00:00
#include "log.hpp"
2022-12-20 23:51:04 +00:00
#include "gfx.hpp"
#include "gfx_device.hpp"
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
#include <string>
namespace engine {
struct Vertex {
glm::vec3 pos;
glm::vec3 norm;
glm::vec2 uv;
};
}
namespace engine::resources {
class Mesh {
public:
Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices)
: m_gfx(gfx)
{
2023-01-06 16:45:39 +00:00
std::vector<uint32_t> indices(vertices.size());
for (uint32_t i = 0; i < indices.size(); i++) {
2022-12-20 23:51:04 +00:00
indices[i] = i;
}
2023-01-06 16:45:39 +00:00
initMesh(vertices, indices);
2022-12-20 23:51:04 +00:00
}
2023-01-05 13:21:33 +00:00
Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices)
: m_gfx(gfx)
{
2023-01-06 16:45:39 +00:00
initMesh(vertices, indices);
2023-01-05 13:21:33 +00:00
}
2022-12-20 23:51:04 +00:00
~Mesh()
{
m_gfx->destroyBuffer(m_ib);
m_gfx->destroyBuffer(m_vb);
}
Mesh(const Mesh&) = delete;
Mesh& operator=(const Mesh&) = delete;
auto getVB() { return m_vb; }
auto getIB() { return m_ib; }
auto getCount() { return m_count; }
private:
GFXDevice* const m_gfx;
const gfx::Buffer* m_vb;
const gfx::Buffer* m_ib;
uint32_t m_count;
2023-01-06 16:45:39 +00:00
void initMesh(const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices)
{
m_vb = m_gfx->createBuffer(gfx::BufferType::VERTEX, vertices.size() * sizeof(Vertex), vertices.data());
m_ib = m_gfx->createBuffer(gfx::BufferType::INDEX, indices.size() * sizeof(uint32_t), indices.data());
m_count = indices.size();
INFO("Loaded mesh, vertices: {}, indices: {}", vertices.size(), indices.size());
}
2022-12-20 23:51:04 +00:00
};
}