简体   繁体   中英

Optimize OpenGL 2D rendering by using depth buffer to discard overlapping pixels?

Is it possible to take advantage of the depth buffer in a way such that it would only draw on those areas where are no pixels drawn yet?

I am rendering simple 1 colored triangles: a lot of them may overlap, which will reduce rendering speed significantly, because it is rendering more pixels than what is visible on the screen.

This is easily possible in 3D render mode: just enable depth testing and set the triangles on different z-positions. But that does not work on 2d mode: i cant set every triangle on higher position than the previous, since that would result in bad rendering quality after certain height when the depth buffer limits come on the way.

How can I do this with shaders? Or if no shaders needed; how to do it without shaders?

为每个三角形分配一个多边形偏移量(通过glPolygonOffset),并启用深度测试。

i cant set every triangle on higher position than the previous, since that would result in bad rendering quality after certain height when the depth buffer limits come on the way.

That would only happen if you do it wrong.

A 24-bit depth buffer offers 16 million different depth values for you to choose from. It's simply a matter of computing a value properly. Granted, the exact mechanics are hardware-specific, but no so specific that you would be unable to get at least 4 million separate layers.

It's a matter of simple math. You're building a function that maps from the integer range [0, N] to the floating-point range [0, 1], where N is the number of triangles. Say, 4 million just to give you room.

Thus, the Z-value for any pariticular triangle is k/N , where k is the integer index of that triangle. You should easily be able to do this in your shader.

Worst comes to worst, you can make a 32-bit floating-point depth buffer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM