Example usage for org.lwjgl.opengl GL20 glAttachShader

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

Introduction

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

Prototype

public static void glAttachShader(@NativeType("GLuint") int program, @NativeType("GLuint") int shader) 

Source Link

Document

Attaches a shader object to 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 w w  w  .  j  av 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 attachShaders(String vertexSource, String fragmentSource) {
    int vertexShader = createShader(GL20.GL_VERTEX_SHADER, vertexSource);
    if (vertexShader < 0) {
        return false;
    }/* ww w  .j  a v a 2s  .  c  om*/

    int fragmentShader = createShader(GL20.GL_FRAGMENT_SHADER, fragmentSource);
    if (fragmentShader < 0) {
        GL20.glDeleteShader(vertexShader);
        return false;
    }

    GL20.glAttachShader(handle, vertexShader);
    GL20.glAttachShader(handle, fragmentShader);
    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 w w  . j  a  va  2  s  . c o m
    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());
    }//from  www  .java  2  s  .c o 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 attachShaders() {
    GL20.glAttachShader(program, vert);
    GL20.glAttachShader(program, frag);
}

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

License:Open Source License

public void compile() {
    Util.checkErr();//from   w ww .j  av a2 s  . c  om

    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();//from  ww  w  .ja v a 2 s . c  o 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 addProgram(final String text, final int type) {
    final int shader = GL20.glCreateShader(type);
    if (shader == 0) {
        System.out.println("Shader is 0.");
        System.exit(0);//w  w w .  j  a v a 2s  . co m
    }
    GL20.glShaderSource(shader, text);
    GL20.glCompileShader(shader);
    if (GL20.glGetShader(shader, GL20.GL_COMPILE_STATUS) == 0) {
        System.out.println(GL20.glGetShaderInfoLog(shader, 1024));
        System.exit(1);
    }
    GL20.glAttachShader(shader, this.getProgram());
}

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

License:Open Source License

/**
 * Attaches the given shader to this program.
 * //from  w  w  w  .j  av a 2s.c o  m
 * @param shader The shader to attach to this program.
 * @return This shader program.
 */
public ShaderProgram attach(Shader shader) {
    if (state != State.NEW)
        throw new IllegalStateException("Shader program must be new to attach shaders.");
    if (shader.state() != Shader.State.COMPILED)
        throw new IllegalStateException("Cannot attach shader that is not compiled.");

    GL20.glAttachShader(id, shader.id);
    shaders.add(shader);
    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);/* ww  w.j  a v  a  2  s.  c  om*/

        // 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;
}