Advertisement
akevintg

OpenGL (GL_POLYGON, glutWire)

Mar 16th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.94 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  OpenGL-03
  4. //
  5. //  Created by Alexander Kevin on 3/16/16.
  6. //  Copyright © 2016 Alexander Kevin. All rights reserved.
  7. //
  8.  
  9. // CASE : https://www.dropbox.com/s/doa7j9llrzleqet/T00740020220154042Tugas%20dan%20Latihan%203.doc?dl=0
  10. // .EXE : https://www.dropbox.com/s/k26cfr2iyet6qck/OpenGL-03?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.  
  31. void display()
  32. {
  33.     glClear(GL_COLOR_BUFFER_BIT);
  34.     glFlush();
  35. }
  36.  
  37. void display_soal_satu()
  38. {
  39.     glClear(GL_COLOR_BUFFER_BIT);
  40.     glPushMatrix();
  41.     glColor3f(1.0, 1.0, 1.0);
  42.     glPushMatrix();
  43.     glRotated(45, 0, 0, 1);
  44.     glScalef(1, 4, 1);
  45.     glutWireCube(2);
  46.     glPopMatrix();
  47.     glRotated(-45, 0, 0, 1);
  48.     glScalef(1, 4, 1);
  49.     glutWireCube(2);
  50.     glPopMatrix();
  51.     glFlush();
  52. }
  53.  
  54. void display_soal_dua()
  55. {
  56.     glClear(GL_COLOR_BUFFER_BIT);
  57.    
  58. //   belakang
  59.     glColor3f(1,0.6,0.8);
  60.     glBegin(GL_POLYGON);
  61.    
  62.     glVertex3f(-1.25, 0, 0);
  63.     //kiri atas
  64.     glVertex3f(-2.8125, 1.5625, 0);
  65.     glVertex3f(-1.5625, 2.8125, 0);
  66.    
  67.     glVertex3f(0, 1.25, 0);
  68.     //kanan atas
  69.     glVertex3f(1.5625, 2.8125, 0);
  70.     glVertex3f(2.8125, 1.5625, 0);
  71.    
  72.     glVertex3f(1.25, 0, 0);
  73.     //kanan bawah
  74.     glVertex3f(2.8125, -1.5625, 0);
  75.     glVertex3f(1.5625, -2.8125, 0);
  76.    
  77.     glVertex3f(0, -1.25, 0);
  78.     //kanan bawah
  79.     glVertex3f(-1.5625, -2.8125, 0);
  80.     glVertex3f(-2.8125, -1.5625, 0);
  81.     glEnd();
  82.    
  83.     //sisi sisi
  84.     //kiri samping kiri (atas)
  85.     glColor3f(0.9,0.5,1.0);
  86.     glBegin(GL_POLYGON);
  87.     glVertex3f(-2.8125, 1.5625, 1.0);
  88.     glVertex3f(-1.25, 0, 1.0);
  89.     glVertex3f(-1.25, 0, 0);
  90.     glVertex3f(-2.8125, 1.5625, 0);
  91.     glEnd();
  92.     //kiri atas
  93.     glColor3f(0.9,0.0,1.0);
  94.     glBegin(GL_POLYGON);
  95.     glVertex3f(-2.8125, 1.5625, 1.0);
  96.     glVertex3f(-1.5625, 2.8125, 1.0);
  97.     glVertex3f(-1.5625, 2.8125, 0);
  98.     glVertex3f(-2.8125, 1.5625, 0);
  99.     glEnd();
  100.     //kiri samping kanan
  101.     glColor3f(0.9,0.3,1.0);
  102.     glBegin(GL_POLYGON);
  103.     glVertex3f(-1.5625, 2.8125, 1.0);
  104.     glVertex3f(0, 1.25, 1.0);
  105.     glVertex3f(0, 1.25, 0);
  106.     glVertex3f(-1.5625, 2.8125, 0);
  107.     glEnd();
  108.    
  109.     //kanan samping kiri(atas)
  110.     glColor3f(0.9,0.5,1.0);
  111.     glBegin(GL_POLYGON);
  112.     glVertex3f(0, 1.25, 1.0);
  113.     glVertex3f(0, 1.25, 0);
  114.     glVertex3f(1.5625, 2.8125, 0);
  115.     glVertex3f(1.5625, 2.8125, 1.0);
  116.     glEnd();
  117.     //kanan atas
  118.     glColor3f(0.9,0.0,1.0);
  119.     glBegin(GL_POLYGON);
  120.     glVertex3f(2.8125, 1.5625, 1.0);
  121.     glVertex3f(1.5625, 2.8125, 1.0);
  122.     glVertex3f(1.5625, 2.8125, 0);
  123.     glVertex3f(2.8125, 1.5625, 0);
  124.     glEnd();
  125.     //kanan samping kanan
  126.     glColor3f(0.9,0.3,1.0);
  127.     glBegin(GL_POLYGON);
  128.     glVertex3f(2.8125, 1.5625, 1.0);
  129.     glVertex3f(2.8125, 1.5625, 0);
  130.     glVertex3f(1.25, 0, 0);
  131.     glVertex3f(1.25, 0, 1.0);
  132.     glEnd();
  133.    
  134.     //kanan samping kiri(bawah)
  135.     glColor3f(0.9,0.3,1.0);
  136.     glBegin(GL_POLYGON);
  137.     glVertex3f(0, -1.25, 1.0);
  138.     glVertex3f(0, -1.25, 0);
  139.     glVertex3f(1.5625, -2.8125, 0);
  140.     glVertex3f(1.5625, -2.8125, 1.0);
  141.     glEnd();
  142.     //kanan atas
  143.     glColor3f(0.9,0.0,1.0);
  144.     glBegin(GL_POLYGON);
  145.     glVertex3f(2.8125, -1.5625, 1.0);
  146.     glVertex3f(1.5625, -2.8125, 1.0);
  147.     glVertex3f(1.5625, -2.8125, 0);
  148.     glVertex3f(2.8125, -1.5625, 0);
  149.     glEnd();
  150.     //kanan samping kanan
  151.     glColor3f(0.9,0.5,1.0);
  152.     glBegin(GL_POLYGON);
  153.     glVertex3f(2.8125, -1.5625, 1.0);
  154.     glVertex3f(2.8125, -1.5625, 0);
  155.     glVertex3f(1.25, 0, 0);
  156.     glVertex3f(1.25, 0, 1.0);
  157.     glEnd();
  158.    
  159.     //kiri samping kiri (bawah)
  160.     glColor3f(0.9,0.3,1.0);
  161.     glBegin(GL_POLYGON);
  162.     glVertex3f(-2.8125, -1.5625, 1.0);
  163.     glVertex3f(-1.25, 0, 1.0);
  164.     glVertex3f(-1.25, 0, 0);
  165.     glVertex3f(-2.8125, -1.5625, 0);
  166.     glEnd();
  167.     //kiri atas
  168.     glColor3f(0.9,0.0,1.0);
  169.     glBegin(GL_POLYGON);
  170.     glVertex3f(-2.8125, -1.5625, 1.0);
  171.     glVertex3f(-1.5625, -2.8125, 1.0);
  172.     glVertex3f(-1.5625, -2.8125, 0);
  173.     glVertex3f(-2.8125, -1.5625, 0);
  174.     glEnd();
  175.     //kiri samping kanan
  176.     glColor3f(0.9,0.5,1.0);
  177.     glBegin(GL_POLYGON);
  178.     glVertex3f(-1.5625, -2.8125, 1.0);
  179.     glVertex3f(0, -1.25, 1.0);
  180.     glVertex3f(0, -1.25, 0);
  181.     glVertex3f(-1.5625, -2.8125, 0);
  182.     glEnd();
  183.    
  184.     //depan
  185.     glColor3f(1,0.6,1.0);
  186.     glBegin(GL_POLYGON);
  187.    
  188.     glVertex3f(-1.25, 0, 1.0);
  189.     //kiri atas
  190.     glVertex3f(-2.8125, 1.5625, 1.0);
  191.     glVertex3f(-1.5625, 2.8125, 1.0);
  192.    
  193.     glVertex3f(0, 1.25, 1.0);
  194.     //kanan atas
  195.     glVertex3f(1.5625, 2.8125, 1.0);
  196.     glVertex3f(2.8125, 1.5625, 1.0);
  197.    
  198.     glVertex3f(1.25, 0, 1.0);
  199.     //kanan bawah
  200.     glVertex3f(2.8125, -1.5625, 1.0);
  201.     glVertex3f(1.5625, -2.8125, 1.0);
  202.    
  203.     glVertex3f(0, -1.25, 1.0);
  204.     //kanan bawah
  205.     glVertex3f(-1.5625, -2.8125, 1.0);
  206.     glVertex3f(-2.8125, -1.5625, 1.0);
  207.     glEnd();
  208.     glFlush();
  209. }
  210.  
  211. void myinit()
  212. {
  213.     glMatrixMode(GL_PROJECTION);
  214.     glLoadIdentity();
  215.     glOrtho(-5.0,5.0,-5.0,5.0,-5.0,5.0);
  216.     glMatrixMode(GL_MODELVIEW);
  217.     glClearColor(0.0,0.0,0.0,1.0);
  218.     glColor3f(0.0,0.0,0.0);
  219.     glShadeModel(GL_FLAT);
  220.     glEnable(GL_DEPTH_TEST);
  221. }
  222. void kunci(unsigned char key, int x, int y)
  223. {
  224.     static int rot=0;
  225.     switch (key)
  226.     {
  227.             /* aplikasi berhenti ketika tombol q ditekan */
  228.         case 27 :
  229.         case 'z':
  230.             exit(0);
  231.             break;
  232.         case 'q':
  233.             glRotated(5, 0,0,1);
  234.             break;
  235.         case 'e':
  236.             glRotated(-5, 0,0,1);
  237.             break;
  238.         case 's':
  239.             glRotated(5, 1,0,0);
  240.             break;
  241.         case 'a':
  242.             glRotated(-5, 0,1,0);
  243.             break;
  244.         case 'w':
  245.             glRotated(-5, 1,0,0);
  246.             break;
  247.         case 'd':
  248.             glRotated(5, 0,1,0);
  249.             break;
  250.         case '1':
  251.             glLoadIdentity();
  252.             glutDisplayFunc(display_soal_satu);
  253.             break;
  254.         case '2':
  255.             glLoadIdentity();
  256.             glutDisplayFunc(display_soal_dua);
  257.             break;
  258.     }
  259.     glutPostRedisplay();
  260. }
  261.  
  262. int main(int argc, char* argv[])
  263. {
  264.     printf("Welcome,\n");
  265.     printf("Press Keyboard\n");
  266.     printf("(1) for WireCube X(Case 1)\n");
  267.     printf("(2) for Polygon X(Case 2)\n");
  268.     printf("for x-y-rotation use standart WASD (up,left,down,right)\n");
  269.     printf("and q for left rotate and e for right z-rotation\n");
  270.     printf("<press z to exit>\n");
  271.     glutInit(&argc,argv);
  272.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  273.     glutInitWindowSize(400,400);
  274.     glutInitWindowPosition(100,100);
  275.     glutCreateWindow("Kotak");
  276.     glutDisplayFunc(display);
  277.     myinit();
  278.     glutKeyboardFunc(kunci);
  279.     glutMainLoop();
  280.     return 0;
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement