com.geekyaubergine.geekyjgameutil.shader.ShaderProgram.java Source code

Java tutorial

Introduction

Here is the source code for com.geekyaubergine.geekyjgameutil.shader.ShaderProgram.java

Source

/**
The MIT License (MIT)
    
Copyright (c) 2015 Chris Aubert | GeekyAubergine
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
 */
package com.geekyaubergine.geekyjgameutil.shader;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;

import com.geekyaubergine.geekyjgameutil.resource.IResource;
import com.geekyaubergine.geekyjgameutil.resource.ResourceManager;
import com.geekyaubergine.geekyjutil.file.FileUtil;
import com.geekyaubergine.geekyjutil.log.Log;
import com.geekyaubergine.geekyjutil.validate.Validate;

/**
 * A class that loads and controls shader programs
 * 
 * @author Chris Aubert
 *
 */
public abstract class ShaderProgram implements IResource {

    private File vertexShaderFile;
    private File fragmentShaderFile;

    private int programID;
    private int vertexShaderID;
    private int fragmentShaderID;

    /**
     * Creates a shader using the two given files
     * @param vertexShaderFile Vertex shader file to read
     * @param fragmentShaderFile Fragment shader file to read
     */
    public ShaderProgram(File vertexShaderFile, File fragmentShaderFile) {
        Validate.notNull(vertexShaderFile, "Vertex shader file must not be null");
        Validate.notNull(fragmentShaderFile, "Fragment shader file must not be null");

        this.vertexShaderFile = vertexShaderFile;
        this.fragmentShaderFile = fragmentShaderFile;
        registerResource();
    }

    @Override
    public void registerResource() {
        ResourceManager.registerResource(this);
    }

    @Override
    public void loadResource() throws IOException {
        vertexShaderID = loadShaderFile(vertexShaderFile, GL20.GL_VERTEX_SHADER);
        fragmentShaderID = loadShaderFile(fragmentShaderFile, GL20.GL_FRAGMENT_SHADER);
        programID = GL20.glCreateProgram();
        GL20.glAttachShader(programID, vertexShaderID);
        GL20.glAttachShader(programID, fragmentShaderID);

        bindAttributes();

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

    /**
     * Starts the current shader
     */
    public void start() {
        GL20.glUseProgram(programID);
    }

    /**
     * Stops the current shader
     */
    public void stop() {
        GL20.glUseProgram(0);
    }

    /**
     * Binds VAO attribute to variable in shader
     * @param attribute Attribute ID
     * @param variableName Variable name in shader files
     */
    public void bindAttribute(int attribute, String variableName) {
        Validate.notNull(variableName, "Variable name must not be null");
        Validate.isTrue(variableName.length() > 0, "Variable name must contain at least one character");
        GL20.glBindAttribLocation(programID, attribute, variableName);
    }

    /**
     * Hook to bind all attributes for shader program
     */
    protected abstract void bindAttributes();

    /**
     * Loads shader program from file and returns the shader ID
     * @param file File to read
     * @param type Type of shader
     * @return Shader ID
     * @throws IOException
     */
    private int loadShaderFile(File file, int type) throws IOException {
        List<String> lines = FileUtil.readLinesFromFile(file, false, true);
        StringBuilder shaderSource = new StringBuilder();
        for (String line : lines) {
            shaderSource.append(line).append("\n");
        }

        int shaderID = GL20.glCreateShader(type);
        GL20.glShaderSource(shaderID, shaderSource);
        GL20.glCompileShader(shaderID);
        if (GL20.glGetShaderi(shaderID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
            Log.error("Could not complile shader");
            Log.error(GL20.glGetShaderInfoLog(shaderID, 500));
        }
        return shaderID;
    }

    @Override
    public void unloadResource() {
        stop();
        GL20.glDetachShader(programID, vertexShaderID);
        GL20.glDetachShader(programID, fragmentShaderID);
        GL20.glDeleteShader(vertexShaderID);
        GL20.glDeleteShader(fragmentShaderID);
        GL20.glDeleteProgram(programID);
    }

    @Override
    public String toString() {
        return "Shader program. Vertex: " + vertexShaderFile.getPath() + " Fragment: "
                + fragmentShaderFile.getPath();
    }

}