Computer Science › Section K · Computer graphics › Topic K.1
The scope of graphics and an overview of 3D
Every frame of a video game and every shot of an animated film is a photograph of a world that exists only as data. This topic maps the field of computer graphics and follows the path from a virtual scene to the pixels on a screen.
What computer graphics covers
Computer graphics is the part of computer science that uses computers to produce and change visual images. Its range is wide: text on a screen, charts, the windows and icons of a graphical user interface, photo editing, film special effects, video games and animated features. Photo editing alone includes removing red-eye and combining parts of several photos into a scene that never happened. Today the phrase often means 3D graphics in particular, the subject of this section. This topic places 3D graphics among its neighbours, then traces how a virtual scene becomes an image.
2D graphics, image processing and 3D graphics
Three related fields are easy to confuse. 2D graphics converts flat shapes such as lines, circles and letters into patterns of pixels: a drawing program filling an oval with colour is doing 2D graphics. Image processing runs the other way. It analyses the pixels of an existing image to find patterns, for example to sharpen a photo or locate faces; the later topic "Perception: vision and language" returns to it. 3D graphics converts descriptions of three-dimensional objects into images. In short, 2D graphics makes images from flat shapes, image processing analyses images, and 3D graphics makes images of virtual 3D worlds. All three work with bit maps, as described in "Representing text, images, and sound as bits".
Photographing a world made of data
3D graphics works like photography with one big difference: the scene has no physical existence. It is only data and algorithms, so a castle can float in the sky. Making an image therefore has two stages. First the scene is designed, built and stored, a creative task much like building a film set. Then an image is produced from it, a computational task that simulates what a camera would record. Games and animated films both rest on this idea. In a game the player interacts with the virtual world; in an animated film the animators do, and viewers receive only the finished sequence of flat images.
Three steps: modelling, rendering, displaying
The whole process has three steps. Modelling builds the scene: the objects' shapes, surfaces and positions, the lights and the camera. Rendering computes the two-dimensional image that camera would see, working out the colour of every pixel. Displaying shows the result on a screen or stores it. The next topics follow this order: "Modeling objects", then "Rendering" and "Global lighting: ray tracing and radiosity", then "Animation", which repeats the process for every frame. Modelling is mostly artistic work done by people; rendering is mostly heavy arithmetic done by machines.
Perspective projection
Treat the camera as a single point, the centre of projection or viewpoint, with a flat projection plane in front of it. Each point of an object is joined to the viewpoint by a straight line called a projector, and the point appears where its projector crosses the plane. This perspective projection matches how eyes and cameras see: distant things look smaller. In a parallel projection the projectors are parallel instead, so sizes do not shrink with distance; engineering drawings use it to show true proportions. Only a rectangle of the plane, the image window, becomes the picture, like the frame in a camera's viewfinder.
The projection formula
Put the viewpoint at the origin, looking along the \(z\)-axis, with the projection plane at distance \(d\), the plane \(z = d\). A point \((x, y, z)\) in front of the camera, with \(z > 0\), lands at
This follows from similar triangles: a projector that rises \(y\) units over a distance \(z\) has risen \(dy/z\) units at distance \(d\). Dividing by \(z\) makes far objects small: double an object's distance and its image halves. Straight segments project to straight segments, so a triangle's image comes from projecting its three corners. The Linear Algebra topic "Matrices in computer graphics" does this with a matrix, placing the eye at (0, 0, d) and the screen at z = 0 instead.
Worked example: projecting points
With the viewpoint at the origin and \(d = 2\), project A(−3, 6, 12), B(1, 0, 5) and C(4, 0, 5). 1. For A: \(x' = 2(-3)/12 = -0.5\) and \(y' = 2(6)/12 = 1\), so A appears at (−0.5, 1). 2. For B: \(x' = 2(1)/5 = 0.4\) and \(y' = 0\), giving (0.4, 0). 3. For C: \(x' = 2(4)/5 = 1.6\), giving (1.6, 0). 4. Segment BC is 3 units long, parallel to the plane at distance 5, so its image is 1.6 − 0.4 = 1.2 units, which equals 3 × 2 ÷ 5. 5. Check: at distance 10 the image would halve to 0.6 units.
From image window to frame buffer
Once rendering knows what lies inside the image window, it computes a colour for each pixel. This is the expensive part, because colour depends on how surfaces interact with light: a shiny surface under a bright lamp looks nothing like soft fabric in shade. It also depends on other objects, which can cast shadows or appear in mirrors. The finished colours are stored as a bit map in memory called the frame buffer: part of main memory, or separate memory beside dedicated graphics hardware. The display reads the frame buffer and lights the screen to match, or the image is saved.
Worked example: a frame buffer and its time budget
A game draws 1920 × 1080 images in 24-bit colour at 60 frames per second. 1. Pixels per frame: 1,920 × 1,080 = 2,073,600. 2. Bytes per frame: 2,073,600 × 3 = 6,220,800 bytes, about 6.2 MB. 3. Bytes produced per second: 6,220,800 × 60 = 373,248,000, about 373 MB. 4. Time per frame: 1/60 of a second, about 16.7 milliseconds, in which the whole scene must be rendered. 5. Check: at 30 frames per second the budget doubles to about 33.3 ms. Systems commonly keep two frame buffers, showing one while drawing the next, so the viewer never sees a half-drawn frame.
Real time versus offline rendering
That budget explains the gap between games and films. A game must render each frame while the player acts, so it gives up some realism for speed. A film studio renders offline, in advance: one frame of a feature film may take minutes or even hours of computing, spread across many machines in a render farm. New techniques usually appear offline first and reach real-time systems as hardware improves. Game consoles and most personal computers contain a graphics processing unit, or GPU, built for the arithmetic of rendering; the topic "Rendering" explains its role.
Common mistakes to avoid
First, confusing image processing, which analyses images, with 2D graphics, which produces them. Second, thinking a 3D scene is a picture: it is data, and any number of pictures can be rendered from it. Third, forgetting to divide by \(z\) in a perspective projection, which would draw far objects as large as near ones, as a parallel projection does. Fourth, confusing the projection plane, which extends without limit, with the image window, the rectangle that becomes the picture. Fifth, leaving out the 3 bytes per pixel of 24-bit colour, or mixing up bits and bytes, when sizing a frame buffer.
Key takeaways
Computer graphics produces and manipulates images. 2D graphics turns flat shapes into pixels, image processing analyses pixels, and 3D graphics photographs virtual worlds made of data, in three steps: modelling, rendering and displaying. A perspective projection lands each point at x′ = dx/z, y′ = dy/z, so distant objects shrink; a parallel projection keeps true sizes. Pixel colours go into the frame buffer. At 60 frames per second each frame has about 16.7 ms, which is why real-time images are less detailed than offline film frames.
Flashcards
Flashcard
Using computers to produce and manipulate visual images
Test your understanding
Question 1 of 5
Which task is image processing rather than 2D or 3D graphics?
Question 2 of 5
The viewpoint is at the origin and the projection plane is z = 4. Where does the point (6, -2, 8) appear on the plane?
Question 3 of 5
A segment parallel to the projection plane has an image 0.6 units long. It is moved to twice its distance from the viewpoint, still parallel to the plane. How long is its image now?
Question 4 of 5
How many bytes does a frame buffer need for one 2560 × 1440 image in 24-bit colour?
Question 5 of 5
Why are the images in an interactive game usually less detailed than the frames of an animated feature film?
