android openGL ES2 一切從繪製紋理開始

來源:互聯網
上載者:User

android openGL ES2 一切從繪製紋理開始

紋理,在openGL中,可以理解為載入到顯卡顯存中的圖片。Android裝置在2.2開始支援openGL ES2.0,從前都是ES1.0 和 ES1.1的版本。簡單來說,openGL ES是為了嵌入裝置進行功能剪裁後的openGL版本。ES2.0是和1.x版本不相容的,區別和相容性參見android 官方文檔。

首先,android使用openGL提供了特殊的view作為基礎叫做GLSurfaceView。我們的view需要繼承GLSurfaceView。如下簡單樣本:

public class MyGLSurfaceView extends GLSurfaceView {     public MyGLSurfaceView(Context context) {        super(context);        setFocusableInTouchMode(true);         // Tell the surface view we want to create an OpenGL ES 2.0-compatible        // context, and set an OpenGL ES 2.0-compatible renderer.        this.setEGLContextClientVersion(2);         this.setRenderer(new MyRenderer());    } }

並沒有什麼特別之處,android view的渲染操作需要實現一個render介面,GLSurfaceView的渲染介面為android.opengl.GLSurfaceView.Renderer。我們需要實現介面的方法。

public class MyRenderer implements Renderer { public void onDrawFrame(GL10 gl) {} public void onSurfaceChanged(GL10 gl, int width, int height) {} public void onSurfaceCreated(GL10 gl, EGLConfig config) {} }

介面實現3個方法,對應繪製,繪製地區變化,地區建立。需要說明的是參數GL10 gl是openGL es1.x版本的對象。這裡我們不會使用到。還有一點就是,onDrawFrame方法的調用是有系統調用的,不需要手動調用。系統會以一定的頻率不斷的回調。

接下來我們進入ES2.0的使用,先上代碼:

public void onSurfaceCreated(GL10 gl, EGLConfig config) {    GLES20.glEnable(GLES20.GL_TEXTURE_2D);    // Active the texture unit 0    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);     loadVertex();    initShader();    loadTexture();}



繪製地區建立的時候,我們設定了啟用2D的紋理,並且啟用了紋理單元unit0。什麼意思呢,說起來話長,以後慢慢說。簡單說一下,記住openGL是基於狀態的,就是很多狀態的設定和切換,這裡啟用GL_TEXTURE_2D就是一個狀態的開啟,表明openGL可以使用2D紋理。

那神馬是啟用紋理單元,這個和硬體有點關係,openGL要顯卡會劃分儲存紋理的儲存地區不止一個地區。這裡是使用地區 unit 0,多重紋理繪製可以開啟多個,這個以後說。接下來,調用了三個函數,載入頂點,初始化著色器,載入紋理。


第一,載入頂點,openGL繪製圖形是根據頂點以後連結起來的。為什麼要這樣,其實這樣很強大是一種設計吧。頂點可以暫時簡單理解為含有位置資訊的座標點。展開代碼如下:

private void loadVertex() {    // float size  = 4    this.vertex = ByteBuffer.allocateDirect(quadVertex.length * 4)                            .order(ByteOrder.nativeOrder())                            .asFloatBuffer();     this.vertex.put(quadVertex).position(0);     // short size = 2    this.index = ByteBuffer.allocateDirect(quadIndex.length * 2)                           .order(ByteOrder.nativeOrder())                           .asShortBuffer();     this.index.put(quadIndex).position(0);} private FloatBuffer vertex;private ShortBuffer index; private float[] quadVertex = new float[] {        -0.5f, 0.5f, 0.0f, // Position 0        0, 1.0f, // TexCoord 0         -0.5f, -0.5f, 0.0f, // Position 1        0, 0, // TexCoord 1         0.5f , -0.5f, 0.0f, // Position 2        1.0f, 0, // TexCoord 2         0.5f, 0.5f, 0.0f, // Position 3        1.0f, 1.0f, // TexCoord 3}; private short[] quadIndex = new short[] {        (short)(0), // Position 0        (short)(1), // Position 1        (short)(2), // Position 2         (short)(2), // Position 2          (short)(3), // Position 3          (short)(0), // Position 0};

FloatBuffer,ShortBuffer是封裝了本機資料結構的封裝對象。是的,這個2個對象裡面的資料不被java虛擬機器管理,相當於C語言的儲存方式。具體的介紹可以參看這裡(想瞭解的猛擊)。 quadVertex的資料就是一個矩形的座標,和紋理座標。一兩句話很難解釋清楚,這裡涉及到openGL的幾個經典的座標系,下次說。概括的說,openGL的座標是單位化的,都是0.0-1.0的浮點型,螢幕的中心點是(0,0)。而紋理的座標左下角是(0,0)。 這裡的quadVertex是在螢幕中大概花了一個矩形貼了一個圖片, position0 是左上點,以後左下,右下,右上的順序,紋理座標同理。

quadIndx神馬意思呢,就是這剛才的這些頂點索引排列。這裡一個矩形也就4個頂點,每個頂點3個位置座標,2個紋理座標。也就是說一個頂點有5個float資料。至於為什麼頂點為什麼這麼排列下次說,是2個三角形合成了一個矩形,幾句話很難解釋清楚。

所以說,這段代碼就是把矩形的位置和紋理座標,儲存到本機資料,準備後面使用而已。

第二,初始化著色器。這個著色器就是ES2.0的特色,又叫可程式化著色器,也是區別於ES1.x的本質。這裡只做簡單的介紹。可程式化著色器是一種指令碼,文法類似C語言,指令碼分為頂點著色器和片段著色器,分別對應了openGL不同的渲染流程。如下:

頂點著色器:

uniform mat4 u_MVPMatrix; attribute vec4 a_position;attribute vec2 a_texCoord; varying vec2 v_texCoord;  void main()  {    gl_Position = a_position;    v_texCoord  = a_texCoord;    }

片段著色器:

precision lowp float;        varying vec2 v_texCoord;                       uniform sampler2D u_samplerTexture; void main()                                          {                                                      gl_FragColor = texture2D(u_samplerTexture, v_texCoord);}

這裡記住一句話,頂點著色器,會在頂點上執行;片段著色器會在像素點上執行。剛才的矩形就有4個頂點,每個頂點都會應用這個指令碼。也就是說,頂點是位置相關資訊,片段是色彩紋理相關資訊。

這個2段指令碼都是文本,需要編譯,連結,等等一些操作才能被ES2.0所使用。過程就像C語言的編譯運行過程。openGL 提供了相關函數去做這些事情。如下:

private void initShader() {    String vertexSource   = Tools.readFromAssets("VertexShader.glsl");    String fragmentSource = Tools.readFromAssets("FragmentShader.glsl");     // Load the shaders and get a linked program    program = GLHelper.loadProgram(vertexSource, fragmentSource);     // Get the attribute locations    attribPosition = GLES20.glGetAttribLocation(program, "a_position");    attribTexCoord = GLES20.glGetAttribLocation(program, "a_texCoord");     uniformTexture = GLES20.glGetUniformLocation(program, "u_samplerTexture");     GLES20.glUseProgram(program);    GLES20.glEnableVertexAttribArray(attribPosition);    GLES20.glEnableVertexAttribArray(attribTexCoord);    // Set the sampler to texture unit 0    GLES20.glUniform1i(uniformTexture, 0);}

可以看到,頂點和片段一起構成一個program,它可以被openGL所使用,是一個編譯好的指令碼程式,儲存在顯存。 GLES20.glGetAttribLocation 和 GLES20.glGetUniformLocation 這句話是神馬作用呢。簡單說就是,java程式和著色器指令碼資料通訊的。把就像參數的傳遞一樣,這樣指令碼就能根據外界的參數變化,即時的改變openGL流水線渲染的處理流程。


以下是我封裝的載入著色器的輔助方法:

public static int loadProgram(String vertexSource, String fragmentSource) {     // Load the vertex shaders    int vertexShader = GLHelper.loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);     // Load the fragment shaders    int fragmentShader = GLHelper.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);     // Create the program object    int program = GLES20.glCreateProgram();     if (program == 0) {        throw new RuntimeException("Error create program.");    }     GLES20.glAttachShader(program, vertexShader);    GLES20.glAttachShader(program, fragmentShader);     // Link the program    GLES20.glLinkProgram(program);     int[] linked = new int[1];     // Check the link status    GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linked, 0);     if (linked[0] == 0) {        GLES20.glDeleteProgram(program);        throw new RuntimeException("Error linking program: " +  GLES20.glGetProgramInfoLog(program));    }     // Free up no longer needed shader resources    GLES20.glDeleteShader(vertexShader);    GLES20.glDeleteShader(fragmentShader);     return program;}

public static int loadShader(int shaderType, String source) {      // Create the shader object    int shader = GLES20.glCreateShader(shaderType);     if (shader == 0) {        throw new RuntimeException("Error create shader.");    }     int[] compiled = new int[1];     // Load the shader source    GLES20.glShaderSource(shader, source);     // Compile the shader    GLES20.glCompileShader(shader);     // Check the compile status    GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);     if (compiled[0] == 0) {        GLES20.glDeleteShader(shader);        throw new RuntimeException("Error compile shader: " + GLES20.glGetShaderInfoLog(shader));    }     return shader;}

為什麼openGL的很多操作目標都是int類型的,因為openGL只會在顯存產生或綁定地址,返回id,以後用id相當於控制代碼去改變它的內部狀態。

第三,就是載入紋理了。載入紋理,就是把圖片的資料上傳到顯存,以後在使用它。請注意紋理圖片的長和寬最好是2的N次方,不然不一定能繪製出來。

static int[] loadTexture(String path) {     int[] textureId = new int[1];      // Generate a texture object     GLES20.glGenTextures(1, textureId, 0);      int[] result = null;      if (textureId[0] != 0) {          InputStream is = Tools.readFromAsserts(path);          Bitmap bitmap;         try {             bitmap = BitmapFactory.decodeStream(is);         } finally {             try {                 is.close();             } catch (IOException e) {                 throw new RuntimeException("Error loading Bitmap.");             }         }          result = new int[3];         result[TEXTURE_ID] = textureId[0]; // TEXTURE_ID         result[TEXTURE_WIDTH] = bitmap.getWidth(); // TEXTURE_WIDTH         result[TEXTURE_HEIGHT] = bitmap.getHeight(); // TEXTURE_HEIGHT          // Bind to the texture in OpenGL         GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[0]);          // Set filtering         GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);         GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);          GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);         GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);          // Load the bitmap into the bound texture.         GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);          // Recycle the bitmap, since its data has been loaded into OpenGL.         bitmap.recycle();      } else {         throw new RuntimeException("Error loading texture.");     }      return result; }

代碼一目瞭然,這裡使用了android的工具類吧bitmap直接轉換成openGL紋理需要的格式了。過程是,先產生一個紋理的id在顯卡上的,以後根據id上傳紋理資料,以後儲存這個id就可以操作這個紋理了。至於紋理的一些過濾特性設定,下次再說。


現在貌似就剩下繪製了,準備好了頂點資訊,頂點對應的紋理座標。初始化了著色器,上傳了紋理圖片。接下來就已把他們合起來繪製了。

public void onDrawFrame(GL10 gl) {    // clear screen to black    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);     vertex.position(0);    // load the position    // 3(x , y , z)    // (2 + 3 )* 4 (float size) = 20    GLES20.glVertexAttribPointer(attribPosition,                                  3, GLES20.GL_FLOAT,                                  false, 20, vertex);     vertex.position(3);    // load the texture coordinate    GLES20.glVertexAttribPointer(attribTexCoord,                                   2, GLES20.GL_FLOAT,                                  false, 20, vertex);     GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT, index);}

我儘力保持了代碼的簡單,openGL的基於狀態體現,bind這個函數無處不在,這裡bindTexture就是通知openGL使用那個id的紋理圖片。接下來的操作就是針對bind的圖片的。繪製就需要讓openGL知道繪製神馬。所以這裡需要用到vertex這個本機資料容器,裡面裝在的是頂點和紋理座標資訊。 GLES20.glVertexAttribPointer就是把頂點資料,按照openGL喜歡的格式上傳到顯卡儲存。draw方法的調用,是在前面應用了紋理id的情況下,所以繪製紋理座標的時候,會使用上傳的紋理圖片。


是的,每次都需要把資料上傳到openGL,畢竟顯存和記憶體不是同一個地方,openGL採用了用戶端-服務端的設計模式。當然使用VBO等技術可以把資料緩衝在顯存,提高運行效能。這個以後再說吧。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.