ModelViewer 0.1
Template for CPP projects
Loading...
Searching...
No Matches
ObjLoader.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** Obj.hpp
4** File description:
5** Obj.hpp
6*/
7
8#ifndef MODELVIEWER_OBJ_LOADER_HPP
9#define MODELVIEWER_OBJ_LOADER_HPP
10
11#include <filesystem>
12#include <fstream>
13#include <iostream>
14#include <memory>
15
18#include "Loaders/ILoader.hpp"
19#include "Material/Material.hpp"
20
21namespace model_viewer::loaders {
22
26class ObjLoader final : public ILoader {
27 private:
29 std::vector<geometry::Vector3> _vertices;
30
32 std::vector<geometry::TextureCoordinate> _textureCoords;
33
35 std::vector<geometry::Vector3> _normals;
36
38 std::vector<geometry::FaceIndex> _faces;
39
41 std::string _modelPath;
42
44 std::unordered_map<std::string, std::function<void(const std::string &)>>
46
48 std::unique_ptr<geometry::ObjGeometry> _geometry;
49
51 std::unique_ptr<material::MTLMaterial> _material;
52
53 public:
55 ObjLoader();
56
58 ~ObjLoader() override = default;
59
66 bool loadModel(const std::string &filepath) override;
67
72 QQuick3DGeometry *geometry() const override { return _geometry.get(); };
73
78 material::MTLMaterial *material() const override {
79 return _material.get();
80 };
81
82 const std::vector<geometry::Vector3> &getVertices() const {
83 return _vertices;
84 }
85
86 const std::vector<geometry::TextureCoordinate> &getTextureCoords() const {
87 return _textureCoords;
88 }
89
90 const std::vector<geometry::Vector3> &getNormals() const {
91 return _normals;
92 }
93
94 const std::vector<geometry::FaceIndex> &getFaces() const { return _faces; }
95
96 const std::string &getModelPath() const { return _modelPath; }
97
98 private:
100 void reset();
101
107 void parseModel(std::ifstream &file);
108
110 void setModelData();
111
113 void resetObj();
114
119 void parseVertice(const std::string &line);
120
125 void parseNormal(const std::string &line);
126
131 void parseTextureCoordinate(const std::string &line);
132
137 void parseFace(const std::string &line);
138
144 geometry::FaceIndex parseFaceIndex(const std::string &token);
145
151 void retrieveMaterial(const std::string &line);
152};
153
154} // namespace model_viewer::loaders
155
156#endif // MODELVIEWER_OBJ_LOADER_HPP
Interface for model loaders.
Definition ILoader.hpp:20
Class for loading OBJ models.
Definition ObjLoader.hpp:26
~ObjLoader() override=default
Destructor.
std::unique_ptr< material::MTLMaterial > _material
Material of the model.
Definition ObjLoader.hpp:51
void parseVertice(const std::string &line)
Parse a vertice line from the OBJ file.
Definition ObjLoader.cpp:87
void parseModel(std::ifstream &file)
Parse an OBJ file line by line.
Definition ObjLoader.cpp:45
geometry::FaceIndex parseFaceIndex(const std::string &token)
Parse a face index token from the OBJ file.
Definition ObjLoader.cpp:123
const std::string & getModelPath() const
Definition ObjLoader.hpp:96
const std::vector< geometry::Vector3 > & getVertices() const
Definition ObjLoader.hpp:82
std::vector< geometry::TextureCoordinate > _textureCoords
Texture coordinates of the model.
Definition ObjLoader.hpp:32
std::vector< geometry::Vector3 > _vertices
Vertices of the model.
Definition ObjLoader.hpp:29
const std::vector< geometry::TextureCoordinate > & getTextureCoords() const
Definition ObjLoader.hpp:86
void setModelData()
Set the model data to the geometry.
Definition ObjLoader.cpp:56
const std::vector< geometry::FaceIndex > & getFaces() const
Definition ObjLoader.hpp:94
void reset()
Reset the OBJ loader state.
Definition ObjLoader.cpp:40
void parseFace(const std::string &line)
Parse a face line from the OBJ file.
Definition ObjLoader.cpp:111
QQuick3DGeometry * geometry() const override
Get the geometry of the loaded model.
Definition ObjLoader.hpp:72
material::MTLMaterial * material() const override
Get the material of the loaded model.
Definition ObjLoader.hpp:78
bool loadModel(const std::string &filepath) override
Load an OBJ model from a file.
Definition ObjLoader.cpp:26
ObjLoader()
Constructor.
Definition ObjLoader.cpp:14
std::vector< geometry::FaceIndex > _faces
Faces of the model.
Definition ObjLoader.hpp:38
std::vector< geometry::Vector3 > _normals
Normals of the model.
Definition ObjLoader.hpp:35
std::unique_ptr< geometry::ObjGeometry > _geometry
Geometry of the model.
Definition ObjLoader.hpp:48
void parseNormal(const std::string &line)
Parse a normal vertice line from the OBJ file.
Definition ObjLoader.cpp:95
std::unordered_map< std::string, std::function< void(const std::string &)> > _parseFunctions
Map of parsing functions for different OBJ line prefixes.
Definition ObjLoader.hpp:45
void retrieveMaterial(const std::string &line)
Retrieve material from a material library line and call material's parsing method.
Definition ObjLoader.cpp:140
void resetObj()
Reset the OBJ model state.
Definition ObjLoader.cpp:80
std::string _modelPath
Path to the model file.
Definition ObjLoader.hpp:41
void parseTextureCoordinate(const std::string &line)
Parse a texture coordinate vertice line from the OBJ file.
Definition ObjLoader.cpp:103
const std::vector< geometry::Vector3 > & getNormals() const
Definition ObjLoader.hpp:90
Class representing a material parsed from an MTL file.
Definition Material.hpp:29
Definition ObjLoader.cpp:12
Struct representing a face's indexes in an OBJ model.
Definition GeometryStructs.hpp:28