Drawing a triangle in 3D to a depth buffer

2k views Asked by At

I am experimenting with 3D graphics without relying on any 3D library, such as Java3D, OpenGL, DirectX, etc. I have a working Z-buffer (aka. "depth buffer"), yet I cannot think of a way of drawing a triangle to that buffer. (Every triangle is specified by three points in 3D.)

Can anyone please provide pseudocode for drawing a triangle to the Z-buffer?

3

There are 3 answers

0
Alex On BEST ANSWER

The answer to your question (as is) involves complex algorithms and a complete answer would require many chapters. I'll only provide key words and links.

First, you need to project the three 3D coordinates (usually called vertex, vertices for the plural form) of your triangle in 2D space. You can start with this article about 3D projection. If you're not familiar with vector operations, matrix transformations, world space and view/camera space, be warned that it may be a long run before you get results. Orthogonal or perspective projection, it's your choice (though I suppose you wish for perspective projection). I only provided a link to the wikipedia article (the less likely to break in the future), thus it mainly talks about involved maths behind it. But with those key words, you should be able to find thousands of good articles about the subject with source code.

Once you transformed (projected) your 3D coordinates in the view/camera space, your X and Y coordinates are the 2D triangle corners/vertices coordinates (let's assume you decided that X goes from left to right, Y goes from top to bottom, this is quite common). You then need to rasterize this triangle into your buffer (which is basically a 2D bitmap). Here's a triangle rasterization article which will teach you (with C-like source code) popular techniques to do this (though this is only about rasterization with a constant color, the principle remains the same).

The next step is that instead of writting a constant color for all pixels, you need a variable depth value. Time to make use of those 3 Z values in your projected vertices. For each horizontal line, you'll have to find the Z depth of the left-most & right-most pixels (using linear interpolation from top vertex Z to bottom vertex Z, just be careful as one side will likely have a top, middle and bottom vertices, so here you must interpolate from top to middle, then from middle to bottom. If you divided your triangle in two like described in the article, no need to bother with it). Then write all pixels between them, also linear interpolating your left Z and right Z.

0
Alex Taylor On

Are you able to rasterize something to the z-buffer already? The question is how to rasterize a triangle? In Chris Hecker's technical articles, he has one dedicated to rasterization. It's part of a series on perspective texture mapping that everyone referred to back when people had to write their own. They're incredibly thorough, but may seem a daunting read at first.

0
selkathguy On

If you are looking to render something in 3D space in general, try removing the term "depth buffer" from your searches. The depth buffer is a specific buffer that doesn't necessarily have to be used in order to draw basic objects in 3D space (but will be useful for drawing objects which interact with one another).

In any case, it is a good idea to get familiar with the overall architecture of the systems you are using. For example DirectX has documentation and a summary for its pipeline. Usually, interaction with a depth buffer is handled automatically based on a flag(s) you set in calls to the graphics library or wrapper. There are reasons to write exclusively to the depth buffer, but those are more advanced rendering techniques and usually for a specific purpose.