#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();
}
opengl繪製Bezier Curve
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.