地形上的 C++ DirectX11 纹理未正确渲染

Posted

技术标签:

【中文标题】地形上的 C++ DirectX11 纹理未正确渲染【英文标题】:C++ DirectX11 Texture On Terrain Not Rendering Properly 【发布时间】:2017-11-01 21:05:15 【问题描述】:

我正在使用Rastertek 教程开发游戏引擎。 我的问题是地形纹理没有正确加载。

像素着色器:

   Texture2D shaderTexture;
SamplerState SampleType;

cbuffer LightBuffer

    float4 ambientColor;
    float4 diffuseColor;
    float3 lightDirection;
    float padding;
;


//////////////
// TYPEDEFS //
//////////////
struct PixelInputType

    float4 position : SV_POSITION;
    float2 tex : TEXCOORD0;
    float3 normal : NORMAL;
;


////////////////////////////////////////////////////////////////////////////////
// Pixel Shader
////////////////////////////////////////////////////////////////////////////////
float4 TerrainPixelShader(PixelInputType input) : SV_TARGET

    float4 textureColor;
    float3 lightDir;
    float lightIntensity;
    float4 color;


    // Sample the pixel color from the texture using the sampler at this texture coordinate location.
    textureColor = shaderTexture.Sample(SampleType, input.tex);

    // Set the default output color to the ambient light value for all pixels.
    color = ambientColor;

    // Invert the light direction for calculations.
    lightDir = -lightDirection;

    // Calculate the amount of light on this pixel.
    lightIntensity = saturate(dot(input.normal, lightDir));

    if(lightIntensity > 0.0f)
    
        // Determine the final diffuse color based on the diffuse color and the amount of light intensity.
        color += (diffuseColor * lightIntensity);
    

    // Saturate the final light color.
    color = saturate(color);

    // Multiply the texture pixel and the final light color to get the result.
    color = color * textureColor;

顶点着色器:

cbuffer MatrixBuffer

    matrix worldMatrix;
    matrix viewMatrix;
    matrix projectionMatrix;
;


//////////////
// TYPEDEFS //
//////////////
struct VertexInputType

    float4 position : POSITION;
    float2 tex : TEXCOORD0;
    float3 normal : NORMAL;
;

struct PixelInputType

    float4 position : SV_POSITION;
    float2 tex : TEXCOORD0;
    float3 normal : NORMAL;
;


////////////////////////////////////////////////////////////////////////////////
// Vertex Shader
////////////////////////////////////////////////////////////////////////////////
PixelInputType TerrainVertexShader(VertexInputType input)

    PixelInputType output;


    // Change the position vector to be 4 units for proper matrix calculations.
    input.position.w = 1.0f;

    // Calculate the position of the vertex against the world, view, and projection matrices.
    output.position = mul(input.position, worldMatrix);
    output.position = mul(output.position, viewMatrix);
    output.position = mul(output.position, projectionMatrix);

    // Store the texture coordinates for the pixel shader.
    output.tex = input.tex;

    // Calculate the normal vector against the world matrix only.
    output.normal = mul(input.normal, (float3x3)worldMatrix);

    // Normalize the normal vector.
    output.normal = normalize(output.normal);

    return output;

地形着色器类:

    bool TerrainShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, D3DXMATRIX world, D3DXMATRIX view,
        D3DXMATRIX projection, D3DXVECTOR4 ambientColor, D3DXVECTOR4 diffuseColor, D3DXVECTOR3 lightDirection,
        ID3D11ShaderResourceView* texture)
    
        HRESULT result;
        D3D11_MAPPED_SUBRESOURCE mappedResource;
        unsigned int bufferNumber;
        MatrixBufferType* matrixData;
        LightBufferType* lightData;

        D3DXMatrixTranspose(&world, &world);
        D3DXMatrixTranspose(&view, &view);
        D3DXMatrixTranspose(&projection, &projection);

        result = deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
        if (FAILED(result))
        
            return false;
        

        matrixData = (MatrixBufferType*)mappedResource.pData;
        matrixData->world = world;
        matrixData->view = view;
        matrixData->projection = projection;

        deviceContext->Unmap(m_matrixBuffer, 0);

        bufferNumber = 0;

        deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);

        deviceContext->Map(m_lightBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);

        lightData = (LightBufferType*)mappedResource.pData;
        lightData->ambientColor = ambientColor;
        lightData->diffuseColor = diffuseColor;
        lightData->lightDirection = lightDirection;
        lightData->padding = 0.0f;

        deviceContext->Unmap(m_lightBuffer, 0);

        bufferNumber = 0;

        deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_lightBuffer);
        deviceContext->PSSetShaderResources(0, 1, &texture);

        return true;
    

    void TerrainShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, LPCSTR shaderFileName)
    
        char* compileErrors = (char*)(errorMessage->GetBufferPointer());
        unsigned long bufferSize = errorMessage->GetBufferSize();
        ofstream fout;

        fout.open("shader-error.txt");

        for (unsigned long i = 0; i < bufferSize; i++)
        
            fout << compileErrors[i];
        

        fout.close();

        errorMessage->Release();
        errorMessage = nullptr;

        MessageBox(hwnd, "Error compiling shader.  Check shader-error.txt for message.", shaderFileName, MB_OK);
    

    void TerrainShaderClass::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
    
        deviceContext->IASetInputLayout(m_layout);
        deviceContext->VSSetShader(m_vertexShader, NULL, 0);
        deviceContext->PSSetShader(m_pixelShader, NULL, 0);
        deviceContext->PSSetSamplers(0, 1, &m_samplerState);
        deviceContext->DrawIndexed(indexCount, 0, 0);
    

bool TerrainShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, LPCSTR vsFileName, LPCSTR psFileName)

    HRESULT result;
    ID3D10Blob* errorMessage = nullptr;
    ID3D10Blob* vertexShaderBuffer = nullptr;
    ID3D10Blob* pixelShaderBuffer = nullptr;
    D3D11_INPUT_ELEMENT_DESC polygonLayout[3];
    unsigned int numElements;
    D3D11_SAMPLER_DESC samplerDesc;
    D3D11_BUFFER_DESC matrixBufferDesc;
    D3D11_BUFFER_DESC lightBufferDesc;

    result = D3DX11CompileFromFile(vsFileName, NULL, NULL, "TerrainVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS,
        0, NULL, &vertexShaderBuffer, &errorMessage, NULL);
    if (FAILED(result))
    
        if (errorMessage)
        
            OutputShaderErrorMessage(errorMessage, hwnd, vsFileName);
        
        else
        
            MessageBox(hwnd, "Missing Shader File", vsFileName, MB_OK);
        

        return false;
    

    result = D3DX11CompileFromFile(psFileName, NULL, NULL, "TerrainPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS,
        0, NULL, &pixelShaderBuffer, &errorMessage, NULL);
    if (FAILED(result))
    
        if (errorMessage)
        
            OutputShaderErrorMessage(errorMessage, hwnd, psFileName);
        
        else
        
            MessageBox(hwnd, "Missing Shader File", psFileName, MB_OK);
        

        return false;
    

    result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
    if (FAILED(result))
    
        return false;
    

    result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader);
    if (FAILED(result))
    
        return false;
    

    polygonLayout[0].SemanticName = "POSITION";
    polygonLayout[0].SemanticIndex = 0;
    polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
    polygonLayout[0].InputSlot = 0;
    polygonLayout[0].AlignedByteOffset = 0;
    polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
    polygonLayout[0].InstanceDataStepRate = 0;

    polygonLayout[1].SemanticName = "TEXCOORD";
    polygonLayout[1].SemanticIndex = 0;
    polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
    polygonLayout[1].InputSlot = 0;
    polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
    polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
    polygonLayout[1].InstanceDataStepRate = 0;

    polygonLayout[2].SemanticName = "NORMAL";
    polygonLayout[2].SemanticIndex = 0;
    polygonLayout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT;
    polygonLayout[2].InputSlot = 0;
    polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
    polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
    polygonLayout[2].InstanceDataStepRate = 0;

    numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);

    result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &m_layout);
    if (FAILED(result))
    
        return false;
    

    vertexShaderBuffer->Release();
    vertexShaderBuffer = nullptr;
    pixelShaderBuffer->Release();
    pixelShaderBuffer = nullptr;

    samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
    samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
    samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
    samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
    samplerDesc.MipLODBias = 0.0f;
    samplerDesc.MaxAnisotropy = 1;
    samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
    samplerDesc.BorderColor[0] = 0;
    samplerDesc.BorderColor[1] = 0;
    samplerDesc.BorderColor[2] = 0;
    samplerDesc.BorderColor[3] = 0;
    samplerDesc.MinLOD = 0;
    samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;

    result = device->CreateSamplerState(&samplerDesc, &m_samplerState);
    if (FAILED(result))
    
        return false;
    

    matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
    matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
    matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
    matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
    matrixBufferDesc.MiscFlags = 0;
    matrixBufferDesc.StructureByteStride = 0;

    result = device->CreateBuffer(&matrixBufferDesc, NULL, &m_matrixBuffer);
    if (FAILED(result))
    
        return false;
    

    //ByteWidth must be a multiple of 16 if using D3D11_BIND_CONSTANT_BUFFER or CreateBuffer will fail.
    lightBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
    lightBufferDesc.ByteWidth = sizeof(LightBufferType);
    lightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
    lightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
    lightBufferDesc.MiscFlags = 0;
    lightBufferDesc.StructureByteStride = 0;

    device->CreateBuffer(&lightBufferDesc, NULL, &m_lightBuffer);
    if (FAILED(result))
    
        return false;
    

    return true;

纹理应该看起来像链接上显示的那样,但看起来真的很奇怪(似乎无法截屏,如果可能会添加)。

我尝试在这里查看其他问题,但没有解决问题。

我对 DX11 还很陌生,因此非常感谢任何帮助。

编辑:这是截图(左侧:假定,右侧:我的游戏)

【问题讨论】:

您能否发布带有预期结果的屏幕截图以及您当前的输出? 顺便说一句,你为什么使用 D3DX 数学库?您应该改用 DirectXMath 库 我正在使用 d3dx10math.h 标头。还添加了截图。 您的纹理坐标似乎有问题 重新审视这一点,在 yoru 纹理和法线上手动将对齐设置为 16 和 32。我通常也会在这里的寄存器上使用完整的 RGBA32 位格式,只是明确地使用正确的对齐方式。您不必在 HLSL 代码中使用所有 4 个浮点数,只是有帮助。这应该确保您在正常和纹理寄存器中的字节对齐是正确的。我从我所看到的猜测您的正常缓冲区在 8 字节边界(不是 16 字节边界)上对齐。 【参考方案1】:

我正在查看您的屏幕截图,您的纹理不仅没有正确渲染,而且您的法线也没有正确渲染,否则您至少会有一个漫反射,至少可以正确地对其进行着色。我想总结一下,尽管您的步幅是正确的,但是您从缓冲区中拉出的 UV 和 Normal 没有正确对齐。我的第一个想法。

【讨论】:

以上是关于地形上的 C++ DirectX11 纹理未正确渲染的主要内容,如果未能解决你的问题,请参考以下文章

C++ DirectX - 对 2D 网格进行纹理化

Directx11学习笔记二十二 用高度图实现地形

纹理未正确显示 - 可能坐标是错误的 OpenGL、C++

Windows 8.1 上的 DirectX 11 缺少 SDK 组件

C++小项目:directx11图形程序:particleSysclass

DirectX11第六篇 纹理映射