Languages

Menu
Sites
Language
glLineStipple deprecated in OpenGL ES 2.0

glLineStipple deprecated in OpenGL ES 2.0

We know that in OpenGL ES 1.1, glLineStipple() is used to draw the dotted line. Following piece of codes shows how the dotted line is drawn in OpenGL ES 1.1.  

        //draw dotted line
        glLineStipple(2, 0x5555);
        glEnable(GL_LINE_STIPPLE);
        glBegin(GL_LINES);
        glVertex3f(0.25,0.85,0.6);
        glVertex3f(0.75,0.85,0.6);
        glEnd();
        glDisable(GL_LINE_STIPPLE);

But this function is deprecated in OpenGL ES 2.0.

I can use following shader codes to simulate the dotted lines, but the effect seems not perfect.

/* Vertex Shader Source */
static const char vertex_shader[] =
  "uniform mat4 u_mvpMatrix;\n"
  "attribute vec4 a_position;\n"
  "attribute vec4 a_color;\n"
  "varying vec4 v_color;\n"
  "varying vec4 v_position;\n"
  "\n"
  "void main()\n"
  "{\n"
  "    v_color = a_color;\n"
  "    gl_Position = u_mvpMatrix * a_position;\n"
  "    v_position = a_position;\n"
  "}";
/* Fragment Shader Source */
static const char fragment_shader[] =
  "#ifdef GL_ES\n"
  "precision mediump float;\n"
  "#endif\n"
  "uniform vec4 u_sourcePoint;\n"
  "varying vec4 v_color;\n"
  "varying vec4 v_position;\n"
  "\n"
  "void main (void)\n"
  "{\n"
  "  if (cos(0.1*abs(distance(u_sourcePoint.xyz, v_position.xyz))) + 0.5 > 0.0) {\n"
  "    gl_FragColor = vec4(0,0,0,0);\n"
  "  } else {\n"
  "    gl_FragColor = v_color;\n"
  "  }\n"
  "}";

In the code to draw the lines, I pass in the coordinate system origin to the uniform vector u_sourcePoint:

        //define the point will be passed in to shader as u_sourcePoint
        float sp[] ={
                0.0f, 0.0f, 0.0f, 0.0f,  // [0] Coordinate system origin;
        };
 
        //draw axies by full_coords_vertices
        glVertexAttribPointer(ad->idx_position, 3, GL_FLOAT, GL_FALSE,
                3 * sizeof(float), full_coords_vertices);
        glVertexAttribPointer(ad->idx_color, 4, GL_FLOAT, GL_FALSE,
                4 * sizeof(float), full_coords_colors);
        glEnableVertexAttribArray(ad->idx_position);
        glEnableVertexAttribArray(ad->idx_color);
        glUniformMatrix4fv(ad->idx_mvp, 1, GL_FALSE, ad->mvp);
        glUniformMatrix4fv(ad->idx_sp, 1, GL_FALSE, sp);
        glDrawElements(GL_LINES, full_coords_indices_count, GL_UNSIGNED_SHORT,
                full_coords_indices);

The dotted lines is drawn by assign different colors to gl_FragColor based on the result of the geometric mathematical expression. You can define whatever geometric mathematical expression used to define your dotted line pattern. Below are the effect above shader generated.

       

But it seems the effect not perfect.

It still cannot draw the line as glLineStipple by define pixels pattern.

Anyone know if there are some good way to draw dotted line on OpenGL ES 2.0?

 

Edited by: Jean Yang on 13 Aug, 2015