1 | #include <iostream>
|
---|
2 | using namespace std;
|
---|
3 | #include <ply.h>
|
---|
4 |
|
---|
5 | #include "PlyParser.h"
|
---|
6 |
|
---|
7 | #include "SceneGraph.h"
|
---|
8 | #include "Mesh.h"
|
---|
9 |
|
---|
10 |
|
---|
11 |
|
---|
12 |
|
---|
13 |
|
---|
14 | /* user's vertex and face definitions for a polygonal object */
|
---|
15 |
|
---|
16 | typedef struct Vertex {
|
---|
17 | int id;
|
---|
18 | float x,y,z;
|
---|
19 | void *other_props; /* other properties */
|
---|
20 | } Vertex;
|
---|
21 |
|
---|
22 | typedef struct PlyFace {
|
---|
23 | int id;
|
---|
24 | unsigned char nverts; /* number of vertex indices in list */
|
---|
25 | int *verts; /* vertex index list */
|
---|
26 | void *other_props; /* other properties */
|
---|
27 | } PlyFace;
|
---|
28 |
|
---|
29 |
|
---|
30 |
|
---|
31 | char *elem_names[] = { /* list of the kinds of elements in the user's object */
|
---|
32 | "vertex", "face"
|
---|
33 | };
|
---|
34 |
|
---|
35 | PlyProperty vert_props[] = { /* list of property information for a vertex */
|
---|
36 | {"x", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,x), 0, 0, 0, 0},
|
---|
37 | {"y", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,y), 0, 0, 0, 0},
|
---|
38 | {"z", PLY_FLOAT, PLY_FLOAT, offsetof(Vertex,z), 0, 0, 0, 0}
|
---|
39 | };
|
---|
40 |
|
---|
41 | PlyProperty face_props[] = { /* list of property information for a face */
|
---|
42 | {"vertex_indices", PLY_INT, PLY_INT, offsetof(PlyFace,verts),
|
---|
43 | 1, PLY_UCHAR, PLY_UCHAR, offsetof(PlyFace,nverts)}
|
---|
44 | };
|
---|
45 |
|
---|
46 | unsigned char has_fverts;
|
---|
47 | static PlyFace **flist;
|
---|
48 |
|
---|
49 | bool
|
---|
50 | PlyParser::ParseFile(const string filename,
|
---|
51 | SceneGraphNode **root,
|
---|
52 | const bool loadPolygonsAsMeshes)
|
---|
53 | {
|
---|
54 |
|
---|
55 | /* user's vertex and face definitions for a polygonal object */
|
---|
56 |
|
---|
57 |
|
---|
58 |
|
---|
59 | typedef double Matrix[4][4];
|
---|
60 |
|
---|
61 | /*** the PLY object ***/
|
---|
62 |
|
---|
63 | int nverts;
|
---|
64 | Vertex **vlist;
|
---|
65 | PlyOtherElems *other_elements = NULL;
|
---|
66 | PlyOtherProp *vert_other;
|
---|
67 | int nelems;
|
---|
68 | char **elist;
|
---|
69 | int num_comments;
|
---|
70 | char **comments;
|
---|
71 | int num_obj_info;
|
---|
72 | char **obj_info;
|
---|
73 | int file_type;
|
---|
74 |
|
---|
75 | int i,j;
|
---|
76 | PlyFile *ply;
|
---|
77 | int nprops;
|
---|
78 | int num_elems;
|
---|
79 | PlyProperty **plist;
|
---|
80 | char *elem_name;
|
---|
81 | float version;
|
---|
82 |
|
---|
83 |
|
---|
84 | /*** Read in the original PLY object ***/
|
---|
85 | FILE *file = fopen(filename.c_str(), "rb");
|
---|
86 |
|
---|
87 | ply = ply_read (file, &nelems, &elist);
|
---|
88 | ply_get_info (ply, &version, &file_type);
|
---|
89 |
|
---|
90 | int nfaces;
|
---|
91 |
|
---|
92 | for (i = 0; i < nelems; i++) {
|
---|
93 |
|
---|
94 | /* get the description of the first element */
|
---|
95 | elem_name = elist[i];
|
---|
96 | plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops);
|
---|
97 |
|
---|
98 | if (equal_strings ("vertex", elem_name)) {
|
---|
99 |
|
---|
100 | /* create a vertex list to hold all the vertices */
|
---|
101 | vlist = (Vertex **) malloc (sizeof (Vertex *) * num_elems);
|
---|
102 | nverts = num_elems;
|
---|
103 |
|
---|
104 | /* set up for getting vertex elements */
|
---|
105 |
|
---|
106 | ply_get_property (ply, elem_name, &vert_props[0]);
|
---|
107 | ply_get_property (ply, elem_name, &vert_props[1]);
|
---|
108 | ply_get_property (ply, elem_name, &vert_props[2]);
|
---|
109 | vert_other = ply_get_other_properties (ply, elem_name,
|
---|
110 | offsetof(Vertex,other_props));
|
---|
111 |
|
---|
112 | /* grab all the vertex elements */
|
---|
113 | for (j = 0; j < num_elems; j++) {
|
---|
114 | vlist[j] = (Vertex *) malloc (sizeof (Vertex));
|
---|
115 | ply_get_element (ply, (void *) vlist[j]);
|
---|
116 | vlist[j]->id = j;
|
---|
117 | // cout<<"("<<vlist[j]->x<<","<<vlist[j]->y<<","<<vlist[j]->z<<")"<<endl;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | else
|
---|
121 | if (equal_strings ("face", elem_name)) {
|
---|
122 | /* create a list to hold all the face elements */
|
---|
123 | ALLOCN(flist, PlyFace *, num_elems);
|
---|
124 | nfaces = num_elems;
|
---|
125 | cerr<<"num faces="<<nfaces<<endl;
|
---|
126 |
|
---|
127 | /* set up for getting face elements */
|
---|
128 | has_fverts = false;
|
---|
129 |
|
---|
130 | for (j=0; j<nprops; j++)
|
---|
131 | {
|
---|
132 | if (equal_strings("vertex_indices", plist[j]->name))
|
---|
133 | {
|
---|
134 | ply_get_property (ply, elem_name, &face_props[0]);
|
---|
135 | has_fverts = true;
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | // face_other = ply_get_other_properties (ply, elem_name,
|
---|
140 | // offsetof(Face,other_props));
|
---|
141 |
|
---|
142 | /* test for necessary properties */
|
---|
143 | if (!has_fverts) {
|
---|
144 | fprintf(stderr,"Faces must have vertex indices\n");
|
---|
145 | exit(-1);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /* grab all the face elements */
|
---|
149 | for (j = 0; j < num_elems; j++) {
|
---|
150 | ALLOCN(flist[j], PlyFace, 1);
|
---|
151 | ply_get_element (ply, (void *) flist[j]);
|
---|
152 | flist[j]->id = j;
|
---|
153 |
|
---|
154 | // for (int k = 0; k < flist[j]->nverts; k++)
|
---|
155 | // cout<<flist[j]->verts[k]<<" ";
|
---|
156 | // cout<<endl;
|
---|
157 | }
|
---|
158 | } else
|
---|
159 | other_elements = ply_get_other_element (ply, elem_name, num_elems);
|
---|
160 |
|
---|
161 |
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 |
|
---|
166 | comments = ply_get_comments (ply, &num_comments);
|
---|
167 | obj_info = ply_get_obj_info (ply, &num_obj_info);
|
---|
168 |
|
---|
169 | ply_close (ply);
|
---|
170 |
|
---|
171 |
|
---|
172 |
|
---|
173 |
|
---|
174 | *root = new SceneGraphNode;
|
---|
175 | SceneGraphNode *currentNode = *root;
|
---|
176 |
|
---|
177 |
|
---|
178 | cerr<<"Baking faces into eshes"<<endl;
|
---|
179 | // bake the faces into meshes
|
---|
180 | Mesh *mesh = NULL;
|
---|
181 | int facesPerMesh = 100;
|
---|
182 | for (i = 0; i < nfaces; i++) {
|
---|
183 | if (i % facesPerMesh == 0) {
|
---|
184 | if (mesh) {
|
---|
185 | mesh->Preprocess;
|
---|
186 | // make an instance of this mesh
|
---|
187 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
188 | currentNode->mGeometry.push_back(mi);
|
---|
189 | }
|
---|
190 | mesh = new Mesh();
|
---|
191 | }
|
---|
192 |
|
---|
193 | // only one face per mesh
|
---|
194 | VertexIndexContainer vc;
|
---|
195 |
|
---|
196 | // add vertices
|
---|
197 | for (int k = 0; k < flist[i]->nverts; k++) {
|
---|
198 | Vertex *v = vlist[flist[i]->verts[k]];
|
---|
199 | vc.push_back(mesh->mVertices.size());
|
---|
200 | mesh->mVertices.push_back(Vector3(v->x, v->y, v->z));
|
---|
201 | }
|
---|
202 |
|
---|
203 | mesh->mFaces.push_back(new Face(vc));
|
---|
204 | }
|
---|
205 |
|
---|
206 | for (i=0; i < nverts; i++)
|
---|
207 | free(vlist[i]);
|
---|
208 | free(vlist);
|
---|
209 |
|
---|
210 | for (i=0; i < nfaces; i++)
|
---|
211 | free(flist[i]);
|
---|
212 | free(flist);
|
---|
213 |
|
---|
214 |
|
---|
215 | return true;
|
---|
216 | }
|
---|