Java tutorial
//package com.java2s; /* * Copyright 2016 nekocode * * 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.opengl.GLES20; import android.util.Log; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class Main { private static final String TAG = "MyGLUtils"; public static int buildProgram(Context context, int vertexSourceRawId, int fragmentSourceRawId) { return buildProgram(getStringFromRaw(context, vertexSourceRawId), getStringFromRaw(context, fragmentSourceRawId)); } public static int buildProgram(String vertexSource, String fragmentSource) { final int vertexShader = buildShader(GLES20.GL_VERTEX_SHADER, vertexSource); if (vertexShader == 0) { return 0; } final int fragmentShader = buildShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource); if (fragmentShader == 0) { return 0; } final int program = GLES20.glCreateProgram(); if (program == 0) { return 0; } GLES20.glAttachShader(program, vertexShader); GLES20.glAttachShader(program, fragmentShader); GLES20.glLinkProgram(program); return program; } private static String getStringFromRaw(Context context, int id) { String str; try { Resources r = context.getResources(); InputStream is = r.openRawResource(id); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int i = is.read(); while (i != -1) { baos.write(i); i = is.read(); } str = baos.toString(); is.close(); } catch (IOException e) { str = ""; } return str; } public static int buildShader(int type, String shaderSource) { final int shader = GLES20.glCreateShader(type); if (shader == 0) { return 0; } GLES20.glShaderSource(shader, shaderSource); GLES20.glCompileShader(shader); int[] status = new int[1]; GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, status, 0); if (status[0] == 0) { Log.e(TAG, GLES20.glGetShaderInfoLog(shader)); GLES20.glDeleteShader(shader); return 0; } return shader; } }