Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import static android.opengl.GLES20.GL_COMPILE_STATUS;

import static android.opengl.GLES20.GL_VERTEX_SHADER;

import static android.opengl.GLES20.glCompileShader;

import static android.opengl.GLES20.glCreateShader;

import static android.opengl.GLES20.glDeleteShader;

import static android.opengl.GLES20.glGetShaderiv;

import static android.opengl.GLES20.glShaderSource;

import android.util.Log;

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

    public static int compileVertexShader(String shaderCode) {
        return compileShader(GL_VERTEX_SHADER, shaderCode);
    }

    private static int compileShader(int type, String shaderCode) {

        //create shader
        final int shaderObjectId = glCreateShader(type);

        if (shaderObjectId != 0) {
            //fill shader with the src code
            glShaderSource(shaderObjectId, shaderCode);

            //compile
            glCompileShader(shaderObjectId);

            //check for success
            final int[] status = new int[1];
            glGetShaderiv(shaderObjectId, GL_COMPILE_STATUS, status, 0);

            if (status[0] != 0) {
                //compilation succesfull
                return shaderObjectId;
            } else {
                //compilation failed
                glDeleteShader(shaderObjectId);
                Log.w(TAG, "compilation failed" + type);

                return 0;
            }

        } else {
            //creation failed
            Log.w(TAG, "Couldn't create shader.");

            return 0;
        }

    }
}