Commits (3)
......@@ -55,6 +55,7 @@ namespace gdw {
mesh_converter& operator=(mesh_converter&&) = default;
void run();
void write_aabb(const std::vector<vertex>& vertices, const std::string& name);
};
}
......
#include <fstream>
#include <limits>
#include <iostream>
#include <stdexcept>
#include <unordered_map>
......@@ -7,6 +8,8 @@
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <glm/gtx/string_cast.hpp>
#include <mesh_converter.hpp>
namespace gdw {
......@@ -28,7 +31,7 @@ namespace gdw {
}
std::unordered_map<std::string, unsigned int> material_db;
std::ifstream mdb_in("material.db", std::ios_base::trunc | std::ios_base::out);
std::ifstream mdb_in("material.db");
if (!mdb_in) {
throw std::runtime_error("could not open material.db");
}
......@@ -53,9 +56,14 @@ namespace gdw {
auto id = std::atoi(value.c_str());
material_db.emplace(std::make_pair(key, id));
++line_number;
}
mdb_in.close();
for (auto& entry : material_db) {
std::cout << entry.first << " : " << entry.second << std::endl;
}
auto it = material_db.find("next_id");
if (it == material_db.end()) {
material_db["next_id"] = 1;
......@@ -254,6 +262,8 @@ namespace gdw {
}
of.close();
write_aabb(mesh.second.vertices, mesh.first);
}
std::ofstream mdb_out("material.db", std::ios_base::trunc);
......@@ -269,4 +279,53 @@ namespace gdw {
mdb_out.close();
}
void mesh_converter::write_aabb(const std::vector<vertex>& vertices, const std::string& name) {
auto min_x = std::numeric_limits<float>::max();
auto min_y = std::numeric_limits<float>::max();
auto min_z = std::numeric_limits<float>::max();
auto max_x = std::numeric_limits<float>::lowest();
auto max_y = std::numeric_limits<float>::lowest();
auto max_z = std::numeric_limits<float>::lowest();
for (auto& v : vertices) {
if (v.position.x < min_x) {
min_x = v.position.x;
}
if (v.position.x > max_x) {
max_x = v.position.x;
}
if (v.position.y < min_y) {
min_y = v.position.y;
}
if (v.position.y > max_y) {
max_y = v.position.y;
}
if (v.position.z < min_z) {
min_z = v.position.z;
}
if (v.position.z > max_z) {
max_z = v.position.z;
}
}
const static int magic = 0x43574447;
auto min = glm::vec3(min_x, min_y, min_z);
auto max = glm::vec3(max_x, max_y, max_z);
auto half_extend = (min - max) / 2.f;
auto center = max + half_extend;
half_extend = glm::abs(half_extend);
auto col_out_path = output_dir_ + "physic/" + name + ".col";
std::ofstream col_out(col_out_path, std::ios_base::trunc | std::ios_base::binary);
if (!col_out) {
throw std::runtime_error("could not open " + col_out_path);
}
col_out.write(reinterpret_cast<const char*>(&magic), sizeof(int));
col_out.write(reinterpret_cast<char*>(&center), sizeof(glm::vec3));
col_out.write(reinterpret_cast<char*>(&half_extend), sizeof(glm::vec3));
col_out.close();
}
}