\$50

### Image of the Day Submit

IOTD | Top Screenshots

## Trouble texture mapping from font images (SlimDX, DX11)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1CdrTomalak  Members

Posted 14 November 2012 - 02:17 AM

I have been experimenting with writing text to screen using font sheets with corresponding config files - i.e. just changing my Tu,Tv values for each vector of a quad to switch to different characters.

I have been using the popular LMNOpc font builder: http://games.ucla.ed...ont-builder-pc/

This generates a 256x256 bitmap, with 16x16 pixel tiles, each with a font character inside.

I can easily calculate the pixel coordinates for each tile, using:

[source lang="csharp"] newTexturePositions[0] = new Vector2(textX*16,textY*16); // 0: top left newTexturePositions[1] = new Vector2((textX+1)*16,textY*16); // 1: top right newTexturePositions[2] = new Vector2(textX*16,(textY+1)*16); // 2: bottom left newTexturePositions[3] = new Vector2((textX+1)*16,(textY+1)*16); // 3: bottom right[/source]
But of course I need the cartesian coordinates - i.e. values between 0 and 1 for texture coordinates (I presumed).

So I thought I'd transform the vectors using an orthogonal matrix (LH - which I used for all sprite rendering to good effect thus far), set to a 256x256 pixel size.

This gives me the following transformed vectors in the case of the tile at (1,1):

... before transformation for textX = [1], textY = [1]
newTexturePositions[0] = [X:16 Y:16]
newTexturePositions[1] = [X:32 Y:16]
newTexturePositions[2] = [X:16 Y:32]
newTexturePositions[3] = [X:32 Y:32]
... AFTER transformation for textX = [1], textY = [1]
newTexturePositions[0] = [X:-0.875 Y:0.875]
newTexturePositions[1] = [X:-0.75 Y:0.875]
newTexturePositions[2] = [X:-0.875 Y:0.75]
newTexturePositions[3] = [X:-0.75 Y:0.75]

However, this does not map as expected.

This is the font bitmap I'm using:

And this is what I get mapped to my sprite...

Two things are very wrong here. Firstly it's not a single tile being mapped - I'm getting four. Secondly it's not even in the right area of the bitmap.

So the question is, how do I generate the right texture coordinates? I'm missing a trick quite desperately here. I thought it would be quite simple...

### #2Auskennfuchs  Members

Posted 14 November 2012 - 11:37 AM

How do you perform your transformation of those texture coordinates? Your transformed coordinates look like you multiply them with the orthogonal view or something.
You can precalculate the coordinates by dividing with texture size e.g.

int texsize=256;
newTexturePositions[0] = new Vector2(textX*16/texsize,textY*16/texsize); // 0: top left


or you can calculate it in pixelshader by providing texturesize via constantbuffer. Calculation is the same:

psInput.u /= constantBuffer.TexSize;
psInput.v /= constantBuffer.TexSize;


### #3Postie  Members

Posted 14 November 2012 - 04:44 PM

Yeah, the after transformation positions aren't right at all. I'm not sure how multiplying by the orthogonal matrix to get the texels has worked for you in the past. I recommend using the technique described by Auskennfuchs. Just be aware that if in future you use a non-square texture, you'll have to use width and height as divisors, not size.
Currently working on an open world survival RPG - For info check out my Development blog:

### #4CdrTomalak  Members

Posted 15 November 2012 - 04:50 AM

Auskennfuchs, Postie - thanks for the responses.

The following morning, whilst reading up on the internet of ways to calculate coordinate textures, I realised how stupid I'd been.

In fact, I was reminded of that episode of The Simpspons (when it was funny, in the early days) when Moe says to Homer, in a dry, sharp tone "You're an idiot.".

Whilst screen cartesian coordinates range from -1 to 1 on both axes, texture coordinates range from 0 to 1. Passing through a matrix to calculate the values was never going to work. Deary me.

Instead I simply calculate a factor to multiply the texture positions by. In my case: factor = 1 / 256

I then multiply the pixel coordinates by this factor to give my texture coordinates.... and this works just fine, and in fact is essentially the same method you have both advocated.

This is what happens when you work all day and code in the evening. The answer was obvious, and I won't be making that mistake again (he says).

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.