MinusculeRender/src/mesh.cc

89 lines
3.2 KiB
C++
Raw Normal View History

2024-03-22 20:12:09 +08:00
// Copyright 2024 SquareBlock Inc. All Rights Reserved.
// Author: tianlei.richard@qq.com (tianlei.richard)
#include "mesh.h"
#include "OBJ_Loader.h"
#include "spdlog/spdlog.h"
2024-03-22 20:12:09 +08:00
#include <filesystem>
#define OBJL_VEC2_TO_EIGEN_VECTOR(objl_v) \
Eigen::Vector2d { objl_v.X, objl_v.Y }
#define OBJL_VEC3_TO_EIGEN_VECTOR(objl_v) \
Eigen::Vector3d { objl_v.X, objl_v.Y, objl_v.Z }
Mesh::Mesh(const std::vector<std::shared_ptr<Vertex>> &vertices,
2024-03-22 20:12:09 +08:00
const std::vector<Triangle> &primitives,
const std::shared_ptr<PhoneMaterial> &phone_material)
: vertices_(vertices), primitives_(primitives), material_(phone_material) {}
2024-03-27 18:31:10 +08:00
std::vector<std::shared_ptr<Vertex>>::size_type Mesh::get_vertex_size() const {
return vertices_.size();
};
2024-03-27 18:31:10 +08:00
const decltype(Vertex::texture_coordinate) &
Mesh::get_texture_coordinate(const unsigned int index) const {
return (vertices_[index])->texture_coordinate;
}
Point3d Mesh::get_normal_vector(const unsigned int index) const {
return (vertices_[index])->normal;
}
2024-03-27 18:31:10 +08:00
const std::vector<Triangle> &Mesh::get_primitives() const {
return primitives_;
};
std::vector<Triangle> &Mesh::get_primitives() { return primitives_; };
std::shared_ptr<PhoneMaterial> &Mesh::get_material() { return material_; }
const std::shared_ptr<PhoneMaterial> &Mesh::get_material() const {
return material_;
}
std::vector<Mesh> Mesh::load_mesh(
const std::string &obj_path,
const std::unordered_map<Texture::TextureType, std::string> texture_path) {
const auto &obj_file_path = std::filesystem::path(obj_path);
const auto &material_directory = obj_file_path.parent_path();
2024-03-22 20:12:09 +08:00
objl::Loader loader{};
assert(loader.LoadFile(obj_path.c_str()));
std::vector<Mesh> res;
for (const auto &mesh : loader.LoadedMeshes) {
spdlog::info("Current mesh has {} vertices.", mesh.Vertices.size());
std::vector<std::shared_ptr<Vertex>> vertices;
2024-03-22 20:12:09 +08:00
for (decltype(mesh.Vertices)::size_type i = 0; i < mesh.Vertices.size();
i++) {
vertices.push_back(std::make_shared<Vertex>(
OBJL_VEC3_TO_EIGEN_VECTOR(mesh.Vertices[i].Position),
OBJL_VEC3_TO_EIGEN_VECTOR(mesh.Vertices[i].Normal),
OBJL_VEC2_TO_EIGEN_VECTOR(mesh.Vertices[i].TextureCoordinate)));
}
std::vector<Triangle> primitives;
2024-03-22 20:12:09 +08:00
for (decltype(mesh.Indices)::size_type i = 0; i < mesh.Indices.size();
i += 3) {
primitives.push_back(Triangle(vertices, mesh.Indices[i],
mesh.Indices[i + 1], mesh.Indices[i + 2]));
}
2024-03-27 18:31:10 +08:00
std::unordered_map<Texture::TextureType, std::shared_ptr<Texture>> textures;
if (!texture_path.empty()) {
2024-03-27 18:31:10 +08:00
for (const auto &[type, path] : texture_path) {
textures.insert({type, std::make_shared<Texture>(path)});
}
} else if (!mesh.MeshMaterial.map_Kd.empty()) {
2024-03-27 18:31:10 +08:00
textures.insert({Texture::DiffuseMap,
std::make_shared<Texture>(material_directory /
mesh.MeshMaterial.map_Kd)});
2024-03-22 20:12:09 +08:00
}
2024-03-27 18:31:10 +08:00
auto material = std::make_shared<PhoneMaterial>(
textures, std::make_shared<Lamp>(Point3d{}, 16.));
2024-03-22 20:12:09 +08:00
res.push_back(Mesh(vertices, primitives, material));
}
return res;
}