当顶点缓冲区更改数据时,DirectX 9无法绘制线条

时间:2014-01-15 13:30:27

标签: c++ directx-9 vertex-buffer

我正在尝试绘制一条线,其中线的起点或终点随着时间的推移而变化(即每帧都有新的位置),但它不起作用。

我已经使用D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC并将池设置为D3DPOOL_DEFAULT来创建顶点缓冲区,但是当我更改顶点时它没有显示任何内容。

实际上,当缓冲区内的顶点数据没有改变时,该行仅显示和绘制。有人可以告诉我它有什么问题吗?

这是我得到的代码:

Line.h

#include"Mesh.h"

class Line : public Mesh
{
public:
    Line(D3DCOLOR color, D3DXVECTOR3 startPoint, D3DXVECTOR3 endPoint);
    virtual ~Line();

    void SetPoints(D3DXVECTOR3 startPoint, D3DXVECTOR3 endPoint);

    void Draw();
};

Line.cpp

#include"Line.h"

Line::Line(D3DCOLOR color, D3DXVECTOR3 startPoint, D3DXVECTOR3 endPoint) : Mesh()
{
    vertexCount = 2;
    vertices = new VERTEX[vertexCount];

    VERTEX vertex;
    vertex.position = startPoint;
    vertex.color = color;
    vertices[0] = vertex;

    vertex.position = endPoint;
    vertex.color = color;
    vertices[1] = vertex;

    DirectX::device->CreateVertexBuffer(sizeof(VERTEX) * vertexCount, D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, VERTEXFORMAT, D3DPOOL_DEFAULT, &vertexBuffer, NULL);

    VOID* vertexLocking;
    vertexBuffer->Lock(0, 0, (void**)&vertexLocking, 0);
    memcpy(vertexLocking, vertices, sizeof(VERTEX) * vertexCount);
    vertexBuffer->Unlock();
}

Line::~Line()
{

}

void Line::SetPoints(D3DXVECTOR3 startPoint, D3DXVECTOR3 endPoint)
{
    vertices[0].position = startPoint;
    vertices[1].position = endPoint;

    VOID* vertexLocking;
    vertexBuffer->Lock(0, 0, (void**)&vertexLocking, D3DLOCK_DISCARD);
    memcpy(vertexLocking, vertices, sizeof(VERTEX) * vertexCount);
    vertexBuffer->Unlock();

}

void Line::Draw()
{
    UINT passNum = 4;
    DirectX::currentShaderEffect->Begin(&passNum, 0);
    DirectX::currentShaderEffect->BeginPass(4);

    DirectX::device->SetStreamSource(0, vertexBuffer, 0, sizeof(VERTEX));
    DirectX::device->SetVertexDeclaration(DirectX::vertexDec);
    DirectX::device->DrawPrimitive(D3DPT_LINELIST, 0, 1);

    DirectX::currentShaderEffect->EndPass();
    DirectX::currentShaderEffect->End();
}

0 个答案:

没有答案