Ray Intersection Steve Marschner CS 4620 Cornell University Cornell CS4620 Fall 2020 Steve Marschner • 1
Ray Intersection 1. Ray-sphere intersection Cornell CS4620 Fall 2020 Steve Marschner • 2
Ray: a half line • Standard representation: origin point and direction p d r ( t ) = p + t d – this is a parametric equation for the line – lets us directly generate the points on the line – if we restrict to then we have a ray t > 0 – note replacing with doesn’t change ray (for ) d α d α > 0 Cornell CS4620 Fall 2020 Steve Marschner • 3
Ray-sphere intersection: algebraic • Condition 1: point is on ray • Condition 2: point is on sphere – assume unit sphere; see book or notes for general • Substitute: – this is a quadratic equation in t Cornell CS4620 Fall 2020 Steve Marschner • 4
Ray-sphere intersection: algebraic • Solution for t by quadratic formula: – simpler form holds when d is a unit vector but we won’t assume this in practice (reason later) – I’ll use the unit-vector form to make the geometric interpretation Cornell CS4620 Fall 2020 Steve Marschner • 5
Ray-sphere intersection: geometric Cornell CS4620 Fall 2020 Steve Marschner • 6
Image so far • With eye ray generation and sphere intersection Surface s = new Sphere((0.0, 0.0, 0.0), 1.0); for 0 <= iy < ny for 0 <= ix < nx { ray = camera.getRay(ix, iy); hitSurface, t = s.intersect(ray, 0, +inf) if hitSurface is not null image.set(ix, iy, white); } Cornell CS4620 Fall 2020 Steve Marschner • 7
Ray Intersection 2. Ray-triangle intersection Cornell CS4620 Fall 2020 Steve Marschner • 8
Barycentric coordinates • A coordinate system for triangles – algebraic viewpoint: – geometric viewpoint (areas): • Triangle interior test: [Shirley 2000] Cornell CS4620 Fall 2020 Steve Marschner • 9
Barycentric coordinates • A coordinate system for triangles – geometric viewpoint: distances – linear viewpoint: basis of edges Cornell CS4620 Fall 2020 Steve Marschner • 10
Barycentric coordinates • Linear viewpoint: basis for the plane [Shirley 2000] – in this view, the triangle interior test is just Cornell CS4620 Fall 2020 Steve Marschner • 11
Barycentric ray-triangle intersection • Every point on the plane can be written in the form: a + β ( b − a ) + γ ( c − a ) for some numbers β and . γ • If the point is also on the ray then it is p + t d for some number t . • Set them equal: 3 linear equations in 3 variables p + t d = a + β ( b − a ) + γ ( c − a ) …solve them to get t , β , and all at once! γ Cornell CS4620 Fall 2020 Steve Marschner • 12
Barycentric ray-triangle intersection p + t d = a + β ( b − a ) + γ ( c − a ) β ( a − b ) + γ ( a − c ) + t d = a − p 2 3 β 5 = ⇥ a − b d ⇤ ⇥ a − p ⇤ γ a − c 4 t 2 3 2 3 2 3 β x a − x b x a − x c x d x a − x p 5 = γ y a − y b y a − y c y d y a − y p 4 5 4 4 5 z a − z b z a − z c z d t z a − z p Cramer’s rule is a good fast way to solve this system (see text Ch. 2 and Ch. 4 for details) Cornell CS4620 Fall 2020 Steve Marschner • 13
Ray Intersection 3. Ray intersection software Cornell CS4620 Fall 2020 Steve Marschner • 14
Rays • Rays are a useful datatype: pair origin with direction • Also very useful to make rays into ray segments – store a start (minimum ) and end (maximum ) t t – ray intersections only count if is between start and end t class Ray { Point origin Vector direction float start float end; } Cornell CS4620 Fall 2020 Steve Marschner • 15
Surfaces • All surfaces need to be able to intersect rays with themselves – surfaces with multiple intersections must return the first one – convenient to return when there is no intersection t = + ∞ ray to be intersected class Surface { class Hit { boolean intersect(Hit hit, Ray r) float t … Surface surface } Vector position Vector normal is there any … intersection? } information about first intersection Cornell CS4620 Fall 2020 Steve Marschner • 16
Intersection with ray segments t = 0 t start t end [ ] [ ] [ ] [ ] Cornell CS4620 Fall 2020 Steve Marschner • 17
Scenes • Scenes contain many objects • Need to find the first intersection along the ray – that is, the one with the smallest positive t value in [start, end] class Scene { List<Surface> surfaces … } Cornell CS4620 Fall 2020 Steve Marschner • 18
Intersection against many shapes • Input is a ray and a collection of surfaces • Simple linear time algorithm: function intersect(ray, surfaceList) for surface in surfaceList intersect ray against surface if hit, reduce ray.end to of hit t return surface corresponding to minimum- intersection t • This is fine for small scenes, too slow for large ones – real applications use sublinear methods (acceleration structures) which we will see later Cornell CS4620 Fall 2020 Steve Marschner • 19
Recommend
More recommend