I am a high school student taking computer science as a subject, and someone who is very interested in the game development industry. I am currently writing an essay on Raytracing and Z-buffering by comparing them and deducing the right situation for each of them. I was wondering if the developers could help me answer a question of in what specific situations those two are used specifically. I already know how both of them work. Also, do developers prefer another method than these two or is there a way to combine both to make an even more efficient 3D graphics algorithm? Thanks a lot!
"Z-buffers" and "raytracing" are not mutually exclusive. Z-buffers are frequently used to support triangle-based, back-projected rendering (though they are optional in actual usage), and they can also be used to support raytracing. Equating z-buffers to traditional GPU polygon-based, back-projected rendering, just because these are often found together, is outright erroneous.
So, let's talk about Ray-tracing vs. the traditional GPU rendering pipeline, which includes Rasterization (see other wayward answers here) as one of its steps.
Practical pros of traditional triangle-based rendering
- Can use familiar pipelines and traditional, mature tools like 3DS Max, Maya, Blender to produce 3D content;
- There are countless resources available for it
- Processing is very fast on current hardware
- Doesn't do realistic lighting as well raytracing since the z-buffer it relies on is a boolean concept - you've either drawn at a nearer depth or you haven't, so composing individual pixels according to different factors at that screen point is difficult;
- It will not require arcane / cutting edges technologies to implement (speaking relatively, of course)
Practical pros of raytracing
- It can be an extremely accurate way of reproducing real-world lighting conditions
- It is conceptually quite a lot easier to understand and implement than the traditional approach
- Since hardware doesn't currently support it specifically (EDIT: This is no longer or only partly true, with the advent of NVidia RTX), you get to write your own implementation which means you understand everything that is going on i.e. how each pixel is composed (also a con, but a lot of fun and direct control over end appearance).
If you want to produce a game quickly, use the former. If you are cutting edge, want a highly individualised art style and have the time to build and endlessly tweak your graphics engine yourself, use the latter. Presently, since time is always of the essence in industry, most prefer the former.
Daniel beat me to it but I'm gonna give my own answer anyway since I had fun writing this. His bullet points are solid but I wanted to expand a bit more.
I'm going to use the term Rasterization rather than Z-buffering.
Raytracing is often used for "offline" rendering. Raytracing algorithms give very good pixel quality at the cost of high execution time. Because Raytracing algorithms simulate a light model very similar to the real world, phenomenon such as shadows, reflection and refraction are all very intuitive and simple operations. However since it takes a long time to calculate how a ray of light bounces and interacts with a scene for every pixel (1920x1080 pixels is a lot!) it's not practical to use Raytracing for real-time applications on the CPU. However because the pixel quality is high, "offline" or non-realtime applications use Raytracing extensively. RenderMan by Disney/Pixar which is a good example of an application that uses Raytracing for Film and Animation.
Most GPUs are designed to rasterize triangles to the screen, not to raytrace a scene. PowerVR by Imagination is a series of GPUs that perform hardware Raytracing but that's the exception rather than the rule (and they just lost a lot of market share since Apple is no longer using their GPUs).
I highly suggest you read up on modern GPU pipelines. Along with some reading it's a good idea to try to visualize the pipleine. A fun way to do this for yourself is to take a capture from your favorite game with something like RenderDoc and walk through the different pipeline stages and see what they do.
In recent years there have been two major developments that you may want to read up on. Pathtracing, as an alternative approach to raytracing has been a really popular area of research. Also as Daniel mentioned we've been seeing more and more short-raytracing algorithms being used in compute shaders on the GPU. Volumetric Fog in CryEngine and Lumberyard is generated by Compute shaders on the GPU that execute very short 1-bounce rays. The rays are executed in parallel on the GPU so the operation only takes a couple milliseconds but the results are pretty good.
If you're curious about modern game rendering techniques I suggest reading the articles here. These are a bit dense and very technical but they should give you an idea of the sort of crazy things people have to come up with to get rasterization on modern GPUs to produce realistic visuals.
To put it simply:
- Pro: Fast, it's what modern graphics cards tend to be designed around.
- Pro: It's easy to implement.
- Con: Getting realistic results isn't necessarily easy.
- Ray Tracing
- Pro: Super easy to implement.
- Pro: Lighting just works. Things like reflection and refraction are pretty much just going to happen.
- Pro: Can get realistic results very easily.
- Con: Can be extremely slow, especially with regards to multiple reflections on the rays. And by slow, I mean potentially multiple seconds, minutes or hours for a single frame.
- Con: Most PC hardware isn't designed to speed this up much. Hardware that does can be very expensive.
Z-buffering is the domain of games and other home computer stuff, mainly because it's so fast to do. Ray-tracing, on the other hand, is more for 3D artists and people in the CG industry as they can afford to wait minutes or hours for a single frame.
For your second question, recent progress in graphics, particularly with deferred rendering, has enabled graphics developers the ability to use some ray tracing techniques in a Z-buffered system for effects like screen-space reflection and shadowing. These are often used with deferred rendering, rather than forward rendering, as shader writers have a lot more detail to perform the ray tracing (Technically ray marching) with. That doesn't mean it can't be done in forward rendering methods, but it tends to be a bit costlier.