opengl绘制Bezier Curve

http://read.pudn.com/downloads16/sourcecode/windows/opengl/60869/OpenGL%20-%20Bezier%20Curve-1/Bezier%20Curve.c__.htm


#include "glut.h"  
  
int Line_Count = 80;   
// 线条数越大 曲面越平滑   
// 线条数越小 曲面越粗糙   
// 可以从 左下 右下 两条曲线得到对比  
  
// 左上曲线控制点  
GLfloat ControlPoints0[4][3] =   
{  
    { -4.0 ,  1.0 , 0.0 } , // 控制点在数组中的顺序决定曲线样式  
    { -1.0 ,  1.0 , 0.0 } ,  
    { -1.0 ,  4.0 , 0.0 } ,   
    { -4.0 ,  4.0 , 0.0 }     
};  
  
// 右上曲线控制点  
GLfloat ControlPoints1[4][3] =   
{  
    {  4.0 ,  1.0 , 0.0 } ,   
    {  1.0 ,  4.0 , 0.0 } ,   
    {  1.0 ,  1.0 , 0.0 } ,   
    {  4.0 ,  4.0 , 0.0 }     
};  
  
// 右下曲线控制点  
GLfloat ControlPoints2[4][3] =   
{  
    {  1.0 , -4.0 , 0.0 } ,  
    {  4.0 , -4.0 , 0.0 } ,   
    {  1.0 , -1.0 , 0.0 } ,  
    {  4.0 , -1.0 , 0.0 }  
};  
  
// 左下曲线控制点  
GLfloat ControlPoints3[4][3] =   
{  
    { -4.0 , -4.0 , 0.0 } ,  
    { -1.0 , -4.0 , 0.0 } ,   
    { -4.0 , -1.0 , 0.0 } ,  
    { -1.0 , -1.0 , 0.0 }  
};  
  
void Display( void )  
{  
    int i;  
  
    glClear( GL_COLOR_BUFFER_BIT );  
    glLoadIdentity();  
  
    // 先绘制控制点 再绘制曲线  
    // 左上曲线  
    glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ControlPoints0[0][0]);   
    glColor3f( 0 , 0 , 1.0 );  
    glBegin( GL_POINTS );  
        for( i = 0 ; i < 4 ; i++ )   
        {  
            glVertex3fv( ControlPoints0[i] );  
        }  
    glEnd();  
  
    glColor3f(0.0, 1.0, 0.0);  
    glBegin( GL_LINE_STRIP );  
        for( i = 0 ; i <= Line_Count ; i++ )   
        {  
            glEvalCoord1f( (GLfloat)i/Line_Count );  
        }    
    glEnd();  
  
    // 右上曲线  
    glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ControlPoints1[0][0]);  
    glColor3f( 1.0 , 0.0 , 0.0 );  
    glBegin( GL_POINTS );  
        for( i = 0 ; i < 4 ; i++ )   
        {  
            glVertex3fv( ControlPoints1[i] );  
        }  
    glEnd();  
  
    glColor3f(0.0, 1.0, 0.0);  
    glBegin( GL_LINE_STRIP );  
        for( i = 0 ; i <= Line_Count ; i++ )   
        {  
            glEvalCoord1f( (GLfloat)i/Line_Count );  
        }    
    glEnd();  
  
    // 右下曲线  
    glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ControlPoints2[0][0]);  
    glColor3f( 0.0 , 0.0 , 1.0 );  
    glBegin( GL_POINTS );  
        for( i = 0 ; i < 4 ; i++ )   
        {  
            glVertex3fv( ControlPoints2[i] );  
        }  
    glEnd();  
  
    glColor3f(0.0, 1.0, 0.0);  
    glBegin( GL_LINE_STRIP );  
        for( i = 0 ; i <= Line_Count ; i++ )   
        {  
            glEvalCoord1f( (GLfloat)i/Line_Count );  
        }    
    glEnd();  
  
    // 左下曲线  
    Line_Count = 8; // 降低线条数 曲线变粗糙  
    glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &ControlPoints3[0][0]);  
    glColor3f( 1.0 , 1.0 , 0.0 );  
    glBegin( GL_POINTS );  
        for( i = 0 ; i < 4 ; i++ )   
        {  
            glVertex3fv( ControlPoints3[i] );  
        }  
    glEnd();  
  
    glColor3f(0.0, 1.0, 0.0);  
    glBegin( GL_LINE_STRIP );  
        for( i = 0 ; i <= Line_Count ; i++ )   
        {  
            glEvalCoord1f( (GLfloat)i/Line_Count );  
        }    
    glEnd();  
  
    glFlush();  
}  
  
void Initialize()  
{  
    glClearColor( 0.0 , 0.0 , 0.0 , 0.0 );  
    glShadeModel( GL_SMOOTH );  
    glEnable(GL_MAP1_VERTEX_3);  
    glEnable( GL_LINE_SMOOTH ); // 平滑线条  
    glPointSize( 6.0 ); // 把点变大一点 看的清楚  
}  
  
void Reshape( int Width , int Height )  
{  
    glViewport( 0 , 0 , (GLsizei)Width , (GLsizei)Height );  
  
    glMatrixMode( GL_PROJECTION );  
    glLoadIdentity();  
  
    if( Width <= Height )  
    {  
        glOrtho( -5.0 , 5.0 , -5.0 * (GLfloat)Height / (GLfloat)Width , 5.0 * (GLfloat)Height / (GLfloat)Width , -5.0 , 5.0 );  
    }  
  
    else  
    {  
        glOrtho( -5.0 * (GLfloat)Width / (GLfloat)Height , 5.0 * (GLfloat)Width / (GLfloat)Height , -5.0 , 5.0 , -5.0 , 5.0 );  
    }  
  
    glMatrixMode( GL_MODELVIEW );  
    glLoadIdentity();  
}  
  
void main()  
{  
    glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );  
    glutInitWindowSize( 700 , 700 );  
    glutInitWindowPosition( 162 , 34 );  
    glutCreateWindow( "Bezier Curve" );  
    Initialize();  
    glutDisplayFunc( Display );  
    glutReshapeFunc( Reshape );  
    glutMainLoop();  
}  


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值