Advertisement

OpenGL 4 thick lines

Started by October 23, 2022 11:22 PM
7 comments, last by taby 2 years ago

Today I tried about a half-dozen of methods from shadertoy, to draw lines using a vertex-geometry-fragment shader, but none worked. Obviously the flaw is with my software.

Do you have any tiny working examples in OpenGL 4 that draws thick lines? I can't seem to replicate anyone's results.

Here is the full source, including the line shaders:

https://github.com/sjhalayka/obj_ogl4

I made some progress. Here is the result. Any ideas on how the hidden line removal using my code?

Advertisement

taby said:
Any ideas on how the hidden line removal using my code?

Ideally you have edge normals for back face culling.

But if not, the only way i see is a depth prepass, so you can do a depth test with some depth bias.
Though, to get the perfect bias, you'd again need the angle between edge and adjacent triangles. And even then artifacts will remain.

So i would go for the edge normals, orienting the edge quad to the edge normal. While calculating those edge normals, you could also reject convex / coplanar edges.

The cell shading approach (drawing extruded back face mesh) might be easier and look better, even if the lines become thinner on a corner than on an edge. Not sure.

Dude, I figured it out, after many hours of trying.

The key was to set the depth range to 0.01 to 1.0 when drawing the outline. It's all working great! Thanks for your input.

P.S. I am trying my best to implement half-transparent lines, like you had suggested. I’m listening to you…. Just takes me a while to process these thoughts of yours . Thanks again, man.

For the record, the shaders are as follows.

The vertex shader:

#version 430
layout (location = 0) in vec3 position;

uniform mat4  u_modelviewprojection_matrix;

void main()
{
    gl_Position = u_modelviewprojection_matrix * vec4(position, 1.0);
}

The geometry shader:

#version 430
layout (lines) in;                              // now we can access 2 vertices
layout (triangle_strip, max_vertices = 4) out;  // always (for now) producing 2 triangles (so 4 vertices)


uniform int img_width;
uniform int img_height;
uniform int cam_factor;
uniform float line_thickness;

vec2  u_viewportSize = vec2(img_width, img_height);
 float u_thickness = cam_factor * line_thickness;




void main()
{
    vec4 p1 = gl_in[0].gl_Position;
    vec4 p2 = gl_in[1].gl_Position;
    vec2 dir    = normalize((p2.xy/p2.w - p1.xy/p1.w) * u_viewportSize);
    vec2 offset = vec2(-dir.y, dir.x) * u_thickness / u_viewportSize;
    gl_Position = p1 + vec4(offset.xy * p1.w, 0.0, 0.0);
    EmitVertex();
    gl_Position = p1 - vec4(offset.xy * p1.w, 0.0, 0.0);
    EmitVertex();
    gl_Position = p2 + vec4(offset.xy * p2.w, 0.0, 0.0);
    EmitVertex();
    gl_Position = p2 - vec4(offset.xy * p2.w, 0.0, 0.0);
    EmitVertex();
    EndPrimitive();
}

And the fragment shader:

#version 430
out vec4 fragColor;
uniform vec4 u_color = vec4(0, 0, 0, 1);
void main()
{
    fragColor = u_color;
}

I got these shaders from https://stackoverflow.com/questions/54686818/glsl-geometry-shader-to-replace-gllinewidth​​ and from https://github.com/Rabbid76/graphics-snippets/blob/master/example/cpp/opengl/example_shader_geometry_1_line.cpp

High res shot

Advertisement

taby said:
P.S. I am trying my best to implement half-transparent lines, like you had suggested.

Can't remember i did that. Transparency is still an open problem, lacking efficient solutions.

I have suggested to use a darker color from the voxel for the lines, or only per object outline. Maybe you meant one of these.

I added a vs/gs/fs program to draw thick (square, for now) points. It’s at:

https://github.com/sjhalayka/obj_ogl4/blob/main/circles.gs.glsl

This topic is closed to new replies.

Advertisement