compile opengl Shader Program - Java javax.media.opengl

Java examples for javax.media.opengl:Shader

Description

compile opengl Shader Program

Demo Code


import javax.media.opengl.GL2;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.charset.Charset;

public class Main{
    public static int compileShaderProgram(GL2 gl, int[] shaders) {
        int shaderProgram = gl.glCreateProgram();

        if (shaderProgram < 1) {
            System.err.println("Could not create Shader Program");
        } else {//from  w  w  w. j av  a2 s .  c o m
            for (int shader : shaders) {
                gl.glAttachShader(shaderProgram, shader);
            }

            gl.glLinkProgram(shaderProgram);
            gl.glValidateProgram(shaderProgram);

            String status = GLUtils.getProgramStatus(gl, shaderProgram,
                    GL2.GL_LINK_STATUS);
            if (status != null) {
                System.err.println("Shader linking error:");
                System.err.println(status);

                shaderProgram = 0;
            }
        }

        return shaderProgram;
    }
    public static String getProgramStatus(GL2 gl, int program, int status) {
        IntBuffer intBuffer = IntBuffer.allocate(1);
        gl.glGetProgramiv(program, status, intBuffer);

        if (intBuffer.get(0) != 1) {
            gl.glGetProgramiv(program, GL2.GL_INFO_LOG_LENGTH, intBuffer);

            int size = intBuffer.get(0);
            if (size > 0) {
                ByteBuffer byteBuffer = ByteBuffer.allocate(size);
                gl.glGetProgramInfoLog(program, size, intBuffer, byteBuffer);
                return new String(byteBuffer.array(),
                        Charset.forName("ASCII"));
            } else {
                return "Unknown";
            }
        }

        return null;
    }
}

Related Tutorials