Loading...

PPT – Ray tracing PowerPoint presentation | free to download - id: c3711-ZDc1Z

The Adobe Flash plugin is needed to view this content

Ray tracing

- Bryan Duggan

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

(No Transcript)

(No Transcript)

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

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

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

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)

Planes

- Can be described using a point on the plane and a

normal vector, a vector perpendicular to the

plane

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

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)

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

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

Construction

- You can construct a planeD3DXPLANE

D3DXPlaneFromPointNormal( D3DXPLANE pOut, CONST

D3DXVECTOR3 pPoint, CONST D3DXVECTOR3 pNormal

)

Or

- Given 3 points
- D3DXPLANE WINAPI D3DXPlaneFromPoints( D

3DXPLANE pOut, CONST D3DXVECTOR3 pV1,

CONST D3DXVECTOR3 pV2, CONST

D3DXVECTOR3 pV3 )

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

If they intersect its easy!

- We just calculate the point using the ray

equation p(t) p0 tu

Projection

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

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)

Picking

- To perform picking, we need to shoot a picking

ray from the camera position that passes through

the point where the mouse clicked

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

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

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

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)

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