10-OpenGLES绘制圆环

public class MyActivity extends Activity {
	private MyRenderer render;
	private MyGLSurfaceView view;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		view = new MyGLSurfaceView(this);
		render = new MyRenderer();
		view.setEGLConfigChooser(5, 6, 5, 0, 16, 4);
		view.setRenderer(render);
		view.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);// 脏渲染,命令渲染
		setContentView(view);
	}

	class MyGLSurfaceView extends GLSurfaceView {
		public MyGLSurfaceView(Context context) {
			super(context);
		}

		public MyGLSurfaceView(Context context, AttributeSet attrs) {
			super(context, attrs);
		}
	}

	class MyRenderer implements GLSurfaceView.Renderer {
		private float xrotate, yrotate;
		private float ratio = 0;

		@Override
		public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
			gl.glClearColor(0f, 0f, 0f, 1.0f);// clearColor
			gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);// vertex array
		}

		@Override
		public void onSurfaceChanged(GL10 gl, int w, int h) {// 设置视口
			gl.glViewport(0, 0, w, h);
			ratio = (float) w / h;
			gl.glMatrixMode(GL10.GL_PROJECTION);
			gl.glLoadIdentity();
			gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7);
		}

		@Override
		public void onDrawFrame(GL10 gl) {
			gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
			gl.glColor4f(0f, 0f, 1f, 1f);

			// 模型视图矩阵
			gl.glMatrixMode(GL10.GL_MODELVIEW);
			gl.glLoadIdentity();
			GLU.gluLookAt(gl, 0, 0, 5, 0, 0, 0, 0, 1, 0);

			// 旋转
			gl.glRotatef(xrotate, 1, 0, 0);
			gl.glRotatef(yrotate, 0, 1, 0);

			List<Float> coordsList = new ArrayList<Float>();
			float Rinner = 0.2f;// 内环半径
			float Rring = 0.3f;// 环半径
			int count = 20;
			float alphaStep = (float) (2 * Math.PI / count);
			float alpha = 0;
			float x0, y0, z0, x1, y1, z1;

			int count0 = 20;
			float betaStep = (float) (2 * Math.PI) / count0;
			float beta = 0;
			for (int i = 0; i < count; i++) {
				alpha = i * alphaStep;
				for (int j = 0; j <= count0; j++) {
					beta = j * betaStep;
					x0 = (float) (Math.cos(alpha) * (Rinner + Rring * (1 + Math.cos(beta))));
					y0 = (float) (Math.sin(alpha) * (Rinner + Rring * (1 + Math.cos(beta))));
					z0 = (float) (-Rring * Math.sin(beta));

					x1 = (float) (Math.cos(alpha + alphaStep) * (Rinner + Rring * (1 + Math.cos(beta))));
					y1 = (float) (Math.sin(alpha + alphaStep) * (Rinner + Rring * (1 + Math.cos(beta))));
					z1 = (float) (-Rring * Math.sin(beta));
					coordsList.add(x0);
					coordsList.add(y0);
					coordsList.add(z0);
					coordsList.add(x1);
					coordsList.add(y1);
					coordsList.add(z1);
				}
			}

			gl.glVertexPointer(3, GL10.GL_FLOAT, 0, list2FloatBuffer(coordsList));
			gl.glDrawArrays(GL10.GL_LINE_STRIP, 0, coordsList.size() / 3);
		}
	}

	private FloatBuffer list2FloatBuffer(List<Float> list) {
		ByteBuffer ibb = ByteBuffer.allocateDirect(list.size() * 4);
		ibb.order(ByteOrder.nativeOrder());
		FloatBuffer fbb = ibb.asFloatBuffer();
		for (Float f : list) {
			fbb.put(f);
		}
		fbb.position(0);
		return fbb;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		float step = 5f;
		if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
			render.xrotate -= step;
		} else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
			render.xrotate += step;
		} else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
			render.yrotate += step;
		} else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
			render.yrotate -= step;
		}
		// 请求渲染,和脏渲染配合使用
		view.requestRender();
		return super.onKeyDown(keyCode, event);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

itzyjr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值