TDME2  1.9.200
WFObjWriter.cpp
Go to the documentation of this file.
2 
3 #include <array>
4 #include <string>
5 #include <vector>
6 
7 #include <tdme/tdme.h>
9 #include <tdme/math/Vector3.h>
13 
14 using std::array;
15 using std::string;
16 using std::to_string;
17 using std::vector;
18 
25 
26 WFObjWriter::WFObjWriter()
27 {
28 }
29 
30 void WFObjWriter::addVertex(const Vector3& vertex) {
31  vertices.push_back(vertex);
32 }
33 
34 void WFObjWriter::addFace(vector<int> faceVertexIndices) {
35  if (faceVertexIndices.size() < 3) throw ModelFileIOException("a face must at least exist of 3 vertices");
36  for (auto vertexIdx: faceVertexIndices) {
37  if (vertexIdx < 0 || vertexIdx >= vertices.size()) throw ModelFileIOException("face vertex index out of bounds");
38  }
39  for (auto i = 0; i < faceVertexIndices.size(); i++) {
40  for (auto j = i + 1; j < faceVertexIndices.size(); j++) {
41  if (faceVertexIndices[i] == faceVertexIndices[j]) throw ModelFileIOException("duplicate face vertex index");
42  }
43  }
44  array<Vector3, 3> minFaceVertices { vertices[faceVertexIndices[0]], vertices[faceVertexIndices[1]], vertices[faceVertexIndices[2]]};
45  auto faceNormal = ModelTools::computeNormal(minFaceVertices);
46  auto faceNormalIdx = 0;
47  for (auto i = 0; i < normals.size(); i++) {
48  if (faceNormal.equals(normals[i]) == true) break;
49  faceNormalIdx++;
50  }
51  if (faceNormalIdx == normals.size()) normals.push_back(faceNormal);
52  vector<array<int, 2>> face;
53  for (auto i = 0; i < faceVertexIndices.size(); i++) {
54  face.push_back({faceVertexIndices[i], faceNormalIdx});
55  }
56  faces.push_back(face);
57 }
58 
59 void WFObjWriter::write(const string& pathName, const string& fileName) {
60  {
61  vector<string> content;
62  content.push_back("mtllib " + fileName + ".mtl");
63  content.push_back("o TDME2_WFOBJ_EXPORT");
64  for (auto& vertex: vertices) {
65  content.push_back("v " + to_string(vertex.getX()) + " " + to_string(vertex.getY()) + " " + to_string(vertex.getZ()));
66  }
67  for (auto& normal: normals) {
68  content.push_back("vn " + to_string(normal.getX()) + " " + to_string(normal.getY()) + " " + to_string(normal.getZ()));
69  }
70  content.push_back("usemtl default");
71  for (auto& faceVertexIndices: faces) {
72  string faceString = "f ";
73  for (auto faceIndices: faceVertexIndices) {
74  faceString+= to_string(faceIndices[0] + 1) +"/" + to_string(faceIndices[1] + 1) + " ";
75  }
76  content.push_back(faceString);
77  }
78  FileSystem::getInstance()->setContentFromStringArray(pathName, fileName, content);
79  }
80  {
81  vector<string> content;
82  content.push_back("newmtl default");
83  content.push_back("Ka 0.2 0.2 0.2");
84  content.push_back("Kd 0.8 0.8 0.8");
85  FileSystem::getInstance()->setContentFromStringArray(pathName, fileName + ".mtl", content);
86  }
87 }
88 
Wavefront object model writer.
Definition: WFObjWriter.h:20
void write(const string &pathName, const string &fileName)
Writes this wave front object file.
Definition: WFObjWriter.cpp:59
void addVertex(const Vector3 &vertex)
Adds a vertex.
Definition: WFObjWriter.cpp:30
vector< vector< array< int, 2 > > > faces
Definition: WFObjWriter.h:24
void addFace(vector< int > faceVertexIndices)
Adds a face.
Definition: WFObjWriter.cpp:34
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
File system singleton class.
Definition: FileSystem.h:17
Model tools functions class.
Definition: ModelTools.h:42