line parse
parent
00649a055d
commit
1817c50b4b
|
@ -85,23 +85,26 @@ namespace blt::gfx
|
|||
};
|
||||
};
|
||||
|
||||
class char_tokenizer;
|
||||
|
||||
class obj_loader
|
||||
{
|
||||
private:
|
||||
std::vector<vertex_t> vertices;
|
||||
std::vector<uv_t> uvs;
|
||||
std::vector<normal_t> normals;
|
||||
|
||||
// maps between vertex indices -> face (constructed vertex)
|
||||
HASHMAP<std::int32_t, constructed_vertex_t> vertex_data;
|
||||
struct object_data
|
||||
{
|
||||
std::string object_name;
|
||||
// maps between vertex indices -> face (constructed vertex)
|
||||
HASHMAP<std::int32_t, constructed_vertex_t> vertex_data;
|
||||
std::vector<std::int32_t> indices;
|
||||
};
|
||||
|
||||
std::vector<object_data> data;
|
||||
private:
|
||||
void parse_extra_line(char_tokenizer& tokenizer);
|
||||
void parse_face(std::string_view line);
|
||||
|
||||
public:
|
||||
void parseFile(std::string_view file);
|
||||
};
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit 903bac9fc10e48005d68a0f9e9d060de538e898c
|
||||
Subproject commit 9147a85dc32f06be2a4cfe4e422fdbc52679adc5
|
|
@ -19,6 +19,8 @@
|
|||
#include <blt/std/loader.h>
|
||||
#include <blt/std/string.h>
|
||||
#include <blt/std/logging.h>
|
||||
#include <cctype>
|
||||
#include <charconv>
|
||||
|
||||
namespace blt::gfx
|
||||
{
|
||||
|
@ -31,22 +33,94 @@ namespace blt::gfx
|
|||
explicit char_tokenizer(std::string_view view): string(view)
|
||||
{}
|
||||
|
||||
char peek()
|
||||
inline char peek()
|
||||
{
|
||||
return string[current_pos];
|
||||
}
|
||||
|
||||
char advance()
|
||||
inline char advance()
|
||||
{
|
||||
return string[current_pos++];
|
||||
}
|
||||
|
||||
bool has_next(size_t offset = 0)
|
||||
inline bool has_next(size_t offset = 0)
|
||||
{
|
||||
return current_pos + offset < string.size();
|
||||
}
|
||||
|
||||
inline std::string_view read_fully()
|
||||
{
|
||||
return blt::string::trim(string.substr(current_pos));
|
||||
}
|
||||
};
|
||||
|
||||
float get(std::string_view str)
|
||||
{
|
||||
float x;
|
||||
const auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), x);
|
||||
// probably not needed.
|
||||
if (ec != std::errc())
|
||||
{
|
||||
// int i;
|
||||
// const auto [ptr2, ec2] = std::from_chars(str.data(), str.data() + str.size(), i);
|
||||
// if (ec2 == std::errc())
|
||||
// {
|
||||
// x = static_cast<float>(i);
|
||||
// } else
|
||||
// {
|
||||
BLT_WARN("Unable to parse string '%s' into number!", std::string(str).c_str());
|
||||
x = 0;
|
||||
// }
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
void obj_loader::parse_extra_line(char_tokenizer& tokenizer)
|
||||
{
|
||||
char type = tokenizer.advance();
|
||||
auto elements = blt::string::split(std::string(tokenizer.read_fully()), " ");
|
||||
if (elements.size() < 2)
|
||||
{
|
||||
BLT_ERROR("Unable to parse line '%s' too few arguments to type '%c'", std::string(tokenizer.read_fully()).c_str(), type);
|
||||
return;
|
||||
}
|
||||
float x = get(elements[0]), y = get(elements[1]);
|
||||
if (std::isspace(type))
|
||||
{
|
||||
if (elements.size() < 3)
|
||||
{
|
||||
BLT_ERROR("Unable to parse line '%s' too few arguments to type '%c'", std::string(tokenizer.read_fully()).c_str(), type);
|
||||
return;
|
||||
}
|
||||
float z = get(elements[2]);
|
||||
vertices.push_back(vertex_t{x, y, z});
|
||||
return;
|
||||
}
|
||||
switch (type)
|
||||
{
|
||||
case 't':
|
||||
uvs.push_back(uv_t{x,y});
|
||||
break;
|
||||
case 'n':
|
||||
{
|
||||
if (elements.size() < 3)
|
||||
{
|
||||
BLT_ERROR("Unable to parse line '%s' too few arguments to type '%c'", std::string(tokenizer.read_fully()).c_str(), type);
|
||||
return;
|
||||
}
|
||||
float z = get(elements[2]);
|
||||
normals.push_back(vertex_t{x, y, z});
|
||||
break;
|
||||
}
|
||||
case 'p':
|
||||
// optional todo
|
||||
break;
|
||||
default:
|
||||
BLT_WARN("Unexpected type '%c'", type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<obj_object_t> quick_load(std::string_view file)
|
||||
{
|
||||
std::vector<obj_object_t> objects;
|
||||
|
@ -61,8 +135,7 @@ namespace blt::gfx
|
|||
for (const auto& line : lines)
|
||||
{
|
||||
char_tokenizer token(line);
|
||||
// line is empty?
|
||||
if (!token.has_next())
|
||||
if (!token.has_next() || token.read_fully().empty())
|
||||
continue;
|
||||
switch (token.advance())
|
||||
{
|
||||
|
@ -75,7 +148,7 @@ namespace blt::gfx
|
|||
}
|
||||
case 'v':
|
||||
{
|
||||
|
||||
parse_extra_line(token);
|
||||
break;
|
||||
}
|
||||
case 'o':
|
||||
|
|
Loading…
Reference in New Issue