0

嘿,我正在尝试在 opengl 上绘制一个三角形。我只是按照教程去做。尽管一切似乎都很好,但它不起作用。每次我运行我的代码时,它都会让我回到主菜单。

调试后我注意到我的应用程序停留在该行

gl.glDrawElements(GL10.GL_TRIANGLES, pIndex.length, GL10.GL_UNSIGNED_SHORT, pBuff); 

这是我的 GLExample.java 类

public class GLExample extends Activity {
    GLSurfaceView ourSurface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        ourSurface = new GLSurfaceView(this);
        ourSurface.setRenderer(new GLRendererEx());
        setContentView(ourSurface);
    }

    @Override
    protected void onPause() {

        super.onPause();
        ourSurface.onPause();
    }

    @Override
    protected void onResume() {

        super.onResume();
        ourSurface.onResume();
    }
}

这是我的 GLTriangleEX 课

public class GLTriangleEx {

    private float vertices[] = { 
            0f, 1f, // p0
            1f, -1f, // p1
            -1f, -1f // p2

    };

    private FloatBuffer vertBuff;

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

    private ShortBuffer pBuff;

    public GLTriangleEx() {

        // her bir float için 4 byte gerektiğinden length * 4 oldu
        ByteBuffer bBuff = ByteBuffer.allocateDirect(vertices.length * 4);
        bBuff.order(ByteOrder.nativeOrder());
        vertBuff = bBuff.asFloatBuffer();
        vertBuff.put(vertices);
        vertBuff.position(0);

        ByteBuffer pbBuff = ByteBuffer.allocate(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);
    }
}

这是我的 GLRendererEx.java 类

public class GLRendererEx implements Renderer {

    private GLTriangleEx tri;

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

    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
        gl.glDisable(GL10.GL_DITHER);
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
        gl.glClearColor(.1f, 0f, .9f, 1f);// set background color
        gl.glClearDepthf(1f);
    }

    @Override
    public void onDrawFrame(GL10 gl) {

        gl.glDisable(GL10.GL_DITHER);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        GLU.gluLookAt(gl, 0, 0, -5, 0, 0, 0, 0, 2, 0);

        tri.Draw(gl);

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);
        float ratio = (float) width / height;
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glFrustumf(-ratio, ratio, -1, 1, 1, 25);

    }

}

和我的 LogCat

02-08 12:11:01.244: I/ActivityManager(58): Starting activity: Intent { cmp=com.android/.GLExample }
02-08 12:11:01.305: D/dalvikvm(328): GC_EXTERNAL_ALLOC freed 1143 objects / 82584 bytes in 39ms
02-08 12:11:01.393: D/libEGL(328): egl.cfg not found, using default config
02-08 12:11:01.393: D/libEGL(328): loaded /system/lib/egl/libGLES_android.so
02-08 12:11:01.534: I/DEBUG(30): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
02-08 12:11:01.534: I/DEBUG(30): Build fingerprint: 'generic/google_sdk/generic/:2.2/FRF91/43546:eng/test-keys'
02-08 12:11:01.534: I/DEBUG(30): pid: 328, tid: 334  >>> com.android <<<
02-08 12:11:01.534: I/DEBUG(30): signal 11 (SIGSEGV), fault addr 00000000
02-08 12:11:01.534: I/DEBUG(30):  r0 00001403  r1 00000003  r2 00000000  r3 00001403
02-08 12:11:01.534: I/DEBUG(30):  r4 00239480  r5 00000000  r6 00000003  r7 00239480
02-08 12:11:01.534: I/DEBUG(30):  r8 00000000  r9 00000000  10 00000000  fp 0012fbb8
02-08 12:11:01.534: I/DEBUG(30):  ip 00000000  sp 45f03d20  lr ac40604c  pc ac40503c  cpsr 60000010
02-08 12:11:01.594: I/DEBUG(30):          #00  pc 0000503c  /system/lib/egl/libGLES_android.so
02-08 12:11:01.594: I/DEBUG(30):          #01  pc 00006048  /system/lib/egl/libGLES_android.so
02-08 12:11:01.594: I/DEBUG(30):          #02  pc 00020618  /system/lib/libandroid_runtime.so
02-08 12:11:01.594: I/DEBUG(30):          #03  pc 00013974  /system/lib/libdvm.so
02-08 12:11:01.594: I/DEBUG(30): code around pc:
02-08 12:11:01.594: I/DEBUG(30): ac40501c e5900548 e3a05b05 e2853003 e1500003 
02-08 12:11:01.604: I/DEBUG(30): ac40502c e2419003 1a000023 e1a05002 e3a0a000 
02-08 12:11:01.604: I/DEBUG(30): ac40503c e1d510b0 e1a00004 ebffff6e e1d510b2 
02-08 12:11:01.604: I/DEBUG(30): ac40504c e1a07000 e1a00004 ebffff6a e1d510b4 
02-08 12:11:01.604: I/DEBUG(30): ac40505c e1a06000 e1a00004 ebffff66 e5971020 
02-08 12:11:01.604: I/DEBUG(30): code around lr:
02-08 12:11:01.604: I/DEBUG(30): ac40602c e59fe0ac e1a00007 e1a01006 e084400e 
02-08 12:11:01.604: I/DEBUG(30): ac40603c e0845105 e1a02008 e1a0e00f e595f2c0 
02-08 12:11:01.604: I/DEBUG(30): ac40604c e8bd81f0 e8bd81f0 e597098c e3a01b01 
02-08 12:11:01.604: I/DEBUG(30): ac40605c e281c008 e150000c 1affffe1 eafffff7 
02-08 12:11:01.604: I/DEBUG(30): ac40606c e3a01c05 e1a00007 e2811001 e8bd41f0 
02-08 12:11:01.604: I/DEBUG(30): stack:
02-08 12:11:01.604: I/DEBUG(30):     45f03ce0  45f03d34  
02-08 12:11:01.604: I/DEBUG(30):     45f03ce4  80834ab3  /system/lib/libdvm.so
02-08 12:11:01.604: I/DEBUG(30):     45f03ce8  8086deac  /system/lib/libdvm.so
02-08 12:11:01.604: I/DEBUG(30):     45f03cec  80846dad  /system/lib/libdvm.so
02-08 12:11:01.614: I/DEBUG(30):     45f03cf0  00000001  
02-08 12:11:01.614: I/DEBUG(30):     45f03cf4  0012fbb8  [heap]
02-08 12:11:01.614: I/DEBUG(30):     45f03cf8  43e460f8  /dev/ashmem/mspace/dalvik-heap/2 (deleted)
02-08 12:11:01.614: I/DEBUG(30):     45f03cfc  00239480  [heap]
02-08 12:11:01.614: I/DEBUG(30):     45f03d00  ac41b858  /system/lib/egl/libGLES_android.so
02-08 12:11:01.614: I/DEBUG(30):     45f03d04  00001100  
02-08 12:11:01.614: I/DEBUG(30):     45f03d08  00000077  
02-08 12:11:01.614: I/DEBUG(30):     45f03d0c  ac908e2c  /system/lib/libpixelflinger.so
02-08 12:11:01.614: I/DEBUG(30):     45f03d10  428a0ee8  
02-08 12:11:01.614: I/DEBUG(30):     45f03d14  00000000  
02-08 12:11:01.614: I/DEBUG(30):     45f03d18  df002777  
02-08 12:11:01.614: I/DEBUG(30):     45f03d1c  e3a070ad  
02-08 12:11:01.614: I/DEBUG(30): #00 45f03d20  ac41b008  /system/lib/egl/libGLES_android.so
02-08 12:11:01.614: I/DEBUG(30):     45f03d24  ac41b018  /system/lib/egl/libGLES_android.so
02-08 12:11:01.614: I/DEBUG(30):     45f03d28  00000003  
02-08 12:11:01.614: I/DEBUG(30):     45f03d2c  00239480  [heap]
02-08 12:11:01.614: I/DEBUG(30):     45f03d30  00000000  
02-08 12:11:01.614: I/DEBUG(30):     45f03d34  428a0ee8  
02-08 12:11:01.614: I/DEBUG(30):     45f03d38  428a0ed0  
02-08 12:11:01.614: I/DEBUG(30):     45f03d3c  ac40604c  /system/lib/egl/libGLES_android.so
02-08 12:11:01.614: I/DEBUG(30): #01 45f03d40  00118d78  [heap]
02-08 12:11:01.624: I/DEBUG(30):     45f03d44  00000003  
02-08 12:11:01.624: I/DEBUG(30):     45f03d48  00000000  
02-08 12:11:01.624: I/DEBUG(30):     45f03d4c  00000004  
02-08 12:11:01.624: I/DEBUG(30):     45f03d50  45f03d80  
02-08 12:11:01.624: I/DEBUG(30):     45f03d54  ad32061c  /system/lib/libandroid_runtime.so
02-08 12:11:02.084: I/BootReceiver(58): Copying /data/tombstones/tombstone_09 to DropBox (SYSTEM_TOMBSTONE)
02-08 12:11:02.094: D/Zygote(32): Process 328 terminated by signal (11)
02-08 12:11:02.174: D/dalvikvm(58): GC_FOR_MALLOC freed 2585 objects / 653024 bytes in 89ms
02-08 12:11:02.184: I/WindowManager(58): WIN DEATH: Window{4400b600 SurfaceView paused=false}
02-08 12:11:02.184: I/WindowManager(58): WIN DEATH: Window{4400b9c0 com.android/com.android.GLExample paused=false}
02-08 12:11:02.184: I/WindowManager(58): WIN DEATH: Window{43f5e388 com.android/com.android.Menu paused=false}
02-08 12:11:02.184: I/ActivityManager(58): Process com.android (pid 328) has died.
02-08 12:11:02.203: I/ActivityManager(58): Start proc com.android for activity com.android/.Menu: pid=335 uid=10040 gids={3003, 1015}
02-08 12:11:02.343: I/UsageStats(58): Unexpected resume of com.android while already resumed in com.android
02-08 12:11:02.354: I/ActivityManager(58): Displayed activity com.android/.Menu: 168 ms (total 1103 ms)
02-08 12:11:02.564: W/InputManagerService(58): Got RemoteException sending setActive(false) notification to pid 328 uid 10040
4

1 回答 1

2

它给出了“信号 11”,这是一个分段错误。因此,该程序正在尝试访问您没有权限或为空的内存位置。

您的代码停留在 glDrawElements(),因为您的 pBuff 分配不正确。

替换这个:

 ByteBuffer pbBuff = ByteBuffer.allocate(pIndex.length * 2);

有了这个:

 ByteBuffer pbBuff = ByteBuffer.allocateDirect(pIndex.length * 2);

它应该可以正常工作。

于 2013-02-08T23:10:23.247 回答