Monday, March 4, 2013

Cylinder and Parallelopiped

6) Write an OpenGL Program to create a cylinder and a parallelepiped by extruding  a circle and quadrilateral respectively. Allow the user to specify the circle and the quadrilateral.

#include<stdlib.h>
#include<GL/glut.h>
#include<math.h>
#define ONERAD 0.0174

float z=0;
void myinit()
{
    glClearColor(1.0,1.0,1.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-10,40.0,-10,30.0,-20,20);
}
void drawObjects(float radius,float u,float v)
{float i;
float x1=5,y1=5,x2=15,y2=5,x3=15,y3=15,x4=5,y4=15;

    glPushMatrix();
    glRotatef(30,1,0,0);
    glRotatef(40,0,1,0);
  while(z<10)
   {
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glColor3f(1.0,0.0,0.0);
   for (i=0; i<360; i+=6)
   {
    glBegin(GL_POLYGON);
    float angle = i*ONERAD;
    glVertex3f(u+cos(angle)*radius,v+(sin(angle))*radius,0);
    angle=(i+6)*ONERAD;
    glVertex3f(u+cos(angle)*radius,v+(sin(angle))*radius,0);
    glVertex3f(u+cos(angle)*radius,v+(sin(angle))*radius,z);
    angle=i*ONERAD;
    glVertex3f(u+cos(angle)*radius,v+(sin(angle))*radius,z);
    glEnd();
   }
   glColor3f(0.0,0.0,1.0);
   glBegin(GL_POLYGON);
   glVertex3f(x1,y1,0);
   glVertex3f(x2,y2,0);
   glVertex3f(x2,y2,z);
   glVertex3f(x1,y1,z);
   glEnd();
   glBegin(GL_POLYGON);
   glVertex3f(x2,y2,0);
   glVertex3f(x3,y3,0);
   glVertex3f(x3,y3,z);
   glVertex3f(x2,y2,z);
   glEnd();
   glBegin(GL_POLYGON);
   glVertex3f(x3,y3,0);
   glVertex3f(x4,y4,0);
   glVertex3f(x4,y4,z);
   glVertex3f(x3,y3,z);
   glEnd();
   glBegin(GL_POLYGON);
   glVertex3f(x4,y4,0);
   glVertex3f(x1,y1,0);
   glVertex3f(x1,y1,z);
    glVertex3f(x4,y4,z);
    glEnd();
   z++;
   glFlush();
   glutSwapBuffers();
    }
  glPopMatrix();
}
void display()
{int j;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    drawObjects(5.0,0.0,0.0);
   glFlush();
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB|GLUT_DEPTH );
    glutInitWindowSize(600,600);
    glutCreateWindow("6");
    glutDisplayFunc(display);
    myinit();
    glEnable(GL_DEPTH_TEST);
    glutMainLoop();
return 0;
}

No comments:

Post a Comment