Jump to content
  • Advertisement
Sign in to follow this  
RobMaddison

Font rendering leads to misaligned pixels

This topic is 2111 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

Hi

 

I'm just revisiting my very simple font shader and I noticed something odd.

 

For my text, I draw a line of text in one go, e.g. 128 x 16 quad.  I pass the indexes of the letters to the shaders in an array and I gauge where the current pixel is in the text line, pick out the correct letter then calculate that letter's uv coordinates in my font texture (256 x 128).

 

It had been working ok but I'd noticed some strange aliasing artifacts in my font characters, so I went back to basics and to find the 'x' texture coordinate for the current letter that I'm rendering the pixel for, I do this in my pixel shader:

 

float xPosOfCurrentCharacter = ((In.texC.x * lineWidth) % 16.0f) / 16.0f;

 

Where In.texC.x is the texture coordinate coming from the vertex shader (in this example, it's a standard 2-tri quad with texture coordinates at 0-1 as per normal, of size 128 x 16) and lineWidth is 128.  I use the modulo at 16 to get the x position of the current character.

 

Just for testing, I decided to quickly return a float4 of this value to see what it looked like (I was expecting a line of white - black gradients at each set of 16 pixels).  What I got was almost that, but after each block of 16, the gradient seems to shift a bit, some gradients are 16 pixels but some are 15.

 

Is there something I should be doing with regard to precision here?  Is there a better way of working out the texture coordinates of my font lookup texture? 

 

I've attached an image.  The black staggered lines above are all 16 pixels in length so you can see the areas which are out of sync as it were...

Edited by RobMaddison

Share this post


Link to post
Share on other sites
Advertisement

Unrelated to your problem (maybe), the top and left edges seem to be translucent - what's up with that?

Do you account for D3D9's half pixel offset by shifting either your vertex positions or UVs? How are the vertex positions generated?

 

The values of all your gradients are different:

...-245 / 6-251 / 12-241 / 2-247 / 8-253 / 14-243 / 4-249 / 10-255 / 0-245 / 6-251 / 12-...

Also, the gradient that ends with 255 and the one that starts with 0 both share a vertical column... which makes me think something is wrong with the vertex positions.

 

As well as something being wrong with the vertex positions, this error is being preserved because floating point mod/fmod/% returns a floating point result.

To get an exact tex-coord (centre of texel) in a 16px wide texture, you probably want:

float u = (floor((In.texC.x * lineWidth) % 16.0f)+0.5) / 16.0f;

Share this post


Link to post
Share on other sites

Unrelated to your problem (maybe), the top and left edges seem to be translucent - what's up with that?

Do you account for D3D9's half pixel offset by shifting either your vertex positions or UVs? How are the vertex positions generated?

 

The values of all your gradients are different:

...-245 / 6-251 / 12-241 / 2-247 / 8-253 / 14-243 / 4-249 / 10-255 / 0-245 / 6-251 / 12-...

Also, the gradient that ends with 255 and the one that starts with 0 both share a vertical column... which makes me think something is wrong with the vertex positions.

 

As well as something being wrong with the vertex positions, this error is being preserved because floating point mod/fmod/% returns a floating point result.

To get an exact tex-coord (centre of texel) in a 16px wide texture, you probably want:

float u = (floor((In.texC.x * lineWidth) % 16.0f)+0.5) / 16.0f;

 

I had noticed that translucency too, quite strange as I'm not using any alpha for this.

 

I'm not catering for the shift texel in this instance (I do for my post processing), I could try that.  I'll give that floor version a go.

Thanks Hodgman

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!