# How do you rotate a cube map?

I'm looking for suggestions on this

Say you have a cube map. You want to rotate it as efficiently as possible.

You can render the cube map, rotate the camera, and render the scene to another cube map.

But it would be faster to rotate the cubemap by operating on the texels.

I'm using D3D11, and I need to read the texels in the code (so I can't use the OpenGL "Texture Matrix"!)

You can use "texture matrix" also in shaders (D3D or OpenGL - doesn't matter).

Pass float4x4 as uniform to your shader, and multiply it with texture coordinate before fetching texel.

• Ok. But I need to be able to fetch the texels @ CPU. Commented Jan 19, 2012 at 16:30
• So? Reading memory on CPU is easy - buffer[index]. Just calculate index correctly. Commented Jan 19, 2012 at 16:37

If I understand correctly, you have a cubemap and want to generate another cubemap that is a rotated version of the first one, and you have the capability of capturing a cubemap from an in-game scene (by rendering six times with cameras set to match the cube faces, or whatever).

It seems this should be easy to do by setting up a temporary scene that consists of a cube around the camera, with the original cubemap applied onto it; then rotate the cube and use the camera to capture the new cubemap.

I did it by the following. Create a new cubemap called rotated. To get the color at each texel in rotated, just get the color from the original cubemap that will land there after rotation.

So, for each texel in the rotated cube map, find the vector u from the center of the cubemap towards that texel.

"Unrotate" u by multiplying by the inverse (which is just the transpose) of the rotation matrix to get v. Find the texel that v corresponds to, and grab the color from the original cubemap, and write that value into rotated.

You will get better results by interpolating between the few pixels that v kinda points to (it will almost never point to the dead center of a pixel).