Types of 3D Rendering
Two popular types of 3D rendering exist: ray tracing and polygon modeling. Ray tracing is a bit like the real world, in that it models rays of light, only in reverse. Instead of modeling light rays from light sources to the eye, it does the opposite, modeling rays from the eye to the world. As you can imagine, modeling a ray of light for every pixel on the screen is computationally expensive. Although ray tracing is not common in real-time 3D graphics, it can give rather realistic results, and it was used in the movie Ice Age. The 3D rendering technique common in real-time games and most movies with computer-generated imagery is polygon modeling. With polygon modeling, the virtual 3D world is interpreted as flat polygons. A polygon is a flat, closed shape with three or more sides (such as triangles, rectangles, and octagons). Using polygons speeds things up, but rounded objects such as spheres don't translate to a polygon model very well (unless you use enough polygons to make them appear round). Polygon modeling is less computationally expensive than ray tracing and can be sped up by a large magnitude using a 3D accelerator card. It is usually in the best interest of your game to take advantage of 3D accelerator cards because they are found on nearly every computer today. In Java, there two common ways to take advantage of a 3D accelerator card: either with Java3D or with an OpenGL binding. Java3D is a high-level API that handles things such as hidden surface removal, simple collision detection, and scene management. Programmers using Java3D can get up and running quickly without having to know a lot of 3D graphics coding techniques. Although Java3D is mostly written in Java, its core uses either OpenGL or DirectX to render 3D graphics. Java3D is available for Windows and many flavors of UNIX but is not included in the Java 1.4.1 SDK. Also, because of licensing, Java3D is not available for all systems. Currently, Java3D is not available for Mac OS X. OpenGL bindings, on the other hand, are usually free to use in commercial products and are available for a wider range of hardware, including Mac OS X. OpenGL is a low-level API, so it requires a more thorough knowledge of 3D graphics coding to take advantage of. The OpenGL bindings simply allow your Java code to access OpenGL functions. A couple of popular OpenGL bindings are GL4Java (www.jausoft.com/gl4java.html) and LWJGL (java-game-lib.sf.net). At this writing, GL4Java is available on more systems and enables you to also use the AWT and Swing at the same time; it hasn't been updated in more than a year, though, so the latest OpenGL features might not be available. LWJGL, or Light-Weight Java Game Library, uses NIO native buffers to interact with OpenGL, so it's a bit faster; it currently is being regularly maintained and updated, but it is not compatible with AWT or Swing. In this chapter and the next, "Texture Mapping and Lighting," you'll create a lightweight, software-based 3D polygon renderer. A software-based renderer won't be as fast as a hardware-based renderer, so it might not be an ideal solution for large-scale games. But in the process of creating a software-based 3D renderer, you'll have a better understanding of how hardware-based renderers work, and you'll learn a lot of 3D graphics techniques that can be applied to a hardware-based solution. Also, a software-based renderer is a great solution if you want to make a small downloadable game without requiring the user to download or install Java3D or an OpenGL binding.