Advertisement
Guest User

Untitled

a guest
Jul 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>  
  2.  #include <stdlib.h>  
  3.  #include <math.h>  
  4. #include <GL/glut.h>
  5.    
  6.    
  7.  int rightHandAngle = 180; // Angle of lower right hand  
  8.  bool mouseLeftState = false; // Is left mouse clicked?  
  9.  bool mouseRightState = false; // Is right mouse clicked?  
  10.  int rightHandMove = 2; // If left mouse clicked, right hand will rotate 2 degrees.  
  11.  int leftLegMove = 1; // If right mouse clicked, left leg will move by 1.  
  12.  int rightLegMove = -1; // If right mouse clicked, right leg will move by 1.  
  13.  int leftLegAngle = 90; // If right mouse clicked, this variable will be used to rotate left leg and it initialized to 90 degrees for first position of leg.  
  14.  int rightLegAngle = 90; // If right mouse clicked, this variable will be used to rotate right leg and it initialized to 90 degrees for first position of leg.  
  15.  float zMove = 0.0; // If right mouse clicked, this variable will be used to change position of object. Object will move on z-axis.  
  16.    
  17.  void display(){  
  18.          glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
  19.    
  20.          glPushMatrix(); //BODY  
  21.          glColor3f(1.0, 0.647, 0.0);  
  22.          glTranslatef(0.0, 221, zMove);  
  23.          glRotatef(90, 1.0, 0.0, 0.0);  
  24.          GLUquadricObj* body = gluNewQuadric();  
  25.          gluQuadricDrawStyle(body, GLU_FILL);  
  26.          gluCylinder(body, 120, 120, 300, 30, 30);  
  27.          glPopMatrix();  
  28.    
  29.          glPushMatrix(); //LEFT UPPER ARM  
  30.          glColor3f(0.180f, 0.545f, 0.341f);  
  31.          glTranslatef(-80, 160, zMove);  
  32.          glRotatef(-45, 0.0, 0.0, 1.0);  
  33.          glRotatef(90, 1.0, 0.0, 0.0);  
  34.          GLUquadricObj* leftUpperArm = gluNewQuadric();  
  35.          gluQuadricDrawStyle(leftUpperArm, GLU_FILL);  
  36.          gluCylinder(leftUpperArm, 16, 16, 200, 30, 30);  
  37.          glPopMatrix();  
  38.    
  39.          glPushMatrix(); // LEFT UPPER ARM AND BODY CONNECTION  
  40.          glColor3f(1.0, 0.0, 0.0);  
  41.          glPushMatrix();  
  42.          glTranslatef(-80.0, 160, zMove);  
  43.          glutSolidSphere(16, 30, 30);  
  44.          glPopMatrix();  
  45.    
  46.          glPushMatrix(); //LEFT LOWER ARM  
  47.          glColor3f(1.0, 1.0, 0.0);  
  48.          glTranslatef(-221.5, 19.5, zMove);  
  49.          glRotatef(225, 0.0, 0.0, 1.0);  
  50.          glRotatef(90, 1.0, 0.0, 0.0);  
  51.          GLUquadricObj* leftLowerArm = gluNewQuadric();  
  52.          gluQuadricDrawStyle(leftLowerArm, GLU_FILL);  
  53.          gluCylinder(leftLowerArm, 16, 16, 200, 30, 30);  
  54.          glPopMatrix();  
  55.    
  56.          glPushMatrix(); // LEFT LOWER ARM AND LEFT UPPER ARM CONNECTION  
  57.          glColor3f(1.0, 0.0, 0.0);  
  58.          glPushMatrix();  
  59.          glTranslatef(-221.5, 19.5, zMove);  
  60.          glutSolidSphere(16, 30, 30);  
  61.          glPopMatrix();  
  62.    
  63.          glPushMatrix(); //RIGHT UPPER ARM  
  64.          glColor3f(0.180f, 0.545f, 0.341f);  
  65.          glTranslatef(80, 160, zMove);  
  66.          glRotatef(90, 0.0, 0.0, 1.0);  
  67.          glRotatef(90, 1.0, 0.0, 0.0);  
  68.          GLUquadricObj* rightUpperArm = gluNewQuadric();  
  69.          gluQuadricDrawStyle(rightUpperArm, GLU_FILL);  
  70.          gluCylinder(rightUpperArm, 16, 16, 200, 30, 30);  
  71.          glPopMatrix();  
  72.    
  73.          glPushMatrix(); // RIGHT UPPER ARM AND BODY CONNECTION  
  74.          glColor3f(1.0, 0.0, 0.0);  
  75.          glPushMatrix();  
  76.          glTranslatef(80, 160, zMove);  
  77.          glutSolidSphere(16, 30, 30);  
  78.          glPopMatrix();  
  79.    
  80.          glPushMatrix(); //RIGHT LOWER ARM  
  81.          glColor3f(1.0, 1.0, 0.0);  
  82.          glTranslatef(280, 160, zMove);  
  83.          glRotatef((GLfloat)rightHandAngle, 0.0, 0.0, 1.0);  
  84.          glRotatef(90, 1.0, 0.0, 0.0);  
  85.          GLUquadricObj* rightLowerArm = gluNewQuadric();  
  86.          gluQuadricDrawStyle(rightLowerArm, GLU_FILL);  
  87.          gluCylinder(rightLowerArm, 16, 16, 200, 30, 30);  
  88.          glPopMatrix();  
  89.    
  90.          glPushMatrix(); // RIGHT LOWER ARM AND RIGHT UPPER ARM CONNECTION  
  91.          glColor3f(1.0, 0.0, 0.0);  
  92.          glPushMatrix();  
  93.          glTranslatef(280, 160, zMove);  
  94.          glutSolidSphere(16, 30, 30);  
  95.          glPopMatrix();  
  96.    
  97.          glPushMatrix(); //LEFT LEG  
  98.          glColor3f(0.180f, 0.545f, 0.341f);  
  99.          glTranslatef(-35, -20, zMove);  
  100.          glRotatef(-15, 0.0, 0.0, 1.0);  
  101.          glRotatef((GLfloat)leftLegAngle, 1.0, 0.0, 0.0);  
  102.          GLUquadricObj* leftLeg = gluNewQuadric();  
  103.          gluQuadricDrawStyle(leftLeg, GLU_FILL);  
  104.          gluCylinder(leftLeg, 16, 16, 400, 30, 30);  
  105.          glPopMatrix();  
  106.    
  107.          glPushMatrix(); // LEFT LEG AND BODY CONNECTION  
  108.          glColor3f(1.0, 0.0, 0.0);  
  109.          glPushMatrix();  
  110.          glTranslatef(-35, -20, zMove);  
  111.          glutSolidSphere(16, 30, 30);  
  112.          glPopMatrix();  
  113.    
  114.          glPushMatrix(); //RIGHT LEG  
  115.          glColor3f(0.180f, 0.545f, 0.341f);  
  116.          glTranslatef(35, -20, zMove);  
  117.          glRotatef(15, 0.0, 0.0, 1.0);  
  118.          glRotatef((GLfloat)rightLegAngle, 1.0, 0.0, 0.0);  
  119.          GLUquadricObj* rightLeg = gluNewQuadric();  
  120.          gluQuadricDrawStyle(rightLeg, GLU_FILL);  
  121.          gluCylinder(rightLeg, 16, 16, 400, 30, 30);  
  122.          glPopMatrix();  
  123.    
  124.          glPushMatrix(); // RIGHT LEG AND BODY CONNECTION  
  125.          glColor3f(1.0, 0.0, 0.0);  
  126.          glPushMatrix();  
  127.          glTranslatef(35, -20, zMove);  
  128.          glutSolidSphere(16, 30, 30);  
  129.          glPopMatrix();  
  130.    
  131.          glPushMatrix(); //NECK  
  132.          glColor3f(0.0, 0.0, 0.545);  
  133.          glTranslatef(0.0, 251, zMove);  
  134.          glRotatef(90, 1.0, 0.0, 0.0);  
  135.          GLUquadricObj* neck = gluNewQuadric();  
  136.          gluQuadricDrawStyle(neck, GLU_FILL);  
  137.          gluCylinder(neck, 20, 20, 30, 30, 30);  
  138.          glPopMatrix();  
  139.    
  140.          glPushMatrix(); // HEAD  
  141.          glColor3f(1.0, 0.647, 0.0);  
  142.          glPushMatrix();  
  143.          glTranslatef(0.0, 350, zMove);  
  144.          glutSolidSphere(100, 30, 30);  
  145.          glPopMatrix();  
  146.            
  147.            
  148.          if (mouseLeftState == true){ // If left mouse clicked right hand of object will shake its lower arm  
  149.              if (rightHandAngle >= 225) { // If angle is greater than 225 incrementing degree will become decrement  
  150.                  rightHandMove = -rightHandMove;  
  151.              }  
  152.              else if (rightHandAngle <= 135){ // If angle is lower than 135 decrementing degree will become increment  
  153.                  rightHandMove = -rightHandMove;  
  154.              }  
  155.              rightHandAngle = (rightHandAngle + rightHandMove) % 360; // changing angle of right hand.  
  156.          }  
  157.          if (mouseRightState == true){ // If right mouse clicked the object will ve moved and legs' angles will be changed.  
  158.              if (leftLegAngle > 110){  
  159.                  leftLegMove = -leftLegMove;  
  160.              }  
  161.              else if (leftLegAngle < 70){  
  162.                  leftLegMove = -leftLegMove;  
  163.              }  
  164.              leftLegAngle = (leftLegAngle + leftLegMove) % 360; // Changing angle of left leg  
  165.    
  166.              if (rightLegAngle > 110) {  
  167.                  rightLegMove = -rightLegMove;  
  168.              }  
  169.              else if (rightLegAngle < 70){  
  170.                  rightLegMove = -rightLegMove;  
  171.              }  
  172.              rightLegAngle = (rightLegAngle + rightLegMove) % 360; // Changing angle of right leg  
  173.              zMove += 1.5f; // Moving object on the z-axis  
  174.          }  
  175.      glutSwapBuffers();  
  176.  }  
  177.  void keyboard(unsigned char key, int x, int y){  
  178.      if (key == 27) // exit when user hits <esc>  
  179.          exit(EXIT_SUCCESS);  
  180.  }  
  181.  void rotate(int key, int x, int y)  
  182.  {  
  183.      if (key == GLUT_KEY_LEFT){  
  184.          glRotatef(-1, 0.0, 1.0, 0.0);    // Rotates left by 1 degree  
  185.          glutPostRedisplay();  
  186.      }  
  187.      if (key == GLUT_KEY_RIGHT){  
  188.            
  189.          glRotatef(1, 0.0, 1.0, 0.0);    // Rotates right by 1 degree  
  190.          glutPostRedisplay();  
  191.      }  
  192.      if (key == GLUT_KEY_UP){  
  193.          glRotatef(1, 1.0, 0.0, 0.0);    // Rotates up by 1 degree  
  194.          glutPostRedisplay();  
  195.      }  
  196.      if (key == GLUT_KEY_DOWN){  
  197.          glRotatef(-1, 1.0, 0.0, 0.0);    // Rotates down by 1 degree  
  198.          glutPostRedisplay();  
  199.      }  
  200.  }  
  201.  void mouse(int button, int state, int x, int y){  
  202.      if (GLUT_LEFT_BUTTON == button && state == GLUT_DOWN && mouseLeftState == false){ // If left button is clicked, left state will be true for shaking lower arm  
  203.          mouseLeftState = true;  
  204.      }  
  205.      else if (GLUT_LEFT_BUTTON == button && state == GLUT_DOWN && mouseLeftState == true){ // If left button is clicked again, left state will be false and shaking will stop.  
  206.          mouseLeftState = false;  
  207.      }  
  208.      else if (GLUT_RIGHT_BUTTON == button && state == GLUT_DOWN && mouseRightState == false){ // If right button is clicked again, moving of object and rotation of legs will stop.  
  209.          mouseRightState = true;  
  210.      }  
  211.      else if (GLUT_RIGHT_BUTTON == button && state == GLUT_DOWN && mouseRightState == true){ // If right button is clicked, right state will be true and moving object and rotation of legs will start  
  212.          mouseRightState = false;  
  213.      }  
  214.  }  
  215.  void timer(int notUsed) // Timer is for animation. This function provides us to redisplay all objects by every 100 miliseconds  
  216.  {  
  217.      glutPostRedisplay();  
  218.      glutTimerFunc(100, timer, 0);  
  219.  }  
  220.  int main(int argc, char *argv[]){  
  221.      glutInit(&argc, argv);  
  222.      glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);  
  223.      glEnable(GL_DEPTH_TEST); // Hidden surface removal  
  224.      glutInitWindowSize(1200, 700);  
  225.      glutCreateWindow(argv[0]);  
  226.      glClearColor(0.0, 1.0, 1.0, 0.0);  
  227.      glMatrixMode(GL_PROJECTION);  
  228.      glLoadIdentity();  
  229.      glOrtho(-750.0, 750.0, -500.0, 500.0, -500.0, 500.0); // Changing the coordinate system.  
  230.      glutDisplayFunc(display);  
  231.      glutSpecialFunc(rotate);  
  232.      glutKeyboardFunc(keyboard);  
  233.      glutMouseFunc(mouse);  
  234.      timer(0);  
  235.      glutMainLoop();  
  236.      return EXIT_SUCCESS;  
  237.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement