Advertisement
akevintg

OpenGL (Animation & Keyboard)

Apr 3rd, 2016
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.95 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  OpenGL-05
  4. //
  5. //  Created by Alexander Kevin on 4/3/16.
  6. //  Copyright © 2016 Alexander Kevin. All right reserved.
  7. //
  8.  
  9. //CASE LINK : https://www.dropbox.com/s/2y8siyes8tfqmfr/T00740020220154042Tugas%20dan%20Latihan%20GSLC-2.doc?dl=0
  10. //.exe LINK : https://www.dropbox.com/s/giwgt1llyr406p8/OpenGL-05?dl=0
  11.  
  12. #ifdef WIN32
  13. #include <windows.h>
  14. #endif
  15.  
  16. #include <stdlib.h>
  17. #include <iostream>
  18. #include <fstream>
  19.  
  20. #ifdef __APPLE__
  21. #include <GLUT/glut.h>
  22. #include <OpenGL/gl.h>
  23. #include <OpenGL/glu.h>
  24. #else
  25. #include <GL/glut.h>
  26. #include <GL/glu.h>
  27. #include <GL/gl.h>
  28. #endif
  29.  
  30. float r=0,g=1,b=1;
  31. int flag=0;
  32. bool isdownKeyPressed,isupKeyPressed;
  33. static GLfloat spin = 0.0;
  34. int sumbu_x=0,sumbu_y=0,sumbu_z=1;
  35.  
  36. void init()
  37. {
  38.     glMatrixMode(GL_PROJECTION);
  39.     glLoadIdentity();
  40.     glOrtho(-7.0,7.0,-7.0,7.0,-7.0,7.0);
  41.     glMatrixMode(GL_MODELVIEW);
  42.     glClearColor(0.0,0.0,0.0,1.0);
  43.     glColor3f(0.0,0.0,0.0);
  44.     glShadeModel(GL_FLAT);
  45.     glEnable(GL_DEPTH_TEST);
  46. }
  47.  
  48. void display(void)
  49. {
  50.     glClear(GL_COLOR_BUFFER_BIT);
  51.     //bintang itu panah keatas sama panah ke kiri/kanan
  52.     //panah keatas
  53.     glFlush();
  54. }
  55.  
  56. void display_soal_satu(void)
  57. {
  58.     glClear(GL_COLOR_BUFFER_BIT);
  59.     //bintang itu panah keatas sama panah ke kiri/kanan
  60.     //panah keatas
  61.     glColor3f(r,g,b);
  62.     glBegin(GL_POLYGON);
  63.     glVertex3f(0, -2.7, 0);
  64.     glVertex3f(-3.1, -5.0, 0);
  65.     glVertex3f(0, 5.0, 0);
  66.     glVertex3f(3.1, -5.0, 0);
  67.     glEnd();
  68.    
  69.     //panah kekiri
  70.     glBegin(GL_POLYGON);
  71.     glVertex3f(1.9, -1.3, 0);
  72.     glVertex3f(3.1, -5, 0);
  73.     glVertex3f(-5, 1.2, 0);
  74.     glVertex3f(5, 1.2, 0);
  75.     glEnd();
  76.    
  77.     glFlush();
  78. }
  79.  
  80. void spinDisplay(void)
  81. {
  82.     spin = spin + 1;
  83.     if (spin > 360.0)
  84.         spin = spin - 360.0;
  85.     glutPostRedisplay();
  86. }
  87.  
  88. void display_soal_dua(void)
  89. {
  90.     glClear(GL_COLOR_BUFFER_BIT);
  91.     glPushMatrix();
  92.     glRotatef(spin, sumbu_x,sumbu_y,sumbu_z);
  93.     glColor3f(1.0, 0, 1.0);
  94.     glRectf(-5, -5, 5, 5);
  95.     glPopMatrix();
  96.    
  97.     glutSwapBuffers();
  98. }
  99.  
  100. void handleSpecialKeypress(int key, int x, int y) {
  101.     switch (key) {
  102.         case GLUT_KEY_DOWN:
  103.             isupKeyPressed = true;
  104.             if (!isdownKeyPressed) {
  105.                 srand(time(NULL));
  106.                 r=(float)rand()/(float)(RAND_MAX/1.0);
  107.                 g=(float)rand()/(float)(RAND_MAX/0.8);
  108.                 b=(float)rand()/(float)(RAND_MAX/0.6);
  109.             }
  110.             glutPostRedisplay();
  111.             break;
  112.         case GLUT_KEY_UP:
  113.             isdownKeyPressed = true;
  114.             if (!isupKeyPressed) {
  115.                 srand(time(NULL));
  116.                 r=(float)rand()/(float)(RAND_MAX/1);
  117.                 g=(float)rand()/(float)(RAND_MAX/0.8);
  118.                 b=(float)rand()/(float)(RAND_MAX/0.6);
  119.             }
  120.             glutPostRedisplay();
  121.             break;
  122.         case 's':
  123.         case 'S':
  124.             if(flag==0){
  125.                 glutIdleFunc(spinDisplay);
  126.                 flag=1;
  127.             }
  128.             else{
  129.                 glutIdleFunc(NULL);
  130.                 flag=0;
  131.             }
  132.             break;
  133.         case 'z':
  134.         case 'Z':
  135.             sumbu_z=1;
  136.             sumbu_x=0;
  137.             sumbu_y=0;
  138.             break;
  139.         case 'x':
  140.         case 'X':
  141.             sumbu_z=0;
  142.             sumbu_x=1;
  143.             sumbu_y=0;
  144.             break;
  145.         case 'y':
  146.         case 'Y':
  147.             sumbu_z=0;
  148.             sumbu_x=0;
  149.             sumbu_y=1;
  150.             break;
  151.         case 27:
  152.         case 'q':
  153.         case 'Q':
  154.             exit(0);
  155.             break;
  156.         case '1':
  157.             glLoadIdentity();
  158.             glutDisplayFunc(display_soal_satu);
  159.             glutPostRedisplay();
  160.             break;
  161.         case '2':
  162.             glLoadIdentity();
  163.             glutDisplayFunc(display_soal_dua);
  164.             glutPostRedisplay();
  165.             break;
  166.     }
  167. }
  168.  
  169. void handleSpecialKeyReleased(int key, int x, int y) {
  170.     switch (key) {
  171.         case GLUT_KEY_UP:
  172.             isupKeyPressed = false;
  173.             break;
  174.         case GLUT_KEY_DOWN:
  175.             isdownKeyPressed = false;
  176.             break;
  177.     }
  178. }
  179.  
  180. int main(int argc, char** argv)
  181. {
  182.     printf("Welcome,\n");
  183.     printf("Press Keyboard\n");
  184.     printf("(1) for Star (Case 1)\n");
  185.     printf("(2) for Square Animation(Case 2)\n");
  186.     printf("for case 1, press up and down arrow to change color\n");
  187.     printf("and for case 2, press s/S to start rotation\n");
  188.     printf("then x/X to rotate on X\n");
  189.     printf("then y/Y to rotate on Y\n");
  190.     printf("then z/Z to rotate on Z\n");
  191.     printf("<press esc/q to exit>\n");
  192.     glutInit(&argc, argv);
  193.     glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
  194.     glutInitWindowSize (400, 400);
  195.     glutCreateWindow (argv[0]);
  196.     init();
  197.     glutSpecialFunc(handleSpecialKeypress);
  198.     glutSpecialUpFunc(handleSpecialKeyReleased);
  199.     glutDisplayFunc (display);
  200.     glutMainLoop();
  201.     return 0;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement