Nanashi-soft○プログラマ専用○DirectX11開発○
//テクスチャー読み込みこれらをコンテキストに設定する部分を,メインループに突っ込みます
D3DX11_IMAGE_LOAD_INFO LoadInfo;
LoadInfo.Width = 0;
LoadInfo.Height = 0;
LoadInfo.Depth = 0;
LoadInfo.FirstMipLevel = 0;
LoadInfo.MipLevels = D3DX11_DEFAULT,
LoadInfo.Usage = D3D11_USAGE_DEFAULT;
LoadInfo.BindFlags = D3D11_BIND_SHADER_RESOURCE;
LoadInfo.CpuAccessFlags = 0;
LoadInfo.MiscFlags = 0;
LoadInfo.Format = DXGI_FORMAT_R32G32B32_FLOAT;
LoadInfo.Filter = D3DX11_FILTER_TRIANGLE;
LoadInfo.MipFilter = D3DX11_FILTER_TRIANGLE;
LoadInfo.pSrcInfo = NULL;
ID3D11ShaderResourceView* hpShaderResourceView = NULL;
if(FAILED(D3DX11CreateShaderResourceViewFromFile(hpDevice, TEXT("ira.siyoko.png"), &LoadInfo, NULL, &hpShaderResourceView, NULL))){
MessageBoxW(hWnd, L"D3DX11CreateShaderResourceViewFromFile", L"Err", MB_ICONSTOP);
goto End;
}
ID3D11ShaderResourceView* hpShaderResourceViews[] = { hpShaderResourceView };
//もう1枚読み込み
ID3D11ShaderResourceView* hpShaderResourceView2 = NULL;
if(FAILED(D3DX11CreateShaderResourceViewFromFile(hpDevice, TEXT("ira.meri-2hai.png"), &LoadInfo, NULL, &hpShaderResourceView2, NULL))){
MessageBoxW(hWnd, L"D3DX11CreateShaderResourceViewFromFile", L"Err", MB_ICONSTOP);
goto End;
}
ID3D11ShaderResourceView* hpShaderResourceViews2[] = { hpShaderResourceView2 };
//メインループそうすると,別々のテクスチャーが描画されました
MSG hMsg;
while(true){
while(PeekMessageW(&hMsg, NULL, 0, 0, PM_REMOVE)){
if(hMsg.message == WM_QUIT){
goto End;
}
TranslateMessage(&hMsg);
DispatchMessage(&hMsg);
}
//背景クリア
float ClearColor[] = {0.0f, 0.0f, 1.0f, 1.0f};
hpDeviceContext->ClearRenderTargetView(hpRenderTargetView, ClearColor);
//テクスチャー1をコンテキストに設定
hpDeviceContext->PSSetShaderResources(0, 1, hpShaderResourceViews);
//ワールド変換用行列を生成
XMMATRIX hWorld; //ワールド変換行列
//初期化
hWorld = XMMatrixIdentity();
XMMATRIX hRotate;
hRotate = XMMatrixTranslation(+0.5f, 0.0f, 0.0f);
hWorld = XMMatrixMultiply(hWorld, hRotate);
XMMATRIX hView; //ビュー変換行列
XMVECTOR hEye = XMVectorSet(0.0f, 0.0f, -2.0f, 0.0f); //カメラの位置
XMVECTOR hAt = XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f); //焦点の位置
XMVECTOR hUp = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
hView = XMMatrixLookAtLH(hEye, hAt, hUp);
XMMATRIX hProjection; //透視射影変換行列
hProjection = XMMatrixPerspectiveFovLH(D3DXToRadian(45.0f), 16.0f/9.0f, 0.0f, 1000.0f);
//それらをシェーダーに送る
struct ConstantBuffer
{
XMMATRIX mWorld;
XMMATRIX mView;
XMMATRIX mProjection;
};
//constantバッファ生成
ID3D11Buffer* hpConstantBuffer = NULL;
hBufferDesc.ByteWidth = sizeof(ConstantBuffer);
hBufferDesc.Usage = D3D11_USAGE_DEFAULT;
hBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
hBufferDesc.CPUAccessFlags = 0;
hBufferDesc.MiscFlags = 0;
hBufferDesc.StructureByteStride = sizeof(float);
if(FAILED(hpDevice->CreateBuffer(&hBufferDesc, NULL, &hpConstantBuffer))){
MessageBoxW(hWnd, L"Create ConstantBuffer", L"Err", MB_ICONSTOP);
goto End;
}
//ID3D11DeviceContext* g_pImmediateContext = NULL;
ConstantBuffer hConstantBuffer;
hConstantBuffer.mWorld = XMMatrixTranspose(hWorld);
hConstantBuffer.mView = XMMatrixTranspose(hView);
hConstantBuffer.mProjection = XMMatrixTranspose(hProjection);
hpDeviceContext->UpdateSubresource(hpConstantBuffer, 0, NULL, &hConstantBuffer, 0, 0);
hpDeviceContext->VSSetConstantBuffers(0, 1, &hpConstantBuffer);
//描画
hpDeviceContext->Draw(TYOUTEN, 0);
//テクスチャー2をコンテキストに設定
hpDeviceContext->PSSetShaderResources(0, 1, hpShaderResourceViews2);
hWorld = XMMatrixIdentity();
hRotate = XMMatrixRotationZ(D3DXToRadian(-45.0f));
hWorld = XMMatrixMultiply(hWorld, hRotate);
hRotate = XMMatrixTranslation(-0.5f, 0.0f, 0.0f);
hWorld = XMMatrixMultiply(hWorld, hRotate);
hConstantBuffer.mWorld = XMMatrixTranspose(hWorld);
hpDeviceContext->UpdateSubresource(hpConstantBuffer, 0, NULL, &hConstantBuffer, 0, 0);
hpDeviceContext->VSSetConstantBuffers(0, 1, &hpConstantBuffer);
//描画
hpDeviceContext->Draw(TYOUTEN, 0);
hpDXGISwpChain->Present(0, 0);
}