create opengl Program - Android android.opengl

Android examples for android.opengl:OpenGL

Description

create opengl Program

Demo Code


//package com.java2s;

import android.opengl.GLES20;

public class Main {
    public static int createProgram(String vertexShaderCode,
            String fragmentShaderCode) {
        int vertexShader = compileVertexShader(vertexShaderCode);
        int fragmentShader = compileFragmentShader(fragmentShaderCode);
        return linkProgram(vertexShader, fragmentShader);
    }/*from ww w.  j  av  a 2s  .c  o m*/

    public static int compileVertexShader(String shaderCode) {
        return compileShader(GLES20.GL_VERTEX_SHADER, shaderCode);
    }

    public static int compileFragmentShader(String shaderCode) {
        return compileShader(GLES20.GL_FRAGMENT_SHADER, shaderCode);
    }

    public static int linkProgram(int vertexShaderId, int fragmentShaderId) {
        final int program = GLES20.glCreateProgram();

        GLES20.glAttachShader(program, vertexShaderId);
        GLES20.glAttachShader(program, fragmentShaderId);
        GLES20.glLinkProgram(program);

        return program;
    }

    private static int compileShader(int type, String shaderCode) {
        final int shader = GLES20.glCreateShader(type);
        GLES20.glShaderSource(shader, shaderCode);
        GLES20.glCompileShader(shader);
        return shader;
    }
}

Related Tutorials