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 | //cout << "vtx: " << numVertices << endl;
|
---|
28 | for (int i = 0; i < numVertices; ++ i)
|
---|
29 | {
|
---|
30 | Vector3 v = vertices[i];
|
---|
31 | ostream << "v " << v.x << " " << v.y << " " << v.z << endl;
|
---|
32 | }
|
---|
33 |
|
---|
34 | return numVertices;
|
---|
35 | }
|
---|
36 |
|
---|
37 |
|
---|
38 | bool ObjExporter::Export(const string &filename, Bvh *bvh)
|
---|
39 | {
|
---|
40 | cout << "Exporting obj to file '" << filename.c_str() << "'" << endl;
|
---|
41 |
|
---|
42 | ofstream ostream(filename.c_str());
|
---|
43 |
|
---|
44 | if (!ostream.is_open())
|
---|
45 | {
|
---|
46 | cerr << "Error: Cannot open file " << filename.c_str() << " for writing" << endl;
|
---|
47 | return false;
|
---|
48 | }
|
---|
49 |
|
---|
50 | int numVertices = 0;
|
---|
51 |
|
---|
52 | BvhLeafContainer nodes;
|
---|
53 | bvh->CollectLeaves(bvh->GetStaticRoot(), nodes);
|
---|
54 |
|
---|
55 | cout << "writing " << nodes.size() << " objects ..." << endl;
|
---|
56 |
|
---|
57 | BvhLeafContainer::const_iterator it, it_end = nodes.end();
|
---|
58 |
|
---|
59 | int j = 0;
|
---|
60 | const int t = 1000;
|
---|
61 |
|
---|
62 | for (it = nodes.begin(); it != it_end; ++ it, ++ j)
|
---|
63 | {
|
---|
64 | if ((j % t) == (t - 1))
|
---|
65 | cout << j + 1 << endl;
|
---|
66 |
|
---|
67 | BvhNode *node = *it;
|
---|
68 |
|
---|
69 | int geometrySize;
|
---|
70 | SceneEntity **entities = bvh->GetGeometry(node, geometrySize);
|
---|
71 |
|
---|
72 | for (int i = 0; i < geometrySize; ++ i)
|
---|
73 | {
|
---|
74 | ShapeContainer::iterator sit, send;
|
---|
75 |
|
---|
76 | entities[i]->GetLODLevel(0, sit, send);
|
---|
77 |
|
---|
78 | for (; sit != send; ++ sit)
|
---|
79 | {
|
---|
80 | Geometry *geom = (*sit)->GetGeometry();
|
---|
81 | numVertices += WriteGeometry(geom, ostream);
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | cout << "finished" << endl;
|
---|
87 | cout << "writing " << numVertices << " indices ..." << endl;
|
---|
88 |
|
---|
89 | for (int i = 0; i < numVertices; i += 3)
|
---|
90 | {
|
---|
91 | ostream << "f " << i << " " << i + 1 << " " << i + 2 << endl;
|
---|
92 | }
|
---|
93 |
|
---|
94 | cout << "successfully exported obj" << endl;
|
---|
95 |
|
---|
96 | return true;
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | } |
---|