Reputation: 300
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.
I'm rendering grid using GL_LINES and simple shader program.
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.
Here is how problem looks:
And here is how it should work:
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.
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.
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.
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?
Other geometry (filled polygons not lines) renders fine.
Here is my code.
DrawFrame and project/view matrices setup:
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
Drawing lines:
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);
Here is my shader code (sorry for such format =)):
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);" +
"}";
}
Here is minimal app to reproduce problem (wait until grid rotates completely):
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.
And here is random stuff I have tried to see if it will affect problem (but no luck, same result) so far:
I also noticed this artifact when i was preparing examples:
It appears and then disappears very quickly when camera is rotated and lines flicker.
Upvotes: 6
Views: 1420