2016-03-05 20 views
0

在我发布的某款Android游戏中,有一个长期问题,其中某些用户在GLSurfaceView中看不到任何呈现游戏玩法的地方。我从来没有能够在我自己的任何测试设备上重现这个问题,所以调试起来非常困难。使用OpenGL ES 2.0的GLSurfaceView显示某些用户的空白屏幕

最近我能够与其中一位报告该错误的用户合作,以​​便将其隔离为相当小的代码子集。不幸的是,在这一点上,它与基本教程示例并没有太大的区别,所以我对可能出错的东西感到有些不知所措。

下面的代码应该在屏幕中心绘制一个白色正方形。它可以在我的测试设备上正常工作,但对于此用户而言失败(并且大概也有许多其他用户向我报告过相同的错误)。

我已经尝试了几个不同的测试。背景颜色的变化对用户是可见的,所以这不是所有GL呼叫的全部故障。此外,当我插入额外的调用glCheckError我从来没有记录任何非零返回。

有没有人有任何想法或建议?

public class MainActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     GLSurfaceView view = (GLSurfaceView) findViewById(R.id.gl); 
     view.setEGLContextClientVersion(2); 
     view.setRenderer(new GLRenderer()); 
     view.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); 

     view.requestRender(); 
    } 

    private static class GLRenderer implements GLSurfaceView.Renderer { 
     public static final float SCALE = 480f; 
     private float ratio; 

     private int mProgram; 
     private int mPositionHandle; 
     private int mColorHandle; 
     private int mMVPMatrixHandle; 

     private final float[] mVMatrix = new float[16]; // View matrix 
     private final float[] mPMatrix = new float[16]; // Projection matrix 
     private final float[] mVPMatrix = new float[16]; // V * P 
     private final float[] mMMatrix = new float[16]; // Object transformation matrix (position/rotation) 
     private final float[] mMVPMatrix = new float[16]; // VP * M 

     private final float[] foregroundColor = {1f, 1f, 1f, 1f}; 
     private final float[] backgroundColor = {0f, 0f, 0f, 1f}; 

     private static final int COORDS_PER_VERTEX = 3; 
     private static final int VERTEX_STRIDE = COORDS_PER_VERTEX * 4; // 4 bytes per vertex 

     private final float[] points = { 
       -20, -20, 0, 
       -20, 20, 0, 
       20, -20, 0, 
       20, 20, 0 
     }; 
     private final short[] drawOrder = { 
       0, 1, 2, 3 
     }; 

     private final FloatBuffer vertexBuffer = buildFloatBuffer(points); 
     private final ShortBuffer drawListBuffer = buildShortBuffer(drawOrder); 

     private static FloatBuffer buildFloatBuffer(float[] array) { 
      FloatBuffer buffer = ByteBuffer.allocateDirect(
        // (# of coordinate values * 4 bytes per float) 
        array.length * 4) 
        .order(ByteOrder.nativeOrder()) 
        .asFloatBuffer() 
        .put(array) 
        ; 
      buffer.position(0); 
      return buffer; 
     } 

     private static ShortBuffer buildShortBuffer(short[] array) { 
      ShortBuffer buffer = ByteBuffer.allocateDirect(
        // (# of coordinate values * 2 bytes per short) 
        array.length * 2) 
        .order(ByteOrder.nativeOrder()) 
        .asShortBuffer() 
        .put(array) 
        ; 
      buffer.position(0); 
      return buffer; 
     } 

     private static final String vertexShaderCode = 
       "uniform mat4 uMVPMatrix;" + 
       "attribute vec4 vPosition;" + 
       "void main() {" + 
       " gl_Position = uMVPMatrix * vPosition;" + 
       "}"; 


     private static final String fragmentShaderCode = 
       "precision mediump float;" + 
       "uniform vec4 vColor;" + 
       "void main() {" + 
       " gl_FragColor = vColor;" + 
       "}"; 

     private static int loadShader(int type, String shaderCode){ 
      // create a vertex shader type (GLES20.GL_VERTEX_SHADER) 
      // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER) 
      int shader = GLES20.glCreateShader(type); 

      // add the source code to the shader and compile it 
      GLES20.glShaderSource(shader, shaderCode); 
      GLES20.glCompileShader(shader); 

      return shader; 
     } 

     @Override 
     public void onSurfaceCreated(GL10 gl, EGLConfig config) { 
      GLES20.glClearColor(backgroundColor[0], backgroundColor[1], backgroundColor[2], backgroundColor[3]); 

      int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, 
        vertexShaderCode); 
      int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, 
        fragmentShaderCode); 

      mProgram = GLES20.glCreateProgram(); 
      GLES20.glAttachShader(mProgram, vertexShader); 
      GLES20.glAttachShader(mProgram, fragmentShader); 
      GLES20.glLinkProgram(mProgram); 
      GLES20.glUseProgram(mProgram); 

      mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition"); 
      mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor"); 
      mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); 
     } 

     @Override 
     public void onSurfaceChanged(GL10 gl, int width, int height) { 
      ratio = 1f * width/height; 

      GLES20.glViewport(0, 0, width, height); 

      Matrix.frustumM(mPMatrix, 0, -ratio * SCALE/2, ratio * SCALE/2, -SCALE/2, SCALE/2, 3, 7); 
      Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0, 0, 0, 0, -1, 0); 
      Matrix.multiplyMM(mVPMatrix, 0, mPMatrix, 0, mVMatrix, 0); 
     } 

     @Override 
     public void onDrawFrame(GL10 gl) { 
      GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); 

      GLES20.glEnableVertexAttribArray(mPositionHandle); 

      GLES20.glUniform4fv(mColorHandle, 1, foregroundColor, 0); 
      draw(0, 0, 0); 

      GLES20.glDisableVertexAttribArray(mPositionHandle); 
     } 

     private void draw(float x, float y, float a) { 
      Matrix.setIdentityM(mMMatrix, 0); 
      Matrix.translateM(mMMatrix, 0, x, y, 0); 
      Matrix.rotateM(mMMatrix, 0, a, 0, 0, 1); 
      Matrix.multiplyMM(mMVPMatrix, 0, mVPMatrix, 0, mMMatrix, 0); 

      GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0); 

      GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, 
        GLES20.GL_FLOAT, false, VERTEX_STRIDE, 
        vertexBuffer); 

      GLES20.glDrawElements(GLES20.GL_TRIANGLE_STRIP, 
        drawListBuffer.capacity(), GLES20.GL_UNSIGNED_SHORT, 
        drawListBuffer); 
     } 
    } 
} 

回答

0

如果我将Matrix.frustumM()呼叫更改为Matrix.orthoM(),问题就消失了。除此之外,我仍然不确定为什么事情在某些设备上运行,而不是在其他设备上运行。如果有人能够更好地理解这里发生的事情,我愿意接受另一个答案,但现在我已经解决了我的问题。

0

驱动程序可能是越野车。您可以联系司机的公司,提交错误报告

问自己以下问题:

  1. 手机是否有不同的驱动程序?它支持OpenGL ES 2.0吗?
  2. GL运营是否成功?即glGetError()是否非零?
  3. glClearColor是否成功?尝试将颜色清除为红色。你看到一个红色的屏幕?
  4. 程序是否成功编译?
+0

我编辑了这个问题来解决点2和3,我也认为在发布这个问题后测试。至于驱动程序问题,我实际上没有任何直接访问驱动程序信息的权限,但假设Google Play商店正确地过滤了所有内容,则我的所有用户的设备都应具有OpenGL ES 2.0支持。 – Brucelet

+0

它可能是一个endianness(字节顺序)的问题。驱动程序的字节顺序可能与设备的字节顺序不同。这不应该发生,但它值得检查。尝试将缓冲区设置为LITTE_ENDIAN或BIG_ENDIAN。 – Swifter

+0

没有endianness运气,但我确实搞清楚了!看到我发布的答案。 – Brucelet