user1107378
user1107378

Reputation: 73

OpenGL ES rendereing error

Well, I got problem here with opengl ES stuff (just started to learn about it by the way). So here's some code

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);
    }
}

And this is what was in 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); but i don't understan what is wrong

Upvotes: 4

Views: 3675

Answers (1)

user520288
user520288

Reputation:

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. You need to use the allocateDirect(int) method from the ByteBuffer class.

Then you take the ByteBuffer object returned by that method and convert it to a FloatBuffer like in this example.

Good luck!

Upvotes: 11

Related Questions