1.android opengl入門の一つ


package com.vortex;

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

public class Vortex extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        VortexView view = new VortexView(this);
        setContentView(view);
    }
}
package com.vortex;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.Log;
import android.view.MotionEvent;

public class VortexView extends GLSurfaceView {

	private VortexRenderer renderer;
	
	public VortexView(Context context) {
		super(context);
		renderer = new VortexRenderer();
		setRenderer(renderer);
	}
	
	@Override
    public boolean onTouchEvent(final MotionEvent event) {
    	 queueEvent(new Runnable() {
    		 public void run() {
    			 Log.e(""+event.getX()/getWidth(), ""+event.getY()/getHeight());
    			 renderer.setColor(event.getX()/getWidth(), event.getY()/getHeight(),(float)Math.random());
    		 }
    	 });
		return true;
	}
}

 
 
package com.vortex;

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

import android.opengl.GLSurfaceView;

public class VortexRenderer implements GLSurfaceView.Renderer {

	private float red = 0.9f;
    private float green = 0.2f;
    private float blue = 0.2f;

    public void setColor(float red, float green, float blue) {
    	this.red = red;
    	this.blue = blue;
    	this.green = green;
    }
    
	@Override
	public void onDrawFrame(GL10 gl) {
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		gl.glClearColor(red, green, blue, 1.0f);
	}

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

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		
	}
}