title: GL_LINES disappear/flicker when rotating camera on Android with OpenGL ES 2.0
body: <p>In my Android application I'm using OpenGL ES 2.0, and i have need to show a grid in the center of the scene. I have also camera which can be rotated around scene center. </p>
  
  <p>I'm rendering grid using GL_LINES and simple shader program. </p>
  
  <p>On some devices that i have, everything works fine, however on one device (specifically Galaxy Note 10.1 2014 edition) i have problem with grid lines disappearing/flickering when camera is rotated, i.e. at some camera angles lines are not visible.</p>
  
  <p>Here is how problem looks:</p>
  
  <p><img src="https://i.stack.imgur.com/ji1i9.gif" alt="enter image description here"></p>
  
  <p>And here is how it should work:</p>
  
  <p><img src="https://i.stack.imgur.com/FJ73y.gif" alt="enter image description here"></p>
  
  <p>What i noticed, is that it looks like lines start to flicker at some camera angles, when their end points (or start points, not sure) appears behind near frustum plane of camera (i.e. behind camera). So there is no flickering when camera looks at grid from above, and no flickering when camera looks from two of four sides, or from far away.</p>
  
  <p>I tried to search for solution or possible cause of this problem for few days, but found nothing so far. So i decided to ask you people. I would appreciate some help, suggestions. Maybe someone with OpenGL experience could guess what causes problem from this description.</p>
  
  <p>There is also question, maybe it’s not problem in my code, but problem with specific device, with driver or etc? But i have no way to check this. I have only three Android devices on my hands, and it’s probably not enough to make such conclusion for sure. </p>
  
  <p>It works fine on old Galaxy Tab 10.1, and on Lenovo P780 phone.
  However Galaxy Note 10.1 is more recent device and has very big resolution (2560 x 1600),
  so i also think, that maybe its not because device/GPU driver, but maybe because high resolution? </p>
  
  <p>Other geometry (filled polygons not lines) renders fine. </p>
  
  <p>Here is my code.</p>
  
  <p>DrawFrame and project/view matrices setup:</p>
  
  <pre><code>    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
  
      GLES20.glLineWidth(1f);
  
      float screenRatio = (float)viewportWidth / (float)viewportHeight;
  
      Matrix.setIdentityM(matrixProjection, 0);
      MatrixHelper.perspectiveM(matrixProjection, 0, 90, screenRatio, 0.1f, 1000f);                
  
      Matrix.setIdentityM(matrixView, 0);
  
      Matrix.translateM(matrixView, 0, 0f, 0f, -10f);        
  
      rotationAngle = rotationAngle + (10f * deltaSec);
  
      Matrix.rotateM(matrixView, 0, 45f, 1f, 0f, 0f);
      Matrix.rotateM(matrixView, 0, rotationAngle, 0f, 1f, 0f);        
  
  
      GLES20.glUseProgram(shaderGrid.ShaderProgramId);
  
      GLES20.glUniformMatrix4fv(shaderGrid.vProjection, 1, false, matrixProjection, 0);
      GLES20.glUniformMatrix4fv(shaderGrid.vView, 1, false, matrixView, 0);
  
      renderGrid.Draw(shaderGrid, origin); //see code from this method below
  </code></pre>
  
  <p>Drawing lines:</p>
  
  <pre><code>    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, bufferIds[0]);                              
      GLES20.glVertexAttribPointer(shader.aPosition, 3, GLES20.GL_FLOAT, false, 0, 0);
      GLES20.glEnableVertexAttribArray(shader.aPosition);        
  
      float [] m = new float[16];
  
      Matrix.setIdentityM(m, 0);
  
      GLES20.glUniformMatrix4fv(shader.vModel, 1, false,  m, 0);            
  
      GLES20.glDrawArrays(GLES20.GL_LINES, 0,     10 * 2 * 4); 
      GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
      GLES20.glDisableVertexAttribArray(shader.aPosition);
  </code></pre>
  
  <p>Here is my shader code (sorry for such format =)):</p>
  
  <pre><code>    public String GetShaderFragmentCode()
      {        
          return
            "precision mediump float;" +
            "void main() " +
            "{" +
            "  gl_FragColor = vec4(0.7, 0.7, 0.7, 1.0);" +
            "}";            
      }
  
      public String GetShaderVertexCode()
      {
          return
            "uniform mat4 vProjection;" +
            "uniform mat4 vModel;" +
            "uniform mat4 vView;" +
  
            "attribute vec3 aPosition;" +
  
            "void main() " +
            "{" +
            "  gl_Position = (vProjection * vView * vModel) * vec4(aPosition.xyz, 1.0);" +
            "}";
       }
  </code></pre>
  
  <p>Here is minimal app to reproduce problem (wait until grid rotates completely):</p>
  
  <p><a href="https://dl.dropboxusercontent.com/u/5344890/GL_LINES_BUG/LinesProblemMinimal.apk" rel="noreferrer">Compiled APK</a></p>
  
  <p><a href="https://dl.dropboxusercontent.com/u/5344890/GL_LINES_BUG/LinesProblemMinimal.zip" rel="noreferrer">Source code</a> </p>
  
  <p>I am not sure if its really helps, because it’s device specific, but I will also appreciate your feedback in case if you will have same problem on other devices. Or if you have other high resolution device that doesn't have this problem. </p>
  
  <p>And here is random stuff I have tried to see if it will affect problem (but no luck, same result) so far:</p>
  
  <ul>
  <li>disable/enable GL_CULL_FACE</li>
  <li>explicitly set glLineWidth to 1f, or other values</li>
  <li>use glDrawElements instead of glDrawArrays</li>
  <li>change camera frustum zNear (from 0.1f to 100f)</li>
  <li>change frustum zFar (from 10f to very big values)</li>
  <li>change vec3 to vec4 for aPosition in shader code (and without converting using .xyz)</li>
  <li>using GL_LINE_STRIP - to see what will happen, lines still disappear</li>
  <li>using GL_TRIANGLE_STRIP etc - polygons doesn't disappear, so problem specifically with lines, but i need lines </li>
  </ul>
  
  <p>I also noticed this artifact when i was preparing examples:</p>
  
  <p><img src="https://i.stack.imgur.com/4oqlN.png" alt="enter image description here"></p>
  
  <p>It appears and then disappears very quickly when camera is rotated and lines flicker.</p>