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;
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
user284131
  • 51
  • 5
  • 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](https://stackoverflow.com/questions/6017176/gllinestipple-deprecated-in-opengl-3-1) or [Dashed line in OpenGL3?](https://stackoverflow.com/questions/52928678/dashed-line-in-opengl3) – Rabbid76 Aug 19 '20 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](https://stackoverflow.com/questions/60293538/draw-a-simple-dotted-line-or-dashed-line-in-opengl-gles20-android-using-fragment). In this case you have to associate a one-dimensional texture coordinate attribute to the vertices. – Rabbid76 Aug 19 '20 at 11:57
  • A screenshot or 2 might help illustrate your problem – Alan Birtles Aug 19 '20 at 12:30
  • I have edited and added the screenshot . For Polygons its working. But not for circle. – user284131 Aug 19 '20 at 12:38
  • The stipple effect appears to be done in the fragment shader, which is computing the color for each line segment between vertices, and creating the stipple effect at a set distance. But a circle is made up of many very tiny line segments between vertices -- which is likely the problem. I don't have a suggested solution for this though – Human-Compiler Aug 19 '20 at 12:53
  • 1
    You've copied the code form [glLineStipple deprecated in OpenGL 3.1](https://stackoverflow.com/questions/6017176/gllinestipple-deprecated-in-opengl-3-1/55088683#55088683). Please do not just copy the code but also read the answer - *"[...] the stipple pattern is restarted at each vertex coordinate [...]"* – Rabbid76 Aug 19 '20 at 20:26
  • Thanks @Rabbid. Yes I refereed this code https://stackoverflow.com/questions/6017176/gllinestipple-deprecated-in-opengl-3-1/55088683#55088683 . Is there way I can fix in my code. I am clueless now... any suggestion? – user284131 Aug 24 '20 at 10:24
  • @user284131 Please read the answer and the comments. I think you didn't understand what I was saying. This solution is not designed for vertex coordinates that are close together. You've to use a different approach. – Rabbid76 Aug 24 '20 at 10:27

0 Answers0