Search in sources :

Example 1 with SamplerType

use of io.xol.chunkstories.api.rendering.shader.Shader.SamplerType in project chunkstories by Hugobros3.

the class TexturingConfigurationImplementation method setup.

/**
 * Setups the required texturing units and links the shaders uniforms to them
 */
public void setup(RenderingInterface renderingInterface) throws NotEnoughtTextureUnitsException {
    ShaderGL shaderProgram = (ShaderGL) renderingInterface.currentShader();
    // Drop the older bound textures when a new shader is used
    if (lastShaderConfigured != shaderProgram) {
        alreadyBound.clear();
        this.resetBoundTextures();
    } else {
        // Go through the already bound textures
        Iterator<Entry<Texture, Integer>> i = alreadyBound.entrySet().iterator();
        while (i.hasNext()) {
            Entry<Texture, Integer> e = i.next();
            Texture texture = e.getKey();
            // If one texture is no longer used, remove it
            if (!allTextures.values().contains(texture)) {
                i.remove();
                boundTextures[e.getValue()] = null;
            }
        }
    }
    // For each sampler defined in the shader we will try to bind the necessary texture
    for (Entry<String, SamplerType> e : shaderProgram.samplers().entrySet()) {
        String samplerName = e.getKey();
        // System.out.println("figuring out texture for"+samplerName);
        Texture texture = allTextures.get(samplerName);
        if (// No texture or the wrong type supplied ? No worries
        texture == null || texture.getSamplerType() != e.getValue())
            texture = defaultTextures[e.getValue().ordinal()];
        // System.out.println(texture);
        int alreadyBoundTextureUnit = alreadyBound.getOrDefault(texture, -1);
        if (alreadyBoundTextureUnit == -1) {
            int freeTextureUnit = findFreeTextureUnit();
            this.selectTextureUnit(freeTextureUnit);
            texture.bind();
            alreadyBound.put(texture, freeTextureUnit);
            boundTextures[freeTextureUnit] = texture;
            int uniform = shaderProgram.getUniformLocation(samplerName);
            if (uniform == -1)
                continue;
            glUniform1i(uniform, freeTextureUnit);
        // if(shaderProgram.getShaderName().contains("postprocess"))
        // System.out.println("bound " + samplerName + " to " + freeTextureUnit + " ("+uniform+") :" + texture);
        } else {
            glUniform1i(shaderProgram.getUniformLocation(samplerName), alreadyBoundTextureUnit);
        }
    }
    lastShaderConfigured = shaderProgram;
}
Also used : Entry(java.util.Map.Entry) ShaderGL(io.xol.chunkstories.renderer.opengl.shader.ShaderGL) SamplerType(io.xol.chunkstories.api.rendering.shader.Shader.SamplerType) GL11.glBindTexture(org.lwjgl.opengl.GL11.glBindTexture) Texture(io.xol.chunkstories.api.rendering.textures.Texture) ArrayTexture(io.xol.chunkstories.api.rendering.textures.ArrayTexture) GL13.glActiveTexture(org.lwjgl.opengl.GL13.glActiveTexture)

Aggregations

SamplerType (io.xol.chunkstories.api.rendering.shader.Shader.SamplerType)1 ArrayTexture (io.xol.chunkstories.api.rendering.textures.ArrayTexture)1 Texture (io.xol.chunkstories.api.rendering.textures.Texture)1 ShaderGL (io.xol.chunkstories.renderer.opengl.shader.ShaderGL)1 Entry (java.util.Map.Entry)1 GL11.glBindTexture (org.lwjgl.opengl.GL11.glBindTexture)1 GL13.glActiveTexture (org.lwjgl.opengl.GL13.glActiveTexture)1