#include <windows.h> #include <GL/glut.h> #include <math.h> /*声明四个二次曲面物体*/ GLUquadricObj *quadObj1; GLUquadricObj *quadObj2; GLUquadricObj *quadObj3; GLUquadricObj *quadObj4; static float light_ambient[] = {0.1,0.1,0.1,1.0}; static float light_diffuse[] = {0.5,1.0,1.0,1.0}; static float light_position[] = {90.0,90.0,150.0,0.0}; static float front_mat_shininess[] = {60.0}; static float front_mat_specular[] = {0.2,0.2,0.2,1.0}; static float front_mat_diffuse[] = {0.5,0.5,0.28,1.0}; static float back_mat_shininess[] = {60.0}; static float back_mat_specular[] = {0.5,0.5,0.2,1.0}; static float back_mat_diffuse[] = {1.0,0.9,0.2,1.0}; static float Imodel_ambient[] = {1.0,1.0,1.0,1.0}; static float Imodel_twoside[] = {GL_TRUE}; static float Imodel_oneside[] = {GL_FALSE}; void myInit(void) { /*设置背景色*/ glClearColor(1.0,1.0,1.0,1.0);
glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL);
/*设置光照*/ glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient); glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse); glLightfv(GL_LIGHT0,GL_POSITION,light_position);
/*设置材质*/ glMaterialfv(GL_FRONT,GL_DIFFUSE,front_mat_diffuse); glMaterialfv(GL_FRONT,GL_SPECULAR,front_mat_specular); glMaterialfv(GL_FRONT,GL_SHININESS,front_mat_shininess);
glMaterialfv(GL_BACK,GL_DIFFUSE,back_mat_diffuse); glMaterialfv(GL_BACK,GL_SPECULAR,back_mat_specular); glMaterialfv(GL_BACK,GL_SHININESS,back_mat_shininess);
/*设置光照模型参数*/ glLightModelfv(GL_LIGHT_MODEL_AMBIENT,Imodel_ambient); glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE,Imodel_twoside);
/*激活关照*/ glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glShadeModel(GL_SMOOTH); } void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
/*创建四个二次曲面物体*/ quadObj1 = gluNewQuadric(); quadObj2 = gluNewQuadric(); quadObj3 = gluNewQuadric(); quadObj4 = gluNewQuadric();
/*绘制一个圆柱体*/ glPushMatrix(); gluQuadricDrawStyle(quadObj1,GLU_FILL); gluQuadricNormals(quadObj1,GL_FLAT); gluQuadricOrientation(quadObj1,GLU_INSIDE); gluQuadricTexture(quadObj1,GL_TRUE);
glColor3f(1.0,1.0,0.0); glRotatef(30,1.0,0.0,0.0); glRotatef(40,0.0,1.0,0.0); gluCylinder(quadObj1,2.0,2.0,9.0,20.0,8.0); glPopMatrix();
/*绘制一个球体*/ glPushMatrix(); gluQuadricDrawStyle(quadObj2,GLU_SILHOUETTE); glTranslatef(-5.0,-1.0,0.0); gluSphere(quadObj2,3.0,20.0,20.0); glPopMatrix();
/*绘制一个圆盘*/ glPushMatrix(); gluQuadricDrawStyle(quadObj3,GLU_LINE); glTranslatef(-2.0,4.0,0.0); gluDisk(quadObj3,2.0,5.0,15.0,10.0); glPopMatrix();
/*绘制一个部分圆盘*/ glPushMatrix(); gluQuadricDrawStyle(quadObj4,GLU_POINT); glTranslatef(-3.0,-7.0,0.0); gluPartialDisk(quadObj4,2.0,5.0,15.0,10.0,10.0,100.0); glPopMatrix();
/*删除四个二次曲面物体对象*/ gluDeleteQuadric(quadObj1); gluDeleteQuadric(quadObj2); gluDeleteQuadric(quadObj3); gluDeleteQuadric(quadObj4); glFlush(); } void myReshape(int w,int h) { glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0,(GLfloat)w/(GLfloat)h,1.0,50.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0,0.0,-25.0); } int main(int argc,char ** argv) { /*初始化*/ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(400,400); glutInitWindowPosition(100,100);
/*创建窗口*/ glutCreateWindow(" DRAW QUADRIC OBJECTS ");
/*绘制与显示*/ myInit(); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay);
glutMainLoop(); return 0; } |