-
Notifications
You must be signed in to change notification settings - Fork 0
/
rendirt.cpp
executable file
·379 lines (306 loc) · 12.8 KB
/
rendirt.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
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
/**
* MIT License
*
* Copyright (c) 2018 Fabio Massaioli
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#define GLM_ENABLE_EXPERIMENTAL
#ifdef NDEBUG
#define GLM_FORCE_INLINE
#endif
#include "rendirt.hpp"
#include <glm/gtc/matrix_access.hpp>
#include <glm/gtx/normal.hpp>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <limits>
#include <numeric>
using namespace rendirt;
// Model methods
void Model::updateBoundingBox() {
if (empty())
boundingBox_ = { { 0, 0, 0 }, { 0, 0, 0 } };
else
boundingBox_ = std::accumulate(begin(), end(),
AABB{
glm::min(front().vertex[0], glm::min(front().vertex[1], front().vertex[2])),
glm::max(front().vertex[0], glm::max(front().vertex[1], front().vertex[2]))
}, [](AABB const& box, Face const& face) -> AABB {
return {
glm::min(glm::min(box.from, face.vertex[0]), glm::min(face.vertex[1], face.vertex[2])),
glm::max(glm::max(box.to, face.vertex[0]), glm::max(face.vertex[1], face.vertex[2]))
};
});
}
namespace {
// STL format parsing helpers
size_t skipWhitespace(std::istream& stream, size_t limit = -1) {
size_t count = 0;
while (std::isspace(stream.peek()) && count < limit) {
stream.ignore();
++count;
}
return count;
}
} /* namespace */
Model::Error Model::loadTextSTL(std::istream& stream, bool useNormals, bool verified) {
std::string tok;
clear();
stream >> std::skipws;
if (!verified) {
// Read and verify 'solid' signature
stream >> tok;
if (!stream) // If error or eof, we have a problem
return FileTruncated;
else if (tok != "solid")
return UnexpectedToken;
}
// Read (and ignore) model name and comment line
stream.ignore(std::numeric_limits<std::streamsize>::max(), stream.widen('\n'));
if (!stream)
return FileTruncated;
stream >> tok;
if (stream.fail())
return FileTruncated;
Face face = {};
for (bool first = true; tok == "facet";) {
// Read normal
stream >> tok;
if (!stream)
return FileTruncated;
else if (tok != "normal")
return UnexpectedToken;
stream >> face.normal.x
>> face.normal.y
>> face.normal.z;
if (stream.eof())
return FileTruncated;
else if (!stream)
return InvalidToken;
// Read vertices
stream >> tok;
if (!stream)
return FileTruncated;
else if (tok != "outer")
return UnexpectedToken;
stream >> tok;
if (!stream)
return FileTruncated;
else if (tok != "loop")
return UnexpectedToken;
for (unsigned int i = 0; i < 3; ++i) {
stream >> tok;
if (!stream)
return FileTruncated;
else if (tok != "vertex")
return UnexpectedToken;
stream >> face.vertex[i].x
>> face.vertex[i].y
>> face.vertex[i].z;
if (stream.eof())
return FileTruncated;
else if (!stream)
return InvalidToken;
}
stream >> tok;
if (!stream)
return FileTruncated;
else if (tok != "endloop")
return UnexpectedToken;
stream >> tok;
if (!stream)
return FileTruncated;
else if (tok != "endfacet")
return UnexpectedToken;
// Recompute normal (some programs are known to write garbage)
if (!useNormals)
face.normal = glm::triangleNormal(face.vertex[0], face.vertex[1], face.vertex[2]);
push_back(face);
// Update bounding box
if (first) {
first = false;
boundingBox_ = {
glm::min(face.vertex[0], glm::min(face.vertex[1], face.vertex[2])),
glm::max(face.vertex[0], glm::max(face.vertex[1], face.vertex[2]))
};
}
boundingBox_ = {
glm::min(glm::min(boundingBox_.from, face.vertex[0]), glm::min(face.vertex[1], face.vertex[2])),
glm::max(glm::max(boundingBox_.to, face.vertex[0]), glm::max(face.vertex[1], face.vertex[2]))
};
// Read next face or end of model
stream >> tok;
if (stream.fail())
return FileTruncated;
}
if (tok != "endsolid")
return tok.empty() ? FileTruncated : UnexpectedToken;
shrink_to_fit();
return Ok;
}
Model::Error Model::loadBinarySTL(std::istream& stream, bool useNormals, size_t skipped) {
// Reassign to free excess memory
*this = Model();
// Skip header
stream.ignore(80 - skipped);
// Read size (32 bit unsigned integer)
uint32_t size = 0;
stream.read(reinterpret_cast<char*>(&size), sizeof(uint32_t));
if (stream.gcount() < std::streamsize(sizeof(uint32_t)))
return FileTruncated;
reserve(size);
resize(size);
auto first = begin(), last = end();
uint16_t attrs = 0;
for (auto face = first; face != last; ++face) {
stream.read(reinterpret_cast<char*>(&*face), sizeof(Face));
if (stream.gcount() < std::streamsize(sizeof(Face)))
return FileTruncated;
// Ignore attrs: they should be zero, some programs use them
// as color values
stream.read(reinterpret_cast<char*>(&attrs), sizeof(uint16_t));
if (stream.gcount() < std::streamsize(sizeof(uint16_t)))
return FileTruncated;
// Recompute normal (some programs are known to write garbage)
if (!useNormals)
face->normal = glm::triangleNormal(face->vertex[0], face->vertex[1], face->vertex[2]);
// Update bounding box
if (face == first)
boundingBox_ = {
glm::min(face->vertex[0], glm::min(face->vertex[1], face->vertex[2])),
glm::max(face->vertex[0], glm::max(face->vertex[1], face->vertex[2]))
};
boundingBox_ = {
glm::min(glm::min(boundingBox_.from, face->vertex[0]), glm::min(face->vertex[1], face->vertex[2])),
glm::max(glm::max(boundingBox_.to, face->vertex[0]), glm::max(face->vertex[1], face->vertex[2]))
};
}
return Ok;
}
Model::Error Model::loadSTL(std::istream& stream, bool useNormals, Mode mode) {
size_t skipped = 0;
if (mode == Guess) {
char signature[6];
skipped += skipWhitespace(stream, 80);
if (skipped == 80)
return GuessFailed;
size_t available = glm::min(size_t(6), 80 - skipped);
stream.read(signature, available);
if (stream.gcount() < std::streamsize(available))
return FileTruncated;
skipped += available;
if (available == 6 && std::equal(signature, signature + 5, "solid") && std::isspace(signature[5]))
mode = Text;
else if (available < 6 && std::equal(signature, signature + available, "solid"))
return GuessFailed;
else
mode = Binary;
}
return (mode == Text) ? loadTextSTL(stream, useNormals, skipped > 0)
: loadBinarySTL(stream, useNormals, skipped);
}
char const* Model::errorString(Error err) {
static char const* strings[LastError + 1] = {
"no error",
"invalid token",
"unexpected token",
"file truncated",
"guess failed"
};
return strings[(unsigned int) err];
}
// Renderer
size_t rendirt::render(Image<Color> const& color, Image<float> const& depth,
Model const& model, glm::mat4 const& modelViewProj,
Shader const& shader, CullingMode cullingMode)
{
assert(color.width == depth.width && color.height == depth.height);
size_t faceCount = 0;
using vec2s = glm::vec<2, size_t>;
const vec2s imgSize(color.width, color.height);
const glm::vec2 imgSizef(imgSize);
glm::vec2 sampleStep = glm::vec2(2.0f, -2.0f)/imgSizef;
for (auto const& face: model) {
glm::vec4 clipf[3] = {
modelViewProj * glm::vec4(face.vertex[0], 1.0f),
modelViewProj * glm::vec4(face.vertex[1], 1.0f),
modelViewProj * glm::vec4(face.vertex[2], 1.0f)
};
clipf[0] /= clipf[0].w; clipf[1] /= clipf[1].w; clipf[2] /= clipf[2].w;
// Face culling by winding detection
const float doubleArea = ((clipf[0].y - clipf[1].y)*clipf[2].x + (clipf[1].x - clipf[0].x)*clipf[2].y + (clipf[0].x*clipf[1].y - clipf[0].y*clipf[1].x));
if ((cullingMode == CullCW && doubleArea <= 0.0f) || (cullingMode == CullCCW && doubleArea > 0.0f))
continue;
AABB brect = {
glm::min(clipf[0], glm::min(clipf[1], clipf[2])),
glm::max(clipf[0], glm::max(clipf[1], clipf[2]))
};
brect.from = glm::max(brect.from, glm::vec3(-1.0f, -1.0f, -1.0f));
brect.to = glm::min(brect.to, glm::vec3(1.0f, 1.0f, 1.0f));
const auto dims = glm::abs(brect.to - brect.from);
// Discard faces outside clipping planes
if (dims.x <= 0.0f || dims.y <= 0.0f || brect.from.z >= 1.0f || brect.to.z <= -1.0f)
continue;
++faceCount;
const vec2s from =
glm::clamp(vec2s(glm::floor((glm::vec2(brect.from.x, -brect.to.y)*0.5f + 0.5f)*imgSizef)), vec2s(0, 0), imgSize);
const vec2s to =
glm::clamp(vec2s(glm::ceil((glm::vec2(brect.to.x, -brect.from.y)*0.5f + 0.5f)*imgSizef)), vec2s(0, 0), imgSize);
// Matrix for computing barycentric coordinates normalized so their sum is 1
// XXX: column-major
const glm::mat3 barycentric = glm::mat3{
{ clipf[1].y - clipf[2].y, clipf[2].y - clipf[0].y, clipf[0].y - clipf[1].y },
{ clipf[2].x - clipf[1].x, clipf[0].x - clipf[2].x, clipf[1].x - clipf[0].x },
{ clipf[1].x*clipf[2].y - clipf[1].y*clipf[2].x, clipf[2].x*clipf[0].y - clipf[2].y*clipf[0].x, clipf[0].x*clipf[1].y - clipf[0].y*clipf[1].x },
} / doubleArea;
const glm::vec3 posParams[3] = {
face.vertex[0],
face.vertex[1] - face.vertex[0],
face.vertex[2] - face.vertex[0]
};
const glm::vec3 zParams(clipf[0].z, clipf[1].z - clipf[0].z, clipf[2].z - clipf[0].z);
const glm::vec2 sampleStart = (glm::vec2(from.x + 0.5f, from.y + 0.5f)/imgSizef - 0.5f) * glm::vec2(2.0f, -2.0f);
glm::vec2 sample = sampleStart;
glm::vec3 rowLambda = barycentric * glm::vec3(sampleStart, 1.0f);
glm::vec3 lambda = rowLambda;
const glm::vec3 rowLambdaStep = barycentric[1]*sampleStep.y;
const glm::vec3 lambdaStep = barycentric[0]*sampleStep.x;
for (size_t y = from.y; y < to.y; ++y, sample.y += sampleStep.y, rowLambda += rowLambdaStep) {
sample.x = sampleStart.x;
lambda = rowLambda;
for (size_t x = from.x; x < to.x; ++x, sample.x += sampleStep.x, lambda += lambdaStep) {
// Interpolate position and depth
const glm::vec3 pos = posParams[0] + lambda.y*posParams[1] + lambda.z*posParams[2];
const float z = zParams.x + lambda.y*zParams.y + lambda.z*zParams.z;
// Test if inside triangle, then depth test
if (!(std::signbit(lambda.x) | std::signbit(lambda.y) | std::signbit(lambda.z)) &&
z > -1.0f && z < depth.buffer[y*depth.stride + x])
{
depth.buffer[y*depth.stride + x] = z;
color.buffer[y*color.stride + x] = shader(glm::vec3(sample, z), pos, face.normal);
}
}
}
}
return faceCount;
}