| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| #version 430 |
|
|
| |
| uniform mat4 view_projection_matrix; |
|
|
| |
| struct Material |
| { |
| |
| vec4 ambient; |
|
|
| |
| |
| |
| vec4 diffuse_and_texture; |
|
|
|
|
| |
| |
| vec4 specular_shininess; |
| }; |
|
|
| |
| |
| uniform bool has_normals = false; |
|
|
| |
| |
| uniform bool has_texcoords = false; |
|
|
| |
| uniform bool has_vertex_colors = false; |
|
|
| |
| uniform bool cull_backfacing = true; |
|
|
| |
| |
| |
| layout(binding=0) buffer mesh { float mesh_buffer[]; }; |
|
|
| |
| |
| layout(binding=1) buffer normals { float normal_buffer[]; }; |
|
|
| |
| |
| layout(binding=2) buffer vertex_colors { float vertex_colors_buffer[]; }; |
|
|
| |
| |
| layout(binding=3) buffer texcoords { float texcoord_buffer[]; }; |
|
|
| |
| |
| layout(binding=4) buffer material_ids { int material_id_buffer[]; }; |
|
|
| |
| layout(std430, binding=5) buffer materials { Material material_buffer[]; }; |
|
|
| |
|
|
| out layout(location = 0) vec3 position; |
| out layout(location = 1) vec3 normal; |
| out layout(location = 2) vec2 texcoord; |
| out layout(location = 3) vec3 out_vertex_color; |
| out layout(location = 4) float depth; |
| out layout(location = 6) flat Material material; |
|
|
| layout(points) in; |
| layout(triangle_strip, max_vertices=12) out; |
|
|
| vec3 get_position(int i) { |
| int o = gl_PrimitiveIDIn * 9 + i * 3; |
| return vec3(mesh_buffer[o + 0], mesh_buffer[o + 1], mesh_buffer[o + 2]); |
| } |
|
|
| vec3 get_normal(int i) { |
| int o = gl_PrimitiveIDIn * 9 + i * 3; |
| return normalize(vec3(normal_buffer[o + 0], normal_buffer[o + 1], normal_buffer[o + 2])); |
| } |
|
|
| vec3 get_vertex_color(int i) { |
| int o = gl_PrimitiveIDIn * 9 + i * 3; |
| if(!has_vertex_colors) return vec3(1.0, 1.0, 1.0); |
| return vec3(vertex_colors_buffer[o + 0], vertex_colors_buffer[o + 1], vertex_colors_buffer[o + 2]); |
| } |
|
|
| vec2 get_texcoord(int i) { |
| int o = gl_PrimitiveIDIn * 6 + i * 2; |
| return vec2(texcoord_buffer[o + 0], texcoord_buffer[o + 1]); |
| } |
|
|
| Material get_material(int i) { |
| if (i < 0) |
| { |
| Material default_material; |
| default_material.ambient = vec4(0.0); |
| default_material.diffuse_and_texture = vec4(0.8, 0.2, 0.8, -1.0); |
| default_material.specular_shininess = vec4(0.0); |
| return default_material; |
| } |
|
|
| return material_buffer[i]; |
| } |
|
|
| bool is_back_facing(vec3 v0, vec3 v1, vec3 v2) { |
| vec4 tv0 = view_projection_matrix * vec4(v0, 1.0); |
| vec4 tv1 = view_projection_matrix * vec4(v1, 1.0); |
| vec4 tv2 = view_projection_matrix * vec4(v2, 1.0); |
| tv0 /= tv0.w; |
| tv1 /= tv1.w; |
| tv2 /= tv2.w; |
| vec2 a = (tv1.xy - tv0.xy); |
| vec2 b = (tv2.xy - tv0.xy); |
| return (a.x * b.y - b.x * a.y) <= 0; |
| } |
|
|
| void main() { |
| vec3 v0 = get_position(0); |
| vec3 v1 = get_position(1); |
| vec3 v2 = get_position(2); |
|
|
| if (cull_backfacing && is_back_facing(v0, v1, v2)) { |
| return; |
| } |
|
|
| vec3 geometric_normal = normalize( |
| cross(normalize(v1 - v0), normalize(v2 - v0))); |
|
|
| material = material_buffer[material_id_buffer[gl_PrimitiveIDIn]]; |
|
|
| vec3 positions[3] = {v0, v1, v2}; |
| for (int i = 0; i < 3; i++) { |
| gl_Position = view_projection_matrix * vec4(positions[i], 1); |
| position = positions[i]; |
| normal = has_normals ? get_normal(i) : geometric_normal; |
| texcoord = has_texcoords ? get_texcoord(i) : vec2(0.0); |
| out_vertex_color = get_vertex_color(i); |
| depth = gl_Position.w; |
|
|
| EmitVertex(); |
| } |
| EndPrimitive(); |
| } |
|
|