1

enter image description hereI 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; } 
8
  • Most likely, the vertices are too close together so that there is no space between them for a dashed line. See also glLineStipple deprecated in OpenGL 3.1 or Dashed line in OpenGL3? Commented Aug 19, 2020 at 11:48
  • Another solution could be to use a one-dimensional texture with the stipple pattern: Draw a simple dotted line or dashed line in OpenGL. In this case you have to associate a one-dimensional texture coordinate attribute to the vertices. Commented Aug 19, 2020 at 11:57
  • A screenshot or 2 might help illustrate your problem Commented Aug 19, 2020 at 12:30
  • I have edited and added the screenshot . For Polygons its working. But not for circle. Commented Aug 19, 2020 at 12:38
  • 1
    You've copied the code form glLineStipple deprecated in OpenGL 3.1. Please do not just copy the code but also read the answer - "[...] the stipple pattern is restarted at each vertex coordinate [...]" Commented Aug 19, 2020 at 20:26

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.