简体   繁体   English

Direct3D 11 使用向量而不是数组

[英]Direct3D 11 Using Vectors instead of Arrays

So basically I am trying to learn Direct3D 11. I have got my program running and importing OBJ files with good success.所以基本上我正在尝试学习 Direct3D 11。我已经让我的程序运行并成功导入 OBJ 文件。 But now I have decided to use vectors instead of arrays since I may want to change the models that I load, and would like it to automatically allocate the required memory.但是现在我决定使用向量而不是数组,因为我可能想要更改我加载的模型,并希望它自动分配所需的内存。 Anyway, it now only seems to display half of the model, I believe it is to do with the ByteWidth and/or the stride, but I have now tried so many different combinations and different setups that I feel I am going round in circles, and I am certain that it is within this block of code here:无论如何,它现在似乎只显示模型的一半,我相信这与 ByteWidth 和/或步幅有关,但我现在尝试了很多不同的组合和不同的设置,我觉得我在绕圈子,我确定它在此代码块中:

ID3D11Buffer *pVertexBuffer;
D3D11_BUFFER_DESC bd;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;   
bd.ByteWidth = vertices.size() * sizeof(Vertex);  
bd.CPUAccessFlags = 0u;
bd.MiscFlags = 0u;
bd.Usage = D3D11_USAGE_DEFAULT;                
bd.StructureByteStride = sizeof(Vertex);
D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = vertices.data();
dev->CreateBuffer(&bd, &sd, &pVertexBuffer);
const UINT stride = sizeof(Vertex);
const UINT offset = 0u;
devcon->IASetVertexBuffers(0u, 1u, &pVertexBuffer, &stride, &offset);

ID3D11Buffer *pIndexBuffer;
D3D11_BUFFER_DESC ibd;
ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;   
ibd.ByteWidth = indices.size() * sizeof(Index);  
ibd.CPUAccessFlags = 0u;
ibd.MiscFlags = 0u;
ibd.Usage = D3D11_USAGE_DEFAULT;                
ibd.StructureByteStride = sizeof(Index);
D3D11_SUBRESOURCE_DATA isd = {};
isd.pSysMem = indices.data();
dev->CreateBuffer(&ibd, &isd, &pIndexBuffer);
devcon->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R16_UINT, 0u);

for reference, I have pastebinned the full code: here作为参考,我已经粘贴了完整的代码: here

Here's my guess.这是我的猜测。

devcon->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R16_UINT, 0u);

Right here you are telling directx that your indices are uint16_t however you are declaring your indices as 'unsigned int'.就在这里,您告诉 directx 您的索引是 uint16_t 但是您将索引声明为“无符号整数”。

This is bad.这不好。

unsigned int is typically 32 bits (4 bytes). unsigned int 通常为 32 位(4 字节)。 And uint16 is 16 bits (2 bytes). uint16 是 16 位(2 个字节)。

Therefor there is a mismatch.因此存在不匹配。

struct Index{
    unsigned int a, b, c;
};

You shouldn't use unsigned int.你不应该使用 unsigned int。 You should use uint16_t.您应该使用 uint16_t。

If this isn't the problem then you should make this change anyway because you are just asking for trouble in my opinion.如果这不是问题,那么无论如何您都应该进行此更改,因为在我看来您只是在自找麻烦。

When working with graphics APIs you be very careful with your chosen types to avoid these types of runtime issues.使用图形 API 时,您要非常小心选择的类型,以避免这些类型的运行时问题。

Here is the link to the reference on fixed width types: https://en.cppreference.com/w/cpp/types/integer这是固定宽度类型参考的链接: https : //en.cppreference.com/w/cpp/types/integer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM