Platform SDK: DirectX

Implementing Stereo View

After you have initialized DirectDraw and enumerated display modes to verify that stereo view is supported, you can access DirectDraw stereo view support. First, you need to declare surfaces for the stereo view flipping chain.

[C++]
LPDIRECTDRAWSURFACE7 pFrontBuffer;
LPDIRECTDRAWSURFACE7 pBackBuffer;
LPDIRECTDRAWSURFACE7 pBackBufferLeft;

Then, you prepare the DDSURFACEDESC2 structure, describing a surface that supports stereo view.

DDSURFACEDESC2 ddsd2 = {sizeof(ddsd2)};

ddsd2.dwFlags = DDSDS_CAPS|DDSD_BACKBUFFERCOUNT;
ddsd2.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE| 
                       DDSCAPS_3DDEVICE;
ddsd2.dwBackBufferCount =1;
ddsd2.ddsCaps.dwCaps2 = DDSCAPS2_STEREOSURFACELEFT;
ddsd2.ddsCaps.dwCaps3 = ddsd2.ddsCaps.dwCaps4 =0;

If the DDSCAPS_PRIMARYSURFACE flag is not specified, you cannot specify DDSCAPS2_STEREOSURFACELEFT for the surface description. This is to prevent an application from attempting to build a stereo flipping chain with separate surfaces and attaching them with a call to the IDirectDrawSurface7::AddAttachedSurface method. Instead, if DDSCAPS2_STEREOSURFACELEFT is specified as a capability flag, then two surfaces are created for every buffer in the primary chain. For example, if dwBackBufferCount = N, then the call will create 2 * (N+1) surfaces. One of the surfaces is created with the DDSCAPS2_STEREOSURFACELEFT capability flag and one is not.

[Visual Basic]
Dim ddsFrontBuffer As DirectDrawSurface7
Dim ddsBackBuffer As DirectDrawSurface7
Dim ddsBackBufferLeft As DirectDrawSurface7

Then, you prepare the DDSURFACEDESC2 structure, describing a surface that supports stereo view.

Dim ddsd2 As DDSURFACEDESC2

ddsd2.lFlags = DDSD_CAPS Or DDSD_BACKBUFFERCOUNT
ddsd2.ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE Or DDSCAPS_3DDEVICE
ddsd2.lBackBufferCount = 1
ddsd2.ddsCaps.lCaps2 = DDSCAPS2_STEREOSURFACELEFT
ddsd2.ddsCaps.lCaps3 = ddsd2.ddsCaps.lCaps4 = 0

If the DDSCAPS_PRIMARYSURFACE flag is not specified, you cannot specify DDSCAPS2_STEREOSURFACELEFT for the surface description. This is to prevent an application from attempting to build a stereo flipping chain with separate surfaces and attaching them with a call to the DirectDrawSurface7.AddAttachedSurface method. Instead, if DDSCAPS2_STEREOSURFACELEFT is specified as a capability flag, then two surfaces are created for every buffer in the primary chain. For example, if lBackBufferCount = N, then the call will create 2 * (N+1) surfaces. One of the surfaces is created with the DDSCAPS2_STEREOSURFACELEFT capability flag and one is not.

The surfaces are attached and labeled according to the following diagram.

Note that if the application specifies DDSCAPS_3DDEVICE, this capability flags will be copied to all surfaces in the chain.

[C++]

Now, you can create the primary surface.

pDD7->CreateSurface(&ddsd2, &pFrontBuffer, NULL);

ddsd2.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
pFrontBuffer->GetAttachedSurface(&ddsd2.ddsCaps, &pBackBuffer);

ddsd2.ddsCaps.dwCaps = 0;
ddsd2.ddsCaps.dwCaps2 = DDSCAPS2_STEREOSURFACELEFT;
pBackBuffer->GetAttachedSurface(&ddsd2.ddsCaps, &pBackBufferLeft);

In the preceding code fragment, the IDirectDrawSurface7::GetAttachedSurface method obtains the attached surfaces in the stereo view flipping chain.

while(running)
{
    RenderRightImage(pBackBuffer);
    RenderLeftImage(pBackBufferLeft);

pFrontBuffer->Flip(NULL,DDFLIP_WAIT|DDFLIP_STEREO);
}

In the preceding code fragment, RenderRightImage and RenderLeftImage are application-defined functions that render the stereo images.

If the DDFLIP_STEREO flag is passed to the IDirectDrawSurface7::Flip method, then two surfaces will be passed to the driver. The surfaces will become the new stereo pair. DirectDraw will only allow this flag on a surface that has a DDSCAPS2_STEREOSURFACELEFT surface attached.

[Visual Basic]

Now, you can create the primary surface.

Set ddsFrontBuffer = dd.CreateSurface(ddsd2)

ddsd2.ddsCaps.lCaps = DDSCAPS_BACKBUFFER
Set ddsBackBuffer = ddsFrontBuffer.GetAttachedSurface(ddsd2.ddsCaps)

ddsd2.ddsCaps.lCaps = 0
ddsd2.ddsCaps.lCaps2 = DDSCAPS2_STEREOSURFACELEFT
Set ddsBackBufferLeft = ddsBackBuffer.GetAttachedSurface(ddsd2.ddsCaps)

In the preceding code fragment, the DirectDrawSurface7.GetAttachedSurface method obtains the attached surfaces in the stereo view flipping chain.

Do While g_bRunning

    RenderRightImage ddsBackBuffer      ' Render the right image.
    RenderLeftImage ddsBackBufferLeft   ' Render the left image.
    
    ddsFrontBuffer.Flip Nothing, DDFLIP_WAIT Or DDFLIP_STEREO
    
Loop

In the preceding code fragment, RenderRightImage and RenderLeftImage are application-defined functions that render the stereo images.

If the DDFLIP_STEREO flag is passed to the DirectDrawSurface7.Flip method, then two surfaces will be passed to the driver. The surfaces will become the new stereo pair. DirectDraw will only allow this flag on a surface that has a DDSCAPS2_STEREOSURFACELEFT surface attached.

If the DDFLIP_STEREO flag is not passed to the Flip method, the surface flipping call to the driver will specify only one surface. This is an indication to the driver that stereo view is no longer required.

For more information on flipping chains, see the Flipping Surfaces overview topic.