android openGL ES 01

实现功能:点击屏幕背景颜色变化。

 

三个类:

VortexRenderer,VortexView,Vortex

 

 

VortexRenderer

package org.jun.opengl01;

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

import android.opengl.GLSurfaceView;

public class VortexRenderer implements GLSurfaceView.Renderer {
	private static final String LOG_TAG = VortexRenderer.class.getSimpleName();

	private float _red = 0.9f;
	private float _green = 0.2f;
	private float _blue = 0.2f;
	
	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) {
		// define the color we want to be displayed as the "clipping wall"
		gl.glClearColor(_red, _green, _blue, 1.0f);
		// clear the color buffer to show the ClearColor we called above...
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
	}
	
	public void setColor(float r, float g, float b) {
		_red = r;
		_green = g;
		_blue = b;
	}
}

 

 

VortexView:

package org.jun.opengl01;

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

public class VortexView extends GLSurfaceView {
	private static final String LOG_TAG = VortexView.class.getSimpleName();
	private VortexRenderer _renderer;

	public VortexView(Context context) {
		super(context);
		_renderer = new VortexRenderer();
		setRenderer(_renderer);
	}

	public boolean onTouchEvent(final MotionEvent event) {
		queueEvent(new Runnable() {
			public void run() {
				_renderer.setColor(event.getX() / getWidth(), event.getY()
						/ getHeight(), 1.0f);
			}
		});
		return true;
	}
}


 

 

Vortex

package org.jun.opengl01;

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

public class Vortex extends Activity {
    /** Called when the activity is first created. */
    private static final String LOG_TAG = Vortex.class.getSimpleName();
    private VortexView _vortexView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    _vortexView = new VortexView(this);
    setContentView(_vortexView);
    }
}


 

效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值