D Basics

So what do those x, y, and z values in a 3D vector actually refer to anyway? When you're doing 2D graphics work, it's common to use a 2D coordinate system that's the same as the screen: the origin in the upper-left corner, x increasing from left to right, and y increasing from top to bottom. In 3D graphics, the three axes won't map directly to any device coordinates, so the goal is to "translate" the 3D world onto the 2D coordinate system of the screen. In this tutorial, we use the 3D coordinate system common in math textbooks, called the "right-handed" coordinate system, where the x-axis points "right," the y-axis points "up," and the z-axis points "back" (away from the viewer). This is also the system used by OpenGL. The right-handed coordinate system is shown in Screenshot.

Screenshot The right-handed coordinate system is common in 3D graphics and is the coordinate system we use in this tutorial.

Java graphics 07fig03.gif

If, on your right hand, you point your index finder in the direction of the y-axis and your thumb in the direction of the x-axis, your middle finger points in the direction of the z-axis. The same thing works for your left hand and the left-handed coordinate system, shown in Screenshot.

Screenshot The left-handed coordinate system is an alternative way of viewing the 3D world.

Java graphics 07fig04.gif

Other 3D coordinate system variations include a rotation of either the left-handed or right-handed coordinate systems so that the z-axis points "up." Here, however, you'll work with only the right-handed system in Screenshot. Next, you'll learn about the concept of the camera and the view window. The camera is where the view is located relative to everything else in the world. The view window is a window in 3D space that is the same size of the 3D window onscreen. In games, the view window is typically the same dimension of the screen. Locate the camera at the origin, (0,0,0) and the view window down the opposite direction of the z-axis, as shown in Screenshot. This makes the z-axis decrease with depth, which is a common way to represent the view window.

Screenshot The camera, located at (0,0,0), looks toward the center of the view window in the opposite direction of the z-axis. The view window is commonly the entire screen.

Java graphics 07fig05.gif

It may sound crazy to keep the camera at (0,0,0), looking straight down the z-axis. Don't you want to move the camera and change the direction the camera is facing? When you turn 90° right, wouldn't you then be looking down the x-axis? The answer is "sorta." Keeping the camera looking down the z-axis and fixed at (0,0,0) actually simplifies many calculations and is the common way to represent 3D graphics. We'll come up with a way to simulate camera movement later in the chapter, but for now, we focus on drawing a 3D world with a fixed camera. Note that everything that is visible to the camera is inside what's called the view frustum, as shown in Screenshot. The view frustum shape is usually a four-sided pyramid, sometimes with part of its tip cut off by a front-clip plane. Only what's inside the frustum needs to be drawn.

Screenshot Everything that is visible is inside the view frustum.

Java graphics 07fig06.gif

Take a step back and look at want you want to do. You have a 3D world, and you want to display the objects inside the view frustum as a 2D image, with farther objects appearing smaller than closer ones. To actually draw something, you'll project the 3D world onto the view window, as shown in Screenshot.

Screenshot Objects in the 3D world are projected onto the view window. Thus, farther objects appear smaller.

Java graphics 07fig07.gif

When projecting a point in 3D to the view window, you can imagine a line from the 3D point being drawn to the camera (like the dotted lines in Screenshot). The intersection of this line with the view window is where the projected point lies. Because you treat the camera as a point, objects farther from the camera appear smaller than closer ones. This brings up to the next topic, which actually gets the math to project a point onto the view window.