handle quad .objs

main
Brett 2024-01-09 21:42:26 -05:00
parent 25fd136e21
commit c1f42b2d3b
4 changed files with 54 additions and 30 deletions

View File

@ -80,6 +80,11 @@ namespace blt::gfx
std::int32_t v[3]; std::int32_t v[3];
}; };
struct quad_t
{
std::int32_t v[4];
};
struct object_data struct object_data
{ {
std::string object_name; std::string object_name;
@ -128,6 +133,8 @@ namespace blt::gfx
void parse_face(char_tokenizer& tokenizer); void parse_face(char_tokenizer& tokenizer);
void handle_face_vertex(const std::vector<std::string>& face_list, std::int32_t* arr);
public: public:
obj_objects_t parseFile(std::string_view file); obj_objects_t parseFile(std::string_view file);
}; };

@ -1 +1 @@
Subproject commit 240ab5890b2e8da294937a1710b021ac3f271472 Subproject commit a1b06823fe2d964a62fda99385499b218cf5cea5

@ -1 +1 @@
Subproject commit cfab14287405a0d34f6a0fec1336f46415728fcf Subproject commit 6675317107257c2cc16c947b359d557821d85bf2

View File

@ -172,11 +172,34 @@ namespace blt::gfx
void obj_loader::parse_face(char_tokenizer& tokenizer) void obj_loader::parse_face(char_tokenizer& tokenizer)
{ {
auto vertexes = blt::string::split(std::string(tokenizer.read_fully()), ' '); auto faces = blt::string::split(std::string(tokenizer.read_fully()), ' ');
if (vertexes.size() == 3) if (faces.size() == 3)
{ {
triangle_t triangle{}; triangle_t triangle{};
for (const auto& pair : blt::enumerate(vertexes)) handle_face_vertex(faces, triangle.v);
current_object.indices.push_back(triangle);
} else if (faces.size() == 4)
{
quad_t quad{};
handle_face_vertex(faces, quad.v);
triangle_t t1{};
triangle_t t2{};
for (int i = 0; i < 3; i++)
t1.v[i] = quad.v[i];
t2.v[0] = quad.v[0];
t2.v[1] = quad.v[2];
t2.v[2] = quad.v[3];
current_object.indices.push_back(t1);
current_object.indices.push_back(t2);
} else
BLT_WARN("Unsupported vertex count! %d", faces.size());
}
void obj_loader::handle_face_vertex(const std::vector<std::string>& face_list, int32_t* arr)
{
for (const auto& pair : blt::enumerate(face_list))
{ {
auto indices = blt::string::split(pair.second, '/'); auto indices = blt::string::split(pair.second, '/');
BLT_ASSERT(indices.size() == 3 && "Must have vertex, uv, and normal indices!!"); BLT_ASSERT(indices.size() == 3 && "Must have vertex, uv, and normal indices!!");
@ -193,17 +216,11 @@ namespace blt::gfx
auto index = static_cast<std::int32_t>(vertex_data.size()); auto index = static_cast<std::int32_t>(vertex_data.size());
vertex_data.push_back({vertices[vi], uvs[ui], normals[ni]}); vertex_data.push_back({vertices[vi], uvs[ui], normals[ni]});
vertex_map.insert({face, index}); vertex_map.insert({face, index});
triangle.v[pair.first] = index; arr[pair.first] = index;
} else } else
{ {
triangle.v[pair.first] = loc->second; arr[pair.first] = loc->second;
} }
} }
current_object.indices.push_back(triangle);
} else if (vertexes.size() == 4)
{
BLT_WARN("Currently unable to process non-triangulated meshes!");
} else
BLT_WARN("Unsupported vertex count! %d", vertexes.size());
} }
} }