Rookie
Rookie

Reputation: 4134

How does OpenGL render its triangles?

I need an algorithm to render pixel perfect triangles without using OpenGL.

Where can I get such an algorithm like OpenGL is using? Preferrably single threaded.

It must do it like OpenGL does it, because OpenGL doesn't leave gaps or overlapped pixels on two triangles that share an edge. Scanline method makes those errors: http://joshbeam.com/articles/triangle_rasterization/ . I tried this half-space algo too: http://devmaster.net/forums/topic/1145-advanced-rasterization/ but I couldnt make it work (renders gibberish).

I would prefer a C++ solution but the language isn't the problem.

Upvotes: 3

Views: 3354

Answers (3)

jb-ddg
jb-ddg

Reputation: 46

OpenGL uses a scanline based method, as Alan and your article link mentions. However, managing gaps is not a matter of math accuracy. You need an additional convention for the pixels on the edges of the triangle. The convention OpenGL uses is the same as the convention used by Direct3D. It's called "top-left" and the best explanation I've seen is on this MSDN rasterization rules article.

Upvotes: 3

Alan
Alan

Reputation: 4935

A typical OpenGL implementation will use the scanline/rasterization method more or less the same as your first link. The key to avoiding the gaps and overlaps is to be very careful with your math to avoid uncertainty in rounding errors.

If you think of pixels as squares of the screen with some finite area, then you can get overlaps because adjoining triangles will often both overlap the pixel. But instead if you think of the pixel as the infinitely small point in the center of the square, then you can can guarantee no gaps or overlaps.

You also need the arithmetic-certainty of fixed-point integer math instead of the rounding errors of floating point math. The left end of each rasterized scanline starts at first pixel greater or equal to the starting scan value. The right end of the rasterized scanline ends on the one before the first pixel strictly less than the end scan value.

Upvotes: 2

Oskar
Oskar

Reputation: 1331

Take a look at OpenMesa, an open-source implementation of the OpenGL specification. It currently supports up to OpenGL 3.0.

Upvotes: 2

Related Questions