-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj.cpp
201 lines (175 loc) · 5.49 KB
/
obj.cpp
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
#include "obj.h"
#include <stdlib.h>
#include "mesh.h"
#include "memory.h"
#include "file.h"
struct ParserState
{
unsigned char* data;
unsigned char* head;
unsigned char* end;
};
struct ParsedFace
{
unsigned v1;
unsigned v2;
unsigned v3;
unsigned n1;
unsigned n2;
unsigned n3;
unsigned u1;
unsigned u2;
unsigned u3;
};
struct ParsedData
{
DynamicArray<Vector3> vertices;
DynamicArray<Vector3> normals;
DynamicArray<Vector2> uvs;
DynamicArray<ParsedFace> faces;
};
static void skip_to_numeric(ParserState* ps)
{
while (ps->head < ps->end && (*ps->head < '0' || *ps->head > '9') && *ps->head != '-')
{
++ps->head;
}
}
static void parse_uv(ParserState* ps, ParsedData* pd)
{
Vector2* uv = pd->uvs.push();
skip_to_numeric(ps);
uv->x = strtof((const char*)ps->head, (char**)&ps->head);
skip_to_numeric(ps);
uv->y = strtof((const char*)ps->head, (char**)&ps->head);
}
static void parse_normal(ParserState* ps, ParsedData* pd)
{
Vector3* normal = pd->normals.push();
skip_to_numeric(ps);
normal->x = strtof((const char*)ps->head, (char**)&ps->head);
skip_to_numeric(ps);
normal->y = strtof((const char*)ps->head, (char**)&ps->head);
skip_to_numeric(ps);
normal->z = strtof((const char*)ps->head, (char**)&ps->head);
}
static void parse_vertex(ParserState* ps, ParsedData* pd)
{
Vector3* vertex = pd->vertices.push();
skip_to_numeric(ps);
vertex->x = strtof((const char*)ps->head, (char**)&ps->head);
skip_to_numeric(ps);
vertex->y = strtof((const char*)ps->head, (char**)&ps->head);
skip_to_numeric(ps);
vertex->z = strtof((const char*)ps->head, (char**)&ps->head);
}
static void parse_face(ParserState* ps, ParsedData* pd)
{
ParsedFace* face = pd->faces.push();
skip_to_numeric(ps);
face->v1 = strtol((const char*)ps->head, (char**)&ps->head, 10) - 1;
skip_to_numeric(ps);
face->u1 = strtol((const char*)ps->head, (char**)&ps->head, 10) - 1;
skip_to_numeric(ps);
face->n1 = strtol((const char*)ps->head, (char**)&ps->head, 10) - 1;
skip_to_numeric(ps);
face->v2 = strtol((const char*)ps->head, (char**)&ps->head, 10) - 1;
skip_to_numeric(ps);
face->u2 = strtol((const char*)ps->head, (char**)&ps->head, 10) - 1;
skip_to_numeric(ps);
face->n2 = strtol((const char*)ps->head, (char**)&ps->head, 10) - 1;
skip_to_numeric(ps);
face->v3 = strtol((const char*)ps->head, (char**)&ps->head, 10) - 1;
skip_to_numeric(ps);
face->u3 = strtol((const char*)ps->head, (char**)&ps->head, 10) - 1;
skip_to_numeric(ps);
face->n3 = strtol((const char*)ps->head, (char**)&ps->head, 10) - 1;
}
static void skip_line(ParserState* ps)
{
while (ps->head < ps->end && *ps->head != '\n')
{
++ps->head;
}
++ps->head;
}
static ParsedData parse(Allocator* alloc, unsigned char* data, unsigned data_size)
{
ParserState ps = {};
ps.data = data;
ps.head = data;
ps.end = (unsigned char*)mem_ptr_add(data, data_size);
ParsedData pd = {};
pd.vertices = dynamic_array_create<Vector3>(alloc);
pd.normals = dynamic_array_create<Vector3>(alloc);
pd.uvs = dynamic_array_create<Vector2>(alloc);
pd.faces = dynamic_array_create<ParsedFace>(alloc);
ps.head = ps.data;
while (ps.head < ps.end)
{
unsigned char c = *ps.head;
bool first_on_line = ps.head == ps.data || (ps.head > ps.data && (*(ps.head - 1)) == '\n');
if (!first_on_line)
skip_line(&ps);
else if (c == 'v' && ps.head + 1 < ps.end && (*(ps.head+1)) == 't')
parse_uv(&ps, &pd);
else if (c == 'v' && ps.head + 1 < ps.end && (*(ps.head+1)) == 'n')
parse_normal(&ps, &pd);
else if (c == 'v')
parse_vertex(&ps, &pd);
else if (c == 'f')
parse_face(&ps, &pd);
else
skip_line(&ps);
}
return pd;
}
static int get_existing_vertex(const DynamicArray<Vertex>& vertices, const Vertex& v1)
{
for (unsigned i = 0; i < vertices.num; ++i)
{
const Vertex& v2 = vertices[i];
if (almost_equal(v1.position, v2.position)
&& almost_equal(v1.normal, v2.normal)
&& almost_equal(v1.uv, v2.uv)
&& almost_equal(v1.color, v2.color))
{
return i;
}
}
return -1;
}
static void add_vertex_to_mesh(Mesh* m, const Vector3& pos, const Vector3& normal, const Vector2& uv, const Color& c)
{
Vertex v = {};
v.position = pos;
v.normal = normal;
v.uv = uv;
v.color = c;
int i = get_existing_vertex(m->vertices, v);
if (i != -1)
{
m->indices.add(i);
return;
}
m->indices.add(m->vertices.num);
m->vertices.add(v);
}
LoadedMesh obj_load(Allocator* alloc, const char* filename)
{
LoadedFile lf = file_load(alloc, filename);
if (!lf.valid)
return {false};
ParsedData pd = parse(alloc, lf.file.data, lf.file.size);
Mesh m = {};
m.vertices = dynamic_array_create<Vertex>(alloc);
m.indices = dynamic_array_create<unsigned>(alloc);
for (unsigned i = 0; i < pd.faces.num; ++i)
{
const ParsedFace& f = pd.faces[i];
add_vertex_to_mesh(&m, pd.vertices[f.v1], pd.normals[f.n1], pd.uvs[f.u1], {1.0f, 0.0f, 1.0f, 1.0f});
add_vertex_to_mesh(&m, pd.vertices[f.v2], pd.normals[f.n2], pd.uvs[f.u2], {1.0f, 0.0f, 1.0f, 1.0f});
add_vertex_to_mesh(&m, pd.vertices[f.v3], pd.normals[f.n3], pd.uvs[f.u3], {1.0f, 0.0f, 1.0f, 1.0f});
}
return {true, m};
}