Accessing Cubic Environment Map Faces

The following code example uses IDirect3DCubeTexture9::GetCubeMapSurface to retrieve the cube-map surface used for the positive-y face (face 2).

// For this example, m_pCubeMap is a valid
// pointer to a IDirect3DCubeTexture9 interface.
LPDIRECT3DSURFACE9 pFace2;
m_pCubeMap->GetCubeMapSurface( D3DCUBEMAP_FACE_POSITIVE_Y, 0, &pFace2);

The first parameter that IDirect3DCubeTexture9::GetCubeMapSurface accepts is a D3DCUBEMAP_FACES enumerated value that describes the attached surface that the method should retrieve. The second parameter tells Microsoft?Direct3D?which level of a mipmapped cube texture to retrieve. The third parameter accepted is the address of the IDirect3DSurface9 interface, representing the returned cube texture surface. Because this cube-map is not mipmapped, 0 is used here.

Note

After calling this method, the internal reference count on the IDirect3DSurface9 interface is increased. When you are done using this surface, be sure to call the IUnknown::Release method on this IDirect3DSurface9 interface or you will have a memory leak.

Rendering to Cubic Environment Maps

You can copy images to the individual faces of the cube map just like you would any other texture or surface object. The most important thing to do before rendering to a face is set the transformation matrices so that the camera is positioned properly and points in the proper direction for that face: forward (+z), backward (-z), left (-x), right (+x), up (+y), or down (-y).

The following C++ code example prepares and sets a view matrix according to the face being rendered.

Remember, each face of a cubic environment map represents a 90-degree field of view. Unless your application requires a different field of view angle—for special effects, for example—take care to set the projection matrix accordingly.

This code example creates and sets a projection matrix for the most common case.

When the camera is in position and the projection matrix set, you can render the scene. Each object in the scene should be positioned as you would normally position them. The following code example, provided for completeness, outlines this task.

Note the call to the IDirect3DDevice9::SetRenderTarget method. When rendering to the cube map faces, you must assign the face as the current render-target surface. Applications that use depth buffers can explicitly create a depth-buffer for the render-target, or reassign an existing depth-buffer to the render-target surface. The code sample above uses the latter approach.