Jump to content
  • Advertisement
Sign in to follow this  
ehsan_the_tiamat

a question about fresnel effect...

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

hi guys a few days before I borrowed the "The CG Tutorial" from one of my friends for reading one of its chapters named "environment mapping techniques" and i came up with a wierd formula which is :: float reflectionCoefficient = max( 0, min(1, bias + scale * pow(1 + dot(In.incident, In.normal), power) ) ); althouh the formula in the book was mathematical, you see the HLSL equivalent(since i coudn't write the original formula because of the power i posted the one above). my question is what are "bias" "scale" "power" and in what range they should be initialized. it seems to me that scale is between 0 and 1. and also bias is lesser that one because of the min() function but i have no idea about power. if you have any practical values for these globals please send me. THX in advance.

Share this post


Link to post
Share on other sites
Advertisement
Ok, so that code calculates something like the fresnel term, however it is written a bit(to be honest, fuc*ing) ugly :), so I do not wonder at your confusion about it. It also doesn't really seem correct to me, since I can't see any indices of refraction involved in the computation. Anyways, here's how I compute the Fresnel term:

float fresnel(float3 light, float3 normal, float R0)
{
float cosAngle = 1.0f - dot(light, normal);
float result = cosAngle * cosAngle;
result = result * result;
result = result * cosAngle;
result = saturate((R0 + (1.0f-R0) * result));
return result;
}




where R0 is (n1 - n2) ^ 2 / (n1 + n2) ^ 2, where n1,n2 are the indices of refraction.

There's a possibility that your code would be a tiny bit faster, but definitely mine is more precise :)

Share this post


Link to post
Share on other sites
Could you explain a little bit more about R0. what are indices of refraction(n1, n2)???
here is my VS and PS for fresnel effect ::

VS_OUTPUT_FRESNEL fresnel_VS(
float4 inPosition : POSITION,
float2 inTexCoord : TEXCOORD0,
float3 inNormal : NORMAL
)
{

VS_OUTPUT_FRESNEL output = (VS_OUTPUT_FRESNEL)0;



float3 incident = inPosition.xyz - eyePosition;

output.incident = incident;

output.reflectVec = reflect(incident, inNormal);

float3 cam2Vert = normalize(eyePosition - inPosition.xyz);

output.refractVec = refract(cam2Vert, inNormal, etaRatio);

output.oPosition = mul(worldViewProjMat, inPosition);

output.texCoord = inTexCoord;

output.normal = inNormal;

output.objPos = inPosition;

return output;

}

float4 fresnel_PS( VS_OUTPUT_FRESNEL In ) : COLOR
{

float4 texColor = tex2D(meshTextureSampler, In.texCoord);

float4 reflectedColor = texCUBE(envMapSampler, In.reflectVec);

float4 refractedColor = texCUBE(envMapSampler, In.refractVec);

lightFuncOutput lightResult;

float4 diffuseSum = 0;

float4 specularSum = 0;

for(int i = 0; i < numOfLights; i++)
{
lightResult = LightPoint(lights, In.objPos, In.normal, In.cam2Vert);
diffuseSum += lightResult.diffuseResult ;
specularSum += lightResult.specularResult ;
}

float4 ambient = materialAmbient * globalAmbient;

float4 diffuse = materialDiffuse * diffuseSum;

float4 specular = materialSpecular * specularSum;

float4 lightingColor = ambient + diffuse + specular;

float4 objColor = lerp(texColor, lightingColor , 0.5);

float reflectionCoefficient = max( 0, min(1, bias + scale * pow(1 + dot(In.incident, In.normal), power) ) );

float4 color = reflectionCoefficient * reflectedColor + (1 - reflectionCoefficient) * refractedColor;

return lerp(objColor, color, 0.5);

}

Share this post


Link to post
Share on other sites
R0 is a constant, fresnel constant. Indices of refraction are numbers, describing each material and how it reacts when the light hits its boundary. Here is a decent paper focused on this subject.

Regarding your shaders, instead of this line:

float reflectionCoefficient = max( 0, min(1, bias + scale * pow(1 + dot(In.incident, In.normal), power) ) );


try this line

float reflectionCoefficient = fresnel(In.incident, In.normal, 0.02037f);


As you can see R0 becoms 0.02037f, it is the fresnel constant for water-air boundary.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!