Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <vector>
  5. #include <GLFW/glfw3.h>
  6.  
  7.  
  8. #define PI 3.14159265
  9.  
  10. #define SCREEN_WIDTH 640
  11. #define SCREEN_HEIGHT 480
  12.  
  13. GLfloat rotationX = 0;
  14. GLfloat rotationY = 0;
  15. GLfloat rotationZ = 0;
  16. GLfloat hW = SCREEN_WIDTH / 2;
  17. GLfloat hH = SCREEN_HEIGHT / 2;
  18.  
  19.  
  20.  
  21. GLfloat matrix[16] = {
  22.          1, 0, 0, 0,
  23.         0, 1, 0, 0,
  24.         -0.866f, -0.5f, 0, 0,
  25.         0, 0, 0, 1
  26. };
  27.  
  28. GLdouble x = 1;
  29. GLdouble y = 1;
  30. GLdouble z = 1;
  31.  
  32. GLfloat RAD = 180 / PI;
  33. GLfloat edge_C = 30;
  34.  
  35.  
  36. struct Point {
  37.     GLfloat x, y, z;
  38.  
  39.     Point(GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) {}
  40. };
  41.  
  42. struct Anti {
  43.     std::vector<Point> top, bottom, edges;
  44.     int n;
  45.     GLfloat r, h;
  46.     bool mode = true;
  47.  
  48.     void buildPoints() {
  49.         top.clear();
  50.         bottom.clear();
  51.         edges.clear();
  52.         for (int i = 0; i < 360; i += 360 / n) {
  53.             Point p1{ r * std::cos(i / RAD), r * std::sin(i / RAD), h / 2 };
  54.             Point p2{ r * std::cos(i / RAD), r * std::sin(i / RAD), -h / 2 };
  55.             top.push_back(p1);
  56.             bottom.push_back(p2);
  57.         }
  58.         GLfloat teta = PI / n;
  59.         GLfloat cosT = std::cos(teta);
  60.         GLfloat sinT = std::sin(teta);
  61.         GLfloat matr[3][3] = {
  62.             {cosT, sinT, 0 },
  63.             {-sinT, cosT, 0},
  64.             {0, 0, 1}
  65.         };
  66.  
  67.         for (int i = 0; i < top.size(); i++) {
  68.             Point p1 = top[i];
  69.             GLfloat a1 = matr[0][0] * p1.x + matr[0][1] * p1.y;
  70.             GLfloat a2 = matr[1][0] * p1.x + matr[1][1] * p1.y;
  71.             top[i].x = a1;
  72.             top[i].y = a2;
  73.         }
  74.  
  75.         for (int i = 0; i < top.size(); i++) {
  76.             Point p1 = top[i];
  77.             Point p2 = bottom[i];
  78.             Point p3 = top[(i + 1) % top.size()];
  79.             Point p4 = bottom[(i + 1) % bottom.size()];
  80.             edges.push_back(p1);
  81.             edges.push_back(p2);
  82.             edges.push_back(p3);
  83.             edges.push_back(p4);
  84.         }
  85.     }
  86.  
  87.     void drow() {
  88.         glColor3f(1.0, 1.0, 0.0);
  89.  
  90.         int isPolygon = mode ? GL_POLYGON : GL_LINE_LOOP;
  91.  
  92.         glBegin(isPolygon);
  93.         for (int i = 0; i < top.size(); i++) {
  94.             glVertex3f(top[i].x, top[i].y, top[i].z);
  95.         }
  96.         glEnd();
  97.         glBegin(isPolygon);
  98.         for (int i = 0; i < top.size(); i++) {
  99.             glVertex3f(bottom[i].x, bottom[i].y, bottom[i].z);
  100.         }
  101.         glEnd();
  102.  
  103.         for (int i = 0; i < edges.size(); i += 2) {
  104.             glColor3f(1.0, 1.0, 0.0);
  105.             glBegin(isPolygon);
  106.  
  107.  
  108.             glVertex3f(edges[i].x, edges[i].y, edges[i].z);
  109.             glVertex3f(edges[i + 1].x, edges[i + 1].y, edges[i + 1].z);
  110.             glVertex3f(edges[(i + 2) % edges.size()].x, edges[(i + 2) % edges.size()].y, edges[(i + 2) % edges.size()].z);
  111.  
  112.             glEnd();
  113.  
  114.             glBegin(isPolygon);
  115.  
  116.  
  117.             glVertex3f(edges[i + 1].x, edges[i + 1].y, edges[i + 1].z);
  118.             glVertex3f(edges[(i + 2) % edges.size()].x, edges[(i + 2) % edges.size()].y, edges[(i + 2) % edges.size()].z);
  119.             glVertex3f(edges[(i + 3) % edges.size()].x, edges[(i + 3) % edges.size()].y, edges[(i + 3) % edges.size()].z);
  120.  
  121.             glEnd();
  122.  
  123.             glColor3f(0.0, 0.0, 1.0);
  124.             glBegin(GL_LINE_LOOP);
  125.  
  126.  
  127.             glVertex3f(edges[i].x, edges[i].y, edges[i].z);
  128.             glVertex3f(edges[i + 1].x, edges[i + 1].y, edges[i + 1].z);
  129.             glVertex3f(edges[(i + 2) % edges.size()].x, edges[(i + 2) % edges.size()].y, edges[(i + 2) % edges.size()].z);
  130.  
  131.             glEnd();
  132.  
  133.             glBegin(GL_LINE_LOOP);
  134.  
  135.  
  136.             glVertex3f(edges[i + 1].x, edges[i + 1].y, edges[i + 1].z);
  137.             glVertex3f(edges[(i + 2) % edges.size()].x, edges[(i + 2) % edges.size()].y, edges[(i + 2) % edges.size()].z);
  138.             glVertex3f(edges[(i + 3) % edges.size()].x, edges[(i + 3) % edges.size()].y, edges[(i + 3) % edges.size()].z);
  139.  
  140.             glEnd();
  141.         }
  142.  
  143.     }
  144.  
  145. };
  146.  
  147. void cube() {
  148.  
  149.  
  150.     glBegin(GL_QUADS);
  151.  
  152.     glColor3f(0.0, 1.0, 0.0);
  153.     glVertex3f(-edge_C, -edge_C, -edge_C);
  154.     glVertex3f(-edge_C, edge_C, -edge_C);
  155.     glVertex3f(edge_C, edge_C, -edge_C);
  156.     glVertex3f(edge_C, -edge_C, -edge_C);
  157.  
  158.     glColor3f(1.0, 0.0, 0.0);
  159.     glVertex3f(-edge_C, -edge_C, edge_C);
  160.     glVertex3f(-edge_C, edge_C, edge_C);
  161.     glVertex3f(edge_C, edge_C, edge_C);
  162.     glVertex3f(edge_C, -edge_C, edge_C);
  163.  
  164.     glColor3f(0.0, 0.0, 1.0);
  165.     glVertex3f(-edge_C, edge_C, -edge_C);
  166.     glVertex3f(edge_C, edge_C, -edge_C);
  167.     glVertex3f(edge_C, edge_C, edge_C);
  168.     glVertex3f(-edge_C, edge_C, edge_C);
  169.  
  170.     glColor3f(1.0, 1.0, 0.0);
  171.     glVertex3f(-edge_C, -edge_C, -edge_C);
  172.     glVertex3f(edge_C, -edge_C, -edge_C);
  173.     glVertex3f(edge_C, -edge_C, edge_C);
  174.     glVertex3f(-edge_C, -edge_C, edge_C);
  175.  
  176.     glColor3f(0.5, 0.5, 0.5);
  177.     glVertex3f(-edge_C, -edge_C, -edge_C);
  178.     glVertex3f(-edge_C, edge_C, -edge_C);
  179.     glVertex3f(-edge_C, edge_C, edge_C);
  180.     glVertex3f(-edge_C, -edge_C, edge_C);
  181.  
  182.     glColor3f(1.0, 1.0, 1.0);
  183.     glVertex3f(edge_C, -edge_C, -edge_C);
  184.     glVertex3f(edge_C, edge_C, -edge_C);
  185.     glVertex3f(edge_C, edge_C, edge_C);
  186.     glVertex3f(edge_C, -edge_C, edge_C);
  187.     glEnd();
  188. }
  189.  
  190. Anti Prisma;
  191.  
  192. void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
  193. {
  194.     if (action == GLFW_PRESS || action == GLFW_REPEAT)
  195.     {
  196.         switch (key)
  197.         {
  198.         case GLFW_KEY_UP:
  199.             rotationX -= 10;
  200.             break;
  201.         case GLFW_KEY_DOWN:
  202.             rotationX += 10;
  203.             break;
  204.         case GLFW_KEY_RIGHT:
  205.             rotationY += 10;
  206.             break;
  207.         case GLFW_KEY_LEFT:
  208.             rotationY -= 10;
  209.             break;
  210.         case 61 :
  211.             rotationZ += 10;
  212.             break;
  213.         case 45:
  214.             rotationZ -= 10;
  215.             break;
  216.         case GLFW_KEY_D:
  217.             hW += 5;
  218.             break;
  219.         case GLFW_KEY_A:
  220.             hW -= 5;
  221.             break;
  222.         case GLFW_KEY_W:
  223.             hH += 5;
  224.             break;
  225.         case GLFW_KEY_S:
  226.             hH -= 5;
  227.             break;
  228.         case GLFW_KEY_Y:
  229.             y *= 1.1;
  230.             x *= 1.1;
  231.             z *= 1.1;
  232.  
  233.             break;
  234.         case GLFW_KEY_X:
  235.             x /= 1.1;
  236.             y /= 1.1;
  237.             z /= 1.1;
  238.             break;
  239.         case GLFW_KEY_Q:
  240.             Prisma.n++;
  241.             break;
  242.         case GLFW_KEY_Z:
  243.             if (Prisma.n > 3) {
  244.                 Prisma.n--;
  245.             }
  246.             break;
  247.         case GLFW_KEY_SPACE:
  248.             Prisma.mode = !Prisma.mode;
  249.             break;
  250.         }
  251.     }
  252.     Prisma.buildPoints();
  253. }
  254.  
  255.  
  256. int main() {
  257.     Prisma.n = 3;
  258.     Prisma.r = 100;
  259.     Prisma.h = 100;
  260.     if (!glfwInit()) {
  261.         return -1;
  262.     }
  263.  
  264.     GLFWwindow* window = glfwCreateWindow(640, 480, "My Title", NULL, NULL);
  265.  
  266.     if (!window) {
  267.         glfwTerminate();
  268.         return -1;
  269.     }
  270.  
  271.     glfwSetKeyCallback(window, keyCallback);
  272.     glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);
  273.  
  274.  
  275.     int screenWidth, screenHeight;
  276.     glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
  277.  
  278.  
  279.     glfwMakeContextCurrent(window);
  280.  
  281.     glViewport(0.0f, 0.0f, screenWidth, screenHeight);
  282.     glMatrixMode(GL_PROJECTION);
  283.     glLoadIdentity();
  284.     //glLoadMatrixf(matrix);
  285.     glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, 0, 1000);
  286.     glMatrixMode(GL_MODELVIEW);
  287.     glLoadIdentity();
  288.  
  289.     Prisma.buildPoints();
  290.     while (!glfwWindowShouldClose(window))
  291.     {
  292.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  293.  
  294.         glPushMatrix();
  295.         glTranslatef(hW, hH, -500);
  296.         glRotatef(rotationX, 1, 0, 0);
  297.         glRotatef(rotationY, 0, 1, 0);
  298.         glRotatef(rotationZ, 0, 0, 1);
  299.         glScaled(x, y, z);
  300.         glEnable(GL_DEPTH_TEST);
  301.         Prisma.drow();
  302.         glPopMatrix();
  303.  
  304.         glPushMatrix();
  305.         glTranslatef(60, 60, -500);
  306.         glRotatef(30, 1, 0, 0);
  307.         glRotatef(30, 0, 1, 0);
  308.         cube();
  309.         glPopMatrix();
  310.         glfwSwapBuffers(window);
  311.  
  312.         glfwPollEvents();
  313.     }
  314.  
  315.     glfwTerminate();
  316.  
  317.     return 0;
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement