1px Bordered Quad

Started by
1 comment, last by dr4cula 9 years, 9 months ago

Hello,

I'm having a bit of trouble rendering a quad with exactly 1px border but can't figure out for the life of me how to do it. I'm using basic vertex and pixel shaders where the position I pass in is in clip space. I thought this would do the trick:


float dx = 1.0f / static_cast<float>(width_);
float dy = 1.0f / static_cast<float>(height_);
 
D3DXVECTOR2 coords[6] = {D3DXVECTOR2(-1.0f + dx, -1.0f + dy), D3DXVECTOR2(-1.0f + dx, 1.0f - dy), D3DXVECTOR2(1.0f - dx, 1.0f - dy),
D3DXVECTOR2(-1.0f + dx, -1.0f + dy), D3DXVECTOR2(1.0f - dx, 1.0f - dy), D3DXVECTOR2(1.0f - dx, -1.0f + dy)};

When I use 2.0f * dx or 2.0f * dy I get a 2px border everywhere. Which I guess means my method should be correct? I'm really confused...

Thanks in advance!

Advertisement

Probably the rasterization rules that only draw pixels where the center is inside it (or perhaps the edges, check the rasterization rules for your DX version). Try offsetting all coords a half pixel (dx and dy are half pixel offsets). As of now you begin your line on a pixel edge, and include half a pixel in each direction, so you include two halves of two different adjacent pixels, instead of including one full pixel.

When you do 2.0f / width then you include one full pixel in each direction, which includes the pixel centers of the adjacent pixels in both directions.

Probably the rasterization rules that only draw pixels where the center is inside it (or perhaps the edges, check the rasterization rules for your DX version). Try offsetting all coords a half pixel (dx and dy are half pixel offsets). As of now you begin your line on a pixel edge, and include half a pixel in each direction, so you include two halves of two different adjacent pixels, instead of including one full pixel.

When you do 2.0f / width then you include one full pixel in each direction, which includes the pixel centers of the adjacent pixels in both directions.

Yeah, I figured that it had something to do with the triangle rasterization rules but didn't really know how to fix the issue. Shifting all coordinates by an extra 1.0f / (dimLength * 2.0f) solved the issue. Thanks!

This topic is closed to new replies.

Advertisement