Microsoft DirectX 8.1 (Visual Basic) |
You create a cubic environment map texture by calling the D3DX8.CreateCubeTexture method. Cubic environment map textures must be square, with dimensions that are a power of two.
The following code shows how your Microsoft® Visual Basic® application might create a simple cubic environment map.
' ' For this example, d3dDevice is a valid Direct3DDevice8 ' object and g_D3DX is a valid Direct3DX object. ' Dim CubeMap As Direct3DCubeTexture8 CubeTexture Set CubeMap = g_D3DX.CreateCubeTexture( d3dDevice, 256, 1, D3DUSAGE_RENDERTARGET, _ D3DFMT_R8G8B8, D3DPOOL_MANAGED )
You can navigate between faces of a cubic environment map by using the Direct3DCubeTexture8.GetCubeMapSurface method.
The following code example uses GetCubeMapSurface to retrieve the cube-map surface used for the positive-y face (face 2).
' ' For this example, CubeMap is a valid ' Direct3DCubeTexture8 object. ' Dim Face2 As Direct3DSurface8 Set Face2 = CubeMap.GetCubeMapSurface( D3DCUBEMAP_FACE_POSITIVE_Y, 0 )
The first parameter that GetAttachedSurface accepts is a CONST_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. Because this cube-map is not mipmapped, 0 is used here.
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.