[Solved] GLSL Tinting not working

With my font rendering, I use a shader [icode]vec4 tint[/icode] to set the color of the text. I transferred this code from an implementation from about a month ago, but the exact same code isn’t working. Any help would be appreciated.

[icode]Uploading the data to the shader[/icode]

// get the location. called in initialization
tintLocation = GL20.glGetUniformLocation(programId, "in_Tint");
// upload the data each draw call to the shader
GL20.glUseProgram(programId);
GL20.glUniform4f(tintLocation, r, g, b, a);
GL20.glUseProgram(0);
GL20.glUseProgram(programId);

[icode]Vertex Shader[/icode]

#version 150 core

uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
uniform vec4 in_Tint;

in vec4 in_Position;
in vec4 in_Color;
in vec2 in_TextureCoord;

out vec4 pass_Color;
out vec2 pass_TextureCoord;
out vec4 tint;

void main(void) {
	gl_Position = in_Position;
	// Override gl_Position with our new calculated position
	gl_Position = projectionMatrix * viewMatrix * modelMatrix * in_Position;
	
	pass_Color = in_Color;
	pass_TextureCoord = in_TextureCoord;
	
	tint = in_Tint;	// tint the buffer
}

[icode]Fragment Shader[/icode]

#version 150 core

uniform sampler2D Texture0;

in vec4 pass_Color;
in vec2 pass_TextureCoord;
in vec4 tint;

out vec4 out_Color;

void main(void) {
	out_Color = pass_Color;
	// Override out_Color with our texture pixel
	out_Color = texture(Texture0, pass_TextureCoord) * tint;
}

Why do you read uniform value at vertex shader and then interpolate it to fragment shader? This is totally redudant use of code and ALU.

Would you be able to clarify that?

no, it does not. he’s doing it like that cos’ that’s how glColor is passed around too.

my guess is you dont set the Texture0 uniform :wink:

to fix what pitbuller said, you should either use [icode]noperspective out tint[/icode] (VS) and [icode]noperspective in tint[/icode] (FS) or just put [icode]uniform vec4 in_Tint[/icode] into the FS directly. either way, the compiler out-smarts you anyway and does something thats even better. o/

It’s not matter of performance but creating minimal test case so bugs can be tracked more easily because there are less parts that can go wrong. So tint color uniform should be read on pixel shader any other way is just adding unneeded bloat.

@basil_ The [icode]Texture0[/icode] uniform is fine because I use the same shader for images and the images render nicely.

@pitbuller : good idea :slight_smile:

@copy : another silly guess, did you debug r g b a values ? you know, just for the case they’re all 1.0.

Break it down. If you output pure tint color does that work? Does only texture work?.

@basil_ I made sure that the ones passed with glUniform4f are correct, yes.
@pitbuller I tested tinting with just creating a vec4 on the fragment shader and it works. Texturing works because the font texture renders and other images do too

Interestingly enough when I change the tint line to

GL20.glUniform4f(tintLocation, 1, 0, 0, 1);

It works fine. So that’s weird.

Nevermind. I am a complete derp. I forgot that I call the method applying tints in my text rendering (separately) and that one uses white as the tint.

:wink: