• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# color per pixel

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.

7 replies to this topic

### #1vivendi  Members

Posted 29 January 2012 - 09:02 AM

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:

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;
}


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...

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

// Apply the black material
vec4 blackMat = texture2D(Black, gl_TexCoord[0].st);

// Blend the textures
gl_FragColor = RefractionColor;


Anyone any idea how to do this..??

### #2FXACE  Members

Posted 29 January 2012 - 10:08 AM

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.

If it's 45 degrees rotated is it should contain 50% - black and 50% - transparent? If yes:
 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]

Otherwise:
 float shade = float(dot(normalize(EyeDir), normalize(Normal))>0.0);


Is this what you want?

Best wishes, FXACE.

### #3vivendi  Members

Posted 29 January 2012 - 10:21 AM

If it's 45 degrees rotated is it should contain 50% - black and 50% - transparent? If yes:

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]


Is this what you want?

Best wishes, FXACE.

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?

### #4vivendi  Members

Posted 29 January 2012 - 12:36 PM

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?

### #5FXACE  Members

Posted 29 January 2012 - 04:52 PM

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.

### #6vivendi  Members

Posted 30 January 2012 - 07:12 AM

Hi, thanks alot for your help! It seems to be working fine when i use the following:

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);


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.

### #7FXACE  Members

Posted 30 January 2012 - 11:03 AM

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:
float dp = dot(normalize(EyePos),normalize(Normal));


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


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

float shade = max((dp - cMaxDegreesOfFacing) / (1.0 - cMaxDegreesOfFacing),0.0);


That's all what I meaned...

Best wishes, FXACE.

### #8vivendi  Members

Posted 30 January 2012 - 01:40 PM

I see, now i get it

It works excellent. Thanks alot for your help!

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.