ESTriangle Activity


package com.vizit;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.opengl.GLSurfaceView;

/**
 * Basic setup of an OpenGL 2 environment. Since this is an activity, we can deliver an intent
 * to it and cause it to be started. We create the GLSurfaceView and set it as the view for this
 * activity. Our GLSurfaceView creates a TriangleRenderer that uses OpenGL to draw to the screen.
 * Extends Activity, so execution starts with the onCreate method.
 */
public class ESTriangle
       extends Activity
{
    private GLSurfaceView view;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // Create a GLSurfaceView instance and set it
        // as the ContentView for this Activity.
        view = new MyGLSurfaceView(this);
        setContentView(view);
    }

    /**
     *  Invoked when this activity returns to the foreground.
     *  Forwarding to the view restarts the the rendering thread when the activity is in the foreground.
     */
    @Override
    protected void onResume()
    {
        // The activity must call the GL surface view's onResume() on activity onResume().
        super.onResume();
        view.onResume();
    }

    /**
     * Invoked when another activity comes to the foreground.
     * Forwarding to the view stops the rendering thread when the activity is not in the foreground.
     */
    @Override
    protected void onPause()
    {
        // The activity must call the GL surface view's onPause() on activity onPause().
        super.onPause();
        view.onPause();
    }

    /**
     * Provides a surface we can draw on.
     * GLSurfaceView
     * allows you to supply a
     * Renderer
     * that does the actual drawing onto the surface.
     *
     * This constructor is focused on the creation of the renderer. The context passed into the
     * constructor is just the activity.
     */
    class MyGLSurfaceView extends GLSurfaceView
    {

        public MyGLSurfaceView(Context context)
        {
            super(context);
            // We are using the OpenGL ES 2 API. setEGLContextClientVersion defines the OpenGL version in
            // use for the default EGLContexFactory and EGLConfigChooser. EGL is another API that controls
            // things like graphics contexts and rendering surfaces, but we will not use it directly.
            setEGLContextClientVersion(2);
            // Set the Renderer for drawing on the GLSurfaceView
            setRenderer(new TriangleRenderer());
            // setRenderMode RENDERMODE_WHEN_DIRTY only rerenders the scene when requestRender is called,
            // which we will do in later examples.
            setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
        }
    }
}
          

Notes

The first thing I did is probably one of the least important as you get going. This prevents Google Play from providing your app to devices that don't at least support OpenGL ES 2. We are all going to run out and build a commercial app, right?


   <uses-feature android:glEsVersion="0x00020000" android:required="true" />
        

Luckly it rapidly gets a more interesting. As soon as the application is created, we create a GLSurfaceView. First thing to notice is that things associated with OpenGL often start with GL. We can think of the GLSurfaceView as representing a region of screen that we can render, or draw, onto. Since we set it as the content view it occupies all the screen space available to the application.


    private GLSurfaceView view;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // Create a GLSurfaceView instance and set it
        // as the ContentView for this Activity.
        view = new MyGLSurfaceView(this);
        setContentView(view);
    }