Sign in to follow this  
Giawa

glsl sampler2D hardcoded value

Recommended Posts

Giawa    130
Hi everyone, I tackled multitexturing today and got it working without any major problems. I'm now looking to do some optimizations. One thing that bothers me about my method is that every time I draw I have to tell the glsl shader which textures are which.
t_shader.SetEffectUniformInt("baseMap", 0);
t_shader.SetEffectUniformInt("layer1map", 1);
t_shader.SetEffectUniformInt("layer2map", 2);
t_shader.SetEffectUniformInt("layer3map", 3);
t_shader.SetEffectUniformInt("alphaMap", 4);
Where SetEffectUniformInt is calling glUniform1i(location, value); Since these uniforms are consistently 0, 1, 2, 3, 4 is there any way to hardcode these values into the shader so I don't need to set them? Fragment Shader
uniform sampler2D baseMap;
uniform sampler2D layer1map;
uniform sampler2D layer2map;
uniform sampler2D layer3map;
uniform sampler2D alphaMap;

varying vec2 texCoord;

void main(void)
{
	vec4 alphaRGB = texture2D(alphaMap, texCoord);
	// layer colours
	vec4 baseRGB = texture2D(baseMap, texCoord);
	vec4 layer1RGB = texture2D(layer1map, texCoord);
	vec4 layer2RGB = texture2D(layer2map, texCoord);
	vec4 layer3RGB = texture2D(layer3map, texCoord);

	// lerp between current layer and previous layer based on alpha values
	vec4 layer1 = mix(baseRGB, layer1RGB, alphaRGB.r);
	vec4 layer2 = mix(layer1, layer2RGB, alphaRGB.g);
	vec4 layer3 = mix(layer2, layer3RGB, alphaRGB.b);
	gl_FragColor = vec4(layer3.r, layer3.g, layer3.b, 1);
}
Thanks for any help, Giawa

Share this post


Link to post
Share on other sites
V-man    813
No, you can't hardcode them into the GLSL code itself.
You can however set them up after you compile your shader and it will become part of the shader state. You only need to call t_shader.SetEffectUniformInt("baseMap", 0); once.

Share this post


Link to post
Share on other sites
Giawa    130
Excellent! I tried that already, but I hadn't realized that the program needed to be enabled first before trying to set any states. Perfect, that's exactly the solution I was looking for. Thanks,

Giawa


t_shader = ProgramHandler.Instance.GetProgram("terrain.vp", "terrain.fp");
t_shader.UseProgram();
t_shader.SetEffectUniformInt("baseMap", 0);
t_shader.SetEffectUniformInt("layer1map", 1);
t_shader.SetEffectUniformInt("layer2map", 2);
t_shader.SetEffectUniformInt("layer3map", 3);
t_shader.SetEffectUniformInt("alphaMap", 4);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this