Java tutorial
//package com.java2s; /** * Copyright 2015 Michael Leahy / TyphonRT, Inc. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import android.content.res.Resources; import android.util.Log; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; 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 buildProgramFromAssets(Resources resources, String shaderFileName, int shaderType) { String shaderSource = loadFromAssets(resources, shaderFileName); return buildProgram(shaderSource, shaderType); } public static int buildProgramFromAssets(Resources resources, String shaderFileName, int shaderType, String shaderHeader) { String shaderSource = loadFromAssets(resources, shaderFileName); return buildProgram(shaderHeader + shaderSource, shaderType); } public static int buildProgramFromAssets(Resources resources, String vertexFileName, String fragmentFileName) { String vertexSource = loadFromAssets(resources, vertexFileName); String fragmentSource = loadFromAssets(resources, fragmentFileName); return buildProgram(vertexSource, fragmentSource); } private static String loadFromAssets(Resources resources, String fileName) { BufferedReader reader = null; StringBuilder sb = new StringBuilder(); try { reader = new BufferedReader(new InputStreamReader(resources.getAssets().open(fileName), "UTF-8")); // do reading, usually loop until end of file reading String mLine = reader.readLine(); while (mLine != null) { sb.append(mLine).append('\n'); mLine = reader.readLine(); } } catch (IOException e) { //log the exception } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { //log the exception } } } return sb.toString(); } 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; } }