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