The small3d library
WavefrontFile.hpp
Go to the documentation of this file.
1 
10 #pragma once
11 #include <vector>
12 #include "File.hpp"
13 #include "BoundingBoxSet.hpp"
14 #include <unordered_map>
15 #include "Material.hpp"
16 
17 namespace small3d {
18 
28  class WavefrontFile : public File {
29 
30  private:
31  std::string materialFile = "";
32  Material material;
33 
34  int getTokens(const std::string& input, const char sep,
35  std::vector<std::string>& tokens);
36 
37  // Does this file only contain triangles?
38  bool onlyTriangles = true;
39 
40  // Data read from .obj file
41  std::vector<std::vector<float> > vertices;
42  std::vector<std::vector<uint32_t> > facesVertexIndices;
43  std::vector<std::vector<float> > normals;
44  std::vector<std::vector<uint32_t> > facesNormalIndices;
45  std::vector<std::vector<float> > textureCoords;
46  std::vector<std::vector<uint32_t> > textureCoordsIndices;
47  std::vector<std::string> objectNames;
48  std::unordered_map<std::string, size_t> objectStartFaceIdx;
49 
50  void loadVertexData(std::vector<float>& vertexData);
51  void loadIndexData(std::vector<uint16_t>& indexData);
52  void loadNormalsData(std::vector<float>& normalsData, const std::vector<float>& vertexData);
53  void loadTextureCoordsData(std::vector<float>& textureCoordsData, const std::vector<float>& vertexData);
54 
55  void loadMaterial(const std::string& filePath, const std::string& name);
56 
57  // Make sure that no texture coordinate information is lost when the data
58  // buffers get created (vertexData, indexData, normalsData and
59  // textureCoordsData) by realigning the data vectors, in order to ensure
60  // unique vertex - texture coordinates pairs.
61  void correctDataVectors();
62 
63  WavefrontFile(); // No default constructor
64 
65  // Forbid moving and copying
66  WavefrontFile(WavefrontFile const&) = delete;
67  void operator=(WavefrontFile const&) = delete;
68  WavefrontFile(WavefrontFile&&) = delete;
69  void operator=(WavefrontFile&&) = delete;
70 
71  public:
76  explicit WavefrontFile(const std::string& filePath);
77 
83  void load(Model& model, const std::string& meshName) override;
84 
90  std::vector<std::string> getMeshNames() override;
91 
92  };
93 }
small3d::Model
A 3D model. It can be loaded from a WavefrontFile or GlbFile. It can also be constructed procedurally...
Definition: Model.hpp:34
small3d::WavefrontFile
.obj (Wavefront) file parser class. The file format has to be somewhat specific, with triangulated fa...
Definition: WavefrontFile.hpp:28
BoundingBoxSet.hpp
Bounding boxes for collision detection.
File.hpp
File parser virtual class.
small3d
Definition: BinaryFile.hpp:15
small3d::WavefrontFile::getMeshNames
std::vector< std::string > getMeshNames() override
Get a list of the names of the meshes contained in the file.
Definition: WavefrontFile.cpp:493
small3d::File
Abstract file parser class.
Definition: File.hpp:23
small3d::WavefrontFile::load
void load(Model &model, const std::string &meshName) override
Load data from the Wavefront file into a Model.
Definition: WavefrontFile.cpp:432
small3d::Material
A 3D model material.
Definition: Material.hpp:23
Material.hpp
Material structure.