Directx11给立方体上纹理出现上不了现象(图形为黑色),不知道什么原因,麻烦高手帮忙看下
这是通过龙书的Box代码改的。void BoxApp::DrawScene()
{
md3dImmediateContext->ClearRenderTargetView(mRenderTargetView, reinterpret_cast<const float*>(&Colors::LightSteelBlue));
md3dImmediateContext->ClearDepthStencilView(mDepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
md3dImmediateContext->IASetInputLayout(mInputLayout);
md3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
UINT stride = sizeof(Vertex);
UINT offset = 0;
md3dImmediateContext->IASetVertexBuffers(0, 1, &mBoxVB, &stride, &offset);
md3dImmediateContext->IASetIndexBuffer(mBoxIB, DXGI_FORMAT_R32_UINT, 0);
// Set constants
XMMATRIX world = XMLoadFloat4x4(&mWorld);
XMMATRIX view = XMLoadFloat4x4(&mView);
XMMATRIX proj = XMLoadFloat4x4(&mProj);
XMMATRIX worldViewProj = world * view*proj;
XMMATRIX I = XMMatrixIdentity();
mfxWorldViewProj->SetMatrix(reinterpret_cast<float*>(&worldViewProj));
mfxWorld->SetMatrix(reinterpret_cast<float*>(&world));
mfxTranform->SetMatrix(reinterpret_cast<float*>(&I));
D3DX11_TECHNIQUE_DESC techDesc;
mTech->GetDesc(&techDesc);
mfxDiffuseMapVar = mFX->GetVariableByName("gDiffuseMap")->AsShaderResource();
ID3D11ShaderResourceView* mDiffuseMapSRV;//获得纹理视图
HR(D3DX11CreateShaderResourceViewFromFile(md3dDevice,L"darkbrickdxt1.dds", 0, 0, &mDiffuseMapSRV, 0));
mfxDiffuseMapVar->SetResource(mDiffuseMapSRV);
for (UINT p = 0; p < techDesc.Passes; ++p)
{
mTech->GetPassByIndex(p)->Apply(0, md3dImmediateContext);
// 36 indices for the box.
md3dImmediateContext->DrawIndexed(36, 0, 0);
}
HR(mSwapChain->Present(0, 0));
}
//这上面是做图形的一个函数代码,其余变量都已经设置好了
下面是.fx代码
Texture2D gDiffuse;
SamplerState samAnisotropic
{
Filter = MIN_MAG_MIP_LINEAR;
MaxAnisotropy=4;
AddressU = WRAP;
AddressV = WRAP;
};
cbuffer cbPerObject
{
float4x4 gWorldViewProj;
float4x4 gTexTranform;//动态对纹理坐标进行变换
float4x4 gWorld;
};
struct VertexIn
{
float3 PosL : POSITION;
float2 Tex : TEXCOORD;
};
struct VertexOut
{
float4 PosH : SV_POSITION;
float2 Tex : TEXCOORD;
};
VertexOut VS(VertexIn vin)
{
VertexOut vout;
// Transform to homogeneous clip space.
vout.PosH = mul(float4(vin.PosL, 1.0f), gWorldViewProj);//通过更改世界矩阵来将局部坐标更换到世界区域
// Just pass vertex color into the pixel shader.
vout.Tex = mul(float4(vin.Tex, 0.0f, 1.0f), gTexTranform).xy;//若不需要变换,gTexTranform为I
return vout;
}
float4 PS(VertexOut pin) : SV_Target
{
float4 texcolor = gDiffuse.Sample(samAnisotropic,pin.Tex);//插值
return texcolor;
}
technique11 ColorTech
{
pass P0
{
SetVertexShader(CompileShader(vs_5_0, VS()));
SetGeometryShader(NULL);
SetPixelShader(CompileShader(ps_5_0, PS()));
}
}
我看了好久也没找出原因,因为第一次画,看了龙书来做的。