setup opengl Program - Android android.opengl

Android examples for android.opengl:OpenGL

Description

setup opengl Program

Demo Code


//package com.java2s;

import android.opengl.GLES20;

import android.util.Log;

public class Main {
    public static int setupProgram(String vxShaderCode, String frShaderCode) {
        int mProgram;
        int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vxShaderCode);
        int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER,
                frShaderCode);//from w w  w. ja v a  2  s . co  m
        if (vertexShader == 0 || fragmentShader == 0) {
            throw new RuntimeException("shader not loaded");
        }
        mProgram = GLES20.glCreateProgram(); // create empty OpenGL ES Program
        GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader
        // to program
        GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment
        // shader to program
        GLES20.glLinkProgram(mProgram);

        int[] linkStatus = new int[1];
        GLES20.glGetProgramiv(mProgram, GLES20.GL_LINK_STATUS, linkStatus,
                0);
        if (linkStatus[0] != GLES20.GL_TRUE) {
            Log.e("jay", "Could not link program: ");
            Log.e("jay", GLES20.glGetProgramInfoLog(mProgram));
            GLES20.glDeleteProgram(mProgram);
            mProgram = 0;
        }

        return mProgram;
    }

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

        // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
        // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
        int shader = GLES20.glCreateShader(type);

        // add the source code to the shader and compile it
        GLES20.glShaderSource(shader, shaderCode);
        GLES20.glCompileShader(shader);

        return shader;
    }
}

Related Tutorials