-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.c
366 lines (277 loc) · 8.26 KB
/
model.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#define _GNU_SOURCE /* for strchrnul */
#include <GL/glew.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <strings.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "panic.h"
#include "bytearray.h"
#include "linmath.h"
#include "model.h"
#include "constants.h"
static struct model *
model_new(void)
{
struct model *m;
m = malloc(sizeof *m);
return m;
}
void
model_free(struct model *m)
{
free(m->coords);
free(m);
}
static struct byte_array *
load_file(const char *path)
{
static char buf[1024];
struct byte_array *b;
int fd;
int nbytes;
b = byte_array_new(1024);
bzero(buf, sizeof buf);
if ((fd = open(path, O_RDONLY)) == -1)
panic("Cannot load model '%s': %s", path, strerror(errno));
while ((nbytes = read(fd, buf, sizeof buf)))
byte_array_append(b, buf, nbytes);
close(fd);
return b;
}
static char *
byte_array_get_next_line(struct byte_array *b)
{
static char *ptr = NULL;
if (b != NULL)
ptr = b->data;
char *newline;
char *ret;
if (*ptr == '\0')
return NULL;
ret = ptr;
newline = strchrnul(ptr, '\n');
if (*newline == '\0')
ptr = newline;
else
ptr = newline + 1;
*newline = '\0';
return ret;
}
static void
byte_array_append_float(struct byte_array *b, GLfloat number)
{
byte_array_append(b, (const char *) &number, sizeof number);
}
static void
byte_array_append_int(struct byte_array *b, GLuint number)
{
byte_array_append(b, (const char *) &number, sizeof number);
}
static void
parse_vertex(struct byte_array *dest)
{
char *token;
token = strtok(NULL, " ");
byte_array_append_float(dest, (GLfloat) atof(token));
token = strtok(NULL, " ");
byte_array_append_float(dest, (GLfloat) atof(token));
token = strtok(NULL, " ");
byte_array_append_float(dest, (GLfloat) atof(token));
}
static void
parse_texture_vertex(struct byte_array *dest)
{
char *token;
token = strtok(NULL, " ");
byte_array_append_float(dest, (GLfloat) atof(token));
token = strtok(NULL, " ");
byte_array_append_float(dest, (GLfloat) atof(token));
}
static void
face_to_indices(const char *face, GLuint *ivertex,
GLuint *itexture, GLuint *inormal)
{
*ivertex = *itexture = *inormal = 0;
const char *ptr = face;
*ivertex = atoi(ptr);
ptr = strchrnul(face, '/');
if (ptr == NULL)
return;
++ptr;
*itexture = atoi(ptr);
ptr = strchrnul(ptr, '/');
if (ptr == NULL)
return;
++ptr;
*inormal = atoi(ptr);
}
static void
parse_face_component(struct byte_array *faces)
{
char *token;
GLuint ivertex;
GLuint itexture;
GLuint inormal;
token = strtok(NULL, " ");
face_to_indices(token, &ivertex, &itexture, &inormal);
byte_array_append_int(faces, ivertex);
byte_array_append_int(faces, itexture);
byte_array_append_int(faces, inormal);
}
static void
parse_face(struct byte_array *faces)
{
parse_face_component(faces); /* first */
parse_face_component(faces); /* second */
parse_face_component(faces); /* third */
}
static void
parse_line(char *line, struct byte_array *vertices,
struct byte_array *normals, struct byte_array *textures,
struct byte_array *faces)
{
char *token;
if (line[0] == '#' || line[0] == 'o')
return;
while (*line == ' ')
line++;
if (strlen(line) == 0)
return;
token = strtok(line, " ");
if (strcmp(token, "v") == 0) {
parse_vertex(vertices);
} else if (strcmp(token, "vn") == 0) {
parse_vertex(normals);
} else if (strcmp(token, "vt") == 0) {
parse_texture_vertex(textures);
} else if (strcmp(token, "f") == 0) {
parse_face(faces);
}
}
static size_t
byte_array_to_float_array(struct byte_array *b, GLfloat **float_array)
{
*float_array = (GLfloat *) malloc(b->size);
memcpy(*float_array, b->data, b->size);
return b->size / sizeof (GLfloat);
}
static size_t
byte_array_to_int_array(struct byte_array *b, GLuint **int_array)
{
*int_array = (GLuint *) malloc(b->size);
memcpy(*int_array, b->data, b->size);
return b->size / sizeof (GLuint);
}
static GLfloat
vertex_coord(const GLfloat *vertex_data, const GLuint *index_data,
int coord, int vertex)
{
const unsigned int offset = vertex * INDEX_STRIDE + VERTEX_OFFSET;
const unsigned int vertex_index = index_data[offset] - 1; /* 0-based indices */
const unsigned int vertex_offset = vertex_index * COORD_STRIDE + coord;
return vertex_data[vertex_offset];
}
static GLfloat
normal_coord(const GLfloat *normal_data, const GLuint *index_data,
int coord, int vertex)
{
const unsigned int offset = vertex * INDEX_STRIDE + NORMAL_OFFSET;
const unsigned int normal_index = index_data[offset] - 1; /* 0-based indices */
const unsigned int normal_offset = normal_index * COORD_STRIDE + coord;
return normal_data[normal_offset];
}
static GLfloat
texture_coord(const GLfloat *texture_data, const GLuint *index_data,
int coord, int vertex)
{
const unsigned int offset = vertex * INDEX_STRIDE + TEXTURE_OFFSET;
const unsigned int texture_index = index_data[offset] - 1; /* 0-based indices */
const unsigned int texture_offset = texture_index * TEX_COORD_STRIDE + coord;
return texture_data[texture_offset];
}
static void
setup_model(struct model *m, struct byte_array *vertices,
struct byte_array *normals, struct byte_array *textures,
struct byte_array *indices)
{
size_t icount;
size_t buffer_size;
size_t tex_size;
int i;
int nvertex;
int current_coord;
GLfloat *vertex_data;
GLfloat *normal_data;
GLfloat *texture_data;
GLuint *index_data;
GLfloat *buffer;
byte_array_to_float_array(vertices, &vertex_data);
byte_array_to_float_array(normals, &normal_data);
tex_size = byte_array_to_float_array(textures, &texture_data);
icount = byte_array_to_int_array(indices, &index_data);
/* icount holds the total number of components of each index
* i.e. icount = number of vertex * 3 (vertex index, texture index and
* normal index).
*/
buffer_size = icount * 3 * sizeof (GLfloat);
nvertex = icount / 3;
/* size of vertices + size of normals */
buffer = (GLfloat *) malloc(buffer_size);
memset(buffer, 0, buffer_size);
for (current_coord = i = 0; i < nvertex; ++i) {
buffer[current_coord++] = vertex_coord(vertex_data, index_data, COORD_X, i);
buffer[current_coord++] = vertex_coord(vertex_data, index_data, COORD_Y, i);
buffer[current_coord++] = vertex_coord(vertex_data, index_data, COORD_Z, i);
buffer[current_coord++] = normal_coord(normal_data, index_data, COORD_X, i);
buffer[current_coord++] = normal_coord(normal_data, index_data, COORD_Y, i);
buffer[current_coord++] = normal_coord(normal_data, index_data, COORD_Z, i);
if (tex_size > 0) {
buffer[current_coord++] = texture_coord(texture_data, index_data, COORD_X, i);
buffer[current_coord++] = texture_coord(texture_data, index_data, COORD_Y, i);
} else {
buffer[current_coord++] = 0.0;
buffer[current_coord++] = 0.0;
}
}
free(vertex_data);
free(normal_data);
free(texture_data);
free(index_data);
m->coords = buffer;
m->csize = buffer_size;
m->nvertex = nvertex;
}
struct model *
load_model(const char *path)
{
struct model *model;
struct byte_array *file;
struct byte_array *vertices;
struct byte_array *normals;
struct byte_array *textures;
struct byte_array *faces;
char *line;
vertices = byte_array_new(1024);
normals = byte_array_new(1024);
textures = byte_array_new(1024);
faces = byte_array_new(1024);
file = load_file(path);
line = byte_array_get_next_line(file);
while (line) {
parse_line(line, vertices, normals, textures, faces);
line = byte_array_get_next_line(NULL);
}
byte_array_free(file);
model = model_new();
setup_model(model, vertices, normals, textures, faces);
byte_array_free(vertices);
byte_array_free(normals);
byte_array_free(textures);
byte_array_free(faces);
return model;
}