Advertisement
akevintg

OpenGL (glTranslated & glRotated)

Mar 3rd, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.50 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  OpenGL02
  4. //
  5. //  Created by Alexander Kevin on 3/3/16.
  6. //  Copyright © 2016 Alexander Kevin. All rights reserved.
  7. //
  8.  
  9. #ifdef WIN32
  10. #include <windows.h>
  11. #endif
  12.  
  13. #include <stdlib.h>
  14. #include <iostream>
  15. #include <fstream>
  16.  
  17. #ifdef __APPLE__
  18. #include <GLUT/glut.h>
  19. #include <OpenGL/gl.h>
  20. #include <OpenGL/glu.h>
  21. #else
  22. #include <GL/glut.h>
  23. #include <GL/glu.h>
  24. #include <GL/gl.h>
  25. #endif
  26.  
  27. void display()
  28. {
  29.     glClear(GL_COLOR_BUFFER_BIT);
  30.     glColor3f(0.0,0.0,0.5);
  31.     glFlush();
  32. }
  33.  
  34. void displaysoalsatu()
  35. {
  36.     glClear(GL_COLOR_BUFFER_BIT);
  37.     glColor3f(0.0,0.0,0.5);
  38.     int x=-25,y=-25,xp=-20,yl=-20;//xp=panjang, y;=lebar);
  39.     //bikin checkboard
  40.     while(y!=70){
  41.         glRecti(x,y, xp, yl);
  42.         x+=10;
  43.         xp+=10;
  44.         //printf("%d %d %d %d\n",x,y,xp,yl);
  45.         if(x>=70&&y!=70){
  46.             //printf("baris baru\n\n");
  47.             y+=5;
  48.             if(x==75){
  49.                 x=-20;
  50.                 xp=-15;
  51.             }
  52.             else{
  53.                 x=-25;
  54.                 xp=-20;
  55.             }
  56.             yl+=5;
  57.         }
  58.     }
  59.     glFlush();
  60. }
  61.  
  62. void displaysoaldua()
  63. {
  64.     glClear(GL_COLOR_BUFFER_BIT);
  65.     glColor3f(0.5,0.5,0.5);
  66.     int rot=0;
  67.     glRecti(22.5,37.5,27.5,42.5);
  68.     do{
  69.         glTranslated(25, 25, 0);
  70.         glRotated(30, 0, 0, 1);
  71.         glTranslated(-25, -25, 0);
  72.         if(rot%2==1)
  73.             glRecti(22.5,37.5,27.5,42.5);
  74.         else
  75.             glRecti(23.5,38.5,26,41);
  76.         rot++;
  77.     }while(rot!=16);
  78.     rot=0;
  79.     do{
  80.         glColor3f(0.5,0.8,0.5);
  81.         glTranslated(25, 25, 0);
  82.         glRotated(0.1, 0, 0, 1);
  83.         glTranslated(-25, -25, 0);
  84.         glRecti(24.9,39.9,25.1,40.1);
  85.         rot++;
  86.     }while(rot!=3600);
  87.     glFlush();
  88. }
  89.  
  90. void displaysoaltiga()
  91. {
  92.     glClear(GL_COLOR_BUFFER_BIT);
  93.     glColor3f(0.5,0.8,0.5);
  94.     int rot=0;
  95.     do{
  96.         glTranslated(25, 25, 0);
  97.         glRotated(30, 0, 0, 1);
  98.         glTranslated(-25, -25, 0);
  99.         glRecti(22.5,37.5,27.5,42.5);
  100.         rot++;
  101.     }while(rot!=16);
  102.     rot=0;
  103.     do{
  104.         glTranslated(25, 25, 0);
  105.         glRotated(0.1, 0, 0, 1);
  106.         glTranslated(-25, -25, 0);
  107.         glRecti(24.9,39.9,25.1,40.1);
  108.         glColor3f(0.5,0.5,0.5);
  109.         rot++;
  110.     }while(rot!=3600);
  111.     glFlush();
  112. }
  113.  
  114.  
  115.  
  116. void kunci(unsigned char key, int x, int y)
  117. {
  118.     static int rot=0;
  119.     switch (key)
  120.     {
  121.             /* aplikasi berhenti ketika tombol q ditekan */
  122.         case 27 :
  123.         case 'q':
  124.             exit(0);
  125.             break;
  126.         case 'z':
  127.             glTranslated(25, 25, 0);
  128.             glRotated(1, 0, 0, 1);
  129.             glTranslated(-25, -25, 0);
  130.             break;
  131.         case 'c':
  132.             glTranslated(25, 25, 0);
  133.             glRotated(-1, 0, 0, 1);
  134.             glTranslated(-25, -25, 0);
  135.             break;
  136.         case 'w':
  137.             glTranslated(0, 0.5, 0);
  138.             break;
  139.         case 'a':
  140.             glTranslated(-0.5, 0, 0);
  141.             break;
  142.         case 's':
  143.             glTranslated(0, -0.5, 0);
  144.             break;
  145.         case 'd':
  146.             glTranslated(0.5, 0, 0);
  147.             break;
  148.         case 'p':
  149.             glutDisplayFunc(displaysoaltiga);
  150.             break;
  151.         case 'i':
  152.             glLoadIdentity();
  153.             glTranslated(25, 25, 0);
  154.             glRotated(45, 0, 0, 1);
  155.             glTranslated(-25, -25, 0);
  156.             glutDisplayFunc(displaysoalsatu);
  157.             break;
  158.         case 'o':
  159.             glutDisplayFunc(displaysoaldua);
  160.             break;
  161.     }
  162.     glutPostRedisplay();
  163. }
  164.  
  165. void myinit()
  166. {
  167.     glMatrixMode(GL_PROJECTION);
  168.     glLoadIdentity();
  169.     gluOrtho2D(0.0,50.0,0.0,50.0);
  170.     glMatrixMode(GL_MODELVIEW);
  171.     glClearColor(1.0,1.0,1.0,1.0);
  172.     glColor3f(0.0,0.0,0.0);
  173. }
  174.  
  175. int main(int argc, char* argv[])
  176. {
  177.     printf("Welcome,\n");
  178.     printf("Press Keyboard\n");
  179.     printf("(p) for square and circle(Case 2)\n");
  180.     printf("(o) for improved square and circle(Case 3)\n");
  181.     printf("(i) for checkboard(Case 1)\n");
  182.     printf("for control use standart wasd (up,left,down,right)\n");
  183.     printf("and z for left rotate and c for right rotate\n");
  184.     printf("<press q to exit>\n");
  185.     glutInit(&argc,argv);
  186.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  187.     glutInitWindowSize(400,400);
  188.     glutInitWindowPosition(100,100);
  189.     glutCreateWindow("Transform");
  190.     glutDisplayFunc(display);
  191.     myinit();
  192.     glutKeyboardFunc(kunci);
  193.     glutMainLoop();
  194.     return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement