need to do the idea thingy tired though
parent
fb29f1f513
commit
1019d4aefb
|
@ -20,12 +20,16 @@
|
|||
#define BLT_WITH_GRAPHICS_OBJ_LOADER_H
|
||||
|
||||
#include "blt/math/vectors.h"
|
||||
#include "blt/std/hashmap.h"
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <string_view>
|
||||
|
||||
namespace blt::gfx
|
||||
{
|
||||
|
||||
typedef blt::vec4 vertex_t;
|
||||
typedef blt::vec3 uv_t;
|
||||
typedef blt::vec3 vertex_t;
|
||||
typedef blt::vec2 uv_t;
|
||||
typedef blt::vec3 normal_t;
|
||||
|
||||
struct face_t
|
||||
|
@ -33,6 +37,68 @@ namespace blt::gfx
|
|||
std::int32_t vertex, uv, normal;
|
||||
};
|
||||
|
||||
struct constructed_vertex_t
|
||||
{
|
||||
vertex_t vertex;
|
||||
uv_t uv;
|
||||
normal_t normal;
|
||||
};
|
||||
|
||||
struct triangle_t
|
||||
{
|
||||
std::int32_t v1, v2, v3;
|
||||
};
|
||||
|
||||
class obj_loader;
|
||||
|
||||
class obj_object_t
|
||||
{
|
||||
friend obj_loader;
|
||||
private:
|
||||
// TODO: shared VBO?
|
||||
std::string object_name;
|
||||
std::vector<constructed_vertex_t> vertices_;
|
||||
std::vector<std::int32_t> indices_;
|
||||
|
||||
obj_object_t(std::string_view name, std::vector<constructed_vertex_t> vertices, std::vector<std::int32_t> indices):
|
||||
object_name(name), vertices_(std::move(vertices)), indices_(std::move(indices))
|
||||
{}
|
||||
|
||||
public:
|
||||
inline const std::vector<constructed_vertex_t>& vertices()
|
||||
{
|
||||
return vertices_;
|
||||
};
|
||||
|
||||
inline const std::vector<std::int32_t>& indices()
|
||||
{
|
||||
return indices_;
|
||||
};
|
||||
};
|
||||
|
||||
class obj_loader
|
||||
{
|
||||
private:
|
||||
std::vector<vertex_t> vertices;
|
||||
std::vector<uv_t> uvs;
|
||||
std::vector<normal_t> normals;
|
||||
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;
|
||||
};
|
||||
|
||||
private:
|
||||
void parse_face(std::string_view line);
|
||||
|
||||
public:
|
||||
void parseFile(std::string_view file);
|
||||
};
|
||||
|
||||
std::vector<obj_object_t> quick_load(std::string_view file);
|
||||
|
||||
}
|
||||
|
||||
#endif //BLT_WITH_GRAPHICS_OBJ_LOADER_H
|
||||
|
|
|
@ -16,3 +16,78 @@
|
|||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <blt/gfx/loader/obj_loader.h>
|
||||
#include <blt/std/loader.h>
|
||||
#include <blt/std/string.h>
|
||||
#include <blt/std/logging.h>
|
||||
|
||||
namespace blt::gfx
|
||||
{
|
||||
class char_tokenizer
|
||||
{
|
||||
private:
|
||||
std::string_view string;
|
||||
std::size_t current_pos = 0;
|
||||
public:
|
||||
explicit char_tokenizer(std::string_view view): string(view)
|
||||
{}
|
||||
|
||||
char peek()
|
||||
{
|
||||
return string[current_pos];
|
||||
}
|
||||
|
||||
char advance()
|
||||
{
|
||||
return string[current_pos++];
|
||||
}
|
||||
|
||||
bool has_next(size_t offset = 0)
|
||||
{
|
||||
return current_pos + offset < string.size();
|
||||
}
|
||||
};
|
||||
|
||||
std::vector<obj_object_t> quick_load(std::string_view file)
|
||||
{
|
||||
std::vector<obj_object_t> objects;
|
||||
|
||||
|
||||
return objects;
|
||||
}
|
||||
|
||||
void obj_loader::parseFile(std::string_view file)
|
||||
{
|
||||
auto lines = blt::fs::getLinesFromFile(std::string(file));
|
||||
for (const auto& line : lines)
|
||||
{
|
||||
char_tokenizer token(line);
|
||||
// line is empty?
|
||||
if (!token.has_next())
|
||||
continue;
|
||||
switch (token.advance())
|
||||
{
|
||||
case '#':
|
||||
continue;
|
||||
case 'f':
|
||||
{
|
||||
|
||||
break;
|
||||
}
|
||||
case 'v':
|
||||
{
|
||||
|
||||
break;
|
||||
}
|
||||
case 'o':
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void obj_loader::parse_face(std::string_view line)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue