Jump to content
  • Advertisement
Sign in to follow this  
maxest

OpenGL Rasterization rules considerations

This topic is 2550 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Recently I decided to take a look again at my software renderer and compare what it produces at the pixel level with OpenGL and Direct3D9. And I discovered something I cannot fully grasp in both OGL/D3D9. As far as I know they both use the left-top rule. This is to prevent from drawing some pixels twice when two triangles are adjacent. If an edge of both triangles passes through the center of a pixel, only one of those triangles will count that pixel. The top-left rule, as I understand, simply means to take into account "left" and "top" edges passing through the center of a pixel, and not take into account "bottom" and "right". My research however makes me believe that the rule is in fact "left-bottom".

My experiment is as follows:
1. Create 8x6 screen.
2. Render two triangles forming a rectangle spanning [-0.5, 0.5] x [-0.5, 0.5] area in normalized device coordinates. Colors used for vertices:
left-top - red
left-bottom - green
right-bottom - blue
right-top - white

The result of that is shown in raster1.png image.

raster2.png shows the details. So, we have the rectangle in the very center, occupying 25% of the screen's area. The yellow pixels are the ones that surely gets rendered. Now, accroding to the left-top rule the red pixels should get rasterized, and orange should be rejected, but raster1.png proves exactly the opposite. Is there anything here I am missing?

Share this post


Link to post
Share on other sites
Advertisement
Are those result PNGs from OpenGL or DirectX? And how are you setting up the viewport and transforms?

D3D defines it's rasterization rules with pixel centers on integer coordinates. OpenGL defines pixel centers as being at half-integer coordinates.

Going by the D3D rules, your vertices should be at (2.0, 1.5), (6.0, 1.5), (2.0, 4.5), and (6.0, 4.5). Pixels in your buffer are at (0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0) on X, (0.0, 1.0, 2.0, 3.0, 4.0, 5.0) on Y

Judging from your png, you're using D3D, and your top edge falls between 1.0 and 2.0 on Y, so 1.0 is excluded, 2.0 is included. Your bottom edge is between 4.0 and 5.0, so 4.0 is included. To match what you're actually sending, you should shift your example rectangle 0.5 pixels to the right, and 0.5 pixels down.

Share this post


Link to post
Share on other sites
Either way, your raster2.png is wrong, as mentioned above. It seems like they're not following top-left, but only because your polygon is not actually positioned to test top-left.

Share this post


Link to post
Share on other sites
OpenGL does not specify how corner cases such as center-intersecting edges are handled. It is up to the implementation to decide what to do, as long as the same choice is made every time when give the same edge, and that every pixel along a shared edge with identical endpoints is rasterized exactly once.

Share this post


Link to post
Share on other sites
Hidden
@ReaperSMS: So how am I supposed to position the polygon to test against top-left edge? This rectangle is made of (left-top, left-bottom, right-bottom) and (left-top, right-bottom, right-top) triangles so the second one definitely has the top edge (left-top, right-top).

I made one more test. Now I have an 8x8 screen, and the triangles have cooridnates unchanged. So now the rectangle is a square and the left-top to right-bottom diagonal hits centers of 4 pixels. raster3.png shows the lower triangle, raster4.png shows the upper triangle. It's clear that the top edge of the lower triangle is not rasterized, whereas the bottom edge of the upper one is. Again, OGL and D3D9 behave identically. Maybe OGL does not specify what the rule is, but DX9 specification clearly states that it uses the top-left rule. And I think they even mentioned that this rule is used after OGL.

Share this post


Link to post
@ReaperSMS: So how am I supposed to position the polygon to test against top-left edge? This rectangle is made of (left-top, left-bottom, right-bottom) and (left-top, right-bottom, right-top) triangles so the second one definitely has the top edge (left-top, right-top).

I made one more test. Now I have an 8x8 screen, and the triangles have cooridnates unchanged. So now the rectangle is a square and the left-top to right-bottom diagonal hits centers of 4 pixels. raster3.png shows the lower triangle, raster4.png shows the upper triangle. It's clear that the top edge of the lower triangle is not rasterized, whereas the bottom edge of the upper one is. Again, OGL and D3D9 behave identically. Maybe OGL does not specify what the rule is, but DX9 specification clearly states that it uses the top-left rule. And I think they even mentioned that this rule is used after OGL.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!