Skip to main content
GameDev.net gamedev.net
🔒 Locked

glsl sampler2D hardcoded value

Started by Giawa Oct 22, 2008 at 1:39 AM 1 replies 1.5k views
Original Post
Giawa
Giawa
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
V-man
V-man
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.
Giawa
Giawa
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);

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.