Created
October 5, 2010 08:03
-
-
Save stephentu/611203 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "algebra3.h" | |
#include <iostream> | |
#include <cmath> | |
using namespace std; | |
static inline bool refract(const vec3& N, const vec3& I, const double n1, const double n2, vec3& result) { | |
double n = n1 / n2; | |
double cosI = -(N * I); | |
double sinT2 = n * n * ( 1.0 - cosI * cosI ); | |
if (sinT2 > 1.0) return false; | |
double cosT = sqrt( 1.0 - sinT2 ); | |
result = n * I + ( n * cosI - cosT ) * N; | |
return true; | |
} | |
static inline bool refract0(const vec3& N, const vec3& I, const double n1, const double n2, vec3& result) { | |
double dDotN = I * N; | |
double cosPhiSq = 1.0 - n1 * n1 * ( 1.0 - dDotN * dDotN ) / ( n2 * n2 ); | |
if (cosPhiSq < 0) return false; | |
result = ( n1 * ( I - ( N * dDotN ) ) / n2 ) - ( N * sqrt(cosPhiSq) ); | |
return true; | |
} | |
int main(int argc, char **argv) { | |
vec3 I(0.707107, -0.707107, 0.0); | |
vec3 N(0, 1, 0); | |
double n2 = 1.333; | |
double n1 = 0.9 * n2; | |
vec3 T1; | |
if (refract(N, I, n1, n2, T1)) { | |
cout << "Success. T = " << T1 << endl; | |
} else | |
cout << "Failure" << endl; | |
vec3 T2; | |
if (refract0(N, I, n1, n2, T2)) { | |
cout << "Success. T = " << T2 << endl; | |
} else | |
cout << "Failure" << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment