Advertisement
Sathvikks8

spin

Jul 15th, 2022
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<GL/glut.h>
  3. float vertices[8][3] = {
  4. {-1.0, -1.0, -1.0},
  5. {-1.0, 1.0, -1.0},
  6. {1.0, 1.0, -1.0},
  7. {1.0, -1.0, -1.0},
  8. {-1.0, -1.0, 1.0},
  9. {-1.0, 1.0, 1.0},
  10. {1.0, 1.0, 1.0},
  11. {1.0, -1.0, 1.0}
  12. };
  13. float colors[8][3] = {
  14. {0.0, 0.0, 0.0},
  15. {0.0, 0.0, 1.0},
  16. {0.0, 1.0, 0.0},
  17. {0.0, 1.0, 1.0},
  18. {1.0, 0.0, 0.0},
  19. {1.0, 0.0, 1.0},
  20. {1.0, 1.0, 0.0},
  21. {0.5, 0.5, 0.5}
  22. };
  23. float theta[3] = {0.0, 0.0, 0.0};int axis=0;
  24. void idleFunc() {
  25.     theta[axis] += 1;
  26.     if (theta[axis] > 360.0)
  27.         theta[axis] = 0;
  28.     glutPostRedisplay();
  29. }
  30. void reshape(int w, int h) {
  31.     glViewport(0, 0, w, h);
  32.     glMatrixMode(GL_PROJECTION);
  33.     glLoadIdentity();
  34.     if (w <= h) {
  35.         glOrtho(-5.0, 5.0, -5.0*(float)h/(float)w, 5.0-(float)h/(float)w, -10.0, 10.0);
  36.     } else {
  37.         glOrtho(-5.0*(float)w/(float)h, 5.0*(float)w/(float)h, -5.0, 5.0, -10.0, 10.0);
  38.     }
  39.     glMatrixMode(GL_MODELVIEW);
  40.     glutPostRedisplay();
  41. }
  42. void drawCubeFace(int a, int b, int c, int d) {
  43.     glBegin(GL_POLYGON);
  44.     glColor3fv(colors[a]);
  45.     glVertex3fv(vertices[a]);
  46.     glColor3fv(colors[b]);
  47.     glVertex3fv(vertices[b]);
  48.     glColor3fv(colors[c]);
  49.     glVertex3fv(vertices[c]);
  50.     glColor3fv(colors[d]);
  51.     glVertex3fv(vertices[d]);
  52.     glEnd();
  53. }
  54. void colorCube() {
  55.     drawCubeFace(1, 0, 2, 3);
  56.     drawCubeFace(5, 4, 7, 6);
  57.     drawCubeFace(1, 2, 6, 5);
  58.     drawCubeFace(0, 4, 7, 3);
  59.     drawCubeFace(2, 3, 7, 6);
  60.     drawCubeFace(5, 4, 0, 1);
  61. }
  62. void display() {
  63.     glClearColor(1.0, 1.0, 1.0, 1.0);
  64.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  65.     glLoadIdentity();
  66.     glRotatef(theta[0], 1.0, 0.0, 0.0);
  67.     glRotatef(theta[1], 0.0, 1.0, 0.0);
  68.     glRotatef(theta[2], 0.0, 0.0, 1.0);
  69.     colorCube();
  70.     glFlush();
  71.     glutSwapBuffers(); 
  72. }
  73. void mouse(int btn, int state, int x, int y) {
  74.     if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  75.         axis = 0;
  76.     else if (btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
  77.         axis = 1;
  78.     else if (btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
  79.         axis = 2;
  80. }
  81. int main(int argc, char* argv[]) {
  82.     glutInit(&argc, argv);
  83.     glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
  84.     glutInitWindowPosition(0, 0);
  85.     glutInitWindowSize(500, 500);
  86.     glutCreateWindow("Spin cube");
  87.     glutReshapeFunc(reshape);
  88.     glutDisplayFunc(display);
  89.     glutIdleFunc(idleFunc);
  90.     glutMouseFunc(mouse);
  91.     glEnable(GL_DEPTH_TEST);
  92.     glutMainLoop();
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement