#include // use as needed for your system #include #include #include #include #define PI 3.14159265 GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; /* White diffuse light. */ GLfloat light_position[] = {10.0, 10.0, 10.0, 1.0}; /* Light location. */ GLfloat cylinder_mat[] = {0.f, .5f, 1.f, 1.f}; /* Material for the dome. */ /* This program demonstrates how to use quad strips to make a shape. In this case, a cylinder made up of strips. */ void cylinder(float r,int steps) { /* Set material properties for the cylinder. */ glMaterialfv(GL_FRONT, GL_DIFFUSE, cylinder_mat); glBegin(GL_QUAD_STRIP); GLfloat u = -PI; GLfloat h = 2*PI/steps; for(int n = 0;n <= steps;n++) { glNormal3f(r*cos(u),r*sin(u),0.0); glVertex3f(r*cos(u),r*sin(u),1.0); glVertex3f(r*cos(u),r*sin(u),-1.0); u += h; } glEnd(); } void resize(int w,int h) { glViewport(0,0,(GLsizei) w,(GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective( /* field of view in degree */ 40.0, /* aspect ratio */ (float) w/(float) h, /* Z near */ 5.0, /* Z far */ 50.0); glutPostRedisplay(); } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* Be sure to set the matrix mode before drawing. */ glMatrixMode(GL_MODELVIEW); /* Draw two copies of the cylinder. */ glPushMatrix(); glTranslatef(-2.0, 0.0, 0.0); glScalef(1.0,1.0,1.5); cylinder(0.5,20); glPopMatrix(); glPushMatrix(); glTranslatef(2.0, 0.0, 0.0); glRotatef(90,1.0, 0.0, 0.0); glScalef(1.0,1.0,1.5); cylinder(0.5,20); glPopMatrix(); glutSwapBuffers(); } void init(void) { /* Enable a single OpenGL light. */ glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); /* Turn on automatic normalization. */ glEnable(GL_NORMALIZE); /* Use depth buffering for hidden surface elimination. */ glEnable(GL_DEPTH_TEST); /* Make sure smooth shading is turned on. */ glShadeModel(GL_SMOOTH); /* Set the clear color to white. */ glClearColor(1.0, 1.0, 1.0, 1.0); /* Setup the view of the cube. */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective( /* field of view in degree */ 40.0, /* aspect ratio */ 1.0, /* Z near */ 5.0, /* Z far */ 50.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 2.0, 10.0, /* eye is at (0,2,10) */ 0.0, 0.0, 0.0, /* center is at (0,0,0) */ 0.0, 1.0, 0.); /* up is in positive Y direction */ } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitWindowSize(512, 512); glutInitWindowPosition(100,100); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow("Cylinders"); glutDisplayFunc(display); glutReshapeFunc(resize); init(); glutMainLoop(); return 0; /* ANSI C requires main to return int. */ }