For some reason this one shader will not compile properly

This is what I have:


#version 330 core

struct Material 
{
    vec3 ambient;
    sampler2D diffuseMap;
	sampler2D digits;
    vec3 specular;
    float shininess;
	int numVal;
};

struct DirectionalLight
{
	vec3 direction;
	vec3 ambient;
	vec3 diffuse;
	vec3 specular;
};
  
in vec2 TexCoord;
in vec3 FragPos;
in vec3 Normal;

uniform DirectionalLight dirLight;
uniform Material material;
uniform vec3 viewPos;

out vec4 frag_color;

void main()
{ 
    // Ambient -------------------------------------------------------------------------
	
	if (true){	//I set this when I want to apply the other texture
		frag_color = vec4(vec3(texture(material.diffuseMap, TexCoord)), 1.0f);
	}else{
		int xseg = int(mod(material.numVal, 6));
		int yseg = int(floor(float(material.numVal) / 6));
		vec2 addloc = vec2(xseg / 6, yseg / 6);
		vec2 newloc;
		newloc = vec2(0, 0);
		if (TexCoord.x < .75){
			newloc = vec2((TexCoord.x - .5) / 1.5, (TexCoord.y - .5) / 3);
		}else{
			newloc = vec2((TexCoord.x - .75) / 1.5, (TexCoord.y - .5) / 3);
		}
		frag_color = vec4(vec3(1, 1, 1) - vec3(texture(material.digits, addloc + newloc)), 1.0f);
	}
}

with a 0 int numVal it looks like it should without the other texture applied. When I set it to 1 I just get the other texture all over the place. Any other value that I have tried will make this all black.

This shader compiles just fine, what do you mean “will not compile properly”?

Re-read this quote, and then look back at your title:

I gather that your title is very misleading and should be ignored.

Sounds like it compiles fine (which it does for me here), but “when” compiled and linked into a program, it’s not doing what you want. Right?

Please describe in more detail what your two test cases are. For instance, the code you’re talking about is currently not even compiled into the resulting shader. Show the C++ code you’re using to populate the material uniform as well.

The shader compiles but the programming logic isn’t applied right. The first ‘if’ condition seems to be dependent on numVal but in my program it is set to true which doesn’t make sense.

Ok. Yeah, that’s definitely an odd one.

Which GPU, GL driver, and GL driver version is this on?

And again, let’s see that C++ code, particularly the part populating all the uniforms for this shader. It’s sounding like you might be doing that incorrectly.

Your test results suggest that your uniform population of material.numVal (which shouldn’t exist BTW – see below) may be stomping on material.diffuseMap – particularly if the diffuseMap texture is bound to texunit 0 and digits texture is bound to texunit 1.

Are you querying the location for the material.numVal uniform? If so, are you properly handling the case where this uniform is completely compiled out of the resulting shader program (i.e. returns -1), which it should be in this case? For instance, see glGetUniformLocation(). You should not try setting the uniform value in this case, because it doesn’t exist.

Along those lines, here’s a test you can try. Try replacing those texture sampling lines:

    if (true){	//I set this when I want to apply the other texture
        frag_color = vec4(vec3(texture(material.diffuseMap, TexCoord)), 1.0f);
    }else{
            ...
            frag_color = vec4(vec3(1, 1, 1) - vec3(texture(material.digits, addloc + newloc)), 1.0f);

with color assignments that don’t lookup into textures:

    if (true){	//I set this when I want to apply the other texture
        frag_color = vec4( 1,0,0,1 );
    }else{
            ...
            frag_color = vec4( 0,1,0,1 );

Do you always see RED?

If so, that’s further evidence that your uniform population of material.numVal (which probably shouldn’t even be done) may be stomping on material.diffuseMap.