namespace Math { const Ray3 Ray3::X_AXIS = Ray3(Point4::ORIGIN, Vector4::UNIT_X); const Ray3 Ray3::Y_AXIS = Ray3(Point4::ORIGIN, Vector4::UNIT_Y); const Ray3 Ray3::Z_AXIS = Ray3(Point4::ORIGIN, Vector4::UNIT_Z); Ray3::Ray3(void) : origin(Point4::ORIGIN), direction(Vector4::UNIT_X) { } Ray3::Ray3(const Point4 &loc, const Vector4 &dir) : origin(loc), direction(dir) { } Point4 Ray3::GetPosition(void) const { return origin; } void Ray3::SetPosition(const Point4 &position) { origin = position; } }
Ray3 GLUtil::GetMouseRay() { GameEngine *engine = GameEngine::getInstance(); GLdouble m1x,m1y,m1z,m2x,m2y,m2z; Vector3 ray_dir; Vector3 ray_pos; GLint viewport[4]; GLdouble projMatrix[16], mvMatrix[16]; glGetIntegerv(GL_VIEWPORT,viewport); glGetDoublev(GL_MODELVIEW_MATRIX,mvMatrix); glGetDoublev(GL_PROJECTION_MATRIX,projMatrix); //unproject to find actual coordinates gluUnProject(engine->m_input->m_cursorX, engine->m_windowHeight - engine->m_input->m_cursorY, 0.0f, mvMatrix, projMatrix, viewport, &m1x,&m1y,&m1z); gluUnProject(engine->m_input->m_cursorX, engine->m_windowHeight - engine->m_input->m_cursorY, 1.0f, mvMatrix, projMatrix, viewport, &m2x,&m2y,&m2z); ray_pos = Vector3(m1x, m1y, m1z); ray_dir = Vector3(m2x, m2y, m2z) - ray_pos; ray_dir.normalize(); // maybe not necessary to have a unit direction return Ray3(ray_pos, ray_dir); }
//============================================================================= bool Intersect (const Sphere3 & s0, const Vector3 & v0, const Sphere3 & s1, const Vector3 & v1) { return Intersect( Ray3(s0.center, v0 - v1), Sphere3(s1.center, s0.r + s1.r) ); }
//============================================================================= bool Intersect (IntersectInfo3 & out, const Sphere3 & s0, const Vector3 & v0, const Sphere3 & s1) { return Intersect( out, Ray3(s0.center, v0), Sphere3(s1.center, s0.r + s1.r) ); }
TEST_F(Ray3fTest, MethodIntersectPlane) { // Intersect. Ray3 ray(Vec3f(0, 0, 0), Vec3f(0, 0, 1)); Plane plane(Vec3f(0, 0, 10), Vec3f(0, 0, -1)); Vec3f intersection; bool intersect = ray.intersect(plane, intersection); float result[3]; result[0] = 0; result[1] = 0; result[2] = 10; ASSERT_TRUE(intersect); cmpVec3f(result, intersection); // Ray pointing away. ray = Ray3(Vec3f(0, 0, 0), Vec3f(0, 0, -1)); plane = Plane(Vec3f(0, 0, 10), Vec3f(0, 0, -1)); intersect = ray.intersect(plane, intersection); ASSERT_TRUE(!intersect); // Normal same direction as ray. ray = Ray3(Vec3f(0, 0, 0), Vec3f(0, 0, 1)); plane = Plane(Vec3f(0, 0, 10), Vec3f(0, 0, 1)); intersect = ray.intersect(plane, intersection); result[0] = 0; result[1] = 0; result[2] = 10; ASSERT_TRUE(intersect); cmpVec3f(result, intersection); // Parallel. ray = Ray3(Vec3f(0, 0, 0), Vec3f(0, 0, 1)); plane = Plane(Vec3f(0, 0, 10), Vec3f(0, 1, 0)); intersect = ray.intersect(plane, intersection); ASSERT_TRUE(!intersect); }
#include "utility.h" #include "vector3.h" #include "aabb3.h" #include "plane.h" #include "sphere.h" #include "ray3.h" #include "ray3.inl" #if MAGICAL_MATH_CACHED_POOL_ENABLE #include "CachePool.h" #endif NS_MAGICAL_BEGIN const Ray3 Ray3::Zero = Ray3( 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f ); Ray3 Ray3::var = Ray3::Zero; Ray3::Ray3( float ox, float oy, float oz, float dx, float dy, float dz ) { magicalRay3Fill( this, ox, oy, oz, dx, dy, dz ); } Ray3::Ray3( const Ray3& r3 ) { magicalRay3Copy( this, &r3 ); } Ray3::Ray3( void ) { magicalRay3SetZero( this );
Ray3 PerspectiveCamera::generateRay(float x, float y) { Vec3 r = right.multiply((x-0.5)*fovScale); Vec3 u = up.multiply((y-0.5)*fovScale); return Ray3(eye, front.add(r).add(u).normalize()); }
/// Finds ray from origin of ray after traveling for time t. inline Ray3 rayAtTime(float t) const { return Ray3(origin + dir*t, dir); }