Advertisement
Guest User

Untitled

a guest
Mar 27th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define GL_SILENCE_DEPRECATION  //эпл не любит опенгл :с
  2. #include <GLFW/glfw3.h>
  3. #include <stdlib.h>  //needed for exit function
  4. #include <iostream>
  5. #include <stdio.h>
  6. #include <cmath>
  7.  
  8. void keyboard_callback(GLFWwindow *window, int key, int scancode, int action, int mods);
  9. void drawCube();
  10. void mouseButtonCallback( GLFWwindow *window, int button, int action, int mods);
  11. void dimetria();
  12.  
  13. int rotate_y = 0;
  14. int rotate_x = 0;
  15.  
  16. float trans_x = 0;
  17. float trans_y = 0;
  18. float trans_z = 0;
  19.  
  20. float sc_x = 0.5;
  21. float sc_y = 0.5;
  22. float sc_z = 0.5;
  23.  
  24. float fi = (asin(0.625/sqrt(2.0-0.625*0.625))); //62,82
  25. float teta = (asin(0.625/sqrt(2.0))); //41,6
  26.  
  27. GLfloat m[16] = { cos(fi), sin(fi)*sin(teta), sin(fi)*cos(teta), 0,
  28.                   0, cos(teta), -sin(teta), 0,
  29.                   sin(fi), -cos(fi)*sin(teta), -cos(fi)*cos(teta), 0,
  30.                   0, 0, 0, 1
  31. }; //как получилась матрица расписать два поворота по х, у
  32.  
  33. int main(int argc, char const *argv[]) {
  34.    
  35.     if(!glfwInit()) {
  36.         exit(EXIT_FAILURE);
  37.     }
  38.    
  39.     //glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
  40.    
  41.     GLFWwindow *window = glfwCreateWindow(620, 620, "Rorate Cube", NULL, NULL);
  42.    
  43.     if (!window) {
  44.         glfwTerminate();
  45.         exit(EXIT_FAILURE);
  46.     }
  47.     glfwSetMouseButtonCallback(window, mouseButtonCallback);
  48.     glfwMakeContextCurrent(window);
  49.     glfwSwapInterval(1);
  50.    
  51.     glEnable(GL_DEPTH_TEST);
  52.    
  53.     while (!glfwWindowShouldClose(window))
  54.     {
  55.         float ratio;
  56.         int width, height;
  57.         glfwGetFramebufferSize(window, &width, &height);
  58.         ratio = width / (float) height;
  59.         glViewport(0, 0, width, height);
  60.        
  61.         glfwSetKeyCallback(window, keyboard_callback);
  62.        
  63.         glClearColor(0, 0, 0, 0);
  64.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  65.        
  66.         glLoadIdentity();
  67.         glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
  68.         //вращение
  69.        
  70.         glTranslatef(0.4, 0.4, 0.4);
  71.         glTranslatef(trans_x, trans_y, trans_z);
  72.        
  73.         glScalef(sc_x, sc_y, sc_z);
  74.        
  75.         glRotatef(rotate_x, 1.0, 0.0, 0.0);
  76.         glRotatef(rotate_y, 0.0, 1.0, 0.0);
  77.        
  78.         dimetria(); //проекция через повороты/ротейты
  79.         //glMultMatrixf(m);
  80.        
  81.         drawCube();
  82.        
  83.        
  84.         glLoadIdentity();
  85.         glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
  86.        
  87.         glTranslatef(-0.6, -0.4, -0.6);
  88.        
  89.         glScalef(0.2, 0.2, 0.2);
  90.         //
  91.         //glRotatef(15, 0.0, -1.0, 0.0);
  92.         //glRotatef(15, -1.0, 0.0, 0.0);
  93.         //
  94.         //dimetria();
  95.         glMultMatrixf(m); //проекция через формулу с лекции
  96.        
  97.         drawCube();
  98.        
  99.  
  100.        
  101.        
  102.        
  103.         glfwSwapBuffers(window);
  104.         glfwPollEvents();
  105.     }
  106.    
  107.     glfwTerminate();
  108.     return 0;
  109. }
  110.  
  111.  
  112. void dimetria() {
  113.    
  114.     //glRotatef(180.0, 0.0, 1.0, 0.0);
  115.     glRotatef(fi*(180/3.14) , 1.0, 0.0, 0.0);
  116.     glRotatef(teta*(180/3.14) , 0.0, 1.0, 0.0);
  117.     //glRotatef(180.0 , 0.0, 1.0, 0.0);
  118. }
  119.  
  120. void drawCube() {
  121.     //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  122.     glBegin(GL_POLYGON); // red side - FRONT
  123.         glColor3f(1.0,  0.0,  0.0);
  124.     glVertex3f( 0.4, -0.4, -0.4);
  125.     glVertex3f( 0.4,  0.4, -0.4);
  126.         glColor3f(0.6,  0.6,  0.3);
  127.     glVertex3f(-0.4,  0.4, -0.4);
  128.     glVertex3f(-0.4, -0.4, -0.4);
  129.     glEnd();
  130.    
  131.     glBegin(GL_POLYGON); // green side - BACK
  132.         glColor3f(0.01,  1.0, 0.8);
  133.     glVertex3f( 0.4, -0.4, 0.4);
  134.     glVertex3f( 0.4,  0.4, 0.4);
  135.         glColor3f(0.09,  0.56, 0.21);
  136.     glVertex3f(-0.4,  0.4, 0.4);
  137.     glVertex3f(-0.4, -0.4, 0.4);
  138.     glEnd();
  139.    
  140.     glBegin(GL_POLYGON); // blue side - RIGHT
  141.         glColor3f(0.04,  0.14,  0.6);
  142.     glVertex3f(0.4, -0.4, -0.4);
  143.     glVertex3f(0.4,  0.4, -0.4);
  144.         glColor3f(1.0,  0.0,  1.0);
  145.     glVertex3f(0.4,  0.4,  0.4);
  146.     glVertex3f(0.4, -0.4,  0.4);
  147.     glEnd();
  148.    
  149.     glBegin(GL_POLYGON); // orange side - LEFT
  150.         glColor3f(0.8,  0.9,  0.04);
  151.     glVertex3f(-0.4, -0.4,  0.4);
  152.     glVertex3f(-0.4,  0.4,  0.4);
  153.         glColor3f(0.9,  0.4,  0.02);
  154.     glVertex3f(-0.4,  0.4, -0.4);
  155.     glVertex3f(-0.4, -0.4, -0.4);
  156.     glEnd();
  157.    
  158.     glBegin(GL_POLYGON); // ppl side - TOP
  159.         glColor3f(0.3,  0.0,  0.7);
  160.     glVertex3f( 0.4,  0.4,  0.4);
  161.     glVertex3f( 0.4,  0.4, -0.4);
  162.         glColor3f(0.7,  0.3,  0.9);
  163.     glVertex3f(-0.4,  0.4, -0.4);
  164.     glVertex3f(-0.4,  0.4,  0.4);
  165.     glEnd();
  166.    
  167.     glBegin(GL_POLYGON); // Red side - BOTTOM
  168.         glColor3f(1.0,  0.9,  0.2);
  169.     glVertex3f( 0.4, -0.4, -0.4);
  170.     glVertex3f( 0.4, -0.4,  0.4);
  171.         glColor3f(0.8,  0.9,  0.5);
  172.     glVertex3f(-0.4, -0.4,  0.4);
  173.     glVertex3f(-0.4, -0.4, -0.4);
  174.     glEnd();
  175. }
  176.  
  177. void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
  178.    
  179.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  180.         glfwSetWindowShouldClose(window, GL_TRUE);
  181.    
  182.     if (key == GLFW_KEY_UP && action == GLFW_PRESS)
  183.         rotate_x += 5;
  184.     if (key == GLFW_KEY_DOWN && action == GLFW_PRESS)
  185.         rotate_x -= 5;
  186.     if (key == GLFW_KEY_RIGHT && action == GLFW_PRESS)
  187.         rotate_y -= 5;
  188.     if (key == GLFW_KEY_LEFT && action == GLFW_PRESS)
  189.         rotate_y += 5;
  190.    
  191.     if (key == GLFW_KEY_W && action == GLFW_PRESS)
  192.         trans_y += 0.05;
  193.     if (key == GLFW_KEY_A && action == GLFW_PRESS)
  194.         trans_x -= 0.05;
  195.     if (key == GLFW_KEY_D && action == GLFW_PRESS)
  196.         trans_x += 0.05;
  197.     if (key == GLFW_KEY_S && action == GLFW_PRESS)
  198.         trans_y -= 0.05;
  199.    // boolean
  200.     if (key == GLFW_KEY_Z && action == GLFW_PRESS)
  201.         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  202.     if (key == GLFW_KEY_X && action == GLFW_PRESS)
  203.         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  204.     //
  205. }
  206.  
  207. void mouseButtonCallback( GLFWwindow *window, int button, int action, int mods) {
  208.     if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) {
  209.         std::cout << fi << std::endl;
  210.         sc_x += 0.2;
  211.         sc_y += 0.2;
  212.         //sc_z += 0.2;
  213.     }
  214.    
  215.     if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
  216.         if (sc_x > 0.2 && sc_y > 0.2) {
  217.             std::cout << teta << std::endl;
  218.             sc_x -= 0.2;
  219.             sc_y -= 0.2;
  220.             //sc_z -= 0.2;
  221.         }
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement