简体   繁体   English

OpenGL ES渲染错误

[英]OpenGL ES rendereing error

Well, I got problem here with opengl ES stuff (just started to learn about it by the way). 好吧,我在这里遇到了opengl ES的问题(顺便开始学习它)。 So here's some code 所以这里有一些代码

GLExample.java 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 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 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);
    }
}

And this is what was in LOGCAT 这就是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

as you see it says that error is here gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertBuff); 正如你所看到的那样,错误就在这里gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertBuff); but i don't understan what is wrong 但我不知道什么是错的

It's quite clear what the problem is: 很清楚问题是什么:

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

The vertBuff buffer needs to be direct so that it isn't moved around in memory. vertBuff缓冲区需要是direct以便它不会在内存中移动。 You need to use the allocateDirect(int) method from the ByteBuffer class. 您需要使用ByteBuffer类中的allocateDirect(int)方法。

Then you take the ByteBuffer object returned by that method and convert it to a FloatBuffer like in this example. 然后,您获取该方法返回的ByteBuffer对象,并将其转换为FloatBuffer,如本例所示。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM