net.betabears.the2dlibrary.graphics.shader.ShaderProgram.java Source code

Java tutorial

Introduction

Here is the source code for net.betabears.the2dlibrary.graphics.shader.ShaderProgram.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package net.betabears.the2dlibrary.graphics.shader;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.betabears.the2dlibrary.structure.PreLoadable;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;

/**
 * Representation of an OpenGL shader program.
 *
 * @author Bobthepeanut
 */
public class ShaderProgram implements PreLoadable {

    private boolean isLoaded, isInited;
    private Shader s0, s1;
    private int id;
    private final static Logger LOGGER = Logger.getLogger(ShaderProgram.class.getName());

    private ShaderProgram() {
    }

    /**
     * Constructor for creating a ShaderProgram.
     *
     * @param shader0 The file of the first Shader.
     * @param type0 The type of the first Shader, either
     * <code>GL20.GL_FRAGMENT_SHADER</code> or
     * <code>GL20.GL_VERTEX_SHADER</code>.
     * @param shader1 The file of the second Shader.
     * @param type1 The type of the second Shader (see above for int - range).
     */
    public ShaderProgram(File shader0, int type0, File shader1, int type1) {
        s0 = new Shader(type0, shader0);
        s1 = new Shader(type1, shader1);
    }

    /**
     * Constructor for creating a ShaderProgram.
     *
     * @param filepath0 The filepath of the first Shader.
     * @param type0 The type of the first Shader, either
     * <code>GL20.GL_FRAGMENT_SHADER</code> or
     * <code>GL20.GL_VERTEX_SHADER</code>.
     * @param filepath1 The filepath of the second Shader.
     * @param type1 The type of the second Shader (see above for int - range).
     */
    public ShaderProgram(String filepath0, int type0, String filepath1, int type1) {
        s0 = new Shader(type0, filepath0);
        s1 = new Shader(type1, filepath1);
    }

    public void use() {

    }

    public static void useZero() {

    }

    @Override
    public void load() {
        if (!(isLoaded && isInited)) {
            s0.loadShader();
            s1.loadShader();
            isLoaded = true;
        } else {
            LOGGER.log(Level.WARNING, "load() or even init() has already been called.");
        }
    }

    @Override
    public void init() {
        if (isLoaded && !isInited) {
            s0.createShader();
            s1.createShader();

            id = GL20.glCreateProgram();
            GL20.glAttachShader(id, s0.getID());
            GL20.glAttachShader(id, s1.getID());
            GL20.glLinkProgram(id);
            GL20.glValidateProgram(id);

            int i;
            while ((i = GL11.glGetError()) != 0) {
                LOGGER.log(Level.WARNING, "Error happened during creation of ShaderProgram. GL error code: {0}", i);
            }
            isInited = true;
        } else {
            LOGGER.log(Level.WARNING, "load() wasn't called before init() or init() has been called already.");
        }
    }

    @Override
    public boolean isLoaded() {
        return isLoaded;
    }

    @Override
    public boolean isInitialised() {
        return isInited;
    }
}