Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2019
76
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. struct circleData {
  9.     int pointsAmount = 6;
  10.     float** pointData;
  11. };
  12.  
  13. void keyboard_callback(GLFWwindow *window, int key, int scancode, int action, int mods);
  14. void drawCylinder(float cylinderHeight, circleData circle, float cylinderRadius, circleData *massOfCircles);
  15. void mouseButtonCallback( GLFWwindow *window, int button, int action, int mods);
  16. void dimetria();
  17. circleData* init(float cylinderHeight, circleData circle, float cylinderRadius);
  18.  
  19. int rotate_y = 0;
  20. int rotate_x = 0;
  21. int rotate_z = 0;
  22.  
  23. float trans_x = 0;
  24. float trans_y = 0;
  25. float trans_z = 0;
  26.  
  27. float sc_x = 0.5;
  28. float sc_y = 0.5;
  29. float sc_z = 0.5;
  30.  
  31. int circlesAmount = 3;
  32.  
  33. float fi = (asin(0.625/sqrt(2.0-0.625*0.625))); //62,82
  34. float teta = (asin(0.625/sqrt(2.0))); //41,6
  35.  
  36. GLfloat m[16] = { cos(fi), sin(fi)*sin(teta), sin(fi)*cos(teta), 0,
  37.                   0, cos(teta), -sin(teta), 0,
  38.                   sin(fi), -cos(fi)*sin(teta), -cos(fi)*cos(teta), 0,
  39.                   0, 0, 0, 1
  40. }; //как получилась матрица расписать два поворота по х, у
  41.  
  42.  
  43. circleData circle;
  44. circleData *mass;
  45.  
  46. int main(int argc, char const *argv[]) {
  47.    
  48.     if(!glfwInit()) {
  49.         exit(EXIT_FAILURE);
  50.     }
  51.    
  52.     //glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
  53.    
  54.     GLFWwindow *window = glfwCreateWindow(620, 620, "Rorate Cube", NULL, NULL);
  55.    
  56.     if (!window) {
  57.         glfwTerminate();
  58.         exit(EXIT_FAILURE);
  59.     }
  60.     glfwSetMouseButtonCallback(window, mouseButtonCallback);
  61.     glfwMakeContextCurrent(window);
  62.     glfwSwapInterval(1);
  63.    
  64.     glEnable(GL_DEPTH_TEST);
  65.    
  66.     circleData *mass = init(1.0, circle, 0.5);
  67.    
  68.     while (!glfwWindowShouldClose(window))
  69.     {
  70.         float ratio;
  71.         int width, height;
  72.         glfwGetFramebufferSize(window, &width, &height);
  73.         ratio = width / (float) height;
  74.         glViewport(0, 0, width, height);
  75.        
  76.         glfwSetKeyCallback(window, keyboard_callback);
  77.        
  78.         glClearColor(0, 0, 0, 0);
  79.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  80.        
  81.         glLoadIdentity();
  82.         glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
  83.         //вращение
  84.        
  85.         glTranslatef(0.4, 0.4, 0.4);
  86.         glTranslatef(trans_x, trans_y, trans_z);
  87.        
  88.         glScalef(sc_x, sc_y, sc_z);
  89.        
  90.         glRotatef(rotate_x, 1.0, 0.0, 0.0);  //повороты подломаны???
  91.         glRotatef(rotate_y, 0.0, 1.0, 0.0);
  92.         glRotatef(rotate_z, 0.0, 0.0, 1.0);
  93.        
  94.         dimetria(); //проекция через повороты/ротейты
  95.         //glMultMatrixf(m);
  96.        
  97.         //drawCube();
  98.        
  99.         drawCylinder(1.0, circle, 0.5, mass);
  100.        
  101.        
  102.         glLoadIdentity();
  103.         glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
  104.        
  105.         glTranslatef(-0.6, -0.4, -0.6);
  106.        
  107.         glScalef(0.2, 0.2, 0.2);
  108.         //
  109.         glRotatef(15, 0.0, -1.0, 0.0);
  110.         glRotatef(15, -1.0, 0.0, 0.0);
  111.         //
  112.         //dimetria();
  113.         glMultMatrixf(m); //проекция через формулу с лекции
  114.        
  115.         //drawCube();
  116.         drawCylinder(1.0, circle, 0.5, mass);
  117.        
  118.  
  119.        
  120.        
  121.        
  122.         glfwSwapBuffers(window);
  123.         glfwPollEvents();
  124.     }
  125.    
  126.     glfwTerminate();
  127.     return 0;
  128. }
  129.  
  130. circleData* init(float cylinderHeight, circleData circle, float cylinderRadius) {
  131.    
  132.     circleData *massOfCircles = new circleData[circlesAmount]; //массив из 4 элементов, каждый из которых содержит кол-во точек 1 кольца и координаты хуz каждой точки
  133.    
  134.     for (int ii=0; ii<circlesAmount; ii++) {
  135.         massOfCircles[ii].pointData = new float*[circle.pointsAmount];
  136.         for (int i=0; i<circle.pointsAmount; i++)
  137.             massOfCircles[ii].pointData[i] = new float[3];
  138.     }
  139.    
  140.    
  141.     float dh = cylinderHeight / circlesAmount;
  142.     float dy = 0.0;
  143.     for(int j=0; j<circlesAmount; j++) {
  144.        
  145.         for(int i=0; i<circle.pointsAmount; i++) { //пробег по точкам j-го круга
  146.             float degInRad = ((3.14159*2)*i)/circle.pointsAmount;
  147.             massOfCircles[j].pointData[i][0] = cos(degInRad)*cylinderRadius;
  148.             massOfCircles[j].pointData[i][1] = dy+dh;
  149.             massOfCircles[j].pointData[i][2] = sin(degInRad)*cylinderRadius;
  150.             std::cout << massOfCircles[j].pointData[i][0] << std::endl;
  151.             std::cout << massOfCircles[j].pointData[i][1] << std::endl;
  152.             std::cout << massOfCircles[j].pointData[i][2] << std::endl;
  153.         }
  154.         dy += dh;
  155.     }
  156.     return massOfCircles;
  157. }
  158.  
  159. void dimetria() {
  160.    
  161.     //glRotatef(180.0, 0.0, 1.0, 0.0);
  162.     glRotatef(-fi*(180/3.14) , 1.0, 0.0, 0.0);
  163.     glRotatef(-teta*(180/3.14) , 0.0, 1.0, 0.0);
  164.     //glRotatef(180.0 , 0.0, 1.0, 0.0);
  165. }
  166.  
  167. void drawCylinder(float cylinderHeight, circleData circle, float cylinderRadius, circleData *massOfCircles) {
  168.    
  169.     float dh = cylinderHeight / circlesAmount;
  170.     float dy = 0.0;
  171.     for(int j=0; j<circlesAmount; j++) {
  172.         glBegin(GL_POLYGON);
  173.         for(int i=0; i<circle.pointsAmount; i++) { //пробег по точкам j-го круга
  174.             glVertex3f(massOfCircles[j].pointData[i][0], massOfCircles[j].pointData[i][1], massOfCircles[j].pointData[i][2]);
  175.         }
  176.         glEnd();
  177.         dy += dh;
  178.        
  179.     }
  180.    
  181.     for (int j=0; j<circlesAmount-1; j++) {
  182.         for (int i=0; i<circle.pointsAmount-1; i++) {
  183.             glBegin(GL_POLYGON);
  184.                 glColor3f(0.3,  0.0,  0.7);
  185.                 glVertex3f(massOfCircles[j].pointData[i][0], massOfCircles[j].pointData[i][1], massOfCircles[j].pointData[i][2]);
  186.                 glVertex3f(massOfCircles[j].pointData[i+1][0], massOfCircles[j].pointData[i+1][1], massOfCircles[j].pointData[i+1][2]);
  187.                 glVertex3f(massOfCircles[j+1].pointData[i][0], massOfCircles[j+1].pointData[i][1], massOfCircles[j+1].pointData[i][2]);
  188.             glEnd();
  189.            
  190.             glBegin(GL_POLYGON);
  191.                 glColor3f(0.04,  0.14,  0.6);
  192.                 glVertex3f(massOfCircles[j].pointData[i+1][0], massOfCircles[j].pointData[i+1][1], massOfCircles[j].pointData[i+1][2]);
  193.                 glVertex3f(massOfCircles[j+1].pointData[i][0], massOfCircles[j+1].pointData[i][1], massOfCircles[j+1].pointData[i][2]);
  194.                 glVertex3f(massOfCircles[j+1].pointData[i+1][0], massOfCircles[j+1].pointData[i+1][1], massOfCircles[j+1].pointData[i+1][2]);
  195.             glEnd();
  196.         }
  197.     }
  198.     for (int j=0; j<circlesAmount-1; j++) {
  199.             glBegin(GL_POLYGON);
  200.             glColor3f(0.3,  0.0,  0.7);
  201.             glVertex3f(massOfCircles[j].pointData[0][0], massOfCircles[j].pointData[0][1], massOfCircles[j].pointData[0][2]);
  202.             glVertex3f(massOfCircles[j].pointData[circle.pointsAmount-1][0], massOfCircles[j].pointData[circle.pointsAmount-1][1], massOfCircles[j].pointData[circle.pointsAmount-1][2]);
  203.             glVertex3f(massOfCircles[j+1].pointData[0][0], massOfCircles[j+1].pointData[0][1], massOfCircles[j+1].pointData[0][2]);
  204.             glEnd();
  205.            
  206.             glBegin(GL_POLYGON);
  207.             glColor3f(0.04,  0.14,  0.6);
  208.             glVertex3f(massOfCircles[j].pointData[circle.pointsAmount-1][0], massOfCircles[j].pointData[circle.pointsAmount-1][1], massOfCircles[j].pointData[circle.pointsAmount-1][2]);
  209.             glVertex3f(massOfCircles[j+1].pointData[0][0], massOfCircles[j+1].pointData[0][1], massOfCircles[j+1].pointData[0][2]);
  210.             glVertex3f(massOfCircles[j+1].pointData[circle.pointsAmount-1][0], massOfCircles[j+1].pointData[circle.pointsAmount-1][1], massOfCircles[j+1].pointData[circle.pointsAmount-1][2]);
  211.             glEnd();
  212.     }
  213. }
  214.  
  215. void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
  216.    
  217.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  218.         glfwSetWindowShouldClose(window, GL_TRUE);
  219.    
  220.     if (key == GLFW_KEY_UP && action == GLFW_PRESS)
  221.         rotate_x += 5;
  222.     if (key == GLFW_KEY_DOWN && action == GLFW_PRESS)
  223.         rotate_x -= 5;
  224.     if (key == GLFW_KEY_RIGHT && action == GLFW_PRESS)
  225.         rotate_y -= 5;
  226.     if (key == GLFW_KEY_LEFT && action == GLFW_PRESS)
  227.         rotate_y += 5;
  228.     if (key == GLFW_KEY_O && action == GLFW_PRESS)
  229.         rotate_z -= 5;
  230.     if (key == GLFW_KEY_L && action == GLFW_PRESS)
  231.         rotate_z += 5;
  232.    
  233.     if (key == GLFW_KEY_W && action == GLFW_PRESS)
  234.         trans_y += 0.05;
  235.     if (key == GLFW_KEY_A && action == GLFW_PRESS)
  236.         trans_x -= 0.05;
  237.     if (key == GLFW_KEY_D && action == GLFW_PRESS)
  238.         trans_x += 0.05;
  239.     if (key == GLFW_KEY_S && action == GLFW_PRESS)
  240.         trans_y -= 0.05;
  241.     //
  242.     if (key == GLFW_KEY_Z && action == GLFW_PRESS)
  243.         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  244.     if (key == GLFW_KEY_X && action == GLFW_PRESS)
  245.         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  246.     //
  247.     if (key == GLFW_KEY_C && action == GLFW_PRESS){
  248.         circle.pointsAmount++;
  249.         mass = init(1.0, circle, 0.5);
  250.         drawCylinder(1.0, circle, 0.5, mass);
  251.     }
  252.     if (key == GLFW_KEY_V && action == GLFW_PRESS){
  253.         circle.pointsAmount--;
  254.         mass = init(1.0, circle, 0.5);
  255.         drawCylinder(1.0, circle, 0.5, mass);
  256.     }
  257.     if (key == GLFW_KEY_B && action == GLFW_PRESS){
  258.         circlesAmount++;
  259.         delete mass;
  260.         mass = init(1.0, circle, 0.5);
  261.         //drawCylinder(1.0, circle, 0.5, mass);
  262.     }
  263.     if (key == GLFW_KEY_N && action == GLFW_PRESS){
  264.         circlesAmount--;
  265.         delete mass;
  266.         mass = init(1.0, circle, 0.5);
  267.         //drawCylinder(1.0, circle, 0.5, mass);
  268.     }
  269. }
  270.  
  271. void mouseButtonCallback( GLFWwindow *window, int button, int action, int mods) {
  272.     if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) {
  273.         std::cout << fi << std::endl;
  274.         sc_x += 0.2;
  275.         sc_y += 0.2;
  276.         //sc_z += 0.2;
  277.     }
  278.    
  279.     if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
  280.         if (sc_x > 0.2 && sc_y > 0.2) {
  281.             std::cout << teta << std::endl;
  282.             sc_x -= 0.2;
  283.             sc_y -= 0.2;
  284.             //sc_z -= 0.2;
  285.         }
  286.     }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement