DirectX - Rendering



Once the process of initialization and the swap chain is created, it is time to start rendering the objects. Rendering is considered to be a very easy process with little preparation.

In this chapter, we will be creating a blank frame with specific color which is being used over the period of time.

Following steps are involved for the rendering process in DirectX objects −

Setting the Render Target

Once the user has created the render target, it has to be set as the active render target. This is mandatory for each frame.

The sample function which renders a single frame of 3D graphics is mentioned below −

void CGame::Render(){
   // set our new render target object
   devcon->OMSetRenderTargets(1, rendertarget.GetAddressOf(), nullptr);
   // switch the back buffer
   swapchain->Present(1, 0);
}

The above mentioned function was created to render the active target in more precise manner. If you observe, it includes the below two parameters.

1st parameter

It includes the number of render targets to set.

2nd parameter

It is considered as a pointer to list render target view objects. We only use one parameter to address the render target interface. This particular interface is obtained with the help of GetAddressOf() function. This function is little different in comparison to Get() function. It refers pointer to a pointer instead of just one pointer.

3rd parameter

It is considered as an advanced parameter which will be discussed later. For now, let us consider it to be nullptr.

Clearing the Back Buffer

We will create a function that renders a particular single frame by setting the entire buffer which is black in color.

// this function renders a single frame of 3D graphics
void CGame::Render(){
   // set our new render target object as the active render target
   devcon->OMSetRenderTargets(1, rendertarget.GetAddressOf(), nullptr);
   float color[4] = {0.0f, 0.2f, 0.4f, 1.0f};
   devcon->ClearRenderTargetView(rendertarget.Get(), color);
   swapchain->Present(1, 0);
}

The ClearRenderTargetView() function as discussed in the code snippet above sets each pixel in a render target to a specific color.

1st parameter

It is used to address the render target interface.

2nd parameter

It is used as an array of four float values. The first is red, the second one is green and the third one is considered blue while the fourth is considered with parameter of transparency.

Note − Each value used in the float array is between 0.0f which means no color, and 1.0f which means full color. With this combination, a user can create any color.

Advertisements