Android OpenGL ES2.0 and GLSL 一個簡單的Demo

device-2016-07-22-112911.png

Demo 介紹

Android 的Samples上有一個例子, 里面包含了圖片中的正方形和三角形的繪制, 但是不包含紋理操作。

這個Demo中加了紋理貼圖, 以及紋理的變換(PS: 這里加了一個紋理模糊效果)。

Texture 代碼

package com.gank.demo.opengl.glsurfaceview;

import android.content.Context;
import android.opengl.GLES20;
import android.util.Log;

import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

/**
 * Created by GankLi on 2016/7/18.
 */

public class Texture {

    private final static int COORDS_POS_VERTEX = 3; // 頂點坐標寬度 (x,y,z)
    private final static int COORDS_TEX_VERTEX = 2; // 紋理坐標寬度 (x,y)

    private static final float[] Full_SquareCoords = new float[]{   //全屏顯示紋理 -- 從(-1, 1)逆時針繪制
            -1f, 1f, 0.0f, // Position 0                                   // (-1, 1)    (1, 1)
            -1f, -1f, 0.0f, // Position 1                                  // (-1,-1)    (1,-1)
            1f, -1f, 0.0f, // Position 2
            1f, 1f, 0.0f, // Position 3
    };

    private static final float[] Texture_SquareCoords = new float[]{ // 紋理坐標 - 從(0, 1)逆時針繪制
            0, 0f, // TexCoord 0                                              // (0, 1)   (1, 1)
            1.0f, 0f, // TexCoord 1                                           // (0, 0)   (1, 0)
            1.0f, 1, // TexCoord 2
            0, 1, // TexCoord 3
    };

    private static final short[] DrawOrder = new short[]{0, 1, 2, 2, 3, 0}; //Opengl 只支持三角形, 所以正方形的紋理是繪制兩次三角形

    private static final FloatBuffer VertexBuffer = GLHelper.floatArrayToBuffer(Full_SquareCoords);        // Java 層數據轉換成底層數據格式
    private static final FloatBuffer TextureBuffer = GLHelper.floatArrayToBuffer(Texture_SquareCoords);   // Java 層數據轉換成底層數據格式
    private static final ShortBuffer DrawListBuffer = GLHelper.shotArrayToBuffer(DrawOrder);                // Java 層數據轉換成底層數據格式

    private Context mContext;
    private final int mProgram;
    private int attrPosition;
    private int attrTexCoords;
    private int uniformProjection;
    private int uniformTexture;
    private int uniformSampler;

    private int uniformAryWeight;
    private int uniformAryVerticalOffset;

    private int mTextureId;

    private static final float SUM = 137;// (12 + 16 +12 ... 16 + 12)
    private static final float Blur = 90f; //(模糊因子)

    private static final float AryWeight[] = {12/ SUM,16/ SUM,12/ SUM,16/ SUM,25/ SUM,16/ SUM,12/ SUM,16/ SUM,12/ SUM}; // 網上找的一組數據
    private static final float VerticalOffset[] = {-1/Blur,-1/Blur, 0,-1/Blur, 1/Blur,-1/Blur                           // 網上找的一組數據
            -1/Blur, 0, 0, 0, 1/Blur, 0
            -1/Blur, 1/Blur, 0, 1/Blur, 1/Blur, 1/Blur};
    private static final FloatBuffer AryWeightBuffer = GLHelper.floatArrayToBuffer(AryWeight);
    private static final FloatBuffer VerticalOffsetBuffer = GLHelper.floatArrayToBuffer(VerticalOffset);

    public Texture(Context context) {
        mContext = context;

        GLES20.glEnable(GLES20.GL_TEXTURE_2D);
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

        mProgram = GLHelper.initProgram(GLScript.Texture.VertexShaderCode, GLScript.Texture.FragmentShaderCode);

        attrPosition = GLES20.glGetAttribLocation(mProgram, "position");
        attrTexCoords = GLES20.glGetAttribLocation(mProgram, "texCoords");
        uniformProjection = GLES20.glGetUniformLocation(mProgram, "projection");
        uniformTexture = GLES20.glGetUniformLocation(mProgram, "texture");
        uniformSampler = GLES20.glGetUniformLocation(mProgram, "sampler");
        uniformAryWeight = GLES20.glGetUniformLocation(mProgram, "g_aryWeight");
        uniformAryVerticalOffset = GLES20.glGetUniformLocation(mProgram, "g_aryVerticalOffset");
        mTextureId = GLHelper.loadTexture(mContext, "test.jpg");
    }

    /**
     * 繪制流程
     * 1: 設置 GLSL 的輸出參數 -- glUniformMatrix4fv
     * 2: 設置頂點和紋理的坐標 -- glVertexAttribPointer
     * 3: 最后繪制 -- glDrawElements
     * @param projectionMatrix
     * @param textureMatrix
     */
    public void draw(float[] projectionMatrix, float[] textureMatrix) {
        GLES20.glUseProgram(mProgram);
        GLES20.glEnableVertexAttribArray(attrPosition);
        GLES20.glEnableVertexAttribArray(attrTexCoords);
        // Set the sampler to texture unit 0
        GLES20.glUniform1i(uniformSampler, 0);

        GLES20.glUniformMatrix4fv(uniformProjection, 1, false, projectionMatrix, 0);
        GLES20.glUniformMatrix4fv(uniformTexture, 1, false, textureMatrix, 0);

        GLES20.glUniform1fv(uniformAryWeight,AryWeight.length, AryWeightBuffer);
        GLES20.glUniform2fv(uniformAryVerticalOffset,AryWeight.length, VerticalOffsetBuffer);

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureId);
        // load the position
        VertexBuffer.position(0);
        GLES20.glVertexAttribPointer(attrPosition,
                COORDS_POS_VERTEX, GLES20.GL_FLOAT,
                false, Full_SquareCoords.length, VertexBuffer);
        // load the texture coordinate
        VertexBuffer.position(0);
        GLES20.glVertexAttribPointer(attrTexCoords,
                COORDS_TEX_VERTEX, GLES20.GL_FLOAT,
                false, Texture_SquareCoords.length, TextureBuffer);

        GLES20.glDrawElements(GLES20.GL_TRIANGLES, DrawOrder.length, GLES20.GL_UNSIGNED_SHORT,
                DrawListBuffer);

        // Disable VertexBuffer array
        GLES20.glDisableVertexAttribArray(attrPosition);
        GLES20.glDisableVertexAttribArray(attrTexCoords);
    }

    static class Matrix {
        public final static float[] Projection = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
        public final static float[] Projection_TEST = {0.5f, 0, 0, 0, // 0.5 -- X軸縮放0.5
                0, 0.5f, 0, 0, // 0.5 -- 軸縮放0.5
                0, 0, 1, 0,
                0, 0, 0, 1};   // 0.5 -- 整體放大2倍

        public final static float[] Texture_Normal = {-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1};
        public final static float[] Texture_90 = {0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1};
        public final static float[] Texture_180 = {1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1};
    }
}

GLSL代碼

package com.gank.demo.opengl.glsurfaceview;

/**
 * @author Gank
 */

public class GLScript {
    public class Texture1 {
        public static final String VertexShaderCode =
                "attribute vec4 position;" +
                        "attribute vec2 texCoords;" + //紋理坐標
                        "varying vec2 outTexCoords;" + //頂點坐標
                        "void main() {" +
                        "gl_Position = position;" + //設置內置變量
                        "outTexCoords = texCoords;" + //設置紋理位置并傳給 FragmentShader
                        "}";

        public static final String FragmentShaderCode =
                "precision lowp float;" +
                        "varying vec2 outTexCoords;" + //接收 VertexShader 的參數
                        "uniform sampler2D sampler;" + //紋理采樣
                        "void main() {" +
                        "gl_FragColor = texture2D(sampler, outTexCoords);" + //根據位置(outTexCoords)獲取紋理上的顏色, 設置像素的顏色
                        "}";
    }

    public class Texture {
        public static final String VertexShaderCode =
                "attribute vec4 texCoords;" +  //紋理坐標
                        "attribute vec4 position;" + //頂點坐標
                        "uniform mat4 projection;" + //頂點矩陣 做旋轉
                        "uniform mat4 texture;" +     //紋理矩陣 做旋轉
                        "varying vec2 outTexCoords;" +
                        "void main() {" +
                        "gl_Position = projection * position;" + 
                        "outTexCoords = (texture * texCoords).st;" +
                        "}";

        public static final String FragmentShaderCode =
                "precision lowp float;" +
                        "const int g_iWeightNumber = 9;" +
                        "const float g_fGene = 0.7;" +
                        "uniform vec2 g_aryVerticalOffset[g_iWeightNumber];" + //采樣的位置便宜
                        "uniform float g_aryWeight[g_iWeightNumber]; " + //每個點的權重
                        "uniform sampler2D sampler;" +
                        "varying vec2 outTexCoords;" +
                        "void main() {" +
                        "vec4 vec4Sum = vec4(0.0);" +
                        "for(int i = 0; i < g_iWeightNumber; ++i)" +
                        "{" +
                        "vec4Sum += texture2D(sampler, outTexCoords + g_aryVerticalOffset[i])*g_aryWeight[i]*g_fGene;" + // X軸模糊
                        "vec4Sum += texture2D(sampler, outTexCoords - g_aryVerticalOffset[i])*g_aryWeight[i]*g_fGene;" + // Y軸模糊
                        "}" +
                        "gl_FragColor = vec4Sum;" +
                        "}";
    }
}

//TODO
代碼整理好了傳一份

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

相關閱讀更多精彩內容

友情鏈接更多精彩內容