Nanashi-soft○プログラマ専用○DirectX11開発○
const int TYOUTEN = 4; //ポリゴンの頂点数この前,ベタっと画面に貼り付いていたポリゴンを,3D空間上に置いてみます
Vertex3D hVectorData[TYOUTEN] = {
//500x811
{ { -0.27f, +0.75f, +0.5f }, { 1.0f, 1.0f, 1.0f, 1.0f }, {0.0f, 0.0f} },
{ { +0.27f, +0.75f, +0.5f }, { 1.0f, 1.0f, 1.0f, 1.0f }, {1.0f, 0.0f} },
{ { -0.27f, -0.75f, +0.5f }, { 1.0f, 1.0f, 1.0f, 1.0f }, {0.0f, 1.0f} },
{ { +0.27f, -0.75f, +0.5f }, { 1.0f, 1.0f, 1.0f, 1.0f }, {1.0f, 1.0f} }
};
#include <xnamath.h>なんで XNAかって? だって,DirectX11サンプルに普通に使われていたから('-'*)
//ワールド変換用行列を生成他はわからんので,次いきます('-'*) ←
XMMATRIX hWorld; //ワールド変換行列
//初期化
hWorld = XMMatrixIdentity();
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);
#define D3DX_PI ((FLOAT) 3.141592654f)・遠近感のある座標
#define D3DXToRadian( degree ) ((degree) * (D3DX_PI / 180.0f))
#define D3DXToDegree( radian ) ((radian) * (180.0f / D3DX_PI))
XMMATRIX hProjection; //透視射影変換行列
hProjection = XMMatrixPerspectiveFovLH(D3DXToRadian(45.0f), 16.0f/9.0f, 0.0f, 1000.0f);
//入力用mulが行列を計算する関数だそうですよ(他人事)
struct vertexIn
{
float4 pos : POSITION0;
float4 col : COLOR0;
float2 tex : TEXCOORD0;
};
//出力用
struct vertexOut
{
float4 pos : SV_POSITION;
float4 col : COLOR;
float2 tex : TEXCOORD0;
};
//変換用行列
cbuffer ConstantBuffer : register( b0 )
{
matrix World; //ワールド変換行列
matrix View; //ビュー変換行列
matrix Projection; //透視射影変換行列
}
vertexOut vs_main(vertexIn IN)
{
vertexOut OUT;
OUT.pos = mul(IN.pos, World); //ワールド変換
OUT.pos = mul(OUT.pos, View); //ビュー変換
OUT.pos = mul(OUT.pos, Projection); //透視射影変換
OUT.col = IN.col;
OUT.tex = IN.tex;
return OUT;
}
//それらをシェーダーに送るこれを実行すると,3D空間に描画されます
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;
}
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);
XMMATRIX hRotate;そうすると,傾いた状態で描画されます
hRotate = XMMatrixRotationZ(D3DXToRadian(-45.0f));
hWorld = XMMatrixMultiply(hWorld, hRotate);
hRotate = XMMatrixRotationY(D3DXToRadian(-45.0f));遠近感でたでしょ?(そうか?)
hWorld = XMMatrixMultiply(hWorld, hRotate);
hEye = XMVectorSet(0.0f, 0.0f, -5.0f, 0.0f);ずずっと,後ろに下げると小さくなります
hView = XMMatrixLookAtLH(hEye, hAt, hUp);
hProjection = XMMatrixPerspectiveFovLH(D3DXToRadian(45.0f), 16.0f/9.0f, 0.0f, 1000.0f);