-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSignedDistanceFields.cpp
More file actions
273 lines (216 loc) · 7.28 KB
/
SignedDistanceFields.cpp
File metadata and controls
273 lines (216 loc) · 7.28 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
#include "SignedDistanceFields.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;
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 GLuint VoxelsToTexture(VoxelSet & voxels) {
int totalSize = voxels.size.x * voxels.size.y * voxels.size.z * 4;
vector<uint8_t> colors(totalSize);
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 colorIdx = z * voxels.size.x * voxels.size.y
+ y * voxels.size.x
+ x;
colorIdx *= 4;
vec4 c = voxels.At(idx);
colors[colorIdx + 0] = RoundByteF(c.r);
colors[colorIdx + 1] = RoundByteF(c.g);
colors[colorIdx + 2] = RoundByteF(c.b);
if (voxels.IsSolid(idx)) {
colors[colorIdx + 3] = 255;
} else {
colors[colorIdx + 3] = 0;
}
}
}
}
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_3D, tex);
glTexImage3D(GL_TEXTURE_3D,
0,
GL_RGBA8,
voxels.size.x,
voxels.size.y,
voxels.size.z,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
&colors[0]);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
CheckGLErrors();
return tex;
}
PerfRecord RunSdfTest(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;
vector<GLuint> textures;
vector<vec3> offsets;
PerfRecord record = RunPerf(
[&]() {
// Setup
program = MakeShaderProgram({
{ "Shaders/sdf.vert", GL_VERTEX_SHADER },
{ "Shaders/sdf.frag", GL_FRAGMENT_SHADER },
});
mvpLoc = glGetUniformLocation(program, "mvp");
mvInvLoc = glGetUniformLocation(program, "mvInv");
mvLoc = glGetUniformLocation(program, "mv");
nearDimLoc = glGetUniformLocation(program, "nearPlaneDim");
nearDistLoc = glGetUniformLocation(program, "nearPlaneDist");
offsetLoc = glGetUniformLocation(program, "offset");
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;
offsets.resize(totalModels);
textures.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);
vec3 offset = vec3(idx) * voxelSpacing;
offset -= vec3(0, gridSize.y, 0) * voxelSpacing / 2.0f;
offsets[nextOffsetIdx] = offset;
textures[nextOffsetIdx] = VoxelsToTexture(model);
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);
for (int i = 0; i < offsets.size(); ++i) {
vec3 offset = offsets[i];
glUniform3fv(offsetLoc, 1, (const GLfloat*)&offset);
glBindVertexArray(cubeVao);
glBindTexture(GL_TEXTURE_3D, textures[i]);
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;
}