繁体   English   中英

立方体贴图纹理化问题(D3D11,C ++)

[英]Cubemap texturing issue (D3D11, C++)

我正在渲染的立方体贴图有一个纹理问题,似乎无法弄清楚。 我已经使用直接x的纹理工具生成了一个立方体贴图,然后使用

D3DX11CreateShaderResourceViewFromFile(device, L"cubemap.dds", 0, 0, &fullcubemap, 0);

立方体贴图纹理根本不是高质量的,它看起来真的很拉伸/变形。 我可以肯定地说,用于立方体贴图的图像正确匹配,但是目前还不是很好

在此处输入图片说明

我不确定为什么会这样。 是因为我的纹理太大/还是太小? 如果是由于纹理的大小,建议的纹理大小是多少? 我正在为立方体贴图而不是立方体使用球体。

编辑:

着色器:

cbuffer SkyboxConstantBuffer {
    float4x4 world;
    float4x4 view;
    float4x4 projection;
};

TextureCube gCubeMap;

SamplerState samTriLinearSam {
    Filter = MIN_MAG_MIP_LINEAR;
    AddressU = Wrap;
    AddressV = Wrap;
};

struct VertexIn {
    float4 position : POSITION;
};

struct VertexOut {
    float4 position : SV_POSITION;
    float4 spherePosition : POSITION;
};

VertexOut VS(VertexIn vin) {
    VertexOut vout = (VertexOut)0;

    vin.position.w = 1.0f;

    vout.position = mul(vin.position, world);
    vout.position = mul(vout.position, view);
    vout.position = mul(vout.position, projection);

    vout.spherePosition = vin.position;

    return vout;
}

float4 PS(VertexOut pin) : SV_Target {
    return gCubeMap.Sample(samTriLinearSam, pin.spherePosition);//float4(1.0, 0.5, 0.5, 1.0);
}

RasterizerState NoCull {
    CullMode = None;
};

DepthStencilState LessEqualDSS {
    DepthFunc = LESS_EQUAL;
};

technique11 SkyTech {
    pass p0 {
        SetVertexShader(CompileShader(vs_4_0, VS()));
        SetGeometryShader(NULL);
        SetPixelShader(CompileShader(ps_4_0, PS()));

        SetRasterizerState(NoCull);
        SetDepthStencilState(LessEqualDSS, 0);
    }
}

画:

immediateContext->OMSetRenderTargets(1, &renderTarget, nullptr);

XMMATRIX sworld, sview, sprojection;
SkyboxConstantBuffer scb;
sview = XMLoadFloat4x4(&_view);
sprojection = XMLoadFloat4x4(&_projection);
sworld = XMLoadFloat4x4(&_world);

scb.world = sworld;
scb.view = sview;
scb.projection = sprojection;

immediateContext->IASetIndexBuffer(cubeMapSphere->getIndexBuffer(), DXGI_FORMAT_R32_UINT, 0);
ID3D11Buffer*  vertexBuffer = cubeMapSphere->getVertexBuffer();
//ID3DX11EffectShaderResourceVariable * cMap;
////cMap = skyboxShader->GetVariableByName("gCubeMap")->AsShaderResource();
immediateContext->PSSetShaderResources(0, 1, &fullcubemap);//textures
//cMap->SetResource(fullcubemap);
immediateContext->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
immediateContext->VSSetShader(skyboxVertexShader, nullptr, 0);
immediateContext->VSSetConstantBuffers(0, 1, &skyboxConstantBuffer);
immediateContext->PSSetConstantBuffers(0, 1, &skyboxConstantBuffer);
immediateContext->PSSetShader(skyboxPixelShader, nullptr, 0);

immediateContext->UpdateSubresource(skyboxConstantBuffer, 0, nullptr, &scb, 0, 0);
immediateContext->DrawIndexed(cubeMapSphere->getIndexBufferSize(), 0, 0);

最初,我打算使用此代码段更新着色器中的TextureCube变量

ID3DX11EffectShaderResourceVariable * cMap;
cMap = skyboxShader->GetVariableByName("gCubeMap")->AsShaderResource();
cMap->SetResource(fullcubemap);

但这似乎没有任何效果,实际上,如果没有以下行,我正在使用的立方体贴图纹理所使用的球体与场景中另一个对象一起使用的纹理,那么也许这里发生了什么? 我不知道怎么办。

immediateContext->PSSetShaderResources(0, 1, &fullcubemap);//textures

编辑:可能不是上面的,意识到如果不进行更新,将应用旧纹理,因为在每次绘制后都不会擦除旧纹理。

编辑:尝试同时使用球体和立方体的立方贴图,仍然是相同的纹理问题。

编辑:尝试以不同方式加载着色器资源视图

D3DX11_IMAGE_LOAD_INFO loadSMInfo;
loadSMInfo.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;

ID3D11Texture2D* SMTexture = 0;
hr = D3DX11CreateTextureFromFile(device, L"cubemap.dds",
    &loadSMInfo, 0, (ID3D11Resource**)&SMTexture, 0);

D3D11_TEXTURE2D_DESC SMTextureDesc;
SMTexture->GetDesc(&SMTextureDesc);

D3D11_SHADER_RESOURCE_VIEW_DESC SMViewDesc;
SMViewDesc.Format = SMTextureDesc.Format;
SMViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
SMViewDesc.TextureCube.MipLevels = SMTextureDesc.MipLevels;
SMViewDesc.TextureCube.MostDetailedMip = 0;

hr = device->CreateShaderResourceView(SMTexture, &SMViewDesc, &fullcubemap);

仍然产生相同的输出,有什么想法吗?

编辑:尝试增加zfar距离,无论我输入什么值,纹理都保持完全相同。

具有增加的可视距离的第二个纹理的示例。 在此处输入图片说明

该纹理用于场景中的另一个对象,效果很好。

编辑:我一直试图弄乱纹理/对象的缩放比例 在此处输入图片说明

为此,我使用vin.position = vin.position * 50.0f;

这开始看起来像应该如何,但是,当我转动相机角度时,图像消失了,所以我显然知道这是不正确的,但是如果我可以正确地按像素或每个顶点缩放图像,我我肯定我可以得到最终结果。

在此处输入图片说明

编辑: 在此处输入图片说明

我可以确认立方体贴图正确渲染,我忽略了视图/投影空间,仅使用世界并设法获得了这个,这是我追求的高质量图像,只是不正确。 是的,这些面孔是不正确的,但是我现在对此并不感到困惑,可以很容易地交换它们,我只需要在正确的空间中以这种质量进行渲染即可。

在相机空间中时,是否考虑它是否在球体的外部/内部? 如果我的纹理在球体的外部,并且从内部可以看到,它看起来会不会一样?

问题是您的纹理尺寸较小,您将其应用在较大的表面上,使用更多的像素制作较大的纹理

它确认zfar和scale与之无关。

终于找到了问题,愚蠢的错误。

scb.world = XMMatrixTranspose(sworld);
scb.view = XMMatrixTranspose(sview);
scb.projection = XMMatrixTranspose(sprojection);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM