2

我在 DirectX 11 中显示简单的网格模式时遇到了一些问题。一切都正确加载,没有任何错误或返回,但实际上没有显示任何内容。

在 DrawGridSquare 函数中设置断点表明 SpritePtr 变量中的所有内容都已正确设置,但实际上我仍然没有在屏幕上显示任何内容。

因此,我相当确定我的问题在于我在下面提供的 Render() 或 LoadContent() 函数。

以下是相关来源:

/***
  * Load simple shaders and other necessary components
***/
bool AStarDemo::LoadContent(){
    ID3DBlob* vsBuffer = 0;
    bool compileResult = CompileD3DShader("TextureMap.hlsl", "VS_Main", "vs_4_0", &vsBuffer);

    if(compileResult == false){
        DXTRACE_MSG("Error compiling vertex shader");
        return false;
    }

    HRESULT d3dResult;

    d3dResult = d3dDevice->CreateVertexShader(vsBuffer->GetBufferPointer(),
        vsBuffer->GetBufferSize(), 0, &returnColorVS);

    if(FAILED(d3dResult)){
        if(vsBuffer){
            vsBuffer->Release();
        }

        return false;
    }

    D3D11_INPUT_ELEMENT_DESC solidColorLayout[] ={
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
        { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
    };

    unsigned int totalLayoutElements = ARRAYSIZE(solidColorLayout);

    d3dResult = d3dDevice->CreateInputLayout(solidColorLayout, totalLayoutElements,
        vsBuffer->GetBufferPointer(), vsBuffer->GetBufferSize(), &inputLayout);

    vsBuffer->Release();

    if(FAILED(d3dResult)){
        return false;
    }

    ID3DBlob* psBuffer = 0;
    compileResult = CompileD3DShader("TextureMap.hlsl", "PS_Main", "ps_4_0", &psBuffer);

    if(compileResult == false){
        DXTRACE_MSG("Error loading pixel shader!");
        return false;
    }

    d3dResult = d3dDevice->CreatePixelShader(psBuffer->GetBufferPointer(),
        psBuffer->GetBufferSize(), 0, &returnColorPS);

    psBuffer->Release();

    if(FAILED(d3dResult)){
        DXTRACE_MSG( "Error creating pixel shader!" );
        return false;
    }

    //*** LOAD DDS FILE ***//
    d3dResult = D3DX11CreateShaderResourceViewFromFile(d3dDevice, "tiles.dds", 0, 0, &colorMap, 0);

    if(FAILED(d3dResult)){
        DXTRACE_MSG( "Error loading textures!" );
        return false;
    }

    D3D11_SAMPLER_DESC colorMapDesc;
    ZeroMemory(&colorMapDesc, sizeof(colorMapDesc));
    colorMapDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
    colorMapDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
    colorMapDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
    colorMapDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
    colorMapDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
    colorMapDesc.MaxLOD = D3D11_FLOAT32_MAX;

    d3dResult = d3dDevice->CreateSamplerState(&colorMapDesc, &colorMapSampler);
    if(FAILED(d3dResult)){
        DXTRACE_MSG( "Failed to create color map sampler state!" );
        return false;
    }

    ID3D11Resource* colorTex;
    colorMap->GetResource(&colorTex);

    D3D11_TEXTURE2D_DESC colorTexDesc;
    ((ID3D11Texture2D*)colorTex)->GetDesc(&colorTexDesc);
    colorTex->Release();

    float halfWidth = (float)colorTexDesc.Width / 2.0f;
    float halfHeight = (float)colorTexDesc.Height / 2.0f;


    vertices = new Vertex[6 * TOTAL_NUMBER_GRID_SQUARES];
    gridSquares = new GridSquare[TOTAL_NUMBER_GRID_SQUARES];
    //for each grid square
    for(int i = 0; i < TOTAL_NUMBER_GRID_SQUARES; i++){

        //create a grid square object
        GridSquare gs;
        gs.type = ((i%2) == 0) ? prey : hunter;

        gs.BottomRight     = Vertex( XMFLOAT3( halfWidth + (halfWidth * (i % GRID_WIDTH)), 
                                            halfHeight + (halfHeight * (i / GRID_WIDTH)), 
                                            Z_LEVEL),
                            //XMFLOAT2(1.0f + gs.position.x, 0.0f + gs.position.y));
                            XMFLOAT2(1.0f, 0.0f));
        gs.BottomCorner    = Vertex( XMFLOAT3(  halfWidth + (halfWidth * (i % GRID_WIDTH)), 
                                            -halfHeight + (halfHeight * (i / GRID_WIDTH)), 
                                            Z_LEVEL),
                            //XMFLOAT2(1.0f + gs.position.x, 1.0f + gs.position.y));
                            XMFLOAT2(1.0f, 1.0f));
        gs.BottomLeft     = Vertex( XMFLOAT3( -halfWidth + (halfWidth * (i % GRID_WIDTH)), 
                                              -halfHeight + (halfHeight * (i / GRID_WIDTH)), 
                                            Z_LEVEL),
                            //XMFLOAT2(0.0f + gs.position.x, 1.0f + gs.position.y));
                            XMFLOAT2(0.0f, 1.0f));
        gs.TopLeft = Vertex( XMFLOAT3( -halfWidth + (halfWidth * (i % GRID_WIDTH)),   
                                            -halfHeight + (halfHeight * (i / GRID_WIDTH)),         
                                            Z_LEVEL),
                            //XMFLOAT2(0.0f + gs.position.x, 1.0f + gs.position.y));
                            XMFLOAT2(0.0f, 1.0f));
        gs.TopCorner  = Vertex( XMFLOAT3( -halfWidth + (halfWidth * (i % GRID_WIDTH)),    
                                            halfHeight + (halfHeight * (i / GRID_WIDTH)), 
                                            Z_LEVEL),
                            //XMFLOAT2(0.0f + gs.position.x, 0.0f + gs.position.y));
                            XMFLOAT2(0.0f, 0.0f));
        gs.TopRight = Vertex( XMFLOAT3(  halfWidth + (halfWidth * (i % GRID_WIDTH)),   
                                            halfHeight + (halfHeight * (i / GRID_WIDTH)), 
                                            Z_LEVEL),
                            //XMFLOAT2(1.0f + gs.position.x, 0.0f + gs.position.y));
                            XMFLOAT2(1.0f, 0.0f));      

        gridSquares[i] = gs;
        vertices[6*i]     = gs.BottomRight;
        vertices[(6*i)+1] = gs.BottomCorner;
        vertices[(6*i)+2] = gs.BottomLeft;
        vertices[(6*i)+3] = gs.TopLeft;
        vertices[(6*i)+4] = gs.TopCorner;
        vertices[(6*i)+5] = gs.TopRight;
    }

    D3D11_BUFFER_DESC vertexDesc;
    ZeroMemory(&vertexDesc, sizeof(vertexDesc));
    vertexDesc.Usage = D3D11_USAGE_DYNAMIC;
    vertexDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
    vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vertexDesc.ByteWidth = 6 * sizeof(Vertex) * TOTAL_NUMBER_GRID_SQUARES; //num vertices * size of single vertex

    d3dResult = d3dDevice->CreateBuffer(&vertexDesc, 0, &vertexBuffer);

    if(FAILED(d3dResult)){
        return false;
    }

    return true;
}

void AStarDemo::Render(){
    if(d3dContext == 0)
        return;

    float clearColor[4] = {0.0f, 0.0f, 0.25f, 1.0f};
    d3dContext->ClearRenderTargetView(backBuffTarget, clearColor);

    unsigned int stride = sizeof(Vertex);
    unsigned int offset = 0;

    d3dContext->IASetInputLayout(inputLayout);
    d3dContext->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
    d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

    //actually set the vertex shader
    d3dContext->VSSetShader(returnColorVS, 0, 0);
    //actually set the pixel shader
    d3dContext->PSSetShader(returnColorPS, 0, 0);

    //set shader resources & sampler
    d3dContext->PSSetShaderResources(0, 1, &colorMap);
    d3dContext->PSSetSamplers(0, 1, &colorMapSampler);

    //update grid squares
    //   A* alg steped in built-in Update()
    for(int i = 0; i < 1/*TOTAL_NUMBER_GRID_SQUARES*/; i+=6){
        //update the squares
        DrawGridSquare(i);
    }

    swapChain->Present(0, 0);
}

任何帮助将不胜感激!

4

0 回答 0