Keenan's Blog

Computer Graphics From Scratch


2-3 Canvas To Viewport

Step 2 of our algorithm asks to Determine which square on the viewport corresponds to this pixel. We already know the canvas coordinates, they are called canvas_x and canvas_y. The viewport is conveniently placed so that it's axis match the orientation of those of the canvas and it's center matches the center of the canvas. The viewport is measure in world units and the canvas is measured in pixels, so to go from canvas coordinates to space coordinates will just be a change of scale.

viewport_x = canvas_x * (viewport_w/canvas_w)

viewport_y = canvas_y * (viewport_h/canvas_h)

Theres one more detail to this though, the viewport is 2D, but it's embedded in 3D space. We have already defined the distance d from the camera, so every point on this plane, called the projection plane has z = d so,

viewport_z = d

For each pixel (canvas_x, canvas_y) on the canavas we can determine its corresponding point on the viewport (viewport_x, viewport_y, viewport_z).

<- 2-2 Basic Assumptions 2-4 Tracing Rays->