Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


GLSL - pixel color based on alpha


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 ardmax1   Members   -  Reputation: 127

Like
0Likes
Like

Posted 12 June 2012 - 06:09 AM

Hi,
is it possible to write GLSL shader such that it will change each texture pixel color based on its alpha value ?
Something like:
[source lang="cpp"]vec4 pixel = texture2D( texture, pos );if( pixel.a > 0.5f ){ pixel = vec4( 0.0, 1.0, 0.0, 1.0 );}else{ pixel = vec4( 1.0, 0.0, 0.0, 1.0 );}[/source]
If it is, could you guys give me tips on how to make it ?

Sponsor:

#2 Relfos   Members   -  Reputation: 322

Like
0Likes
Like

Posted 12 June 2012 - 07:17 AM

Yes, it is possible, and you just gave the answer yourself Posted Image

#3 ardmax1   Members   -  Reputation: 127

Like
0Likes
Like

Posted 12 June 2012 - 07:53 AM

If it should work, then i have error somewhere else.

This produces just red
fragment:
[sourcelang="cpp"]varying vec4 pos;
uniform sampler2D texture;

void main(){
vec4 pixel = texture2D(texture, vec2(pos));
if (pixel.a > 0.5){
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}else{
gl_FragColor = vec4(0.0, 0.0, 1.0, 0.3);
}
}
[/source]
vertex:
[source lang="cpp"]varying vec4 pos;void main(){ pos = gl_MultiTexCoord0; gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;}[/source]

#4 Cornstalks   Crossbones+   -  Reputation: 6991

Like
0Likes
Like

Posted 12 June 2012 - 08:07 AM

Have you enabled alpha blending? How have you created the texture? The shaders look ok, so I'm guessing the problem is outside of the shaders.

Edited by Cornstalks, 12 June 2012 - 08:09 AM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#5 clb   Members   -  Reputation: 1785

Like
0Likes
Like

Posted 12 June 2012 - 08:42 AM

Sampling the alpha channel of a texture does not require alpha blending to the framebuffer to be enabled. Make sure that your texture actually does have an alpha channel, and it's not all 1.f in each pixel.

Try debugging visually the contents of the alpha channel with the following fragment shader:

	 
	varying vec4 pos;
	uniform sampler2D texture;
	
	void main(){
	vec4 pixel = texture2D(texture, vec2(pos));
	gl_FragColor = vec4(pixel.aaa, 1.0);
	}

this should render the alpha channel out in grayscale, so you can confirm that it is not all white.
Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

#6 ardmax1   Members   -  Reputation: 127

Like
0Likes
Like

Posted 12 June 2012 - 09:10 AM

ok I found out was the problem.
First it was caused by clearing texture, which is used to apply shaders on whole screen rather that on parts that are drawn (This is I think SFML problem).
Second, I had to normalize position, so it looks like this now
pixel = texture2D(texture, pos.xy / texResolution);

but why this makes texture flip vertically?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS