GLSL and discard? problem

Hello i have been working on shaders for a little while now reading the orange book but still having troubles

Anyways i was hoping someone would be able to show or explain simply how to discard a fragment on the background colour of a texel

like so

fragment shader

sampler2D myTexture;
varying vec2 vTexCoord;
void main (void)
vec4 color = texture2D(myTexture, gl_TexCoord);

gl_FragColor = color;

if(color.rgb = vec3(1,0,0))



vertex shader

varying vec2 vTexCoord;
void main(void)
vTexCoord = gl_MultiTexCoord0.xy;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

using version 1.2 glsl code

on another note is it wrong to not like too much technicality’s?

I have looked around on the net namingly the clockworkers,lighthouse,gl site, all sorts and the tutorials arnt working especially the clockworkers one i’m not shore about the other few because i was only looking on how to colorkey.


This is the closes i have found to helping me but it dont work it discards everything rather then select color i asked and my texture has red color on the background ,that color is red like in the example above

thanks in advance

  1. A scalar Boolean expression is expected.
  2. This is not a boolean operation.

Try this:

    bvec3 bv = equal(, vec3(1.0f, 0.0f, 0.0f));
    if ( bv.x && bv.y && bv.z) discard;

oh cool so could do the color with xyz then thats knew to me must have missed something in the orange book thanks anyways :slight_smile:

ahh man that doesn’t work

Have you tried with “==” instead of “=”?

Maybe it is just a matter of numeric accuracy. Comparing floats for equality is always a bit of a lottery. Try

  if( distance(color.rgb, vec3(1.0, 0.0, 0.0)) < 0.01 )

No, 0.0 is always 0 (8 bit)
and 1.0 is always 255 (8 bit). It is never 0.99999 or anything else.

Btw, in your fragment shader your using

vec4 color = texture2D(myTexture, gl_TexCoord); 

for the texture lookup. What you really want is to use your interpolated coordinates stored in vTexCoord.

vec4 color = texture2D(myTexture, vTexCoord); 

Maybe this’ll fix things.

Depends on what you want to achieve. If you’re striving to become an OpenGL professional then not liking technicalities is probably dead wrong. :slight_smile:

No, 0.0 is always 0 (8 bit)
and 1.0 is always 255 (8 bit). It is never 0.99999 or anything else. [/QUOTE]

Sorry for taking this a bit offtopic, but is there actually a guarantee for this anywhere in the GL spec ? We’re talking about texture fetch here, not vertex or texel fetch, so it may involve interpolation.

In GLES2 the answer is mostly clear: it depends on the precision qualifier. However lowp does not mean 8-bits per channel(!)

For GL the precision of computation on the fragment shader is essentially the GLES2 equivalent of highp, if textures is filtered I’d say no… but if the texture is unfiltered, it might work. However I would not count on it :stuck_out_tongue:

However, if one can assume that the bit depth of the texture is no more than 8-bits in a given channel one can do:

vec4 delta;

c=texture(samplerName, texutre_coordinate);
if(delta.x>=1.0f/255.0f || ... )

then for unfiltered textures that if(…) should be true only on those texels that match the value of c.

but if you really need equality, then integer textures are the way to go.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.