Sign in to follow this  

Fragment shader if statement

This topic is 3590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a simple if statement in my fragment shader to determine if something should be done or not:
uniform float maxVal;
uniform float multiplier;
uniform bool useColorMap;
uniform sampler2D mainTexture;
uniform sampler1D lutTexture;
uniform sampler1D colorMapTexture;

void main()
{
	vec3 finalCol;
	float tmpColor;
	
	float color = vec3(texture2D(mainTexture, gl_TexCoord[0].st)).r * multiplier;

	if (useColorMap == true) {
		/* apply the window level LUT first ... */
		tmpColor = texture1D(lutTexture, color).r;
		/* ... then apply the LUT to each color channel */
		finalCol = vec3(texture1D(colorMapTexture, tmpColor));
		//finalCol = vec3(1.0,0.0,0.0);
	}
	else {
		/* don't apply color map, so just use the color from above */
		finalCol = vec3(texture1D(lutTexture, color));
	}

	gl_FragColor = vec4(finalCol, 1.0);
}
It works on an ATI card, but not an NVidia card! What might be going on here; bug in the driver, bad syntax for nvidia, etc? Shader Designed and 3D lab syntax checker both say the code is ok.

Share this post


Link to post
Share on other sites
This syntax actually implies a float register and it will use floating point compares:

if (useColorMap == true)
if (useColorMap == false)

if (useColorMap == 1.0f)
if (useColorMap == 0.0f)

If you wanted to use a boolean register, use this syntax instead:

if (useColorMap)
if (!useColorMap)

Share this post


Link to post
Share on other sites
Ah, thanks for the pointers! It turns out it was this statement in the main program:

glUniform1f(glGetUniformLocation(pWindow,"useColorMap"), mainVars->ColorMap_useColor());

Should be glUniform1i... not f.

Your comments on floating point comparisons made me think to go back and look at the program code and see exactly what value/type was passed to the shader.

I should also clarify, the statement "if (colorMap == true) wasn't working" meant it was never evaluating to true. But you guys understood that :)

Thanks for the help!

Share this post


Link to post
Share on other sites

This topic is 3590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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