# Rotate UV

## Recommended Posts

cozzie    5029

Hi,

I'm currently learning d3d11 including the basics of texturing/ UV updating etc.

In the current exercise I'm trying to rotate a texture on each face of a cube (6 faces), around the center of the texture/ aka face. The UV's range nicely from 0.0 to 1.0 and I'm using 'WRAP' as address mode.

To achieve the result I'm multiply the UV's by a texture transform matrix.

Here's my attempt:

	XMMATRIX zRotMat = XMMatrixRotationZ(XMConvertToRadians(zRot));
XMMATRIX centerMat = XMMatrixTranslation(0.5f, 0.5f, 0.0f);
XMMATRIX scaleMat = XMMatrixScaling(1.0f, 1.0f, 1.0f);

//	XMStoreFloat4x4(&mTexTransform, scaleMat * zRotMat * centerMat);
XMStoreFloat4x4(&mTexTransform, centerMat * zRotMat);



So far I've found out that just doing a rotation, doesn't give the expected result, because the rotation then uses 0.0 as origin (in my case the left top corner of the cube face). So I tried to do a translation of 0.5f on both X and Y, because the cube has the dimensions of 1.0 x 1.0 x 1.0.

The result of this is also that the origin of the rotation is not the center of both the cube face and/or the texture.

I think I'm overseeing something, but can't figure it out yet.

Any input is appreciated.

Edited by cozzie

##### Share on other sites
HappyCoder    5053
Try this

        XMMATRIX zRotMat = XMMatrixRotationZ(XMConvertToRadians(zRot));
XMMATRIX centerMat = XMMatrixTranslation(0.5f, 0.5f, 0.0f);
XMMATRIX centerInverse = XMMatrixTranslation(-0.5f, -0.5f, 0.0f);

XMStoreFloat4x4(&mTexTransform, centerMat * zRotMat * centerInverse);

If that doesn't work, then switch the order of centerMat and centerInverse to be

	XMStoreFloat4x4(&mTexTransform, centerInverse * zRotMat * centerMat);


##### Share on other sites
cozzie    5029

Thanks, got it.

When I read your reply it 'felt', first move to the center, rotate and then move back to the initial coordinate.

Working like a charm now, with the 2nd option (inverse * zRot * center).