1. 程式人生 > >OpenGL——點的繪製(使用OpenGL來繪製可旋轉座標系的螺旋線)

OpenGL——點的繪製(使用OpenGL來繪製可旋轉座標系的螺旋線)

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

}