DirectX Graphics 프로그래밍 가이드 프로그래밍 가능한 파이프라인 픽셀 셰이더 픽셀 셰이더의 예   [목차열람] [주소복사] [슬롯비우기]
픽셀 셰이더의 예
 
Microsoft DirectX 9.0

픽셀 셰이더의 예


여기에서는, 픽셀 셰이더의 예를 3 개 소개한다. 각 예는, 전의 예를 기본으로 해, 각각 필요한 픽셀 셰이더 기능을 추가한 것이다.

텍스처 맵의 적용

이 예에서는, 평면에 텍스처 맵을 적용한다. 이 예와 전의 예의 차이는 다음과 같다.

샘플 코드를 다음에 나타낸다.

// Define vertex data structure.
struct CUSTOMVERTEX
{
    FLOAT x, y, z;
    FLOAT u1, v1;
};

// Define corresponding FVF macro.
#define D3DFVF_CUSTOMVERTEX (
                 D3DFVF_XYZ|D3DFVF_TEX|D3DFVF_TEXCOORDSIZE2(0))

// Create vertex data with position and texture coordinates.
static CUSTOMVERTEX g_Vertices[]=
{
    //  x      y     z     u1    v1
    { -1. 0f, -1. 0f, 0.0f,  0, 1, }, 
    {  1.0f, -1. 0f, 0.0f,  1, 1, }, 
    {  1.0f,  1.0f, 0.0f,  1, 0, }, 
    { -1. 0f,  1.0f, 0.0f,  0, 0, }, 
    // v1 is flipped to meet the top down convention in Windows.
    // The upper left texture coordinate is (0,0).
    // The lower right texture coordinate is (1,1).  
};

// Create a texture.This file is in the DirectX 9.0
//   media from the SDK download.
TCHAR        strTexturePath[512];
DXUtil_FindMediaFileCb( strTexturePath, sizeof(strTexturePath), 
                        _T("DX5_Logo.bmp") );

LPDIRECT3DTEXTURE9      m_pTexture0;
D3DUtil_CreateTexture( m_pd3dDevice, strTexturePath, 
                       &m_pTexture0, D3DFMT_R5G6B5 );

// Create the pixel shader.
TCHAR        strShaderPath[512];
DXUtil_FindMediaFileCb( strShaderPath, sizeof(strShaderPath), 
                        _T("PixelShader2.txt") );

이 함수는,샘플 프레임워크(framework)에 의해 사용되는 헬퍼-함수이다. 샘플의 상당수는, 이 샘플 프레임워크(framework)을 베이스로 구축되고 있다.

LPD3DXBUFFER pCode;                  // buffer with the assembled shader code
LPD3DXBUFFER pErrorMsgs;             // buffer with error messages
LPDIRECT3DPIXELSHADER9 m_pPixelShader;
D3DXAssembleShaderFromFile( strShaderPath, 0, NULL, &pCode, NULL );
m_pd3dDevice->CreatePixelShader( (DWORD*) pCode->GetBufferPointer(),
                                          &m_pPixelShader );
m_pd3dDevice->SetPixelShader( m_pPixelShader );

// Load the texture and render the output pixels.
m_pd3dDevice->SetTexture( 0, m_pTexture0 );	
m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2 );

// Contents of the file "PixelShader2.txt"
// Applies a texture map to object vertices.
ps_1_1      // Version instruction must be first in the file.
tex t0      // Declare texture register t0
            // which is loaded from stage 0.
mov r0, t0  // Move the texture register data(t0) to the 
            // output register(r0).

이 예의 결과의 이미지를 다음에 나타낸다.

텍스처 맵의 예

디퓨즈 정점색과 텍스처의 블렌드

이 예에서는, 텍스처 맵내의 색을 정점색과 블렌드 (곱셈) 한다. 이 예와 전의 예의 차이는 다음과 같다.

텍스처의 생성과 로드의 코드는 같다. 전체를 나타내기 위해서(때문에), 그 코드도 여기에 포함되어 있다.

struct CUSTOMVERTEX
{
    FLOAT x, y, z;
    DWORD color1;
    FLOAT tu1, tv1;
};

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1|
                             D3DFVF_TEXCOORDSIZE2(0))
static CUSTOMVERTEX g_Vertices[]=
{
    //  x      y     z     diffuse     u1    v1
    { -1. 0f, -1. 0f, 0.0f, 0xffff0000, 0, 1, }, // red
    {  1.0f, -1. 0f, 0.0f, 0xff00ff00, 1, 1, }, // green
    {  1.0f,  1.0f, 0.0f, 0xff0000ff, 1, 0, }, // blue
    { -1. 0f,  1.0f, 0.0f, 0xffffffff, 0, 0, }, // white
    // v1 is flipped to meet the top down convention in Windows
    // the upper left texture coordinate is (0,0)
    // the lower right texture coordinate is (1,1).  
};

// Create a texture.This file is in the DirectX 9.0 media 
//   from the SDK download.
// Create a texture.This file is in the DirectX 9.0
//   media from the SDK download.
TCHAR        strTexturePath[512];
DXUtil_FindMediaFileCb( strTexturePath, sizeof(strTexturePath), 
                        _T("DX5_Logo.bmp") );

LPDIRECT3DTEXTURE9      m_pTexture0;
D3DUtil_CreateTexture( m_pd3dDevice, strTexturePath, 
                       &m_pTexture0, D3DFMT_R5G6B5 );

// Create the pixel shader.
TCHAR        strShaderPath[512];
DXUtil_FindMediaFileCb( strShaderPath, sizeof(strShaderPath), 
                        _T("PixelShader3.txt") );

LPD3DXBUFFER pCode;                  // buffer with the assembled shader code
LPD3DXBUFFER pErrorMsgs;             // buffer with error messages
LPDIRECT3DPIXELSHADER9 m_pPixelShader;
D3DXAssembleShaderFromFile( strShaderPath, 0, NULL, &pCode, NULL );
m_pd3dDevice->CreatePixelShader( (DWORD*) pCode->GetBufferPointer(),
                                          &m_pPixelShader );
m_pd3dDevice->SetPixelShader( m_pPixelShader );

// Load the texture and render the output pixels.
m_pd3dDevice->SetTexture( 0, m_pTexture0 );
m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2 );

// Contents of the file "PixelShader3.txt"
ps_1_1          // version instruction
tex t0          // declare texture register t0, 
                // to be loaded from Texture Stage 0
mul r0, v0, t0  // v0*t0, then move to r0

이 예의 셰이더에의 입력을 다음에 나타낸다. 최초의 화면은, 정점색을 나타내고 있다. 2 번째의 화면은 텍스처 맵을 나타내고 있다.

정점색 텍스처

이 예의 결과의 이미지를 다음에 나타낸다. 이것은, 출력, 즉 정점색과 텍스처 이미지의 블렌드를 나타내고 있다.

정점색과 텍스처의 블렌드

색값을 사용한 2 개의 텍스처의 블렌드

이 예에서는, 정점색을 사용해 2 개의 텍스처 맵을 블렌드 해, 각 텍스처 맵의 색이 어느 정도 두개사용되는지를 확인한다. 이 예와 전의 예의 차이는 다음과 같다.

다음에 샘플 코드를 나타낸다.

struct CUSTOMVERTEX
{
    FLOAT x, y, z;
	DWORD color;
    FLOAT tu1, tv1;
    FLOAT tu2, tv2;    // a second set of texture coordinates
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3D_FVF_DIFFUSE|D3DFVF_TEX2|
                             D3DFVF_TEXCOORDSIZE4(0))
static CUSTOMVERTEX g_Vertices[]=
{
    //  x      y     z     color       u1    v1    u2    v2
    { -1. 0f, -1. 0f, 0.0f, 0xff0000ff, 1.0f, 1.0f, 1.0f, 1.0f },
    { +1. 0f, -1. 0f, 0.0f, 0xffff0000, 0.0f, 1.0f, 0.0f, 1.0f },
    { +1. 0f, +1. 0f, 0.0f, 0xffffff00, 0.0f, 0.0f, 0.0f, 0.0f },
    { -1. 0f, +1. 0f, 0.0f, 0xffffffff, 1.0f, 0.0f, 1.0f, 0.0f },
};

// Create texture.This file is in DirectX 9.0 media from 
//   the SDK download.
TCHAR        strTexturePath[512];
LPDIRECT3DTEXTURE9      m_pTexture0, m_pTexture1;

DXUtil_FindMediaFileCb( strTexturePath, sizeof(strTexturePath), 
                        _T("DX5_Logo.bmp") );
D3DUtil_CreateTexture( m_pd3dDevice, strTexturePath, 
                       &m_pTexture0, D3DFMT_R5G6B5 );

DXUtil_FindMediaFileCb( strTexturePath, sizeof(strTexturePath), 
                        _T("snow2.jpg") );
D3DUtil_CreateTexture( m_pd3dDevice, strTexturePath, 
                       &m_pTexture0, D3DFMT_R5G6B5 );

					   
// Create the pixel shader.
TCHAR        strShaderPath[512];
DXUtil_FindMediaFileCb( strShaderPath, sizeof(strShaderPath), 
                        _T("PixelShader4.txt") );

LPD3DXBUFFER pCode;                  // buffer with the assembled shader code
LPD3DXBUFFER pErrorMsgs;             // buffer with error messages
LPDIRECT3DPIXELSHADER9 m_pPixelShader;
D3DXAssembleShaderFromFile( strShaderPath, 0, NULL, &pCode, NULL );
m_pd3dDevice->CreatePixelShader( (DWORD*) pCode->GetBufferPointer(),
                                          &m_pPixelShader );
m_pd3dDevice->SetPixelShader( m_pPixelShader );

// Load the textures stages.
m_pd3dDevice->SetTexture( 0, m_pTexture0 );
m_pd3dDevice->SetTexture( 1, m_pTexture1 );  // Use a second stage.

m_pd3dDevice->SetStreamSource( 0, m_pQuadVB, sizeof(CUSTOMVERTEX) );
m_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
m_pd3dDevice->SetPixelShader( m_pPixelShader );
m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2 );

// Contents of the file "PixelShader4.txt"
ps_1_1              // Pixel shader version.
tex t0              // Texture register t0 is loaded from stage 0.
tex t1              // Texture register t1 is loaded from stage 1.
mov r1, t1          // Move texture t1 into output register r1.
lrp r0, v0, t0, r1  // Linearly interpolate between t0 and r1 
                    //   by a proportion specified in v0.

결과 출력을 다음에 나타낸다.

텍스처 1 텍스처 2 2 개의 텍스처의 블렌드



© 2002 Microsoft Corporation. All rights reserved.
↑TOP