2012-11-16 82 views
1

我使用GL_LINE_STRIP繪製了具有貝塞爾曲線算法的2個形狀。 我的形狀喜歡月亮,我想着色兩條曲線之間的空間。 我用GL_POLYGON,但它沒有產生我想要的結果。如何用GL_LINE_STRIP繪製形狀內部的顏色opengl

這是我的代碼:

float Points[4][3] = { 
{275,356,0}, 
{ 395,353,0 }, 
{ 435,325,0 }, 
{ 476,232,0 } 
        }; 
float SecendPoints[4][3] = { 
{ 476,232,0}, 
{441,331,0}, 
{369,372,0}, 
{283,388,0} 
        }; 
void init() 
    { 
    glClearColor(0.0,0.0,0.0,0.0); 
    glMatrixMode(GL_PROJECTION); 
    gluOrtho2D(0.0,640,0.0,480.0); 

    } 
void Display(void) 
    { 
    int segment = 20; 

    // clear the screen & depth buffer 
    glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); 

    glColor3f(1,1,1); 


    glColor3f(1,0,1); 

    // we will draw lots of little lines to make our curve 

    glBegin(GL_LINE_STRIP); 

for(int i=0;i!=segment;++i) { 

    // use the parametric time value 0 to 1 
    float t = (float)i/(segment-1); 

    // nice to pre-calculate 1.0f-t because we will need it frequently 
    float it = 1.0f-t; 

    // calculate blending functions 
    float b0 = t*t*t; 
    float b1 = 3*t*t*it; 
    float b2 = 3*t*it*it; 
    float b3 = it*it*it; 

    // calculate the x,y and z of the curve point by summing 
    // the Control vertices weighted by their respective blending 
    // functions 
    // 
    float x = b0*Points[0][0] + 
       b1*Points[1][0] + 
       b2*Points[2][0] + 
       b3*Points[3][0] ; 

    float y = b0*Points[0][1] + 
       b1*Points[1][1] + 
       b2*Points[2][1] + 
       b3*Points[3][1] ; 

    float z = b0*Points[0][2] + 
       b1*Points[1][2] + 
       b2*Points[2][2] + 
       b3*Points[3][2] ; 

    // specify the point 
    glVertex3f(x,y,z); 
} 


for(int i=0;i!=segment;++i) { 

    // use the parametric time value 0 to 1 
    float t = (float)i/(segment-1); 

    // nice to pre-calculate 1.0f-t because we will need it frequently 
    float it = 1.0f-t; 

    // calculate blending functions 
    float b0 = t*t*t; 
    float b1 = 3*t*t*it; 
    float b2 = 3*t*it*it; 
    float b3 = it*it*it; 

    // calculate the x,y and z of the curve point by summing 
    // the Control vertices weighted by their respective blending 
    // functions 
    // 
    float x = b0*SecendPoints[0][0] + 
       b1*SecendPoints[1][0] + 
       b2*SecendPoints[2][0] + 
       b3*SecendPoints[3][0] ; 

    float y = b0*SecendPoints[0][1] + 
       b1*SecendPoints[1][1] + 
       b2*SecendPoints[2][1] + 
       b3*SecendPoints[3][1] ; 

    float z = b0*SecendPoints[0][2] + 
       b1*SecendPoints[1][2] + 
       b2*SecendPoints[2][2] + 
       b3*SecendPoints[3][2] ; 

    // specify the point 
    glVertex3f(x,y,z); 
} 
glVertex3f(476,232,0); 
glEnd(); 
glFlush(); 
} 

任何想法?

回答

2
+0

感謝您來信genpfault.i'm對不起,我不明白你什麼意思,你可以寫一些示例代碼嗎? –

+0

感謝您的關注,但我的形狀是圓形的,我認爲這種算法不適用於我。我在項目中有許多圓形的形狀。 –

+0

我用一個循環來生成三角形和你的建議工作!謝謝 –