渲染管線一般是由顯示芯片GPU內部處理圖形信號的并行處理單元組成,這些并行處理單元之間是獨立的,從另一個角度看,渲染管線實際上也是一系列繪制過程,這一系列過程的輸入是待繪制物體的相關描述信息,輸出的是要顯示的圖像幀數據。
OpenGL ES管線主要包括:
讀取頂點數據—>頂點著色器—>組裝圖元—>光柵化圖元—>片元著色器—>寫入幀緩沖區—>顯示到屏幕上
在OpenGL ES2.0中主要的兩個部分就是上面的可編程頂點著色器和片段著色器。學習OpenGL ES主要是要了解渲染管線,了解CPU的渲染過程,主要編程工作在于頂點著色器和片元著色器的編寫。
繪制一個六邊形
效果如圖所示
六邊形類
public class SixShape { private FloatBuffer mVertexBuffer; private FloatBuffer mColorBuffer; private int mProgram; private int mPositionHandle; private int mColorHandle; private int muMVPMatrixHandle; public SixShape(float r) { initVetexData(r); } public void initVetexData(float r) { // 初始化頂點坐標 float[] vertexArray = new float[8*3]; // 初始化頂點顏色 float[] colorArray=new float[8*4]; int j = 0, k = 0; vertexArray[j++] = 0; vertexArray[j++] = 0; vertexArray[j++] = 0; colorArray[k++] = 1; colorArray[k++] = 1; colorArray[k++] = 1; colorArray[k++] = 0; for (int angle = 0; angle <= 360; angle += 60) { vertexArray[j++] = (float) (r*Math.cos(Math.toRadians(angle))); vertexArray[j++] = (float) (r*Math.sin(Math.toRadians(angle))); vertexArray[j++] = 0; colorArray[k++] = 1; colorArray[k++] = 0; colorArray[k++] = 0; colorArray[k++] = 0; } ByteBuffer buffer = ByteBuffer.allocateDirect(vertexArray.length * 4); buffer.order(ByteOrder.nativeOrder()); mVertexBuffer = buffer.asFloatBuffer(); mVertexBuffer.put(vertexArray); mVertexBuffer.position(0); ByteBuffer cbb=ByteBuffer.allocateDirect(colorArray.length*4); cbb.order(ByteOrder.nativeOrder()); mColorBuffer=cbb.asFloatBuffer(); mColorBuffer.put(colorArray); mColorBuffer.position(0); int vertexShader = loaderShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode); int fragmentShader = loaderShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode); mProgram = GLES20.glCreateProgram(); GLES20.glAttachShader(mProgram, vertexShader); GLES20.glAttachShader(mProgram, fragmentShader); GLES20.glLinkProgram(mProgram); mPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition"); mColorHandle = GLES20.glGetAttribLocation(mProgram, "aColor"); muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); } public void draw(float[] mvpMatrix) { GLES20.glUseProgram(mProgram); // 將頂點數據傳遞到管線,頂點著色器 GLES20.glVertexAttribPointer(mPositionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, mVertexBuffer); // 將頂點顏色傳遞到管線,頂點著色器 GLES20.glVertexAttribPointer(mColorHandle, 4, GLES20.GL_FLOAT, false,4*4, mColorBuffer); // 將變換矩陣傳遞到管線 GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mvpMatrix, 0); GLES20.glEnableVertexAttribArray(mPositionHandle); GLES20.glEnableVertexAttribArray(mColorHandle); // 繪制圖元 GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 8); } private int loaderShader(int type, String shaderCode) { int shader = GLES20.glCreateShader(type); GLES20.glShaderSource(shader, shaderCode); GLES20.glCompileShader(shader); return shader; } private String vertexShaderCode = "uniform mat4 uMVPMatrix;" + "attribute vec3 aPosition;" + "attribute vec4 aColor;" + "varying vec4 aaColor;" + "void main(){" + "gl_Position = uMVPMatrix * vec4(aPosition,1);" + "aaColor = aColor;" + "}"; private String fragmentShaderCode = "precision mediump float;" + "varying vec4 aaColor;" + "void main(){" + "gl_FragColor = aaColor;" + "}";}
六邊形View
public class SixView extends GLSurfaceView{ public SixView(Context context) { super(context); setEGLContextClientVersion(2); setRenderer(new MyRender()); } class MyRender implements GLSurfaceView.Renderer { private SixShape circle; @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1); circle = new SixShape(0.5f); GLES20.glEnable(GLES20.GL_DEPTH_TEST); } // 投影矩陣 private final float[] mProjectionMatrix = new float[16]; // 視圖矩陣 private final float[] mViewMatrix = new float[16]; // 模型矩陣 private final float[] mMMatrix = new float[16]; private final float[] mViewProjectionMatrix = new float[16]; private final float[] mMVPMatrix = new float[16]; @Override public void onSurfaceChanged(GL10 gl, int width, int height) { GLES20.glViewport(0, 0, width, height); float ratio= (float) width / height; // 設置正交投影 Matrix.orthoM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 0, 5); // 設置視圖矩陣 Matrix.setLookAtM(mViewMatrix, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0); } @Override public void onDrawFrame(GL10 gl) { GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT); Matrix.multiplyMM(mViewProjectionMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0); // 設置模型矩陣 Matrix.setIdentityM(mMMatrix, 0); Matrix.translateM(mMMatrix,0,0,0,1); Matrix.rotateM(mMMatrix, 0, 30, 0, 0, 1); Matrix.multiplyMM(mMVPMatrix, 0, mViewProjectionMatrix, 0, mMMatrix, 0); circle.draw(mMVPMatrix); } }}
接下來在Activity中就可以使用這個View了。上面的例子雖然簡單,但是包括了使用OpenGL ES編程的主要流程,包括生成頂點數據,編寫頂點著色器,片元著色器,傳遞數據給頂點/片元著色器,這里最主要的就是著色器語言。此外包括投影,平移,旋轉等操作。在后面會詳細學習每個細節以及上面例子沒有涉及到的光照,紋理等OpenGL的知識。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答