Hi All,
I am trying to draw solid wireframe over mesh.
And trying to look for a lot of sources but still can’t build proper shader yet.
#shader vertex
#version 430 core
layout (location = 0) in vec3 geo_Pos;
/* Global information sent by the engine */
layout (std140) uniform UBO {
mat4 ubo_Model;
mat4 ubo_View;
mat4 ubo_Projection;
vec3 ubo_ViewPos;
float ubo_Time;
};
/* Information passed to the fragment shader */
out VS_OUT {
vec3 FragPos;
vec3 Normal;
vec2 TexCoords;
mat3 TBN;
flat vec3 TangentViewPos;
vec3 TangentFragPos;
} vs_out;
out vec2 vPosition;
vec4 og_ClipToWindowCoordinates(vec4 v, mat4 viewportTransformationMatrix) {
v.xyz /= v.w; // normalized device coordinates
v.xyz = (viewportTransformationMatrix * vec4(v.xyz, 1.0)).xyz; // window coordinates
return v;
}
void main() {
gl_Position = ubo_Projection * ubo_View * ubo_Model * vec4(geo_Pos, 1.0);
vPosition = og_ClipToWindowCoordinates(gl_Position, ubo_View * ubo_Projection).xy;
}
#shader geometry
#version 430 core
layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;
in vec2 vPosition[];
noperspective out vec3 distanceToEdges;
float og_distanceToLine(vec2 f, vec2 p0, vec2 p1) {
vec2 l = f - p0;
vec2 d = p1 - p0;
vec2 p = p0 + (d * (dot(l, d) / dot(d, d)));
return distance(f, p);
}
void main() {
vec2 p0 = vPosition[0];
vec2 p1 = vPosition[1];
vec2 p2 = vPosition[2];
// Alternate between using the first and last vertex as the one opposite the
// interior edge based on primitive ID.
bool strip_flip = (bool (gl_PrimitiveIDIn & 1));
gl_Position = gl_in[0].gl_Position;
if (strip_flip)
distanceToEdges = vec3(og_distanceToLine(p0, p1, p2), 0.0, 0.0);
else
distanceToEdges = vec3(og_distanceToLine(p0, p1, p2), 0.0, 1.0);
EmitVertex();
gl_Position = gl_in[1].gl_Position;
if (strip_flip)
distanceToEdges = vec3(1.0, og_distanceToLine(p1, p2, p0), 0.0);
else
distanceToEdges = vec3(0.0, og_distanceToLine(p1, p2, p0), 1.0);
EmitVertex();
gl_Position = gl_in[2].gl_Position;
if (strip_flip)
distanceToEdges = vec3(1.0, 0.0, og_distanceToLine(p2, p0, p1));
else
distanceToEdges = vec3( 0.0, 0.0, og_distanceToLine(p2, p0, p1));
EmitVertex();
}
#shader fragment
#version 430 core
/* Global information sent by the engine */
layout (std140) uniform UBO {
mat4 ubo_Model;
mat4 ubo_View;
mat4 ubo_Projection;
vec3 ubo_ViewPos;
float ubo_Time;
};
/* Information passed from the fragment shader */
in VS_OUT {
vec3 FragPos;
vec3 Normal;
vec2 TexCoords;
mat3 TBN;
flat vec3 TangentViewPos;
vec3 TangentFragPos;
} fs_in;
uniform float u_halfLineWidth;
uniform vec3 u_color;
noperspective in vec3 distanceToEdges;
out vec4 fragmentColor;
void main() {
float d = min(distanceToEdges.x, min(distanceToEdges.y, distanceToEdges.z));
if (d > u_halfLineWidth + 1.0) {
fragmentColor = vec4(0.0, 0.0, 0.0, 1.0);
return;
}
d = clamp(d - (u_halfLineWidth - 1.0), 0.0, 2.0);
fragmentColor = vec4(u_color, exp2(-2.0 * d * d));
}
As you can see above that the meshes are shown up with color specified by u_color
but I don’t see any wireframe, can you please let me know what’s the problem I have and how to fix it?
Thanks,