9-OpenGLES绘制球体

9-OpenGLES绘制球体_第1张图片

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(1f, 0f, 0f, 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);

			// 计算球体坐标
			float R = 0.7f;// 球半径
			int statck = 8;// 水平层数
			float stackStep = (float) (Math.PI / statck);// 单位角度值

			int slice = 12;
			float sliceStep = (float) Math.PI / slice;// 水平圆递增的角度

			float r0, r1, y0, y1, x0, x1, z0, z1;

			float alpha0 = 0, alpha1 = 0;
			float beta = 0;

			List coordsList = new ArrayList();
			// 外层循环
			for (int i = 0; i < statck; i++) {
				alpha0 = (float) (-Math.PI / 2 + i * stackStep);
				alpha1 = (float) (-Math.PI / 2 + (i + 1) * stackStep);
				y0 = (float) (R * Math.sin(alpha0));
				r0 = (float) (R * Math.cos(alpha0));
				y1 = (float) (R * Math.sin(alpha1));
				r1 = (float) (R * Math.cos(alpha1));
				// 循环每一层圆
				for (int j = 0; j <= slice * 2; j++) {
					beta = j * sliceStep;
					x0 = (float) (r0 * Math.cos(beta));
					z0 = -(float) (r0 * Math.sin(beta));
					x1 = (float) (r1 * Math.cos(beta));
					z1 = -(float) (r1 * Math.sin(beta));
					coordsList.add(x0);
					coordsList.add(y0);
					coordsList.add(z0);
					coordsList.add(x1);
					coordsList.add(y1);
					coordsList.add(z1);
				}
			}

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

	private FloatBuffer list2FloatBuffer(List 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);
	}
}

你可能感兴趣的:(OpenGL超级宝典视频教程)