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;
}
Aggregations