画点或填充圆圈

时间:2014-01-24 10:36:41

标签: c++ directx directx-9

在OpenGL中,当我想画一个圆圈时,我会这样做:

void DrawPoint(float X, float Y, float Z, float Radius) const
{
    glRasterPos2f(X, Y);
    glPointSize(Radius);
    glBegin(GL_POINTS);
        glVertex3f(X, Y, Z);
    glEnd();
    glPointSize(this->PointSize);
    glFlush();
}

但是,我在Direct-X中找不到glPointSize的任何等价物。所以我试过了:

struct Vector3
{
    double X, Y, Z;
};

#include <vector>
void DrawCircle1(float X, float Y, DWORD Color)
{
    const int sides = 20;
    std::vector<D3DXVECTOR3> points;
    for(int i = 0; i < sides; ++i)
    {
        double angle = D3DX_PI * 2 / sides * i;
        points.emplace_back(D3DXVECTOR3(sin(angle), cos(angle), 0));
    }

    device->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, sides, &points[0], sizeof(D3DXVECTOR3));
}

void DrawCircle2(float CenterX, float CenterY, float Radius, int Rotations)
{
    std::vector<D3DXVECTOR3> Points;
    float Theta = 2 * 3.1415926535897932384626433832795 / float(Rotations);
    float Cos = cosf(Theta);
    float Sine = sinf(Theta);
    float X = Radius, Y = 0, Temp = 0;

    for(int I = 0; I < Rotations; ++I)
    {
        Points.push_back(D3DXVECTOR3(X + CenterX, Y + CenterY, 0));
        Temp = X;
        X = Cos * X - Sine * Y;
        Y = Sine * Temp + Cos * Y;
    }

    device->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, Points.size(), &Points[0], sizeof(D3DXVECTOR3));
}

但这些都不起作用。我无法弄清楚为什么没有效果。第一个绘制一个黑色的巨大圆圈,第二个绘制一个长三角形。

如何在Direct-X中绘制填充圆圈或某个尺寸和颜色的点?

1 个答案:

答案 0 :(得分:2)

static const int CIRCLE_RESOLUTION = 64;

struct VERTEX_2D_DIF { // transformed colorized
    float x, y, z, rhw;
    D3DCOLOR color;
    static const DWORD FVF = D3DFVF_XYZRHW|D3DFVF_DIFFUSE;
};

void DrawCircleFilled(float mx, float my, float r, D3DCOLOR color)
{
    VERTEX_2D_DIF verts[CIRCLE_RESOLUTION+1];

    for (int i = 0; i < CIRCLE_RESOLUTION+1; i++)
    {
        verts[i].x = mx + r*cos(D3DX_PI*(i/(CIRCLE_RESOLUTION/2.0f)));
        verts[i].y = my + r*sin(D3DX_PI*(i/(CIRCLE_RESOLUTION/2.0f)));
        verts[i].z = 0;
        verts[i].rhw = 1;
        verts[i].color = color;
    }

    m_pDevice->SetFVF(VERTEX_2D_DIF::FVF);
    m_pDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, CIRCLE_RESOLUTION-1, &verts, sizeof(VERTEX_2D_DIF));
}