答案 0 :(得分:0)
//Definitions
LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL;
struct SimpleVertexCombined{
D3DXVECTOR3 Pos;
SimpleVertexCombined(FLOAT X, FLOAT Y, FLOAT Z):Pos(X, Y, Z){}
};
d3ddev->CreateVertexBuffer(8*sizeof(SimpleVertexCombined),
0,
0,
D3DPOOL_MANAGED,
&v_buffer,
NULL);
SimpleVertexCombined* cube = 0;
v_buffer->Lock(0, 0, (void**)&cube, 0);
cube[0] = SimpleVertexCombined(-1.0f, -1.0f, -1.0f);
cube[1] = SimpleVertexCombined(-1.0f, 1.0f, -1.0f);
cube[2] = SimpleVertexCombined( 1.0f, 1.0f, -1.0f);
cube[3] = SimpleVertexCombined( 1.0f, -1.0f, -1.0f);
cube[4] = SimpleVertexCombined(-1.0f, -1.0f, 1.0f);
cube[5] = SimpleVertexCombined(-1.0f, 1.0f, 1.0f);
cube[6] = SimpleVertexCombined( 1.0f, 1.0f, 1.0f);
cube[7] = SimpleVertexCombined( 1.0f, -1.0f, 1.0f);
v_buffer->Unlock();
我认为这应该可行,首先我们使用CreateVertexBuffer()创建v_buffer,第一个值是它的大小,所以记得在添加更多顶点时更改该数字,然后我们使用指针立方体将数据传输到缓冲区。如果要从预先存在的数组传输数据,也可以在锁定和解锁之间使用memcpy(),如下所示:
SimpleVertexCombined verticesCombo[] = {
D3DXVECTOR3( 0.0f, 0.5f, 0.5f ),
D3DXVECTOR3( 0.0f, 0.0f, 0.5f ),
D3DXVECTOR3( 0.5f, -0.5f, 0.5f ),
D3DXVECTOR3( 0.5f, 0.0f, 0.0f ),
D3DXVECTOR3( -0.5f, -0.5f, 0.5f ),
D3DXVECTOR3( 0.0f, 0.5f, 0.0f ),
};
VOID* pVoid;
v_buffer->Lock(0, 0, (void**)&cube, 0);
memcpy(pVoid, verticesCombo, sizeof(verticesCombo));
v_buffer->Unlock();
所有这些都是在没有颜色的情况下完成的,如果你想要颜色,你必须将它添加到构造函数中,如果你想渲染你必须创建一个D3DVERTEXELEMENT9及其声明。