2022年9月4日

Android Opengl 讀取shader





import android.opengl.GLES20;


public class Shader_info {


    public  int mProgram;
    private   int vertexShader;
    private   int fragmentShader;

    public  void create(String  vertexShaderCode,String fragmentShaderCode){

        vertexShader = loadShader(GLES20.GL_VERTEX_SHADER,
                vertexShaderCode);
        fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER,
                fragmentShaderCode);
        mProgram = GLES20.glCreateProgram();

        // add the vertex shader to program
        GLES20.glAttachShader(mProgram, vertexShader);
        // add the fragment shader to program
        GLES20.glAttachShader(mProgram, fragmentShader);
        // creates OpenGL ES program executables
        GLES20.glLinkProgram(mProgram);


        GLES20.glDeleteShader(vertexShader);
        GLES20.glDeleteShader(fragmentShader);



    }
    public  void delete(){
        GLES20.glDeleteProgram(mProgram);
    }


    private static int loadShader(int type, String shaderCode){


        int shader = GLES20.glCreateShader(type);
        GLES20.glShaderSource(shader, shaderCode);
        GLES20.glCompileShader(shader);


        int[] compiled = new int[1];
        GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
        if (compiled[0] == 0) {

            throw new RuntimeException("Error loading shader.");
        }


        return shader;
    }


}

沒有留言:

張貼留言