欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

4-OpenGLES绘制螺旋点、线

程序员文章站 2022-06-23 22:02:58
public class MyActivity extends Activity {private MyRenderer render;private MyGLSurfaceView view;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);view = new MyGLSurfaceView(this);render = new M....
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.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);
		}
	}

	public class MyRenderer implements GLSurfaceView.Renderer {
		private float ratio;
		private float xrotate = 0f;// 围绕x轴旋转角度
		private float yrotate = 0f;// 围绕x轴旋转角度
		
		@Override
		public void onSurfaceCreated(GL10 gl, EGLConfig config) {
			// 清屏色
			gl.glClearColor(0f, 0f, 0f, 1f);
			// 启用顶点缓冲区数组
			gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		}

		@Override
		public void onSurfaceChanged(GL10 gl, int width, int height) {
			// 设置视口
			gl.glViewport(0, 0, width, height);
			ratio = (float) width / (float) height;
			// 投影矩阵
			gl.glMatrixMode(GL10.GL_PROJECTION);
			// 加载单位矩阵
			gl.glLoadIdentity();
			// 设置平截头体
			gl.glFrustumf(-ratio, ratio, -1, 1, 3f, 7f);
		}

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

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

			// 计算点坐标
			float r = 0.5f;// 半径
			List<Float> coordsList = new ArrayList<Float>();
			float x = 0f, y = 0f, z = 1f;
			float zstep = 0.01f;
			for (float alpha = 0f; alpha < Math.PI * 6; alpha += (float) (Math.PI / 16)) {
				x = (float) (r * Math.cos(alpha));
				y = (float) (r * Math.sin(alpha));
				z = z - zstep;
				coordsList.add(x);
				coordsList.add(y);
				coordsList.add(z);
			}

			// 转换点成为缓冲区
			ByteBuffer ibb = ByteBuffer.allocateDirect(coordsList.size() * 4);
			ibb.order(ByteOrder.nativeOrder());
			FloatBuffer fbb = ibb.asFloatBuffer();
			for (float f : coordsList) {
				fbb.put(f);
			}
			ibb.position(0);

			// 指定顶点指针
			gl.glVertexPointer(3, GL10.GL_FLOAT, 0, ibb);
			gl.glDrawArrays(GL10.GL_POINTS, 0, coordsList.size() / 3);// 3个坐标表示一个点
		}
	}

	@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);
	}
}

4-OpenGLES绘制螺旋点、线

螺旋线:gl.glDrawArrays(GL10.GL_POINTS,...)->gl.glDrawArrays(GL10.GL_LINE_STRIP,......):

4-OpenGLES绘制螺旋点、线

图元:
1.点
    pionts:
2.线
    lines:线段集合
    line_strip,线带
    line_loop,线环
3.三角形

本文地址:https://blog.csdn.net/itzyjr/article/details/107303440