그래픽스/DirectX

[DX11] 튜토리얼 2 - 삼각형 렌더링

ShovelingLife 2023. 7. 12. 11:41

우선적으로 봐야할게 ID3D11DeviceContext*에 대한 포인터 변수다 /

디바이스 컨텍스트란? 디스플레이 또는 프린터와 같은 디바이스의 그리기 특성에 대한 정보를 포함하는 Windows 데이터 구조다. 모든 그리기 호출은 선, 도형 및 텍스트를 그리기 위해 Windows API를 캡슐화하는 디바이스 컨텍스트 개체를 통해 수행된다.

ID3D11DeviceContext*    g_pImmediateContext = NULL;

아래는 InitDevice 함수에 해당하는 내 / 렌더 타겟 뷰 개념 참조

// 하나 이상의 렌더링 대상을 원자성으로 바인딩하고 깊이 스텐실 버퍼를 
// 출력-병합 단계에 바인딩합니다.
g_pImmediateContext->OMSetRenderTargets( 1, &g_pRenderTargetView, NULL );

// 뷰포트 배열을 파이프라인의 래스터라이저 단계에 바인딩합니다.
g_pImmediateContext->RSSetViewports( 1, &vp );

// 입력 레이아웃 개체를 입력 어셈블러 단계에 바인딩합니다.
g_pImmediateContext->IASetInputLayout( g_pVertexLayout );

// 꼭짓점 버퍼 배열을 입력 어셈블러 단계에 바인딩합니다.
g_pImmediateContext->IASetVertexBuffers( 0, 1, &g_pVertexBuffer, &stride, &offset );

// 기본 형식 및 입력 어셈블러 단계에 대한 입력 데이터를 설명하는 데이터 
// 순서에 대한 정보를 바인딩합니다.
g_pImmediateContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );

HLSL 파일을 사용해서 버텍스 셰이더 > 세모 크기와 픽셀 셰이더 > 색상을 지정한다

//--------------------------------------------------------------------------------------
// File: Tutorial02.fx
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
float4 VS( float4 Pos : POSITION ) : SV_POSITION
{
    return Pos;
}

//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PS( float4 Pos : SV_POSITION ) : SV_Target
{
    return float4( 1.0f, 1.0f, 0.0f, 1.0f );    // Yellow, with Alpha = 1
}

이걸 가지고서 버퍼를 초기화한다, 이 또한 InitDevice 함수에서 진행된다

// 여기서 CompileShaderFromFile 함수에서 파라미터로 넘어가는 두번째 인자는
// 위 셰이더 파일 함수에 해당되는 명칭임.

// Compile the vertex shader
ID3DBlob* pVSBlob = NULL;
hr = CompileShaderFromFile( L"Tutorial02.fx", "VS", "vs_4_0", &pVSBlob );
if( FAILED( hr ) )
{
    MessageBox( NULL,
                L"The FX file cannot be compiled.  Please run this executable from the directory that contains the FX file.", L"Error", MB_OK );
    return hr;
}

Create the vertex shader
= g_pd3dDevice->CreateVertexShader( pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, &g_pVertexShader );
 FAILED( hr ) )

SBlob->Release();
    return hr;


// Compile the pixel shader
ID3DBlob* pPSBlob = NULL;
hr = CompileShaderFromFile( L"Tutorial07.fx", "PS", "ps_4_0", &pPSBlob );
if( FAILED( hr ) )
{
    MessageBox( NULL,
                L"The FX file cannot be compiled.  Please run this executable from the directory that contains the FX file.", L"Error", MB_OK );
    return hr;
}

// Create the pixel shader
hr = g_pd3dDevice->CreatePixelShader( pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &g_pPixelShader );
pPSBlob->Release();
if( FAILED( hr ) )
    return hr;

 

그리고 winMain 함수에서 호출하고 있는 Render() 함수

스왑체인은 더블 버퍼링 방지용이라고 보면 됨 따라서 여기에 등록하면 n장을 출력함

void Render()
{
    // Clear the back buffer 
    float ClearColor[4] = { 0.0f, 0.125f, 0.3f, 1.0f }; // red,green,blue,alpha
    g_pImmediateContext->ClearRenderTargetView( g_pRenderTargetView, ClearColor );

    // Render a triangle
	g_pImmediateContext->VSSetShader( g_pVertexShader, NULL, 0 );
	g_pImmediateContext->PSSetShader( g_pPixelShader, NULL, 0 );
    g_pImmediateContext->Draw( 3, 0 );

    // Present the information rendered to the back buffer to the front buffer (the screen)
    g_pSwapChain->Present( 0, 0 );
}