DirectX - 三角形不显示在背景上

时间:2014-07-20 17:37:11

标签: c++ directx

我不知道这段代码有什么问题。应用程序应在蓝色背景上显示一个三角形。唯一显示的是蓝色背景。 D3DGame类继承自D3DApp。

D3DApp.cpp的一部分:

void D3DApp::onResize() {

    assert(devCon);
    assert(dev);
    assert(swapChain);

    ReleaseCOM(renderTargetView);
    ReleaseCOM(depthStencilView);
    ReleaseCOM(depthStencilBuffer);

    swapChain->ResizeBuffers(1, width, height, DXGI_FORMAT_R8G8B8A8_UNORM, 0);
    ID3D11Texture2D* _backBuffer;
    swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&_backBuffer));
    dev->CreateRenderTargetView(_backBuffer, 0, &renderTargetView);
    ReleaseCOM(_backBuffer);

    D3D11_TEXTURE2D_DESC _depthStencilDesc;

    _depthStencilDesc.Width = width;
    _depthStencilDesc.Height = height;
    _depthStencilDesc.MipLevels = 1;
    _depthStencilDesc.ArraySize = 1;
    _depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;

    if (enable4xMsaa)
    {
        _depthStencilDesc.SampleDesc.Count = 4;
        _depthStencilDesc.SampleDesc.Quality = x4MsaaQuality - 1;
    }
    else
    {
        _depthStencilDesc.SampleDesc.Count = 1;
        _depthStencilDesc.SampleDesc.Quality = 0;
    }

    _depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
    _depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
    _depthStencilDesc.CPUAccessFlags = 0;
    _depthStencilDesc.MiscFlags = 0;

    dev->CreateTexture2D(&_depthStencilDesc, 0, &depthStencilBuffer);
    dev->CreateDepthStencilView(depthStencilBuffer, 0, &depthStencilView);

    devCon->OMSetRenderTargets(1, &renderTargetView, depthStencilView);

    screenViewport.TopLeftX = 0;
    screenViewport.TopLeftY = 0;
    screenViewport.Width = static_cast<float>(width);
    screenViewport.Height = static_cast<float>(height);
    screenViewport.MinDepth = 0.0f;
    screenViewport.MaxDepth = 1.0f;

    devCon->RSSetViewports(1, &screenViewport);
}

bool D3DApp::init() {
    if (!initWND())
        return false;
    showWND();

    UINT _dev_flag = 0;
#if defined(DEBUG) || defined(_DEBUG)
    _dev_flag = D3D11_CREATE_DEVICE_DEBUG;
#endif

    HRESULT hr = D3D11CreateDevice(
        0,
        driverType,
        0,
        _dev_flag,
        0, 0,
        D3D11_SDK_VERSION,
        &dev,
        &featurelLevel,
        &devCon);

    if (FAILED(hr)) {
        MessageBox(0, "Nie mozna utworzyc urzadzenia!", 0, 0);
        return false;
    }

    DXGI_SWAP_CHAIN_DESC _sw_desc;
    _sw_desc.BufferDesc.Height = height;
    _sw_desc.BufferDesc.Width = width;
    _sw_desc.BufferDesc.RefreshRate.Numerator = 60;
    _sw_desc.BufferDesc.RefreshRate.Denominator = 1;
    _sw_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    _sw_desc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
    _sw_desc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;

    if (enable4xMsaa) {
        _sw_desc.SampleDesc.Count = 4;
        _sw_desc.SampleDesc.Quality = x4MsaaQuality - 1;
    }
    else {
        _sw_desc.SampleDesc.Count = 1;
        _sw_desc.SampleDesc.Quality = 0;
    }

    _sw_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    _sw_desc.BufferCount = 1;
    _sw_desc.Windowed = true;
    _sw_desc.OutputWindow = windowHandle;
    _sw_desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
    _sw_desc.Flags = 0;

    IDXGIDevice* dxgiDevice = 0;
    dev->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);

    IDXGIAdapter* dxgiAdapter = 0;
    dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&dxgiAdapter);

    IDXGIFactory* dxgiFactory = 0;
    dxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&dxgiFactory);

    dxgiFactory->CreateSwapChain(dev, &_sw_desc, &swapChain);

    ReleaseCOM(dxgiDevice);
    ReleaseCOM(dxgiAdapter);
    ReleaseCOM(dxgiFactory);

    onResize();

    return true;
}

D3DGame.cpp的一部分:

bool D3DGame::buildGeoBuffers() {
    Vertex _vertices[] = {
        { XMFLOAT3(-1.0f, -1.0f, 0.0f), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) },
        { XMFLOAT3(1.0f, -1.0f, 0.0f), XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f) },
        { XMFLOAT3(0.0f, 1.0f, 0.0f), XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f) }
    };

    D3D11_BUFFER_DESC _vbd;
    _vbd.Usage = D3D11_USAGE_IMMUTABLE;
    _vbd.ByteWidth = sizeof(Vertex)* 3;
    _vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    _vbd.CPUAccessFlags = 0;
    _vbd.MiscFlags = 0;
    _vbd.StructureByteStride = 0;

    D3D11_SUBRESOURCE_DATA _sub_s;
    _sub_s.pSysMem = _vertices;
    HRESULT hr1 = dev->CreateBuffer(&_vbd, &_sub_s, &triangleVB);
    if (FAILED(hr1)) {
        MessageBox(0, "Nie mozna stworzyc bufora wierzcholkow trojkata!", 0, 0);
        return false;
    }

    //bufor indeksow
    UINT indices[] = {
        0, 1, 2
    };

    D3D11_BUFFER_DESC _ibd;
    _ibd.Usage = D3D11_USAGE_IMMUTABLE;
    _ibd.ByteWidth = sizeof(UINT)* 36;
    _ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
    _ibd.CPUAccessFlags = 0;
    _ibd.MiscFlags = 0;
    _ibd.StructureByteStride = 0;

    D3D11_SUBRESOURCE_DATA _sub_s_indx;
    _sub_s_indx.pSysMem = indices;
    HRESULT hr2 = dev->CreateBuffer(&_ibd, &_sub_s_indx, &indexB);
    if (FAILED(hr2)) {
        MessageBox(0, "Nie mozna stworzyc bufora indeksow!", 0, 0);
        return false;
    }

    return true;
}

bool D3DGame::buildEffects() {
    DWORD shaderFlags = 0;
#if defined( DEBUG ) || defined( _DEBUG )
    shaderFlags |= D3D10_SHADER_DEBUG;
    shaderFlags |= D3D10_SHADER_SKIP_OPTIMIZATION;
#endif

    ID3D10Blob* compiledShader = 0;
    ID3D10Blob* compilationMsgs = 0;
    HRESULT hr = D3DX11CompileFromFile("color.fx", 0, 0, 0, "fx_5_0", shaderFlags,
        0, 0, &compiledShader, &compilationMsgs, 0);

    if (compilationMsgs != 0) {
        MessageBoxA(0, (char*)compilationMsgs->GetBufferPointer(), 0, 0);
        ReleaseCOM(compilationMsgs);
    }

    if (FAILED(hr)) {
        MessageBox(0, "Nie mozna skompilowac pliku color.fx", 0, 0);
        return false;
    }

    D3DX11CreateEffectFromMemory(compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(),
        0, dev, &effect);

    ReleaseCOM(compiledShader);

    eTech = effect->GetTechniqueByName("ColorTech");

    return true;
}

void D3DGame::buildVertexLay()
{
    D3D11_INPUT_ELEMENT_DESC vertexDesc[] =
    {
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
        { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
    };

    D3DX11_PASS_DESC passDesc;
    eTech->GetPassByIndex(0)->GetDesc(&passDesc);
    dev->CreateInputLayout(vertexDesc, 2, passDesc.pIAInputSignature,
        passDesc.IAInputSignatureSize, &inputLay);
}

bool D3DGame::init() {
    if (!D3DApp::init())
        return false;

    buildGeoBuffers();
    buildEffects();
    buildVertexLay();

    return true;
}

void D3DGame::draw() {
    float _colors[] = {
        0.0f, 0.2f, 0.4f, 1.0f
    };

    devCon->ClearRenderTargetView(renderTargetView, _colors);
    devCon->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);

    devCon->IASetInputLayout(inputLay);
    devCon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

    UINT stride = sizeof(Vertex);
    UINT offset = 0;
    devCon->IASetVertexBuffers(0, 1, &triangleVB, &stride, &offset);
    devCon->IASetIndexBuffer(indexB, DXGI_FORMAT_R32_UINT, 0);

    D3DX11_TECHNIQUE_DESC techDesc;
    eTech->GetDesc(&techDesc);

    for (UINT p = 0; p < techDesc.Passes; ++p) {
        eTech->GetPassByIndex(p)->Apply(0, devCon);
        devCon->DrawIndexed(3, 0, 0);
    }

    HRESULT hr = swapChain->Present(0, 0);
    if (FAILED(hr)) {
        MessageBox(0, "blad podczas zamiany buforow lancucha wymiany!", 0, 0);
    }
}

int D3DGame::run() {

    MSG msg = { 0 };

    while (msg.message != WM_QUIT) {
        if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        } else {
            draw();
        }
    }

    return (int)msg.wParam;
}

效果来源:

struct VertexIn
{
    float3 PosL  : POSITION;
    float4 Color : COLOR;
};

struct VertexOut
{
    float4 PosH  : SV_POSITION;
    float4 Color : COLOR;
};

VertexOut VS(VertexIn vin)
{
    VertexOut vout;

    // Transform to homogeneous clip space.
    //vout.PosH = mul(float4(vin.PosL, 1.0f), gWorldViewProj);
    vout.PosH = float4(vin.PosL, 1.0f);
    // Just pass vertex color into the pixel shader.
    vout.Color = vin.Color;

    return vout;
}

float4 PS(VertexOut pin) : SV_Target
{
    return pin.Color;
}

technique11 ColorTech
{
    pass P0
    {
        SetVertexShader( CompileShader( vs_5_0, VS() ) );
        SetGeometryShader( NULL );
        SetPixelShader( CompileShader( ps_5_0, PS() ) );
    }
}

0 个答案:

没有答案