来自顶点的 VertexBuffer

发布于 2024-10-05 02:08:08 字数 201 浏览 0 评论 0原文

我需要从顶点创建一个顶点缓冲区。 MSDN 中的这个教程非常棒。

但这一点适用于 DirectX10+。我正在使用 DirectX9。在这里如何实现同样的功能?

谢谢。

I need to create a vertex buffer out of vertices. This tutorial in MSDN is splendid.

But this one holds good for DirectX10+. I am using DirectX9. How to accomplish the same here?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小情绪 2024-10-12 02:08:08
//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 并对其进行声明。

//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();

I think this should work, First we create the v_buffer using CreateVertexBuffer(), the first value is its size so remember to change that number if you're adding more vertices, and then we use the pointer cube to transfer data to the buffer. You can also use memcpy() between the lock and unlock if you want to transfer data from a pre-existing array, like this:

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();

All of this is done without colour, if you want colour you have to add it to the constructor, also if you want to render you have to create a D3DVERTEXELEMENT9 and a declaration to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文