4-4 下標(biāo)矩陣

使用下標(biāo)畫矩陣

運(yùn)行圖

result.png

要點(diǎn)

  • 修改點(diǎn)的個(gè)數(shù),
  • 定義索引指向, 0,1,2, 0,2,3
  • 綁定 索引buffer 到指定的 target 上面去(GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexBufferId);)
  • 使用 glDrawElement() 函數(shù)來(lái)畫矩形

做法

  1. 修改點(diǎn)的個(gè)數(shù), 將點(diǎn)的個(gè)數(shù)修改為4個(gè).

    static final float squareCoords[] = {
            -0.5f,  0.5f, 0.0f,     1, 0, 0, 0.1f,  // top left
            -0.5f, -0.5f, 0.0f,     0, 1, 0, 0.1f, // bottom left
            0.5f, -0.5f, 0.0f,      0, 0, 1, 0.1f, // bottom right
            0.5f,  0.5f, 0.0f,      1, 1, 0, 0.1f,// top right
    };
    
  2. 定義索引指向,并綁定數(shù)據(jù)到 buffer

    • 定義下標(biāo)順序, 3個(gè)代表一個(gè)三角形.
    static  final short indexs[] = {
            0,1,2,  0,2,3
    };
    
    • 綁定下標(biāo)屬性到的 GL_ELEMENT_ARRAY_BUFFER 上面去

      IntBuffer intBuffer = IntBuffer.allocate(1);
      GLES20.glGenBuffers(1, intBuffer);
      indexBufferId = intBuffer.get(0);

      GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexBufferId);
      GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexs.length * 2, indexBuffer, GLES20.GL_STATIC_DRAW);

      創(chuàng)建 bufferID , 并將bufferID賦值給全局變量, 之后畫圖的時(shí)候調(diào)用.

    indexBuffer = BufferUtils.newShortBuffer(indexs.length);
    indexBuffer.put(indexs);
    indexBuffer.position(0);
    
    IntBuffer intBuffer = IntBuffer.allocate(1);
    GLES20.glGenBuffers(1, intBuffer);
    indexBufferId = intBuffer.get(0);
    
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexBufferId);
    GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexs.length * 2, indexBuffer, GLES20.GL_STATIC_DRAW);
    
  3. 繪制

        //public static native void glDrawElements(        
        //         int mode,
        //        int count,
        //        int type,
        //        int offset
        //);
    
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexBufferId);
            GLES20.glDrawElements(GLES20.GL_TRIANGLES, indexs.length , GLES20.GL_UNSIGNED_SHORT, 0);
    
    

    ?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容