dimanche 13 mars 2022

Some problem about creating a cone with DX9.0

I want to create a cone with index in DirectX9.0. The vertex attribute is

struct CONEVERTEX
    {
        float x, y, z;
        float nx, ny, nz;
    };

No primitives are displayed after running the code.

I check the size of vertexbuffer and indexbuffer, the algorithm of generate index, U can't find any problem, and the every function relate draw primitive,but i still can't figure it out.

void CreatCone(){

unsigned int segments = 6;

std::vector<D3DXVECTOR3> position;
std::vector<D3DXVECTOR3> normals;


position.push_back(D3DXVECTOR3(0.0f, 0.0f, 1.0f));

normals.push_back(D3DXVECTOR3(0.0f, 0.0f, 1.0f));

// bottom vertex
for (size_t i = 1; i <= segments; i++)
{
    float phi = (2 * PI) / segments;

    D3DXVECTOR3 tempV3;

    tempV3.x = std::cos(phi * i);
    tempV3.y = std::sin(phi * i);
    tempV3.z = 0;

    position.push_back(tempV3);
}
position.push_back(D3DXVECTOR3(0, 0, 0)); 

for (size_t i = 1; i < segments; i++)
{
    normals.push_back(position[i]);
}
normals.push_back(D3DXVECTOR3(0, -1, 0));


std::vector<unsigned int> surfaceIndices;

// surfaceindex
for (size_t i = 0; i < segments; i++)
{
    if (i == segments - 1)
    {
        surfaceIndices.push_back(0);
        surfaceIndices.push_back(i + 1);
        surfaceIndices.push_back(1);
    }
    else
    {
        surfaceIndices.push_back(0);
        surfaceIndices.push_back(i + 1);
        surfaceIndices.push_back(i + 2);
    }
}

// bottom index
for (size_t i = 0; i < segments; i++)
{
    if (i == segments - 1)
    {
        surfaceIndices.push_back(segments + 1);
        surfaceIndices.push_back(i + 1);
        surfaceIndices.push_back(1);
    }
    else
    {
        surfaceIndices.push_back(segments + 1);
        surfaceIndices.push_back(i + 1);
        surfaceIndices.push_back(i + 2);
    }
}

std::vector<CONEVERTEX> vectices;
for (size_t i = 0; i < segments + 1; i++)
{
    CONEVERTEX temp;

    temp.x = position[i].x;
    temp.y = position[i].y;
    temp.z = position[i].z;

    temp.nx = normals[i].x;
    temp.ny = normals[i].y;
    temp.nz = normals[i].z;

    vectices.push_back(temp);
}


g_pd3dDevice->CreateVertexBuffer(sizeof(vectices), 0, D3DFVF_CUSTOMVERTEX,
                                 D3DPOOL_DEFAULT, &g_pVertexBuffer, NULL);

g_pd3dDevice->CreateIndexBuffer (sizeof(surfaceIndices), 0, D3DFMT_INDEX16,
                                 D3DPOOL_DEFAULT, &g_pIndexBuffer, NULL);

VOID* pVectices = NULL;
g_pVertexBuffer->Lock(0, 0, (void**)&pVectices, 0);
memcpy(pVectices, &vectices, sizeof(vectices));
g_pVertexBuffer->Unlock();

DWORD* pIndices = NULL;
g_pIndexBuffer->Lock(0, 0, (void**)&pIndices, 0);
memcpy(pIndices, &surfaceIndices, sizeof(surfaceIndices));
g_pIndexBuffer->Unlock();

}

Aucun commentaire:

Enregistrer un commentaire