https://docs.microsoft.com/en-us/previous-versions//ff729724(v=vs.85)?redirectedfrom=MSDN
μ λ΄μ©μ μ°Έκ³ νμ¬ μ 리ν λ΄μ©μ λλ€.
ν μ€μ² 맀νμ 2D μ΄λ―Έμ§λ₯Ό 3D νμμ ν¬μνλ κ²μ λ§νλ€. 그리기 μν΄μλ νλ©΄μ μ λ€μ΄ 2D μ΄λ―Έμ§μ μ΄λ»κ² λμλλμ§ λͺ μν΄μΌ νλ€.
μ΄λ² νν 리μΌμ μμμμλ μ μ μ μν΄ ν μ€μ² μ’νκ° κ²°μ λλ©°, 보κ°λλ€.
ν μ€μ²λ μ °μ΄λ 리μμ€ λ·°λ₯Ό λ§λλ λ° μ¬μ©λλ 2D μ΄λ―Έμ§μ΄λ€.
hr = D3DX11CreateShaderResourceViewFromFile( g_pd3dDevice, L"seafloor.dds", NULL, NULL,
&g_pTextureRV, NULL );
μ
°μ΄λκ° νν°λ§(filtering), addressing λ±μ λ°©μμ μ μ΄νλ sampler stateλ₯Ό λ§λ€μ΄μΌ νλ€. ν
μ€μ³ addressingμ UVμ’νκ° 0κ³Ό 1 μ¬μ΄κ° μλ λ€λ₯Έ κ°μ΄ λμμ λ, μ΄λ»κ² ννλ κ²μΈ μ§λ₯Ό κ²°μ νλ μ΅μ
μ΄λ€.
sampler stateλ₯Ό μμ±νκΈ° μν΄ D3D11Device::CreateSamplerState()λ₯Ό νΈμΆνλ€.
// Create the sample state
D3D11_SAMPLER_DESC sampDesc;
ZeroMemory( &sampDesc, sizeof(sampDesc) );
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
sampDesc.MinLOD = 0;
sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
hr = g_pd3dDevice->CreateSamplerState( &sampDesc, &g_pSamplerLinear );
μ΄λ―Έμ§λ₯Ό νλΈμ 맀ννκΈ° μ μ λ¨Όμ νλΈμ κ° μ μ μ ν μ€μ² μ’νλ₯Ό μ μν΄μΌ νλ€. μ¬μ©λ μ’νκ³λ [0, 1]λ‘ νμ€ν λμλ€. (ν μ€μ²μ μΌμͺ½ μλ¨ λͺ¨μ리λ (0,0), μ€λ₯Έμͺ½ νλ¨ λͺ¨μ리λ (1,1))
λ¨Όμ μ μ μ μ μν λ ν μ€μ² μ’νλ₯Ό ν¬ν¨μμΌ μ£Όμλ€.
struct SimpleVertex
{
XMFLOAT3 Pos;
XMFLOAT2 Tex;
};
κ·Έλ° λ€μ μ °μ΄λμ input layoutλ μ λ°μ΄νΈνμ¬ ν¬ν¨μμΌμ£Όμλ€.
// Define the input layout
D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
input layoutμ΄ λ³κ²½λμμΌλ―λ‘ ν΄λΉ μ μ μ °μ΄λ μ λ ₯λ μΌμΉνλλ‘ μμ ν΄μΌ νλ€.
struct VS_INPUT
{
float4 Pos : POSITION;
float2 Tex : TEXCOORD;
};
λ§μ§λ§μΌλ‘ μ μ μ λμλλ ν μ€μ² μ’νλ₯Ό μ λ ₯ν΄μ€λ€. νλΈμ κ° κΌμ§μ μ ν μ€μ²μ λͺ¨μ리μ ν΄λΉνλ€. μ΄λ κ² νλ©΄ κ° κΌμ§μ μ΄ (0,0) (0,1) (1,0) λλ (1,1)μ μ’νλ‘ κ°λ λ¨μ 맀νμ΄ λ§λ€μ΄μ§λλ€.
// Create vertex buffer
SimpleVertex vertices[] =
{
{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
};
ν μ€μ²μ sampler stateλ μ΄μ νν 리μΌμμ μ΄ν΄λ³Έ Constantμ κ°μ κ°μ²΄μ΄λ€. μ °μ΄λκ° μ¬μ©νκΈ° μ μ ID3D11DeviceContext::PSSetSamplers(), ID3D11DeviceContext::PSSetShaderResources() λ±μ νΈμΆν΄μ£Όμ΄μΌ νλ€.
g_pImmediateContext->PSSetShaderResources( 0, 1, &g_pTextureRV );
g_pImmediateContext->PSSetSamplers( 0, 1, &g_pSamplerLinear );
μ΄λ κ² νλ©΄ μ °μ΄λμμ ν μ€μ²λ₯Ό μ¬μ©ν μ€λΉκ° λλλ€.
ν μ€μ²λ₯Ό 맀ννκΈ° μν΄ ν½μ μ °μ΄λμμ texture lookup functionμ νΈμΆνλ€. 2D ν μ€μ²λ₯Ό μ‘°νν λ€μ μν μμμ λ°ννλ€.
μλ ν½μ μ °μ΄λλ μ΄ ν¨μλ₯Ό νΈμΆνμ¬ λ©μ μμ(λλ λ¨Έν°λ¦¬μΌ μμ)μΌλ‘ κ³±ν λ€μ μ΅μ’ μμμ μΆλ ₯νλ€.
// Pixel Shader
float4 PS( PS_INPUT input) : SV_Target
{
return txDiffuse.Sample( samLinear, input.Tex ) * vMeshColor;
}
μ μ μ °μ΄λλ₯Ό ν΅ν΄ ν μ€μ² μ’νλ₯Ό μ λ¬ν΄μΌ νλ€.
// Vertex Shader
PS_INPUT VS( VS_INPUT input )
{
PS_INPUT output = (PS_INPUT)0;
output.Pos = mul( input.Pos, World );
output.Pos = mul( output.Pos, View );
output.Pos = mul( output.Pos, Projection );
output.Tex = input.Tex;
return output;
}
Direct3D 11μμ Constant Bufferλ₯Ό μ¬μ©νμ¬ μ °μ΄λ μμ(μ °μ΄λ λ³μ)λ₯Ό μ€μ ν μ μλ€. Constant Bufferλ μ °μ΄λ μμλ₯Ό μ λ°μ΄νΈνλλ° νμν bandwidthλ₯Ό μ€μΈλ€.
μ΄μ νν 리μΌμμλ νλμ Constant Bufferλ₯Ό μ΄μ©νμ¬ νμν λͺ¨λ μ °μ΄λ μμλ₯Ό 보κ΄νλ€. κ·Έλ¬λ Constant Bufferλ₯Ό ν¨μ¨μ μΌλ‘ μ¬μ©νλ κ°μ₯ μ’μ λ°©λ²μ μ °μ΄λ λ³μλ₯Ό μ λ°μ΄νΈ λΉλμ λ°λΌ ꡬμ±νλ κ²μ΄λ€. μ΄λ₯Ό ν΅ν΄ μ΄ν리μΌμ΄μ μ μ °μ΄λ μμλ₯Ό μ λ°μ΄νΈ νλλ° νμν bandwidthλ₯Ό μ΅μν ν μ μλ€.
μλ₯Ό λ€μ΄ μ΄ νν 리μΌμμλ μμλ₯Ό μΈκ°μ§ κ΅¬μ‘°λ‘ κ·Έλ£Ήν νμλ€.
νλλ λͺ¨λ νλ μμμ λ³κ²½λλ λ³μ, λ€λ₯Έ νλλ μ°½ ν¬κΈ°κ° λ³κ²½λ λλ§ λ³κ²½λλ λ³μ, λ€λ₯Έ νλλ μ²μ ν λ² μ€μ λ ν λ³κ²½λμ§ μλ λ³μμ΄λ€.
cbuffer cbNeverChanges
{
matrix View;
};
cbuffer cbChangeOnResize
{
matrix Projection;
};
cbuffer cbChangesEveryFrame
{
matrix World;
float4 vMeshColor;
};
Constant Bufferλ₯Ό μ¬μ©νλ €λ©΄ κ° λ²νΌμ λν΄ ID3D11Buffer κ°μ²΄λ₯Ό μμ±ν΄μΌ νλ€. κ·Έλ° λ€μ ID3D11DeviceContext::UpdateSubresource()λ₯Ό νΈμΆνμ¬ νμν κ²½μ° λ€λ₯Έ μμ λ²νΌμ μν₯μ λ―ΈμΉμ§ μκ³ κ° μμ λ²νΌλ₯Ό μ λ°μ΄νΈ ν μ μλ€.
//
// Update variables that change once per frame
//
CBChangesEveryFrame cb;
cb.mWorld = XMMatrixTranspose( g_World );
cb.vMeshColor = g_vMeshColor;
g_pImmediateContext->UpdateSubresource( g_pCBChangesEveryFrame, 0, NULL, &cb, 0, 0 );