c++ - Directx:Obj文件解析到Index Buffer

标签 c++ directx game-engine indices .obj

我在获取索引缓冲区以正确显示某些导入的 .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 的字长而变化,这听起来很有趣。 DWORDalways 32 位,无论 Windows 上的主机 CPU 是什么,它实际上是 Microsoft 的 uint32_t。另一方面,UINT 非常模糊,很可能随 CPU 字长而变化。顺便说一句,与 DXGI_FORMAT_R16_UINT 配对的适当数据类型实际上是 WORD(16 位)。

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

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

关于c++ - Directx:Obj文件解析到Index Buffer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21153549/

相关文章:

c++ - 在 Win32 C 中查询 Sqlite DB 的最佳方式

c++ - 如何在终止前强制输入?

c++ - 使用 WPF 获取 DirectX View /窗口?

windows - 如何找出实际屏幕刷新率(不是四舍五入的数字)

javascript - 除了 Impact,还有哪些 JavaScript 游戏引擎?

c++ - 这段代码是否泄漏内存?

c++ - Qt 信号和插槽 - 没有任何反应

c++ - 函数 undefined reference C++

c# - RPG库存: Adding and Deleting Items

javascript - 有没有办法在 Canvas 上绘制部分超出屏幕范围的内容?