build Shader for opengl - Android android.opengl

Android examples for android.opengl:OpenGL Shader

Description

build Shader for opengl

Demo Code

/**//  www .  j  ava  2 s  . co m
 * 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_SHADER_STATUS = new ThreadLocal<int[]>();

    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