ovh.tgrhavoc.gameengine.core.Shader.java Source code

Java tutorial

Introduction

Here is the source code for ovh.tgrhavoc.gameengine.core.Shader.java

Source

/*******************************************************************************
 *     Copyright (C) 2015 Jordan Dalton
 *
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License along
 *     with this program; if not, write to the Free Software Foundation, Inc.,
 *     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *******************************************************************************/
package ovh.tgrhavoc.gameengine.core;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;

import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL32;

public class Shader {

    int pointer;

    HashMap<String, Integer> uniforms;

    public Shader() {
        pointer = GL20.glCreateProgram();

        uniforms = new HashMap<String, Integer>();

        if (pointer == 0) {
            System.err.println("Could not find valid memoryu location in construstor");
            System.exit(-1);
        }
    }

    public void addVertexShaderFromFile(String text) {
        addProgram(loadShaders(text), GL20.GL_VERTEX_SHADER);
    }

    public void addGeomertyShaderFromFile(String text) {
        addProgram(loadShaders(text), GL32.GL_GEOMETRY_SHADER);
    }

    public void addFragmentShaderFromFile(String text) {
        addProgram(loadShaders(text), GL20.GL_FRAGMENT_SHADER);
    }

    private static String loadShaders(String filename) {
        StringBuilder shaderSource = new StringBuilder();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("./res/shaders/" + filename));

            String line;

            while ((line = br.readLine()) != null) {
                shaderSource.append(line).append("\n");
            }

            br.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }

        return shaderSource.toString();
    }

    public void bind() {
        GL20.glUseProgram(pointer);
    }

    public void updateUniforms(Matrix4f worldMatrix, Matrix4f projectedMatrix, Material mat) {

    }

    public void addUniform(String uniform) {
        int uniformLoc = GL20.glGetUniformLocation(pointer, uniform);

        if (uniformLoc == 0xFFFFFF) {
            System.err.println("Error: Could not find uniform " + uniform);
            new Exception().printStackTrace();
            System.exit(-1);
        }

        uniforms.put(uniform, uniformLoc);
    }

    public void setUniformi(String name, int value) {
        GL20.glUniform1i(uniforms.get(name), value);
    }

    public void setUniformf(String name, float value) {
        GL20.glUniform1f(uniforms.get(name), value);
    }

    public void setUniform(String name, Vector3f value) {
        GL20.glUniform3f(uniforms.get(name), value.getX(), value.getY(), value.getZ());
    }

    public void setUniform(String name, Matrix4f value) {
        GL20.glUniformMatrix4(uniforms.get(name), true, Util.createFlippedBufer(value));
    }

    public void addVertexShader(String text) {
        addProgram(text, GL20.GL_VERTEX_SHADER);
    }

    public void addGeomertyShader(String text) {
        addProgram(text, GL32.GL_GEOMETRY_SHADER);
    }

    public void addFragmentShader(String text) {
        addProgram(text, GL20.GL_FRAGMENT_SHADER);
    }

    public void compileShader() {
        GL20.glLinkProgram(pointer);
        if (GL20.glGetProgrami(pointer, GL20.GL_LINK_STATUS) == 0) {
            System.err.println(GL20.glGetShaderInfoLog(pointer, 1024));
            System.exit(-1);
        }

        GL20.glValidateProgram(pointer);
        if (GL20.glGetProgrami(pointer, GL20.GL_VALIDATE_STATUS) == 0) {
            System.err.println(GL20.glGetShaderInfoLog(pointer, 1024));
            System.exit(-1);
        }

    }

    private void addProgram(String text, int type) {
        int shader = GL20.glCreateShader(type);
        if (shader == 0) {
            System.err.println("Could not find valid memory location when adding shader");
            System.exit(-1);
        }

        GL20.glShaderSource(shader, text);
        GL20.glCompileShader(shader);

        if (GL20.glGetShaderi(shader, GL20.GL_COMPILE_STATUS) == 0) {
            System.err.println(GL20.glGetShaderInfoLog(shader, 1024));
            System.exit(-1);
        }

        GL20.glAttachShader(pointer, shader);
    }

}