-3

Many times I tried to change the triangle points to circle in the vertexShader.glsl without a successful outcome. Would I ask you to rewrite the code so that the program draws a circle instead of a triangle?

So the vertexShader code:

uniform float offsetX;
uniform float offsetY;

void main(void)
{

    if(gl_VertexID == 0) gl_Position = vec4(0.25 + offsetX, -0.25 + offsetY, 0.0, 1.0);
    else if(gl_VertexID == 1) gl_Position = vec4(-0.25 + offsetX, -0.25 + offsetY, 0.0, 1.0);
    else gl_Position = vec4(0.0 + offsetX, 0.25 + offsetY, 0.0, 1.0);
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • One option is to draw the inner circle of the triangle in the fragment shader. – Rabbid76 Apr 05 '21 at 06:10
  • Right now you got most likely 3 vertexes where you simply change them slightly which will lead always to 3 vertexes in output so still a triangle that is why its not working. If this is for morphing purposes then you have to use geometry shader that will emit n-triangles per each of the triangle edges. (beware n is limited, and each triangle is a pie slice of your circle), If you want just render inscribed circle that is done in fragment (computing distance to center and `discard;` too far fragments Just like the Rabbid76's answer suggest. So which one it is? How are circle parameters encoded? – Spektre Apr 05 '21 at 07:27
  • I recently did a hybrid approach using geometry and fragment shaders ... passing just single 4D point from CPU side `(x,y,z,r)` ... in geometry emit 2D BBOX quad and in fragment render 3D sphere ... you can do this in 2D too. Just beware `w` is used for perspecive divide so you need to pass the `r` from vertex in separate variable – Spektre Apr 05 '21 at 07:34

1 Answers1

0

One option is to draw the inner circle of the triangle in the fragment shader.

Compute the coordinates and the radius of the the Incenter of a Triangle. See Incircle and excircles of a triangle and pass it to the fragment shader:

#version 150

uniform float offsetX;
uniform float offsetY;

out vec2 v_pos;
out vec2 center;
out float radius;

void main(void)
{
    vec2 pts[3] = vec2[3](
       vec2(0.25, -0.25),
       vec2(-0.25, -0.25),
       vec2(0.0, 0.25));

    v_pos = pts[gl_VertexID] + vec2(offsetX, offsetY);

    float a = distance(pts[1], pts[2]); 
    float b = distance(pts[2], pts[0]); 
    float c = distance(pts[0], pts[1]); 

    center = (pts[0] * a + pts[1] * b + pts[2] * c) / (a+b+c) + vec2(offsetX, offsetY);
    float s = (a + b + c) / 2.0;
    radius = sqrt((s - a) * (s - b) * (s - c) / s);

    gl_Position = vec4(v_pos, 0.0, 1.0);
}

discard the points outside the incircle in the fragment shader:

#version 150

out vec4 out_color;

in vec2 v_pos;
in vec2 center;
in float radius;

void main(void)
{
    if (distance(v_pos, center) > radius)
    {
        discard;

        // debug
        //out_color = vec4(0.5, 0.5, 0.5, 1.0);
        //return;
    }
    out_color = vec4(1.0, 0.0, 0.0, 1.0);
}

Rabbid76
  • 202,892
  • 27
  • 131
  • 174