uk.co.ifs_studios.engine.shader.VertexShader.java Source code

Java tutorial

Introduction

Here is the source code for uk.co.ifs_studios.engine.shader.VertexShader.java

Source

/*
Copyright (C) 2013 IFS Studios
    
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 3 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, see <http://www.gnu.org/licenses/>.
 */

package uk.co.ifs_studios.engine.shader;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.lwjgl.opengl.GL20;

import uk.co.ifs_studios.engine.exceptions.ShaderException;
import uk.co.ifs_studios.engine.loggers.EngineLogger;

/**
 * A class to easily load and use vertex shader programs.
 * 
 * @author BleedObsidian (Jesse Prescott)
 */
public class VertexShader {
    private int id;

    private final InputStream shaderStream;

    /**
     * Create new VertexShader.
     * 
     * @param inputStream
     *            - File of shader.
     */
    public VertexShader(InputStream inputStream) {
        this.shaderStream = inputStream;
    }

    /**
     * Load and compile shader.
     */
    public void load() {
        StringBuilder shaderSource = new StringBuilder();

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(this.shaderStream));

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

            reader.close();
        } catch (FileNotFoundException e) {
            EngineLogger.error(new ShaderException("Shader file not found.", e));
        } catch (IOException e) {
            EngineLogger.error(new ShaderException("Couldn't read shader file.", e));
        }

        this.id = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
        GL20.glShaderSource(this.id, shaderSource);
        GL20.glCompileShader(this.id);
    }

    /**
     * @return - Shader ID.
     */
    public int getID() {
        return this.id;
    }
}