2D triangle with OpenGL ES on Android

by Anar Bayramov   Last Updated January 13, 2018 21:13 PM

I am trying to draw a triangle on OpenGL ES. I followed a tutorial to do it. even though everything seems to be fine it does not work. Everytime I run my code it sends me back to main menu.

this is my GLExample.java class:

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

this is my GLTriangleEX class:

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

and this is my GLRendererEx.java class:

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

    }

}

and my 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


Answers 1


Did you try adding the activity on the Android Manifest too?

<activity
        android:name="com.example.PROGRAM_NAME.GLEXAMPLE"
        android:label="@string/app_name" >
    </activity>
Diana Lam
Diana Lam
March 05, 2013 00:25 AM

Related Questions


OpenGLES texture coordinates in shader

Updated December 16, 2016 08:05 AM



OpenGL ES 2.0 2D-Image displaying

Updated April 21, 2015 21:05 PM

OpenGL ES God Ray Precision error

Updated July 25, 2015 13:05 PM