Jump to content
  • Advertisement
Sign in to follow this  

Fragment shader if statement

This topic is 3899 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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!