Android examples for App:Assets
build Program From Assets for opengl
/**//w ww . jav a 2 s . c o 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.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 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; } }