whodini Posted February 21, 2014 Share Posted February 21, 2014 Hey Guys! I've been experimenting with the refract node in vopsop context as well as the refract() function in vex. I'm curious if anyone has been able to break that node down to it's raw form? I would essentially like to know how it actually evaluates it's inputs. Can anyone shed some light on that? I would like to know the math behind it. The refract function takes three inputs, a direction vector, a normalized normal and an index of refraction. thanks! Quote Link to comment Share on other sites More sharing options...
jkunz07 Posted February 21, 2014 Share Posted February 21, 2014 The direction vector is the incident vector, "i" in the above image. The normalized normal is the surface normal "n". Index of refraction controls the amount that the incident ray will be refracted, a value of 1 will leave "i" unchanged. A value of 1.33 will refract the ray similar to water. The red arrow in the image above represents the output vector of the refract() function. Hope that helps! 1 Quote Link to comment Share on other sites More sharing options...
whodini Posted February 21, 2014 Author Share Posted February 21, 2014 thanks for the diagram! however I was really looking for an explanation of the math that is going on within the refract() function. How does the refract() function generate the angle of refraction, given the angle of incidence, the normalized normal and an index of refraction? Quote Link to comment Share on other sites More sharing options...
magneto Posted February 21, 2014 Share Posted February 21, 2014 Not tested but this looks fine (eta is IOR): inline void Refract( VEC3 &out, const VEC3 &incidentVec, const VEC3 &normal, float eta) { float N_dot_I = Dot(normal, incidentVec); float k = 1.f - eta * eta * (1.f - N_dot_I * N_dot_I); if (k < 0.f) out = VEC3(0.f, 0.f, 0.f); else out = eta * incidentVec - (eta * N_dot_I + sqrtf(k)) * N; } Source: http://asawicki.info/news_1301_reflect_and_refract_functions.html 1 Quote Link to comment Share on other sites More sharing options...
whodini Posted February 22, 2014 Author Share Posted February 22, 2014 thank you magneto!!!! this has made my day! I've been googling like a mad man for the past 3 days. This will be added to my bookmarks. Quote Link to comment Share on other sites More sharing options...
edward Posted February 22, 2014 Share Posted February 22, 2014 Refraction is just algebraic simplification of Snell's law, here's a link from google that shows the derivation: http://graphics.stanford.edu/courses/cs148-10-summer/docs/2006--degreve--reflection_refraction.pdf EDIT: Nevermind, the wikipedia entry for Snell's law has the algebraic derivation as well. Quote Link to comment Share on other sites More sharing options...
whodini Posted February 22, 2014 Author Share Posted February 22, 2014 (edited) thank you guys, my goal is to implement a "raw" version of the refract node for the purpose of using it in other contexts such as python, where the refract() function would not be accessible. In other words I'm writing a python equivalent of the refract() vex, but I want to be sure the calculation is correct and produces the exact same results. Edited February 22, 2014 by rhussain Quote Link to comment Share on other sites More sharing options...
whodini Posted February 22, 2014 Author Share Posted February 22, 2014 (edited) I've found that the results are not the same between the provided code and the refract vex function. Thats because the refract() function performs a few additional calculations than the code you provided. If there is total internal reflection then the calculation is changed to reflect() instead of returning {0,0,0}. Also if the incident ray is already under the normals direction :inside to inside relationship then the normal must be flipped and the value of eta needs to be changed from n1 / n2 to n2 / n1. It's all making sense now! Edited March 5, 2014 by rhussain Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.