All Downloads are FREE. Search and download functionalities are using the official Maven repository.

darwin.renderer.shader.uniform.ShaderMaterialFactory Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2012 daniel
 *
 * 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 .
 */
package darwin.renderer.shader.uniform;

import java.util.ArrayList;
import java.util.Collection;

import darwin.geometrie.unpacked.Material;
import darwin.renderer.shader.*;

import com.google.common.base.Optional;
import com.jogamp.opengl.util.texture.Texture;
import javax.inject.Inject;
import javax.inject.Singleton;

/**
 *
 * @author daniel
 */
@Singleton
public class ShaderMaterialFactory {

    public ShaderMaterial create(Shader shader, Collection setter) {
        UniformSetter[] array = new UniformSetter[setter.size()];
        setter.toArray(array);
        return new ShaderMaterial(shader, array);
    }

    public ShaderMaterial create(Shader shader, Material material) {
        return create(shader, material, new ArrayList());
    }

    public ShaderMaterial create(Shader shader, Material material,
                                 Collection setter) {
        ArrayList s = new ArrayList<>(setter);
        add2List(s, createSetter(shader, "mat_diffuse", material.getDiffuse()));
        add2List(s, createSetter(shader, "mat_ambient", material.getAmbient()));
        add2List(s, createSetter(shader, "mat_specular", material.getSpecular()));
        add2List(s, createSetter(shader, "mat_spec_exp", material.specular_exponet));

        add2List(s, createSetter(shader, "diffuse_sampler", material.diffuseTex));
        add2List(s, createSetter(shader, "specular_sampler", material.specularTex));

        add2List(s, createSetter(shader, "alpha_sampler", material.alphaTex));

        //TODO normalmapping
//        add2List(s, createSetter(shader, "normal_sampler", material.normalTex));

        return create(shader, s);
    }

    public ShaderMaterial create(Shader s, Texture[] texs, String... unames) {
        UniformSetter[] setter = new UniformSetter[texs.length];
        for (int i = 0; i < texs.length; i++) {
            Optional sampler = s.getSampler(unames[i]);
            if (sampler.isPresent()) {
                setter[i] = new SamplerSetter(sampler.get(), texs[i]);
            }
        }
        return new ShaderMaterial(s, setter);
    }

    private void add2List(Collection list, Optional s) {
        if (s != null) {
            list.add(s.get());
        }
    }

    private Optional createSetter(Shader shader, String name, float... values) {
        Optional u = shader.getUniform(name);
        if (u.isPresent() && values != null && values.length != 0) {
            return Optional.of(new FloatSetter(u.get(), values));
        } else {
            return Optional.absent();
        }
    }

    private Optional createSetter(Shader shader, String name, String path) {
        Optional s = shader.getSampler(name);
//        if (s.isPresent()) {
//            TextureLoadJob tlj = factory.create(path, GL.GL_LINEAR, GL.GL_REPEAT);
//            TextureContainer tc = loader.getTexture(tlj);
//            return Optional.of(new SamplerSetter(s.get(), tc));
//        } else {
            return Optional.absent();
//        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy