我在为网格创建顶点和索引缓冲区时尝试使用动态数组,如下所示:
// 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)
我曾经遇到过这个问题。这是因为面部指数的顺序是倒退的。而不是顺时针索引,它需要逆时针索引。这意味着,(0,1,2)没有工作,但(0,2,1)没有。我想这取决于你如何设置矩阵和背面剔除。