I have a set of points, which I triangulate to a Delaunay graph to make a mesh.
Every mesh vertex has a unique colour, and I can render it without any problem. But I want Voronoi rendering to be possible too. Ideally, I want to make a shader which computes the distance from every vertex, and colours the vertex fragments with the colour of the nearest vertex, but somehow I get the wrong result.
This is the GLSL shader I'm using:
Vertex:
#version 330
layout (location=0) in vec2 position;
layout (location=1) in vec3 inColour;
flat out vec3 col_in;
void main()
{
gl_Position = vec4(position, 0.0, 1.0);
col_in = inColour;
}
Geometry:
#version 330 core
layout (triangles) in;
layout (triangle_strip, max_vertices = 3) out;
uniform vec2 window_size;
flat in vec3 col_in[];
out vec4 vertex_a;
out vec4 vertex_b;
out vec4 vertex_c;
flat out vec3 colour_a;
flat out vec3 colour_b;
flat out vec3 colour_c;
void main() {
vertex_a = (gl_in[0].gl_Position);
vertex_b = (gl_in[1].gl_Position);
vertex_c = (gl_in[2].gl_Position);
gl_Position = vertex_a;
colour_a = col_in[0];
EmitVertex();
gl_Position = vertex_b;
colour_b = col_in[1];
EmitVertex();
gl_Position = vertex_c;
colour_c = col_in[2];
EmitVertex();
EndPrimitive();
}
Fragment:
#version 330 core
uniform vec2 window_size;
in vec4 vertex_a;
in vec4 vertex_b;
in vec4 vertex_c;
flat in vec3 colour_a;
flat in vec3 colour_b;
flat in vec3 colour_c;
out vec4 fragColor;
void main()
{
vec2 uv = (gl_FragCoord.xy / window_size) - vec2(0.5);
float da = distance(vertex_a * 0.5, vec4(uv, 0.0, 0.0));
float db = distance(vertex_b * 0.5, vec4(uv, 0.0, 0.0));
float dc = distance(vertex_c * 0.5, vec4(uv, 0.0, 0.0));
if(da < db && da < dc){
fragColor = vec4(colour_a, 1.0);
} else if (db < da && db < dc){
fragColor = vec4(colour_b, 1.0);
} else if(dc < da && dc < db) {
fragColor = vec4(colour_c, 1.0);
} else {
fragColor = vec4(1.0);
}
}
This is the result:
Wireframe:
My question is: why is my result not a Voronoi diagram? Does it depend on the shader, or maybe the triangulation is wrong? Is this the best way to do this? If not, what should I do?