Textures in D3D aren't properly mapped

gdp2000

Newcomer
Joined
Sep 5, 2003
Messages
4
Hi.

I'm converting my GDI+ isometric tile engine to managed Direct3D due to performance issues. This is my first programming expirience in Direct3D.

For every tile I use two triangle strips made of 4 vertices. The vertices are of Transformed.Textured Format:



int vertexCounter = 0;
for(int x = 0; x <= 13; x++)
{
for(int y = 0; y <= 13; y++)
{
int positionX = 390 - y * 30 + x * 30;
int positionY = y * 15 + x * 15;
customVertex[vertexCounter++] = new CustomVertex.TransformedTextured(positionX, positionY, 0.5F, 1, 1, -1);
customVertex[vertexCounter++] = new CustomVertex.TransformedTextured(positionX + 60, positionY, 0.5F, 1, 0, -1);
customVertex[vertexCounter++] = new CustomVertex.TransformedTextured(positionX, positionY + 29, 0.5F, 1, 1, 0);
customVertex[vertexCounter++] = new CustomVertex.TransformedTextured(positionX + 60, positionY + 29, 0.5F, 1, 0, 0);
}
}



I load the texture with the TextureLoader.FromFile method and attach a color key to it:



public void CreateOverlayTexture(string file)
{
Texture texture = TextureLoader.FromFile(device, file, 60, 29, 0, 0, 0, Pool.Default, Filter.None, Filter.None, Color.FromArgb(255,0,255).ToArgb());
arrayListTexture.Add(texture);
}



The problem is, that the textures aren't mapped properly as you can see in the attached GIF.

All the tiles should have 60x29 pixels. But they all have a different size.

What do I do wrong?

THX and C ya
gdp2000
 

Attachments

The last two parameters to the CustomVertex.TransformTextured should range from 0 to 1 (0,0 being top left of the surface, 1,1 being bottom right)

Also textures should be a power of 2 for best performance /appearance
 
Hi

Thanks for the fast reply, but this didn't help. I think it has something to do with the coordinates of the vertices. As you can see, I'm using transformed position for the vertices. Every tile which is painted has its own 4 vertices. When the first vertex would be on 0,0 then the second on 60,0, third on 0,29, and fourth on 60,29. When the texture would be mapped properly, every tile should look like in the attached picture...

C ya
gdp2000
 

Attachments

  • test.gif
    test.gif
    1.4 KB · Views: 52
Will try and look into it more over the weekend (no promises).
The Texture shouldn't be affected by the vertices though. Setting the texture's v co-ordinate to -1 will prevent it mapping across the tile correctly i think (0, -1) puts it at the top but one unit to the left of the square.
 
Maybe the problem is, that the textures doesn't have the same x and y dimmension? I mean that they are all 60x29 and not 60x60.

I even tried it with 60x20 and corrected the uv mapping coodinates as you said and it didn't help.
 
I don't know if this is what you should have, but if you already have it then it won't help
C#:
int vertexCounter = 0;
for(int x = 0; x <= 13; x++)
{
for(int y = 0; y <= 13; y++)
{
int positionX = 390 - y * 30 + x * 30;
int positionY = y * 15 + x * 15;
customVertex[vertexCounter++] = new CustomVertex.TransformedTextured(positionX, positionY, 0.5F, 1, 0, 0);
customVertex[vertexCounter++] = new CustomVertex.TransformedTextured(positionX + 60, positionY, 0.5F, 1, 1, 0);
customVertex[vertexCounter++] = new CustomVertex.TransformedTextured(positionX, positionY + 29, 0.5F, 1, 0, 1); 
customVertex[vertexCounter++] = new CustomVertex.TransformedTextured(positionX + 60, positionY + 29, 0.5F, 1, 1, 1);
}
}

public void CreateOverlayTexture(string file)
{
Texture texture = TextureLoader.FromFile(device, file, 64, 32, 0, 0, 0, Pool.Default, Filter.None, Filter.None, Color.FromArgb(255,0,255).ToArgb());
arrayListTexture.Add(texture);
}
 
OK, finally i got it. :D

The vertex distance in transformed co-ordinates between the x vertices must be 63 and the y distance 31.
For example the co-ordinates of one TriangleStrip primitive are:

1. 0,0
2. 63,0
3. 0,31
4. 63,31

This is very interesting, because the image I'm loading with TextureLoader.FromFile has the dimensions of 60x29:

TextureLoader.FromFile(device, TextureFileName, 60, 29, 0, Usage.RenderTarget, 0, Pool.Default, Filter.None, Filter.None, Color.FromArgb(255,0,255).ToArgb());

Can someone please explain this???

I tried every sugestion from AndreRyan and PlausiblyDamp, but only this helped. Is this normal behavior or do I make something wrong?
 
Have you checked the size of the texture after you load it? I've never seen TextureLoad.FromFile load a non-power-of-2 texture properly. Meaning, if I load a 60x29 bitmap, it'll scale it to 128x32 or something similar. You can get the surface description after you load the bitmap to see the size to make sure it's what you passed to FromFile().

Also, I know that all vertices must be offset by 0.5 pixels when drawing. It took me the longest time to find it in the docs because *most* of the 2D sprites would draw fine, but some would be scaled off by 1 pixel.

-Ner
 
Back
Top