1. 程式人生 > >Android OpenGL教程 二 2D形狀畫畫

Android OpenGL教程 二 2D形狀畫畫

glm osi ret vertex depth 1.0 inter 開始 wrap

首先畫個三角形

    private float[] mTriangleArray={
            0.0f,1.0f,0.0f,
            1.0f,-1.0f,0.0f,
            -1.0f,-1.0f,0.0f
    };

gl.glDrawArrays(GL10.GL_TRIANGLES, 0, mTriangleArray.length/3);

然後畫個兩個三角形拼成一個正方形,把之前的坐標換一下就好了

    private float[] mTriangleArray={
            1.0f,1.0f,0.0f,
            1.0f,-1.0f,0.0f,
            -1.0f,-1.0f,0.0f,
            -1.0f,-1.0f,0.0f,
            -1.0f,1.0f,0.0f,
            1.0f,1.0f,0.0f
    };

看來glDrawArrays是三個三個坐標來畫的

我現在使用glDrawElements這個函數來畫正方形,這函數不僅可以畫多方形,而且還可以畫3D

    private float[] mTriangleArray={
            1.0f,1.0f,0.0f,
            1.0f,-1.0f,0.0f,
            -1.0f,-1.0f,0.0f,
            -1.0f,1.0f,0.0f
    };

    private float[] mColor ={
            1.0f,0.0f,0.0f,1.0f,
            1.0f,0.0f,0.0f,1.0f,
            1.0f,0.0f,0.0f,1.0f,
            1.0f,0.0f,0.0f,1.0f
    };
    private byte[] cubeFacets = new byte[]{
            0, 1, 2,
            0, 2, 3
    };

        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mTriangleBuffer);
        gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer);
        gl.glDrawElements(GL10.GL_TRIANGLE_STRIP
                , cubeFacetsBuffer.remaining(),
                GL10.GL_UNSIGNED_BYTE, cubeFacetsBuffer);

這函數的意思是你傳他畫三個點的坐標組的位置(第一個從0開始,一個坐標組有三個float),比如這個cubeFacets就是說0、1、2三個坐標連起來畫個三角形,然後0、2、3坐標畫個三角形。

以下是完整代碼

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 {

    private float[] mTriangleArray={
            1.0f,1.0f,0.0f,
            1.0f,-1.0f,0.0f,
            -1.0f,-1.0f,0.0f,
            -1.0f,1.0f,0.0f
    };

    private float[] mColor ={
            1.0f,0.0f,0.0f,1.0f,
            1.0f,0.0f,0.0f,1.0f,
            1.0f,0.0f,0.0f,1.0f,
            1.0f,0.0f,0.0f,1.0f
    };
    private byte[] cubeFacets = new byte[]{
            0, 1, 2,
            0, 2, 3
    };
    private FloatBuffer mTriangleBuffer;
    private FloatBuffer mColorBuffer;
    ByteBuffer cubeFacetsBuffer;
    public GLRenderer() {

        mTriangleBuffer = floatBufferUtil(mTriangleArray);
        mColorBuffer = 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);

        // 設置三角形
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mTriangleBuffer);
        // 設置三角形顏色
        gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer);
        // 繪制三角形
        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();

    }

    @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所需的FloatBuffer
private 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;
}

}

Android OpenGL教程 二 2D形狀畫畫