Computer Graphics Camera Projection / Picking CO2409 Week 8 - Optional Advanced Material Not on Exam
Contents 1.World / View Matrices Recap 2.Projection Maths 3.Pixel from World-Space Vertex 4.World Space Ray from Pixel These notes are presented as additional material and are not examinable
Model Space An model’s mesh is defined in its own local coordinate system - model space Each model is positioned with a matrix –Transforming it from model space into world space This matrix is called the World Matrix
World to Camera Space Next consider how the models are positioned and oriented relative to the camera Convert the models from world space into camera space –The scene as viewed from camera’s position This transformation is done with the view matrix
Camera to Viewport Space Finally project the camera space models into 2D The 3D vertices are projected to camera position Assume the viewport is an actual rectangle in the scene Calculate where the rays hit the viewport = 2D geometry This is done with the projection matrix
Projection Details Cameras have two settings: –Field of View (FOV) –Viewport distance (D) Viewport distance is same as the near clipping plane –Where geometry “slices” through the viewport FOV works as a wide angle or zoom lens –FOV can be different for width and height – FOV X & FOV Y
Projecting a Vertex Consider the projection of a single 3D vertex to 2D Want 2D coordinates (X V, Y V ) –Y V not shown in diagram Calculate using similar triangles: X / Z = X V / D, so X V = D * X / Z In a similar way, Y V = D * Y / Z This is the perspective divide Now have 2D coords, but still in camera space units –Need to convert to pixels
Converting to Pixels Calculate the actual viewport dimensions (camera space): tan(FOV X / 2) = (W V / 2) / D so W V = 2 * D * tan(FOV X / 2) similarly, H V = 2 * D * tan(FOV Y / 2) Then calculate: X N = 2 * X V / W V Y N = 2 * Y V / H V This 2D coordinate(X N, Y N ) is in the range –1 to 1 –Ready to convert to pixel position
Converting to Pixels If the viewport width & height (in pixels) are W P and H P : then X P = (X N + 1) W P / 2 and Y P = (1 - Y N ) H P / 2 –The second formula flips the Y axis (viewport Y is down) (X P, Y P ) are the coordinates of the final pixel we want
Picking Sometimes we need to manually perform the projection process: –To find the pixel for a particular 3D point –E.g. To draw text/sprites in same place as a 3D model Or perform the process in reverse: –Each 2D pixel corresponds to a ray in 3D space (refer to the projection diagram) –Finding and working with this ray is called picking –E.g. to find the 3D object under the mouse The algorithms for both follow – they are derived from the previous slides
Pixel from World-Space Vertex Start with world space vertex P Transform this vertex by combined view / projection matrix to give Q If Q.z < 0 then the vertex is behind us, discard Otherwise do perspective divide: Q.x /= Q.z and Q.y /= Q.z Finally, scale to pixel coordinates X,Y: X = (Q.x + 1) * (ViewportWidth / 2) Y = (1 - Q.y) * (ViewportHeight / 2) Use to draw text/sprites in same place as 3D entity
World-Space Ray From Pixel 1 Initial pixel (X,Y), first convert to point Q in the range -1 -> 1: Q.x = (2 * X / ViewportWidth) - 1 Q.y = 1 – (2 * Y / ViewportHeight) Set Q.z = D (viewport / near clip distance) –The result vertex will be exactly on the clip plane Calculate viewport size in camera space: W V = 2 * D * tan(FOV X / 2) H V = 2 * D * tan(FOV Y / 2) If FOV Y not available: H V = W V * ViewportHeight / ViewportWidth
World-Space Ray From Pixel 2 Convert Q into camera space: Q.x *= W V / 2 Q.y *= H V / 2 Finally transform by the inverse of the view matrix to get a point in world space Then cast a ray from camera to this point –Use this 3D ray to detect the entity at the pixel