使用动态缓冲区在 Directx 9 中显示空网格
Using dynamic buffer displays empty mesh in Directx 9
我在为网格创建顶点缓冲区和索引缓冲区时尝试使用动态数组,如下所示:
// Create the mesh with a call to D3DXCreateMeshFVF
D3DXCreateMeshFVF(caras_a_dibujar, // NumFaces
cantidad_de_puntos, // NumVertices
D3DXMESH_MANAGED, // Options
CUSTOMFVF, // FVF
d3ddev, // pDevice
&esfera_purete); // ppMesh
CUSTOMVERTEX* g_Vertices = NULL; // Pointer to CUSTOMVERTEX, initialize to nothing.
g_Vertices = new CUSTOMVERTEX[cantidad_de_puntos]; // Allocate cantidad_de_puntos and save pointer in g_Vertices.
for (int n = 0; n < cantidad_de_puntos; n++) g_Vertices[n] = { puntos_unicos[n].x, puntos_unicos[n].y, puntos_unicos[n].z, { puntos_unicos[n].x, puntos_unicos[n].y, puntos_unicos[n].z }, 0.5f + atan2f(puntos_unicos[n].z, puntos_unicos[n].x) / (2 * D3DX_PI), 0.5f - asinf(puntos_unicos[n].y) / D3DX_PI };
VOID* pVertices;
// Lock the vertex buffer
esfera_purete->LockVertexBuffer(D3DLOCK_DISCARD, (void**)&pVertices);
// Copy the vertices into the buffer
memcpy(pVertices, g_Vertices, sizeof(g_Vertices));
// Unlock the vertex buffer
esfera_purete->UnlockVertexBuffer();
WORD* IndexData = NULL; // Pointer to WORD, initialize to nothing.
IndexData = new WORD[caras_a_dibujar * 3]; // Allocate caras_a_dibujar * 3 and save pointer in IndexData.
for (int n = 0; n < caras_a_dibujar * 3; n++) IndexData[n] = indice[n];
// Prepare to copy the indices into the index buffer
VOID* IndexPtr;
// Lock the index buffer
esfera_purete->LockIndexBuffer(0, &IndexPtr);
// Copy the indices into the buffer
memcpy(IndexPtr, IndexData, sizeof(IndexData));
// Unlock the buffer
esfera_purete->UnlockIndexBuffer();
delete[] g_Vertices; // free memory pointed to by g_Vertices.
g_Vertices = NULL; // Clear a to prevent using invalid memory reference.
delete[] IndexData; // free memory pointed to by IndexData.
IndexData = NULL; // Clear a to prevent using invalid memory reference.
cantidad_faces = esfera_purete->GetNumFaces();
代码可以顺利编译和运行,甚至可以使用 GetNumFaces() 和 GetNumVertices() 为我提供网格中正确数量的面和顶点,但网格不会渲染,它只是不可见。我是第一次使用这种动态内存分配,所以我确定问题出在以下几行:
CUSTOMVERTEX* g_Vertices = NULL; // Pointer to CUSTOMVERTEX, initialize to nothing.
g_Vertices = new CUSTOMVERTEX[cantidad_de_puntos]; // Allocate cantidad_de_puntos and save pointer in g_Vertices.
和:
WORD* IndexData = NULL; // Pointer to WORD, initialize to nothing.
IndexData = new WORD[caras_a_dibujar * 3]; // Allocate caras_a_dibujar * 3 and save pointer in IndexData.
但是,怎么了?
我曾经遇到过这个问题。这是因为面部索引的顺序是倒过来的。它需要逆时针索引而不是顺时针索引。这意味着,(0,1,2) 不起作用,但 (0,2,1) 起作用了。我想这取决于你如何设置矩阵和背面剔除。
我在为网格创建顶点缓冲区和索引缓冲区时尝试使用动态数组,如下所示:
// Create the mesh with a call to D3DXCreateMeshFVF
D3DXCreateMeshFVF(caras_a_dibujar, // NumFaces
cantidad_de_puntos, // NumVertices
D3DXMESH_MANAGED, // Options
CUSTOMFVF, // FVF
d3ddev, // pDevice
&esfera_purete); // ppMesh
CUSTOMVERTEX* g_Vertices = NULL; // Pointer to CUSTOMVERTEX, initialize to nothing.
g_Vertices = new CUSTOMVERTEX[cantidad_de_puntos]; // Allocate cantidad_de_puntos and save pointer in g_Vertices.
for (int n = 0; n < cantidad_de_puntos; n++) g_Vertices[n] = { puntos_unicos[n].x, puntos_unicos[n].y, puntos_unicos[n].z, { puntos_unicos[n].x, puntos_unicos[n].y, puntos_unicos[n].z }, 0.5f + atan2f(puntos_unicos[n].z, puntos_unicos[n].x) / (2 * D3DX_PI), 0.5f - asinf(puntos_unicos[n].y) / D3DX_PI };
VOID* pVertices;
// Lock the vertex buffer
esfera_purete->LockVertexBuffer(D3DLOCK_DISCARD, (void**)&pVertices);
// Copy the vertices into the buffer
memcpy(pVertices, g_Vertices, sizeof(g_Vertices));
// Unlock the vertex buffer
esfera_purete->UnlockVertexBuffer();
WORD* IndexData = NULL; // Pointer to WORD, initialize to nothing.
IndexData = new WORD[caras_a_dibujar * 3]; // Allocate caras_a_dibujar * 3 and save pointer in IndexData.
for (int n = 0; n < caras_a_dibujar * 3; n++) IndexData[n] = indice[n];
// Prepare to copy the indices into the index buffer
VOID* IndexPtr;
// Lock the index buffer
esfera_purete->LockIndexBuffer(0, &IndexPtr);
// Copy the indices into the buffer
memcpy(IndexPtr, IndexData, sizeof(IndexData));
// Unlock the buffer
esfera_purete->UnlockIndexBuffer();
delete[] g_Vertices; // free memory pointed to by g_Vertices.
g_Vertices = NULL; // Clear a to prevent using invalid memory reference.
delete[] IndexData; // free memory pointed to by IndexData.
IndexData = NULL; // Clear a to prevent using invalid memory reference.
cantidad_faces = esfera_purete->GetNumFaces();
代码可以顺利编译和运行,甚至可以使用 GetNumFaces() 和 GetNumVertices() 为我提供网格中正确数量的面和顶点,但网格不会渲染,它只是不可见。我是第一次使用这种动态内存分配,所以我确定问题出在以下几行:
CUSTOMVERTEX* g_Vertices = NULL; // Pointer to CUSTOMVERTEX, initialize to nothing.
g_Vertices = new CUSTOMVERTEX[cantidad_de_puntos]; // Allocate cantidad_de_puntos and save pointer in g_Vertices.
和:
WORD* IndexData = NULL; // Pointer to WORD, initialize to nothing.
IndexData = new WORD[caras_a_dibujar * 3]; // Allocate caras_a_dibujar * 3 and save pointer in IndexData.
但是,怎么了?
我曾经遇到过这个问题。这是因为面部索引的顺序是倒过来的。它需要逆时针索引而不是顺时针索引。这意味着,(0,1,2) 不起作用,但 (0,2,1) 起作用了。我想这取决于你如何设置矩阵和背面剔除。