Example usage for org.lwjgl.opengl GL20 glLinkProgram

List of usage examples for org.lwjgl.opengl GL20 glLinkProgram

Introduction

In this page you can find the example usage for org.lwjgl.opengl GL20 glLinkProgram.

Prototype

public static void glLinkProgram(@NativeType("GLuint") int program) 

Source Link

Document

Links a program object.

Usage

From source file:com.github.ryancwilliams.WJ3dPL.graphics.GLUtils.ShaderProgram.java

License:Apache License

/**
 * Creates a new shader program with the provided vertex and fragment 
 * shader source code./*from  ww w . j a v  a2 s .  co m*/
 * The provided attributes are linked to this program.
 * @param vertexShaderSource the source code of the vertex shader.
 * @param fragmentShaderSource the source code of the fragment shader.
 * @param attributes The Vertex Attributes to bind to this shader program.
 * @throws LWJGLException If their is a issue compiling the shaders or 
 * creating or binding the program.  
 */
public ShaderProgram(String vertexShaderSource, String fragmentShaderSource, List<VertexAttribute> attributes)
        throws LWJGLException {
    //Check if any of the sourcecode paramaters are null
    if (fragmentShaderSource == null || fragmentShaderSource == null) {
        //If any of the sourcecode paramaters were null
        //throw a exception
        throw new IllegalArgumentException("Shader source may not be null");
    }
    //Check if shaders are not supported
    if (!ShaderProgram.isSupported()) {
        //If shaders are not supported
        //throw a exception
        throw new LWJGLException("Shaders are not supported on this device");
    }

    //Compile the shaders
    int vertexShader = ShaderProgram.compileShader(GL20.GL_VERTEX_SHADER, vertexShaderSource);
    int fragmentShader = ShaderProgram.compileShader(GL20.GL_FRAGMENT_SHADER, fragmentShaderSource);

    //Create the program
    this.program = GL20.glCreateProgram();

    //Bind the attrib locations
    //Check if attributes were provided
    if (attributes != null) {
        //For each attribute
        for (VertexAttribute attribute : attributes) {
            //Check if the attribute is not null
            if (attribute != null) {
                //bind the attribute
                GL20.glBindAttribLocation(this.program, attribute.index, attribute.name);
            }
        }
    }

    //Attach the shaders
    GL20.glAttachShader(this.program, vertexShader);
    GL20.glAttachShader(this.program, fragmentShader);

    //Link the program
    GL20.glLinkProgram(this.program);

    //Get if the program link was good
    boolean programLink = GL20.glGetProgrami(this.program, GL20.GL_LINK_STATUS) == GL11.GL_TRUE;

    //Get the log
    String infoLog = GL20.glGetProgramInfoLog(this.program,
            GL20.glGetProgrami(this.program, GL20.GL_INFO_LOG_LENGTH));

    //Log the log if a log is present
    if (infoLog != null && infoLog.trim().length() != 0) {
        Logger.getLogger(ShaderProgram.class.getName()).log(Level.FINEST, infoLog);
    }

    //Check if program link is bad
    if (programLink == false) {
        throw new LWJGLException("Failure in linking program. Error log:\n" + infoLog);
    }

    //detach and delete the shaders which are no longer needed
    GL20.glDetachShader(this.program, vertexShader);
    GL20.glDetachShader(this.program, fragmentShader);
    GL20.glDeleteShader(vertexShader);
    GL20.glDeleteShader(fragmentShader);
}

From source file:com.google.gapid.glviewer.gl.Shader.java

License:Apache License

private boolean link() {
    GL20.glLinkProgram(handle);
    if (GL20.glGetProgrami(handle, GL20.GL_LINK_STATUS) != GL11.GL_TRUE) {
        LOG.log(WARNING, "Failed to link program:\n" + GL20.glGetProgramInfoLog(handle));
        return false;
    }/*from   w ww  . j  a  v a2 s .co  m*/
    return true;
}

From source file:com.grillecube.client.opengl.GLProgram.java

public void link() {
    this.progID = GL20.glCreateProgram();
    for (GLShader shader : this.shaders) {
        GL20.glAttachShader(this.progID, shader.getID());
    }/*w  ww. jav  a2  s .c om*/
    this.bindAttributes();
    GL20.glLinkProgram(this.progID);
    String message = GL20.glGetProgramInfoLog(this.progID);
    if (message.length() > 0) {
        Logger.get().log(Logger.Level.WARNING, "Linking shader message: " + message);
    }

    GL20.glValidateProgram(this.progID);

    this.linkUniforms();

    GLH.glhAddObject(this);
}

From source file:com.grillecube.engine.opengl.object.GLProgram.java

public void link() {
    this._programID = GL20.glCreateProgram();
    for (GLShader shader : this._shaders) {
        GL20.glAttachShader(this._programID, shader.getID());
    }//  w ww.  j  ava 2  s  . co  m
    this.bindAttributes();
    GL20.glLinkProgram(this._programID);
    String message = GL20.glGetProgramInfoLog(this._programID);
    if (message.length() > 0) {
        Logger.get().log(Logger.Level.WARNING, "Linking shader message: " + message);
    }

    GL20.glValidateProgram(this._programID);

    this.linkUniforms();

    GLH.glhAddObject(this);
}

From source file:com.kauridev.lunarfever.graphics.ShaderProgram.java

License:Open Source License

private void linkProgram(List<VertexAttribute> attribLocations) {
    if (!isValid()) {
        throw new RuntimeException();
    }//from  w  w w.  j ava2s.  c  o m

    if (attribLocations != null) {
        for (VertexAttribute a : attribLocations) {
            if (a != null) {
                GL20.glBindAttribLocation(program, a.location, a.name);
            }
        }
    }

    attachShaders();
    GL20.glLinkProgram(program);
    int comp = GL20.glGetProgrami(program, GL20.GL_LINK_STATUS);
    int len = GL20.glGetProgrami(program, GL20.GL_INFO_LOG_LENGTH);

    if (comp == GL11.GL_FALSE) {
        throw new RuntimeException(GL20.glGetProgramInfoLog(program, len));
    }
}

From source file:com.opengrave.og.resources.ShaderProgram.java

License:Open Source License

public void compile() {
    Util.checkErr();//from   www . j  a  v  a 2s .com

    PID = GL20.glCreateProgram();
    Util.checkErr();

    vert = loadShader(sfv.getSource(), GL20.GL_VERTEX_SHADER);
    Util.checkErr();

    frag = loadShader(sff.getSource(), GL20.GL_FRAGMENT_SHADER);
    Util.checkErr();

    GL20.glAttachShader(PID, vert);
    Util.checkErr();

    GL20.glAttachShader(PID, frag);
    Util.checkErr();

    GL20.glLinkProgram(PID);
    Util.checkErr();

    System.out.println(GL20.glGetProgramInfoLog(PID, 2000));
    Util.checkErr();

    GL20.glValidateProgram(PID);
    Util.checkErr();

    // System.out.println("Compiled " + label + " as number " + PID);
    if (GL20.glGetProgrami(PID, GL20.GL_LINK_STATUS) == GL11.GL_FALSE) {
        new DebugExceptionHandler(new Exception(), label, GL20.glGetShaderInfoLog(PID, 2000),
                GL20.glGetProgramInfoLog(PID, 2000));
        // System.out.println("Failed to link " + label);
        // System.out.println(sfv.getSource());
        // System.out.println(sff.getSource());

        // Util.checkErr();

        // printShaderLogs();
    }
    Util.checkErr();

    GL20.glDetachShader(PID, vert);
    Util.checkErr();

    GL20.glDetachShader(PID, frag);
    GL20.glDeleteShader(vert);
    Util.checkErr();

    GL20.glDeleteShader(frag);
    Util.checkErr();
}

From source file:com.redthirddivision.quad.rendering.shaders.Shader.java

License:Apache License

public Shader(String file) {
    this.vertexID = loadVertex(file);
    this.fragmentID = loadFragment(file);
    this.programID = GL20.glCreateProgram();
    GL20.glAttachShader(programID, vertexID);
    GL20.glAttachShader(programID, fragmentID);

    bindAttributes();//w  w  w. j a  v a  2s  .co  m

    GL20.glLinkProgram(programID);
    GL20.glValidateProgram(programID);

    addUniforms();
}

From source file:com.runescape.client.revised.client.lwjgl.Shader.java

License:Open Source License

@SuppressWarnings("deprecation")
public void compileShader() {
    GL20.glLinkProgram(this.getProgram());
    if (GL20.glGetProgram(this.getProgram(), GL20.GL_LINK_STATUS) == 0) {
        System.out.println(GL20.glGetProgramInfoLog(this.getProgram(), 1024));
        System.exit(0);/*from www  . j  a va  2  s  .c om*/
    }
    GL20.glValidateProgram(this.getProgram());
    if (GL20.glGetProgram(this.getProgram(), GL20.GL_VALIDATE_STATUS) == 0) {
        System.out.println(GL20.glGetProgramInfoLog(this.getProgram(), 1024));
        System.exit(0);
    }
}

From source file:com.samrj.devil.gl.ShaderProgram.java

License:Open Source License

/**
 * Links this shader program, creating executables that may run on the GPU
 * and compiling a list of input attributes.
 * /*w  ww .  j a va2  s.  c om*/
 * @return This shader program.
 */
public ShaderProgram link() {
    if (state != State.NEW)
        throw new IllegalStateException("Shader program must be new to link.");

    GL20.glLinkProgram(id);
    checkStatus(GL20.GL_LINK_STATUS);

    int numAttributes = GL20.glGetProgrami(id, GL20.GL_ACTIVE_ATTRIBUTES);
    ArrayList<Attribute> attList = new ArrayList<>(numAttributes);
    attMap = new HashMap<>(numAttributes);

    int attBytes = 4 + 4 + 4 + 32;
    long address = MemStack.push(attBytes);
    ByteBuffer buffer = MemoryUtil.memByteBuffer(address, attBytes);
    for (int index = 0; index < numAttributes; index++) {
        long nameAddress = address + 12;
        GL20.nglGetActiveAttrib(id, index, 31, address, address + 4, address + 8, nameAddress);

        buffer.rewind();
        buffer.getInt();
        int size = buffer.getInt();
        int type = buffer.getInt();
        String name = MemoryUtil.memASCII(nameAddress);
        int location = GL20.nglGetAttribLocation(id, nameAddress);

        Attribute att = new Attribute(name, type, size, location);
        attList.add(att);
        attMap.put(name, att);
    }
    MemStack.pop();
    attributes = Collections.unmodifiableList(attList);

    state = State.LINKED;
    return this;
}

From source file:com.timvisee.voxeltex.module.shader.raw.RawShader.java

License:Open Source License

@Override
public int compile() {
    // Show a status message
    System.out.print("Compiling shader... ");

    // Create a new OpenGL shader program
    int program = GL20.glCreateProgram();

    // Shader IDs
    int vertexId = 0;
    int fragmentId = 0;

    // Compile the vertex shader if available
    if (hasVertexShader()) {
        // Create the vertex shader
        vertexId = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);

        // Attach the vertex shader source and compile it
        GL20.glShaderSource(vertexId, vertexSource);
        GL20.glCompileShader(vertexId);/* w  ww  .ja  v a2 s . com*/

        // Check for compiling errors
        if (GL20.glGetShaderi(vertexId, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
            // Show an error message
            System.out.println("FAIL\nFailed to compile the vertex shader");
            System.out.println(GL20.glGetShaderInfoLog(vertexId));

            // Delete the shader before returning
            GL20.glDeleteShader(vertexId);
            throw new RuntimeException("Failed to compile the vertex shader");
        }
    }

    // Compile the fragment shader if available
    if (hasFragmentShader()) {
        // Create the fragment shader
        fragmentId = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);

        // Attach the fragment shader source and compile it
        GL20.glShaderSource(fragmentId, fragmentSource);
        GL20.glCompileShader(fragmentId);

        // Check for compiling errors
        if (GL20.glGetShaderi(fragmentId, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
            // Show an error message
            System.out.println("FAIL\nFailed to compile the fragment shader");
            System.out.println(GL20.glGetShaderInfoLog(fragmentId));

            // Delete the shader before returning
            GL20.glDeleteShader(fragmentId);
            throw new RuntimeException("Failed to compile the vertex shader");
        }
    }

    // Attach all compiled shaders
    if (hasVertexShader())
        GL20.glAttachShader(program, vertexId);
    if (hasFragmentShader())
        GL20.glAttachShader(program, fragmentId);

    // Link the shader program to OpenGL and link it
    GL20.glLinkProgram(program);
    GL20.glValidateProgram(program);

    // Shaders have been attached to the program, delete their compiled sources to save memory
    if (hasVertexShader())
        GL20.glDeleteShader(vertexId);
    if (hasFragmentShader())
        GL20.glDeleteShader(fragmentId);

    // Show a status message
    System.out.println("OK");

    // Return the created OpenGL shader program
    return program;
}