Search Projects

Tuesday, August 25, 2015

OpenGL ES Tutorial for Android - draw triangle in OpenGL ES

How to draw a triangle in Android OpenGL ES?

There are about billions of Android devices activated till date, and more will add to the list in coming days. These billions of devices have so many of games which uses the graphics to render the objects. These games use OpenGL API to show high performing graphics game.  These games are developed by designers, programmers, developers and others, but to begin this we need to start from a small step. Hence in this OpenGL ES Android tutorial we are going to learn how to draw a triangle in Android OpenGL ES.

Prerequisite

For this OpenGL ES Android tutorial you must know the OpenGL, basic Java, and about  Android Programming. Also your system should have the Android Studio (or Eclipse with ADT Plugin) and Android SDK installed. This Android game Development tutorial will use Android Studio.

Creating the new Project

1.       First Step in this OpenGL ES tutorial is to create a new project without any activity.
2.       Next we need to create 3 classes (3 separate java files) - “MainActivity.java” , “MainRenderer.java” and “MainSurfaceView.java”. These classes are essential for almost all OpenGL ES programming in Android. You can name them a Renderer, SurfaceView or whatever but it is good practice to do like this, it will help in understanding the files.

Main Activity

3.       In the MainActivity we are going to check if Android device support OpenGL ES with specific version. For this check out our previous OpenGL ES tutorial for Android.

MainSurfaceView

4.       Extend the MainSurfaceView with GLSurfaceView and create the constructor in the class. Also include all required imports. Following is the code snippet -

package in.ruks.openglobjects;
import android.content.Context;
import android.opengl.GLSurfaceView;
public class MainSurfaceView extends GLSurfaceView {
    public MainSurfaceView(Context context) {
        super(context);
    }
}

MainRenderer

5.       MainRenderer class will implement the GLSurfaceView.Renderer.  Call all the unimplemented methods in the class. Check the code below –

package in.ruks.openglobjects;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class MainRenderer implements GLSurfaceView.Renderer {
    @Override
    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
    }

    @Override
    public void onSurfaceChanged(GL10 gl10, int i, int i2) {
    }

    @Override
    public void onDrawFrame(GL10 gl10) {
    }
}

We are going to render our object here in this class and draw the object. 

Our Triangle Class

6.       Next Step is to create a new class, name  it “Triangle.java”.  This is the class where we are going to define vertices for our object (which is triangle). Here are going to define color for the vertices as well. We have to create buffer for each.
private FloatBuffer vertexBuffer;
    private float vertices[] = {
        0.0f , 0.5f,0.0f,
        -0.5f , -0.5f, 0.0f,
        0.0f , 0.5f, 0.0f
    };
    private float color[] = new float[]{ 0 .0f ,0.5f,0.0f,1.0f };  
           
First we have defined the buffer, which will contain the vertices. Next we have defined the vertices for each side of the triangle.



Final Thought

Reference

http://developer.android.com/reference/android/pm/ ConfigurationInfo.html
http://developer.android.com/guide/topics/graphics/opengl.html


No comments:

Post a Comment