Jump to content
  • Advertisement
Sign in to follow this  
Aardvajk

Rotating quads - problem with texture co-ordinates on texture atlas

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

I've just been experimenting with rotating some textured quads in Direct3D 9. I seem to have a problem. I'm using a texture atlas system whereby loads of different images are tiled on a texture and their location specified in the quads texture co-ordinates. This is all correct, offset by 0.5, all that jazz and works fine without rotation. I've now tried rotating the corners of the quad (haven't touched the texture co-ordinates). The rotation is fine, but it appears that the edges of the quad are taking pixels from the wrong parts of the texture. I guess this makes sense, since I'm effectively drawing a jagged line down the side of the source rectangle on the texture, but it is annoying. I've solved it for roughly square quads by seperating the sprites on the atlas texture by a border of two pixels with zero alpha, but that is a hack and it still doesn't work for long rectangular quads. Is there any way around this without abandoning texture atlases? I'm thinking it through in my head and I can't really see a solution. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
I believe this is your problem:
http://www.gamedev.net/community/forums/topic.asp?topic_id=432539

Clamping tu and tv is definitely the way to go when working with textured quads.

Share this post


Link to post
Share on other sites
Do you get artifacts only on one side of the quad? If so, your 0.5 factor might be a bit off. I've heard of several different values for this, usually a bit lower than 0.5. Try playing with the value until you manage to avoid artifacts on both sides, if that's possible.

Hope this helps.

Share this post


Link to post
Share on other sites
Quote:
Original post by polaris2013
I believe this is your problem:
http://www.gamedev.net/community/forums/topic.asp?topic_id=432539

Clamping tu and tv is definitely the way to go when working with textured quads.


You can't clamp texture coordinates that don't "leave" the atlas AFAIK. The problem here is sampling the atlas. I don't really understand why you are experiencing color bleeding artefacts, since these should only occur at high mipmap levels. In a 2D game, mipmapping shouldn't occur a lot because of the rougly one-to-one mapping between pixels and texels. Try turning mipmap sampling off anyway. The offets generally don't apply because of the rotation (their semantics change).

A clear-cut border is probably the best solution. Yet some tools arrange tiles in an atlas in such a way that adjacent tiles have similar colors at their borders (source: nVidia).

Share this post


Link to post
Share on other sites
Right, well I agree with Todo that clamping is not the answer.

Now this is weird - when my quads are upright, I need to add 0.5f to the texture co-ordinates to get the right results.

But if I move around the quad co-ordinates so the quad is upside down (manually, not with rotation maths for now), I need to subtract 0.5f from the texture co-ordinates to get the right result.

If I don't do the above, the texture sampling goes off by one pixel to the top and right of the quad.

What is that all about? Surely I don't have to recalculate the texture co-ordinates based on the angle I've rotated the quad?

Help!

@Todo - I'm generating my own texture atlas so the border is no problem, but the texture co-ords are still going off my one so I'm losing a line of pixels down the left and along the bottom of the quad.

Share this post


Link to post
Share on other sites
Although you should be careful when linearly (or higher-order) sampling a texture atlas, as I mentioned above, changing the texture coordinates won't generally make a difference (or at least not a stable one). What should be offset are the vertex coordinates. Recommendations generally range from 0.325 (@ OpenGL tips) to 0.5 (most texts). The problem is the linear lerping of texture coordinates (amongst other vertex attributes) when vertices don't lie on pixel centers.

Share this post


Link to post
Share on other sites
Still completely stumped.

I've tried changing the amounts I offset the texture co-ordinates, adding and subtracting various portions of 1 to the vertex co-ordinates, everything I can think of and still...

When I rotate a quad, my texture co-ordinates gradually go off by one. The problem gets worse as I approach 180 degrees, the slowly better as I come back up the other side of the circle back to zero.

This happens the same whether I actually rotate the quad corners, or if I just switch around the order that I pass the co-ordinates into the vertex buffer, which would simulate a "perfect" 90, 180 or 270 degree rotation.

When my quads are rendered without rotation, I need to add 0.5 to the texture co-ords to get it to render properly.

When my quads are rendered at 180 degrees (or PI radians) I need to subtract 0.5 from the texture co-ords to get it to render properly.

I am unable to work out what I should add or subtract at the angles between. But surely I shouldn't have to modify the texture co-ords when I rotate the quad?

Totally stuck on this one. Any help appreciated.

Share this post


Link to post
Share on other sites
EDIT: I read this post again, and realized that it was utter nonsense :-).

EDIT: solved

[Edited by - Todo on January 15, 2007 7:44:44 PM]

Share this post


Link to post
Share on other sites
Solved it. Phew. Can go to bed now.

When I was calculating my texture co-ordinates when I set up the atlas, I was basing it on an inclusive-exclusive rectangle instead of an inclusive-inclusive one.

Obviously for quads rendered without rotation this tiny error was too small to be noticeable but as soon as I started doing rotations, it bit me in the bum.

I just had to change:


float Fudge=0.5f;

R.left=(FLOAT(X)+Fudge)/(FLOAT(W));
R.top=(FLOAT(Y)+Fudge)/(FLOAT(H));

R.right=(FLOAT(X+Wt)+Fudge)/(FLOAT(W));
R.bottom=(FLOAT(Y+Ht)+Fudge)/(FLOAT(H));


to

float Fudge=0.5f;

R.left=(FLOAT(X)+Fudge)/(FLOAT(W));
R.top=(FLOAT(Y)+Fudge)/(FLOAT(H));

R.right=(FLOAT(X+Wt-1)+Fudge)/(FLOAT(W));
R.bottom=(FLOAT(Y+Ht-1)+Fudge)/(FLOAT(H));


and now I can rotate to any angle without getting any artifacts.

Thanks anyway to you all and sorry for the time wasting. It's gone 1am in the UK and I have to go to work tomorrow, so I'm really glad I've solved this since I will be able to go to sleep now.

Night.

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!