build Program for opengl - Android android.opengl

Android examples for android.opengl:OpenGL

Description

build Program for opengl

Demo Code

/**//w w  w .j  a v a 2 s  . com
 * Copyright 2015 Michael Leahy / TyphonRT, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;

import android.util.Log;

import static android.opengl.GLES20.*;

public class Main {
    private static final String s_LOG_TAG = "AndroidGLES20Util";
    private static final ThreadLocal<int[]> s_BUILD_PROGRAM_STATUS = new ThreadLocal<int[]>();
    private static final ThreadLocal<int[]> s_BUILD_SHADER_STATUS = new ThreadLocal<int[]>();

    public static int buildProgram(String shaderSource, int shaderType) {
        int computeShader = buildShader(shaderSource, shaderType);
        if (computeShader == 0)
            return 0;

        int program = glCreateProgram();
        glAttachShader(program, computeShader);

        glLinkProgram(program);

        int[] status = s_BUILD_PROGRAM_STATUS.get();
        if (status == null) {
            status = new int[1];
            s_BUILD_PROGRAM_STATUS.set(status);
        }

        glGetProgramiv(program, GL_LINK_STATUS, status, 0);
        if (status[0] != GL_TRUE) {
            String error = glGetProgramInfoLog(program);
            Log.d(s_LOG_TAG, "Error while linking program:\n" + error);
            glDeleteShader(computeShader);
            glDeleteProgram(program);
            return 0;
        }

        return program;
    }

    public static int buildProgram(String vertexSource,
            String fragmentSource) {
        int vertexShader = buildShader(vertexSource, GL_VERTEX_SHADER);
        if (vertexShader == 0)
            return 0;

        int fragmentShader = buildShader(fragmentSource, GL_FRAGMENT_SHADER);
        if (fragmentShader == 0)
            return 0;

        int program = glCreateProgram();
        glAttachShader(program, vertexShader);
        glAttachShader(program, fragmentShader);

        glLinkProgram(program);

        int[] status = s_BUILD_PROGRAM_STATUS.get();
        if (status == null) {
            status = new int[1];
            s_BUILD_PROGRAM_STATUS.set(status);
        }

        glGetProgramiv(program, GL_LINK_STATUS, status, 0);
        if (status[0] != GL_TRUE) {
            String error = glGetProgramInfoLog(program);
            Log.d(s_LOG_TAG, "Error while linking program:\n" + error);
            glDeleteShader(vertexShader);
            glDeleteShader(fragmentShader);
            glDeleteProgram(program);
            return 0;
        }

        return program;
    }

    public static int buildShader(String source, int type) {
        int shader = glCreateShader(type);

        glShaderSource(shader, source);

        glCompileShader(shader);

        int[] status = s_BUILD_SHADER_STATUS.get();
        if (status == null) {
            status = new int[1];
            s_BUILD_SHADER_STATUS.set(status);
        }

        glGetShaderiv(shader, GL_COMPILE_STATUS, status, 0);
        if (status[0] != GL_TRUE) {
            String error = glGetShaderInfoLog(shader);
            Log.d(s_LOG_TAG, "Error while compiling shader:\n" + error);
            glDeleteShader(shader);
            return 0;
        }

        return shader;
    }
}

Related Tutorials