Skip to content

Instantly share code, notes, and snippets.

@httpdigest httpdigest/cubeConvexHull.gs.glsl Secret
Last active Oct 13, 2018

Embed
What would you like to do?
Convex Hull Rendering of Cubes
#version 430 core
#if GL_NV_gpu_shader5
#extension GL_NV_gpu_shader5 : enable
#elif GL_EXT_shader_8bit_storage
#extension GL_EXT_shader_8bit_storage
#endif
layout(points) in;
layout(triangle_strip, max_vertices = 6) out;
uniform mat4 vpt;
uniform vec3 cameraPosition;
uniform float boxHalfSize = 0.5;
flat in vec3 voxelCenter_vg[];
flat in uint8_t flags_vg[];
flat out vec3 voxelCenter_gf;
flat out uint8_t flags_gf;
/* Table adapted from "Fast Projected Area Computation for Three-Dimensional Bounding Boxes" */
const int8_t table[] = int8_t[] (
0, 0, 0, 0, 0, 0, 0, // 0
4, 4, 0, 2, 6, 0, 0, // 1
4, 5, 7, 3, 1, 0, 0, // 2
0, 0, 0, 0, 0, 0, 0, // 3
4, 4, 5, 1, 0, 0, 0, // 4
6, 5, 4, 6, 2, 0, 1, // 5
6, 4, 5, 7, 3, 1, 0, // 6
0, 0, 0, 0, 0, 0, 0, // 7
4, 7, 6, 2, 3, 0, 0, // 8
6, 0, 2, 3, 7, 6, 4, // 9
6, 7, 6, 2, 3, 1, 5, // 10
0, 0, 0, 0, 0, 0, 0, // 11
0, 0, 0, 0, 0, 0, 0, // 12
0, 0, 0, 0, 0, 0, 0, // 13
0, 0, 0, 0, 0, 0, 0, // 14
0, 0, 0, 0, 0, 0, 0, // 15
4, 4, 6, 7, 5, 0, 0, // 16
6, 4, 0, 2, 6, 7, 5, // 17
6, 4, 6, 7, 3, 1, 5, // 18
0, 0, 0, 0, 0, 0, 0, // 19
6, 4, 6, 7, 5, 1, 0, // 20
6, 5, 1, 0, 2, 6, 7, // 21
6, 4, 6, 7, 3, 1, 0, // 22
0, 0, 0, 0, 0, 0, 0, // 23
6, 4, 6, 2, 3, 7, 5, // 24
6, 4, 0, 2, 3, 7, 5, // 25 // front, top, left
6, 4, 6, 2, 3, 1, 5, // 26
0, 0, 0, 0, 0, 0, 0, // 27
0, 0, 0, 0, 0, 0, 0, // 28
0, 0, 0, 0, 0, 0, 0, // 29
0, 0, 0, 0, 0, 0, 0, // 30
0, 0, 0, 0, 0, 0, 0, // 31
4, 0, 1, 3, 2, 0, 0, // 32
6, 0, 1, 3, 2, 6, 4, // 33
6, 5, 7, 3, 2, 0, 1, // 34
0, 0, 0, 0, 0, 0, 0, // 35
6, 4, 5, 1, 3, 2, 0, // 36
6, 4, 5, 1, 3, 2, 6, // 37
6, 4, 5, 7, 3, 2, 0, // 38
0, 0, 0, 0, 0, 0, 0, // 39
6, 7, 6, 2, 0, 1, 3, // 40 // back, top
6, 4, 0, 1, 3, 7, 6, // 41
6, 5, 7, 6, 2, 0, 1 // 42
);
const vec4 project(in const int i) {
const vec3 off = vec3((i & 1)<<1, (i>>1 & 1)<<1, (i>>2 & 1)<<1) - vec3(1.0);
vec4 p = vec4(voxelCenter_vg[0] + off * boxHalfSize, 1.0);
return vec4(dot(vpt[0], p), dot(vpt[1], p), 0.0, dot(vpt[3], p));
}
void main(void) {
voxelCenter_gf = voxelCenter_vg[0];
flags_gf = flags_vg[0];
const vec3 bmin = voxelCenter_gf - vec3(boxHalfSize);
const vec3 bmax = voxelCenter_gf + vec3(boxHalfSize);
const vec3 eye = cameraPosition;
const int pos = int8_t(eye.x < bmin.x) | int8_t(eye.x > bmax.x) << 1u
| int8_t(eye.y < bmin.y) << 2u | int8_t(eye.y > bmax.y) << 3u
| int8_t(eye.z > bmax.z) << 4u | int8_t(eye.z < bmin.z) << 5u;
const int off = 7 * pos;
const int num = table[off];
#define EMIT(IDX) gl_Position = project(table[off + IDX]); EmitVertex();
switch (num) {
case 4:
EMIT(2); EMIT(1); EMIT(3); EMIT(4);
break;
case 6:
EMIT(2); EMIT(1); EMIT(3); EMIT(6); EMIT(4); EMIT(5);
break;
}
#undef EMIT
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.