How does OpenGL render its triangles?

天大地大妈咪最大 提交于 2019-12-13 00:57:45

问题


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).

  • How does OpenGL do it? Is it so intensive job that I should just forget about it and use the (buggy) scanline method instead?

  • Where can I get the algorithm?

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


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/11703412/how-does-opengl-render-its-triangles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!