讀書筆記-----OpenGL ES概覽

<<Android.遊戲開發入門>>

CHAPTER 7: OpenGL ES: A Gentle Introduction

This means that the memory is
not allocated in the virtual machine’s heap memory, but in native heap memory.
NIO直接從本地堆內存分配  ,而不是VM 堆內存

float[] vertices = { ... definitions of vertex positions etc ...;//定義頂點
floatBuffer.clear();//重置buffer sets the position to zero and the limit to the capacity.
floatBuffer.put(vertices);//把頂點數據存入buffer
floatBuffer.flip();//切換模式  從寫入模式進入讀出模式  just swaps the position and limit.

we can only access OpenGL ES on the rendering
thread.

http://insanitydesign.com/wp/projects/nehe-android-ports/

2012-6-5

NOTE: While this method looks like it sets up a 2D coordinate system for us to render to, it
actually does not. It only defines the portion of the framebuffer OpenGL ES uses to output the
final image. Our coordinate system is defined via the projection and model-view matrices.
GL10.glViewport(int x, int y, int width, int height) 看起來像設置顯示,其實不是

opengl es 追蹤三個矩陣:projection matrix ,model-view matrix,texture matrix
在操作他們的數據前,先要告訴openglES  要操作哪個,使用:
GL10.glMatrixMode(int mode)
mode: 可選值 GL10.GL_PROJECTION, GL10.GL_MODELVIEW, or
GL10.GL_TEXTURE.

This matrix mode is one of OpenGL ES’s states (which will get lost when we
lose the context if our application is paused and resumed)
矩陣模式是opengl es的狀態之一(會隨應用程序狀態變化發生丟失)

gl.glClearColor(0,0,0,1);//用RGBA顏色清除屏幕
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);//執行清除
gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight());
gl.glMatrixMode(GL10.GL_PROJECTION);//設置matrix mode
gl.glLoadIdentity();//恢復單位矩陣
gl.glOrthof(0, 320, 0, 480, 1, -1);//projection matrix produced by glOrthof()

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);//要繪製頂點位置了
gl.glVertexPointer( 2, GL10.GL_FLOAT, 0, vertices);//一旦執行,頂點信息被存儲供後續使用
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
 
package com.test.opengles;

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

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

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
/**
 * OpenGL ES 繪製三角形測試
 * 按照作者所說,不要直接複製粘貼代碼了事,要明白每一步在做什麼,也不辜負作者用六頁詳細通俗苦心的解釋了:)
 * "
 * Now compare that to the six pages it took me to explain this to you. I
 * could have of course left out the details and used coarser language. The problem is that
 * OpenGL ES is a pretty complex beast at times, and to avoid getting an empty screen, it’s
 * best to learn what it is all about rather than just copying and pasting code.
 * "
 * @author Administrator
 *
 */
public class MainActivity extends Activity {
	GLSurfaceView glView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		glView = new GLSurfaceView(this);
		glView.setRenderer(new SimpleRenderer());
		setContentView(glView);
	}

	@Override
	public void onResume() {
		super.onPause();
		
		glView.onResume();
	}

	@Override
	public void onPause() {
		super.onPause();
		glView.onPause();
	}
	/**
	 * render
	 * @author Administrator
	 *
	 */
	private class SimpleRenderer implements Renderer {
		Random rand = new Random();

		@Override
		public void onSurfaceCreated(GL10 gl, EGLConfig config) {
			Log.d("GLSurfaceViewTest", "surface created");
			gl.glViewport(0, 0, glView.getWidth(), glView.getHeight());
			gl.glMatrixMode(GL10.GL_PROJECTION);//設置要操作的matrix mode
			gl.glLoadIdentity();//單位矩陣
			gl.glOrthof(0, 320, 0, 480, 1, -1);//設置projection
			/**
			 * 依賴NIO ByteBuffer來從本地堆分配內存
			 */
			ByteBuffer byteBuffer = ByteBuffer.allocateDirect(3 * 2 * 4);//或最好寫成float[].length*4
			byteBuffer.order(ByteOrder.nativeOrder());
			FloatBuffer vertices = byteBuffer.asFloatBuffer();
			vertices.put(new float[] { 0.0f, 0.0f,
			319.0f, 0.0f,
			160.0f, 479.0f });
			vertices.flip();//buffer模式切換,由寫變成讀  
			gl.glColor4f(1, 0, 0, 1);//設置RGBA顏色
			gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
			gl.glVertexPointer( 2, GL10.GL_FLOAT, 0, vertices);
		}

		@Override
		public void onSurfaceChanged(GL10 gl, int width, int height) {
			Log.d("GLSurfaceViewTest", "surface changed: " + width + "x"
					+ height);
		}
		private long start;
		
		@Override
		public void onDrawFrame(GL10 gl) {
			/**
			 * The 10 in GL10
			 * indicates that it offers us all the functions defined in
			 *  the OpenGL ES 1.0 standard.
			 * OpenGL ES 1.0標準
			 */
			
			gl.glClearColor(0,0,0,1);
			gl.glClear(GL10.GL_COLOR_BUFFER_BIT);//和setViewPort間無先後關係
			
			gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
		}
	}

}

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