2012-06-13 213 views
4

那麼,我在這裏遇到了與opengl ES的東西問題(剛開始瞭解它的方式)。 因此,這裏的一些代碼OpenGL ES渲染錯誤

GLExample.java

package com.android.OpGL; 

import android.app.Activity; 
import android.opengl.GLSurfaceView; 
import android.os.Bundle; 


public class GLExample extends Activity { 

    GLSurfaceView ourSurface; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     ourSurface = new GLSurfaceView(this); 
     ourSurface.setRenderer(new GLRendererEx()); 

     setContentView(ourSurface); 
    } 
} 

GLRendererEx.java

package com.android.OpGL; 

import javax.microedition.khronos.egl.EGLConfig; 
import javax.microedition.khronos.opengles.GL10; 

import android.opengl.GLSurfaceView.Renderer; 

public class GLRendererEx implements Renderer { 

    private GLTriangleEx tri; 

    public GLRendererEx(){ 
     tri = new GLTriangleEx(); 
    } 


    @Override 
    public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) { 
     // TODO Auto-generated method stub 
     gl.glDisable(GL10.GL_DITHER); 
     gl.glClearColor(.8f, 0f, .2f, 1f); 
     gl.glClearDepthf(1f); 

    } 

    @Override 
    public void onDrawFrame(GL10 gl) { 
     // TODO Auto-generated method stub 
     gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 

     tri.draw(gl); 
    } 

    @Override 
    public void onSurfaceChanged(GL10 gl, int width, int height) { 
     // TODO Auto-generated method stub 

    } 



} 

GLTriangleEx.java

package com.android.OpGL; 

import java.nio.ByteBuffer; 
import java.nio.ByteOrder; 
import java.nio.FloatBuffer; 
import java.nio.ShortBuffer; 

import javax.microedition.khronos.opengles.GL10; 

public class GLTriangleEx { 

    private float vertices[]= { 
     0f, 1f, //(0,1) point 
     1f, -1f,//(1,-1) 
     -1f,-1f//(-1,-1) 
    }; 

    private FloatBuffer vertBuff; 

    private short[] pIndex = {0,1,2}; 

    private ShortBuffer pBuff; 

    public GLTriangleEx(){ 

     ByteBuffer bBuff = ByteBuffer.allocate(vertices.length *4); 
     bBuff.order(ByteOrder.nativeOrder()); 
     vertBuff = bBuff.asFloatBuffer(); 
     vertBuff.put(vertices); 
     vertBuff.position(0); 

     ByteBuffer pbBuff = ByteBuffer.allocateDirect(pIndex.length * 2); 
     pbBuff.order(ByteOrder.nativeOrder()); 
     pBuff = pbBuff.asShortBuffer(); 
     pBuff.put(pIndex); 
     pBuff.position(0); 
    } 

    public void draw(GL10 gl){ 
     gl.glFrontFace(GL10.GL_CW); 
     gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
     gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertBuff); 
     gl.glDrawElements(GL10.GL_TRIANGLES, pIndex.length, GL10.GL_UNSIGNED_SHORT, pBuff); 
     gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
    } 
} 

這是什麼在logcat的

06-13 10:06:34.074: INFO/ActivityManager(71): Displayed com.android.OpGL/.GLExample: +1s391ms 
06-13 10:06:34.493: ERROR/OpenGLES(727): Application com.android.OpGL (SDK target 10) called a GL11 Pointer method with an indirect Buffer. 
06-13 10:06:34.523: WARN/dalvikvm(727): threadid=9: thread exiting with uncaught exception (group=0x40015560) 
06-13 10:06:34.523: ERROR/AndroidRuntime(727): FATAL EXCEPTION: GLThread 10 
06-13 10:06:34.523: ERROR/AndroidRuntime(727): java.lang.IllegalArgumentException: Must use a native order direct Buffer 
06-13 10:06:34.523: ERROR/AndroidRuntime(727):  at com.google.android.gles_jni.GLImpl.glVertexPointerBounds(Native Method) 
06-13 10:06:34.523: ERROR/AndroidRuntime(727):  at com.google.android.gles_jni.GLImpl.glVertexPointer(GLImpl.java:1121) 
06-13 10:06:34.523: ERROR/AndroidRuntime(727):  at com.android.OpGL.GLTriangleEx.draw(GLTriangleEx.java:42) 
06-13 10:06:34.523: ERROR/AndroidRuntime(727):  at com.android.OpGL.GLRendererEx.onDrawFrame(GLRendererEx.java:31) 
06-13 10:06:34.523: ERROR/AndroidRuntime(727):  at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1363) 
06-13 10:06:34.523: ERROR/AndroidRuntime(727):  at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1118) 
06-13 10:06:34.553: WARN/ActivityManager(71): Force finishing activity com.android.OpGL/.GLExample 

你看它說,錯誤是在這裏gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertBuff);但我不understan什麼是錯

回答

11

這是相當清楚的問題是什麼:

... called a GL11 Pointer method with an indirect Buffer. 

vertBuff緩衝區需要爲direct,以便它不會在內存中移動。 您需要使用ByteBuffer類中的allocateDirect(int)方法。

然後你把由該方法返回的ByteBuffer對象,並將其在this example.

好運轉換爲FloatBuffer喜歡!