Back to project page Tanks.
The source code is released under:
MIT License
If you think the Android project Tanks listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.ThirtyNineEighty.Renderable; //from ww w . j a va 2 s . c o m import android.opengl.GLES20; import android.util.Log; import com.ThirtyNineEighty.System.GameContext; import java.io.IOException; import java.io.InputStream; public abstract class Shader { protected static Shader current; private static Shader shader2D; private static Shader shader3D; protected int vertexShaderHandle; protected int fragmentShaderHandle; protected int programHandle; public abstract void compile(); protected abstract void getLocations(); public static Shader getCurrent() { return current; } public static void initShader3D() { if (shader3D != null) shader3D.deleteProgram(); shader3D = new Shader3D(); shader3D.compile(); } public static void initShader2D() { if (shader2D != null) shader2D.deleteProgram(); shader2D = new Shader2D(); shader2D.compile(); } public static void setShader3D() { if (current == shader3D) return; current = shader3D; GLES20.glUseProgram(shader3D.programHandle); } public static void setShader2D() { if (current == shader2D) return; current = shader2D; GLES20.glUseProgram(shader2D.programHandle); } public boolean validateProgram() { if (!GameContext.isDebuggable()) return true; int[] result = new int[1]; GLES20.glValidateProgram(programHandle); GLES20.glGetProgramiv(programHandle, GLES20.GL_VALIDATE_STATUS, result, 0); boolean validated = result[0] != 0; if (!validated) { Log.e("Error", "Program do not validated!"); if (!GLES20.glIsProgram(programHandle)) Log.e("Error", "Program handle deprecated!"); } return validated; } protected void compile(String vertexFileName, String fragmentFileName) { Log.d("Shader", String.format("compiler called for [vertex: %s, fragment: %s]", vertexFileName, fragmentFileName)); vertexShaderHandle = compileShader(GLES20.GL_VERTEX_SHADER, vertexFileName); fragmentShaderHandle = compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentFileName); programHandle = GLES20.glCreateProgram(); GLES20.glAttachShader(programHandle, vertexShaderHandle); GLES20.glAttachShader(programHandle, fragmentShaderHandle); GLES20.glLinkProgram(programHandle); GLES20.glReleaseShaderCompiler(); getLocations(); } private int compileShader(int type, String path) { int shaderHandle = 0; try { //load shader source shaderHandle = GLES20.glCreateShader(type); InputStream stream = GameContext.getAppContext().getAssets().open(path); int size = stream.available(); byte[] buffer = new byte[size]; int readCount = stream.read(buffer); if (readCount != size) Log.e("Error", "file read not fully: " + path); String source = new String(buffer); stream.close(); //compile shader GLES20.glShaderSource(shaderHandle, source); GLES20.glCompileShader(shaderHandle); //check for errors int[] compiled = new int[1]; GLES20.glGetShaderiv(shaderHandle, GLES20.GL_COMPILE_STATUS, compiled, 0); if (compiled[0] == 0) { Log.e("Error", GLES20.glGetShaderInfoLog(shaderHandle)); GLES20.glDeleteShader(shaderHandle); } } catch(IOException e) { Log.e("Error", e.getMessage()); } return shaderHandle; } private void deleteProgram() { GLES20.glDeleteShader(vertexShaderHandle); GLES20.glDeleteShader(fragmentShaderHandle); GLES20.glDeleteProgram(programHandle); } }