实验三的作业:
绘制如下图形:
提示:
(1)写一个绘制菱形的函数drawDiamond(void);
void drawDiamond(void) //绘制中心在原点的菱形
{
glBegin (GL_POLYGON); //顶点指定需要按逆时针方向
glVertex2f (0.0f,-1.0f);//下点
glVertex2f (2.0f,0.0f);//右点
glVertex2f (0.0f, 1.0f);//上点
glVertex2f (-2.0f,0.0f);//左点
glEnd ( );
}
void init (void)
{
glClearColor (1.0, 1.0, 1.0, 0.0);
glMatrixMode (GL_PROJECTION);
gluOrtho2D (-5.0, 5.0, -5.0, 5.0); //设置显示的范围是X:-5.0~5.0, Y:-5.0~5.0
glMatrixMode (GL_MODELVIEW);
}
void drawDiamond(void) //绘制中心在原点的菱形
{
glBegin (GL_POLYGON); //顶点指定需要按逆时针方向
glVertex2f (0.0f,-1.0f);//下点
glVertex2f (2.0f,0.0f);//右点
glVertex2f (0.0f, 1.0f);//上点
glVertex2f (-2.0f,0.0f);//左点
glEnd ( );
}
void myDraw (void)
{
glClear (GL_COLOR_BUFFER_BIT); //清空
glLoadIdentity();
glColor3f(0.0,0.0,1.0);
drawDiamond();
glRotatef(90,0.0,0.0,1.0);
glTranslatef(1.0,0.0,0.0);
glColor3f(1.0,0.0,0.0);
drawDiamond();
glLoadIdentity();
glTranslatef(-1.78,-2.0,0.0);
glRotatef(30.0,0.0,0.0,1.0);
glColor3f(0.0,1.0,0.0);
drawDiamond();
glLoadIdentity();
glTranslatef(1.78,-2.0,0.0);
glRotatef(-30.0,0.0,0.0,1.0);
glColor3f(0.0,1.0,1.0);
drawDiamond();
glFlush ( );
}
void main (int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition (0, 0);
glutInitWindowSize (600, 600);
glutCreateWindow ("几何变换函数综合示例");
init();
glutDisplayFunc (myDraw);
glutMainLoop ( );
}
我的实验效果图: