TosKen
TosKen

Reputation: 491

How to eliminate distortion when rotating a texture

the background of my question is the following:
I draw a simple texture mapped rectangle.
The rectangle has always the same aspect as the texture.
Becuase of that i simply use texture coordines (0,0), (0,1), (1,0), (1,1) at the corners to get the complete texture mapped.
Now i have implemented a feature to rotate the texture on the rectangle by altering the OpenGL texture matrix.

By simply rotating the texture coordinates i will get texture repeating or wrapping artifacts because the rotated texture coordinates sometimes gets out of the valid range [0, 1].
I have already solved this problem. I compute a scale factor to make sure that the visible texture cutout will always be in the correct interval (zooming into the texture).

One maybe simple problem left is the following.
Beucause of the fact that the texture space is always [0,1] on u and v axis (independent of the real texture aspect), i get texture distortion when rotating the texture.

Can you help me of how to compute a scale factor for the texture coordinates (maybe two for x and y) to correct this distortion based on the rotation angle and the texture aspect (width and height)

Thanks and best regards Sebastian

Upvotes: 3

Views: 851

Answers (1)

Yakov Galka
Yakov Galka

Reputation: 72479

Let w and h be the width and height of your rectangle (or the texture, here only the aspect ratio w:h matters). Then we compose the total texture transformation as follows.

First we map the uv coordinates from (0,0)..(1,1) to (0,0)..(w,h):

[ w 0 0 ]
[ 0 h 0 ]
[ 0 0 1 ]

Next, assuming that you want to rotate and scale around the texture center we shift it by half the size, so that (0,0) maps to (-w/2,-h/2), (.5,.5) to (0,0), and (1,1) to (w/2,h/2):

[  1   0 -w/2 ]   [ w 0 0 ]   [  w   0 -w/2 ]
[  0   1 -h/2 ] x [ 0 h 0 ] = [  0   h -h/2 ]
[  0   0   1  ]   [ 0 0 1 ]   [  0   0   1  ]

Then we apply the scale S and rotation R from the left, and convert back to the texture space by applying the inverse of the above transformation. The net transformation is given by:

[ 1/w  0  1/2 ]     [  w   0 -w/2 ]
[  0  1/h 1/2 ] R S [  0   h -h/2 ]
[  0   0   1  ]     [  0   0   1  ]

Upvotes: 3

Related Questions