OpenGL ES渲染错误

时间:2012-06-13 10:10:00

标签: java android opengl-es draw

好吧,我在这里遇到了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);,但我没有说明错误

1 个答案:

答案 0 :(得分:11)

很清楚问题是什么:

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

vertBuff缓冲区必须为direct,以便它不会在内存中移动。 您需要使用ByteBuffer类中的allocateDirect(int)方法。

然后你获取该方法返回的ByteBuffer对象并将其转换为像this example.中的FloatBuffer

祝你好运!