我正在尝试为正方形设置棋盘纹理

时间:2013-09-03 04:11:48

标签: c++ directx textures directx-11 direct3d11

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

    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);

1 个答案:

答案 0 :(得分:3)

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

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