bool Plane::intersect(const Ray& ray, IntersectionData& intersectionData) { if ((ray.Start().Y() > this->Y() && ray.Direction().Y() > VerySmall) || (ray.Start().Y() < this->Y() && ray.Direction().Y() < VerySmall)) { return false; } double yRayDirection = ray.Direction().Y(); double yDifference = ray.Start().Y() - this->Y(); double multiplier = yDifference / -yRayDirection; if (multiplier > intersectionData.getDistance()) { return false; } Vector intersectionPoint = ray.Start() + ray.Direction() * multiplier; if (fabs(intersectionPoint.X()) > this->Limit() || fabs(intersectionPoint.Z()) > this->Limit()) { return false; } intersectionData.setIntersectionPoint(intersectionPoint); intersectionData.setDistance(multiplier); intersectionData.setNormal(Vector(0.0, 1.0, 0.0)); intersectionData.setTextureU(intersectionData.IntersectionPoint().X()); intersectionData.setTextureV(intersectionData.IntersectionPoint().Z()); return true; }
//------------------------------------------------------------- SColor Scene :: IntersectShadow( Ray r, float maxt ) { //------------------------------------------------------------- SColor att = SColor(1); Primitive3D * p; while( (p = world.NextPrimitive( )) != NULL ) { float t = p -> Intersect( r ); if ( t > EPSILON && t < maxt) { Point x = r.Start() + r.Dir() * t; att *= p -> kt( x ); } } return att; }
//------------------------------------------------------------- float Sphere :: Intersect( Ray& r ) { //------------------------------------------------------------- Vector dist = r.Start() - center; float b = (dist * r.Dir()) * 2.0; float a = (r.Dir() * r.Dir()); float c = (dist * dist) - radius * radius; float discr = b * b - 4.0 * a * c; if ( discr < 0 ) return -1; float sqrt_discr = sqrt( discr ); float t1 = (-b + sqrt_discr)/2.0/a; float t2 = (-b - sqrt_discr)/2.0/a; if (t1 < EPSILON) t1 = -EPSILON; if (t2 < EPSILON) t2 = -EPSILON; if (t1 < 0 && t2 < 0) return -1; float t; if ( t1 < 0 && t2 >= 0 ) t = t2; else if ( t2 < 0 && t1 >= 0 ) t = t1; else if (t1 < t2) t = t1; else t = t2; return t; }