簡體   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