Commit c24d4f02 authored by Georg Schaefer's avatar Georg Schaefer
Browse files

add basic mesh baking

parent d8a9d879
#ifndef __MESH_CONVERTER_HPP__
#define __MESH_CONVERTER_HPP__
#include <string>
#include <vector>
#include <glm/glm.hpp>
namespace gdw {
struct header {
const uint32_t magic = 0x4D574447;
uint64_t vertex_count = 0;
uint64_t index_count = 0;
uint64_t submesh_count = 0;
};
struct vertex {
glm::vec3 position;
glm::vec2 texcoord;
glm::vec3 normal;
glm::vec3 tangent;
};
struct submesh {
uint64_t index_count = 0;
uint64_t offset = 0;
glm::vec3 color = glm::vec3(0.f);
uint64_t diffuse = 0;
uint64_t normal = 0;
uint64_t material = 0;
};
struct mesh {
std::vector<vertex> vertices;
std::vector<int> indices;
std::vector<submesh> submeshes;
};
}
namespace gdw {
class mesh_converter {
private:
std::string filename_;
std::string output_dir_;
public:
mesh_converter(const std::string& filename, const std::string& output_dir);
~mesh_converter() = default;
mesh_converter(const mesh_converter&) = delete;
mesh_converter& operator=(const mesh_converter&) = delete;
mesh_converter(mesh_converter&&) = default;
mesh_converter& operator=(mesh_converter&&) = default;
void run();
};
}
#endif
#include <stdexcept>
#include <string>
#include <mesh_converter.hpp>
int main(int argc, char* argv[]) {
if (argc < 3) {
......@@ -7,5 +10,8 @@ int main(int argc, char* argv[]) {
throw std::runtime_error("too many arguments provided");
}
gdw::mesh_converter mc(argv[1], argv[2]);
mc.run();
return 0;
}
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <unordered_map>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <mesh_converter.hpp>
namespace gdw {
mesh_converter::mesh_converter(const std::string& filename, const std::string& output_dir)
: filename_(filename), output_dir_(output_dir) {}
void mesh_converter::run() {
Assimp::Importer importer;
auto scene = importer.ReadFile(filename_, aiProcessPreset_TargetRealtime_Fast);
if (!scene) {
throw std::runtime_error("could not load file " + filename_);
}
std::unordered_map<std::string, mesh> meshes;
for (auto i = 0; i < scene->mNumMeshes; ++i) {
auto mesh = scene->mMeshes[i];
auto name = std::string(mesh->mName.C_Str());
auto& vertices = meshes[name].vertices;
auto& indices = meshes[name].indices;
submesh s;
s.offset = indices.size();
for (auto j = 0; j < mesh->mNumFaces; ++j) {
auto& face = mesh->mFaces[j];
for (auto k = 0; k < 3; ++k) {
auto position = mesh->mVertices[face.mIndices[k]];
auto texcoord = mesh->HasTextureCoords(0) ? mesh->mTextureCoords[0][face.mIndices[k]] : aiVector3D(0.f);
auto normal = mesh->mNormals[face.mIndices[k]];
auto tangent = mesh->HasTangentsAndBitangents() ? mesh->mTangents[face.mIndices[k]] : aiVector3D(0.f);
auto vertex = gdw::vertex {
glm::vec3(position.x, position.y, position.z),
glm::vec2(texcoord.x, texcoord.y),
glm::vec3(normal.x, normal.y, normal.z),
glm::vec3(tangent.x, tangent.y, tangent.z)
};
vertices.emplace_back(vertex);
indices.emplace_back(face.mIndices[k]);
}
}
auto material = scene->mMaterials[mesh->mMaterialIndex];
auto& submeshes = meshes[name].submeshes;
s.index_count = indices.size() - s.offset;
aiColor3D color(0.f,0.f,0.f);
material->Get(AI_MATKEY_COLOR_DIFFUSE, color);
s.color = glm::vec3(color.r, color.g, color.b);
}
for (auto mesh : meshes) {
header h;
h.vertex_count = mesh.second.vertices.size();
h.index_count = mesh.second.indices.size();
h.submesh_count = mesh.second.submeshes.size();
auto path = output_dir_ + "mesh/" + mesh.first + ".msh";
std::ofstream of(path, std::ios_base::binary | std::ios_base::trunc);
if (!of) {
throw std::runtime_error("could not open file for writing " + path);
}
of.write(reinterpret_cast<char*>(&h), sizeof(header));
of.write(reinterpret_cast<char*>(mesh.second.vertices.data()), h.vertex_count * sizeof(vertex));
of.write(reinterpret_cast<char*>(mesh.second.indices.data()), h.index_count * sizeof(int));
of.write(reinterpret_cast<char*>(mesh.second.submeshes.data()), h.submesh_count * sizeof(submesh));
of.close();
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment