OpenGL入门笔记(十六)

#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;
}


200782801.jpg

你可能感兴趣的:(OpenGL)