OpenGL進階之關(guān)于生成紋理ID

  • 載入紋理的步驟:

    • GLES20.glGenTextures() : 生成紋理資源的句柄

    • GLES20.glBindTexture(): 綁定句柄

    • GLUtils.texImage2D() :將bitmap傳遞到已經(jīng)綁定的紋理中

    • GLES20.glTexParameteri() :設(shè)置紋理屬性,過濾方式,拉伸方式等

  • 紋理的數(shù)據(jù)來源

    • 將Bitmap以紋理的形式載入OpenGL中:

      /**
       * @param textureTarget Texture類型。
       *                      1. 相機用 GLES11Ext.GL_TEXTURE_EXTERNAL_OES
       *                      2. 圖片用GLES20.GL_TEXTURE_2D
       * @param minFilter     縮小過濾類型 (1.GL_NEAREST ; 2.GL_LINEAR)
       * @param magFilter     放大過濾類型
       * @param wrapS         X方向邊緣環(huán)繞
       * @param wrapT         Y方向邊緣環(huán)繞
       * @return 返回創(chuàng)建的 Texture ID
       */
      public static int createTexture(int textureTarget, @Nullable Bitmap bitmap, int minFilter,
                                      int magFilter, int wrapS, int wrapT) {
          int[] textureHandle = new int[1];
      
          GLES20.glGenTextures(1, textureHandle, 0);
          checkGlError("glGenTextures");
          GLES20.glBindTexture(textureTarget, textureHandle[0]);
          checkGlError("glBindTexture " + textureHandle[0]);
          GLES20.glTexParameterf(textureTarget, GLES20.GL_TEXTURE_MIN_FILTER, minFilter);
          GLES20.glTexParameterf(textureTarget, GLES20.GL_TEXTURE_MAG_FILTER, magFilter); //線性插值
          GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_WRAP_S, wrapS);
          GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_WRAP_T, wrapT);
      
          if (bitmap != null) {
              GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
          }
      
          checkGlError("glTexParameter");
          return textureHandle[0];
      }
      
    • 將 RGB 以紋理的形式載入OpenGL中:

      /***
      * 將 RGB 數(shù)據(jù) 轉(zhuǎn)化 成紋理 ID
      * @param videoW 紋理寬
      * @param videoH 紋理高
      * @param rgbData 視頻楨的 RGB 數(shù)據(jù)
      * @param textureId 因為視頻根據(jù)幀率刷新,頻繁調(diào)用onDrawFrame,
      *                     所以不適合多次創(chuàng)建紋理資源ID,
      *                     所以最好在onCreate創(chuàng)建好,免得OOM
      * @return 綁定好 RGB 數(shù)據(jù)的紋理 ID
      */
      public static int generateRGBTexture(int videoW, int videoH, byte[] rgbData, int textureId) {
            if (rgbData == null) {
                return -1;
            }
      
            ByteBuffer colorByteBuffer = null;
      
            if (colorByteBuffer == null) {
                colorByteBuffer = ByteBuffer.allocate(videoW * videoH * 4);
            }
      
            //生成紋理ID
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
            if (colorByteBuffer != null) {
                colorByteBuffer.clear();
                colorByteBuffer.put(rgbData, 0, videoW * videoH * 4);
            } else {
                return -1;
            }
      
            colorByteBuffer.position(0);
            GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0,
                      GLES20.GL_RGBA, videoW, videoH, 0,
                      GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, colorByteBuffer);
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
      
            return textureId;
      }
      
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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