標籤:rip 三角形 2.0 surface pause 顏色 draw ntb 視窗
畫個三棱椎需要四個點,畫四個面
private float[] mTriangleArray={ -1.0f,-1.0f,0.0f, 1.0f,-1.0f,0.0f, 0.0f,1.0f,0.0f, 0.0f,0.0f,1.0f }; // 定義三棱椎的4個頂點的顏色 private float[] mColor={ 1.0f,0.0f,0.0f,0.0f, 0.0f,1.0f,0.0f,0.0f, 0.0f,0.0f,1.0f,0.0f, 1.0f,1.0f,0.0f,0.0f, }; private byte[] cubeFacets = new byte[]{ 0, 1, 2, 0, 2, 3, 2, 1, 3, 0, 1, 3 }; gl.glDrawElements(GL10.GL_TRIANGLE_STRIP , cubeFacetsBuffer.remaining(), GL10.GL_UNSIGNED_BYTE, cubeFacetsBuffer);
如果我們要畫個正方形就要八個點畫六個面,其他代碼不變
// 定義三棱椎的4個頂點的顏色
private float[] mColor={
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f,
1.0f,0.0f,0.0f,0.0f
};
// 定義立方體的8個頂點 float[] cubeVertices = new float[] { // 上頂面正方形的四個頂點 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, // 下底面正方形的四個頂點 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f }; // 定義立方體所需要的6個面(一共是12個三角形所需的頂點) private byte[] cubeFacets = new byte[]{ 0, 2, 1, 0, 2, 3, 2, 7, 3, 2, 7, 6, 0, 7, 3, 0, 7, 4, 4, 6, 5, 4, 6, 7, 1, 4, 0, 1, 4, 5, 1, 6, 2, 1, 6, 5 };
然後加個旋轉方便看清楚全貌
gl.glRotatef(rotate, 1f, 0f, 0f);
rotate++;
完整代碼如下
public class MainActivity extends Activity {
private GLSurfaceView mGLView;
public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); mGLView = new GLSurfaceView(this); //這裡使用的是自訂的GLSurfaceView的子類 mGLView.setRenderer(new GLRenderer()); setContentView(mGLView);}public void onPause(){ super.onPause(); mGLView.onPause();}public void onResume(){ super.onResume(); mGLView.onResume();}public class GLRenderer implements GLSurfaceView.Renderer { // 定義三棱椎的4個頂點的顏色 private float[] mColor={ 1.0f,0.0f,0.0f,0.0f, 1.0f,0.0f,0.0f,0.0f, 1.0f,0.0f,0.0f,0.0f, 1.0f,0.0f,0.0f,0.0f, 1.0f,0.0f,0.0f,0.0f, 1.0f,0.0f,0.0f,0.0f, 1.0f,0.0f,0.0f,0.0f, 1.0f,0.0f,0.0f,0.0f }; // 定義立方體的8個頂點 float[] cubeVertices = new float[] { // 上頂面正方形的四個頂點 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, // 下底面正方形的四個頂點 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f }; // 定義立方體所需要的6個面(一共是12個三角形所需的頂點) private byte[] cubeFacets = new byte[]{ 0, 2, 1, 0, 2, 3, 2, 7, 3, 2, 7, 6, 0, 7, 3, 0, 7, 4, 4, 6, 5, 4, 6, 7, 1, 4, 0, 1, 4, 5, 1, 6, 2, 1, 6, 5 }; private FloatBuffer mTriangleBuffer; ByteBuffer cubeFacetsBuffer; FloatBuffer taperColorsBuffer; public GLRenderer() { mTriangleBuffer = floatBufferUtil(cubeVertices); // 將三棱椎的四個定點的顏色數組封裝成IntBuffer taperColorsBuffer = floatBufferUtil(mColor); cubeFacetsBuffer = ByteBuffer.wrap(cubeFacets); } @Override public void onDrawFrame(GL10 gl) { // 清除螢幕和深度緩衝 gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); // 設定當前矩陣模式為模型視圖。 gl.glMatrixMode(GL10.GL_MODELVIEW); // 允許設定頂點 //GL10.GL_VERTEX_ARRAY頂點數組 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); // 允許設定顏色 //GL10.GL_COLOR_ARRAY顏色數組 gl.glEnableClientState(GL10.GL_COLOR_ARRAY); // 重設當前的模型觀察矩陣 gl.glLoadIdentity(); //將三角形在z軸上移動 gl.glTranslatef(0f, 0.0f, -2.0f); // 沿著X軸旋轉 gl.glRotatef(rotate, 1f, 0f, 0f); // 設定三角形 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mTriangleBuffer); // 設定頂點的顏色資料 gl.glColorPointer(4, GL10.GL_FIXED, 0, taperColorsBuffer); // 繪製三角形 gl.glDrawElements(GL10.GL_TRIANGLE_STRIP , cubeFacetsBuffer.remaining(), GL10.GL_UNSIGNED_BYTE, cubeFacetsBuffer); // 取消顏色設定 gl.glDisableClientState(GL10.GL_COLOR_ARRAY); // 取消頂點設定 gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); //繪製結束 gl.glFinish(); rotate++; } int rotate = 0; @Override public void onSurfaceChanged(GL10 gl, int width, int height) { float ratio = (float) width / height; // 設定OpenGL情境的大小,(0,0)表示視窗內部視口的左下角,(w,h)指定了視口的大小 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(); } @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { // 設定白色為清屏 gl.glClearColor(1, 1, 1, 1); }}// 定義一個工具方法,將float[]數群組轉換為OpenGL ES所需的FloatBufferprivate FloatBuffer floatBufferUtil(float[] arr){ FloatBuffer mBuffer; // 初始化ByteBuffer,長度為arr數組的長度*4,因為一個int佔4個位元組 ByteBuffer qbb = ByteBuffer.allocateDirect(arr.length * 4); // 數組排列用nativeOrder qbb.order(ByteOrder.nativeOrder()); mBuffer = qbb.asFloatBuffer(); mBuffer.put(arr); mBuffer.position(0); return mBuffer;}// 定義一個工具方法,將int[]數群組轉換為OpenGL ES所需的IntBufferprivate IntBuffer intBufferUtil(int[] arr){ IntBuffer mBuffer; // 初始化ByteBuffer,長度為arr數組的長度*4,因為一個int佔4個位元組 ByteBuffer qbb = ByteBuffer.allocateDirect(arr.length * 4); // 數組排列用nativeOrder qbb.order(ByteOrder.nativeOrder()); mBuffer = qbb.asIntBuffer(); mBuffer.put(arr); mBuffer.position(0); return mBuffer;}
}
Android OpenGL教程 三 3D隨便畫畫