I am trying to implement the Line Stipple in OpenGL because the Line stipple API is deprecated. It is working fine for lines and Polygons but It is not working for Circle. is there any problem with the below shaders? I see the pattern coming up fine for lines and polygons. But not working for circles. But when I zoom in case of circle I see the pattern. Don’t know what is the issue. Please let me know .
Thanks in advance.
Below is my code for vertex and fragment shader.
Vertex Shader:
#version 420 core
layout (location = 0)in vec4 aPos;
flat out vec3 startPos;
out vec3 vertPos;
uniform mat4 mvp;
void main()
{
vec4 pos = mvp * vec4(aPos.x, aPos.y, 0.0, 1.0)
gl_Position = pos;
vertPos = pos.xyz/pos.w;
startPos = vertPos;
}
Fragment Shader:
#version 420 core
flat in vec3 startPos;
in vec3 vertPos;
uniform vec4 my_color;
uniform vec2 u_resolution;
uniform uint u_pattern;
uniform float u_factor;
uniform int stipple;
out vec4 color;
void main()
{
if (stipple == 1)
{
vec2 dir = (vertPos.xy-startPos.xy) * u_resolution/2.0;
float dist = length(dir);
uint bit = uint(round(dist/u_factor)) & 15U;
if((u_pattern & (1U<<bit)) == 0U)
discard;
}
color = my_color;
}
Below is the code where I am setting the uniform values.
if (vModel->getLineStipple() > 1 )
{
glUniform1i(stip, 1);
GLint loc_res = glGetUniformLocation(shaderProgram, "u_resolution");
GLint loc_pattern = glGetUniformLocation(shaderProgram, "u_pattern");
GLint loc_factor = glGetUniformLocation(shaderProgram, "u_factor");
GLushort pattern = vModel->getLineStipple();
GLfloat factor = vModel->getStippleFactor();
glUniform1ui(loc_pattern, pattern);
glUniform1f(loc_factor, factor);
glUniform2f(loc_res, _scrWidth, _scrHeight);
}
...
then draw function
...
switch (vModel->getDrawMode())
{
case 0: //GL_POINTS
//glPointSize(6.0);
glDrawArrays(GL_POINTS, 0, vModel->getVertices().size());
break;
case 1: //GL_LINES
//glEnable(GL_LINE_SMOOTH);
//GLint range[2];
//glGetIntegerv(GL_ALIASED_LINE_WIDTH_RANGE,range);
glDrawArrays(GL_LINES, 0, vModel->getVertices().size());
break;
case 2: //GL_LINE_LOOP
glDrawArrays(GL_LINE_LOOP, 0, vModel->getVertices().size());
break;
case 3: //GL_LINE_STRIP
glDrawArrays(GL_LINE_STRIP, 0, vModel->getVertices().size());
break;
}