2024-01-02 17:10:29 -05:00
|
|
|
/*
|
|
|
|
* <Short Description>
|
|
|
|
* Copyright (C) 2024 Brett Terpstra
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef BLT_WITH_GRAPHICS_OBJ_LOADER_H
|
|
|
|
#define BLT_WITH_GRAPHICS_OBJ_LOADER_H
|
|
|
|
|
|
|
|
#include "blt/math/vectors.h"
|
2024-01-03 23:26:02 -05:00
|
|
|
#include "blt/std/hashmap.h"
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
#include <string_view>
|
2024-01-02 17:10:29 -05:00
|
|
|
|
|
|
|
namespace blt::gfx
|
|
|
|
{
|
|
|
|
|
2024-01-08 21:12:31 -05:00
|
|
|
typedef blt::vec3f vertex_t;
|
|
|
|
typedef blt::vec2f uv_t;
|
|
|
|
typedef blt::vec3f normal_t;
|
|
|
|
|
|
|
|
class model_data
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
private:
|
|
|
|
std::vector<vertex_t> vertices;
|
|
|
|
std::vector<uv_t> uvs;
|
|
|
|
std::vector<normal_t> normals;
|
|
|
|
};
|
2024-01-02 17:10:29 -05:00
|
|
|
|
|
|
|
struct face_t
|
|
|
|
{
|
|
|
|
std::int32_t vertex, uv, normal;
|
|
|
|
};
|
|
|
|
|
2024-01-03 23:26:02 -05:00
|
|
|
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_;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-01-09 00:50:12 -05:00
|
|
|
class char_tokenizer;
|
|
|
|
|
2024-01-03 23:26:02 -05:00
|
|
|
class obj_loader
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::vector<vertex_t> vertices;
|
|
|
|
std::vector<uv_t> uvs;
|
|
|
|
std::vector<normal_t> normals;
|
2024-01-09 00:50:12 -05:00
|
|
|
|
|
|
|
// maps between vertex indices -> face (constructed vertex)
|
|
|
|
HASHMAP<std::int32_t, constructed_vertex_t> vertex_data;
|
2024-01-03 23:26:02 -05:00
|
|
|
struct object_data
|
|
|
|
{
|
|
|
|
std::string object_name;
|
|
|
|
std::vector<std::int32_t> indices;
|
|
|
|
};
|
2024-01-09 00:50:12 -05:00
|
|
|
std::vector<object_data> data;
|
2024-01-03 23:26:02 -05:00
|
|
|
private:
|
2024-01-09 12:01:23 -05:00
|
|
|
bool handle_vertex_and_normals(float x, float y, float z, char type);
|
2024-01-09 00:50:12 -05:00
|
|
|
void parse_extra_line(char_tokenizer& tokenizer);
|
2024-01-09 12:01:23 -05:00
|
|
|
void parse_face(char_tokenizer& tokenizer);
|
2024-01-03 23:26:02 -05:00
|
|
|
public:
|
|
|
|
void parseFile(std::string_view file);
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<obj_object_t> quick_load(std::string_view file);
|
|
|
|
|
2024-01-02 17:10:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif //BLT_WITH_GRAPHICS_OBJ_LOADER_H
|