我以前在DirectX中多次渲染3d对象,但是这次遇到了一个奇怪的问题,在这个问题上,我浪费了一天的时间弄清楚,却一无所获。我有一个DX11上下文,其中从obj文件加载3d模型。我必须制作一个Skybox,并找出一个好的起点是尝试渲染一个多维数据集,然后继续进行。我的图形管道绘制了很多东西,因此,除非有意取消绑定索引着色器,否则它将保持绑定状态。如果我尝试以绑定了多维数据集的索引缓冲区的方式渲染该多维数据集,那么它将无法正常工作。但是,如果我不绑定索引缓冲区,它将使用先前绑定的缓冲区(据我所知),并按预期方式怪异地渲染顶点。我一定在这里缺少重要的东西。我已经花了几个小时,但仍然无法解决问题。如果有人可以帮助我,我将不胜感激。
PS:我以为这些奇怪的索引仅用于测试,因为我认为它们是导致问题的原因。
我对这种行为感到困惑,我在这里一定错过了一些东西。
void SkyBox::CreateShader(Vertex * verts, size_t size_of_verts, WORD* indices, size_t size_of_indices)
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Shader Compilation ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ID3D10Blob* VS = nullptr;
ID3D10Blob* PS = nullptr;
ID3D10Blob* ErrMSG = nullptr;
D3DX11CompileFromFile("SkyShader.shader", 0, 0, "SKYMAP_VS", "vs_4_0", 0, 0, 0, &VS, &ErrMSG, 0);
D3DX11CompileFromFile("SkyShader.shader", 0, 0, "SKYMAP_PS", "ps_4_0", 0, 0, 0, &PS, &ErrMSG, 0);
char* p = new char[100];
// encapsulate both shaders into shader objects
if (!ErrMSG && VS && PS)
{
pRenderer->GetDevice()->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), nullptr, &pVertexShader);
pRenderer->GetDevice()->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), nullptr, &pPixelShader);
}
else
{
ELOG("The Shader file cannot be compiled. Please run this executable from the directory that contains the Shader file.");
exit(1);
}
D3D11_INPUT_ELEMENT_DESC IED[] =
{
{"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 },
};
UINT numElements = ARRAYSIZE(IED);
pRenderer->GetDevice()->CreateInputLayout(IED, 2, VS->GetBufferPointer(), VS->GetBufferSize(), &pLayout);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Vertex Buffer ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DYNAMIC;
bd.ByteWidth = size_of_verts;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
D3D11_SUBRESOURCE_DATA InitData;
ZeroMemory(&InitData, sizeof(InitData));
InitData.pSysMem = verts;
pRenderer->GetDevice()->CreateBuffer(&bd, &InitData, OUT &pVBuffer); // creating the buffer
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Index Buffer /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = size_of_indices;
bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
bd.CPUAccessFlags = 0;
ZeroMemory(&InitData, sizeof(InitData));
InitData.pSysMem = indices;
pRenderer->GetDevice()->CreateBuffer(&bd, &InitData, OUT &pIBuffer);
}
SkyBox::SkyBox()
{
}
SkyBox::SkyBox(CRenderer * pRenderer)
:
pRenderer(pRenderer)
{
}
void SkyBox::CreateTri()
{
Vertex verts[]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 0.0f, 0.0f, 0.0f},
{ 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f},
{-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f},
{-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f},
{-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 0.0f},
{ 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 0.0f},
{ 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f},
{ 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f},
{-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f},
{-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 0.0f}
};
WORD indices[] {
0, 1, 2,
3, 4, 5,
6, 7, 8,
9, 10, 11,
12, 13, 14,
15, 16, 17,
18, 19, 20,
21, 22, 23,
24, 25, 26,
27, 28, 29,
30, 31, 32,
33, 34, 35,
};
CubeMesh Cube;
Cube.CreateMesh();
CreateShader(verts, sizeof(Vertex) * 36, indices, sizeof(WORD) * 36);
}
void SkyBox::Render()
{
const UINT vStride = sizeof(Vertex);
const UINT vOffset = 0;
pRenderer->GetDeviceContext()->VSSetShader(pVertexShader, 0, 0);
pRenderer->GetDeviceContext()->PSSetShader(pPixelShader, 0, 0);
pRenderer->GetDeviceContext()->IASetInputLayout(pLayout);
pRenderer->GetDeviceContext()->IASetVertexBuffers(0, 1, &pVBuffer, &vStride, &vOffset);
pRenderer->GetDeviceContext()->IASetIndexBuffer(pIBuffer, DXGI_FORMAT_R16_UINT, 0);
pRenderer->GetDeviceContext()->DrawIndexed(36, 0, 0);
}
And here's the shader
float4 SKYMAP_VS( float4 Pos : POSITION, float4 Norm : NORMAL ) : SV_POSITION
{
return Pos;
}
float4 SKYMAP_PS( float4 Pos : SV_POSITION ) : SV_Target
{
return float4( 1.0f, 1.0f, 0.0f, 1.0f );
}
// I don't know how it could be simpler...
```c++
In SkyBox::Render(), if i don't set the index buffer it uses the previously bound buffer (as mentioned before) but if i bind the buffer it literally draws nothing. And yes, the the index buffer is created successfully.