OpenGL en Android

Introducción

Tenemos como objetivo el de entender como funciona el sistema de Visualización de Android.

Un primer ejemplo

Vamos ya a utilizar la plataforma Android 2.1.

android create project --target 11 --name HolaGL --path holaGL --activity HolaGL --package es.acsblog

El código fuente de ejemplo lo cogemos de la entrada de GLSurfaceView blog de Android:

holaGL$ cat src/es/acsblog/HolaGL.java
package es.acsblog;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

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

public class HolaGL extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mGLView = new GLSurfaceView(this);
        mGLView.setRenderer(new ClearRenderer());
        setContentView(mGLView);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mGLView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mGLView.onResume();
    }

    private GLSurfaceView mGLView;
}

class ClearRenderer implements GLSurfaceView.Renderer {
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // Do nothing special.
    }

    public void onSurfaceChanged(GL10 gl, int w, int h) {
        gl.glViewport(0, 0, w, h);
    }

    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    }
}
holaGL$ ant debug
Buildfile: build.xml
    [setup] Project Target: Google APIs
    [setup] Vendor: Google Inc.
    [setup] Platform Version: 2.1
    [setup] API level: 7
...
BUILD SUCCESSFUL
Total time: 4 seconds

Arrancamos el emulador y probamos a ver que hace:

holaGL$ emulator -avd a21.avd
holaGL$ adb install bin/HolaGL-debug.apk 
182 KB/s (13706 bytes in 0.073s)
        pkg: /data/local/tmp/HolaGL-debug.apk
Success

De momento no hace más que pintar una pantalla en negro. Para reinstalar la aplicación

holaGL$ adb shell rm /data/app/es.acsblog.apk
holaGL$ adb install bin/HolaGL-debug.apk

Para hacer algo más interesante, como pintar la pantalla en función de donde pongamos el dedo aquí va otro ejemplo:

package es.acsblog;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;


public class HolaGL2 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mGLView = new ClearGLSurfaceView(this);
        setContentView(mGLView);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mGLView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mGLView.onResume();
    }

    private GLSurfaceView mGLView;
}

class ClearGLSurfaceView extends GLSurfaceView {
    public ClearGLSurfaceView(Context context) {
        super(context);
        setDebugFlags(DEBUG_CHECK_GL_ERROR | DEBUG_LOG_GL_CALLS);
        mRenderer = new ClearRenderer2();
        setRenderer(mRenderer);
    }

    public boolean onTouchEvent(final MotionEvent event) {
        Log.i(this.getClass().getName(), "onTouchEvent");
        queueEvent(new Runnable(){
            public void run() {
                mRenderer.setColor(event.getX() / getWidth(),
                        event.getY() / getHeight(), 1.0f);
            }});
            return true;
        }

        ClearRenderer2 mRenderer;
}

class ClearRenderer2 implements GLSurfaceView.Renderer {
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // Do nothing special.
    }

    public void onSurfaceChanged(GL10 gl, int w, int h) {
        gl.glViewport(0, 0, w, h);
    }

    public void onDrawFrame(GL10 gl) {
        gl.glClearColor(mRed, mGreen, mBlue, 1.0f);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    }

    public void setColor(float r, float g, float b) {
        mRed = r;
        mGreen = g;
        mBlue = b;
    }

    private float mRed;
    private float mGreen;
    private float mBlue;
}

AndroidGL (last edited 2010-05-04 21:02:54 by AlvaroDelCastillo)