1 | #include "ObjExporter.h"
|
---|
2 | #include "SceneEntity.h"
|
---|
3 | #include "Shape.h"
|
---|
4 | #include "Geometry.h"
|
---|
5 | #include "Transform3.h"
|
---|
6 |
|
---|
7 |
|
---|
8 | using namespace std;
|
---|
9 |
|
---|
10 | // MAGIC of all bin exports
|
---|
11 | #ifndef MAGIC
|
---|
12 | #define MAGIC 0x827923
|
---|
13 | #endif
|
---|
14 |
|
---|
15 | #define BVH_VERSION 2.1
|
---|
16 |
|
---|
17 | namespace CHCDemoEngine
|
---|
18 | {
|
---|
19 |
|
---|
20 | static int currentIdx = 0;
|
---|
21 |
|
---|
22 |
|
---|
23 | int ObjExporter::WriteGeometry(Geometry *geom, ofstream &ostream, Transform3 *trafo)
|
---|
24 | {
|
---|
25 | int numVertices;
|
---|
26 | Vector3 *vertices = geom->GetVertices(numVertices);
|
---|
27 |
|
---|
28 | char str[100];
|
---|
29 |
|
---|
30 | //cout << "vtx: " << numVertices << endl;
|
---|
31 | for (int i = 0; i < numVertices; ++ i)
|
---|
32 | {
|
---|
33 | Vector3 v = trafo->GetMatrix() * vertices[i];
|
---|
34 |
|
---|
35 | sprintf(str, "v %f %f %f\n", v.x, v.z, -v.y);
|
---|
36 | ostream << str;
|
---|
37 | }
|
---|
38 |
|
---|
39 | return numVertices;
|
---|
40 | }
|
---|
41 |
|
---|
42 |
|
---|
43 | bool ObjExporter::Export(const string &filename, Bvh *bvh)
|
---|
44 | {
|
---|
45 | cout << "Exporting obj to file '" << filename.c_str() << "'" << endl;
|
---|
46 |
|
---|
47 | ofstream ostream(filename.c_str());
|
---|
48 |
|
---|
49 | if (!ostream.is_open())
|
---|
50 | {
|
---|
51 | cerr << "Error: Cannot open file " << filename.c_str() << " for writing" << endl;
|
---|
52 | return false;
|
---|
53 | }
|
---|
54 |
|
---|
55 | int numVertices = 0;
|
---|
56 |
|
---|
57 | BvhNodeContainer nodes;
|
---|
58 | bvh->CollectVirtualLeaves(bvh->GetStaticRoot(), nodes);
|
---|
59 |
|
---|
60 | cout << "writing " << nodes.size() << " objects ..." << endl;
|
---|
61 |
|
---|
62 | BvhNodeContainer::const_iterator it, it_end = nodes.end();
|
---|
63 |
|
---|
64 | int j = 0;
|
---|
65 | const int t = 1000;
|
---|
66 |
|
---|
67 | for (it = nodes.begin(); it != it_end; ++ it, ++ j)
|
---|
68 | {
|
---|
69 | if ((j % t) == (t - 1))
|
---|
70 | cout << j + 1 << endl;
|
---|
71 |
|
---|
72 | BvhNode *node = *it;
|
---|
73 |
|
---|
74 | int geometrySize;
|
---|
75 | SceneEntity **entities = bvh->GetGeometry(node, geometrySize);
|
---|
76 |
|
---|
77 | for (int i = 0; i < geometrySize; ++ i)
|
---|
78 | {
|
---|
79 | Transform3 *trafo = entities[i]->GetTransform();
|
---|
80 |
|
---|
81 | ShapeContainer::iterator sit, send;
|
---|
82 | entities[i]->GetLODLevel(0, sit, send);
|
---|
83 |
|
---|
84 | for (; sit != send; ++ sit)
|
---|
85 | {
|
---|
86 | Geometry *geom = (*sit)->GetGeometry();
|
---|
87 |
|
---|
88 | int dummy;
|
---|
89 | if (!geom->GetVertices(dummy))
|
---|
90 | {
|
---|
91 | cerr << "error: geometry was deleted on loading, exporting obj failed" << endl;
|
---|
92 | return false;
|
---|
93 | }
|
---|
94 |
|
---|
95 | numVertices += WriteGeometry(geom, ostream, trafo);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | cout << "finished" << endl;
|
---|
101 | cout << "writing " << numVertices << " indices ..." << endl;
|
---|
102 |
|
---|
103 | for (int i = 0; i < numVertices; i += 3)
|
---|
104 | {
|
---|
105 | ostream << "f " << i + 1 << " " << i + 2 << " " << i + 3 << endl;
|
---|
106 | }
|
---|
107 |
|
---|
108 | cout << "successfully exported obj" << endl;
|
---|
109 |
|
---|
110 | return true;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | } |
---|