Thursday, August 16, 2007

Generic XNA - How to get images from a cube map

OK, may be a bit of an odd post, but I thought I would put it up any way.

I have 2 methods for creating a skybox now, either by using 6 textured quads and positioning them around the camera at set distance and scale or using an inverted cube mesh and then applying a cube map texture to it. So for both these methods I need textures to generate these skybox's, and the majority of images I have found were in cube maps but this then means that I can only use them in one of my skybox methods.

So I had a look at the DirectX Texture tool to see if there was a way to dissect a cube map there (as this is where I create my maps at the moment) but could not find how to do this here. So I decided to write my own.

In short, what I am posting here is a method to take a given cube map and break it up into it's 6 constituent parts and save them as jpg files so you can use them in the textured quad skybox method.

Here is the code:
   cube = game.Content.Load<TextureCube>("Content/textures/skybox/CubeMap");
   Color[][] sideData = new Color[6][];

   for (int s = 0; s < 6; s++)
       sideData[s] = new Color[cube.Size * cube.Size];
       cube.GetData<Color>((CubeMapFace)s, sideData[s]);
       Texture2D side = new Texture2D(cube.GraphicsDevice, cube.Size, cube.Size, 0, ResourceUsage.AutoGenerateMipMap, cube.Format);
       side.Save("Content/textures/skybox/" + ((CubeMapFace)s).ToString() + ".jpg", ImageFileFormat.Jpg);

I guess you could reverse this to take 6 images and generate a cube map possibly to build a cube map to be used as an environment cube map used in a reflection shader or just to create a cube map for a skybox.

I intend to post an example of creating cube maps on the fly so that reflective shades will give true reflections.

Hope you find it useful.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.