このブログを検索

2019年5月18日土曜日

【C++】 DirectX11 - 四角形の回転と移動

【C++】 DirectX11 - 四角形の回転と移動
(2019年5月18日)


■使用ソフト
・Visual Studio Community 2019


■言語
・C/C++


■Windows SDK バージョン
・10.0.17763.0
 ※Windows SDK バージョンの変更方法


■手順
1.以下をベースにコード変更する。
【C++】 DirectX11 - 四角形の描画(インデックスバッファ)

2.以下の6つのファイルを作成してビルドを行う。
(1)Main.h //前回から変更なし
(2)Main.cpp //前回から変更なし
(3)DirectX.h //前回から変更なし
(4)DirectX.cpp
(5)VertexShader.hlsl //前回から変更なし
(6)PixelShader.hlsl //前回から変更なし

(1)Main.h
#pragma once

#include <windows.h>
#pragma comment(lib,"winmm.lib")

//--------------------------------------------------------------------------------------
// Windowクラス:Window関係
//--------------------------------------------------------------------------------------
class Window
{
public:
    HRESULT InitWindow(HINSTANCE hInstance, int nCmdShow);
    void InitFps();
    void CalculationFps();
    void CalculationSleep();
    void CalculationFrameTime();

    static HWND GethWnd();
    static int GetClientWidth();
    static int GetClientHeight();
    static double GetFps();
    static double GetFrameTime();
private:
    LARGE_INTEGER m_freq = { 0 };
    LARGE_INTEGER m_starttime = { 0 };
    LARGE_INTEGER m_nowtime = { 0 };
    LARGE_INTEGER m_frametime_a = { 0 };
    LARGE_INTEGER m_frametime_b = { 0 };
    int m_iCount = 0;

    static HWND g_hWnd;
    static int g_iClientWidth;
    static int g_iClientHeight;
    static double g_dFps;
    static double g_dFrameTime;
};

(2)Main.cpp
#include "Main.h"
#include "DirectX.h"

//--------------------------------------------------------------------------------------
// 静的メンバ
//--------------------------------------------------------------------------------------
HWND Window::g_hWnd = nullptr;//ウィンドウハンドル
int Window::g_iClientWidth = 800;//クライアント領域の横幅
int Window::g_iClientHeight = 600;//クライアント領域の高さ
double Window::g_dFps = 0;//FPS
double Window::g_dFrameTime = 0;//1フレームあたりの時間

//--------------------------------------------------------------------------------------
// 前方宣言
//--------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

//--------------------------------------------------------------------------------------
// wWinMain()関数:エントリーポイント
//--------------------------------------------------------------------------------------
int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
{
    if (FAILED(CoInitialize(nullptr)))//COMの初期化
        return 0;

    Window win;

    if (FAILED(win.InitWindow(hInstance, nCmdShow)))
        return 0;

    DirectX11 dx;

    if (FAILED(dx.InitDevice()))
        return 0;

    win.InitFps();

    // メインメッセージループ
    MSG msg = { 0 };
    while (WM_QUIT != msg.message)
    {
        if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
            win.CalculationFps();

            win.CalculationFrameTime();

            dx.Render();

            win.CalculationSleep();
        }
    }

    CoUninitialize();//COMの終了処理

    return (int)msg.wParam;
}

//--------------------------------------------------------------------------------------
// WndProc()関数:ウィンドウプロシージャ
//--------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT uiMessage, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;

    switch (uiMessage)
    {
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        EndPaint(hWnd, &ps);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProc(hWnd, uiMessage, wParam, lParam);
    }

    return 0;
}

//--------------------------------------------------------------------------------------
// Window::InitWindow()関数:ウィンドウの表示
//--------------------------------------------------------------------------------------
HRESULT Window::InitWindow(HINSTANCE hInstance, int nCmdShow)
{
    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 = nullptr;
    wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszMenuName = nullptr;
    wcex.lpszClassName = L"WindowClass";
    wcex.hIconSm = nullptr;
    if (!RegisterClassEx(&wcex))
        return E_FAIL;

    RECT rc = { 0, 0, g_iClientWidth, g_iClientHeight };
    AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
    g_hWnd = CreateWindow(L"WindowClass", L"DirectXプログラミング",
        WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
        CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, nullptr, nullptr, hInstance,
        nullptr);
    if (!g_hWnd)
        return E_FAIL;

    ShowWindow(g_hWnd, nCmdShow);

    return S_OK;
}

//--------------------------------------------------------------------------------------
// Window::InitFps()関数:FPS計測の初期化
//--------------------------------------------------------------------------------------
void Window::InitFps()
{
    QueryPerformanceFrequency(&m_freq);
    QueryPerformanceCounter(&m_starttime);//現在の時間を取得(1フレーム目)
}

//--------------------------------------------------------------------------------------
// Window::CalculationFps()関数:FPS計測
//--------------------------------------------------------------------------------------
void Window::CalculationFps()
{
    if (m_iCount == 60)//カウントが60の時の処理
    {
        QueryPerformanceCounter(&m_nowtime);//現在の時間を取得(60フレーム目)
        //FPS = 1秒 / 1フレームの描画にかかる時間
        //    = 1000ms / ((現在の時間ms - 1フレーム目の時間ms) / 60フレーム)
        g_dFps = 1000.0 / (static_cast<double>((m_nowtime.QuadPart - m_starttime.QuadPart) * 1000 / m_freq.QuadPart) / 60.0);
        m_iCount = 0;//カウントを初期値に戻す
        m_starttime = m_nowtime;//1フレーム目の時間を現在の時間にする
    }
    m_iCount++;//カウント+1
}

//--------------------------------------------------------------------------------------
// Window::CalculationSleep()関数:Sleepさせる時間の計算
//--------------------------------------------------------------------------------------
void Window::CalculationSleep()
{
    QueryPerformanceCounter(&m_nowtime);//現在の時間を取得
    //Sleepさせる時間ms = 1フレーム目から現在のフレームまでの描画にかかるべき時間ms - 1フレーム目から現在のフレームまで実際にかかった時間ms
    //                  = (1000ms / 60)*フレーム数 - (現在の時間ms - 1フレーム目の時間ms)
    DWORD dwSleepTime = static_cast<DWORD>((1000.0 / 60.0) * m_iCount - (m_nowtime.QuadPart - m_starttime.QuadPart) * 1000 / m_freq.QuadPart);
    if (dwSleepTime > 0 && dwSleepTime < 18)//大きく変動がなければSleepTimeは1~17の間に納まる
    {
        timeBeginPeriod(1);
        Sleep(dwSleepTime);
        timeEndPeriod(1);
    }
    else//大きく変動があった場合
    {
        timeBeginPeriod(1);
        Sleep(1);
        timeEndPeriod(1);
    }
}

//--------------------------------------------------------------------------------------
// Window::CalculationFrameTime()関数:1フレームあたりの時間の計測
//--------------------------------------------------------------------------------------
void Window::CalculationFrameTime()
{
    static int iFlg;
    if (iFlg == 0)
    {
        QueryPerformanceCounter(&m_frametime_a);
        iFlg = 1;
    }
    QueryPerformanceCounter(&m_frametime_b);
    g_dFrameTime = (m_frametime_b.QuadPart - m_frametime_a.QuadPart) * 1000.0 / m_freq.QuadPart;
    m_frametime_a = m_frametime_b;
}

//--------------------------------------------------------------------------------------
// Window::GethWnd()関数:hWndの取得
//--------------------------------------------------------------------------------------
HWND Window::GethWnd()
{
    return g_hWnd;
}

//--------------------------------------------------------------------------------------
// Window::GetClientWidth()関数:クライアント領域の横幅の取得
//--------------------------------------------------------------------------------------
int Window::GetClientWidth()
{
    return g_iClientWidth;
}

//--------------------------------------------------------------------------------------
// Window::GetClientHeight()関数:クライアント領域の高さの取得
//--------------------------------------------------------------------------------------
int Window::GetClientHeight()
{
    return g_iClientHeight;
}

//--------------------------------------------------------------------------------------
// Window::GetFps()関数:FPSの取得
//--------------------------------------------------------------------------------------
double Window::GetFps()
{
    return g_dFps;
}

//--------------------------------------------------------------------------------------
// Window::GetFrameTime()関数:1フレームあたりの時間の取得
//--------------------------------------------------------------------------------------
double Window::GetFrameTime()
{
    return g_dFrameTime;
}

(3)DirectX.h
#pragma once

#pragma comment(lib,"d3d11.lib")
#pragma comment(lib,"d3dCompiler.lib")
#pragma comment(lib,"d2d1.lib")
#pragma comment(lib,"dwrite.lib")
#include <d3d11_1.h>
#include <d3dcompiler.h>
#include <directxmath.h>
#include <directxcolors.h>
#include <d2d1_1.h>
#include <dwrite.h>
#include <wchar.h>
#include <wrl/client.h>
#include <random>

//--------------------------------------------------------------------------------------
// 構造体
//--------------------------------------------------------------------------------------
struct SimpleVertex
{
    DirectX::XMFLOAT3 Pos;
    DirectX::XMFLOAT4 Color;
};

struct ConstantBuffer
{
    DirectX::XMMATRIX world;
    DirectX::XMMATRIX view;
    DirectX::XMMATRIX projection;
};

//--------------------------------------------------------------------------------------
// DirectX11クラス:DirectX関係
//--------------------------------------------------------------------------------------
class DirectX11
{
public:
    DirectX11();
    ~DirectX11();
    HRESULT CompileShaderFromFile(const WCHAR* wcFileName, LPCSTR lpEntryPoint, LPCSTR lpShaderModel, ID3DBlob** D3DBlob);
    HRESULT InitDevice();
    void Render();
private:
    //------------------------------------------------------------
    // DirectX11とDirect2D 1.1の初期化
    //------------------------------------------------------------
    Microsoft::WRL::ComPtr<ID3D11DeviceContext> m_D3DDeviceContext;
    Microsoft::WRL::ComPtr<ID2D1DeviceContext> m_D2DDeviceContext;
    Microsoft::WRL::ComPtr<IDXGISwapChain1> m_DXGISwapChain1;
    Microsoft::WRL::ComPtr<ID2D1Bitmap1> m_D2DBitmap1;
    Microsoft::WRL::ComPtr<ID3D11RenderTargetView> m_D3DRenderTargetView;
    Microsoft::WRL::ComPtr<ID3D11VertexShader> m_D3DVertexShader;
    Microsoft::WRL::ComPtr<ID3D11PixelShader> m_D3DPixelShader;
    Microsoft::WRL::ComPtr<ID3D11Buffer> m_D3DVertexBuffer;
    Microsoft::WRL::ComPtr<ID3D11Buffer> m_D3DConstantBuffer;
    DirectX::XMMATRIX m_matWorld;
    DirectX::XMMATRIX m_matView;
    DirectX::XMMATRIX m_matProjection;

    //------------------------------------------------------------
    // DirectWriteの初期化
    //------------------------------------------------------------
    Microsoft::WRL::ComPtr<IDWriteTextFormat> m_DWriteTextFormat;
    Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> m_D2DSolidBrush;
};

(4)DirectX.cpp
#include "Main.h"
#include "DirectX.h"

//--------------------------------------------------------------------------------------
// DirectX11::DirectX11()関数:コンストラクタ
//--------------------------------------------------------------------------------------
DirectX11::DirectX11()
{
    m_matWorld = DirectX::XMMatrixIdentity();
    m_matView = DirectX::XMMatrixIdentity();
    m_matProjection = DirectX::XMMatrixIdentity();
}

//--------------------------------------------------------------------------------------
// DirectX11::~DirectX11関数:デストラクタ
//--------------------------------------------------------------------------------------
DirectX11::~DirectX11()
{

}

//--------------------------------------------------------------------------------------
// DirectX11::CompileShaderFromFile():シェーダのコンパイル
//--------------------------------------------------------------------------------------
HRESULT DirectX11::CompileShaderFromFile(const WCHAR* wcFileName, LPCSTR lpEntryPoint, LPCSTR lpShaderModel, ID3DBlob** D3DBlob)
{
    HRESULT hr = S_OK;

    DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#ifdef _DEBUG
    dwShaderFlags |= D3DCOMPILE_DEBUG;

    dwShaderFlags |= D3DCOMPILE_SKIP_OPTIMIZATION;
#endif

    Microsoft::WRL::ComPtr<ID3DBlob> D3DErrorBlob;
    hr = D3DCompileFromFile(wcFileName, nullptr, nullptr, lpEntryPoint, lpShaderModel,
        dwShaderFlags, 0, D3DBlob, &D3DErrorBlob);
    if (FAILED(hr))
    {
        if (D3DErrorBlob)
        {
            OutputDebugStringA(reinterpret_cast<const char*>(D3DErrorBlob->GetBufferPointer()));
        }
        return hr;
    }

    return S_OK;
}

//--------------------------------------------------------------------------------------
// DirectX11::InitDevice():DirectX関係の初期化
//--------------------------------------------------------------------------------------
HRESULT DirectX11::InitDevice()
{
    //------------------------------------------------------------
    // DirectX11とDirect2D 1.1の初期化
    //------------------------------------------------------------
    HRESULT hr = S_OK;

    UINT uiDeviceFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;//DirectX11上でDirect2Dを使用するために必要
#ifdef _DEBUG
    uiDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

    D3D_DRIVER_TYPE drivertypes[] =
    {
        D3D_DRIVER_TYPE_HARDWARE,
        D3D_DRIVER_TYPE_WARP,
        D3D_DRIVER_TYPE_REFERENCE,
    };
    UINT uiDriverTypesNum = ARRAYSIZE(drivertypes);

    D3D_FEATURE_LEVEL featurelevels[] =
    {
        D3D_FEATURE_LEVEL_11_1,
        D3D_FEATURE_LEVEL_11_0,
        D3D_FEATURE_LEVEL_10_1,
        D3D_FEATURE_LEVEL_10_0,
    };
    UINT uiFeatureLevelsNum = ARRAYSIZE(featurelevels);

    Microsoft::WRL::ComPtr<ID3D11Device> D3DDevice;
    D3D_DRIVER_TYPE drivertype = D3D_DRIVER_TYPE_NULL;
    D3D_FEATURE_LEVEL featurelevel = D3D_FEATURE_LEVEL_11_0;
    for (UINT uiDriverTypeIndex = 0; uiDriverTypeIndex < uiDriverTypesNum; uiDriverTypeIndex++)
    {
        drivertype = drivertypes[uiDriverTypeIndex];
        hr = D3D11CreateDevice(nullptr, drivertype, nullptr, uiDeviceFlags, featurelevels, uiFeatureLevelsNum,
            D3D11_SDK_VERSION, &D3DDevice, &featurelevel, &m_D3DDeviceContext);//&D3DDevice &m_D3DDeviceContext 初期化

        if (hr == E_INVALIDARG)
        {
            hr = D3D11CreateDevice(nullptr, drivertype, nullptr, uiDeviceFlags, &featurelevels[1], uiFeatureLevelsNum - 1,
                D3D11_SDK_VERSION, &D3DDevice, &featurelevel, &m_D3DDeviceContext);//&D3DDevice &m_D3DDeviceContext 初期化
        }

        if (SUCCEEDED(hr))
            break;
    }
    if (FAILED(hr))
        return hr;

    Microsoft::WRL::ComPtr<IDXGIDevice2> DXGIDevice2;
    hr = D3DDevice.As(&DXGIDevice2);//D3DDevice->QueryInterface()ではなくD3DDevice.As()、&DXGIDevice2 初期化
    if (FAILED(hr))
        return hr;

    Microsoft::WRL::ComPtr<ID2D1Factory1> D2DFactory1;
    D2D1_FACTORY_OPTIONS factoryoptions = {};
#ifdef _DEBUG
    factoryoptions.debugLevel = D2D1_DEBUG_LEVEL_INFORMATION;
#endif
    hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, factoryoptions, D2DFactory1.GetAddressOf());//&D2DFactory1ではなくD2DFactory1.GetAddressOf()
    if (FAILED(hr))
        return hr;

    Microsoft::WRL::ComPtr<ID2D1Device> D2D1Device;
    hr = D2DFactory1->CreateDevice(DXGIDevice2.Get(), &D2D1Device);//DXGIDevice2ではなくDXGIDevice2.Get()、&D2D1Device 初期化
    if (FAILED(hr))
        return hr;

    hr = D2D1Device->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &m_D2DDeviceContext);//&m_D2DDeviceContext 初期化
    if (FAILED(hr))
        return hr;

    Microsoft::WRL::ComPtr<IDXGIAdapter> DXGIAdapter;
    hr = DXGIDevice2->GetAdapter(&DXGIAdapter);//&DXGIAdapter 初期化
    if (FAILED(hr))
        return hr;

    Microsoft::WRL::ComPtr<IDXGIFactory2> DXGIFactory2;
    hr = DXGIAdapter->GetParent(IID_PPV_ARGS(&DXGIFactory2));//&DXGIFactory2 初期化
    if (FAILED(hr))
        return hr;

    DXGI_SWAP_CHAIN_DESC1 desc = {};
    desc.Width = Window::GetClientWidth();
    desc.Height = Window::GetClientHeight();
    desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
    desc.Stereo = FALSE;
    desc.SampleDesc.Count = 1;
    desc.SampleDesc.Quality = 0;
    desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    desc.BufferCount = 2;
    desc.Scaling = DXGI_SCALING_STRETCH;
    desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
    desc.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED;

    hr = DXGIFactory2->CreateSwapChainForHwnd(D3DDevice.Get(), Window::GethWnd(), &desc, nullptr, nullptr, &m_DXGISwapChain1);//D3DDeviceではなくD3DDevice.Get()、&m_DXGISwapChain1 初期化
    if (FAILED(hr))
        return hr;

    (void)DXGIDevice2->SetMaximumFrameLatency(1);

    DXGIFactory2->MakeWindowAssociation(Window::GethWnd(), DXGI_MWA_NO_ALT_ENTER);//Alt+Enter時フルスクリーンを無効

    Microsoft::WRL::ComPtr<IDXGISurface2> DXGISurface2;
    hr = m_DXGISwapChain1->GetBuffer(0, IID_PPV_ARGS(&DXGISurface2));//&DXGISurface2 初期化
    if (FAILED(hr))
        return hr;

    hr = m_D2DDeviceContext->CreateBitmapFromDxgiSurface(DXGISurface2.Get(),//DXGISurface2ではなくDXGISurface2.Get()
        D2D1::BitmapProperties1(D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW,
            D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE)), &m_D2DBitmap1);//&m_D2DBitmap1 初期化
    if (FAILED(hr))
        return hr;

    m_D2DDeviceContext->SetTarget(m_D2DBitmap1.Get());//D2DBitmap1ではなくD2DBitmap1.Get()

    Microsoft::WRL::ComPtr<ID3D11Texture2D> D3DTexture2D;
    hr = m_DXGISwapChain1->GetBuffer(0, IID_PPV_ARGS(&D3DTexture2D));//&D3DTexture2D 初期化
    if (FAILED(hr))
        return hr;

    hr = D3DDevice->CreateRenderTargetView(D3DTexture2D.Get(), nullptr, &m_D3DRenderTargetView);//D3DTexture2DではなくD3DTexture2D.Get()、&m_D3DRenderTargetView 初期化
    if (FAILED(hr))
        return hr;

    m_D3DDeviceContext->OMSetRenderTargets(1, m_D3DRenderTargetView.GetAddressOf(), nullptr);//&m_D3DRenderTargetViewではなくm_D3DRenderTargetView.GetAddressOf()

    D3D11_VIEWPORT vp;
    vp.Width = (FLOAT)Window::GetClientWidth();
    vp.Height = (FLOAT)Window::GetClientHeight();
    vp.MinDepth = 0.0f;
    vp.MaxDepth = 1.0f;
    vp.TopLeftX = 0;
    vp.TopLeftY = 0;
    m_D3DDeviceContext->RSSetViewports(1, &vp);

    //バーテックスシェーダのコンパイル
    Microsoft::WRL::ComPtr<ID3DBlob> D3DBlobVS;
    hr = CompileShaderFromFile(L"VertexShader.hlsl", "main", "vs_5_0", &D3DBlobVS);
    if (FAILED(hr))
        return hr;

    //バーテックスシェーダの作成
    hr = D3DDevice->CreateVertexShader(D3DBlobVS->GetBufferPointer(), D3DBlobVS->GetBufferSize(), nullptr, &m_D3DVertexShader);
    if (FAILED(hr))
        return hr;

    //インプットレイアウトの定義
    D3D11_INPUT_ELEMENT_DESC layout[] =
    {
        //5番目のパラメータ:先頭からのバイト数4バイト(FLAOT)×3(RGB)=12
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
        { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    };
    UINT uiElements = ARRAYSIZE(layout);

    //インプットレイアウトの作成
    Microsoft::WRL::ComPtr<ID3D11InputLayout> D3DInputLayout;
    hr = D3DDevice->CreateInputLayout(layout, uiElements, D3DBlobVS->GetBufferPointer(),
        D3DBlobVS->GetBufferSize(), &D3DInputLayout);
    if (FAILED(hr))
        return hr;

    //インプットレイアウトの設定
    m_D3DDeviceContext->IASetInputLayout(D3DInputLayout.Get());

    //ピクセルシェーダのコンパイル
    Microsoft::WRL::ComPtr<ID3DBlob> D3DBlobPS;
    hr = CompileShaderFromFile(L"PixelShader.hlsl", "main", "ps_5_0", &D3DBlobPS);
    if (FAILED(hr))
        return hr;

    //ピクセルシェーダの作成
    hr = D3DDevice->CreatePixelShader(D3DBlobPS->GetBufferPointer(), D3DBlobPS->GetBufferSize(), nullptr, &m_D3DPixelShader);
    if (FAILED(hr))
        return hr;

    //バーテックスバッファの作成
    SimpleVertex vertices[] =
    {
        { DirectX::XMFLOAT3(-0.5f, 0.5f, 0.0f), DirectX::XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) },
        { DirectX::XMFLOAT3(0.5f, 0.5f, 0.0f), DirectX::XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f) },
        { DirectX::XMFLOAT3(-0.5f, -0.5f, 0.0f), DirectX::XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f) },
        { DirectX::XMFLOAT3(0.5f, -0.5f, 0.0f), DirectX::XMFLOAT4(1.0f, 1.0f, 0.0f, 1.0f) },
    };
    D3D11_BUFFER_DESC bd = {};
    bd.Usage = D3D11_USAGE_DYNAMIC;
    bd.ByteWidth = sizeof(SimpleVertex) * 4;
    bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;

    D3D11_SUBRESOURCE_DATA sub = {};
    sub.pSysMem = vertices;
    hr = D3DDevice->CreateBuffer(&bd, &sub, &m_D3DVertexBuffer);
    if (FAILED(hr))
        return hr;

    //バーテックスバッファの設定
    UINT uiStride = sizeof(SimpleVertex);
    UINT uiOffset = 0;
    m_D3DDeviceContext->IASetVertexBuffers(0, 1, m_D3DVertexBuffer.GetAddressOf(), &uiStride, &uiOffset);

    //インデックスバッファの作成
    Microsoft::WRL::ComPtr<ID3D11Buffer> D3DIndexBuffer;
    WORD indices[] =
    {
        0,1,2,
        2,1,3,
    };
    bd.Usage = D3D11_USAGE_DEFAULT;
    bd.ByteWidth = sizeof(WORD) * 6;
    bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
    bd.CPUAccessFlags = 0;
    sub.pSysMem = indices;
    hr = D3DDevice->CreateBuffer(&bd, &sub, &D3DIndexBuffer);
    if (FAILED(hr))
        return hr;

    //インデックスバッファの設定
    m_D3DDeviceContext->IASetIndexBuffer(D3DIndexBuffer.Get(), DXGI_FORMAT_R16_UINT, 0);

    //プリミティブトポロジの設定
    m_D3DDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

    //コンスタントバッファの作成
    bd.Usage = D3D11_USAGE_DYNAMIC;
    bd.ByteWidth = sizeof(ConstantBuffer);
    bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
    bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
    hr = D3DDevice->CreateBuffer(&bd, nullptr, &m_D3DConstantBuffer);
    if (FAILED(hr))
        return hr;

    //--------------------------★追加↓--------------------------
    //ラスタライザの作成
    Microsoft::WRL::ComPtr<ID3D11RasterizerState> D3DRasterizerState;
    D3D11_RASTERIZER_DESC ras = {};
    //D3D11_FILL_WIREFRAME(ワイヤーフレーム) D3D11_FILL_SOLID(ソリッド)
    ras.FillMode = D3D11_FILL_SOLID;
    //D3D11_CULL_NONE(カリングなし:裏表描画) D3D11_CULL_FRONT(表面カリング:裏面描画) D3D11_CULL_BACK(裏面カリング:表面描画)
    ras.CullMode = D3D11_CULL_NONE;
    hr = D3DDevice->CreateRasterizerState(&ras, &D3DRasterizerState);
    if (FAILED(hr))
        return hr;

    //ラスタライザの設定
    m_D3DDeviceContext->RSSetState(D3DRasterizerState.Get());
    //--------------------------★追加↑--------------------------

    //ワールドマトリックスの設定
    m_matWorld = DirectX::XMMatrixIdentity();

    //ビューマトリックスの設定
    DirectX::XMVECTOR vecEye = DirectX::XMVectorSet(0.0f, 0.0f, -3.0f, 0.0f);//カメラの位置//★---変更---
    DirectX::XMVECTOR vecFocus = DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);//カメラの焦点
    DirectX::XMVECTOR vecUp = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);//カメラの上方向
    m_matView = DirectX::XMMatrixLookAtLH(vecEye, vecFocus, vecUp);

    //プロジェクションマトリックスの設定
    m_matProjection = DirectX::XMMatrixPerspectiveFovLH(DirectX::XM_PIDIV4, static_cast<FLOAT>(Window::GetClientWidth()) / static_cast<FLOAT>(Window::GetClientHeight()), 0.01f, 100.0f);//★---変更---

    //シェーダのセット
    m_D3DDeviceContext->VSSetShader(m_D3DVertexShader.Get(), nullptr, 0);
    m_D3DDeviceContext->VSSetConstantBuffers(0, 1, m_D3DConstantBuffer.GetAddressOf());
    m_D3DDeviceContext->PSSetShader(m_D3DPixelShader.Get(), nullptr, 0);

    //------------------------------------------------------------
    // DirectWriteの初期化
    //------------------------------------------------------------
    Microsoft::WRL::ComPtr<IDWriteFactory> DWriteFactory;
    hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), &DWriteFactory);//&DWriteFactory 初期化
    if (FAILED(hr))
        return hr;

    //関数CreateTextFormat()
    //第1引数:フォント名(L"メイリオ", L"Arial", L"Meiryo UI"等)
    //第2引数:フォントコレクション(nullptr)
    //第3引数:フォントの太さ(DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_WEIGHT_BOLD等)
    //第4引数:フォントスタイル(DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STYLE_OBLIQUE, DWRITE_FONT_STYLE_ITALIC)
    //第5引数:フォントの幅(DWRITE_FONT_STRETCH_NORMAL,DWRITE_FONT_STRETCH_EXTRA_EXPANDED等)
    //第6引数:フォントサイズ(20, 30等)
    //第7引数:ロケール名(L"")
    //第8引数:テキストフォーマット(&g_pTextFormat)
    hr = DWriteFactory->CreateTextFormat(L"メイリオ", nullptr, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 20, L"", &m_DWriteTextFormat);//&m_DWriteTextFormat 初期化
    if (FAILED(hr))
        return hr;

    //関数SetTextAlignment()
    //第1引数:テキストの配置(DWRITE_TEXT_ALIGNMENT_LEADING:前, DWRITE_TEXT_ALIGNMENT_TRAILING:後, DWRITE_TEXT_ALIGNMENT_CENTER:中央)
    hr = m_DWriteTextFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING);
    if (FAILED(hr))
        return hr;

    //関数CreateSolidColorBrush()
    //第1引数:フォント色(D2D1::ColorF(D2D1::ColorF::Black):黒, D2D1::ColorF(D2D1::ColorF(0.0f, 0.2f, 0.9f, 1.0f)):RGBA指定)
    hr = m_D2DDeviceContext->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &m_D2DSolidBrush);//&m_D2DSolidBrush 初期化
    if (FAILED(hr))
        return hr;

    return S_OK;
}

//--------------------------------------------------------------------------------------
// DirectX11::Render():DirectX関係の描画
//--------------------------------------------------------------------------------------
void DirectX11::Render()
{
    m_D3DDeviceContext->ClearRenderTargetView(m_D3DRenderTargetView.Get(), DirectX::Colors::Aquamarine);//m_D3DRenderTargetViewではなくm_D3DRenderTargetView.Get()

    //------------------------------------------------------------
    //初期設定
    //------------------------------------------------------------
    static double dElapsedTime = 0;//経過時間

    //------------------------------------------------------------
    //計算
    //------------------------------------------------------------
    dElapsedTime += Window::GetFrameTime();

    //--------------------------★追加↓--------------------------
    static FLOAT fAngle = 0;
    fAngle += static_cast<FLOAT>(Window::GetFrameTime() / 1000.0);
    m_matWorld = DirectX::XMMatrixRotationY(fAngle);//Y軸回転

    static FLOAT fMoveX = 0;
    static int iMoveX = 0;
    if (iMoveX == 0)
    {
        fMoveX += static_cast<FLOAT>(Window::GetFrameTime() / 1000.0);
        if (fMoveX > 1.5f)
        {
            iMoveX = 1;
        }
    }
    else
    {
        fMoveX -= static_cast<FLOAT>(Window::GetFrameTime() / 1000.0);
        if (fMoveX < -1.5f)
        {
            iMoveX = 0;
        }
    }
    DirectX::XMMATRIX matMove;
    matMove = DirectX::XMMatrixTranslation(fMoveX, 0, 0);//X方向移動
    m_matWorld = m_matWorld * matMove;
    //--------------------------★追加↑--------------------------

    //------------------------------------------------------------
    // 文字操作
    //------------------------------------------------------------
    //FPS表示用
    WCHAR wcText1[256] = { 0 };
    swprintf(wcText1, 256, L"FPS=%lf", Window::GetFps());

    //------------------------------------------------------------
    // 3D描画
    //------------------------------------------------------------
    //頂点の書き替え
    D3D11_MAPPED_SUBRESOURCE msr;
    m_D3DDeviceContext->Map(m_D3DVertexBuffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &msr);
    SimpleVertex vertices[] =
    {
        { DirectX::XMFLOAT3(-0.5f, 0.5f, 0.0f), DirectX::XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) },
        { DirectX::XMFLOAT3(0.5f, 0.5f, 0.0f), DirectX::XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f) },
        { DirectX::XMFLOAT3(-0.5f, -0.5f, 0.0f), DirectX::XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f) },
        { DirectX::XMFLOAT3(0.5f, -0.5f, 0.0f), DirectX::XMFLOAT4(1.0f, static_cast<FLOAT>(fabs(cos(dElapsedTime / 1000))), 0.0f, 1.0f) },
    };
    memcpy(msr.pData, vertices, sizeof(vertices));
    m_D3DDeviceContext->Unmap(m_D3DVertexBuffer.Get(), 0);
    //カメラの更新
    ConstantBuffer cb;
    cb.world = DirectX::XMMatrixTranspose(m_matWorld);
    cb.view = DirectX::XMMatrixTranspose(m_matView);
    cb.projection = DirectX::XMMatrixTranspose(m_matProjection);
    m_D3DDeviceContext->Map(m_D3DConstantBuffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &msr);
    memcpy(msr.pData, (void*)(&cb), sizeof(cb));
    m_D3DDeviceContext->Unmap(m_D3DConstantBuffer.Get(), 0);
    //四角形の描画
    m_D3DDeviceContext->DrawIndexed(6, 0, 0);

    //------------------------------------------------------------
    // 2D描画
    //------------------------------------------------------------
    m_D2DDeviceContext->BeginDraw();
    m_D2DDeviceContext->DrawText(wcText1, ARRAYSIZE(wcText1) - 1, m_DWriteTextFormat.Get(), D2D1::RectF(0, 0, 800, 20), m_D2DSolidBrush.Get(), D2D1_DRAW_TEXT_OPTIONS_NONE);//m_DWriteTextFormatではなくm_DWriteTextFormat.Get()
    m_D2DDeviceContext->EndDraw();

    m_DXGISwapChain1->Present(0, 0);
}

(5)VertexShader.hlsl
cbuffer ConstantBuffer : register(b0)
{
    matrix world;
    matrix view;
    matrix projection;
}

struct VS_OUTPUT
{
    float4 pos : SV_POSITION;
    float4 color : COLOR0;
};

//--------------------------------------------------------------------------------------
// バーテックスシェーダ
//--------------------------------------------------------------------------------------
VS_OUTPUT main(float4 pos : POSITION, float4 color : COLOR)
{
    VS_OUTPUT output = (VS_OUTPUT)0;
    output.pos = mul(pos, world);
    output.pos = mul(output.pos, view);
    output.pos = mul(output.pos, projection);
    output.color = color;
    return output;
}

(6)PixelShader.hlsl
struct VS_OUTPUT
{
    float4 pos : SV_POSITION;
    float4 color : COLOR0;
};

//--------------------------------------------------------------------------------------
// ピクセルシェーダ
//--------------------------------------------------------------------------------------
float4 main(VS_OUTPUT input) : SV_TARGET
{
    return input.color;
}

★部分を追加または変更

3.四角形がY軸回転およびX方向移動する。

0 件のコメント:

コメントを投稿