Step 5.2: Render the Scene

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( LPDIRECT3DDEVICE3 pd3dDevice, 
                    LPDIRECT3DVIEWPORT3 pvViewport,
                    D3DRECT* prcViewportRect )
{
    // Clear the viewport to a blue color.
    pvViewport->Clear2( 1UL, prcViewportRect, D3DCLEAR_TARGET, 0x000000ff,
                        0L, 0L );
 

The preceding code calls the IDirect3DViewport3::Clear2 method to clear the viewport. The first two methods that the Clear2 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 Clear2 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. 
    pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, D3DFVF_VERTEX,
                               g_pvTriangleVertices, 6, NULL );
 
    // End the scene.
    pd3dDevice->EndScene();
 
    return S_OK;
}
 

The IDirect3DDevice3::BeginScene and IDirect3DDevice3::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.