Ever wondered how Transformers makes such sleek amazing robots that look completely real? How Harry Potter puts the magic onscreen? How Pixar makes such adorable characters come to life?
Well, wonder no more! In this series, I'm going to explain how computers create the art that our talented artists direct them to. In this part one, I'm explaining the very basics, how everything is a triangle, and how those triangles get turned into an image. By the end of series, you should be able to have a knowledgeable conversation with any CG artist, and understand how computers are used to create the movies you love!
Everything is a Triangle
That face? Made of triangles. That car? Made of triangles. Dolphin? Triangles. Landscape? Triangles. Gun? Triangles. In computer graphics, everything is made out of triangles. The reason they can make such intricate shapes is because there's lots of triangles. It's not uncommon for an object to be made out of millions of triangles. I've done it myself. Intricate scenes in AAA movies might be billions of triangles. Often triangles are even smaller than a single pixel.*
Why triangles though? Why not squares or circles or balls? Because triangles are the most concise, precisely defined, least ambiguous primitive possible for describing a surface in 3D space. You can fit them together edge to edge with no discontinuities or holes. They're simple to draw, but expressive enough to create any shape.
Triangles in 3D graphics are defined as an ordered list of 3 points. A point is an ordered list of 3 numbers, the x position, the y position, and the z position. The order of the points determines which side of the triangle is the "out" side of the surface, and which side is the "in" side. This is often referred to as the normal** of the surface.
Computers Need a Camera
Without a camera, the computer has no idea what to draw. Sure, it knows the shape of the object it's drawing, but it can't begin the process until it knows what angle it's drawing it from. You do't want to spend 5 hours painstakingly creating a piece of art, and then have the computer draw it from an angle that doesn't even include your piece!
The camera in computer graphics might just be a virtual object, but it's usually got many of the attributes a real photographer has to worry about, such as focal length. Focal length changes perspective transformation that the camera does. It controls the amount of foreshortening. Close objects should appear bigger than far ones, right? Foreshortening is how much bigger they appear.
In computer graphics, you also get to play with a special type of camera that doesn't exist in real life - the orthographic camera. With this camera, there is no perspective distortion. Everything is at the same scale. It's kind of like looking down at a city from a satellite camera.
The Two Camps of Rendering
There are two main ways of turning your camera and triangles into an image, rasterization and raytracing. Neither is better than the other; they both have different use cases and pros and cons.
The first technique is rasterization. This is used in games, and other applications that are drawn in real time on your computer. The crux of this technique is the perspective transformation matrix. I won't go into detail, but it basically takes everything in 3D and then does some math, and everything comes out in 2D. But you've still got 2D triangles, so you've go to walk through all the pixels on the triangle and put them on the output image. Be careful to draw the ones closer to the camera on top! (with a depth buffer) This is a very fast process that computers can do in parallel easily, so it's used in all kinds of games.
Production Quality Raytracing
The other technique is raytracing. Basically, the computer shoots out a bunch of lines out of the camera at each pixel, figures out what they hit, simulates them bouncing around the scene, then uses that to figure out what color the pixel is. In terms of lighting, this technique can make the final result much more realistic and detailed. It also allows for a more true-to-life material design. This is a very physics based technique, approximating the behavior of the very real photons that bounce around the real world allowing us to see. But it takes a lot of time for this to resolve into a clear image, so this is generally restricted to things like movies and video clips.
I hoped you liked part one of my overview! If you have any questions feel free to ask, I know a lot about computer graphics. Part two will touch on textures and lighting. Part three will probably about animation.
*Truthfully, you can't arbitrarily compare a triangle's size to a pixel's size. I am talking about when the camera is positioned in such a way that triangles projected onto the image have less area on the image plane than a pixel.