Search in sources :

Example 1 with ShaderGL

use of io.xol.chunkstories.renderer.opengl.shader.ShaderGL in project chunkstories by Hugobros3.

the class ShadersStore method loadShader.

boolean loadShader(String name) {
    ShaderGL subject = new ShaderGL(clientContent.modsManager(), name);
    loadedShaders.put(name, subject);
    return subject.isLoadedCorrectly();
}
Also used : ShaderGL(io.xol.chunkstories.renderer.opengl.shader.ShaderGL)

Example 2 with ShaderGL

use of io.xol.chunkstories.renderer.opengl.shader.ShaderGL 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)

Example 3 with ShaderGL

use of io.xol.chunkstories.renderer.opengl.shader.ShaderGL in project chunkstories by Hugobros3.

the class AttributesConfigurationImplementation method setup.

public void setup(RenderingInterface renderingInterface) {
    ShaderGL shaderProgram = (ShaderGL) renderingInterface.currentShader();
    Set<Integer> unusedAttributes = enabledVertexAttributes;
    enabledVertexAttributes = new HashSet<Integer>(enabledVertexAttributes);
    for (Entry<String, AttributeSource> e : attributes.entrySet()) {
        String attributeName = e.getKey();
        AttributeSource attributeSource = e.getValue();
        int attributeLocation = shaderProgram.getVertexAttributeLocation(attributeName);
        if (attributeLocation == -1)
            continue;
        unusedAttributes.remove(attributeLocation);
        // Enable only when it wasn't
        if (enabledVertexAttributes.add(attributeLocation))
            glEnableVertexAttribArray(attributeLocation);
        attributeSource.setup(attributeLocation);
    }
    // Disable and forget about unused ones
    for (int unused : unusedAttributes) {
        glDisableVertexAttribArray(unused);
        enabledVertexAttributes.remove(unused);
    }
}
Also used : AttributeSource(io.xol.chunkstories.api.rendering.vertex.AttributeSource) ShaderGL(io.xol.chunkstories.renderer.opengl.shader.ShaderGL)

Aggregations

ShaderGL (io.xol.chunkstories.renderer.opengl.shader.ShaderGL)3 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 AttributeSource (io.xol.chunkstories.api.rendering.vertex.AttributeSource)1 Entry (java.util.Map.Entry)1 GL11.glBindTexture (org.lwjgl.opengl.GL11.glBindTexture)1 GL13.glActiveTexture (org.lwjgl.opengl.GL13.glActiveTexture)1