-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobjLoader.cpp
More file actions
412 lines (330 loc) · 8.86 KB
/
Copy pathobjLoader.cpp
File metadata and controls
412 lines (330 loc) · 8.86 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
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
#include "objLoader.h"
float _ParseInt(char* Buffer, int* Index, int Length)
{
int part = 0;
bool neg = false;
int ret;
while (*Index < Length && (Buffer[*Index] > '9' || Buffer[*Index] < '0') && Buffer[*Index] != '-')
(*Index)++;
// sign
if (Buffer[*Index] == '-')
{
neg = true;
(*Index)++;
}
// integer part
while (*Index < Length && !(Buffer[*Index] > '9' || Buffer[*Index] < '0'))
part = part * 10 + (Buffer[(*Index)++] - '0');
ret = neg ? (part * -1) : part;
return ret;
}
float _ParseFloat(char* Buffer, int* Index, int Length)
{
int part = 0;
bool neg = false;
float ret;
// find start
while (*Index < Length && (Buffer[*Index] < '0' || Buffer[*Index] > '9') && Buffer[*Index] != '-' && Buffer[*Index] != '.')
(*Index)++;
// sign
if (Buffer[*Index] == '-')
{
neg = true;
(*Index)++;
}
// integer part
while (*Index < Length && !(Buffer[*Index] > '9' || Buffer[*Index] < '0'))
part = part * 10 + (Buffer[(*Index)++] - '0');
ret = neg ? (float)(part * -1) : (float)part;
// float part
if (*Index < Length && Buffer[*Index] == '.')
{
(*Index)++;
double mul = 1;
part = 0;
while (*Index < Length && !(Buffer[*Index] > '9' || Buffer[*Index] < '0'))
{
part = part * 10 + (Buffer[*Index] - '0');
mul *= 10;
(*Index)++;
}
if (neg)
ret -= (float)part / (float)mul;
else
ret += (float)part / (float)mul;
}
// scientific part
if (*Index < Length && (Buffer[*Index] == 'e' || Buffer[*Index] == 'E'))
{
(*Index)++;
neg = (Buffer[*Index] == '-'); *Index++;
part = 0;
while (*Index < Length && !(Buffer[*Index] > '9' || Buffer[*Index] < '0'))
{
part = part * 10 + (Buffer[(*Index)++] - '0');
}
if (neg)
ret /= (float)pow(10.0, (double)part);
else
ret *= (float)pow(10.0, (double)part);
}
return ret;
}
struct HashNode
{
int vertId;
HashNode* next;
};
HashNode** hashMap = new HashNode*[64007];
HashNode* hashNodes = new HashNode[64007];
int hashNodeCount = 0;
vsVertex* verts = new vsVertex[64000];
vec3* tempVerts = new vec3[64000];
vec2* tempUVs = new vec2[64000];
vec3* tempNormals = new vec3[64000];
uint16_t* tris = new uint16_t[192000];
int tempVertCount = 0;
int tempUVCount = 0;
int tempNormalCount = 0;
int triCount = 0;
int vertCount = 0;
int _GetUniqueVert(char* Buffer, int* Index, int Length)
{
int vertId = _ParseInt(Buffer, Index, Length); (*Index)++;
int uvId = _ParseInt(Buffer, Index, Length); (*Index)++;
int normalId = _ParseInt(Buffer, Index, Length);
uint32_t hash = 0;
hash += ((int*)(tempVerts + (vertId - 1)))[0];
hash += ((int*)(tempVerts + (vertId - 1)))[1];
hash += ((int*)(tempVerts + (vertId - 1)))[2];
hash += ((int*)(tempUVs + (uvId - 1)))[0];
hash += ((int*)(tempUVs + (uvId - 1)))[1];
hash += ((int*)(tempNormals + (normalId - 1)))[0];
hash += ((int*)(tempNormals + (normalId - 1)))[1];
hash += ((int*)(tempNormals + (normalId - 1)))[2];
hash %= 64007;
// See if hash exists
int vertIndex = -1;
HashNode* next = hashMap[hash];
while (next != NULL)
{
if (verts[next->vertId].pos == tempVerts[vertId - 1] &&
verts[next->vertId].uv == tempUVs[uvId - 1] &&
verts[next->vertId].normal == tempNormals[normalId - 1])
{
vertIndex = next->vertId;
break;
}
else
{
next = next->next;
}
}
if (vertIndex == -1)
{
verts[vertCount].pos = tempVerts[vertId - 1];
verts[vertCount].uv = tempUVs[uvId - 1];
verts[vertCount].normal = tempNormals[normalId - 1];
HashNode* hashNode = &hashNodes[hashNodeCount++];
hashNode->next = hashMap[hash];
hashNode->vertId = vertCount;
hashMap[hash] = hashNode;
return vertCount++;
}
return vertIndex;
}
vsOBJModel CreateOBJ(const char* FileName, vec4 UVScaleBias)
{
vsOBJModel result = {};
tempVertCount = 0;
tempUVCount = 0;
tempNormalCount = 0;
triCount = 0;
vertCount = 0;
memset(hashMap, 0, sizeof(hashMap) * 64007);
double time = GetTime();
FILE* file = fopen(FileName, "rb");
fseek(file, 0, SEEK_END);
int fileLen = ftell(file);
fseek(file, 0, SEEK_SET);
char* fileBuffer = new char[fileLen + 1];
fread(fileBuffer, 1, fileLen, file);
fileBuffer[fileLen] = 0;
int idx = 0;
char c = fileBuffer[idx++];
while (c != 0)
{
if (c == 'v')
{
c = fileBuffer[idx++];
if (c == ' ')
{
vec3 attr;
attr.x = _ParseFloat(fileBuffer, &idx, fileLen); idx++;
attr.y = _ParseFloat(fileBuffer, &idx, fileLen); idx++;
attr.z = _ParseFloat(fileBuffer, &idx, fileLen); idx++;
c = fileBuffer[idx++];
if (attr.x == 0.0f) attr.x = 0.0f;
if (attr.y == 0.0f) attr.y = 0.0f;
if (attr.z == 0.0f) attr.z = 0.0f;
//attr.x = -attr.x;
tempVerts[tempVertCount++] = attr;
}
else if (c == 't')
{
vec2 attr;
attr.x = _ParseFloat(fileBuffer, &idx, fileLen); idx++;
attr.y = _ParseFloat(fileBuffer, &idx, fileLen); idx++;
c = fileBuffer[idx++];
if (attr.x == 0.0f) attr.x = 0.0f;
if (attr.y == 0.0f) attr.y = 0.0f;
tempUVs[tempUVCount++] = attr;
}
else if (c == 'n')
{
vec3 attr;
attr.x = _ParseFloat(fileBuffer, &idx, fileLen); idx++;
attr.y = _ParseFloat(fileBuffer, &idx, fileLen); idx++;
attr.z = _ParseFloat(fileBuffer, &idx, fileLen); idx++;
c = fileBuffer[idx++];
if (attr.x == 0.0f) attr.x = 0.0f;
if (attr.y == 0.0f) attr.y = 0.0f;
if (attr.z == 0.0f) attr.z = 0.0f;
tempNormals[tempNormalCount++] = attr;
}
}
else if (c == 'f')
{
c = fileBuffer[idx++];
int rootVertId = _GetUniqueVert(fileBuffer, &idx, fileLen);
int currVertId = _GetUniqueVert(fileBuffer, &idx, fileLen);
c = fileBuffer[idx++];
while (c == ' ')
{
int nextVertId = currVertId;
currVertId = _GetUniqueVert(fileBuffer, &idx, fileLen);
tris[triCount++] = rootVertId;
tris[triCount++] = currVertId;
tris[triCount++] = nextVertId;
c = fileBuffer[idx++];
}
}
else
{
while (c != '\n' && c != 0)
c = fileBuffer[idx++];
if (c == '\n')
c = fileBuffer[idx++];
}
}
delete[] fileBuffer;
fclose(file);
// TODO: The object just contains pointers to our permanent storage.
result.verts = verts;
result.indices = tris;
result.vertCount = vertCount;
result.indexCount = triCount;
CalculateTangents(&result);
for (int i = 0; i < vertCount; ++i)
{
verts[i].uv.y = 1.0f - verts[i].uv.y;
verts[i].uv *= vec2(UVScaleBias.x, UVScaleBias.y) / 131072.0f;
verts[i].uv += vec2(UVScaleBias.z, UVScaleBias.w) / 131072.0f;
}
time = GetTime() - time;
std::cout << "Loaded OBJ " << FileName << " in " << (time * 1000.0) << "ms Verts: " << vertCount << " Tris: " << (triCount / 3) << "\n";
/*
// Determine how good the hash table usage is.
int maxCollisions = 0;
int emptyBuckets = 0;
int entryCount[5] = {};
for (int i = 0; i < 64007; ++i)
{
if (hashMap[i] == NULL)
{
++emptyBuckets;
entryCount[0]++;
}
else
{
int entries = 0;
HashNode *next = hashMap[i];
while (next != NULL)
{
++entries;
next = next->next;
}
if (entries > maxCollisions)
maxCollisions = entries;
entryCount[entries]++;
}
}
std::cout << "Hash stats - Empty Buckets: " << emptyBuckets << " Max Entires: " << maxCollisions << "\n";
for (int i = 0; i < 5; ++i)
{
std::cout << "Entries[" << i << "]: " << entryCount[i] << "\n";
}
//*/
return result;
}
void CalculateTangents(vsOBJModel* Model)
{
double time = GetTime();
static vec3 tan1[64000];
static vec3 tan2[64000];
memset(tan1, 0, sizeof(tan1));
memset(tan2, 0, sizeof(tan2));
for (i32 i = 0; i < Model->indexCount; i += 3)
{
i32 i1 = Model->indices[i + 0];
i32 i2 = Model->indices[i + 1];
i32 i3 = Model->indices[i + 2];
vec3 v1 = Model->verts[i1].pos;
vec3 v2 = Model->verts[i2].pos;
vec3 v3 = Model->verts[i3].pos;
vec2 w1 = Model->verts[i1].uv;
vec2 w2 = Model->verts[i2].uv;
vec2 w3 = Model->verts[i3].uv;
f32 x1 = v2.x - v1.x;
f32 x2 = v3.x - v1.x;
f32 y1 = v2.y - v1.y;
f32 y2 = v3.y - v1.y;
f32 z1 = v2.z - v1.z;
f32 z2 = v3.z - v1.z;
f32 s1 = w2.x - w1.x;
f32 s2 = w3.x - w1.x;
f32 t1 = w2.y - w1.y;
f32 t2 = w3.y - w1.y;
f32 d = (s1 * t2 - s2 * t1);
f32 r = 0.0f;
if (d != 0)
r = 1.0f / d;
vec3 sdir = vec3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
vec3 tdir = vec3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);
tan1[i1] += sdir;
tan1[i2] += sdir;
tan1[i3] += sdir;
tan2[i1] += tdir;
tan2[i2] += tdir;
tan2[i3] += tdir;
}
for (i32 i = 0; i < Model->vertCount; ++i)
{
vec3 n = Model->verts[i].normal;
vec3 t = tan1[i];
// GS Orthonormalize
n = glm::normalize(n);
t = glm::normalize(t);
vec3 proj = n * glm::dot(t, n);
t = t - proj;
t = glm::normalize(t);
vec4 tangent;
tangent.x = t.x;
tangent.y = t.y;
tangent.z = t.z;
tangent.w = (glm::dot(glm::cross(n, t), tan2[i]) < 0.0f) ? -1.0f : 1.0f;
Model->verts[i].tangent = tangent;
}
time = GetTime() - time;
//std::cout << "Tangent time: " << (time * 1000.0) << "ms\n";
}