-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel_obj.c
314 lines (272 loc) · 7.58 KB
/
model_obj.c
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "mio.h"
#define SEP " \t\r\n"
struct floatarray {
int len, cap;
float *data;
};
struct intarray {
int len, cap;
unsigned short *data;
};
struct partarray {
int len, cap;
struct part *data;
};
// temp buffers are global so we can reuse them between models
static struct floatarray position = { 0, 0, NULL };
static struct floatarray normal = { 0, 0, NULL };
static struct floatarray texcoord = { 0, 0, NULL };
static struct floatarray vertex = { 0, 0, NULL };
static struct intarray element = { 0, 0, NULL };
static struct partarray part = { 0, 0, NULL };
static inline void push_float(struct floatarray *a, float v)
{
if (a->len + 1 >= a->cap) {
a->cap = 600 + a->cap * 2;
a->data = realloc(a->data, a->cap * sizeof(*a->data));
}
a->data[a->len++] = v;
}
static inline void push_int(struct intarray *a, int v)
{
assert(v >= 0 && v < 65535);
if (a->len + 1 >= a->cap) {
a->cap = 600 + a->cap * 2;
a->data = realloc(a->data, a->cap * sizeof(*a->data));
}
a->data[a->len++] = v;
}
static inline void push_part(struct partarray *a, int first, int last, int material)
{
if (a->len + 1 >= a->cap) {
a->cap = 600 + a->cap * 2;
a->data = realloc(a->data, a->cap * sizeof(*a->data));
}
a->data[a->len].first = first;
a->data[a->len].count = last - first;
a->data[a->len].material = material;
a->len++;
}
static void add_position(float x, float y, float z)
{
push_float(&position, x);
push_float(&position, -z);
push_float(&position, y);
}
static void add_texcoord(float u, float v)
{
push_float(&texcoord, u);
push_float(&texcoord, 1-v);
}
static void add_normal(float x, float y, float z)
{
push_float(&normal, x);
push_float(&normal, -z);
push_float(&normal, y);
}
static int add_vertex_imp(float v[8])
{
int i;
for (i = vertex.len - 8; i >= 0; i -= 8)
if (!memcmp(vertex.data + i, v, sizeof(float) * 8))
return i / 8;
for (i = 0; i < 8; i++)
push_float(&vertex, v[i]);
return vertex.len / 8 - 1;
}
static int add_vertex(int pi, int ti, int ni)
{
float v[8];
v[0] = position.data[pi * 3 + 0];
v[1] = position.data[pi * 3 + 1];
v[2] = position.data[pi * 3 + 2];
v[3] = ti >= 0 && ti < texcoord.len ? texcoord.data[ti * 2 + 0] : 0;
v[4] = ti >= 0 && ti < texcoord.len ? texcoord.data[ti * 2 + 1] : 0;
v[5] = ni >= 0 && ni < normal.len ? normal.data[ni * 3 + 0] : 0;
v[6] = ni >= 0 && ni < normal.len ? normal.data[ni * 3 + 1] : 0;
v[7] = ni >= 0 && ni < normal.len ? normal.data[ni * 3 + 2] : 1;
return add_vertex_imp(v);
}
static void add_triangle(
int vp0, int vt0, int vn0,
int vp1, int vt1, int vn1,
int vp2, int vt2, int vn2)
{
push_int(&element, add_vertex(vp0, vt0, vn0));
push_int(&element, add_vertex(vp1, vt1, vn1));
push_int(&element, add_vertex(vp2, vt2, vn2));
}
static char *abspath(char *path, const char *dirname, const char *filename, int maxpath)
{
if (dirname[0]) {
strlcpy(path, dirname, maxpath);
strlcat(path, "/", maxpath);
strlcat(path, filename, maxpath);
} else {
strlcpy(path, filename, maxpath);
}
return path;
}
static int mtl_count = 0;
static struct {
char name[80];
int material;
} mtl_map[256];
static void mtllib(char *dirname, char *filename)
{
char path[1024];
char *line, *next;
unsigned char *data;
int len;
char *s;
data = load_file(abspath(path, dirname, filename, sizeof path), &len);
if (!data) {
warn("cannot load material library: '%s'", filename);
return;
}
data[len-1] = 0; /* over-write final newline to zero-terminate */
for (line = (char*)data; line; line = next) {
next = strchr(line, '\n');
if (next)
*next++ = 0;
s = strtok(line, SEP);
if (!s) {
continue;
} else if (!strcmp(s, "newmtl")) {
s = strtok(NULL, SEP);
if (s) {
strlcpy(mtl_map[mtl_count].name, s, sizeof mtl_map[0].name);
mtl_map[mtl_count].material = 0;
mtl_count++;
}
} else if (!strcmp(s, "map_Kd")) {
s = strtok(NULL, SEP);
if (s && mtl_count > 0) {
mtl_map[mtl_count-1].material = load_texture(abspath(path, dirname, s, sizeof path), 1);
}
}
}
free(data);
}
int usemtl(char *matname)
{
int i;
for (i = 0; i < mtl_count; i++)
if (!strcmp(mtl_map[i].name, matname))
return mtl_map[i].material;
return 0;
}
static void splitfv(char *buf, int *vpp, int *vnp, int *vtp)
{
char tmp[200], *p, *vp, *vt, *vn;
strlcpy(tmp, buf, sizeof tmp);
p = tmp;
vp = strsep(&p, "/");
vt = strsep(&p, "/");
vn = strsep(&p, "/");
*vpp = vp && vp[0] ? atoi(vp) - 1 : 0;
*vtp = vt && vt[0] ? atoi(vt) - 1 : 0;
*vnp = vn && vn[0] ? atoi(vn) - 1 : 0;
}
struct model *load_obj_from_memory(const char *filename, unsigned char *data, int len)
{
char dirname[1024];
char *line, *next, *p, *s;
struct model *model;
struct mesh *mesh;
int fvp[20], fvt[20], fvn[20];
int first, material;
int i, n;
printf("loading obj model '%s'\n", filename);
strlcpy(dirname, filename, sizeof dirname);
p = strrchr(dirname, '/');
if (!p) p = strrchr(dirname, '\\');
if (p) *p = 0;
else strlcpy(dirname, "", sizeof dirname);
mtl_count = 0;
position.len = 0;
texcoord.len = 0;
normal.len = 0;
element.len = 0;
part.len = 0;
first = 0;
material = 0;
data[len-1] = 0; /* over-write final newline to zero-terminate */
for (line = (char*)data; line; line = next) {
next = strchr(line, '\n');
if (next)
*next++ = 0;
s = strtok(line, SEP);
if (!s) {
continue;
} else if (!strcmp(s, "v")) {
char *x = strtok(NULL, SEP);
char *y = strtok(NULL, SEP);
char *z = strtok(NULL, SEP);
add_position(atof(x), atof(y), atof(z));
} else if (!strcmp(s, "vt")) {
char *u = strtok(NULL, SEP);
char *v = strtok(NULL, SEP);
add_texcoord(atof(u), atof(v));
} else if (!strcmp(s, "vn")) {
char *x = strtok(NULL, SEP);
char *y = strtok(NULL, SEP);
char *z = strtok(NULL, SEP);
add_normal(atof(x), atof(y), atof(z));
} else if (!strcmp(s, "f")) {
n = 0;
s = strtok(NULL, SEP);
while (s) {
if (*s) {
splitfv(s, fvp+n, fvn+n, fvt+n);
n++;
}
s = strtok(NULL, SEP);
}
for (i = 1; i < n - 1; i++) {
add_triangle(fvp[0], fvn[0], fvt[0],
fvp[i], fvn[i], fvt[i],
fvp[i+1], fvn[i+1], fvt[i+1]);
}
} else if (!strcmp(s, "mtllib")) {
s = strtok(NULL, SEP);
mtllib(dirname, s);
} else if (!strcmp(s, "usemtl")) {
if (element.len > first)
push_part(&part, first, element.len, material);
s = strtok(NULL, SEP);
material = usemtl(s);
first = element.len;
}
}
if (element.len > first)
push_part(&part, first, element.len, material);
printf("\t%d parts; %d vertices; %d triangles", part.len, vertex.len/8, element.len/3);
mesh = malloc(sizeof(struct mesh));
mesh->tag = TAG_MESH;
mesh->enabled = 1<<ATT_POSITION | 1<<ATT_NORMAL | 1<<ATT_TEXCOORD;
mesh->skel = NULL;
mesh->inv_bind_matrix = NULL;
mesh->count = part.len;
mesh->part = malloc(part.len * sizeof(struct part));
memcpy(mesh->part, part.data, part.len * sizeof(struct part));
glGenVertexArrays(1, &mesh->vao);
glGenBuffers(1, &mesh->vbo);
glGenBuffers(1, &mesh->ibo);
glBindVertexArray(mesh->vao);
glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->ibo);
glBufferData(GL_ARRAY_BUFFER, vertex.len * 4, vertex.data, GL_STATIC_DRAW);
glEnableVertexAttribArray(ATT_POSITION);
glEnableVertexAttribArray(ATT_NORMAL);
glEnableVertexAttribArray(ATT_TEXCOORD);
glVertexAttribPointer(ATT_POSITION, 3, GL_FLOAT, 0, 32, (void*)0);
glVertexAttribPointer(ATT_NORMAL, 3, GL_FLOAT, 0, 32, (void*)20);
glVertexAttribPointer(ATT_TEXCOORD, 2, GL_FLOAT, 0, 32, (void*)12);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, element.len * 2, element.data, GL_STATIC_DRAW);
model = malloc(sizeof *model);
model->skel = NULL;
model->mesh = mesh;
model->anim = NULL;
return model;
}