Ray tracing - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Ray tracing

Description:

If the ray intersects, then we can check each individual ... Where a, b, c are the x, y, z of the normal d is the constant d. Useful things to do with planes ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 26
Provided by: bdug1
Category:
Tags: ray | tracing

less

Transcript and Presenter's Notes

Title: Ray tracing


1
Ray tracing
  • Bryan Duggan

2
Ray tracing
  • Is a useful technique for
  • Calculating projectile paths (instantly)
  • Figuring out what object is selected when the
    mouse clicks on an area in a 3D scene
  • Implementing perception
  • Implementing collision detection
  • Lots of things!
  • Some examples

3
(No Transcript)
4
(No Transcript)
5
The theory
  • We derive an equation for a ray
  • point
  • orientation (normalised)
  • We derive an equation for the things in our world
  • Spheres
  • Planes
  • We put one into the other and solve to get points
    of intersection
  • We then check to see if the point is within the
    bounds of the object

6
For a complex model
  • We create hitboxes. If the ray intersects, then
    we can check each individual plane of the model
  • If we really want to

7
What is a ray?
  • A 3D line
  • A point and a direction
  • Equation of a ray will give you any point on the
    ray
  • You can plug a point into the equation and see if
    it is on the ray

8
Equation
  • p(t) p0 t(u)
  • p a 3D point
  • t Distance from the origin to the point
  • u direction
  • To check if a point is on the Ray
  • 0 p0 t(u) p(t)

9
Planes
  • Can be described using a point on the plane and a
    normal vector, a vector perpendicular to the
    plane

10
Equation of a plane
  • Remember
  • Cos(PI / 2) 0, so
  • If A and B are perpendicular vectorsA.B 0
  • The equation of a plane is given by
  • n.(p p0) 0

11
Equation of a plane
  • n.(p-p0) Can be written as
  • n.p n.p0 (Distributive)
  • We can therefore write the equation asn.p d
    0Where d -n.p0
  • d is also shortest signed distance from the
    origin to the plane
  • When storing a plane, we can get away with
    storing n and d (A 4D vector)

12
Planes in DirectX
  • We can use the classD3DXPLANE to represent a
    plane D3DXPLANE plane D3DXPLANE(float a,
    float b, float c, float d)Where a, b, c are
    the x, y, z of the normal d is the constant d

13
Useful things to do with planes
  • If p is a pointIf n.p d 0, the point is on
    the planeIf n.p d gt 0, the point is in front
    of the planeIf n.p d lt 0, the point is behind
    the plane
  • Use D3DXPlaneDotCoord(D3DXPLANE plane,
    D3DXVECTOR3 v)
  • To do the above calculation

14
Construction
  • You can construct a planeD3DXPLANE
    D3DXPlaneFromPointNormal( D3DXPLANE pOut, CONST
    D3DXVECTOR3 pPoint, CONST D3DXVECTOR3 pNormal
    )

15
Or
  • Given 3 points
  • D3DXPLANE WINAPI D3DXPlaneFromPoints(          D
    3DXPLANE pOut,     CONST D3DXVECTOR3 pV1,
        CONST D3DXVECTOR3 pV2,     CONST
    D3DXVECTOR3 pV3 )

16
Ray Plane intersection
  • Given a ray p(t) p0 tu
  • And a plane n.p d 0
  • We first solve for t by plugging the ray into the
    plane
  • n.(p0 tu) d 0n.p0 n.tu d 0n.tu -d
    n.p0t(n.u) -d n. p0t -d n. p0 / (n.u)
  • If t is gt 0, the ray intersects the plane
  • If t lt 0, the ray does not intersect the plane

17
If they intersect its easy!
  • We just calculate the point using the ray
    equation p(t) p0 tu

18
Projection
  • Transforming a 3D Scene to a 2D one
  • Uses perspective projection
  • Far away objects are smaller
  • Defines our frustum

19
The projection matrix
  • Is complex to derive, but directX has an API to
    do it
  • D3DXMATRIX proj
  • D3DXMatrixPerspectiveFovLH(
  • proj,
  • D3DX_PI 0.5f, // 90 - degree
  • (float)_width / (float)_height,
  • 1.0f,
  • 1000.0f)
  • _device-gtSetTransform(D3DTS_PROJECTION, proj)

20
Picking
  • To perform picking, we need to shoot a picking
    ray from the camera position that passes through
    the point where the mouse clicked

21
To calculate the picking ray
  • Assuming View Space transforms the camera to
    0,0,0
  • We calculate the point at which the ray
    intersects the plane z 1

22
Step 1, calculate u
  • Transform the screen point to the projection
    window (z 1) plane
  • The viewport transformation matrix is
  • Transforming a point p by the viewport
    transformation gives s, a point on the projection
    window
  • sx px(Width / 2) X (Width / 2)
  • sy -py(Height / 2) Y Height / 2

23
Solving for p we obtain
  • px (2sx 2X Width) / Width
  • py (-2sy 2Y Height) / Height
  • Assuming X and Y are 0,
  • Px ((2.Sx)/Width) 1
  • Py ((-2.sy)/Height) 1
  • pz 1

24
Scaling
  • The projection matrix also scales the points to
    simulate different points of view, so we must
    multiply by the inverse of the scaling (1/scale)
  • The scaling is stored on p00 and p11 of the
    projection matrix, so we get
  • px ((2x/viewportWidth) -1)(1/p00)
  • py ((2y/viewportHeight) -1)(1/p11)

25
Step 2, transform p0 and u to world space
  • Remember the view transform moves the camera to
    0,0,0 facing down the positive z axis
  • We need to transform the ray by the inverse of
    the view matrix to bring it back to world space
Write a Comment
User Comments (0)
About PowerShow.com