Directx:Obj文件解析到索引缓冲区
Directx: Obj file parsing to Index Buffer
我在获取索引缓冲区以正确显示一些导入的.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
。
相关文章:
- 是否可以在 OpenGL 中的同一调用中呈现两个具有不同索引起点的不同缓冲区?
- 缓冲区溢出 - 数组索引越界(严重)
- DirectX11只绘制填充索引和顶点缓冲区中的一半顶点
- tiny_obj_loader到 Direct3D 顶点缓冲区和索引缓冲区数组
- DirectX:如果绑定索引缓冲区但不绑定顶点缓冲区,会发生什么情况
- 无法获取索引缓冲区以在 OpenGL 中绘制正方形
- 从索引缓冲区中获取索引
- 自动生成的顶点缓冲区和索引缓冲区不起作用
- 在DirectX 11中使用索引缓冲区;它怎么知道
- 如何从顶点/索引缓冲区创建 CGAL Nef_polyhedron_3
- Directx11如何管理多个顶点/索引缓冲区
- DirectX 9 裝置.在渲染时创建顶点和索引缓冲区.(2 个线程)
- 为什么我的索引缓冲区未与gldrawarraysinstanced一起使用(OpenGles 3.0)
- 顶点/索引缓冲区混淆,带有OpenGL 3.3
- Directx:Obj文件解析到索引缓冲区
- 写入索引缓冲区时崩溃
- 使用索引缓冲区对象时出现奇怪的分段错误
- 如何读取顶点/索引缓冲区
- DX10+多顶点缓冲区,单索引缓冲区
- (DirectX 11)动态顶点/索引缓冲区实现恒定的场景内容变化