-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobj.cpp
290 lines (237 loc) · 7.15 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
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
#include <fstream>
#include <iostream>
#include <assert.h>
#include "obj.h"
#include "util.h"
using std::cout;
using std::endl;
ObjFile::ObjFile(std::ifstream &fin, const std::string &name)
{
this->name = name;
#define SKIP_WS(_f) _f>>std::ws
for (std::string line; SKIP_WS(fin), getline(fin, line); ) {
switch(line.at(0)) {
case '#':
case 's':
continue;
case 'g':
AddGroup(line.substr(2));
continue;
case 'v':
switch(line.at(1)) {
case ' ':
case '\t':
AddVertex(line.substr(2));
break;
case 'n':
AddNormal(line.substr(3));
break;
case 't':
AddTexcoord(line.substr(3));
break;
default:
throw StackException("Unexpected 'V' line (" + line + ")");
}
continue;
case 'f':
AddFace(line.substr(2));
continue;
case 'u':
if (line.find("usemtl") == 0)
SetMaterial(line.substr(7));
else
throw StackException("Unexpected 'u' line (" + line + ")");
break;
default:
throw StackException("Unexpected line (" + line + ")");
}
}
}
static inline bool
is_num(char c) { return isdigit(c) || (c=='-'); }
static std::tuple<int, int, int, char*> read_group(char *str) {
int v0, n0, c0;
v0=n0=c0 = 0;
v0 = strtol(str, &str, 10);
if ((*str++) == '/') {
if (is_num(*str)) {
c0 = strtol(str, &str, 10);
}
if ((*str++) == '/') {
if (is_num(*str)) {
n0 = strtol(str, &str, 10);
}
}
}
return std::tuple<int, int, int, char*>(v0, c0, n0, str);
}
int
ObjFile::PushVertex(int pos_idx, int tc_idx, int norm_idx)
{
assert(pos_idx != 0);
if (pos_idx < 0)
pos_idx = positions.size() + pos_idx;
else
pos_idx -= 1;
Vertex v(positions.at(pos_idx));
if (tc_idx != 0) {
if (tc_idx < 0)
tc_idx = coords.size() + tc_idx;
else
tc_idx -= 1;
v.SetTex(coords.at(tc_idx));
}
if (norm_idx != 0) {
if (norm_idx < 0)
norm_idx = normals.size() + norm_idx;
else
norm_idx -= 1;
v.SetNormal(normals.at(norm_idx));
}
groups.back().count++;
return vc.feed(v);
}
void ObjFile::AddFace(const std::string &line)
{
char* str = (char*)line.c_str();
int pos, tc, norm;
int first, last;
std::tie(pos, tc, norm, str) = read_group(str);
first = PushVertex(pos, tc, norm);
std::tie(pos, tc, norm, str) = read_group(str);
last = PushVertex(pos, tc, norm);
std::tie(pos, tc, norm, str) = read_group(str);
int current = PushVertex(pos, tc, norm);
(void)first;
(void)last;
(void)current;
do {
std::tie(pos, tc, norm, str) = read_group(str);
if (pos == 0) break;
// todo: trianglulate faces with more points
//printf("TODO: BIGGER FACES\n");
} while (pos != 0);
}
void ObjFile::AddVertex(const std::string &line)
{
char* end = (char*)line.c_str();
float x = strtof( end, &end );
float y = strtof( end, &end );
float z = strtof( end, &end );
positions.push_back(float3(x,y,z));
}
void ObjFile::AddNormal(const std::string &line)
{
char* end = (char*)line.c_str();
float x = strtof( end, &end );
float y = strtof( end, &end );
float z = strtof( end, &end );
normals.push_back(float3(x,y,z));
}
void ObjFile::AddTexcoord(const std::string &line)
{
char* end = (char*)line.c_str();
float s = strtof( end, &end );
float t = strtof( end, &end );
coords.push_back(float2(s,t));
}
void ObjFile::toASCII(std::ofstream &fout)
{
int i = 0;
for (auto p: vc.vertices) {
fout << "Vertex: "<< ++i << std::endl;
fout << " p: "<< p.pos << std::endl;
fout << " t: "<< p.tc << std::endl;
fout << " n: "<< p.normal << std::endl;
};
fout << std::endl;
fout << "Indices:" << std::endl;
i=0;
for (auto x: vc.indices) {
i++;
fout << " V" << x;
if (i & ((i%3)==0)) fout<<std::endl;
else fout<<"\t";
}
}
static inline void
do_write(std::ofstream &fp, std::string str, int len)
{
str.resize(len, '\0');
fp.write(str.c_str(), len);
}
template<typename T>
static inline void
do_write(std::ofstream &fp, T val)
{
auto ptr = reinterpret_cast<const char *>(&val);
fp.write(ptr, sizeof(val));
}
static inline void
do_write(std::ofstream &fp, float3 &t)
{
do_write<float>(fp, t.x);
do_write<float>(fp, t.y);
do_write<float>(fp, t.z);
}
void ObjFile::toBin(std::ofstream &fout)
{
const uint32_t magic = 0x0B1EC701;
const uint16_t major = 0x0002;
const uint16_t minor = 0x0000;
const uint32_t index_type = vc.index_type();
const uint32_t index_count = vc.index_count();
// const int floats_per_vert = 8; // pos(x,y,z) + norm(x,y,z) + tex(s,t) = 3 + 3 + 2 = 8
const int floats_per_vert = 6; // pos(x,y,z) + norm(x,y,z) = 3 + 3 = 6
const uint32_t buffsize = vc.vertices.size() * floats_per_vert * sizeof(float);
const uint32_t num_attrs = 2;
const uint32_t num_groups = groups.size();
// header
do_write<uint32_t>(fout, magic);
do_write<uint16_t>(fout, major);
do_write<uint16_t>(fout, minor);
do_write<uint32_t>(fout, index_type);
do_write<uint32_t>(fout, index_count);
do_write<uint32_t>(fout, buffsize);
do_write(fout, name, 16);
do_write<uint32_t>(fout, num_attrs);
do_write<uint32_t>(fout, num_groups);
//const uint32_t float1_size = 1 * sizeof(float);
const uint32_t float3_size = 3 * sizeof(float);
// Attr1: Position
do_write(fout, "position", 16);
do_write<uint32_t>(fout, 2*float3_size); // stride
do_write<uint32_t>(fout, 0); // offset
do_write<uint32_t>(fout, GL_FLOAT); // elem_type
do_write<uint32_t>(fout, 3); // elem_count
// Attr2: Normal
do_write(fout, "colour", 16);
do_write<uint32_t>(fout, 2*float3_size); // stride
do_write<uint32_t>(fout, float3_size); // offset
do_write<uint32_t>(fout, GL_FLOAT); // elem_type
do_write<uint32_t>(fout, 3); // elem_count
// Groups
for(auto g: groups) {
do_write(fout, g.name, 16);
do_write<uint16_t>(fout, g.base_idx);
do_write<uint16_t>(fout, g.count);
}
if (index_type==GL_UNSIGNED_INT) {
for (auto i: vc.indices)
do_write<uint32_t>(fout, i);
}
else if (index_type==GL_UNSIGNED_SHORT) {
for (auto i: vc.indices)
do_write<uint16_t>(fout, i);
}
else if (index_type==GL_UNSIGNED_BYTE) {
for (auto i: vc.indices)
do_write<uint8_t>(fout, i);
}
else assert(!"Unexpected index type");
for (auto v: vc.vertices) {
do_write(fout, v.pos);
do_write(fout, v.normal);
// do_write(fout, v.tc);
}
}