1 | #include "PlyConverter.h"
|
---|
2 | #include "SimpleTri.h"
|
---|
3 | #include "SimpleVec.h"
|
---|
4 | #include "gzstream.h"
|
---|
5 | #include <iostream>
|
---|
6 | #include "rply.h"
|
---|
7 |
|
---|
8 |
|
---|
9 | using namespace std;
|
---|
10 |
|
---|
11 | static int sGlobalIndex = 0;
|
---|
12 |
|
---|
13 |
|
---|
14 | VertexArray sVertices;
|
---|
15 | VertexArray sNormals;
|
---|
16 |
|
---|
17 |
|
---|
18 | PlyConverter::~PlyConverter()
|
---|
19 | {
|
---|
20 | for (size_t i = 0; i < mGeometry.size(); ++ i) delete mGeometry[i];
|
---|
21 | mGeometry.clear();
|
---|
22 | }
|
---|
23 |
|
---|
24 |
|
---|
25 | void PlyConverter::LoadShape(const VertexArray &faceVertices,
|
---|
26 | const VertexArray &faceNormals,
|
---|
27 | const vector<Texcoord> &faceTexcoords
|
---|
28 | )
|
---|
29 | {
|
---|
30 | int numElements = (int)faceVertices.size();
|
---|
31 | Geometry *geom = new Geometry();
|
---|
32 | /*
|
---|
33 | // convert the triangles to geometry
|
---|
34 | geom->mVertices = new SimpleVec[numElements];
|
---|
35 | geom->mNormals = new SimpleVec[numElements];
|
---|
36 | geom->mTexcoords = new Texcoord[numElements];
|
---|
37 |
|
---|
38 | geom->mVertexCount = numElements;
|
---|
39 | geom->mTexcoordCount = (int)faceTexcoords.size();
|
---|
40 |
|
---|
41 | cout << "creating new geometry with " << numElements << " vertices" << endl;
|
---|
42 |
|
---|
43 | for (int i = 0; i < numElements; ++ i)
|
---|
44 | {
|
---|
45 | geom->mVertices[i] = faceVertices[i]
|
---|
46 | geom->mNormals[i] = faceNormals[i];
|
---|
47 |
|
---|
48 | /*if (i < geom->mTexcoordCount)
|
---|
49 | {
|
---|
50 | geom->mTexcoords[i].first = faceTexcoords[i].first;
|
---|
51 | geom->mTexcoords[i].second = faceTexcoords[i].second;
|
---|
52 | }
|
---|
53 | }
|
---|
54 | */
|
---|
55 | mGeometry.push_back(geom);
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | bool PlyConverter::Convert(const string &filename,
|
---|
60 | const std::string &outputFilename)
|
---|
61 | {
|
---|
62 | mNumShapes = 0;
|
---|
63 |
|
---|
64 | for (size_t i = 0; i < mGeometry.size(); ++ i) delete mGeometry[i];
|
---|
65 | mGeometry.clear();
|
---|
66 |
|
---|
67 | if (!ReadFile(filename))
|
---|
68 | {
|
---|
69 | cerr << "could not read file" << endl;
|
---|
70 | return false;
|
---|
71 | }
|
---|
72 |
|
---|
73 | if (!WriteFile(outputFilename))
|
---|
74 | {
|
---|
75 | cerr << "could not write file" << endl;
|
---|
76 | return false;
|
---|
77 | }
|
---|
78 |
|
---|
79 | return true;
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | static int vertex_cb(p_ply_argument argument)
|
---|
84 | {
|
---|
85 | long eol;
|
---|
86 | ply_get_argument_user_data(argument, NULL, &eol);
|
---|
87 |
|
---|
88 | static int mainIndex = 0;
|
---|
89 | static int i = 0;
|
---|
90 |
|
---|
91 | static SimpleVec v;
|
---|
92 |
|
---|
93 | v[i] = (float)ply_get_argument_value(argument);
|
---|
94 | //printf("here3 %g", ply_get_argument_value(argument));
|
---|
95 |
|
---|
96 | if (eol) sVertices.push_back(v);
|
---|
97 | i = (i + 1) % 3;
|
---|
98 | //else printf(" ");
|
---|
99 |
|
---|
100 | return 1;
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | static int face_cb(p_ply_argument argument)
|
---|
105 | {
|
---|
106 | long length, value_index;
|
---|
107 | ply_get_argument_property(argument, NULL, &length, &value_index);
|
---|
108 |
|
---|
109 | switch (value_index)
|
---|
110 | {
|
---|
111 | case 0:
|
---|
112 | case 1:
|
---|
113 | printf("here4 %g ", ply_get_argument_value(argument));
|
---|
114 | break;
|
---|
115 | case 2:
|
---|
116 | printf("here5 %g\n", ply_get_argument_value(argument));
|
---|
117 | break;
|
---|
118 | default:
|
---|
119 | break;
|
---|
120 | }
|
---|
121 | return 1;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 |
|
---|
126 | bool PlyConverter::ReadFile(const string &filename)
|
---|
127 | {
|
---|
128 | long nvertices, ntriangles;
|
---|
129 |
|
---|
130 | p_ply ply = ply_open(filename.c_str(), NULL);
|
---|
131 |
|
---|
132 | if (!ply) return 1;
|
---|
133 | if (!ply_read_header(ply)) return 1;
|
---|
134 |
|
---|
135 | nvertices = ply_set_read_cb(ply, "vertex", "x", vertex_cb, NULL, 0);
|
---|
136 |
|
---|
137 | ply_set_read_cb(ply, "vertex", "y", vertex_cb, NULL, 0);
|
---|
138 | ply_set_read_cb(ply, "vertex", "z", vertex_cb, NULL, 1);
|
---|
139 |
|
---|
140 | ntriangles = ply_set_read_cb(ply, "face", "vertex_indices", face_cb, NULL, 0);
|
---|
141 |
|
---|
142 | printf("%ld\n%ld\n", nvertices, ntriangles);
|
---|
143 |
|
---|
144 | if (!ply_read(ply)) return 1;
|
---|
145 | ply_close(ply);
|
---|
146 |
|
---|
147 | return 0;
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 |
|
---|
152 | void PlyConverter::WriteGeometry(ogzstream &str, Geometry *geom)
|
---|
153 | {
|
---|
154 | int vertexCount = (int)geom->mVertices.size();
|
---|
155 | str.write(reinterpret_cast<char *>(&vertexCount), sizeof(int));
|
---|
156 |
|
---|
157 | SimpleVec hvertices[3];
|
---|
158 | SimpleVec hnormals[3];
|
---|
159 |
|
---|
160 | for (int i = 0; i < 3; ++ i)
|
---|
161 | {
|
---|
162 | hvertices[i] = geom->mVertices[i];
|
---|
163 | hnormals[i] = geom->mNormals[i];
|
---|
164 | }
|
---|
165 |
|
---|
166 | int texCoordCount = 0;
|
---|
167 |
|
---|
168 | str.write(reinterpret_cast<char *>(hvertices), sizeof(SimpleVec) * vertexCount);
|
---|
169 | str.write(reinterpret_cast<char *>(hnormals), sizeof(SimpleVec) * vertexCount);
|
---|
170 | str.write(reinterpret_cast<char *>(&texCoordCount), sizeof(int));
|
---|
171 |
|
---|
172 | //if (texCoordCount) str.write(reinterpret_cast<char *>(geom->mTexcoords), sizeof(float) * texCoordCount * 2);
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | bool PlyConverter::WriteFile(const string &filename)
|
---|
177 | {
|
---|
178 | ogzstream ofile(filename.c_str());
|
---|
179 |
|
---|
180 | if (!ofile.is_open()) return false;
|
---|
181 |
|
---|
182 |
|
---|
183 | /////////
|
---|
184 | //-- write textures
|
---|
185 |
|
---|
186 | #ifdef USE_TEXTURE
|
---|
187 | int textureCount = 1;
|
---|
188 | #else
|
---|
189 | int textureCount = 0;
|
---|
190 | #endif
|
---|
191 | ofile.write(reinterpret_cast<char *>(&textureCount), sizeof(int));
|
---|
192 |
|
---|
193 | if (textureCount > 0)
|
---|
194 | {
|
---|
195 | // hack
|
---|
196 | const string texName("wood.jpg");
|
---|
197 |
|
---|
198 | int texnameSize = (int)texName.length() + 1;
|
---|
199 | ofile.write(reinterpret_cast<char *>(&texnameSize), sizeof(int));
|
---|
200 |
|
---|
201 | ofile.write(texName.c_str(), sizeof(char) * texnameSize);
|
---|
202 |
|
---|
203 | int boundS = 1, boundT = 1;
|
---|
204 |
|
---|
205 | ofile.write(reinterpret_cast<char *>(&boundS), sizeof(int));
|
---|
206 | ofile.write(reinterpret_cast<char *>(&boundT), sizeof(int));
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | ///////////
|
---|
211 | //-- write shapes
|
---|
212 |
|
---|
213 | ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
|
---|
214 |
|
---|
215 | vector<Geometry *>::const_iterator it, it_end = mGeometry.end();
|
---|
216 |
|
---|
217 | for (it = mGeometry.begin(); it != it_end; ++ it)
|
---|
218 | {
|
---|
219 | WriteGeometry(ofile, *it);
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | int entityCount = 1;
|
---|
224 | ofile.write(reinterpret_cast<char *>(&entityCount), sizeof(int));
|
---|
225 |
|
---|
226 |
|
---|
227 | //////////
|
---|
228 | //-- write single scene entity
|
---|
229 |
|
---|
230 | // no transformation
|
---|
231 | bool hasTrafo = false;
|
---|
232 | ofile.write(reinterpret_cast<char *>(&hasTrafo), sizeof(bool));
|
---|
233 |
|
---|
234 | // a dummy lod
|
---|
235 | int numLODs = 1;
|
---|
236 | ofile.write(reinterpret_cast<char *>(&numLODs), sizeof(int));
|
---|
237 |
|
---|
238 | float dist = 0;
|
---|
239 | ofile.write(reinterpret_cast<char *>(&dist), sizeof(float));
|
---|
240 |
|
---|
241 | ofile.write(reinterpret_cast<char *>(&mNumShapes), sizeof(int));
|
---|
242 |
|
---|
243 | // all shapes belong to this scene entity
|
---|
244 | for (int i = 0; i < mNumShapes; ++ i)
|
---|
245 | {
|
---|
246 | int shapeId = i;
|
---|
247 | ofile.write(reinterpret_cast<char *>(&shapeId), sizeof(int));
|
---|
248 | }
|
---|
249 |
|
---|
250 | return true;
|
---|
251 | }
|
---|