๐Ÿ“˜ Direct3D Tutorial5

ํŒŒ์ธยท2022๋…„ 2์›” 9์ผ
0

Directx

๋ชฉ๋ก ๋ณด๊ธฐ
5/8
post-thumbnail

https://docs.microsoft.com/en-us/previous-versions//ff729722(v=vs.85)?redirectedfrom=MSDN
์œ„ ๋‚ด์šฉ์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

Tutorial5 : ๋‹ค๋ฅธ ๊ฐœ์ฒด ๊ณต์ „


๐Ÿ“Œ Transformation (๋ณ€ํ™˜)

์ •์ ์—์„œ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋ณ€ํ™˜์—๋Š” 3๊ฐ€์ง€ ์œ ํ˜•์ด ์žˆ๋‹ค.
์ด๋™, ํšŒ์ „, ์Šค์ผ€์ผ๋ง.
XNA Math ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์ด๋™, ํšŒ์ „, ์Šค์ผ€์ผ๋ง, ์›”๋“œ-๋ทฐ ๋ณ€ํ™˜, ๋ทฐ-ํˆฌ์˜ ๋ณ€ํ™˜๊ณผ ๊ฐ™์€ API๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค.


๐Ÿ“Œ Translation (์ด๋™)

๐Ÿ”Ž ์ด๋™์— ์‚ฌ์šฉ๋˜๋Š” ํ–‰๋ ฌ

    1  0  0  0
    0  1  0  0
    0  0  1  0
    a  b  c  1

์—ฌ๊ธฐ์„œ (a, b, c)๋Š” ์ด๋™ํ•  ๋ฐฉํ–ฅ๊ณผ ๊ฑฐ๋ฆฌ๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฒกํ„ฐ์ด๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด x์ถ•์œผ๋กœ -5๋งŒํผ ์ด๋™ํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด ์•„๋ž˜ ํ–‰๋ ฌ์„ ๊ณฑํ•˜๋ฉด ๋œ๋‹ค.

๐Ÿ”Ž x์ถ•์œผ๋กœ -5๋งŒํผ ์ด๋™

    1  0  0  0
    0  1  0  0
    0  0  1  0
   -5  0  0  1   


๐Ÿ“Œ Rotation (ํšŒ์ „)

ํšŒ์ „์€ ์›์ ์„ ํ†ต๊ณผํ•˜๋Š” X, Y, Z ์ถ•์„ ์ค‘์‹ฌ์œผ๋กœ ์ผ์–ด๋‚œ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด [1 0]๋ฅผ ๋ฐ˜์‹œ๊ณ„๋กœ 90๋„ ํšŒ์ „์‹œํ‚ค๋ฉด [0 1]์ด ๋œ๋‹ค.

๐Ÿ”Ž y์ถ•์„ ๊ธฐ์ค€์œผ๋กœ ์‹œ๊ณ„ ๋ฐฉํ–ฅ์œผ๋กœ i๋„ ํšŒ์ „์‹œํ‚ค๋Š” ํ–‰๋ ฌ

    cosฮ  0  -sinฮ   0
     0    1     0    0
    sinฮ  0   cosฮ  0
     0    0     0    1

๐Ÿ“Œ Scaling (์Šค์ผ€์ผ๋ง)

์Šค์ผ€์ผ๋ง์€ ์ถ• ๋ฐฉํ–ฅ์„ ๋”ฐ๋ผ ํ™•๋Œ€ํ•˜๊ฑฐ๋‚˜ ์ถ•์†Œํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.
๋ชจ๋“  ๋ฐฉํ–ฅ์œผ๋กœ ํฌ๊ธฐ๋ฅผ ์กฐ์ •ํ• ์ˆ˜๋„ ์žˆ๊ณ  ํ•˜๋‚˜์˜ ์ถ•๋งŒ ๋”ฐ๋ผ ํฌ๊ธฐ๋ฅผ ์กฐ์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

๐Ÿ”Ž ์Šค์ผ€์ผ๋ง ์‹œํ‚ค๋Š” ํ–‰๋ ฌ

    p  0  0  0
    0  q  0  0
    0  0  r  0
    0  0  0  1

๐Ÿ“Œ Multiple Transformations (๋‹ค์ค‘ ๋ณ€ํ™˜)

๋ฒกํ„ฐ์— ๋‹ค์ค‘ ๋ณ€ํ™˜์„ ์ ์šฉํ•˜๋ ค๋ฉด ์ฒซ ๋ฒˆ์งธ ๋ณ€ํ™˜ ํ–‰๋ ฌ์„ ๊ณฑํ•œ ๋‹ค์Œ ๊ทธ ํ–‰๋ ฌ์— ๋‘ ๋ฒˆ์งธ ๋ณ€ํ™˜ ํ–‰๋ ฌ์„ ๊ณฑํ•˜๋ฉด ๋œ๋‹ค.


๐Ÿ“Œ Creating the Orbit (๊ถค๋„ ์ƒ์„ฑํ•˜๊ธฐ)

์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ์ œ์ž๋ฆฌ์—์„œ ํšŒ์ „ํ•˜๋Š” ํ๋ธŒ์™€, ์ž์‹ ์˜ ์ถ•์— ๋Œ€ํ•ด ํšŒ์ „ํ•จ๊ณผ ๋™์‹œ์— ๋‹ค๋ฅธ ํ๋ธŒ๋ฅผ ํšŒ์ „ ํ•˜๋Š” ํ๋ธŒ 2๊ฐœ๋ฅผ ๋งŒ๋“ค ๊ฒƒ์ด๋‹ค.

XNA Math์— ํšŒ์ „, ์ด๋™ ๋ฐ ์Šค์ผ€์ผ๋ง ํ–‰๋ ฌ์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” ๊ธฐ๋Šฅ์ด ์žˆ๋‹ค.

  • XMMatrixRotationX, XMMatrixRotationY, XMMatrixRotationZ : x์ถ•, y์ถ•, z์ถ• ์ค‘์‹ฌ ํšŒ์ „
  • XMMatrixTranslation : ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ์ ์„ ์ด๋™
  • XMMatrixScaling : ์Šค์ผ€์ผ๋ง. ๊ธฐ๋ณธ ์ถ•์„ ๋”ฐ๋ผ์„œ๋งŒ ์กฐ์ •๋˜๋ฉฐ ์ž„์˜์˜ ์ถ•์„ ๋”ฐ๋ผ ์Šค์ผ€์ผ๋ง์„ ํ•˜๊ณ  ์‹ถ์œผ๋ฉด ์Šค์ผ€์ผ๋ง ํ–‰๋ ฌ์— ์ ์ ˆํ•œ ํšŒ์ „ ํ–‰๋ ฌ์„ ๊ณฑํ•˜๋ฉด ๋œ๋‹ค.

์ฒซ ๋ฒˆ์งธ ์ •์œก๋ฉด์ฒด๋Š” ์ œ์ž๋ฆฌ์—์„œ ํšŒ์ „ํ•˜๊ณ  ๊ถค๋„์˜ ์ค‘์‹ฌ ์—ญํ• ์„ ํ•  ๊ฒƒ์ด๋‹ค. XMMatrixRotationY ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋œ๋‹ค. ํ๋ธŒ๋Š” ๊ฐ ํ”„๋ ˆ์ž„์— ์„ค์ •๋œ ์–‘๋งŒํผ ํšŒ์ „ํ•œ๋‹ค.

    // 1st Cube: Rotate around the origin
    g_World1 = XMMatrixRotationY( t );

๋‘ ๋ฒˆ์งธ ์ •์œก๋ฉด์ฒด๋Š” ์ฒซ ๋ฒˆ์งธ ์ •์œก๋ฉด์ฒด ์ฃผ์œ„๋ฅผ ๊ณต์ „ํ•œ๋‹ค.
๋จผ์ € ์ •์œก๋ฉด์ฒด์˜ ํฌ๊ธฐ๊ฐ€ 30%๋กœ ์ถ•์†Œ๋œ ๋‹ค์Œ, ์ถ•(์ด ๊ฒฝ์šฐ z์ถ•)์„ ๋”ฐ๋ผ ํšŒ์ „ํ•œ๋‹ค.
๊ถค๋„๋ฅผ ๋Œ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์›์ ์—์„œ ๋ฉ€๋ฆฌ ๋–จ์–ด์ง„ ๊ณณ์œผ๋กœ ์ด๋™์‹œํ‚จ๋‹ค์Œ y์ถ•์„ ๋”ฐ๋ผ ํšŒ์ „ํ•œ๋‹ค.
์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋Š” 4๊ฐœ์˜ ํ–‰๋ ฌ(mScale, mSpin, mTranslate, mOrbit)์˜ ๊ณฑ์œผ๋กœ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

    // 2nd Cube:  Rotate around origin
    XMMATRIX mSpin = XMMatrixRotationZ( -t );
    XMMATRIX mOrbit = XMMatrixRotationY( -t * 2.0f );
    XMMATRIX mTranslate = XMMatrixTranslation( -4.0f, 0.0f, 0.0f );
    XMMATRIX mScale = XMMatrixScaling( 0.3f, 0.3f, 0.3f );
    g_World2 = mScale * mSpin * mTranslate * mOrbit;

t : ์‹œ๊ฐ„

์‹œ๊ฐ„์„ ์ด์šฉํ•˜์—ฌ ์—…๋ฐ์ดํŠธ๋ฅผ ์‹œํ‚ค๋ฏ€๋กœ t ๊ฐ’์„ ์ฆ๊ฐ€์‹œ์ผœ ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

    // Update our time
    t += XM_PI * 0.0125f;

๋ Œ๋”๋ง์„ ํ•˜๊ธฐ ์ „์— ์…ฐ์ด๋”์— ๋Œ€ํ•ด ์ƒ์ˆ˜ ๋ฒ„ํผ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•ด์•ผ ํ•œ๋‹ค. ์›”๋“œ ๋ณ€ํ™˜ ํ–‰๋ ฌ์€ ํ๋ธŒ๋งˆ๋‹ค ๊ณ ์œ ํ•˜๋ฏ€๋กœ ๋ชจ๋“  ํ๋ธŒ์— ๊ฐ๊ฐ ์ ์šฉ๋œ๋‹ค.

    //
    // Update variables for the first cube
    //
    ConstantBuffer cb1;
    cb1.mWorld = XMMatrixTranspose( g_World1 );
    cb1.mView = XMMatrixTranspose( g_View );
    cb1.mProjection = XMMatrixTranspose( g_Projection );
    g_pImmediateContext->UpdateSubresource( g_pConstantBuffer, 0, NULL, &cb1, 0, 0 );

    //
    // Render the first cube
    //
    g_pImmediateContext->VSSetShader( g_pVertexShader, NULL, 0 );
    g_pImmediateContext->VSSetConstantBuffers( 0, 1, &g_pConstantBuffer );
    g_pImmediateContext->PSSetShader( g_pPixelShader, NULL, 0 );
    g_pImmediateContext->DrawIndexed( 36, 0, 0 );

    //
    // Update variables for the second cube
    //
    ConstantBuffer cb2;
    cb2.mWorld = XMMatrixTranspose( g_World2 );
    cb2.mView = XMMatrixTranspose( g_View );
    cb2.mProjection = XMMatrixTranspose( g_Projection );
    g_pImmediateContext->UpdateSubresource( g_pConstantBuffer, 0, NULL, &cb2, 0, 0 );

    //
    // Render the second cube
    //
    g_pImmediateContext->DrawIndexed( 36, 0, 0 );

๐Ÿ“Œ The Depth Buffer (๊นŠ์ด ๋ฒ„ํผ)

Depth Buffer๊ฐ€ ์—†๋‹ค๋ฉด ๊ถค๋„๋ฅผ ๋„๋Š” ์ž‘์€ ์ •์œก๋ฉด์ฒด๊ฐ€ ๋’ค๋ฅผ ๋Œ ๋•Œ ๋‹ค๋ฅธ ์ •์œก๋ฉด์ฒด ๋’ค๊ฐ€ ์•„๋‹Œ ์•ž์— ๊ทธ๋ ค์งˆ ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ ๊นŠ์ด ๋ฒ„ํผ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Direct3D ํ™”๋ฉด์— ๊ทธ๋ ค์ง„ ๋ชจ๋“  ํ”ฝ์…€์˜ ๊นŠ์ด๋ฅผ ํŒ๋‹จํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
Direct3D 11์˜ ๊นŠ์ด ๋ฒ„ํผ๋Š” ํ™”๋ฉด์— ๊ทธ๋ ค์ง€๋Š” ๋ชจ๋“  ํ”ฝ์…€์„ ํ•ด๋‹น ํ™”๋ฉด ๊ณต๊ฐ„ ํ”ฝ์…€์˜ ๊นŠ์ด ๋ฒ„ํผ์— ์ €์žฅ๋œ ๊ฐ’๊ณผ ๋น„๊ตํ•˜์—ฌ ํ™•์ธํ•œ๋‹ค.
๋ Œ๋”๋ง ๋˜๋Š” ํ”ฝ์…€์˜ z๊ฐ’์ด ๋ฒ„ํผ์— ์ด๋ฏธ ์žˆ๋Š” ๊ฐ’๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด ํ”ฝ์…€์ด ๊ทธ๋ ค์ง€๊ณ , Depth Buffer์˜ ๊ฐ’์ด ์ƒˆ๋กœ ๊ทธ๋ฆฐ ํ”ฝ์…€์˜ ๊นŠ์ด๋กœ ์—…๋ฐ์ดํŠธ ๋œ๋‹ค. ๋ฐ˜๋ฉด ๊ทธ๋ฆฌ๋ ค๋Š” ํ”ฝ์…€์˜ z๊ฐ’์ด Depth Buffer์— ๊ธฐ์กด์— ์žˆ๋Š” ๊ฐ’๋ณด๋‹ค ํฌ๋ฉด ํ”ฝ์…€์€ ๋ฒ„๋ ค์ง€๊ณ  ๊นŠ์ด ๋ฒ„ํผ์˜ z ๊ฐ’์€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š”๋‹ค.

๐Ÿ”Ž Depth Buffer์˜ DepthStencilView๋ฅผ ์ž‘์„ฑํ•˜์—ฌ Depth Stencil texture๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ํ•œ๋‹ค.

    // Create depth stencil texture
    D3D11_TEXTURE2D_DESC descDepth;
    ZeroMemory( &descDepth, sizeof(descDepth) );
    descDepth.Width = width;
    descDepth.Height = height;
    descDepth.MipLevels = 1;
    descDepth.ArraySize = 1;
    descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
    descDepth.SampleDesc.Count = 1;
    descDepth.SampleDesc.Quality = 0;
    descDepth.Usage = D3D11_USAGE_DEFAULT;
    descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
    descDepth.CPUAccessFlags = 0;
    descDepth.MiscFlags = 0;
    hr = g_pd3dDevice->CreateTexture2D( &descDepth, NULL, &g_pDepthStencil );
    if( FAILED(hr) )
        return hr;

    // Create the depth stencil view
    D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
    ZeroMemory( &descDSV, sizeof(descDSV) );
    descDSV.Format = descDepth.Format;
    descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
    descDSV.Texture2D.MipSlice = 0;
    hr = g_pd3dDevice->CreateDepthStencilView( g_pDepthStencil, &descDSV, &g_pDepthStencilView );
    if( FAILED(hr) )
        return hr;

์ƒˆ๋กœ ๋งŒ๋“  Depth Stencil Buffer๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด device์— ๋ฐ”์ธ๋”ฉํ•ด์•ผ ํ•œ๋‹ค. ์ด ์ž‘์—…์€ Depth Stencil View๋ฅผ OMSetRenderTargets ํ•จ์ˆ˜์˜ ์„ธ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•˜์—ฌ ์ˆ˜ํ–‰๋œ๋‹ค.

    g_pImmediateContext->OMSetRenderTargets( 1, &g_pRenderTargetView, g_pDepthStencilView );

๋ Œ๋” ํƒ€๊ฒŸ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ Œ๋”๋ง ์ „์— Depth Buffer๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•œ๋‹ค.

    //
    // Clear the depth buffer to 1.0 (max depth)
    //
profile
๊ณต๋ถ€์ •๋ฆฌ์šฉ

0๊ฐœ์˜ ๋Œ“๊ธ€