Browse files

Fixed P shader for imaginary square root value; Added SP shaders; The…

… whole think totally, like, works now
  • Loading branch information...
1 parent 9b1de81 commit 4bfa8b6503bded4f55ba1ba5c57ffd952e813a68 Igor Zablotchi committed Dec 15, 2011
Showing with 77 additions and 7 deletions.
  1. +4 −2 shaders/fresnelComplexP.vp
  2. +20 −0 shaders/fresnelComplexSP.fp
  3. +47 −0 shaders/fresnelComplexSP.vp
  4. +6 −5 src/HdrApp.cpp
View
6 shaders/fresnelComplexP.vp
@@ -27,8 +27,10 @@ void main()
float cosThetaI = dot(-I,N);
// Real fresnel equation
- refFactor = (etaRatio * sqrt(1.0 - (1.0 - cosThetaI * cosThetaI)*etaRatio*etaRatio) - cosThetaI);
- refFactor = refFactor/(etaRatio * sqrt(1.0 - (1.0 - cosThetaI * cosThetaI)*etaRatio*etaRatio) + cosThetaI);
+ float squareRoot = 1.0 - (1.0 - cosThetaI * cosThetaI)*etaRatio*etaRatio;
+ squareRoot = (squareRoot < 0.0) ? 0.0 : squareRoot;
+ refFactor = (etaRatio * sqrt(squareRoot) - cosThetaI);
+ refFactor = refFactor/(etaRatio * sqrt(squareRoot) + cosThetaI);
refFactor = refFactor * refFactor;
// Transform vertex
View
20 shaders/fresnelComplexSP.fp
@@ -0,0 +1,20 @@
+// Cube map
+uniform samplerCube env;
+
+// Reflected and refracted vectors
+varying vec3 reflectedVector, refractedVector;
+
+// Reflection factor based on fresnel equation
+varying float refFactor;
+
+void main()
+{
+ // Read cube map
+ vec4 reflectedColor = textureCube(env, reflectedVector);
+ vec4 refractedColor = textureCube(env, refractedVector);
+
+ // Mix reflected and refracted colors
+ vec4 color = mix(refractedColor, reflectedColor, refFactor);
+ color.a = 1.0;
+ gl_FragColor = color;
+}
View
47 shaders/fresnelComplexSP.vp
@@ -0,0 +1,47 @@
+// Position of the view eye in world space
+uniform vec3 eyePos;
+
+// Ratio of indices of refraction
+uniform float etaRatio;
+
+// Reflected and refracted vectors
+varying vec3 reflectedVector, refractedVector;
+
+// Reflection factor based on fresnel equation
+varying float refFactor;
+
+void main()
+{
+ // Create incident and normal vectors
+ vec3 I = normalize(gl_Vertex.xyz - eyePos.xyz);
+ vec3 N = normalize(gl_Normal);
+
+ // Calculate reflected and refracted vectors
+ reflectedVector = reflect(I, N);
+ refractedVector = refract(I, N, etaRatio);
+
+
+ // Compute cos(incident angle) for the fresnel equations
+ float cosThetaI = dot(-I,N);
+
+ // Real fresnel equation
+ float squareRoot = 1.0 - (1.0 - cosThetaI * cosThetaI)*etaRatio*etaRatio;
+ squareRoot = (squareRoot < 0.0) ? 0.0 : squareRoot;
+
+ float RS;
+ RS = etaRatio * cosThetaI - sqrt(squareRoot);
+ RS = RS/(etaRatio * cosThetaI + sqrt(squareRoot));
+ RS = RS * RS;
+
+ float RP;
+ RP = (etaRatio * sqrt(squareRoot) - cosThetaI);
+ RP = RP/(etaRatio * sqrt(squareRoot) + cosThetaI);
+ RP = RP * RP;
+
+ refFactor = (RS + RP)/2.0;
+
+
+ // Transform vertex
+ gl_TexCoord[0] = gl_MultiTexCoord0;
+ gl_Position = ftransform();
+}
View
11 src/HdrApp.cpp
@@ -34,8 +34,9 @@ HdrApp::HdrApp()
fileShaderEffect.push_back("reflectTex");
fileShaderEffect.push_back("refract");
fileShaderEffect.push_back("fresnel");
- fileShaderEffect.push_back("fresnelComplexP");
- fileShaderEffect.push_back("fresnelComplexS");
+ fileShaderEffect.push_back("fresnelComplexP");
+ fileShaderEffect.push_back("fresnelComplexS");
+ fileShaderEffect.push_back("fresnelComplexSP");
fileShaderEffect.push_back("chromaticDispersion");
shaderEffect.resize(fileShaderEffect.size());
resetEffect();
@@ -431,7 +432,7 @@ void HdrApp::renderText()
font.addText(2, 122, c, "(e) Exposure: %.2f", exposure);
// Fresnel parameters
- if (currentEffect == 3 || currentEffect == 6)
+ if (currentEffect == 3 || currentEffect == 7)
{
c = Color(0.6f, 0.6f, 0.8f);
font.addText(screenW-190, 62, c, "Fresnel effect:");
@@ -451,14 +452,14 @@ void HdrApp::renderText()
}
// eta ratio
- if (currentEffect >= 2 && currentEffect <= 5 )
+ if (currentEffect >= 2 && currentEffect <= 6 )
{
c = Color(0.6f, 0.6f, 0.8f);
font.addText(2, 142, c, "(a) eta ratio: %.2f", etaRatio.x);
}
// eta ratio : rgb
- if (currentEffect == 6)
+ if (currentEffect == 7)
{
c = Color(0.6f, 0.6f, 0.8f);
font.addText(screenW-360, 62, c, "(a) eta ratio:");

0 comments on commit 4bfa8b6

Please sign in to comment.