Lesson 13: SDK文档:Tutorial 1 Direct3D 11 Basics分析

头文件:

#include <windows.h>
#include <d3d11.h>
#include <d3dx11.h>
#include "resource.h"

全局变量声明:

// Global Variables
HINSTANCE               g_hInst = NULL; //实例句柄
HWND                    g_hWnd = NULL;  //窗口句柄
D3D_DRIVER_TYPE         g_driverType = D3D_DRIVER_TYPE_NULL;    //驱动类型
D3D_FEATURE_LEVEL       g_featureLevel = D3D_FEATURE_LEVEL_11_0;    //特征等级
ID3D11Device*           g_pd3dDevice = NULL;    //设备接口指针
ID3D11DeviceContext*    g_pImmediateContext = NULL; //设备上下文接口指针
IDXGISwapChain*         g_pSwapChain = NULL;    //交换链接口指针
ID3D11RenderTargetView* g_pRenderTargetView = NULL; //渲染目标视图接口指针

函数声明:

// Forward declarations
HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow );    //窗口初始化函数
HRESULT InitDevice();   //设备初始化函数
void CleanupDevice();   //清除设备函数
LRESULT CALLBACK    WndProc( HWND, UINT, WPARAM, LPARAM );  //窗口过程函数
void Render();  //渲染函数

程序入口点函数:

int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow )
{
    UNREFERENCED_PARAMETER( hPrevInstance );
    UNREFERENCED_PARAMETER( lpCmdLine );

    // 窗口初始化
    if( FAILED( InitWindow( hInstance, nCmdShow ) ) )
        return 0;

    // 设备初始化
    if( FAILED( InitDevice() ) )
    {
        CleanupDevice();
        return 0;
    }

    // 消息主循环
    // Main message loop
    MSG msg = {0};
    while( WM_QUIT != msg.message )
    {
        // 有消息则对消息进行处理
        if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
        // 没有消息则进行渲染
        else
        {
            Render();
        }
    }

    // 清除设备
    CleanupDevice();

    return ( int )msg.wParam;
}

注册窗口类并创建窗口:

// Register class and create window
//--------------------------------------------------------------------------------------
HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow )
{
    // 注册窗口类
    // Register class
    WNDCLASSEX wcex;
    wcex.cbSize = sizeof( WNDCLASSEX );
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = LoadIcon( hInstance, ( LPCTSTR )IDI_TUTORIAL1 );
    wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
    wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = L"TutorialWindowClass";
    wcex.hIconSm = LoadIcon( wcex.hInstance, ( LPCTSTR )IDI_TUTORIAL1 );
    if( !RegisterClassEx( &wcex ) )
        return E_FAIL;

    // 创建窗口
    // Create window
    g_hInst = hInstance;
    RECT rc = { 0, 0, 640, 480 };
    AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
    g_hWnd = CreateWindow( L"TutorialWindowClass", L"Direct3D 11 Tutorial 1: Direct3D 11 Basics", WS_OVERLAPPEDWINDOW,
                           CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance,
                           NULL );
    if( !g_hWnd )
        return E_FAIL;

    // 显示窗口
    ShowWindow( g_hWnd, nCmdShow );

    return S_OK;
}

窗口过程函数:

// Called every time the application receives a message
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
    // 该结构体包含了某应用程序用来绘制它所拥有的窗口客户区所需要的信息。
    PAINTSTRUCT ps;
    HDC hdc;

    switch( message )
    {
        // 这个消息在Windows程序设计中是很重要的。当窗口显示区域的一部分显示内容或者全部变为"无效",以致于必须"更新画面"时,将由这个消息通知程序。
        case WM_PAINT:
            // 为指定窗口进行绘图工作的准备,并用将和绘图有关的信息填充到一个PAINTSTRUCT结构中。
            hdc = BeginPaint( hWnd, &ps );
            EndPaint( hWnd, &ps );
            break;

        case WM_DESTROY:
            PostQuitMessage( 0 );
            break;

        default:
            // 调用缺省的窗口过程来为应用程序没有处理的任何窗口消息提供缺省的处理。该函数确保每一个消息得到处理。
            return DefWindowProc( hWnd, message, wParam, lParam );
    }

    return 0;
}

设备初始化函数:

// Create Direct3D device and swap chain
HRESULT InitDevice()
{
    HRESULT hr = S_OK;

    // 矩形rc,用于获得客户区矩形
    RECT rc;
    GetClientRect( g_hWnd, &rc );
    // 计算窗口的宽度和高度
    UINT width = rc.right - rc.left;
    UINT height = rc.bottom - rc.top;

    UINT createDeviceFlags = 0;
    // 把createDeviceFlags除去D3D10_CREATE_DEVICE_DEBUG这种属性
#ifdef _DEBUG
    createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

    // 设置驱动类型
    D3D_DRIVER_TYPE driverTypes[] =
    {
        D3D_DRIVER_TYPE_HARDWARE,
        D3D_DRIVER_TYPE_WARP,
        D3D_DRIVER_TYPE_REFERENCE,
    };
    // 计算驱动类型数量
    UINT numDriverTypes = ARRAYSIZE( driverTypes );

    //特征等级advanced
    D3D_FEATURE_LEVEL featureLevels[] =
    {
        D3D_FEATURE_LEVEL_11_0,
        D3D_FEATURE_LEVEL_10_1,
        D3D_FEATURE_LEVEL_10_0,
    };
    UINT numFeatureLevels = ARRAYSIZE( featureLevels );

    // 交换链结构体
    DXGI_SWAP_CHAIN_DESC sd;
    // 清空内存
    ZeroMemory( &sd, sizeof( sd ) );
    // 填充交换链结构体
    sd.BufferCount = 1;
    sd.BufferDesc.Width = width;
    sd.BufferDesc.Height = height;
    sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    sd.BufferDesc.RefreshRate.Numerator = 60;
    sd.BufferDesc.RefreshRate.Denominator = 1;
    sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    sd.OutputWindow = g_hWnd;
    sd.SampleDesc.Count = 1;
    sd.SampleDesc.Quality = 0;
    sd.Windowed = TRUE;

    // 为每种驱动类型创建设备和交换链
    for( UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++ )
    {
        g_driverType = driverTypes[driverTypeIndex];
        hr = D3D11CreateDeviceAndSwapChain( NULL, g_driverType, NULL, createDeviceFlags, featureLevels, numFeatureLevels,
                                            D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &g_featureLevel, &g_pImmediateContext );
        if( SUCCEEDED( hr ) )
            break;
    }
    if( FAILED( hr ) )
        return hr;

    // Create a render target view
    // 声明2D纹理接口
    ID3D11Texture2D* pBackBuffer = NULL;
    // 获得2D纹理的缓冲区地址
    hr = g_pSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&pBackBuffer );
    if( FAILED( hr ) )
        return hr;

    // 创建渲染目标视图
    hr = g_pd3dDevice->CreateRenderTargetView( pBackBuffer, NULL, &g_pRenderTargetView );
    pBackBuffer->Release();
    if( FAILED( hr ) )
        return hr;

    // 设置渲染目标
    g_pImmediateContext->OMSetRenderTargets( 1, &g_pRenderTargetView, NULL );

    // 设置视口
    // Setup the viewport
    // 声明视口结构体
    D3D11_VIEWPORT vp;
    // 填充视口结构体
    vp.Width = (FLOAT)width;
    vp.Height = (FLOAT)height;
    vp.MinDepth = 0.0f;
    vp.MaxDepth = 1.0f;
    vp.TopLeftX = 0;
    vp.TopLeftY = 0;
    // 设置视口
    g_pImmediateContext->RSSetViewports( 1, &vp );

    return S_OK;
}

渲染函数:

// Render the frame
void Render()
{
    // Just clear the backbuffer
    // 设置清空颜色
    float ClearColor[4] = { 0.0f, 0.125f, 0.3f, 1.0f }; //red,green,blue,alpha
    // 清空后台缓冲区
    g_pImmediateContext->ClearRenderTargetView( g_pRenderTargetView, ClearColor );
    // ...
    // 这里描述渲染内容
    // ...
    // 将渲染到后台缓冲区中的信息展现到前台缓冲区
    g_pSwapChain->Present( 0, 0 );
}

清除设备函数:

// Clean up the objects we've created
void CleanupDevice()
{
    if( g_pImmediateContext ) g_pImmediateContext->ClearState();

    if( g_pRenderTargetView ) g_pRenderTargetView->Release();
    if( g_pSwapChain ) g_pSwapChain->Release();
    if( g_pImmediateContext ) g_pImmediateContext->Release();
    if( g_pd3dDevice ) g_pd3dDevice->Release();
}

我们快速回顾一遍整个程序的工作

程序入口点函数wWinMain:

->初始化窗口InitWindow:
    ->注册窗口类
    ->创建窗口
    ->显示窗口
->初始化设备InitDevice:
    ->设置驱动类型
    ->设置特征等级
    ->创建设备和交换链
    ->渲染目标视图
    ->设置渲染目标
    ->设置视口
->进入消息循环:
    ->收到消息,则对消息进行处理,同时调用窗口过程函数
    ->没有消息,则进行渲染
->清除所有设备CleanupDevice
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值