2012-11-16 52 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

我用一个循环来生成三角形和你的建议工作!谢谢 –