简体   繁体   English

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

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

I have a texture problem with the cubemap I'm rendering and can't seem to figure it out. 我正在渲染的立方体贴图有一个纹理问题,似乎无法弄清楚。 I've generated a cube map with direct x's texture tools and then read it using 我已经使用直接x的纹理工具生成了一个立方体贴图,然后使用

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

The cubemap texture is not high quality at all and it looks really stretched/distorted. 立方体贴图纹理根本不是高质量的,它看起来真的很拉伸/变形。 I can definitely tell that the images used for the cubemap match correctly, but it's not great at all at the moment 我可以肯定地说,用于立方体贴图的图像正确匹配,但是目前还不是很好

在此处输入图片说明

I'm not sure why this is happening. 我不确定为什么会这样。 Is it because my textures are too large/small or is it something else? 是因为我的纹理太大/还是太小? If it's due to the size of the textures, what is a recommended texture size? 如果是由于纹理的大小,建议的纹理大小是多少? I am using a sphere for the cubemap not a cube. 我正在为立方体贴图而不是立方体使用球体。

Edit: 编辑:

Shader: 着色器:

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

Draw: 画:

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

Initially I was planning to use this snippet to update the TextureCube variable in the shader 最初,我打算使用此代码段更新着色器中的TextureCube变量

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

But it seems that has no effect, and in fact, without the following line, the sphere I'm using for the cubemap textures with a texture used with another object in the scene, so perhaps there's something going on here? 但这似乎没有任何效果,实际上,如果没有以下行,我正在使用的立方体贴图纹理所使用的球体与场景中另一个对象一起使用的纹理,那么也许这里发生了什么? I'm not sure what though. 我不知道怎么办。

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

Edit: Probably not the above, realised that if this wasn't updated, the old texture would be applied as it's never wiped after each draw. 编辑:可能不是上面的,意识到如果不进行更新,将应用旧纹理,因为在每次绘制后都不会擦除旧纹理。

Edit: Tried the cubemap with both a sphere and a cube, still the same texture issue. 编辑:尝试同时使用球体和立方体的立方贴图,仍然是相同的纹理问题。

Edit: Tried loading the shader resource view differently 编辑:尝试以不同方式加载着色器资源视图

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

Still produces the same output, any ideas? 仍然产生相同的输出,有什么想法吗?

Edit: Tried increasing the zfar distance and the texture remains the exact same no matter what value I put. 编辑:尝试增加zfar距离,无论我输入什么值,纹理都保持完全相同。

Example with second texture with increased view distance. 具有增加的可视距离的第二个纹理的示例。 在此处输入图片说明

This texture is used on another object in my scene and comes out fine. 该纹理用于场景中的另一个对象,效果很好。

Edit: I have been trying to mess with the scaling of the texture/object 编辑:我一直试图弄乱纹理/对象的缩放比例 在此处输入图片说明

To achieve this I used vin.position = vin.position * 50.0f; 为此,我使用vin.position = vin.position * 50.0f;

This is beginning to look sort of like how it should, however, when I turn my camera angle, the image disappears so I obviously know this isn't correct, but if I could just scale the image per pixel or per vertex properly, I'm sure I could get the end result. 这开始看起来像应该如何,但是,当我转动相机角度时,图像消失了,所以我显然知道这是不正确的,但是如果我可以正确地按像素或每个顶点缩放图像,我我肯定我可以得到最终结果。

在此处输入图片说明

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

I can confirm the cubemap is rendering correctly, I was ignoring the view/projection space and just using world and managed to get this, which is the high quality image i'm after, just not correct. 我可以确认立方体贴图正确渲染,我忽略了视图/投影空间,仅使用世界并设法获得了这个,这是我追求的高质量图像,只是不正确。 Yes the faces are incorrect, but I'm not fussed about that now, it's easy enough to swap them around, I just need to get it rendering with this quality, in the correct space. 是的,这些面孔是不正确的,但是我现在对此并不感到困惑,可以很容易地交换它们,我只需要在正确的空间中以这种质量进行渲染即可。

When in camera space does it take into account whether or not it's the outside/inside of the sphere? 在相机空间中时,是否考虑它是否在球体的外部/内部? If my textures were over the outside of the sphere and I have the view from the inside, it's not going to look the same? 如果我的纹理在球体的外部,并且从内部可以看到,它看起来会不会一样?

Issue is with your texture size, its small, you are applying it on larger surface, Make larger textures with more pixels 问题是您的纹理尺寸较小,您将其应用在较大的表面上,使用更多的像素制作较大的纹理

Its confirm that zfar and scaling has nothing to do with it. 它确认zfar和scale与之无关。

Finally found the issue, silly mistake. 终于找到了问题,愚蠢的错误。

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