-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesh.cpp
291 lines (235 loc) · 6.47 KB
/
mesh.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// mesh.cpp: implementation of the mesh class.
//
//////////////////////////////////////////////////////////////////////
#include "mesh.h"
#include <iostream>
const char* obj_database = ""; // ©w∏q mesh ™∫πw≥]•ÿø˝
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
mesh::mesh(const char* obj_file)
{
mTotal = 0; // mList[0] reserved for default meterial
vTotal = tTotal = nTotal = fTotal = 0;
Init(obj_file);
}
mesh::mesh()
{
mTotal = 0;
vTotal = tTotal = nTotal = fTotal = 0;
}
mesh::~mesh()
{
}
void mesh::LoadMesh(string obj_file)
{
FILE *scene;
char token[100], buf[100], v[5][100]; // v[5] ™Ì•‹§@≠” polygon ≥öh•i•H¶≥ 5≠” vertex
float vec[3];
size_t n_vertex, n_texture, n_normal;
size_t cur_mtl = 0; // state variable: •ÿ´e©“®œ•Œ™∫ material
scene = fopen(obj_file.c_str(),"r");
s_file = obj_file;
if (!scene)
{
cout<< string("Can not open object File \"") << obj_file << "\" !" << endl;
return;
}
cout<<endl<<obj_file<<endl;
while(!feof(scene))
{
token[0] = NULL;
fscanf(scene,"%s", token); // ≈™ token
if (!strcmp(token,"g"))
{
fscanf(scene,"%s",buf);
}
else if (!strcmp(token,"mtllib"))
{
char mat_file[256];
matFile = mat_file;
fscanf(scene,"%s", mat_file);
LoadMtl(string(obj_database) + string(mat_file));
}
else if (!strcmp(token,"usemtl"))
{
fscanf(scene,"%s",buf);
cur_mtl = matMap[s_file+string("_")+string(buf)];
}
else if (!strcmp(token,"v"))
{
fscanf(scene,"%f %f %f",&vec[0],&vec[1],&vec[2]);
vList.push_back(Vec3(vec));
}
else if (!strcmp(token,"vn"))
{
fscanf(scene,"%f %f %f",&vec[0],&vec[1],&vec[2]);
nList.push_back(Vec3(vec));
}
else if (!strcmp(token,"vt"))
{
fscanf(scene,"%f %f",&vec[0],&vec[1]);
tList.push_back(Vec3(vec));
}
else if (!strcmp(token,"f"))
{
size_t i;
for (i=0;i<3;i++) // face πw≥]¨∞ 3°A∞≤≥]§@≠” polygon ≥£•u¶≥ 3 ≠” vertex
{
fscanf(scene,"%s",v[i]);
//printf("[%s]",v[i]);
}
//printf("\n");
Vertex tmp_vertex[3]; // for faceList structure
for (i=0;i<3;i++) // for each vertex of this face
{
char str[20], ch;
size_t base,offset;
base = offset = 0;
// calculate vertex-list index
while( (ch=v[i][base+offset]) != '/' && (ch=v[i][base+offset]) != '\0')
{
str[offset] = ch;
offset++;
}
str[offset] = '\0';
n_vertex = atoi(str);
base += (ch == '\0')? offset : offset+1;
offset = 0;
// calculate texture-list index
while( (ch=v[i][base+offset]) != '/' && (ch=v[i][base+offset]) != '\0')
{
str[offset] = ch;
offset++;
}
str[offset] = '\0';
n_texture = atoi(str); // case: xxx//zzz°Atexture ≥]¨∞ 0 (tList ±q 1 ∂}©l)
base += (ch == '\0')? offset : offset+1;
offset = 0;
// calculate normal-list index
while( (ch=v[i][base+offset]) != '\0')
{
str[offset] = ch;
offset++;
}
str[offset] = '\0';
n_normal = atoi(str); // case: xxx/yyy°Anormal ≥]¨∞ 0 (nList ±q 1 ∂}©l)
tmp_vertex[i].v = n_vertex;
tmp_vertex[i].t = n_texture;
tmp_vertex[i].n = n_normal;
}
faceList.push_back(FACE(tmp_vertex[0],tmp_vertex[1],tmp_vertex[2], cur_mtl));
}
else if (!strcmp(token,"#")) // µ˘∏—
fgets(buf,100,scene);
// printf("[%s]\n",token);
}
if (scene) fclose(scene);
vTotal = vList.size();
nTotal = nList.size();
tTotal = tList.size();
fTotal = faceList.size();
printf("vetex: %d, normal: %d, texture: %d, triangles: %d\n",vTotal, nTotal, tTotal, fTotal);
}
void mesh::LoadMtl(string tex_file)
{
char token[100], buf[100];
float r,g,b;
fp_mtl = fopen(tex_file.c_str(),"r");
if (!fp_mtl)
{
cout << "Can't open material file \"" << tex_file << "\"!" << endl;
return;
}
cout<<tex_file<<endl;
size_t cur_mat;
while(!feof(fp_mtl))
{
token[0] = NULL;
fscanf(fp_mtl,"%s", token); // ≈™ token
if (!strcmp(token,"newmtl"))
{
fscanf(fp_mtl,"%s",buf);
material newMtl;
mList.push_back(newMtl);
cur_mat = mTotal++; // ±q mList[1] ∂}©l°AmList[0] ™≈§U®”µπ default material •Œ
matMap[s_file+string("_")+string(buf)] = cur_mat; // matMap["material_name"] = material_id;
}
else if (!strcmp(token,"Ka"))
{
fscanf(fp_mtl,"%f %f %f",&r,&g,&b);
mList[cur_mat].Ka[0] = r;
mList[cur_mat].Ka[1] = g;
mList[cur_mat].Ka[2] = b;
mList[cur_mat].Ka[3] = 1;
}
else if (!strcmp(token,"Kd"))
{
fscanf(fp_mtl,"%f %f %f",&r,&g,&b);
mList[cur_mat].Kd[0] = r;
mList[cur_mat].Kd[1] = g;
mList[cur_mat].Kd[2] = b;
mList[cur_mat].Kd[3] = 1;
}
else if (!strcmp(token,"Ks"))
{
fscanf(fp_mtl,"%f %f %f",&r,&g,&b);
mList[cur_mat].Ks[0] = r;
mList[cur_mat].Ks[1] = g;
mList[cur_mat].Ks[2] = b;
mList[cur_mat].Ks[3] = 1;
}
else if (!strcmp(token,"Ns"))
{
fscanf(fp_mtl,"%f",&r);
mList[cur_mat].Ns = r;
}
else if (!strcmp(token,"Tr"))
{
fscanf(fp_mtl,"%f",&r);
mList[cur_mat].Tr = r;
}
else if (!strcmp(token,"d"))
{
fscanf(fp_mtl,"%f",&r);
mList[cur_mat].Tr = r;
}
if (!strcmp(token,"map_Kd"))
{
fscanf(fp_mtl,"%s",buf);
mList[cur_mat].map_Kd = buf;
}
if (!strcmp(token,"map_Ks"))
{
fscanf(fp_mtl,"%s",buf);
mList[cur_mat].map_Ks = buf;
}
if (!strcmp(token,"map_Ka"))
{
fscanf(fp_mtl,"%s",buf);
mList[cur_mat].map_Ka = buf;
}
else if (!strcmp(token,"#")) // µ˘∏—
fgets(buf,100,fp_mtl);
// printf("[%s]\n",token);
}
printf("total material:%d\n",matMap.size());
if (fp_mtl) fclose(fp_mtl);
}
void mesh::Init(const char* obj_file)
{
float default_value[3] = {1,1,1};
vList.push_back(Vec3(default_value)); // ¶]¨∞ *.obj ™∫ index ¨O±q 1 ∂}©l
nList.push_back(Vec3(default_value)); // ©“•H≠n•˝ push §@≠” default value ®Ï vList[0],nList[0],tList[0]
tList.push_back(Vec3(default_value));
material defaultMtl;
mList.push_back(defaultMtl);
// ©w∏q default meterial: mList[0]
mList[0].Ka[0] = 0.0f; mList[0].Ka[1] = 0.0f; mList[0].Ka[2] = 0.0f; mList[0].Ka[3] = 1.0f;
mList[0].Kd[0] = 1.0f; mList[0].Kd[1] = 1.0f; mList[0].Kd[2] = 1.0f; mList[0].Kd[3] = 1.0f;
mList[0].Ks[0] = 0.8f; mList[0].Ks[1] = 0.8f; mList[0].Ks[2] = 0.8f; mList[0].Ks[3] = 1.0f;
mList[0].Ns = 32.0f;
mTotal++;
LoadMesh(string(obj_file)); // ≈™§J .obj ¿… (•i≥B≤z Material)
}