基于OPENGL绘制菱形
#include
#include
#include
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(1.0, 0.0, 0.0);
glTranslatef(0.0, 2.0, 0.0);
glPushMatrix();
glRotatef(90.0, 0.0, 0.0, 1.0); //逆时针旋转90角度
drawDiamond();
glPopMatrix();
glPushMatrix();
glTranslatef(-sqrt(3.0), -3.0, 0.0);
glRotatef(30.0, 0.0, 0.0, 1.0);
glColor3f(0.0, 1.0, 0.0);
drawDiamond();
glPopMatrix(); //从堆栈栈顶弹出一个矩阵为当前矩阵
glTranslatef(sqrt(3.0), -3.0, 0.0);
glRotatef(-30.0, 0.0, 0.0, 1.0);
glColor3f(0.0, 0.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();
}