-
-
Notifications
You must be signed in to change notification settings - Fork 649
Expand file tree
/
Copy pathdebug_groups.cc
More file actions
72 lines (58 loc) · 1.84 KB
/
Copy pathdebug_groups.cc
File metadata and controls
72 lines (58 loc) · 1.84 KB
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
#include "../tinyobj_v3.hh"
#include <cstdio>
#include <cstdlib>
const char* PosixFileRead(const char* filepath, size_t* out_size, void* user_data) {
(void)user_data;
FILE* fp = fopen(filepath, "rb");
if (!fp) {
printf("Failed to open: %s\n", filepath);
*out_size = 0;
return nullptr;
}
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
fseek(fp, 0, SEEK_SET);
char* buffer = (char*)malloc(size);
if (!buffer) {
fclose(fp);
*out_size = 0;
return nullptr;
}
size_t bytes_read = fread(buffer, 1, size, fp);
fclose(fp);
*out_size = bytes_read;
printf("Read file: %s (%zu bytes)\n", filepath, bytes_read);
return buffer;
}
void PosixFileFree(const char* data, void* user_data) {
(void)user_data;
free((void*)data);
}
int main() {
size_t obj_size;
const char* obj_data = PosixFileRead("../models/cube.obj", &obj_size, nullptr);
if (!obj_data) {
printf("Failed to load OBJ\n");
return 1;
}
tinyobj::v3::ParserConfig config;
config.file_callbacks.read_fn = PosixFileRead;
config.file_callbacks.free_fn = PosixFileFree;
config.mtl_search_path = "../models/";
tinyobj::v3::ObjParser parser(config);
auto result = parser.parseFromMemory(obj_data, obj_size);
PosixFileFree(obj_data, nullptr);
if (!result.success()) {
printf("Parse failed!\n");
printf("Errors: %s\n", result.errors().formatErrors().c_str());
return 1;
}
printf("Parse succeeded!\n");
printf("Shapes loaded: %zu\n", result.shapes().size());
for (size_t i = 0; i < result.shapes().size(); i++) {
const auto& shape = result.shapes()[i];
printf("Shape[%zu]: name='%s', faces=%zu\n",
i, shape.name.c_str(), shape.mesh.num_face_vertices.size());
}
return 0;
}