-
Notifications
You must be signed in to change notification settings - Fork 4
/
SdfJump.cpp
383 lines (304 loc) · 9.44 KB
/
SdfJump.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
380
381
382
383
#include "SdfJump.h"
#include <array>
#include <iostream>
#include <cstddef>
#include <vector>
using namespace glm;
using namespace std;
#pragma pack(push, 1)
static struct SdfVertex {
SdfVertex(vec3 p) : position(p) {}
vec3 position;
};
#pragma pack(pop)
// Buffers a single face of a voxel in the given vector
static void BufferCubeFace(vec3 center, vec3 normal, float cubeWidth, vector<SdfVertex>& vertices) {
// Center of voxel to center of face
center += vec3(normal) * cubeWidth / 2.0f;
vec3 d1 = -vec3(normal.z, normal.x, normal.y);
vec3 d2 = -vec3(normal.y, normal.z, normal.x);
vector<vec2> weights = {
{ -1, -1 },
{ 1, -1 },
{ 1, 1 },
{ -1, 1 },
};
// Reverse winding order for negative normals
if (normal.x < 0 || normal.y < 0 || normal.z < 0) {
std::swap(d1, d2);
}
for (int i = 0; i < 4; ++i) {
vec2 w = weights[i];
SdfVertex v(center + (d1 * w.x + d2 * w.y) / 2.0f * cubeWidth);
vertices.push_back(v);
}
}
// Buffers all faces of a voxel in the given vector
static GLuint BufferCube(float cubeWidth, GLuint program) {
vec3 center = vec3(cubeWidth, cubeWidth, cubeWidth) / 2.0f;
std::vector<SdfVertex> vertices;
vector<ivec3> normals = {
{ 1, 0, 0 },
{ -1, 0, 0 },
{ 0, 1, 0 },
{ 0,-1, 0 },
{ 0, 0, 1 },
{ 0, 0,-1 },
};
for (auto& n : normals) {
BufferCubeFace(center, vec3(n), cubeWidth, vertices);
}
GLuint vao;
GLuint vbo;
glGenBuffers(1, &vbo);
CheckGLErrors();
glGenVertexArrays(1, &vao);
CheckGLErrors();
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(SdfVertex) * vertices.size(), &vertices[0], GL_STATIC_DRAW);
GLint vPosLoc = glGetAttribLocation(program, "vPos");
glEnableVertexAttribArray(vPosLoc);
glVertexAttribPointer(vPosLoc, 3, GL_FLOAT, GL_FALSE,
sizeof(SdfVertex),
(void*)offsetof(SdfVertex, position));
CheckGLErrors();
return vao;
}
static void MakeSdfQuad(VoxelSet& model, ivec3 dimensions, vec3 spacing, GLuint& vao,
GLuint& vbo, GLuint program) {
size_t modelCount = dimensions.x * dimensions.y * dimensions.z;
CheckGLErrors();
glGenBuffers(1, &vbo);
CheckGLErrors();
glGenVertexArrays(1, &vao);
CheckGLErrors();
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
array<SdfVertex, 4> vertices = { {
{ vec3(-1, -1, 0) },
{ vec3(1, -1, 0) },
{ vec3(1, 1, 0) },
{ vec3(-1, 1, 0) },
} };
glBufferData(GL_ARRAY_BUFFER, sizeof(SdfVertex) * vertices.size(), &vertices[0], GL_STATIC_DRAW);
GLint vPosLoc = glGetAttribLocation(program, "vPos");
glEnableVertexAttribArray(vPosLoc);
glVertexAttribPointer(vPosLoc, 3, GL_FLOAT, GL_FALSE,
sizeof(SdfVertex),
(void*)offsetof(SdfVertex, position));
CheckGLErrors();
}
static uint8_t RoundByteF(float f) {
return (uint8_t)round(f * 255.0f);
}
static bool IsEmpty(VoxelSet& voxels, ivec3 startIdx, ivec3 endIdx) {
for (int z = startIdx.z; z <= endIdx.z; ++z) {
for (int y = startIdx.y; y <= endIdx.y; ++y) {
for (int x = startIdx.x; x <= endIdx.x; ++x) {
ivec3 idx(x, y, z);
int shapeIdx = z * voxels.size.x * voxels.size.y
+ y * voxels.size.x
+ x;
if (voxels.IsSolid(idx)) {
return false;
}
}
}
}
return true;
}
static uint8_t FindMaxJump(VoxelSet& voxels, ivec3 idx) {
uint8_t jumpSize = 0;
while (IsEmpty(voxels, idx - ivec3(jumpSize + 1), idx + ivec3(jumpSize + 1)) && jumpSize < 32) {
jumpSize++;
}
return jumpSize;
}
static void MakeJumpTexture(VoxelSet& voxels, vector<uint8_t>& jumpTexture) {
int jumpIdx = 0;
int shapeSize = voxels.size.x * voxels.size.y * voxels.size.z;
jumpTexture.resize(shapeSize);
for (int z = 0; z < voxels.size.z; ++z) {
for (int y = 0; y < voxels.size.y; ++y) {
for (int x = 0; x < voxels.size.x; ++x) {
ivec3 idx(x, y, z);
int jumpIdx = z * voxels.size.x * voxels.size.y
+ y * voxels.size.x
+ x;
if (voxels.IsSolid(idx)) {
jumpTexture[jumpIdx] = 255;
} else {
jumpTexture[jumpIdx] = FindMaxJump(voxels, idx);
}
}
}
}
}
static void VoxelsToTexture(VoxelSet & voxels, GLuint& colorTexture, GLuint& shapeTexture, GLuint& colorSampler, GLuint& shapeSampler, vector<uint8_t>& shape) {
int colorsSize = voxels.size.x * voxels.size.y * voxels.size.z * 4;
int shapeSize = voxels.size.x * voxels.size.y * voxels.size.z;
vector<uint8_t> colors(colorsSize);
for (int z = 0; z < voxels.size.z; ++z) {
for (int y = 0; y < voxels.size.y; ++y) {
for (int x = 0; x < voxels.size.x; ++x) {
ivec3 idx(x, y, z);
int shapeIdx = z * voxels.size.x * voxels.size.y
+ y * voxels.size.x
+ x;
int colorIdx = shapeIdx * 3;
vec4 c = voxels.At(idx);
colors[colorIdx + 0] = RoundByteF(c.r);
colors[colorIdx + 1] = RoundByteF(c.g);
colors[colorIdx + 2] = RoundByteF(c.b);
}
}
}
// Color texture
glGenTextures(1, &colorTexture);
glBindTexture(GL_TEXTURE_3D, colorTexture);
glTexImage3D(GL_TEXTURE_3D,
0,
GL_RGB8,
voxels.size.x,
voxels.size.y,
voxels.size.z,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
&colors[0]);
glGenSamplers(1, &colorSampler);
glSamplerParameteri(colorSampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glSamplerParameteri(colorSampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glSamplerParameteri(colorSampler, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glSamplerParameteri(colorSampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glSamplerParameteri(colorSampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
CheckGLErrors();
// Shape texture
glGenTextures(1, &shapeTexture);
glBindTexture(GL_TEXTURE_3D, shapeTexture);
CheckGLErrors();
glTexImage3D(GL_TEXTURE_3D,
0,
GL_R8UI,
voxels.size.x,
voxels.size.y,
voxels.size.z,
0,
GL_RED_INTEGER,
GL_UNSIGNED_BYTE,
&shape[0]);
CheckGLErrors();
glGenSamplers(1, &shapeSampler);
glSamplerParameteri(shapeSampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glSamplerParameteri(shapeSampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glSamplerParameteri(shapeSampler, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glSamplerParameteri(shapeSampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glSamplerParameteri(shapeSampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
CheckGLErrors();
}
struct VoxObjInfo {
vec3 offset;
GLuint colorTexture;
GLuint shapeTexture;
GLuint colorSampler;
GLuint shapeSampler;
};
PerfRecord RunSdfJumpTest(VoxelSet & model, glm::ivec3 gridSize, glm::vec3 voxelSpacing) {
GLuint program;
GLint mvpLoc;
GLint mvInvLoc;
GLint mvLoc;
GLint nearDistLoc;
GLint nearDimLoc;
GLint offsetLoc;
GLuint vao;
GLuint vbo;
GLuint cubeVao;
GLuint colorTexLoc;
GLuint shapeTexLoc;
vector<VoxObjInfo> voxObjs;
PerfRecord record = RunPerf(
[&]() {
// Setup
vector<uint8_t> shape;
MakeJumpTexture(model, shape);
program = MakeShaderProgram({
{ "Shaders/sdf.vert", GL_VERTEX_SHADER },
{ "Shaders/sdf_jump.frag", GL_FRAGMENT_SHADER },
});
CheckGLErrors();
mvpLoc = glGetUniformLocation(program, "mvp");
CheckGLErrors();
mvInvLoc = glGetUniformLocation(program, "mvInv");
CheckGLErrors();
mvLoc = glGetUniformLocation(program, "mv");
CheckGLErrors();
// Assign texture slots
colorTexLoc = glGetUniformLocation(program, "voxelColor");
CheckGLErrors();
shapeTexLoc = glGetUniformLocation(program, "voxelShape");
CheckGLErrors();
nearDimLoc = glGetUniformLocation(program, "nearPlaneDim");
nearDistLoc = glGetUniformLocation(program, "nearPlaneDist");
offsetLoc = glGetUniformLocation(program, "offset");
CheckGLErrors();
MakeSdfQuad(model, gridSize, voxelSpacing, vao, vbo, program);
cubeVao = BufferCube(32.0f * VOXEL_SIZE, program);
int nextOffsetIdx = 0;
int totalModels = gridSize.x * gridSize.y * gridSize.z;
voxObjs.resize(totalModels);
for (int z = 0; z < gridSize.z; ++z) {
for (int y = 0; y < gridSize.y; ++y) {
for (int x = 0; x < gridSize.x; ++x) {
ivec3 idx(x, y, z);
voxObjs[nextOffsetIdx].offset = vec3(idx) * voxelSpacing;
voxObjs[nextOffsetIdx].offset -= vec3(0, gridSize.y, 0) * voxelSpacing / 2.0f;
//offsets[nextOffsetIdx] = offset;
VoxelsToTexture(model, voxObjs[nextOffsetIdx].colorTexture, voxObjs[nextOffsetIdx].shapeTexture,
voxObjs[nextOffsetIdx].colorSampler, voxObjs[nextOffsetIdx].shapeSampler, shape);
nextOffsetIdx++;
}
}
}
},
[&]() {
// Draw
mat4 mvp = MakeMvp();
mat4 mv = MakeModelView();
mat4 mvInv = glm::inverse(mv);
vec3 nearPlane = GetNearPlane();
glUseProgram(program);
glUniform2fv(nearDimLoc, 1, &nearPlane.x);
glUniform1fv(nearDistLoc, 1, &nearPlane.z);
glUniformMatrix4fv(mvpLoc, 1, GL_FALSE, (const GLfloat*)&mvp);
glUniformMatrix4fv(mvInvLoc, 1, GL_FALSE, (const GLfloat*)&mvInv);
glUniformMatrix4fv(mvLoc, 1, GL_FALSE, (const GLfloat*)&mv);
glUniform1i(colorTexLoc, 0);
glUniform1i(shapeTexLoc, 1);
for (int i = 0; i < voxObjs.size(); ++i) {
vec3 offset = voxObjs[i].offset;
glUniform3fv(offsetLoc, 1, (const GLfloat*)&offset);
glBindVertexArray(cubeVao);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_3D, voxObjs[i].colorTexture);
glBindSampler(0, voxObjs[i].colorSampler);
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_3D, voxObjs[i].shapeTexture);
glBindSampler(1, voxObjs[i].shapeSampler);
glDrawArrays(GL_QUADS, 0, 24);
}
},
[&]() {
// Teardown
glDeleteBuffers(1, &vbo);
CheckGLErrors();
glDeleteVertexArrays(1, &vao);
CheckGLErrors();
glDeleteProgram(program);
CheckGLErrors();
//glDeleteTextures(textures.size(), &textures[0]);
CheckGLErrors();
});
return record;
}