# How scanline rendering finds an intersection with an object

I'm a newbie with graphics and after I read many articles on the web I still don't understand how in rasterizing from a pixel coordinate like (0;0) on the screen the intersection with an object (let's say a sphere) is found before determining its color and how the Z buffer comes into play.

I know something about ray tracing.. is a ray shot from the 0;0 pixel and the intersection point thus determined by the equality between the ray equation with the sphere equation? If it works that way I don't see what the Z buffer is useful to.

I don't know if I explained myself correctly, if not please let me know and I'll try to be as clear as I can on my doubt.

• What is your actual question? – Trevor Powell May 2 '13 at 4:12

In some ways, rasterisation is the opposite problem to raytracing. In raytracing, you know which pixel you are testing, and you have to find the triangles that are hit by the ray through it. In rasterisation, you have information about a triangle and you need to find which pixels it covers.

Basically, the vertices describing the triangle are projected into screen space through a series of matrix transformations and then interpolation is used to find all the pixels inside the 2D triangle. These are then tested by the depth buffer to see if they are closer than anything else that was written, and if so, then the colour at that pixel can be determined, with lighting, texturing and anything else you want to do.

Here is a good discussion and code regarding software rasterisation. If you are searching for more information, here are a few terms of interest: "scan conversion" "half spaces" and "perspective correct"

Depth Buffer is not as fancy as ray tracing, it's just a simple sort of Z values from objects.

One of many types of information you send to the GPU are postions (x,y,z), after the data is in VRAM (Video Ram) each z value that a pixel intersects is taken in consideration. Something like this:

for every polygon in the polygon list in VRAM
if polygon intersect pixel P
if not closest Z
update Z values in buffer


Z Buffer is useful because it is done at hardware level and it consists of a comparison between a point and a pixel to say so, ray intersection on the other hand in software development is quite an expensive algorithm.