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

Java tutorial

Introduction

Here is the source code for de.ikosa.mars.viewer.glviewer.engine.GLConfigurableMaterial.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.GL20;

public class GLConfigurableMaterial extends GLMaterial {
    private GLColor ambient;
    private GLColor diffuse;
    private GLColor specular;
    private float specularCoeff;
    private int illuminationModel;
    private boolean hasDiffuseTexture;
    private GLTexture diffuseTexture;
    private GLShader shader;

    public GLConfigurableMaterial(String name, GLColor ambient, GLColor diffuse, GLColor specular,
            float specularCoeff, int illuminationModel, boolean hasDiffuseTexture, GLTexture diffuseTexture,
            GLShader shader) {
        super(name);

        this.ambient = ambient;
        this.diffuse = diffuse;
        this.specular = specular;
        this.specularCoeff = specularCoeff;
        this.illuminationModel = illuminationModel;
        this.hasDiffuseTexture = hasDiffuseTexture;
        this.diffuseTexture = diffuseTexture;
        this.shader = shader;
    }

    @Override
    public GLShader preparePass(GLScene.PassType passType, GLScene.DrawModifier drawModifier) {
        if (passType == GLScene.PassType.Forward) {
            shader.use();
            uploadUniforms(drawModifier);
            return shader;
        }
        return null;
    }

    @Override
    public boolean isCompatible(boolean hasNormals, boolean hasTexCoords) {
        return (hasDiffuseTexture ? hasTexCoords : true) & hasNormals;
    }

    private void uploadUniforms(GLScene.DrawModifier drawModifier) {
        drawModifier.apply(shader);

        GL20.glUniform4f(shader.getUniformColorAmbient(), ambient.r, ambient.g, ambient.b, ambient.a);
        GL20.glUniform4f(shader.getUniformColorDiffuse(), diffuse.r, diffuse.g, diffuse.b, diffuse.a);
        GL20.glUniform4f(shader.getUniformColorSpecular(), specular.r, specular.g, specular.b, specular.a);

        GL20.glUniform1f(shader.getUniformValueSpecularCoefficient(), specularCoeff);
        // bind texture
        if (hasDiffuseTexture)
            diffuseTexture.use(shader.getTextureImageUnitDiffuse());
    }
}