PDA

View Full Version : GLSL and discard? problem



noobscratcher
10-23-2011, 03:31 AM
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))
discard;


}

/////////////////////////////////////////////////

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.

http://www.opengl.or...ers/discard.php

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

biller23
10-23-2011, 06:54 AM
if(color.rgb = vec3(1,0,0))
discard;


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

Try this:


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

noobscratcher
10-23-2011, 07:15 AM
oh cool so could do the color with xyz then thats knew to me must have missed something in the orange book thanks anyways :)

noobscratcher
10-23-2011, 08:31 AM
ahh man that doesn't work

Dan Bartlett
10-23-2011, 01:42 PM
Have you tried with "==" instead of "="?

mbentrup
10-24-2011, 12:14 AM
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 )
discard;

V-man
10-24-2011, 04:43 AM
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 )
discard;




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

thokra
10-24-2011, 05:26 AM
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.


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

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

mbentrup
10-24-2011, 06:30 AM
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 )
discard;



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

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.

kRogue
10-24-2011, 10:27 AM
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 :p

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);
delta=abs(p-c);
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.