-
Notifications
You must be signed in to change notification settings - Fork 0
/
wavefront.cpp
284 lines (233 loc) · 6.5 KB
/
wavefront.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
#include "wavefront.h"
#include "fast_float.h"
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include "fastvector.h"
using namespace DirectX::SimpleMath;
unsigned long fast_atoi(const char* str)
{
unsigned long val = 0;
while (*str)
{
val = (val << 1) + (val << 3) + (*str - '0');
str++;
}
return val;
}
bool readFile(const char * const filename, char*& buffer, size_t& buffersize)
{
FILE* f = nullptr;
if (fopen_s(&f, filename, "rb") || !f)
return false;
if (fseek(f, 0, SEEK_END))
{
fclose(f); // NOLINT(cert-err33-c)
return false;
}
buffersize = ftell(f);
rewind(f);
buffer = new char[buffersize];
if (!buffer)
{
fclose(f); // NOLINT(cert-err33-c)
return false;
}
if (fread(buffer, 1, buffersize, f) != buffersize)
{
delete[] buffer;
buffer = nullptr;
buffersize = 0;
fclose(f); // NOLINT(cert-err33-c)
return false;
}
fclose(f); // NOLINT(cert-err33-c)
return true;
}
size_t getToken(const char* const buffer, const size_t size, const size_t start, char* token, const char separator = 0)
{
size_t stop = start;
while (stop < size)
{
if (buffer[stop] == separator || buffer[stop] == ' ' || buffer[stop] == '\r' || buffer[stop] == '\n')
break;
token[stop - start] = buffer[stop];
stop++;
}
token[stop - start] = 0;
return stop - start;
}
size_t seekEndLine(const char* const buffer, const size_t size, const size_t start)
{
size_t stop = start;
while (stop < size)
{
if (buffer[stop] == '\n')
break;
stop++;
}
return stop;
}
void calculateBoundingSphere(const FastVector<Vector3>& points, Vector4& sphere)
{
Vector3 center{ 0, 0, 0 };
for (const auto& point : points)
center += point;
center /= static_cast<float>(points.size());
float radius = 0;
for (const auto& point : points)
{
const Vector3 v = point - center;
const float distSq = v.LengthSquared();
if (distSq > radius)
radius = distSq;
}
radius = sqrtf(radius);
sphere.x = center.x;
sphere.y = center.y;
sphere.z = center.z;
sphere.w = radius;
}
void calculateTangents(TbnVertex& a, TbnVertex& b, TbnVertex& c)
{
const Vector3 v = b.position - a.position;
const Vector3 w = c.position - a.position;
float sx = b.texcoord.x - a.texcoord.x, sy = b.texcoord.y - a.texcoord.y;
float tx = c.texcoord.x - a.texcoord.x, ty = c.texcoord.y - a.texcoord.y;
const float dirCorrection = (tx * sy - ty * sx) < 0.0f ? -1.0f : 1.0f;
if (sx * ty == sy * tx) // NOLINT(clang-diagnostic-float-equal)
{
sx = 0.0;
sy = 1.0;
tx = 1.0;
ty = 0.0;
}
const Vector3 tangent
{
(w.x * sy - v.x * ty) * dirCorrection,
(w.y * sy - v.y * ty) * dirCorrection,
(w.z * sy - v.z * ty) * dirCorrection
};
const Vector3 bitangent
{
(w.x * sx - v.x * tx) * dirCorrection,
(w.y * sx - v.y * tx) * dirCorrection,
(w.z * sx - v.z * tx) * dirCorrection
};
a.tangent = tangent - a.normal * tangent.Dot(a.normal);
a.bitangent = bitangent - a.normal * bitangent.Dot(a.normal);
a.tangent.Normalize();
a.bitangent.Normalize();
b.tangent = tangent - b.normal * tangent.Dot(b.normal);
b.bitangent = bitangent - b.normal * bitangent.Dot(b.normal);
b.tangent.Normalize();
b.bitangent.Normalize();
c.tangent = tangent - c.normal * tangent.Dot(c.normal);
c.bitangent = bitangent - c.normal * bitangent.Dot(c.normal);
c.tangent.Normalize();
c.bitangent.Normalize();
}
template <class T>
bool loadTbnObject(const char* const filename, FastVector<TbnVertex>& vertices, FastVector<T>& indices, Vector4& sphere)
{
FastVector<Vector3> position;
FastVector<Vector3> normal;
FastVector<Vector2> texcoord;
char* buffer;
size_t size;
if (!readFile(filename, buffer, size))
return false;
for (size_t bufferIndex = 0; bufferIndex < size; bufferIndex++)
{
char token[32]; // max token size observed: 11
const size_t len = getToken(buffer, size, bufferIndex, token);
if (len == 1)
{
if (token[0] == 'f')
{
size_t offset = bufferIndex + len + 1;
auto vertexCount = static_cast<T>(vertices.size());
for (int n = 0; n < 3; n++)
{
assert(offset < size && buffer[offset] != '\r' && buffer[offset] != '\n');
size_t pointIndices[3];
for (size_t& index : pointIndices)
{
const size_t toklen = getToken(buffer, size, offset, token, '/');
assert(toklen != 0);
offset += toklen + 1;
const size_t i = fast_atoi(token);
index = i - 1;
}
assert(pointIndices[0] < position.size() && pointIndices[1] < texcoord.size() && pointIndices[2] < normal.size());
vertices.emplace_back(position[pointIndices[0]], normal[pointIndices[2]], Vector3::Zero, Vector3::Zero, texcoord[pointIndices[1]]);
}
indices.push_back(vertexCount);
indices.push_back(vertexCount + 2);
indices.push_back(vertexCount + 1);
}
else if (token[0] == 'v')
{
size_t offset = bufferIndex + len + 1;
float components[3];
for (float& component : components)
{
const size_t toklen = getToken(buffer, size, offset, token);
assert(toklen != 0);
offset += toklen + 1;
const auto result = fast_float::from_chars(token, token + toklen, component);
assert(result.ec == std::errc());
}
position.emplace_back(-components[0], components[1], components[2]);
}
}
else if (len == 2 && token[0] == 'v')
{
if (token[1] == 't')
{
size_t offset = bufferIndex + len + 1;
float components[2];
for (float& component : components)
{
const size_t toklen = getToken(buffer, size, offset, token);
assert(toklen != 0);
offset += toklen + 1;
const auto result = fast_float::from_chars(token, token + toklen, component);
assert(result.ec == std::errc());
}
texcoord.emplace_back(components[0], 1 - components[1]);
}
else if (token[1] == 'n')
{
size_t offset = bufferIndex + len + 1;
float components[3]{ 0, 0, 0 };
for (float& component : components)
{
const size_t toklen = getToken(buffer, size, offset, token);
assert(toklen != 0);
offset += toklen + 1;
const auto result = fast_float::from_chars(token, token + toklen, component);
assert(result.ec == std::errc());
}
Vector3 n =
{
-components[0],
components[1],
components[2],
};
n.Normalize();
normal.push_back(n);
}
}
bufferIndex = seekEndLine(buffer, size, bufferIndex);
}
calculateBoundingSphere(position, sphere);
for (size_t i = 0; i < indices.size() - 2; i += 3)
{
TbnVertex& a = vertices[indices[i]];
TbnVertex& b = vertices[indices[i + 1]];
TbnVertex& c = vertices[indices[i + 2]];
calculateTangents(a, b, c);
}
return true;
}