3

我正在尝试为正方形设置带有绿色和蓝色纹素的棋盘纹理。相反,我的正方形是黑色的。我没有收到任何编译或运行时错误。我的纹理代码有问题吗?

    vector<unsigned char> texelsBuffer;

for(int i = 0; i < N; ++i)
    for(int j = 0; j < N; ++j)
    {
        texelsBuffer.push_back(0.0f);

        if((i+j) % 2 == 0) {
            texelsBuffer.push_back(1.0f);
            texelsBuffer.push_back(0.0f);
        }
        else {
            texelsBuffer.push_back(0.0f);
            texelsBuffer.push_back(1.0f);
        }
        texelsBuffer.push_back(1.0f);
    }

ID3D11Texture2D* tex = 0;

D3D11_TEXTURE2D_DESC textureDescr;
ZeroMemory(&textureDescr, sizeof(D3D11_TEXTURE2D_DESC));
textureDescr.ArraySize = 1;
textureDescr.BindFlags = D3D11_BIND_SHADER_RESOURCE;
textureDescr.CPUAccessFlags = 0;
textureDescr.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
textureDescr.Height = N;
textureDescr.Width = N;
textureDescr.MipLevels = 1;
textureDescr.MiscFlags = 0;
textureDescr.SampleDesc.Count = 1;
textureDescr.SampleDesc.Quality = 0;
textureDescr.Usage = D3D11_USAGE_IMMUTABLE;

D3D11_SUBRESOURCE_DATA texInitData;
ZeroMemory(&texInitData, sizeof(D3D11_SUBRESOURCE_DATA));
texInitData.pSysMem = (void*)&texelsBuffer[0];
texInitData.SysMemPitch = sizeof(unsigned char) * N;
texInitData.SysMemSlicePitch = 0;

g_pd3dDevice->CreateTexture2D(&textureDescr, &texInitData, &tex);
g_pd3dDevice->CreateShaderResourceView(tex, NULL, &g_pTextureRV);

还是我的采样器代码?

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

g_pd3dDevice->CreateSamplerState(&sampDesc, &g_pSamplerLinear);
4

1 回答 1

3

您正在填充texelsBuffer浮点值。UNORM在编码中使用 255 或 0xFF 将通道设置为 1.0 。

你还应该考虑让你的棋格大于一个纹素;除非您总是以 1:1 的像素:纹素比率渲染纹理,否则您最终会在大多数样本位置混合两种颜色。

于 2013-09-03T16:43:50.317 回答