-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGeometryShader.cpp
More file actions
170 lines (137 loc) · 4.9 KB
/
GeometryShader.cpp
File metadata and controls
170 lines (137 loc) · 4.9 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
#include "GeometryShader.h"
#include <vector>
using namespace glm;
using namespace std;
#pragma pack(push, 1)
struct PointVertex {
vec3 position;
vec3 color;
uint8_t enabledFaces;
};
#pragma pack(pop)
// Buffers all voxels as GL_POINTS with face information
void BufferVoxelPoint(VoxelSet& voxels, vec3 offset, ivec3 idx, vector<PointVertex>& vertices, int& nextIdx) {
if (!voxels.IsSolid(idx)) {
return;
}
vec3 color = voxels.At(idx);
vec3 center = offset + vec3(idx) * VOXEL_SIZE + vec3(VOXEL_SIZE, VOXEL_SIZE, VOXEL_SIZE) / 2.0f;
vector<ivec3> normals = {
{ 1, 0, 0 },
{ -1, 0, 0 },
{ 0, 1, 0 },
{ 0,-1, 0 },
{ 0, 0, 1 },
{ 0, 0,-1 },
};
PointVertex p;
p.enabledFaces = 0;
for (int i = 0; i < normals.size(); ++i) {
if (voxels.IsSolid(idx + normals[i])) {
continue;
}
p.enabledFaces |= (1 << i);
}
if (p.enabledFaces == 0) {
return;
}
p.position = center;
p.color = color;
if (nextIdx >= vertices.size()) {
vertices.push_back(p);
} else {
vertices[nextIdx] = p;
}
nextIdx++;
}
// Buffers an entire voxel model in the given vector
void BufferVoxelSetPoints(VoxelSet& voxels, vec3 offset, vector<PointVertex>& vertices) {
int nextIdx = 0;
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) {
BufferVoxelPoint(voxels, offset, ivec3(x, y, z), vertices, nextIdx);
}
}
}
}
size_t MakeGridPoints(VoxelSet& model, ivec3 dimensions, vec3 spacing, std::vector<GLuint>& vaos,
std::vector<GLuint>& vbos, GLuint program) {
vbos.resize(dimensions.x * dimensions.y * dimensions.z);
vaos.resize(dimensions.x * dimensions.y * dimensions.z);
glGenBuffers(vbos.size(), &vbos[0]);
CheckGLErrors();
glGenVertexArrays(vaos.size(), &vaos[0]);
CheckGLErrors();
int nextVbo = 0;
vector<PointVertex> vertices;
for (int z = 0; z < dimensions.z; ++z) {
for (int y = 0; y < dimensions.y; ++y) {
for (int x = 0; x < dimensions.x; ++x) {
ivec3 idx(x, y, z);
vec3 offset = vec3(idx) * spacing;
offset -= vec3(0, dimensions.y, 0) * spacing / 2.0f;
BufferVoxelSetPoints(model, offset, vertices);
glBindVertexArray(vaos[nextVbo]);
glBindBuffer(GL_ARRAY_BUFFER, vbos[nextVbo]);
glBufferData(GL_ARRAY_BUFFER, sizeof(PointVertex) * vertices.size(), &vertices[0], GL_STATIC_DRAW);
GLint vPosLoc = glGetAttribLocation(program, "vPos");
glEnableVertexAttribArray(vPosLoc);
glVertexAttribPointer(vPosLoc, 3, GL_FLOAT, GL_FALSE,
sizeof(PointVertex), (void*)0);
CheckGLErrors();
GLint vColorPos = glGetAttribLocation(program, "vColor");
glEnableVertexAttribArray(vColorPos);
glVertexAttribPointer(vColorPos, 3, GL_FLOAT, GL_TRUE,
sizeof(PointVertex), (void*)(sizeof(float) * 3));
CheckGLErrors();
GLint vEnabledFaces = glGetAttribLocation(program, "vEnabledFaces");
glEnableVertexAttribArray(vEnabledFaces);
glVertexAttribIPointer(vEnabledFaces, 1, GL_UNSIGNED_BYTE,
sizeof(PointVertex), (void*)(sizeof(float) * 6));
CheckGLErrors();
nextVbo++;
}
}
}
return vertices.size();
}
PerfRecord RunGeometryShaderTest(VoxelSet & model, glm::ivec3 gridSize, glm::vec3 voxelSpacing) {
GLuint program;
GLint mvpLoc;
vector<GLuint> vaos;
vector<GLuint> vbos;
size_t vertexCount;
PerfRecord record = RunPerf(
[&]() {
// Setup
program = MakeShaderProgram({
{ "Shaders/point_voxels.vert", GL_VERTEX_SHADER },
{ "Shaders/point_voxels.frag", GL_FRAGMENT_SHADER },
{ "Shaders/point_voxels.geom", GL_GEOMETRY_SHADER },
});
mvpLoc = glGetUniformLocation(program, "mvp");
vertexCount = MakeGridPoints(model, gridSize, voxelSpacing, vaos, vbos, program);
},
[&]() {
// Draw
mat4 mvp = MakeMvp();
//PrintMatrix(mvp);
glUseProgram(program);
glUniformMatrix4fv(mvpLoc, 1, GL_FALSE, (const GLfloat*)&mvp);
for (GLuint vao : vaos) {
glBindVertexArray(vao);
glDrawArrays(GL_POINTS, 0, vertexCount);
}
},
[&]() {
// Teardown
glDeleteBuffers(vbos.size(), &vbos[0]);
CheckGLErrors();
glDeleteVertexArrays(vaos.size(), &vaos[0]);
CheckGLErrors();
glDeleteProgram(program);
CheckGLErrors();
});
return record;
}