我试图从我的 DirectX 11 应用程序中的文件加载网格。作为基础,我采用了我自己的代码(现在渲染了一些基元),我想添加来自 DirectX 11 SDK 示例的“基本 HLSL”项目的网格创建/渲染代码(它使用例如 SDKmesh.h 和 DXUT.h从样品。
网格代码为:
class FeyModel : public Graphic::Model, public FeyGraphicElement{
protected:
CDXUTSDKMesh mesh;
public:
FeyModel(ID3D11Device * device, std::string filename, const Common::Point3D center){
mesh.Create( device, L"tiny\\tiny.sdkmesh", false ); //for now tiny.sdkmesh
}
void render(FeyShader * shader,
FeyCamera * camera,
ID3D11DeviceContext* context,
float t
){
UINT Strides[1];
UINT Offsets[1];
ID3D11Buffer* pVB[1];
pVB[0] = mesh.GetVB11( 0, 0 );
Strides[0] = ( UINT )mesh.GetVertexStride( 0, 0 );
Offsets[0] = 0;
context->IASetVertexBuffers( 0, 1, pVB, Strides, Offsets );
context->IASetIndexBuffer( mesh.GetIB11( 0 ), mesh.GetIBFormat11( 0 ), 0 );
SDKMESH_SUBSET* pSubset = NULL;
D3D11_PRIMITIVE_TOPOLOGY PrimType;
//! context->PSSetSamplers( 0, 1, &samLinear );
for( UINT subset = 0; subset < mesh.GetNumSubsets( 0 ); ++subset )
{
// Get the subset
pSubset = mesh.GetSubset( 0, subset );
PrimType = CDXUTSDKMesh::GetPrimitiveType11( ( SDKMESH_PRIMITIVE_TYPE )pSubset->PrimitiveType );
context->IASetPrimitiveTopology( PrimType );
// TODO: D3D11 - material loading
ID3D11ShaderResourceView* pDiffuseRV = mesh.GetMaterial( pSubset->MaterialID )->pDiffuseRV11;
context->PSSetShaderResources( 0, 1, &pDiffuseRV );
context->DrawIndexed( ( UINT )pSubset->IndexCount, 0, ( UINT )pSubset->VertexStart );
}
}
};
我还注意到,在示例中,有一个不同的顶点布局,所以我将着色器的类代码更改为(代码将在着色器的类构造函数中执行一次,在网格创建之前):
...
const D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
UINT numElements = ARRAYSIZE( layout );
// Create the input layout
hr = device->CreateInputLayout( layout, numElements, pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), &*vertexLayout );
pVSBlob->Release();
// Set the input layout
context->IASetInputLayout( *vertexLayout );
...
编译工作。但是在执行代码的过程中我得到了错误:
Access violation reading location 0x00000000
在 SDKmish.h 附近:
if (bSRGB) {
// This is a workaround so that we can load linearly, but sample in SRGB. Right now, we can't load
// as linear since D3DX will try to do conversion on load. Loading as TYPELESS doesn't work either, and
// loading as typed _UNORM doesn't allow us to create an SRGB view.
// on d3d11 featuer levels this is just a copy, but on 10L9 we must use a cpu side copy with 2 staging resources.
ID3D11Texture2D* unormStaging = NULL;
ID3D11Texture2D* srgbStaging = NULL;
D3D11_TEXTURE2D_DESC CopyDesc;
pRes->GetDesc( &CopyDesc );
pLoadInfo->BindFlags = 0;
pLoadInfo->CpuAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
pLoadInfo->Depth = 0;
pLoadInfo->Filter = D3DX11_FILTER_LINEAR;
pLoadInfo->FirstMipLevel = 0;
pLoadInfo->Format = CopyDesc.Format;
pLoadInfo->Height = CopyDesc.Height;
pLoadInfo->MipFilter = D3DX11_FILTER_LINEAR;
pLoadInfo->MiscFlags = CopyDesc.MiscFlags;
pLoadInfo->Usage = D3D11_USAGE_STAGING;
pLoadInfo->Width = CopyDesc.Width;
CopyDesc.BindFlags = 0;
CopyDesc.Usage = D3D11_USAGE_STAGING;
CopyDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
CopyDesc.Format = MAKE_SRGB(CopyDesc.Format);
hr = D3DX11CreateTextureFromFile( pDevice, pSrcFile, pLoadInfo, pPump, ( ID3D11Resource** )&unormStaging, NULL );
DXUT_SetDebugName( unormStaging, "CDXUTResourceCache" );
hr = pDevice->CreateTexture2D(&CopyDesc, NULL, &srgbStaging);
DXUT_SetDebugName( srgbStaging, "CDXUTResourceCache" );
pContext->CopyResource( srgbStaging, unormStaging );
ID3D11Texture2D* srgbGPU;
pRes->GetDesc( &CopyDesc );
CopyDesc.Format = MAKE_SRGB(CopyDesc.Format);
hr = pDevice->CreateTexture2D(&CopyDesc, NULL, &srgbGPU);
pContext->CopyResource( srgbGPU, srgbStaging );
SAFE_RELEASE(pRes);
SAFE_RELEASE(srgbStaging);
SAFE_RELEASE(unormStaging);
pRes = srgbGPU;
}
准确地说:
pContext->CopyResource( srgbStaging, unormStaging );
当我评论该函数的几行代码和代码中的某些内容时(我现在不记得那是哪一行了),我在窗口中看到了网格,但没有纹理。所以我猜这是有纹理的东西?我在正确的文件夹中有微小的模型和纹理,我还像上面发布的那样设置了顶点布局。
我还应该做什么?