2014-01-24 99 views
2

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

但是,我無法找到直接-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中繪製一個填充圓或某個特定大小和顏色的點?

回答

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