-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobjloader.h
446 lines (387 loc) · 17.1 KB
/
objloader.h
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#ifndef OBJLOADER_H
#define OBJLOADER_H
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "zerogl.h"
#include "external/upng/upng.h"
static inline zgl_canvas_t loadTexture(char* filename) {
ZGL_DEBUG_PRINT("DEBUG: Loading texture %s\n", filename);
zgl_canvas_t texture = {0};
texture.frameBuffer = NULL;
upng_t* upng = upng_new_from_file(filename);
if (upng == NULL) {
fprintf(stderr, "ERROR: Couldn't load texture file %s\n", filename);
exit(-1);
}
upng_decode(upng);
uint8_t a, r, g, b;
if (upng_get_error(upng) == UPNG_EOK) {
texture.width = upng_get_width(upng);
texture.height = upng_get_height(upng);
ZGL_DEBUG_PRINT("DEBUG: Texture size %d x %d\n", texture.width, texture.height);
texture.frameBuffer = (uint32_t*) malloc(texture.width * texture.height * sizeof(uint32_t));
if (texture.frameBuffer == NULL) {
fprintf(stderr, "ERROR: Texture memory allocation failed.\n");
exit(-1);
}
upng_format format = upng_get_format(upng);
switch (format) {
case UPNG_BADFORMAT:
ZGL_DEBUG_PRINT("DEBUG: Texture format UPNG_BADFORMAT\n");
break;
case UPNG_RGB8:
ZGL_DEBUG_PRINT("DEBUG: Texture format UPNG_RGB8\n");
break;
case UPNG_RGB16:
ZGL_DEBUG_PRINT("DEBUG: Texture format UPNG_RGB16\n");
break;
case UPNG_RGBA8:
ZGL_DEBUG_PRINT("DEBUG: Texture format UPNG_RGBA8\n");
break;
case UPNG_RGBA16:
ZGL_DEBUG_PRINT("DEBUG: Texture format UPNG_RGBA16\n");
break;
case UPNG_LUMINANCE1:
ZGL_DEBUG_PRINT("DEBUG: Texture format UPNG_LUMINANCE1\n");
break;
case UPNG_LUMINANCE2:
ZGL_DEBUG_PRINT("DEBUG: Texture format UPNG_LUMINANCE2\n");
break;
case UPNG_LUMINANCE4:
ZGL_DEBUG_PRINT("DEBUG: Texture format UPNG_LUMINANCE4\n");
break;
case UPNG_LUMINANCE8:
ZGL_DEBUG_PRINT("DEBUG: Texture format UPNG_LUMINANCE8\n");
break;
case UPNG_LUMINANCE_ALPHA1:
ZGL_DEBUG_PRINT("DEBUG: Texture format UPNG_LUMINANCE_ALPHA1\n");
break;
case UPNG_LUMINANCE_ALPHA2:
ZGL_DEBUG_PRINT("DEBUG: Texture format UPNG_LUMINANCE_ALPHA2\n");
break;
case UPNG_LUMINANCE_ALPHA4:
ZGL_DEBUG_PRINT("DEBUG: Texture format UPNG_LUMINANCE_ALPHA4\n");
break;
case UPNG_LUMINANCE_ALPHA8:
ZGL_DEBUG_PRINT("DEBUG: Texture format UPNG_LUMINANCE_ALPHA8\n");
break;
default:
ZGL_DEBUG_PRINT("DEBUG: Texture format UNKNOWN\n");
break;
}
if (format == UPNG_RGBA8) {
uint32_t* buffer = (uint32_t*) upng_get_buffer(upng);
for (int i = 0; i < texture.width * texture.height; i++) {
uint32_t pixel = buffer[i];
a = (pixel & 0xFF000000) >> 24;
b = (pixel & 0x00FF0000) >> 16;
g = (pixel & 0x0000FF00) >> 8;
r = (pixel & 0x000000FF);
texture.frameBuffer[i] = zgl_color(r, g, b);
}
} else if (format == UPNG_RGB8) {
uint8_t* buffer = (uint8_t*) upng_get_buffer(upng);
for (int i = 0; i < texture.width * texture.height; i++) {
int bufferIndex = i * 3;
r = buffer[bufferIndex];
g = buffer[bufferIndex + 1];
b = buffer[bufferIndex + 2];
texture.frameBuffer[i] = zgl_color(r, g, b);
}
} else {
fprintf(stderr, "ERROR: Unsupported texture format\n");
exit(-1);
}
} else {
fprintf(stderr, "ERROR: Couldn't decode texture file\n");
exit(-1);
}
upng_free(upng);
ZGL_DEBUG_PRINT("DEBUG: Loaded texture %s\n", filename);
return texture;
}
static inline char* getPath(const char* filename) {
char* path = (char*) malloc(128 * sizeof(char));
if (!path) {
fprintf(stderr, "ERROR: Failed to allocate memory for path.\n");
exit(1);
}
strcpy(path, filename);
char* lastSlash = strrchr(path, '/');
if (lastSlash) {
*(lastSlash + 1) = '\0';
} else {
strcpy(path, "");
}
return path;
}
static inline zgl_material_t* loadMtlFile(const char* filename, int* numMaterials) {
*numMaterials = 0;
zgl_material_t* materials = NULL;
char line[128];
FILE* fp = fopen(filename, "r");
if (fp == NULL) {
fprintf(stderr, "WARN: Failed to open file %s.\n", filename);
return NULL;
}
int hasKa = 0;
int hasMapKa = 0;
float r, g, b;
while (fgets(line, 128, fp) != NULL) {
if (line[0] == 'n' && line[1] == 'e' && line[2] == 'w') {
*numMaterials = *numMaterials + 1;
materials = (zgl_material_t*) realloc(materials, *numMaterials * sizeof(zgl_material_t));
if (materials == NULL) {
fprintf(stderr, "ERROR: Material memory allocation failed.\n");
exit(1);
}
char* name = (char*) malloc(128 * sizeof(char));
sscanf(line, "newmtl %s\n", name);
materials[*numMaterials - 1].name = name;
materials[*numMaterials - 1].diffuseColor = zgl_color(255, 255, 255);
materials[*numMaterials - 1].specularColor = zgl_color(0, 0, 0);
materials[*numMaterials - 1].specularExponent = 10.0f;
materials[*numMaterials - 1].diffuseTexture = (zgl_canvas_t) {NULL, 0, 0,};
materials[*numMaterials - 1].specularTexture = (zgl_canvas_t) {NULL, 0, 0,};
}
if (line[0] == 'K' && line[1] == 'a') {
sscanf(line, "Ka %f %f %f\n", &r, &g, &b);
materials[*numMaterials - 1].ambientColor = zgl_color_from_floats(r, g, b);
hasKa = 1;
}
if (line[0] == 'K' && line[1] == 'd') {
sscanf(line, "Kd %f %f %f\n", &r, &g, &b);
materials[*numMaterials - 1].diffuseColor = zgl_color_from_floats(r, g, b);
}
if (line[0] == 'K' && line[1] == 's') {
sscanf(line, "Ks %f %f %f\n", &r, &g, &b);
materials[*numMaterials - 1].specularColor = zgl_color_from_floats(r, g, b);
}
if (line[0] == 'N' && line[1] == 's') {
sscanf(line, "Ns %f\n", &materials[*numMaterials - 1].specularExponent);
}
if (line[0] == 'm' && line[1] == 'a' && line[2] == 'p' && line[3] == '_' && line[4] == 'K' && line[5] == 'a') {
char* textureFilename = (char*) malloc(128 * sizeof(char));
sscanf(line, "map_Ka %s\n", textureFilename);
char* path = getPath(filename);
strcat(path, textureFilename);
ZGL_DEBUG_PRINT("DEBUG: Loading diffuse texture %s\n", textureFilename);
materials[*numMaterials - 1].ambientTexture = loadTexture(path);
hasMapKa = 1;
}
if (line[0] == 'm' && line[1] == 'a' && line[2] == 'p' && line[3] == '_' && line[4] == 'K' && line[5] == 'd') {
char* textureFilename = (char*) malloc(128 * sizeof(char));
sscanf(line, "map_Kd %s\n", textureFilename);
char* path = getPath(filename);
strcat(path, textureFilename);
ZGL_DEBUG_PRINT("DEBUG: Loading diffuse texture %s\n", textureFilename);
materials[*numMaterials - 1].diffuseTexture = loadTexture(path);
}
if (line[0] == 'm' && line[1] == 'a' && line[2] == 'p' && line[3] == '_' && line[4] == 'K' && line[5] == 's') {
char* textureFilename = (char*) malloc(128 * sizeof(char));
sscanf(line, "map_Ks %s\n", textureFilename);
char* path = getPath(filename);
strcat(path, textureFilename);
ZGL_DEBUG_PRINT("DEBUG: Loading diffuse texture %s\n", textureFilename);
materials[*numMaterials - 1].specularTexture = loadTexture(path);
}
}
if (!hasKa) {
materials[*numMaterials - 1].ambientColor = materials[*numMaterials - 1].diffuseColor;
}
if (!hasMapKa) {
materials[*numMaterials - 1].ambientTexture = materials[*numMaterials - 1].diffuseTexture;
}
fclose(fp);
return materials;
}
static inline zgl_mesh_t* loadObjFile(const char* filename, bool flipTexturesVertically) {
char name[128];
zgl_vec3_t* vertices = NULL;
int num_vertices = 0;
zgl_vec3_t *textureCoords = NULL;
int num_textureCoords = 0;
zgl_vec3_t* normals = NULL;
int num_normals = 0;
zgl_triangle_t* triangles = NULL;
int num_triangles = 0;
zgl_material_t* materials = NULL;
int num_materials = 0;
int currentMaterial;
char line[128];
FILE* fp = fopen(filename, "r");
while (fgets(line, 128, fp) != NULL) {
if (line[0] == 'o') {
sscanf(line, "o %s\n", name);
ZGL_DEBUG_PRINT("DEBUG: Loading object %s.\n", name);
}
if (line[0] == 'v' && line[1] == ' ') {
num_vertices++;
vertices = (zgl_vec3_t*) realloc(vertices, num_vertices * sizeof(zgl_vec3_t));
if (vertices == NULL) {
fprintf(stderr, "ERROR: Vertex memory couldn't be allocated.\n");
exit(-1);
}
sscanf(line, "v %f %f %f\n", &vertices[num_vertices - 1].x, &vertices[num_vertices - 1].y, &vertices[num_vertices - 1].z);
}
if (line[0] == 'v' && line[1] == 't') {
num_textureCoords++;
textureCoords = (zgl_vec3_t*) realloc(textureCoords, num_textureCoords * sizeof(zgl_vec3_t));
if (textureCoords == NULL) {
fprintf(stderr, "ERROR: Texture coordinate memory couldn't be allocated.\n");
exit(-1);
}
float u, v;
sscanf(line, "vt %f %f\n", &u, &v);
textureCoords[num_textureCoords - 1].x = u;
textureCoords[num_textureCoords - 1].y = flipTexturesVertically ? 1 - v : v;
textureCoords[num_textureCoords - 1].z = 0.0f;
}
if (line[0] == 'v' && line[1] == 'n') {
num_normals++;
normals = (zgl_vec3_t*) realloc(normals, num_normals * sizeof(zgl_vec3_t));
if (normals == NULL) {
fprintf(stderr, "ERROR: Normal memory couldn't be allocated.\n");
exit(-1);
}
sscanf(line, "vn %f %f %f\n", &normals[num_normals - 1].x, &normals[num_normals - 1].y, &normals[num_normals - 1].z);
}
if (line[0] == 'm' && line[1] == 't' && line[2] == 'l' && line[3] == 'l') {
char mtl_filename[128];
sscanf(line, "mtllib %s\n", mtl_filename);
char* path = getPath(filename);
strcat(path, mtl_filename);
ZGL_DEBUG_PRINT("DEBUG: Loading MTL %s\n", path);
materials = loadMtlFile(path, &num_materials);
ZGL_DEBUG_PRINT("DEBUG: Loaded %d materials\n", num_materials);
}
if (line[0] == 'u' && line[1] == 's' && line[2] == 'e') {
char material_name[128];
sscanf(line, "usemtl %s\n", material_name);
for (int i = 0; i < num_materials; i++) {
if (strcmp(material_name, materials[i].name) == 0) {
ZGL_DEBUG_PRINT("DEBUG: Using material %s\n", materials[i].name);
uint8_t r, g, b;
zgl_color_components(materials[i].diffuseColor, &r, &g, &b);
ZGL_DEBUG_PRINT("DEBUG: Color %d %d %d\n", r, g, b);
currentMaterial = i;
}
}
}
if (line[0] == 'f' && line[1] == ' ') {
num_triangles++;
triangles = (zgl_triangle_t*) realloc(triangles, num_triangles * sizeof(zgl_triangle_t));
if (triangles == NULL) {
fprintf(stderr, "ERROR: Triangle memory couldn't be allocated.\n");
exit(-1);
}
// Check how many slashes there are to determine the format of the face
int num_slashes = 0;
for (int i = 0; i < strlen(line); i++) {
if (line[i] == '/') {
num_slashes++;
}
}
int v0, v1, v2;
int n0, n1, n2;
int t0, t1, t2;
if (num_slashes == 6) {
sscanf(line, "f %d/%d/%d %d/%d/%d %d/%d/%d\n", &v0, &t0, &n0, &v1, &t1, &n1, &v2, &t2, &n2);
triangles[num_triangles - 1].v0 = v0 - 1;
triangles[num_triangles - 1].v1 = v1 - 1;
triangles[num_triangles - 1].v2 = v2 - 1;
triangles[num_triangles - 1].t0 = t0 - 1;
triangles[num_triangles - 1].t1 = t1 - 1;
triangles[num_triangles - 1].t2 = t2 - 1;
triangles[num_triangles - 1].n0 = n0 - 1;
triangles[num_triangles - 1].n1 = n1 - 1;
triangles[num_triangles - 1].n2 = n2 - 1;
} else if (num_slashes == 3) {
sscanf(line, "f %d/%d %d/%d %d/%d\n", &v0, &t0, &v1, &t1, &v2, &t2);
triangles[num_triangles - 1].v0 = v0 - 1;
triangles[num_triangles - 1].v1 = v1 - 1;
triangles[num_triangles - 1].v2 = v2 - 1;
triangles[num_triangles - 1].t0 = t0 - 1;
triangles[num_triangles - 1].t1 = t1 - 1;
triangles[num_triangles - 1].t2 = t2 - 1;
// Compute normals
zgl_vec3_t v0v1 = zgl_sub(vertices[v1 - 1], vertices[v0 - 1]);
zgl_vec3_t v0v2 = zgl_sub(vertices[v2 - 1], vertices[v0 - 1]);
zgl_vec3_t normal = zgl_cross(v0v1, v0v2);
normal = zgl_normalize(normal);
num_normals++;
normals = (zgl_vec3_t*) realloc(normals, num_normals * sizeof(zgl_vec3_t));
if (normals == NULL) {
fprintf(stderr, "ERROR: Normal memory couldn't be allocated.\n");
exit(-1);
}
normals[num_normals - 1] = normal;
triangles[num_triangles - 1].n0 = num_normals - 1;
triangles[num_triangles - 1].n1 = num_normals - 1;
triangles[num_triangles - 1].n2 = num_normals - 1;
} else if (num_slashes == 0) {
sscanf(line, "f %d %d %d\n", &v0, &v1, &v2);
triangles[num_triangles - 1].v0 = v0 - 1;
triangles[num_triangles - 1].v1 = v1 - 1;
triangles[num_triangles - 1].v2 = v2 - 1;
// Compute normals
zgl_vec3_t v0v1 = zgl_sub(vertices[v1 - 1], vertices[v0 - 1]);
zgl_vec3_t v0v2 = zgl_sub(vertices[v2 - 1], vertices[v0 - 1]);
zgl_vec3_t normal = zgl_cross(v0v1, v0v2);
normal = zgl_normalize(normal);
num_normals++;
normals = (zgl_vec3_t*) realloc(normals, num_normals * sizeof(zgl_vec3_t));
if (normals == NULL) {
fprintf(stderr, "ERROR: Normal memory couldn't be allocated.\n");
exit(-1);
}
ZGL_DEBUG_PRINT("DEBUG: Normal %d: ", num_normals - 1);
normals[num_normals - 1] = normal;
triangles[num_triangles - 1].n0 = num_normals - 1;
triangles[num_triangles - 1].n1 = num_normals - 1;
triangles[num_triangles - 1].n2 = num_normals - 1;
}
triangles[num_triangles - 1].materialIndex = currentMaterial;
}
}
fclose(fp);
zgl_mesh_t* mesh = (zgl_mesh_t*) malloc(sizeof(zgl_mesh_t));
if (mesh == NULL) {
fprintf(stderr, "ERROR: Mesh memory couldn't be allocated.\n");
exit(-1);
}
float* invMagnitudeNormals = (float*) malloc(num_normals * sizeof(float));
if (invMagnitudeNormals == NULL) {
fprintf(stderr, "ERROR: Normal magnitudes couldn't be allocated.\n");
exit(-1);
}
for (int i = 0; i < num_normals; i++) {
invMagnitudeNormals[i] = 1.0f / zgl_magnitude(normals[i]);
}
mesh->numVertices = num_vertices;
mesh->vertices = vertices;
mesh->numTextureCoords = num_textureCoords;
mesh->textureCoords = textureCoords;
mesh->numNormals = num_normals;
mesh->normals = normals;
mesh->invMagnitudeNormals = invMagnitudeNormals;
mesh->numTriangles = num_triangles;
mesh->triangles = triangles;
mesh->numMaterials = num_materials;
mesh->materials = materials;
mesh->center = zgl_mesh_center(vertices, num_vertices);
mesh->boundsRadius = zgl_mesh_bound_radius(vertices, num_vertices, mesh->center);
// Mesh name
mesh->name = (char*) malloc(strlen(name) * sizeof(char) + 1);
if (mesh->name == NULL) {
fprintf(stderr, "ERROR: Mesh name memory couldn't be allocated.\n");
exit(-1);
}
strcpy(mesh->name, name);
ZGL_DEBUG_PRINT("DEBUG: Loaded mesh %s\n", mesh->name);
return mesh;
}
#endif // OBJLOADER_H