00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <cmath>
00014
00015 #include "General/Types.hh"
00016 #include "Math/Vector.hh"
00017 #include "Render/Ray.hh"
00018
00019 namespace Render {
00020 Ray Ray::RayFromPoints(const Math::Vector &Source,
00021 const Math::Vector &Destination)
00022 {
00023 return Ray(Source, (Destination - Source).Normalize());
00024 }
00025
00026
00027 Ray Ray::Reflect(const Math::Vector &Normal,
00028 const Math::Vector &Point) const
00029 {
00034 const Math::Vector tmp = Normal * (2 * Normal.Dot(this->D));
00035 return Ray(Point, this->D - tmp);
00036 }
00037
00038 Ray Ray::Refract(const Math::Vector &Normal,
00039 const Math::Vector &Point,
00040 Double FromN, Double IntoN) const
00041 {
00042 const Double n = FromN / IntoN;
00043 const Double c1 = - Normal.Dot(this->D);
00044 const Double c2 = std::sqrt(1.0 - n*n * (1.0 - c1*c1) );
00045 const Double c3 = n * c1 - c2;
00046 const Math::Vector Dir = (this->D * n) + (Normal * c3);
00047 return Ray(Point, Dir);
00048 }
00049
00050 std::ostream &operator<<(std::ostream &os, const Ray &R)
00051 {
00052 os << "[Ray Start="
00053 << R.Start()
00054 << " Dir="
00055 << R.Direction()
00056 << "]";
00057 return os;
00058 }
00059
00060 }