Helper function to compile a OpenGL shader. - Android android.opengl

Android examples for android.opengl:OpenGL Shader

Description

Helper function to compile a OpenGL shader.

Demo Code


//package com.java2s;

import android.opengl.GLES20;

import android.util.Log;

public class Main {
    private static final String TAG = "GLES20Helper";

    /**/*from w w  w.j a v  a 2  s.  c o m*/
     * Helper function to compile a shader.
     * 
     * @param shaderType
     *            The shader type.
     * @param shaderSource
     *            The shader source code.
     * @return An OpenGL handle to the shader.
     */
    public static int compileShader(final int shaderType,
            final String shaderSource) {
        int shaderHandle = GLES20.glCreateShader(shaderType);

        if (shaderHandle != 0) {
            // Pass in the shader source.
            GLES20.glShaderSource(shaderHandle, shaderSource);

            // Compile the shader.
            GLES20.glCompileShader(shaderHandle);

            // Get the compilation status.
            final int[] compileStatus = new int[1];
            GLES20.glGetShaderiv(shaderHandle, GLES20.GL_COMPILE_STATUS,
                    compileStatus, 0);

            // If the compilation failed, delete the shader.
            if (compileStatus[0] == 0) {
                Log.e(TAG,
                        "Error compiling shader: "
                                + GLES20.glGetShaderInfoLog(shaderHandle));
                GLES20.glDeleteShader(shaderHandle);
                shaderHandle = 0;
            }
        }

        if (shaderHandle == 0) {
            throw new RuntimeException("Error creating shader.");
        }

        return shaderHandle;
    }
}

Related Tutorials