Question

In OpenGL, when I want to draw a filled circle, I'd do:

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

However, I could not find any equivalent for glPointSize in Direct-X. So I tried:

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

But none of these work. I cannot figure out why nothing works. The first one draws a gigantic circle that is black and the second one draws a long triangle.

Any ideas how I can draw a filled in circle or a point of a certain size and colour in Direct-X?

Était-ce utile?

La solution

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));
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top