-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
61 lines (49 loc) · 1.69 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <DIFBuilder/DIFBuilder.hpp>
#include <tiny_obj_loader.h>
int main(int argc, const char **argv) {
//Read everything we can
tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string err;
tinyobj::LoadObj(&attrib, &shapes, &materials, &err, argv[1]);
//Default material
materials.push_back(tinyobj::material_t());
for (const tinyobj::shape_t shape : shapes) {
DIF::DIFBuilder builder;
int vertStart = 0;
for (int i = 0; i < shape.mesh.num_face_vertices.size(); i ++) {
tinyobj::index_t idx[3] = {
shape.mesh.indices[vertStart + 2],
shape.mesh.indices[vertStart + 1],
shape.mesh.indices[vertStart + 0]
};
DIF::DIFBuilder::Triangle triangle;
for (int j = 0; j < 3; j ++) {
triangle.points[j].vertex = glm::vec3(
attrib.vertices[(idx[j].vertex_index * 3) + 0],
attrib.vertices[(idx[j].vertex_index * 3) + 1],
attrib.vertices[(idx[j].vertex_index * 3) + 2]
);
triangle.points[j].uv = glm::vec2(
attrib.texcoords[(idx[j].texcoord_index * 2) + 0],
-attrib.texcoords[(idx[j].texcoord_index * 2) + 1]
);
triangle.points[j].normal = glm::vec3(
attrib.normals[(idx[j].normal_index * 3) + 0],
attrib.normals[(idx[j].normal_index * 3) + 1],
attrib.normals[(idx[j].normal_index * 3) + 2]
);
}
int material = shape.mesh.material_ids[i];
builder.addTriangle(triangle, (material == -1 ? shape.name : materials[material].diffuse_texname));
vertStart += 3;
}
DIF::DIF dif;
builder.build(dif);
std::ofstream outStr(std::string(argv[2]) + shape.name + ".dif");
dif.write(outStr, DIF::Version());
}
return 0;
}