Sign in to follow this  

color per pixel

This topic is 2178 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

This should be pretty basic stuff, but i just can't get my head around it.

Basically what i want to do is check wether a pixel is facing the camera or not. If the pixel is directly facing the camera, then i want to color it completely black. When it's 90 degrees rotated, then it should have full transparency.

I guess i have all the needed stuff in my vertex shader:

[code]
varying vec3 Normal;
varying vec3 EyeDir;
varying vec4 EyePos;

void main(void)
{
gl_Position = ftransform();
Normal = normalize(gl_NormalMatrix * gl_Normal);
vec4 pos = gl_ModelViewMatrix * gl_Vertex;
EyeDir = pos.xyz;
EyePos = gl_ModelViewProjectionMatrix * gl_Vertex;
}
[/code]

In my fragment shader i have two textures. One is just some random image and the other is completely black. I guess i have to "mix" them some how, depending on the position of the pixel. But i have no idea how to calculate that...

[size=4]This is something i've tried, but didn't worked out as planned.[/size]
[code]
vec4 RefractionColor = texture2D(RefractionMap, index);

// Apply the black material
vec4 blackMat = texture2D(Black, gl_TexCoord[0].st);
float shade = dot(normalize(EyeDir), normalize(Normal));

// Blend the textures
RefractionColor = mix(blackMat, RefractionColor, shade);
gl_FragColor = RefractionColor;
[/code]

[size=4]Anyone any idea how to do this..??[/size]

Share this post


Link to post
Share on other sites
[quote]
Basically what i want to do is check wether a pixel is facing the camera or not. If the pixel is directly facing the camera, then i want to color it completely black. When it's 90 degrees rotated, then it should have full transparency.
[/quote]
If it's 45 degrees rotated is it should contain 50% - black and 50% - transparent? If yes:
[code]
float shade = max(dot(normalize(EyeDir), normalize(Normal)),0.0); // fixing 'solve' because dot returns [-1.0..1.0] but mix expects result in range [0.0...1.0]
[/code]
Otherwise:
[code]
float shade = float(dot(normalize(EyeDir), normalize(Normal))>0.0);
[/code]

Is this what you want?

Best wishes, FXACE.

Share this post


Link to post
Share on other sites
[quote name='FXACE' timestamp='1327853293' post='4907352']
If it's 45 degrees rotated is it should contain 50% - black and 50% - transparent? If yes:
[code]
float shade = max(dot(normalize(EyeDir), normalize(Normal)),0.0); // fixing 'solve' because dot returns [-1.0..1.0] but mix expects result in range [0.0...1.0]
[/code]

Is this what you want?

Best wishes, FXACE.
[/quote]

Yes, that's exactly what i mean. When it's 45 degrees then the color should be 50% transparent.

I tried your example, but it still paints my entire sphere black. I have no compile errors though. So the shader is fine. The calculation is somehow not correct.

Any idea what the problem could be?

Share this post


Link to post
Share on other sites
It's working now. I had to change the EyeDir with EyePos.

Just one other thing that i'm struggling with. Now the gradient goes from all black (0 degrees; a pixel which faces the camera), to 90 degrees, fully transparent (a pixel that isn't facing the camera). Like i wanted.

What if i want it to go from 0 degrees (fully black) to 30 degrees (100% transparent) instead of 90 degrees..?

I've tried to do some additions, subtractions on the dot product. But they didn't gave the desired result.

Anyone any idea?

Share this post


Link to post
Share on other sites
shade = max((dp - 0.866025) / (1.0 - 0.866025),0.0); where: "dp" is a dot production of EyePos to Normal; 0.866025 - cos(30 <your degrees> * Pi / 180).

Best wishes, FXACE.

Share this post


Link to post
Share on other sites
Hi, thanks alot for your help! It seems to be working fine when i use the following:

[code]
float dp = (max(dot(normalize(EyePos), normalize(Normal)), 0.0) + MixRatio); //MixRatio = for strength

float shade = max((dp - 0.866025) / (1.0 - 0.866025),0.0);
//float shade = dp - cos(30 * 3.14159 * 180);
[/code]

Although i wasn't sure what the 2nd formula you posted was for? It didn't seem to give the desired effect. And i'm also not sure how you got to the number '866025'. It looks like that when i change that number, that it also changes the range of the falloff. Kinda like i'm doing with the MixRatio variable when i caluclate the 'dp' variable.

Share this post


Link to post
Share on other sites
2nd formula from my first post?... Never mind. (It was variant "did you want to make it blended or not")

Hmmm, I would try to rewrite my previous post to make it more clear...

1. Dot product calculation looks like this:
[code]
float dp = dot(normalize(EyePos),normalize(Normal));
[/code]

2. Compute value for 'gradient' (from post #4, 0 degrees - black and 30 degrees - transparent):
[code]
float cMaxDegreesOfFacing = cos(30 * Pi / 180); // which equals to (0.866025...) <<<!
[/code]

0.866025..... was returned from calculation I made in 'Microsoft Calculator' (calc.exe) -> cos of 30 degrees. I don't think that was incorrect.


3.Finally compute 'shade':
[code]
float shade = max((dp - cMaxDegreesOfFacing) / (1.0 - cMaxDegreesOfFacing),0.0);
[/code]

That's all what I meaned...

Best wishes, FXACE.

Share this post


Link to post
Share on other sites

This topic is 2178 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this