2
#ifdef GL_ES                                
precision lowp float;                        
#endif                                        

varying vec4 v_fragmentColor;                        
varying vec2 v_texCoord;                            
varying vec2 v_texCoordBlendMap;                    
varying vec3 v_normal;                              

varying vec4 v_FogColor;                    
varying float v_distance;                     
uniform sampler2D u_texture;                      
uniform sampler2D u_tex1;                    
uniform sampler2D u_tex2;                     
uniform sampler2D u_tex3;                     
void main()                                    
{                                                
 vec4 colour1    = texture2D(u_texture,          v_texCoord); 
vec4 colour2    = texture2D(u_tex1,      v_texCoord);  
float blendVal = texture2D(u_tex2,       v_texCoordBlendMap).r; 
float fValue = 1.0 - blendVal; 
vec4 texColor  = colour1*fValue +   colour2*blendVal; 
texColor.a = 1;                 
v_fragmentColor.a = 1;          
vec4 finalclr = texColor * v_fragmentColor; 
    if(finalclr.a == 1)         //why not it equal 1        
        {                                       
        gl_FragColor=vec4(1,0,0,1);         
     }                                      
     else                                   
     {                                      
        gl_FragColor=vec4(1,1,1,1);         
    }                                       
}                                           

hi,guys ,sorry about asking it on cellphone。I'm being puzzled by debugging blender script in this week. the above ps script it was used for moduling two RGBA.but for testing and simplification,i 'm writing two Alpha channel to be 1.0 intentionally and expecting red colour of output.but the frag colour output is white instead of red colour. how could it be?

4
  • 1
    equality tests on floating point data are always a bad idea Commented Nov 17, 2013 at 3:18
  • @derhass normally I'd agree with you, but they're essentially multiplying 1 by 1 and it would appear not getting 1. There are no circumstances under which multiplying 1 by 1 should not give 1, and this equality test is fine in this case. Commented Nov 17, 2013 at 3:49
  • That said, I'd verify that the else is getting called. I've seen many cases where setting up other OpenGL state leads to an all white output. Verify that it really is taking the else by changing the white to green or something. If it is, then my money is on v_fragmentColor not being writeable. Commented Nov 17, 2013 at 3:52
  • 1
    If you read the specification, it tells you that no precision of int is representable by lowp float. I would use floating-point constants instead of integer constants, even though the concept of integer representation actually refers to the representable range (lowp int = -2^8 - 2^8, lowp float = 2.0^-8 - 2.0). It is good to understand that lowp float limits you to +/- 2.0 anyway, a lot of people are completely unaware of this. Commented Nov 17, 2013 at 10:16

1 Answer 1

1

Try narrowing it down a bit more with:

finalclr.a = texColor.a * v_fragmentColor.a;

It could very well be that:

  • the vector multiplication operator may not be what you are expecting
  • the vector multiplication is introducing floating point inaccuracies.
  • the float multiplication is introducing floating point inaccuracies.
  • v_fragmentColor is not writeable.
  • fragment shaders get a default medium precision AFAIK, you might have to supply precision highp precision specifier

If it comes down to a precision issue you cant get around, compare the difference of floats a and b using something like:

if (abs(a - b) < EPSILON) // absolute value of difference
{
    // consider them equal...
}

where EPSILON is some small value.

Sign up to request clarification or add additional context in comments.

3 Comments

It occurs to me that it pretty much can't be a precision issue. If you think about how floating point numbers work, the number 1.0 has to be exactly representable.
@Preet Kukreti,thank your reply.could you please tell me why v_fragmentColor is not writeable.
What is more, fragment shaders are not even required to support highp. You need to make a habit of checking the pre-processor definition: GL_FRAGMENT_PRECISION_HIGH before ever declaring anything highp in a fragment shader.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.