|
| 1 | +#include "SimpleObjLoader.h" |
| 2 | +#include "framework/mesh.h" |
| 3 | + |
| 4 | +#include <fstream> |
| 5 | +#include <sstream> |
| 6 | +#include <cstdio> |
| 7 | +#include <unordered_map> |
| 8 | +#include <cstring> |
| 9 | + |
| 10 | +#define Msg(...) printf(__VA_ARGS__), printf("\n") |
| 11 | + |
| 12 | +namespace xray { |
| 13 | +namespace animation { |
| 14 | +namespace tools { |
| 15 | + |
| 16 | +// Hash function for vertex deduplication |
| 17 | +struct VertexKey { |
| 18 | + int v_idx, vt_idx, vn_idx; |
| 19 | + |
| 20 | + bool operator==(const VertexKey& other) const { |
| 21 | + return v_idx == other.v_idx && vt_idx == other.vt_idx && vn_idx == other.vn_idx; |
| 22 | + } |
| 23 | +}; |
| 24 | + |
| 25 | +} // namespace tools |
| 26 | +} // namespace animation |
| 27 | +} // namespace xray |
| 28 | + |
| 29 | +namespace std { |
| 30 | +template <> |
| 31 | +struct hash<xray::animation::tools::VertexKey> { |
| 32 | + size_t operator()(const xray::animation::tools::VertexKey& k) const { |
| 33 | + return ((hash<int>()(k.v_idx) ^ (hash<int>()(k.vt_idx) << 1)) >> 1) ^ (hash<int>()(k.vn_idx) << 1); |
| 34 | + } |
| 35 | +}; |
| 36 | +} |
| 37 | + |
| 38 | +namespace xray { |
| 39 | +namespace animation { |
| 40 | +namespace tools { |
| 41 | + |
| 42 | +bool SimpleObjLoader::LoadObjFile(const std::string& file_path, ozz::sample::Mesh& out_mesh) { |
| 43 | + std::ifstream file(file_path); |
| 44 | + if (!file.is_open()) { |
| 45 | + Msg("! Failed to open OBJ file: %s", file_path.c_str()); |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + // Temporary storage for OBJ data (1-indexed, so we add dummy at index 0) |
| 50 | + std::vector<float> positions; // x,y,z |
| 51 | + std::vector<float> texcoords; // u,v |
| 52 | + std::vector<float> normals; // x,y,z |
| 53 | + |
| 54 | + // Add dummy elements at index 0 |
| 55 | + positions.push_back(0); positions.push_back(0); positions.push_back(0); |
| 56 | + texcoords.push_back(0); texcoords.push_back(0); |
| 57 | + normals.push_back(0); normals.push_back(0); normals.push_back(0); |
| 58 | + |
| 59 | + // Final mesh data |
| 60 | + std::vector<float> mesh_positions; |
| 61 | + std::vector<float> mesh_normals; |
| 62 | + std::vector<float> mesh_uvs; |
| 63 | + std::vector<uint32_t> mesh_indices; |
| 64 | + |
| 65 | + // Vertex deduplication |
| 66 | + std::unordered_map<VertexKey, uint32_t> vertex_map; |
| 67 | + |
| 68 | + std::string line; |
| 69 | + int line_number = 0; |
| 70 | + |
| 71 | + while (std::getline(file, line)) { |
| 72 | + line_number++; |
| 73 | + |
| 74 | + if (line.empty() || line[0] == '#') { |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + std::istringstream iss(line); |
| 79 | + std::string prefix; |
| 80 | + iss >> prefix; |
| 81 | + |
| 82 | + if (prefix == "v") { |
| 83 | + // Vertex position |
| 84 | + float x, y, z; |
| 85 | + iss >> x >> y >> z; |
| 86 | + positions.push_back(x); |
| 87 | + positions.push_back(y); |
| 88 | + positions.push_back(z); |
| 89 | + } |
| 90 | + else if (prefix == "vt") { |
| 91 | + // Texture coordinate |
| 92 | + float u, v; |
| 93 | + iss >> u >> v; |
| 94 | + texcoords.push_back(u); |
| 95 | + texcoords.push_back(v); |
| 96 | + } |
| 97 | + else if (prefix == "vn") { |
| 98 | + // Normal |
| 99 | + float nx, ny, nz; |
| 100 | + iss >> nx >> ny >> nz; |
| 101 | + normals.push_back(nx); |
| 102 | + normals.push_back(ny); |
| 103 | + normals.push_back(nz); |
| 104 | + } |
| 105 | + else if (prefix == "f") { |
| 106 | + // Face (triangle) |
| 107 | + std::vector<VertexKey> face_vertices; |
| 108 | + std::string vertex_str; |
| 109 | + |
| 110 | + while (iss >> vertex_str) { |
| 111 | + VertexKey key{}; |
| 112 | + |
| 113 | + // Parse vertex format: v/vt/vn or v//vn or v/vt or v |
| 114 | + int v_idx = 0, vt_idx = 0, vn_idx = 0; |
| 115 | + |
| 116 | + size_t pos1 = vertex_str.find('/'); |
| 117 | + if (pos1 == std::string::npos) { |
| 118 | + // Format: v |
| 119 | + v_idx = std::stoi(vertex_str); |
| 120 | + } else { |
| 121 | + v_idx = std::stoi(vertex_str.substr(0, pos1)); |
| 122 | + |
| 123 | + size_t pos2 = vertex_str.find('/', pos1 + 1); |
| 124 | + if (pos2 == std::string::npos) { |
| 125 | + // Format: v/vt |
| 126 | + vt_idx = std::stoi(vertex_str.substr(pos1 + 1)); |
| 127 | + } else { |
| 128 | + // Format: v/vt/vn or v//vn |
| 129 | + if (pos2 > pos1 + 1) { |
| 130 | + vt_idx = std::stoi(vertex_str.substr(pos1 + 1, pos2 - pos1 - 1)); |
| 131 | + } |
| 132 | + vn_idx = std::stoi(vertex_str.substr(pos2 + 1)); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + // Convert negative indices to positive |
| 137 | + if (v_idx < 0) v_idx = static_cast<int>(positions.size() / 3) + v_idx; |
| 138 | + if (vt_idx < 0) vt_idx = static_cast<int>(texcoords.size() / 2) + vt_idx; |
| 139 | + if (vn_idx < 0) vn_idx = static_cast<int>(normals.size() / 3) + vn_idx; |
| 140 | + |
| 141 | + key.v_idx = v_idx; |
| 142 | + key.vt_idx = vt_idx; |
| 143 | + key.vn_idx = vn_idx; |
| 144 | + |
| 145 | + face_vertices.push_back(key); |
| 146 | + } |
| 147 | + |
| 148 | + // Triangulate if needed (fan triangulation for n>3) |
| 149 | + for (size_t i = 1; i + 1 < face_vertices.size(); ++i) { |
| 150 | + const size_t triangle_indices[3] = {0, i, i + 1}; |
| 151 | + for (size_t idx = 0; idx < 3; ++idx) { |
| 152 | + const size_t j = triangle_indices[idx]; |
| 153 | + const VertexKey& key = face_vertices[j]; |
| 154 | + |
| 155 | + auto it = vertex_map.find(key); |
| 156 | + if (it != vertex_map.end()) { |
| 157 | + // Reuse existing vertex |
| 158 | + mesh_indices.push_back(it->second); |
| 159 | + } else { |
| 160 | + // Add new vertex |
| 161 | + uint32_t new_index = static_cast<uint32_t>(mesh_positions.size() / 3); |
| 162 | + vertex_map[key] = new_index; |
| 163 | + mesh_indices.push_back(new_index); |
| 164 | + |
| 165 | + // Add position |
| 166 | + if (key.v_idx > 0 && key.v_idx * 3 < static_cast<int>(positions.size())) { |
| 167 | + mesh_positions.push_back(positions[key.v_idx * 3 + 0]); |
| 168 | + mesh_positions.push_back(positions[key.v_idx * 3 + 1]); |
| 169 | + mesh_positions.push_back(positions[key.v_idx * 3 + 2]); |
| 170 | + } else { |
| 171 | + mesh_positions.push_back(0); mesh_positions.push_back(0); mesh_positions.push_back(0); |
| 172 | + } |
| 173 | + |
| 174 | + // Add UV |
| 175 | + if (key.vt_idx > 0 && key.vt_idx * 2 < static_cast<int>(texcoords.size())) { |
| 176 | + mesh_uvs.push_back(texcoords[key.vt_idx * 2 + 0]); |
| 177 | + mesh_uvs.push_back(texcoords[key.vt_idx * 2 + 1]); |
| 178 | + } else { |
| 179 | + mesh_uvs.push_back(0); mesh_uvs.push_back(0); |
| 180 | + } |
| 181 | + |
| 182 | + // Add normal |
| 183 | + if (key.vn_idx > 0 && key.vn_idx * 3 < static_cast<int>(normals.size())) { |
| 184 | + mesh_normals.push_back(normals[key.vn_idx * 3 + 0]); |
| 185 | + mesh_normals.push_back(normals[key.vn_idx * 3 + 1]); |
| 186 | + mesh_normals.push_back(normals[key.vn_idx * 3 + 2]); |
| 187 | + } else { |
| 188 | + mesh_normals.push_back(0); mesh_normals.push_back(0); mesh_normals.push_back(1); |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + file.close(); |
| 197 | + |
| 198 | + if (mesh_positions.empty() || mesh_indices.empty()) { |
| 199 | + Msg("! OBJ file contained no geometry: %s", file_path.c_str()); |
| 200 | + return false; |
| 201 | + } |
| 202 | + |
| 203 | + // Convert uint32_t indices to uint16_t for ozz |
| 204 | + out_mesh.triangle_indices.clear(); |
| 205 | + out_mesh.triangle_indices.reserve(mesh_indices.size()); |
| 206 | + for (uint32_t idx : mesh_indices) { |
| 207 | + if (idx > 65535) { |
| 208 | + Msg("! Warning: OBJ has too many vertices for uint16_t indices"); |
| 209 | + } |
| 210 | + out_mesh.triangle_indices.push_back(static_cast<uint16_t>(idx)); |
| 211 | + } |
| 212 | + |
| 213 | + // Create a single part with all vertices (no skinning) |
| 214 | + out_mesh.parts.resize(1); |
| 215 | + ozz::sample::Mesh::Part& part = out_mesh.parts[0]; |
| 216 | + |
| 217 | + part.positions.assign(mesh_positions.begin(), mesh_positions.end()); |
| 218 | + part.normals.assign(mesh_normals.begin(), mesh_normals.end()); |
| 219 | + part.uvs.assign(mesh_uvs.begin(), mesh_uvs.end()); |
| 220 | + |
| 221 | + // No skinning data |
| 222 | + out_mesh.joint_remaps.clear(); |
| 223 | + out_mesh.inverse_bind_poses.clear(); |
| 224 | + |
| 225 | + Msg("* Loaded OBJ: %zu vertices, %zu triangles", |
| 226 | + mesh_positions.size() / 3, mesh_indices.size() / 3); |
| 227 | + |
| 228 | + return true; |
| 229 | +} |
| 230 | + |
| 231 | +} // namespace tools |
| 232 | +} // namespace animation |
| 233 | +} // namespace xray |
0 commit comments