337 |
jab |
1 |
/*
|
|
|
2 |
* ply_load.cpp
|
|
|
3 |
* GEL
|
|
|
4 |
*
|
|
|
5 |
* Created by J. Andreas Bærentzen on 08/08/07.
|
|
|
6 |
* Copyright 2007 __MyCompanyName__. All rights reserved.
|
|
|
7 |
*
|
|
|
8 |
*/
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
#include <stdio.h>
|
|
|
12 |
#include <math.h>
|
|
|
13 |
#include <strings.h>
|
|
|
14 |
#include "ply_load.h"
|
341 |
jab |
15 |
#include <CGLA/Vec4f.h>
|
337 |
jab |
16 |
|
|
|
17 |
using namespace std;
|
|
|
18 |
using namespace Geometry;
|
|
|
19 |
using namespace CGLA;
|
|
|
20 |
|
|
|
21 |
typedef struct Vertex {
|
|
|
22 |
float x,y,z;
|
|
|
23 |
float r,g,b;
|
|
|
24 |
float nx,ny,nz;
|
|
|
25 |
void *other_props; /* other properties */
|
|
|
26 |
} Vertex;
|
|
|
27 |
|
|
|
28 |
typedef struct Face {
|
|
|
29 |
unsigned char nverts; /* number of vertex indices in list */
|
|
|
30 |
int *verts; /* vertex index list */
|
|
|
31 |
void *other_props; /* other properties */
|
|
|
32 |
} Face;
|
|
|
33 |
|
|
|
34 |
char *elem_names[] = { /* list of the elements in the object */
|
|
|
35 |
"vertex", "face"
|
|
|
36 |
};
|
|
|
37 |
|
|
|
38 |
PlyProperty vert_props[] = { /* list of property information for a vertex */
|
|
|
39 |
{"x", Float32, Float32, offsetof(Vertex,x), 0, 0, 0, 0},
|
|
|
40 |
{"y", Float32, Float32, offsetof(Vertex,y), 0, 0, 0, 0},
|
|
|
41 |
{"z", Float32, Float32, offsetof(Vertex,z), 0, 0, 0, 0},
|
|
|
42 |
{"r", Float32, Float32, offsetof(Vertex,r), 0, 0, 0, 0},
|
|
|
43 |
{"g", Float32, Float32, offsetof(Vertex,g), 0, 0, 0, 0},
|
|
|
44 |
{"b", Float32, Float32, offsetof(Vertex,b), 0, 0, 0, 0},
|
|
|
45 |
{"nx", Float32, Float32, offsetof(Vertex,nx), 0, 0, 0, 0},
|
|
|
46 |
{"ny", Float32, Float32, offsetof(Vertex,ny), 0, 0, 0, 0},
|
|
|
47 |
{"nz", Float32, Float32, offsetof(Vertex,nz), 0, 0, 0, 0},
|
|
|
48 |
};
|
|
|
49 |
|
|
|
50 |
PlyProperty face_props[] = { /* list of property information for a face */
|
|
|
51 |
{"vertex_indices", Int32, Int32, offsetof(Face,verts),
|
|
|
52 |
1, Uint8, Uint8, offsetof(Face,nverts)},
|
|
|
53 |
};
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
/*** the PLY object ***/
|
|
|
57 |
|
|
|
58 |
static int nverts,nfaces;
|
|
|
59 |
static Vertex **vlist;
|
|
|
60 |
static Face **flist;
|
|
|
61 |
|
|
|
62 |
static PlyOtherProp *vert_other,*face_other;
|
|
|
63 |
|
|
|
64 |
static int per_vertex_color = 0;
|
|
|
65 |
static int has_normals = 0;
|
|
|
66 |
|
|
|
67 |
void read_file(FILE* f)
|
|
|
68 |
{
|
|
|
69 |
int i,j;
|
|
|
70 |
int elem_count;
|
|
|
71 |
char *elem_name;
|
|
|
72 |
PlyFile *in_ply;
|
|
|
73 |
|
|
|
74 |
/*** Read in the original PLY object ***/
|
|
|
75 |
|
|
|
76 |
in_ply = read_ply (f);
|
|
|
77 |
|
|
|
78 |
for (i = 0; i < in_ply->num_elem_types; i++) {
|
|
|
79 |
|
|
|
80 |
/* prepare to read the i'th list of elements */
|
|
|
81 |
elem_name = setup_element_read_ply (in_ply, i, &elem_count);
|
|
|
82 |
|
|
|
83 |
if (equal_strings ("vertex", elem_name)) {
|
|
|
84 |
|
|
|
85 |
/* create a vertex list to hold all the vertices */
|
|
|
86 |
vlist = (Vertex **) malloc (sizeof (Vertex *) * elem_count);
|
|
|
87 |
nverts = elem_count;
|
|
|
88 |
|
|
|
89 |
/* set up for getting vertex elements */
|
|
|
90 |
|
|
|
91 |
setup_property_ply (in_ply, &vert_props[0]);
|
|
|
92 |
setup_property_ply (in_ply, &vert_props[1]);
|
|
|
93 |
setup_property_ply (in_ply, &vert_props[2]);
|
|
|
94 |
|
|
|
95 |
for (j = 0; j < in_ply->elems[i]->nprops; j++) {
|
|
|
96 |
PlyProperty *prop;
|
|
|
97 |
prop = in_ply->elems[i]->props[j];
|
|
|
98 |
if (equal_strings ("r", prop->name)) {
|
|
|
99 |
setup_property_ply (in_ply, &vert_props[3]);
|
|
|
100 |
per_vertex_color = 1;
|
|
|
101 |
}
|
|
|
102 |
if (equal_strings ("g", prop->name)) {
|
|
|
103 |
setup_property_ply (in_ply, &vert_props[4]);
|
|
|
104 |
per_vertex_color = 1;
|
|
|
105 |
}
|
|
|
106 |
if (equal_strings ("b", prop->name)) {
|
|
|
107 |
setup_property_ply (in_ply, &vert_props[5]);
|
|
|
108 |
per_vertex_color = 1;
|
|
|
109 |
}
|
|
|
110 |
if (equal_strings ("nx", prop->name)) {
|
|
|
111 |
setup_property_ply (in_ply, &vert_props[6]);
|
|
|
112 |
has_normals = 1;
|
|
|
113 |
}
|
|
|
114 |
if (equal_strings ("ny", prop->name)) {
|
|
|
115 |
setup_property_ply (in_ply, &vert_props[7]);
|
|
|
116 |
has_normals = 1;
|
|
|
117 |
}
|
|
|
118 |
if (equal_strings ("nz", prop->name)) {
|
|
|
119 |
setup_property_ply (in_ply, &vert_props[8]);
|
|
|
120 |
has_normals = 1;
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
vert_other = get_other_properties_ply (in_ply,
|
|
|
125 |
offsetof(Vertex,other_props));
|
|
|
126 |
|
|
|
127 |
/* grab all the vertex elements */
|
|
|
128 |
for (j = 0; j < elem_count; j++) {
|
|
|
129 |
vlist[j] = (Vertex *) malloc (sizeof (Vertex));
|
|
|
130 |
vlist[j]->r = 1;
|
|
|
131 |
vlist[j]->g = 1;
|
|
|
132 |
vlist[j]->b = 1;
|
|
|
133 |
get_element_ply (in_ply, (void *) vlist[j]);
|
|
|
134 |
}
|
|
|
135 |
}
|
|
|
136 |
else if (equal_strings ("face", elem_name)) {
|
|
|
137 |
|
|
|
138 |
/* create a list to hold all the face elements */
|
|
|
139 |
flist = (Face **) malloc (sizeof (Face *) * elem_count);
|
|
|
140 |
nfaces = elem_count;
|
|
|
141 |
|
|
|
142 |
/* set up for getting face elements */
|
|
|
143 |
|
|
|
144 |
setup_property_ply (in_ply, &face_props[0]);
|
|
|
145 |
face_other = get_other_properties_ply (in_ply,
|
|
|
146 |
offsetof(Face,other_props));
|
|
|
147 |
|
|
|
148 |
/* grab all the face elements */
|
|
|
149 |
for (j = 0; j < elem_count; j++) {
|
|
|
150 |
flist[j] = (Face *) malloc (sizeof (Face));
|
|
|
151 |
get_element_ply (in_ply, (void *) flist[j]);
|
|
|
152 |
}
|
|
|
153 |
}
|
|
|
154 |
else
|
|
|
155 |
get_other_element_ply (in_ply);
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
close_ply (in_ply);
|
|
|
159 |
free_ply (in_ply);
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
void ply_load(const std::string& fn, Geometry::TriMesh& mesh)
|
|
|
165 |
{
|
|
|
166 |
Vec3f v;
|
|
|
167 |
FILE* f = fopen(fn.c_str(),"rb");
|
|
|
168 |
read_file(f);
|
|
|
169 |
|
|
|
170 |
mesh.materials.resize(1);
|
341 |
jab |
171 |
mesh.materials[0].diffuse[0] = 172.0/256;
|
|
|
172 |
mesh.materials[0].diffuse[1] = 48.0/256;
|
|
|
173 |
mesh.materials[0].diffuse[2] = 72.0/256;
|
|
|
174 |
mesh.materials[0].specular[0] = 0.6;
|
|
|
175 |
mesh.materials[0].specular[1] = 0.6;
|
|
|
176 |
mesh.materials[0].specular[2] = 0.6;
|
|
|
177 |
mesh.materials[0].shininess =128;
|
|
|
178 |
|
|
|
179 |
|
337 |
jab |
180 |
int i;
|
|
|
181 |
for (i = 0; i < nverts; i++)
|
|
|
182 |
mesh.geometry.add_vertex(Vec3f(vlist[i]->x, vlist[i]->y, vlist[i]->z));
|
|
|
183 |
|
|
|
184 |
for (i = 0; i < nfaces; i++) {
|
|
|
185 |
for(int j=2;j<flist[i]->nverts;++j)
|
|
|
186 |
{
|
|
|
187 |
mesh.mat_idx.push_back(0);
|
|
|
188 |
Vec3i f_geo(Vec3i(flist[i]->verts[0], flist[i]->verts[1], flist[i]->verts[j]));
|
|
|
189 |
mesh.geometry.add_face(f_geo);
|
|
|
190 |
}
|
|
|
191 |
}
|
|
|
192 |
cout << "LOADED " << nverts << " verts " << nfaces << " faces " << endl;
|
|
|
193 |
|
|
|
194 |
}
|
|
|
195 |
|