Members - Reputation: 270
Posted 14 November 2012 - 02:17 AM
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 = new Vector2(textX*16,textY*16); // 0: top left newTexturePositions = new Vector2((textX+1)*16,textY*16); // 1: top right newTexturePositions = new Vector2(textX*16,(textY+1)*16); // 2: bottom left newTexturePositions = 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 = , textY = 
newTexturePositions = [X:16 Y:16]
newTexturePositions = [X:32 Y:16]
newTexturePositions = [X:16 Y:32]
newTexturePositions = [X:32 Y:32]
... AFTER transformation for textX = , textY = 
newTexturePositions = [X:-0.875 Y:0.875]
newTexturePositions = [X:-0.75 Y:0.875]
newTexturePositions = [X:-0.875 Y:0.75]
newTexturePositions = [X:-0.75 Y:0.75]
However, this does not map as expected.
This is the font bitmap I'm using:
courier_new_10_lmno.bmp 192.05KB 27 downloads
And this is what I get mapped to my sprite...
fontlolz1.bmp 23.82KB 23 downloads
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...
Members - Reputation: 428
Posted 14 November 2012 - 11:37 AM
You can precalculate the coordinates by dividing with texture size e.g.
int texsize=256; newTexturePositions = 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;
Members - Reputation: 726
Posted 14 November 2012 - 04:44 PM
Members - Reputation: 270
Posted 15 November 2012 - 04:50 AM
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).