Skip to content
Search

Android:OpenGLES

lastmod 2018-08-17created 2014-03-179 edits

A summary of how to use OpenGLES on Android.

Caveats when using Opengl ES 2.0 on Android

If you just start using ES 2.0 features blindly, you will see an error log like the one below.

called unimplemented OpenGL ES API

The error log above means 2.0 cannot be used yet; you need to set the following in the GLSurfaceView.

setEGLContextClientVersion(2);

How to print Text using OpenGL on Android is shown below.

Create a Text Bitmap.

Bitmap bitmap = Bitmap.createBitmap( 64, 64, Bitmap.Config.ARGB_4444 );
Canvas canvas = new Canvas( bitmap );
bitmap.eraseColor( 0 );
Paint textPaint = new Paint();
textPaint.setTextSize( 18 );
textPaint.setAntiAlias( true );
textPaint.setARGB( 0xff, 0xff, 0xff, 0xff );
textPaint.setTextAlign( Paint.Align.LEFT );
textPaint.setTextScaleX( 0.5f );
canvas.drawText( str, 0.f, 15.f , textPaint);

Bind the Bitmap to a Texture.

gl.glGenTextures( 1, mTextures, 0 );
gl.glBindTexture( GL10.GL_TEXTURE_2D, mTextures[ 0 ] );
gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST );
gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR );
gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT );
gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT );
GLUtils.texImage2D( GL10.GL_TEXTURE_2D, 0, bitmap, 0 );
bitmap.recycle();

Apply the Texture.

gl.glEnableClientState( GL10.GL_VERTEX_ARRAY );
gl.glEnableClientState( GL10.GL_TEXTURE_COORD_ARRAY );
gl.glVertexPointer( NUMBER_OF_AXIS, GL10.GL_FLOAT, 0, mImageRectVertexBuffer );
gl.glBindTexture( GL10.GL_TEXTURE_2D, mTextures[ 0 ] );
gl.glTexCoordPointer( 2, GL10.GL_FLOAT, 0, mUVBuffer );
gl.glDrawElements( GL10.GL_TRIANGLES, INDICES.length, GL10.GL_UNSIGNED_SHORT, mIndicesBuffer);

Android opengles tutorial list

See the files below.

  • Android OpenGL 01 Writing the basic structure: Berabue_android_opengl_01.pdf
  • Android OpenGL 02 Drawing triangles and rectangles: Berabue_android_opengl_02.pdf
  • Android OpenGL 03 Applying textures: Berabue_android_opengl_03.pdf
  • Android OpenGL 04 Cleaning up the basic source: Berabue_android_opengl_04.pdf
  • Android OpenGL function descriptions: Berabue_android_opengl_functions.pdf

See also

classes

Favorite site

Guide

FAQ

References


  1. Android_app_tutorial10_graphics_2d_and_3d_with_opengl.pdf ↩

  2. Android_opengles_2.0_introduction_and_lecture.pdf ↩

  3. Opengles_loadtexture_relation.pdf ↩