de.ikosa.mars.viewer.glviewer.engine.GLShader.java Source code

Java tutorial

Introduction

Here is the source code for de.ikosa.mars.viewer.glviewer.engine.GLShader.java

Source

/*
 * Copyright (C) 2013 Clemens-Alexander Brust IT-Dienstleistungen
 *
 *     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 de.ikosa.mars.viewer.glviewer.engine;

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

public class GLShader {
    private int programId, vertexShaderId, fragmentShaderId;

    private int ul_col_Diffuse, ul_col_Specular, ul_col_Ambient;
    private int ul_mat_Projection, ul_mat_View, ul_mat_Model;
    private int ul_mat_Normal;

    private int ul_PixelSizeH, ul_PixelSizeV;

    private int ul_val_SpecularCoefficient;

    private int ul_val_drawModifier;

    private int tiu_Diffuse;
    private int tiu_Color0;
    private int tiu_Depth;

    public GLShader(int programId, int vertexShaderId, int fragmentShaderId) {
        this.programId = programId;
        this.vertexShaderId = vertexShaderId;
        this.fragmentShaderId = fragmentShaderId;

        use();

        // save uniform positions
        ul_col_Ambient = GL20.glGetUniformLocation(programId, "col_Ambient");
        ul_col_Diffuse = GL20.glGetUniformLocation(programId, "col_Diffuse");
        ul_col_Specular = GL20.glGetUniformLocation(programId, "col_Specular");

        ul_val_SpecularCoefficient = GL20.glGetUniformLocation(programId, "val_SpecularCoefficient");

        ul_mat_Projection = GL20.glGetUniformLocation(programId, "mat_Projection");
        ul_mat_View = GL20.glGetUniformLocation(programId, "mat_View");
        ul_mat_Model = GL20.glGetUniformLocation(programId, "mat_Model");

        ul_mat_Normal = GL20.glGetUniformLocation(programId, "mat_Normal");

        ul_PixelSizeH = GL20.glGetUniformLocation(programId, "pixelSizeH");
        ul_PixelSizeV = GL20.glGetUniformLocation(programId, "pixelSizeV");

        ul_val_drawModifier = GL20.glGetUniformLocation(programId, "val_drawModifier");

        int texSlot = 0;

        int ul_tex_Diffuse = GL20.glGetUniformLocation(programId, "tex_Diffuse");
        if (ul_tex_Diffuse >= 0) {
            tiu_Diffuse = texSlot++;
            GL20.glUniform1i(ul_tex_Diffuse, tiu_Diffuse);
        } else {
            tiu_Diffuse = -1;
        }

        int ul_tex_Color0 = GL20.glGetUniformLocation(programId, "tex_Color0");
        if (ul_tex_Color0 >= 0) {
            tiu_Color0 = texSlot++;
            GL20.glUniform1i(ul_tex_Color0, tiu_Color0);
        } else {
            tiu_Color0 = -1;
        }

        int ul_tex_Depth = GL20.glGetUniformLocation(programId, "tex_Depth");
        if (ul_tex_Depth >= 0) {
            tiu_Depth = texSlot++;
            GL20.glUniform1i(ul_tex_Depth, tiu_Depth);
        } else {
            tiu_Depth = -1;
        }

        int error = GL11.glGetError();
    }

    public int getProgramId() {
        return programId;
    }

    public int getVertexShaderId() {
        return vertexShaderId;
    }

    public int getFragmentShaderId() {
        return fragmentShaderId;
    }

    public void use() {
        GL20.glUseProgram(programId);
    }

    public int getUniformColorDiffuse() {
        return ul_col_Diffuse;
    }

    public int getUniformColorSpecular() {
        return ul_col_Specular;
    }

    public int getUniformColorAmbient() {
        return ul_col_Ambient;
    }

    public int getUniformProjectionMatrix() {
        return ul_mat_Projection;
    }

    public int getUniformViewMatrix() {
        return ul_mat_View;
    }

    public int getUniformModelMatrix() {
        return ul_mat_Model;
    }

    public int getUniformNormalMatrix() {
        return ul_mat_Normal;
    }

    public int getTextureImageUnitDiffuse() {
        return tiu_Diffuse;
    }

    public int getTextureImageUnitColor0() {
        return tiu_Color0;
    }

    public int getTextureImageUnitDepth() {
        return tiu_Depth;
    }

    public int getUniformValueSpecularCoefficient() {
        return ul_val_SpecularCoefficient;
    }

    public int getUniformValuePixelSizeH() {
        return ul_PixelSizeH;
    }

    public int getUniformValuePixelSizeV() {
        return ul_PixelSizeV;
    }

    public int getUniformValueDrawModifier() {
        return ul_val_drawModifier;
    }
}