我想獲得一個簡單的2D廣場出現在屏幕上,但我遇到了方法GLES20.glVertexAttribPointer()中的最後一個參數的問題。如何在VBO上正確使用android上的GLES20.glVertexAttribPointer()?
這裏是我的代碼(draw()方法):
public void draw(float[] matrix){
int shaderHandle = shader.getShaderHandle();
int mMVPMatrixHandle;
GLES20.glUseProgram(shaderHandle);
mPositionHandle = GLES20.glGetAttribLocation(shaderHandle, "vPosition");
mMVPMatrixHandle = GLES20.glGetUniformLocation(shaderHandle, "uMVPMatrix");
MyGLRenderer.checkGlError("glGetUniformLocation");
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, matrix, 0);
MyGLRenderer.checkGlError("glUniformMatrix4fv");
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER,vbo);
GLES20.glEnableVertexAttribArray(mPositionHandle);
GLES20.glVertexAttribPointer(mPositionHandle,COORDS_PER_VERTEX,GLES20.GL_FLOAT,false,0,0);
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER,ibo);
GLES20.glDrawElements(GLES20.GL_TRIANGLES,DRAW_ORDER.length,GLES20.GL_UNSIGNED_SHORT,null);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER,0);
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER,0);
}
的問題是在這條線:
GLES20.glVertexAttribPointer(mPositionHandle,COORDS_PER_VERTEX,GLES20.GL_FLOAT,false,0,0);
我使用過Android Studio,它告訴我,我不能通過0
作爲此方法中的最後一個參數,它需要一個Buffer對象。
爲了使用我的VBO,我被告知我必須有一個零作爲最後一個參數。我該怎麼做呢?
的logcat的進入我收到如下:
06-23 22:27:23.098 2709-2709/? I/art: Not late-enabling -Xcheck:jni (already on)
06-23 22:27:23.222 2709-2709/com.example.android.opengl W/System: ClassLoader referenced unknown path: /data/app/com.example.android.opengl-2/lib/x86
06-23 22:27:23.362 2709-2709/com.example.android.opengl W/System: ClassLoader referenced unknown path: /data/app/com.example.android.opengl-2/lib/x86
06-23 22:27:23.432 2709-2730/com.example.android.opengl D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
[ 06-23 22:27:23.439 2709: 2709 D/ ]
HostConnection::get() New Host Connection established 0xab9e4670, tid 2709
[ 06-23 22:27:23.472 2709: 2730 D/ ]
HostConnection::get() New Host Connection established 0xaf4471b0, tid 2730
06-23 22:27:23.474 2709-2730/com.example.android.opengl I/OpenGLRenderer: Initialized EGL, version 1.4
[ 06-23 22:27:23.506 2709: 2729 D/ ]
HostConnection::get() New Host Connection established 0xab9e4ac0, tid 2729
06-23 22:27:23.557 2709-2729/com.example.android.opengl I/System.out: DEBUG
06-23 22:27:23.562 2709-2729/com.example.android.opengl E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb4093c00
06-23 22:27:23.565 2709-2729/com.example.android.opengl E/AndroidRuntime: FATAL EXCEPTION: GLThread 129
Process: com.example.android.opengl, PID: 2709
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.nio.Buffer.remaining()' on a null object reference
at android.opengl.GLES20.glVertexAttribPointer(GLES20.java:1913)
at com.example.android.opengl.Sprite.draw(Sprite.java:105)
at com.example.android.opengl.MyGLRenderer.onDrawFrame(MyGLRenderer.java:76)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1535)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
06-23 22:27:23.607 2709-2730/com.example.android.opengl E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa3b11230
你能給我們你正在得到的logcat錯誤聲明嗎? – brohan322
我已在 – Lockrick
以上添加logcat我仍然支持我的答案下面,你試過嗎? – brohan322