Tutorial07 Dx10

Tutorial 07 - DXSDK - Mapeamento de textura

// Esta aplicação demonstra animação usando transformações de matriz
 
#include <windows.h>
#include <d3d10.h>
#include <d3dx10.h>
 
struct SimpleVertex{
    D3DXVECTOR3 Pos;
    D3DXVECTOR2 Tex;
};
 
HINSTANCE g_hInst = NULL;
HWND g_hWnd = NULL;
D3D10_DRIVER_TYPE g_driverType = D3D10_DRIVER_TYPE_NULL;
ID3D10Device*    g_pd3dDevice = NULL;
IDXGISwapChain*    g_pSwapChain = NULL;
ID3D10RenderTargetView*    g_pRenderTargetView = NULL;
ID3D10Effect*    g_pEffect = NULL;
ID3D10EffectTechnique*    g_pTechnique = NULL;
ID3D10InputLayout*    g_pVertexLayout = NULL;
ID3D10Buffer*    g_pVertexBuffer = NULL;
ID3D10Buffer*    g_pIndexBuffer = NULL;
ID3D10ShaderResourceView*    g_pTextureRV = NULL;
ID3D10EffectMatrixVariable* g_pWorldVariable = NULL;
ID3D10EffectMatrixVariable* g_pViewVariable = NULL;
ID3D10EffectMatrixVariable* g_pProjectionVariable = NULL;
ID3D10EffectVectorVariable* g_pMeshColorVariable = NULL;
ID3D10EffectShaderResourceVariable* g_pDiffuseVariable = NULL;
D3DXMATRIX    g_World;
D3DXMATRIX    g_View;
D3DXMATRIX    g_Projection;
D3DXVECTOR4    g_vMeshColor( 0.7f, 0.7f, 0.7f, 1.0f );
 
HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow );
HRESULT InitDevice();
void CleanupDevice();
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
void Render();
 
// Ponto de entrada para o programa. Inicializa tudo e entra em um loop de processamento de
// mensagem. Tempo ocioso é usado para renderizar a cena
int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow ){
    if( FAILED( InitWindow( hInstance, nCmdShow ) ) )
        return 0;
 
    if( FAILED( InitDevice() ) ){
        CleanupDevice();
        return 0;
    }
 
    // Loop principal de mensagens
    MSG msg = {0};
    while( WM_QUIT != msg.message ){
        if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ){
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        } else {
            Render();
        }
    }
 
    return (int) msg.wParam;
}
 
// Registra classe e cria janela
HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow ){
    // Registra a classe
    WNDCLASSEX wcex;
    wcex.cbSize = sizeof( WNDCLASSEX );
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    //wcex.hIcon = LoadIcon( hInstance, ( LPCTSTR )IDI_TUTORIAL1 );
    wcex.hIcon = NULL;
    wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
    wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = L"TutorialWindowClass";
    //wcex.hIconSm = LoadIcon( hInstance, ( LPCTSTR )IDI_TUTORIAL1 );
    wcex.hIconSm = NULL;
 
    if( !RegisterClassEx( &wcex ) )
        return E_FAIL;
 
    // Cria a janela
    g_hInst = hInstance;
    RECT rc = { 0, 0, 640, 480 };
    AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
    g_hWnd = CreateWindow( L"TutorialWindowClass", L"Direct3D 10 Tutorial 7",
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top,
        NULL, NULL, hInstance, NULL );
 
    if( !g_hWnd )
        return E_FAIL;
 
    ShowWindow( g_hWnd, nCmdShow );
 
    return S_OK;
}
 
// Chamado toda vez que a aplicação recebe uma mensagem
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ){
    PAINTSTRUCT ps;
    HDC hdc;
 
    switch( message ){
        case WM_PAINT:
            hdc = BeginPaint( hWnd, &ps );
            EndPaint( hWnd, &ps );    
            break;
        case WM_DESTROY:
            PostQuitMessage( 0 );
            break;
 
        default:
            return DefWindowProc( hWnd, message, wParam, lParam );
    }
 
    return 0;
}
 
// Cria o dispositivo Direct3D e a cadeia de troca
HRESULT InitDevice(){
    HRESULT hr = S_OK;
 
    RECT rc;
    GetClientRect( g_hWnd, &rc );
    UINT width = rc.right - rc.left;
    UINT height = rc.bottom - rc.top;
 
    UINT createDeviceFlags = 0;
 
#ifdef _DEBUG
    createDeviceFlags |= D3D10_CREATE_DEVICE_DEBUG;
#endif
 
    D3D10_DRIVER_TYPE driverTypes[] = {
        D3D10_DRIVER_TYPE_HARDWARE,
        D3D10_DRIVER_TYPE_REFERENCE
    };
 
    UINT numDriverTypes = sizeof( driverTypes ) / sizeof( driverTypes[0] );
    DXGI_SWAP_CHAIN_DESC sd;
    ZeroMemory( &sd, sizeof( sd ) );
    sd.BufferCount = 1;
    sd.BufferDesc.Width = width;
    sd.BufferDesc.Height = height;
    sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    sd.BufferDesc.RefreshRate.Numerator = 60;
    sd.BufferDesc.RefreshRate.Denominator = 1;
    sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    sd.OutputWindow = g_hWnd;
    sd.SampleDesc.Count = 1;
    sd.SampleDesc.Quality = 0;
    sd.Windowed = TRUE;
 
    for( UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++ ){
        g_driverType = driverTypes[driverTypeIndex];
        hr = D3D10CreateDeviceAndSwapChain( NULL, g_driverType, NULL, createDeviceFlags,
            D3D10_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice );
 
        if( SUCCEEDED( hr ) )
            break;
    }
 
    if( FAILED( hr ) )
        return hr;
 
    // cria uma visão de renderização alvo (Render Target)
    ID3D10Texture2D* pBackBuffer;
    hr = g_pSwapChain->GetBuffer( 0, __uuidof( ID3D10Texture2D ), (LPVOID *) &pBackBuffer );
 
    if( FAILED( hr ) )
        return hr;
 
    hr = g_pd3dDevice->CreateRenderTargetView( pBackBuffer, NULL, &g_pRenderTargetView );
    pBackBuffer->Release();
 
    if( FAILED( hr ) )
        return hr;
 
    g_pd3dDevice->OMSetRenderTargets( 1, & g_pRenderTargetView, NULL );
 
    // Configure o viewport
    D3D10_VIEWPORT vp;
    vp.Width = width;
    vp.Height = height;
    vp.MinDepth = 0.0f;
    vp.MaxDepth = 1.0f;
    vp.TopLeftX = vp.TopLeftY = 0;
    g_pd3dDevice->RSSetViewports( 1, &vp );
 
    //Cria o efeito
    DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
    // Configure a opção D3D10_SHADER_DEBUG para embutir informação de debug nos shaders.
    // Configurar esta opção melhora a debugação do shader, mas ainda permite aos
    // shaders serem otimizados e executarem da maneira que eles executarão na 
    // configuração release deste programa
    dwShaderFlags |= D3D10_SHADER_DEBUG;
#endif
 
    hr = D3DX10CreateEffectFromFile( L"..\\Tutorial07.fx", NULL, NULL, "fx_4_0", dwShaderFlags, 0,
        g_pd3dDevice, NULL, NULL, &g_pEffect, NULL, NULL );
 
    if( FAILED( hr ) ){
        MessageBox( g_hWnd, L"O arquivo fx não pôde ser localizado. Por favor execute este executável do diretório que contém o arquivo FX", 
            L"Erro", MB_OK );
 
        return hr;
    }
 
    // Obtem as técnicas
    g_pTechnique = g_pEffect->GetTechniqueByName( "Render" );
 
    // Obtém as variáveis
    g_pWorldVariable = g_pEffect->GetVariableByName( "World" )->AsMatrix();
    g_pViewVariable = g_pEffect->GetVariableByName( "View" )->AsMatrix();
    g_pProjectionVariable = g_pEffect->GetVariableByName( "Projection" )->AsMatrix();
    g_pMeshColorVariable = g_pEffect->GetVariableByName( "vMeshColor" )->AsVector();
    g_pDiffuseVariable = g_pEffect->GetVariableByName( "txDiffuse" )->AsShaderResource();
 
    // Defina o leiaute de entrada
    D3D10_INPUT_ELEMENT_DESC layout[] = {
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA,0 },
        { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA,0 },
    };
    UINT numElements = sizeof( layout ) / sizeof( layout[0] );
 
    // Cria o leiaute de entrada
    D3D10_PASS_DESC PassDesc;
    g_pTechnique->GetPassByIndex( 0 )->GetDesc( &PassDesc );
    hr = g_pd3dDevice->CreateInputLayout( layout, numElements, PassDesc.pIAInputSignature,
        PassDesc.IAInputSignatureSize, &g_pVertexLayout );
 
    if( FAILED( hr ) )
        return hr;
 
    // Configure o leiaute de entrada
    g_pd3dDevice->IASetInputLayout( g_pVertexLayout );
 
    // Crie o buffer de vértice
    SimpleVertex vertices[] =
    {
        { D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
        { D3DXVECTOR3( 1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
        { D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
        { D3DXVECTOR3( -1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },
 
        { D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
        { D3DXVECTOR3( 1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
        { D3DXVECTOR3( 1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
        { D3DXVECTOR3( -1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },
 
        { D3DXVECTOR3( -1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
        { D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
        { D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
        { D3DXVECTOR3( -1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },
 
        { D3DXVECTOR3( 1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
        { D3DXVECTOR3( 1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
        { D3DXVECTOR3( 1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
        { D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },
 
        { D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
        { D3DXVECTOR3( 1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
        { D3DXVECTOR3( 1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
        { D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },
 
        { D3DXVECTOR3( -1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
        { D3DXVECTOR3( 1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
        { D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
        { D3DXVECTOR3( -1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },
    };
 
    D3D10_BUFFER_DESC bd;
    bd.Usage = D3D10_USAGE_DEFAULT;
    bd.ByteWidth = sizeof( SimpleVertex ) * 24;
    bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
    bd.CPUAccessFlags = 0;
    bd.MiscFlags = 0;
    D3D10_SUBRESOURCE_DATA InitData;
    InitData.pSysMem = vertices;
    hr = g_pd3dDevice->CreateBuffer( &bd, &InitData, &g_pVertexBuffer );
 
    if( FAILED( hr ) )
        return hr;
 
    // Configure o buffer de vértice
    UINT stride = sizeof( SimpleVertex );
    UINT offset = 0;
    g_pd3dDevice->IASetVertexBuffers( 0,1, &g_pVertexBuffer, &stride, &offset );
 
    // Cria o buffer de índice
    // Cria o buffer de vértice
    DWORD indices[] =
    {
        3,1,0,
        2,1,3,
 
        6,4,5,
        7,4,6,
 
        11,9,8,
        10,9,11,
 
        14,12,13,
        15,12,14,
 
        19,17,16,
        18,17,19,
 
        22,20,21,
        23,20,22
    };
 
    bd.Usage = D3D10_USAGE_DEFAULT;
    bd.ByteWidth = sizeof( DWORD ) * 36;    
    bd.BindFlags = D3D10_BIND_INDEX_BUFFER;
    bd.CPUAccessFlags = bd.MiscFlags = 0;
    InitData.pSysMem = indices;
    hr = g_pd3dDevice->CreateBuffer( &bd, &InitData, &g_pIndexBuffer );
    if( FAILED( hr ) )
        return hr;
 
    // Configura o buffer de índice
    g_pd3dDevice->IASetIndexBuffer( g_pIndexBuffer, DXGI_FORMAT_R32_UINT, 0 );
 
    // Configura a topologia primitiva
    g_pd3dDevice->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
 
    // Carrega a textura
    hr = D3DX10CreateShaderResourceViewFromFile( g_pd3dDevice, L"..\\seafloor.dds", NULL, NULL,
        &g_pTextureRV, NULL );
    if( FAILED( hr ) )
        return hr;
 
    // Inicializa a matriz do mundo
    D3DXMatrixIdentity( &g_World );
 
    // Inicializa a matriz visão
    D3DXVECTOR3 Eye( 0.0f, 3.0f, -6.0f );
    D3DXVECTOR3 At( 0.0f, 1.0f, 0.0f );
    D3DXVECTOR3 Up( 0.0f, 1.0f, 0.0f );
    D3DXMatrixLookAtLH( &g_View, &Eye, &At, &Up );
 
    // Inicializa a matriz de projeção
    D3DXMatrixPerspectiveFovLH( &g_Projection, (float) D3DX_PI * 0.25f, width / (FLOAT) height, 0.1f, 100.0f );
 
    // Atualize variáveis que nunca mudarão
    g_pViewVariable->SetMatrix( (float*) &g_View );
    g_pProjectionVariable->SetMatrix( (float*) &g_Projection );
    g_pDiffuseVariable->SetResource( g_pTextureRV );
 
    return S_OK;
}
 
// Renderiza um quadro
void Render(){
    // Atualize nosso tempo
    static float t = 0.0f;
    if( g_driverType == D3D10_DRIVER_TYPE_REFERENCE ){
        t += (float) D3DX_PI * 0.0125f;
    } else {
        static DWORD dwTimeStart = 0;
        DWORD dwTimeCur = GetTickCount();
        if( dwTimeStart == 0 )
            dwTimeStart = dwTimeCur;
        t = ( dwTimeCur - dwTimeStart ) / 1000.0f;
    }
 
    // Rotacione o cubo ao redor da origem
    D3DXMatrixRotationY( &g_World, t );
 
    // Modifica a cor
    g_vMeshColor.x = ( sinf( t*1.0f ) + 1.0f ) * 0.5f;
    g_vMeshColor.y = ( cosf( t*3.0f ) + 1.0f ) * 0.5f;
    g_vMeshColor.z = ( sinf( t*5.0f ) + 1.0f ) * 0.5f;
 
    // Limpa o buffer de trás
    float ClearColor[4] = {0.0f, 0.125f, 0.3f, 1.0f};    // vermelho, verde, azul, alfa
    g_pd3dDevice->ClearRenderTargetView( g_pRenderTargetView, ClearColor );
 
    // Atualiza as variáveis que mudam uma vez por quadro
    g_pWorldVariable->SetMatrix( (float* )&g_World );
    g_pMeshColorVariable->SetFloatVector( (float*) g_vMeshColor);
 
    // Renderiza o cubo
    D3D10_TECHNIQUE_DESC techDesc;    
    g_pTechnique->GetDesc( &techDesc );
    for( UINT p = 0; p < techDesc.Passes; ++p ){
        g_pTechnique->GetPassByIndex( p )->Apply( 0 );
        g_pd3dDevice->DrawIndexed( 36, 0, 0 );    // 36 vértices necessários para 12 triângulos em uma lista de triângulos
    }
 
    // Apresenta nosso buffer de trás para o buffer da frente
    g_pSwapChain->Present(0,0);
}
 
// Limpa os objetos que nós criamos
void CleanupDevice(){
    if( g_pd3dDevice ) g_pd3dDevice->ClearState();
 
    if( g_pVertexBuffer ) g_pVertexBuffer->Release();
    if( g_pIndexBuffer ) g_pIndexBuffer->Release();
    if( g_pVertexLayout ) g_pVertexLayout->Release();
    if( g_pEffect ) g_pEffect->Release();
    if( g_pRenderTargetView ) g_pRenderTargetView->Release();
    if( g_pSwapChain ) g_pSwapChain->Release();
    if( g_pd3dDevice ) g_pd3dDevice->Release();
}

E o código para o arquivo de efeito é:

//--------------------------------------------------------------------------------------
// File: Tutorial07.fx
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
 
//--------------------------------------------------------------------------------------
// Constant Buffer Variables
//--------------------------------------------------------------------------------------
Texture2D txDiffuse;
SamplerState samLinear
{
    Filter = MIN_MAG_MIP_LINEAR;
    AddressU = Wrap;
    AddressV = Wrap;
};
 
cbuffer cbNeverChanges
{
    matrix View;
};
 
cbuffer cbChangeOnResize
{
    matrix Projection;
};
 
cbuffer cbChangesEveryFrame
{
    matrix World;
    float4 vMeshColor;
};
 
struct VS_INPUT
{
    float4 Pos : POSITION;
    float2 Tex : TEXCOORD;
};
 
struct PS_INPUT
{
    float4 Pos : SV_POSITION;
    float2 Tex : TEXCOORD0;
};
 
//--------------------------------------------------------------------------------------
// 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;
}
 
//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PS( PS_INPUT input) : SV_Target
{
    return txDiffuse.Sample( samLinear, input.Tex ) * vMeshColor;
}
 
//--------------------------------------------------------------------------------------
technique10 Render
{
    pass P0
    {
        SetVertexShader( CompileShader( vs_4_0, VS() ) );
        SetGeometryShader( NULL );
        SetPixelShader( CompileShader( ps_4_0, PS() ) );
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.