OpenGL——点的绘制(使用OpenGL来绘制可旋转坐标系的螺旋线)

时间:2022-09-10 17:48:46


package com.example.opengl1;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

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

import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.Menu;

/**
 * 下面这个demo实现了画一个红色的三角形
 * @author pc
 *
 */
public class MainActivity extends Activity {

	MyPointRender1 render;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		MyGLSurfaceView view = new MyGLSurfaceView(this);
		
		render = new MyPointRender1();
		
		//renderer: 渲染器..
		view.setRenderer(render);
		
		setContentView(view);
	}

	class MyGLSurfaceView extends GLSurfaceView{
		public MyGLSurfaceView(Context context) {
			super(context);
		}
		
		public MyGLSurfaceView(Context context,AttributeSet attrs) {
			super(context, attrs);
		}
	}
	
	/**
	 * 用来控制整个坐标系的旋转
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		float step = 5f;
		
		if(keyCode == KeyEvent.KEYCODE_DPAD_UP){
			render.xrotate = render.xrotate - step;
		}else if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN){
			render.xrotate = render.xrotate + step;
		}else if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT){
			render.yrotate = render.yrotate + step;
		}else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){
			render.yrotate = render.yrotate - step;
		}
		
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


package com.example.opengl1;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;

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

import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;

public class MyPointRender1 implements Renderer {

	public float xrotate = 0f;
	public float yrotate = 0f;
	
	/**
	 * 3.
	 */
	@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, 5f, 0, 0, 0, 0, 1, 0);
		
		/**
		 * gl.glRotatef(-90f, 1, 0, 0):
		 * 设置旋转角度
		 * 
		 * gl.glRotatef(-90f, 1, 0, 0):中1,0,0.表示绕x轴旋转
		 * 第一个參数表示旋转角度。-90f表示顺时针旋转90度.90f表示逆时针旋转90度
		 * 
		 * 旋转以后的坐标是意图是这种
		 *      
		 *     /|\z /y
		 *      |  /
		 *      | /
		 *      /------->x
		 *     
		 *    
		 *      
		 */
		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;
		float y = 0f;
		float z = 1.5f;
		float zstep = 0.005f;
		
		for(float alpha = 0f ; alpha < Math.PI*6 ; alpha = (float)(alpha + Math.PI/32)){
			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);
		
	}

	/**
	 * 2.
	 */
	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		gl.glViewport(0, 0, width, height);
		float ratio = (float)width/(float)height;
		
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		gl.glFrustumf(-1f, 1f, -ratio, ratio, 3f, 7f);
	}

	/**
	 * 1.
	 */
	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		gl.glClearColor(0, 0, 0, 1f);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
	}

}