openGL ES 學習第一課(繪製三角形和四邊形)

openGL ES主要是用於嵌入式設備的3D圖形的繪製


GLSurfaceView      GLSurfaceView.Renderer


效果如下:


直接代碼:


1、GLRender.java

package wu.demo.www;

import java.nio.IntBuffer;

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

import android.opengl.GLSurfaceView.Renderer;

public class GLRender implements Renderer {
	
	int one = 0x10000;
	//三角形的三個頂點
	private int[] triggerBuffer = {
			0, one, 0, //上頂點
			-one, -one, 0, //下左
			one, -one, 0, //下右
	};
	
	private int[] quaterBuffer = {
			-one, one, 0,
			one, one, 0, 
			-one, -one, 0, 
			one, -one, 0
	};
	
	private IntBuffer getVertexBuffer(int capacity, int sum[]) {
		IntBuffer vertexBuffer = BufferFactory.createIntBuffer(capacity).put(sum);
		vertexBuffer.position(0);
		return vertexBuffer;
	}
	

	public void onDrawFrame(GL10 gl) {
		//清除屏幕和深度緩存
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();		
		//移動頂點,左移1.5,向裏6個單位
		gl.glTranslatef(-1.5f, 0.0f, -6.0f);
		//移動好了後,設置頂點
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		//繪製三角形
		gl.glVertexPointer(3, GL10.GL_FIXED, 0, getVertexBuffer(9, triggerBuffer));
		gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
		//重置觀察矩陣
		gl.glLoadIdentity();
		//移動頂點,移動到右邊
		gl.glTranslatef(1.5f, 0f, -6f);
		//繪製四邊形
		gl.glVertexPointer(3, GL10.GL_FIXED, 0, getVertexBuffer(12, quaterBuffer));
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
		//取消頂點設置
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		
	}

	public void onSurfaceChanged(GL10 gl, int width, int height) {
		float ratio = (float) width / height;
		gl.glViewport(0, 0, width, height);
		//設置投影矩陣
		gl.glMatrixMode(GL10.GL_PROJECTION);
		//重置投影矩陣
		gl.glLoadIdentity();
		gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		//告訴 系統對透視進行修正
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
		//白色背景
		gl.glClearColor(0, 0, 0, 0);
		//啓用陰影平滑
		gl.glShadeModel(GL10.GL_SMOOTH);
		//設置深度緩存
		gl.glClearDepthf(1.0f);
		//啓用深度測試
		gl.glEnable(GL10.GL_DEPTH_TEST);
		//所做深度測試 的類型
		gl.glDepthFunc(GL10.GL_LEQUAL);

	}

}


2、BufferFactory.java

package wu.demo.www;

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

/**
 * A utility class to create buffers.
 * 
 * All public methods are static. The Singleton pattern was avoided to avoid concerns about
 * threading and the Android life cycle. If needed, It can be implemented later given some research.
 */
public class BufferFactory {
	// This class cannot and should not be instantiated
	private BufferFactory() {}

	// We use Buffer.allocateDirect() to get memory outside of
	// the normal, garbage collected heap. I think this is done
	// because the buffer is subject to native I/O.
	// See http://download.oracle.com/javase/1.4.2/docs/api/java/nio/ByteBuffer.html#direct

	/**
	 * Creates a buffer of floats using memory outside the normal, garbage collected heap
	 * 
	 * @param capacity		The number of primitives to create in the buffer.
	 */
	public static FloatBuffer createFloatBuffer(int capacity) {
		// 4 is the number of bytes in a float
		ByteBuffer vbb = ByteBuffer.allocateDirect(capacity * 4);
		vbb.order(ByteOrder.nativeOrder());
		return vbb.asFloatBuffer();
	}

	/**
	 * Creates a buffer of shorts using memory outside the normal, garbage collected heap
	 * 
	 * @param capacity		The number of primitives to create in the buffer.
	 */
	public static ShortBuffer createShortBuffer(int capacity) {
		// 2 is the number of bytes in a short
		ByteBuffer vbb = ByteBuffer.allocateDirect(capacity * 2);
		vbb.order(ByteOrder.nativeOrder());
		return vbb.asShortBuffer();
	}
	
	public static IntBuffer createIntBuffer(int capacity) {
		ByteBuffer vbb = ByteBuffer.allocateDirect(capacity * 4);
		vbb.order(ByteOrder.nativeOrder());
		return vbb.asIntBuffer();
	}
}

3、OpenGLTest1Activity.java

package wu.demo.www;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;

public class OpenGLTest1Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        Renderer render = new GLRender();
        GLSurfaceView glView = new GLSurfaceView(this);
        glView.setRenderer(render);
        setContentView(glView);
    }
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章