26 WFObjWriter::WFObjWriter()
35 if (faceVertexIndices.size() < 3)
throw ModelFileIOException(
"a face must at least exist of 3 vertices");
36 for (
auto vertexIdx: faceVertexIndices) {
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");
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;
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});
56 faces.push_back(face);
61 vector<string> content;
62 content.push_back(
"mtllib " + fileName +
".mtl");
63 content.push_back(
"o TDME2_WFOBJ_EXPORT");
65 content.push_back(
"v " + to_string(vertex.getX()) +
" " + to_string(vertex.getY()) +
" " + to_string(vertex.getZ()));
68 content.push_back(
"vn " + to_string(normal.getX()) +
" " + to_string(normal.getY()) +
" " + to_string(normal.getZ()));
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) +
" ";
76 content.push_back(faceString);
78 FileSystem::getInstance()->setContentFromStringArray(pathName, fileName, content);
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);
Wavefront object model writer.
void write(const string &pathName, const string &fileName)
Writes this wave front object file.
void addVertex(const Vector3 &vertex)
Adds a vertex.
vector< vector< array< int, 2 > > > faces
vector< Vector3 > normals
vector< Vector3 > vertices
void addFace(vector< int > faceVertexIndices)
Adds a face.
Vector3 class representing vector3 mathematical structure and operations with x, y,...
File system singleton class.