Directx:Obj文件解析到索引缓冲区

Directx: Obj file parsing to Index Buffer

本文关键字:索引 缓冲区 Obj 文件 Directx      更新时间:2023-10-16

我在获取索引缓冲区以正确显示一些导入的.obj时遇到问题。已考虑坐标系方向。(obj加载程序是硬编码的)可以很好地打印出索引,因此它正确地填充了要读入和设置的DWORD数组:

vector < vector <float> > index;
index = GetObjData(FilePath, VERTEXINDEXLIST);
for (int n=0; n<index.size(); n++){
m=m+index[n].size();
}

DWORD *IndexBuffer = new DWORD[m];

iBufferDescription.Usage                   =D3D11_USAGE_DEFAULT;
iBufferDescription.ByteWidth               =sizeof(DWORD)*m;
iBufferDescription.BindFlags               =D3D11_BIND_INDEX_BUFFER;
iBufferDescription.CPUAccessFlags          =0;
iBufferDescription.MiscFlags               =0;
D3D11_SUBRESOURCE_DATA iSRData;
iSRData.pSysMem=IndexBuffer;
Device->CreateBuffer(&iBufferDescription, &iSRData, &D3DIndexBuffer);
DeviceContext->IASetIndexBuffer(D3DIndexBuffer, DXGI_FORMAT_R16_UINT, 0);

这是Maya生成的.obj:

# This file uses centimeters as units for non-parametric coordinates.
mtllib tbox.mtl
g default
v -0.500000 -0.500000 -0.000000
v 0.500000 -0.500000 -0.000000
v -0.500000 0.500000 0.000000
v 0.500000 0.500000 0.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
vn 0.000000 -0.000000 1.000000
vn 0.000000 -0.000000 1.000000
vn 0.000000 -0.000000 1.000000
vn 0.000000 -0.000000 1.000000
s 1
g pPlane1
usemtl initialShadingGroup
f 1/1/1 2/2/2 3/3/3
f 3/3/3 2/2/2 4/4/4

具有4个顶点的二维正方形。通过函数传入,DWORD IndexBuffer的内容为:

2
1
0
3
1
2

(所有索引中为-1,以符合DirectX)我还要补充一点,其他一些东西也设置好了,比如ID3D11RasterizerState

D3D11_RASTERIZER_DESC DrawStyleState;
DrawStyleState.AntialiasedLineEnable=true;
DrawStyleState.CullMode=D3D11_CULL_NONE;
DrawStyleState.DepthBias=0;
DrawStyleState.FillMode=D3D11_FILL_SOLID;
DrawStyleState.DepthClipEnable=true;
DrawStyleState.MultisampleEnable=true;
DrawStyleState.FrontCounterClockwise=false;
DrawStyleState.ScissorEnable=false;
ID3D11RasterizerState *DS_State;
Device->CreateRasterizerState(&DrawStyleState, &DS_State);
DeviceContext->RSSetState(DS_State);

最后,渲染功能是相当标准的:

void Render(){
float ColorBlue[] = {0.3f,0.3f,1.0f,1.0f};
DeviceContext->ClearRenderTargetView(RenderTargetView,ColorBlue);
UINT stride=sizeof(VERTEX);
UINT Offset=0;
DeviceContext->IASetVertexBuffers(0,1,&D3DBuffer,&stride,&Offset);
DeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);    
DeviceContext->DrawIndexed(IndSz,0,0);
Swapchain->Present(0,0);
}

IndSz是一个全球性的,针对指数大小。哪个是正确的:我为它创建了一个调试器,并给出反馈:

4 vertices
6 index array size //element size = IndSz 
Index 0: 2
Index 1: 1
Index 2: 0
Index 3: 3
Index 4: 1
Index 5: 2

以上内容被解析为1个三角形。

|
| 
|   
|   
------

我已经得出结论,这可能是我所能想到的之外的另一个问题。我已经检查了挑选问题、排序、数据类型的有趣性、内存大小的疯狂性,现在似乎快要重写了。帮助

DWORD不会随着CPU的字大小而改变,听起来很有趣。DWORD始终32位,无论Windows上的主机CPU如何,它实际上都是Microsoft的uint32_t。另一方面,UINT是非常模糊的,并且可能随着CPU的字大小而改变。顺便说一下,与DXGI_FORMAT_R16_UINT配对的适当数据类型实际上是WORD(16位)。

然而,您在这里的实际问题似乎是您对D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP的使用。您显示的导入模型由两个面组成,三角形条带中的六个顶点将生成四个面。

如果这6个索引恰好产生2个三角形,则需要D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST