DirectX SDK

Step 5.2: Render the Scene

[Visual Basic]

The information in this section pertains only to applications written in C and C++. See Direct3D Immediate Mode Visual Basic Tutorials.

[C++]

Once the geometry has been updated to reflect the desired animation, you can render the scene. The Triangle sample takes a typical approach: the App_Render application-defined function called from the sample's Render3DEnvironment function starts by clearing the viewport:

HRESULT App_Render( LPDIRECT3DDEVICE7 pd3dDevice, D3DRECT* prcViewRect )
{
    // Clear the viewport to a blue color.
    pd3dDevice->Clear( 1UL, prcViewRect, D3DCLEAR_TARGET, 0x000000ff,
                       0L, 0L );

The preceding code calls the IDirect3DDevice7::Clear method to clear the viewport. The first two methods that the Clear method accepts are the address of an array of rectangles that describe the areas on the render target surface to be cleared, and a value that informs the method how many rectangles from the array should be cleared. In most cases, you'll use a single rectangle that covers the entire render target. The third parameter determines the method's behavior. It can clear a render-target surface, an associated depth buffer, the stencil buffer, or any combination of the three. Because this tutorial doesn't use a depth buffer, D3DCLEAR_TARGET is the only flag used. The last three parameters are set to reflect clearing values for the render target, depth buffer, and stencil buffer. The Triangle sample sets the clear color for the render target surface to blue. Because the remaining parameters aren't used in this tutorial, the code just sets them to zero. The Clear method will ignores them when the corresponding flag isn't present.

After clearing the viewport, the Triangle sample informs Direct3D that rendering will begin, renders the scene, then signals that rendering is complete, as shown here:

    // Begin the scene
    if( FAILED( pd3dDevice->BeginScene() ) )
        return E_FAIL;
 
    // Draw the triangle using a DrawPrimitive() call. Subsequent
    // tutorials will go into more detail on the various calls for
    // drawing polygons.
    pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, D3DFVF_VERTEX,
                               g_pvTriangleVertices, 6, NULL );
 
    // End the scene.
    pd3dDevice->EndScene();
 
    return S_OK;
}

The IDirect3DDevice7::BeginScene and IDirect3DDevice7::EndScene methods signal to the system when rendering is beginning or is complete. You can only call rendering methods between calls to these methods. Even if rendering methods fail, you should call EndScene before calling BeginScene again.

After rendering the scene to the off-screen render target, you can update the user's display. The tutorial sample performs this in Step 5.3: Update the Display.