-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobj_loader.cpp
More file actions
163 lines (130 loc) · 5.33 KB
/
obj_loader.cpp
File metadata and controls
163 lines (130 loc) · 5.33 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
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
#include "obj_loader.h"
#include <stdio.h>
#include <string.h>
#include <vector>
#include <utility> // std::swap
#include "platform.h"
#ifndef _MSC_VER
#define fscanf_s fscanf
#endif
#define OBJ_LOADER_DEBUG true
std::unique_ptr<triangle[]> readObj(const char* filename, material* mat, size_t* numTris, bool flip_triangles, const Mat4& scale, const Vec3& translate, const Mat4& rotate) {
std::vector<Vec3> verts;
std::vector<Vec3> norms;
std::vector<triangle> polys; // TODO: this creates an unnecessary copy but is easier for now, we're not really worried about scene build cost anyways
Mat4 invRot = Mat4::Invert(rotate);
FILE* f = fopen(filename, "r");
if (f) {
float x, y, z;
Vec3 a, b, c;
Vec3 an, bn, cn;
while (!feof(f)) {
char s = (char) getc(f);
if (s == '#') { // comment
char buf[64] = { 0 };
do {
fgets(buf, sizeof(buf), f);
} while (buf[strlen(buf) - 1] != '\n' && !feof(f)); // read until newline
}
else if (s == '\n' || s == ' ' || s == '\t') {
// continue
}
else if (s == 'v') {
s = (char) getc(f);
if (s == ' ' || s == '\t') { // vertex
if (fscanf_s(f, " %f %f %f ", &x, &y, &z) == 3) {
verts.push_back(Vec3(x, y, z));
}
else {
if (OBJ_LOADER_DEBUG) MRT_Assert(false);
MRT_DebugPrint("Warning: unknown obj format! May slow down or crash the program!\n");
break;
}
}
else if (s == 'n') { // vertex normal
if (fscanf_s(f, " %f %f %f ", &x, &y, &z) == 3) {
norms.push_back(Vec3(x, y, z));
}
else {
if (OBJ_LOADER_DEBUG) MRT_Assert(false);
MRT_DebugPrint("Warning: unknown obj format! May slow down or crash the program!\n");
break;
}
}
}
else if (s == 'f') { // face
if (norms.empty()) { // format: f 1 2 3
int ai, bi, ci;
if (fscanf_s(f, " %i %i %i ", &ai, &bi, &ci) == 3) {
if (flip_triangles) {
std::swap(ai, ci);
}
a = scale * verts[ai - 1];
b = scale * verts[bi - 1];
c = scale * verts[ci - 1];
a = rotate * a;
b = rotate * b;
c = rotate * c;
a += translate;
b += translate;
c += translate;
polys.emplace_back(a, b, c, mat);
}
else {
if (OBJ_LOADER_DEBUG) MRT_Assert(false);
MRT_DebugPrint("Warning: unknown obj format! May slow down or crash the program!\n");
break;
}
}
else { // format : f1//4 2//5 3//6
int ai, bi, ci;
int ani, bni, cni;
if (fscanf_s(f, " %i//%i %i//%i %i//%i ", &ai, &ani, &bi, &bni, &ci, &cni) == 6) {
if (flip_triangles) {
std::swap(ai, ci);
std::swap(ani, cni);
}
a = scale * verts[ai - 1];
b = scale * verts[bi - 1];
c = scale * verts[ci - 1];
an = norms[ani - 1];
bn = norms[bni - 1];
cn = norms[cni - 1];
an = an * invRot;
bn = bn * invRot;
cn = cn * invRot;
a = rotate * a;
b = rotate * b;
c = rotate * c;
a += translate;
b += translate;
c += translate;
polys.emplace_back(a, b, c, an, bn, cn, mat);
}
else {
if (OBJ_LOADER_DEBUG) MRT_Assert(false);
MRT_DebugPrint("Warning: unknown obj format! May slow down or crash the program!\n");
break;
}
}
}
else {
char buf[64] = { 0 };
do {
fgets(buf, sizeof(buf), f);
} while (buf[strlen(buf) - 1] != '\n' && !feof(f)); // read until newline
//if (OBJ_LOADER_DEBUG) MRT_Assert(false);
MRT_DebugPrint("Warning: unknown obj format! May slow down or crash the program!\n");
}
}
fclose(f);
*numTris = polys.size();
std::unique_ptr<triangle[]> list = std::make_unique_for_overwrite<triangle[]>(polys.size());
memcpy(list.get(), polys.data(), polys.size() * sizeof(triangle));
return list;
}
else {
*numTris = 0;
return nullptr;
}
}