#include
//旋转参数
static GLfloat xRot = 180;
static GLfloat yRot = 180;
void doMyInit() {
glClearColor(0.0, 0.0, 0.0, 0.0); // Set the clear color to black
// Specify the boundaries of the viewing window
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0); // The para are: (left, right, bottom, top)
glMatrixMode(GL_MODELVIEW);
}
void snowman() {
GLUquadricObj *pObj; // declare Quadric Object
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the frame buffer
// Save the matrix state and put the snowman in place
glPushMatrix();
// Move snowman back and do in place rotation
glTranslatef(0.0f, -1.0f, -5.0f);
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
// create Quadric object
pObj = gluNewQuadric();
gluQuadricNormals(pObj, GLU_SMOOTH);
// Main Body
glPushMatrix();
glColor3f(1.0f, 1.0f, 1.0f);
gluSphere(pObj, .40f, 26, 13); // Bottom
glTranslatef(0.0f, .550f, 0.0f);
gluSphere(pObj, .3f, 26, 13); // Mid section
glTranslatef(0.0f, 0.45f, 0.0f);
gluSphere(pObj, 0.24f, 26, 13); // Head
// Eyes
glColor3f(0.0f, 0.0f, 0.0f);
glTranslatef(0.1f, 0.1f, 0.21f);
gluSphere(pObj, 0.02f, 26, 13);
glTranslatef(-0.2f, 0.0f, 0.0f);
gluSphere(pObj, 0.02f, 26, 13);
// Nose
glColor3f(1.0f, 0.3f, 0.3f);
glTranslatef(0.1f, -0.12f, 0.0f);
gluCylinder(pObj, 0.04f, 0.0f, 0.3f, 26, 13);
glPopMatrix();
// Hat
glPushMatrix();
glColor3f(0.0f, 0.0f, 0.0f);
glTranslatef(0.0f, 1.17f, 0.0f);
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
gluCylinder(pObj, 0.17f, 0.17f, 0.4f, 26, 13);
// Hat brim
glDisable(GL_CULL_FACE);
gluDisk(pObj, 0.17f, 0.28f, 26, 13);
glEnable(GL_CULL_FACE);
glTranslatef(0.0f, 0.0f, 0.40f);
gluDisk(pObj, 0.0f, 0.17f, 26, 13);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
glFlush(); // Force to display the new drawings immediately
}
void reshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);//设置视口
glMatrixMode(GL_PROJECTION);//指明当前矩阵为GL_PROJECTION
glLoadIdentity();//将当前矩阵置换为单位阵
if (w <= h)
gluOrtho2D(-1.0, 1.5, -1.5, 1.5*(GLfloat)h / (GLfloat)w);//定义二维正视投影矩阵
else
gluOrtho2D(-1.0, 1.5*(GLfloat)w / (GLfloat)h, -1.5, 1.5);
glMatrixMode(GL_MODELVIEW);//指明当前矩阵为GL_MODELVIEW
}
int main(int argc, char ** argv)
{
//初始化
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
//创建窗口
glutCreateWindow("SnowMan");
/*绘制与显示*/
doMyInit();
glutReshapeFunc(reshape);
glutDisplayFunc(snowman);
//main event loop
glutMainLoop();
return(0);
}