Uniform locations return -1

Sorry for all the GLSL topics, I’m having bug after bug…

My fragment shader has two uniform samplers:


...
uniform sampler2D t_diffuse;
uniform sampler2D t_specular;
...
 
void main() {
	vec4 diffuse = texture2D(t_diffuse, gl_TexCoord[0].st).rgba;
	vec4 specular = texture2D(t_specular, gl_TexCoord[0].st).rgba;
	...	
   gl_FragColor = specular;
}

I access them with: (Per frame)


   glActiveTexture(GL_TEXTURE0);
	testTexture.bind();
	texLoc = glGetUniformLocation(world.getLightingProgram().programID, "t_diffuse");
	glUniform1f(texLoc,  testTexture.id);
	if(texLoc < 0) System.out.println("Error accessing diffuse: " + texLoc);
			
	glActiveTexture(GL_TEXTURE1);
	testSpecular.bind(); //glBindTexture(GL_TEXTURE_2D, id); - Works...
	texLoc = glGetUniformLocation(world.getLightingProgram().programID, "t_specular");
	glUniform1f(texLoc, testSpecular.id);
   if(texLoc < 0) System.out.println("Error accessing specular: " + texLoc);

And here’s the output:

 Error accessing diffuse: -1

The light shader program ID returns 1. So that works.
The diffuse texture returns 1. (testTexture.id;)
The specular texture return 2. (testSpecular.id;)
But the specular IS working…

As you’ve just ignored the diffuse value… the GLSL compiler has helpfully optimised the whole thing away. Slightly annoying when you’re debugging shaders but that’s what it does.

Cas :slight_smile:

Shouldn’t you use t_diffuse as that is the variable name in the shader, when you access it? I’ve only a few hours experience with GLSL, so correct me if I’m wrong.

Yeah, I just renamed all of them for the sake of the thread, same error.

Also, it seems to be binding the testTexture to the specular sampler.

Okay, I see what you mean, I’m using the specular map but the diffuse map isn’t being used. Thanks for the info!

also, you have to bind the texture unit not the texture id.
GL_Texture0 == 0
GL_Texture1 == 1

Thanks, its all working now! Except for the specular mapping. Can anyone please help me out with that?

Heres my shader:

#version 120
varying vec3 normal; 
varying vec3 position;

uniform vec2 lightLocation;
uniform vec3 lightColor;
uniform vec3 ambientLight;

uniform sampler2D t_diffuse;
uniform sampler2D t_specular;

uniform bool material;
 
void main() {
		vec4 diffuse = texture2D(t_diffuse, gl_TexCoord[0].st);
		vec4 specular = texture2D(t_specular, gl_TexCoord[0].st);
		
        float distance = length(lightLocation - gl_FragCoord.xy);
        float attenuation = 1.0 / distance;
        vec4 light = vec4(attenuation, attenuation, attenuation, pow(attenuation, 3)) * vec4(lightColor, 1);
        
        vec4 color;
        
        if(material)
        light += specular;
 		color = light * diffuse;
 		if(!material)
 		color = light;
 		
        gl_FragColor = color;
}

You are using a strange combination of the modern and deprecated pipelines.