Android examples for android.opengl:OpenGL Shader
Method that load a fragment shader and returns its handler identifier for OpenGL.
/*// w w w . ja v a 2 s. c om * Copyright (C) 2014 Jorge Ruesga * * 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. */ import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.Rect; import android.media.effect.Effect; import android.opengl.GLES20; import android.opengl.GLUtils; import android.util.Log; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; public class Main{ private static final String TAG = "GLESUtil"; public static final boolean DEBUG_GL_MEMOBJS = false; public static final String DEBUG_GL_MEMOBJS_NEW_TAG = "MEMOBJS_NEW"; /** * Method that load a fragment shader and returns its handler identifier. * * @param src The source shader * @return int The handler identifier of the shader */ public static int loadFragmentShader(String src) { return loadShader(src, GLES20.GL_FRAGMENT_SHADER); } /** * Method that load a shader and returns its handler identifier. * * @param src The source shader * @param type The type of shader * @return int The handler identifier of the shader */ public static int loadShader(String src, int type) { int[] compiled = new int[1]; // Create, load and compile the shader int shader = GLES20.glCreateShader(type); if (GLESUtil.DEBUG_GL_MEMOBJS) { Log.d(GLESUtil.DEBUG_GL_MEMOBJS_NEW_TAG, "glCreateShader (" + type + "): " + shader); } GLESUtil.glesCheckError("glCreateShader"); if (shader <= 0) { Log.e(TAG, "Cannot create a shader"); return 0; } GLES20.glShaderSource(shader, src); GLESUtil.glesCheckError("glShaderSource"); GLES20.glCompileShader(shader); GLESUtil.glesCheckError("glesCheckError"); GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0); GLESUtil.glesCheckError("glesCheckError"); if (compiled[0] <= 0) { String msg = "Shader compilation error trace:\n" + GLES20.glGetShaderInfoLog(shader); Log.e(TAG, msg); return 0; } return shader; } /** * Method that checks if an GLES error is present * * @param func The GLES function to check * @return boolean If there was an error */ public static boolean glesCheckError(String func) { int error = GLES20.glGetError(); if (error != 0) { Log.e(TAG, "GLES20 Error (" + glesGetErrorModule() + ") (" + func + "): " + GLUtils.getEGLErrorString(error)); return true; } return false; } /** * Method that returns the line and module that generates the current error * * @return String The line and module */ private static String glesGetErrorModule() { try { return String .valueOf(Thread.currentThread().getStackTrace()[4]); } catch (IndexOutOfBoundsException ioobEx) { // Ignore } return ""; } }