Microsoft DirectX 8.0 (Visual Basic)

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 Microsoft® Visual Basic® code example prepares and sets a view matrix according to the face being rendered.

'
' For this example, CubeMap is a valid Direct3DCubeTexture8 object
' and m_D3DDevice is a valid Direct3DDevice8 object.
'
Sub RenderFaces

    ' Save transformation matrices of the device.
    Dim matProjSave As D3DMATRIX, _
        matViewSave As D3DMATRIX
    m_D3DDevice.GetTransform D3DTS_VIEW, matViewSave
    m_D3DDevice.GetTransform D3DTS_PROJECTION, matProjSave

    ' Store the current back buffer and z-buffer.
    Dim BackBuffer As Direct3DSurface8, _
        Zbuffer As Direct3DSurface8
    Set BackBuffer = m_D3DDevice.GetRenderTarget()
    Set Zbuffer = m_D3DDevice.GetDepthStencilSurface()

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.

    ' Use 90-degree field of view in the projection.
    Dim matProj As D3DMATRIX
    D3DXMatrixPerspectiveFovLH matProj, g_pi/4, 1, 1/2, 1000
    m_D3DDevice.SetTransform D3DTS_PROJECTION, matProj

    ' Loop through the six faces of the cube map.
    Dim i As Integer
    For i = 0 To 6

        'Standard view that will be overridden below.
        Dim vEnvEyePt As D3DVECTOR, _
            vLookatPt As D3DVECTOR, _
            vUpVec As D3DVECTOR

        Select Case i

            case D3DCUBEMAP_FACE_POSITIVE_X:
                vLookatPt.x = 1#: vUpVec.y = 1#

            case D3DCUBEMAP_FACE_NEGATIVE_X:
                vLookatPt.x = -1#: vUpVec.y = 1#

            case D3DCUBEMAP_FACE_POSITIVE_Y:
                vLookatPt.y = 1#: vUpVec.z = -1#

            case D3DCUBEMAP_FACE_NEGATIVE_Y:
                vLookatPt.y = -1#: vUpVec.z = 1#

            case D3DCUBEMAP_FACE_POSITIVE_Z:
                vLookatPt.z = 1#: vUpVec.y = 1#

            case D3DCUBEMAP_FACE_NEGATIVE_Z:
                vLookatPt.z = -1#: vUpVec.y = 1#

        End Select

        Dim matView As D3DMATRIX
        D3DXMatrixLookAtLH matView, vEnvEyePt, vLookatPt, vUpVec
        m_D3DDevice.SetTransform D3DTS_VIEW, matView

Once 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.

        ' Get cube surface in order to render to it.
        Dim Face As Direct3DSurface8
        Set Face = CubeMap.GetCubeMapSurface i, 0
        m_D3DDevice.SetRenderTarget Face, Zbuffer
        Set Face = Nothing

        m_D3DDevice.BeginScene
        ' Render scene here.
        m_D3DDevice.EndScene

    Next

    ' Change the render target back to the main back buffer.
    m_D3DDevice.SetRenderTarget BackBuffer, pZBuffer
    Set BackBuffer = Nothing
    Set Zbuffer = Nothing

    // Restore the original transformation matrices.
    m_D3DDevice.SetTransform D3DTS_VIEW, matViewSave
    m_D3DDevice.SetTransform D3DTS_PROJECTION, matProjSave

End Sub

Note the call to the Direct3DDevice8.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 method.