Guest User

Untitled

a guest
Jun 28th, 2018
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  main.c
  3. //  GEOMPRA-CV2b
  4. //
  5. //  Created by Jakub Mejtský on 27.02.12.
  6. //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
  7. //
  8.  
  9.  
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <GLUT/GLUT.h>
  14. #include <math.h>
  15.  
  16. GLfloat X = 100.0f; // Translate screen to x direction (left or right)
  17. GLfloat Y = 100.0f; // Translate screen to y direction (up or down)
  18. GLfloat Z = 0.0f; // Translate screen to z direction (zoom in or out)
  19. GLfloat rotX = 0.0f; // Rotate screen on x axis
  20. GLfloat rotY = 0.0f; // Rotate screen on y axis
  21. GLfloat rotZ = 0.0f; // Rotate screen on z axis
  22.  
  23. void onResize(int, int);
  24. void onDisplay(void);
  25. void onKeyboard(unsigned char, int, int);
  26. void specialKey(int key, int x, int y);
  27.  
  28.  
  29. void onResize(int w, int h)                    
  30. {
  31.     glViewport(0, 0, w, h);                    
  32.     glMatrixMode(GL_PROJECTION);                
  33.     glLoadIdentity();                          
  34.     glOrtho(0, w, 0, h, -1, 1);                
  35. }
  36.  
  37. void onDisplay(void)
  38. {
  39.     glClear(GL_COLOR_BUFFER_BIT);
  40.     glClearColor(0.8, 0.8, 0.8, 0.0);
  41.     glClear(GL_DEPTH_BUFFER_BIT);
  42.    
  43.     GLfloat A[] = {0.0f, 0.0f, 0.0f};
  44.     GLfloat B[] = {100.0f, 0.0f, 0.0f};
  45.     GLfloat C[] = {100.0f, 100.0f, 0.0f};
  46.     GLfloat D[] = {0.0f, 100.0f, 0.0f};
  47.     GLfloat E[] = {0.0f, 0.0f, 100.0f};
  48.     GLfloat F[] = {100.0f, 0.0f, 100.0f};
  49.     GLfloat G[] = {100.0f, 100.0f, 100.0f};
  50.     GLfloat H[] = {0.0f, 100.0f, 100.0f};
  51.    
  52.     glPolygonMode(GL_FRONT, GL_FILL);
  53.     glPolygonMode(GL_BACK, GL_FILL);
  54.     glMatrixMode(GL_MODELVIEW);
  55.     glDisable(GL_CULL_FACE);
  56.     glEnable(GL_DEPTH_TEST);
  57.     glLoadIdentity();
  58.    
  59.     glTranslatef(X, Y, Z);
  60.     glRotatef(rotX,1.0,0.0,0.0); // Rotate on x
  61.     glRotatef(rotY,0.0,1.0,0.0); // Rotate on y
  62.     glRotatef(rotZ,0.0,0.0,1.0); // Rotate on z
  63.    
  64.    
  65.     glColor3f(1.0f, 0.0f, 0.0f);
  66.     glBegin(GL_POLYGON);
  67.     glVertex3fv(A);
  68.     glVertex3fv(B);
  69.     glVertex3fv(C);
  70.     glVertex3fv(D);
  71.     glEnd();
  72.    
  73.     glColor3f(0.0f, 1.0f, 0.0f);
  74.     glBegin(GL_POLYGON);
  75.     glVertex3fv(E);
  76.     glVertex3fv(F);
  77.     glVertex3fv(G);
  78.     glVertex3fv(H);
  79.     glEnd();
  80.    
  81.     glColor3f(0.0f, 0.0f, 1.0f);
  82.     glBegin(GL_POLYGON);
  83.     glVertex3fv(B);
  84.     glVertex3fv(F);
  85.     glVertex3fv(G);
  86.     glVertex3fv(C);
  87.     glEnd();
  88.    
  89.     glColor3f(0.0f, 1.0f, 1.0f);
  90.     glBegin(GL_POLYGON);
  91.     glVertex3fv(A);
  92.     glVertex3fv(E);
  93.     glVertex3fv(H);
  94.     glVertex3fv(D);
  95.     glEnd();
  96.    
  97.     glColor3f(1.0f, 0.0f, 1.0f);
  98.     glBegin(GL_POLYGON);
  99.     glVertex3fv(A);
  100.     glVertex3fv(B);
  101.     glVertex3fv(F);
  102.     glVertex3fv(E);
  103.     glEnd();
  104.    
  105.     glColor3f(1.0f, 1.0f, 0.0f);
  106.     glBegin(GL_POLYGON);
  107.     glVertex3fv(D);
  108.     glVertex3fv(C);
  109.     glVertex3fv(G);
  110.     glVertex3fv(H);
  111.     glEnd();
  112.    
  113.     glFlush();                              
  114. }
  115.  
  116. // This function is used for the navigation keys
  117. void onKeyboard (unsigned char key, int x, int y){
  118.     switch (key) {
  119.         case 27: exit(0);
  120.         case 'x': // Rotates screen on x axis
  121.             rotX -= 1.0f;
  122.             break;
  123.         case 'X': // Opposite way
  124.             rotX += 1.0f;
  125.             break;
  126.         case 'y': // Rotates screen on y axis
  127.             rotY -= 1.0f;
  128.             break;
  129.         case 'Y': // Opposite way
  130.             rotY += 1.0f;
  131.             break;
  132.         case 'z': // Rotates screen on z axis
  133.             rotZ -= 1.0f;
  134.             break;
  135.         case 'Z': // Opposite way
  136.             rotZ += 1.0f;
  137.             break;
  138.     }
  139.         glutPostRedisplay();
  140. }
  141.                        
  142. void specialKey(int key, int x, int y) {
  143.     switch(key) {
  144.         case GLUT_KEY_LEFT : // Rotate on x axis
  145.             X -= 2.0f;
  146.             break;
  147.         case GLUT_KEY_RIGHT : // Rotate on x axis (opposite)
  148.             X += 2.0f;
  149.             break;
  150.         case GLUT_KEY_UP : // Rotate on y axis
  151.             Y += 2.0f;
  152.             break;
  153.         case GLUT_KEY_DOWN : // Rotate on y axis (opposite)
  154.             Y -= 2.0f;
  155.             break;
  156.         case GLUT_KEY_F1: // Rotate on z axis
  157.             Z -= 2.0f;
  158.             break;
  159.         case GLUT_KEY_F2:// Rotate on z axis (opposite)
  160.             Z += 2.0f;
  161.             break;
  162.     }
  163.     glutPostRedisplay();
  164. }
  165.  
  166. int main (int argc, char * argv[])
  167. {
  168.     glutInit(&argc, argv);    
  169.     glutInitDisplayMode(GLUT_RGB);
  170.     glutCreateWindow("Úkol Geometrické praktikum - CV2b");            
  171.     glutReshapeWindow(350, 350);            
  172.     glutPositionWindow(50, 50);          
  173.     glutDisplayFunc(onDisplay);            
  174.     glutReshapeFunc(onResize);              
  175.     glutKeyboardFunc(onKeyboard);
  176.     glutSpecialFunc(specialKey);
  177.     glutMainLoop();                        
  178.     return 0;
  179. }
Add Comment
Please, Sign In to add comment