Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.88 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GLFW/glfw3.h>
  3. #include <iostream>
  4. #include <math.h>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. #define SCREEN_WIDTH 800
  10. #define SCREEN_HEIGHT 600
  11. GLfloat rotationX = 0;
  12. GLfloat rotationY = 0;
  13.  
  14. GLfloat moveX = 0.0f;
  15. GLfloat moveY = 0.0f;
  16.  
  17. GLfloat polygonL = 0.3f;
  18.  
  19. bool clippMode = false;
  20. bool normMode = false;
  21.  
  22. void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
  23.     if (action == GLFW_PRESS || action == GLFW_REPEAT) {
  24.         switch (key) {
  25.             case GLFW_KEY_F:
  26.                 clippMode = !clippMode;
  27.                 break;
  28.             case GLFW_KEY_N:
  29.                 normMode = !normMode;
  30.                 break;
  31.             case GLFW_KEY_UP:
  32.                 rotationX -= 5;
  33.                 break;
  34.             case GLFW_KEY_DOWN:
  35.                 rotationX += 5;
  36.                 break;
  37.             case GLFW_KEY_RIGHT:
  38.                 rotationY += 5;
  39.                 break;
  40.             case GLFW_KEY_LEFT:
  41.                 rotationY -= 5;
  42.                 break;
  43.         }
  44.     }
  45. }
  46.  
  47.  
  48. class Point {
  49. public:
  50.     GLfloat x;
  51.     GLfloat y;
  52.     GLfloat z;
  53.  
  54.     Point(GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) {}
  55. };
  56.  
  57. class Line {
  58. public:
  59.     Point p0;
  60.     Point p1;
  61.  
  62.     Point n;
  63.  
  64.     bool disp = true;
  65.  
  66.     Line(const Point &p0, const Point &p1) : p0(p0), p1(p1), n(Point(0, 0, 0)) {}
  67.  
  68.     void display() {
  69.         if (disp) {
  70.             glColor3d(0.8f, 0, 0);
  71.             glVertex3d(p0.x, p0.y, p0.z);
  72.             glVertex3d(p1.x, p1.y, p1.z);
  73.         }
  74.     }
  75.  
  76. };
  77.  
  78. class Plane {
  79. public:
  80.     Point A;
  81.     Point B;
  82.     Point C;
  83.     Point D;
  84.  
  85.     Point n;
  86.  
  87.     Plane(const Point &A, const Point &B, const Point &C, const Point &D) : A(A), B(B), C(C), D(D), n(Point(0, 0, 0)) {
  88.         //   compN();
  89.     }
  90.  
  91.     Point compN() {
  92.         GLfloat Vector[3], Vector2[3];
  93.  
  94.         //3 points of Vector quad
  95.         GLfloat v1[] = {A.x, A.y, A.z};
  96.         GLfloat v2[] = {B.x, B.y, B.z};
  97.         GLfloat v3[] = {C.x, C.y, C.z};
  98.  
  99.  
  100.         Vector[0] = v3[0] - v1[0];
  101.         Vector[1] = v3[1] - v1[1];
  102.         Vector[2] = v3[2] - v1[2];
  103.  
  104.         Vector2[0] = v2[0] - v1[0];
  105.         Vector2[1] = v2[1] - v1[1];
  106.         Vector2[2] = v2[2] - v1[2];
  107.  
  108.         GLfloat Product[3];
  109.  
  110.         //Cross product formula
  111.         Product[0] = (Vector[1] * Vector2[2]) - (Vector[2] * Vector2[1]);
  112.         Product[1] = (Vector[2] * Vector2[0]) - (Vector[0] * Vector2[2]);
  113.         Product[2] = (Vector[0] * Vector2[1]) - (Vector[1] * Vector2[0]);
  114.  
  115.         Point an(0, 0, 0);
  116.  
  117.         an.x = Product[0];
  118.         an.y = Product[1];
  119.         an.z = Product[2];
  120.  
  121.         return an;
  122.     }
  123.  
  124.     void display(bool norm) {
  125.         glColor3d(1, 1, 1);
  126.         glVertex3d(A.x, A.y, A.z);
  127.         glVertex3d(B.x, B.y, B.z);
  128.  
  129.         glVertex3d(B.x, B.y, B.z);
  130.         glVertex3d(C.x, C.y, C.z);
  131.  
  132.         glVertex3d(C.x, C.y, C.z);
  133.         glVertex3d(D.x, D.y, D.z);
  134.  
  135.         glVertex3d(D.x, D.y, D.z);
  136.         glVertex3d(A.x, A.y, A.z);
  137.  
  138.         if (norm) {
  139.             GLfloat dx = (A.x + B.x + C.x + D.x) / 4;
  140.             GLfloat dy = (A.y + B.y + C.y + D.y) / 4;
  141.             GLfloat dz = (A.z + B.z + C.z + D.z) / 4;
  142.             glColor3f(1, 0, 1);
  143.             glVertex3d(dx, dy, dz);
  144.             glVertex3d(n.x + dx, n.y + dy, n.z + dz);
  145.         }
  146.     }
  147. };
  148.  
  149. double dot(Point p1, Point p2) {
  150.     return p1.x * p2.x + p1.y * p2.y + p1.z * p2.z;
  151. }
  152.  
  153.  
  154. int chopCI(double &tIn, double &tOut, double num, double denom) {
  155.     double tHit;
  156.     if (denom < 0) {
  157.         tHit = num / denom;
  158.         if (tHit > tOut) return 0;
  159.         else if (tHit > tIn) tIn = tHit;
  160.  
  161.     } else if (denom > 0) {
  162.         tHit = num / denom;
  163.         if (tHit < tOut) tOut = tHit;
  164.     } else {
  165.         if (num <= 0) return 0;
  166.         return 1;
  167.     }
  168. }
  169.  
  170.  
  171. int main() {
  172.     GLFWwindow *window;
  173.  
  174.     if (!glfwInit()) {
  175.         return -1;
  176.     }
  177.  
  178.     window = glfwCreateWindow(800, 600, "wowWindowWow", NULL, NULL);
  179.     glfwSetKeyCallback(window, keyCallback);
  180.  
  181.     int screenWidth, screenHeight;
  182.     glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
  183.     glfwMakeContextCurrent(window);
  184.  
  185.     glMatrixMode(GL_MODELVIEW);
  186.     glLoadIdentity();
  187.  
  188.     vector<Plane> polygon;
  189.     vector<Line> lines;
  190.  
  191.     polygon.push_back(Plane(Point(-0.3f, +0.3f, +0.3f),
  192.                             Point(+0.3f, +0.3f, +0.3f),
  193.                             Point(+0.3f, -0.3f, +0.3f),
  194.                             Point(-0.3f, -0.3f, +0.3f)));
  195.     polygon[0].n = Point(0.0f, 0, 0.5f);
  196.  
  197.     polygon.push_back(Plane(Point(-0.3f, +0.3f, -0.3f),
  198.                             Point(+0.3f, +0.3f, -0.3f),
  199.                             Point(+0.3f, -0.3f, -0.3f),
  200.                             Point(-0.3f, -0.3f, -0.3f)));
  201.     polygon[1].n = Point(0.0f, 0, -0.5f);
  202.  
  203.     polygon.push_back(Plane(Point(-0.3f, +0.3f, +0.3f),
  204.                             Point(-0.3f, +0.3f, -0.3f),
  205.                             Point(-0.3f, -0.3f, -0.3f),
  206.                             Point(-0.3f, -0.3f, +0.3f)));
  207.     polygon[2].n = Point(-0.5f, 0, 0);
  208.  
  209.     polygon.push_back(Plane(Point(+0.3f, +0.3f, +0.3f),
  210.                             Point(+0.3f, +0.3f, -0.3f),
  211.                             Point(+0.3f, -0.3f, -0.3f),
  212.                             Point(+0.3f, -0.3f, +0.3f)));
  213.     polygon[3].n = Point(0.5f, 0, 0);
  214.  
  215.     polygon.push_back(Plane(Point(-0.3f, +0.3f, +0.3f),
  216.                             Point(-0.3f, +0.3f, -0.3f),
  217.                             Point(+0.3f, +0.3f, -0.3f),
  218.                             Point(+0.3f, +0.3f, +0.3f)));
  219.     polygon[4].n = Point(0, 0.5f, 0);
  220.  
  221.     polygon.push_back(Plane(Point(-0.3f, -0.3f, +0.3f),
  222.                             Point(-0.3f, -0.3f, -0.3f),
  223.                             Point(+0.3f, -0.3f, -0.3f),
  224.                             Point(+0.3f, -0.3f, +0.3f)));
  225.     polygon[5].n = Point(0, -0.5f, 0);
  226.  
  227.  
  228.     lines.push_back(Line(Point(-0.8f, 0, 0), Point(0.8f, 0.2f, -0.15f)));
  229.     lines.push_back(Line(Point(-0.1f, -0.1f,-0.1f), Point(0.1f, 0.1f,0.1f)));
  230.  
  231.     lines.push_back(Line(Point(0.5f, 0.5f,0.5f), Point(0.0f, 0.4f,0.0f)));
  232.     lines.push_back(Line(Point(0.4f, 0.6f, 0.4f), Point(-0.4f, 0.6f, 0.4f)));
  233.  
  234.     lines.push_back(Line(Point(0.4f, -0.3f, -0.5f), Point(-0.4f, 0.55f, 0.45f)));
  235.     lines.push_back(Line(Point(0.0f, -0.6f, 0.0f), Point(-0.0f, 0.6f, 0.0f)));
  236.  
  237.     while (!glfwWindowShouldClose(window)) {
  238.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  239.         glPushMatrix();
  240. //        glRotated(-35.26f, 1, 0, 0);
  241.         glRotated(-30.0f, 1, 0, 0);
  242.         glRotated(-40.0f, 0, 1, 0);
  243.  
  244.         glRotatef( rotationX, 1, 0, 0 );
  245.         glRotatef( rotationY, 0, 1, 0 );
  246.  
  247.         glBegin(GL_LINES);
  248.  
  249.         for (int i = 0; i < polygon.size(); i++) {
  250.             polygon[i].display(normMode);
  251.         }
  252.         if (!clippMode) {
  253.             for (int i = 0; i < lines.size(); i++) {
  254.                 lines[i].display();
  255.             }
  256.         }
  257.  
  258.         if (clippMode) {
  259.             for (int i = 0; i < lines.size(); i++) {
  260.                 double num, denom;
  261.                 double tIn = 0.0, tOut = 1.0;
  262.                 Point c = Point(lines[i].p1.x - lines[i].p0.x,
  263.                                 lines[i].p1.y - lines[i].p0.y,
  264.                                 lines[i].p1.z - lines[i].p0.z);
  265.  
  266.                 for (int j = 0; j < polygon.size(); j++) {
  267.                     Point tmp = Point(polygon[j].A.x - lines[i].p0.x,
  268.                                       polygon[j].A.y - lines[i].p0.y,
  269.                                       polygon[j].A.z - lines[i].p0.z);
  270.  
  271.                     num = dot(polygon[j].n, tmp);
  272.                     denom = dot(polygon[j].n, c);
  273.                     if (!chopCI(tIn, tOut, num, denom)) {
  274.                         lines[i].disp = false;
  275.                         break;
  276.                     }
  277.                 }
  278.                 if (tOut < 1.0) {
  279.                     lines[i].p1.x = lines[i].p0.x + c.x * tOut;
  280.                     lines[i].p1.y = lines[i].p0.y + c.y * tOut;
  281.                     lines[i].p1.z = lines[i].p0.z + c.z * tOut;
  282.                 }
  283.  
  284.                 if (tIn > 0.0) {
  285.                     lines[i].p0.x = lines[i].p0.x + c.x * tIn;
  286.                     lines[i].p0.y = lines[i].p0.y + c.y * tIn;
  287.                     lines[i].p0.z = lines[i].p0.z + c.z * tIn;
  288.                 }
  289.  
  290.                 for (int i = 0; i < lines.size(); i++) {
  291.                     lines[i].display();
  292.                 }
  293.  
  294.             }
  295.         }
  296.  
  297.         glEnd();
  298.         glPopMatrix();
  299.  
  300.         glfwSwapBuffers(window);
  301.         glfwPollEvents();
  302.     }
  303.  
  304.     return 0;
  305. }
  306.  
  307. /*
  308.     vector<Line> polygon;
  309.     vector<Line> lines;
  310.     vector<Point> normals;
  311.  
  312.     polygon.push_back(Line(Point(-polygonL + moveX, -polygonL + moveY),
  313.                            Point(polygonL + moveX, -polygonL + moveY)));
  314.  
  315.     polygon.push_back(Line(Point(polygonL + moveX, -polygonL + moveY),
  316.                            Point(polygonL + moveX, polygonL + moveY)));
  317.  
  318.     polygon.push_back(Line(Point(polygonL + moveX, polygonL + moveY),
  319.                            Point(-polygonL + moveX, polygonL + moveY)));
  320.  
  321.     polygon.push_back(Line(Point(-polygonL + moveX, polygonL + moveY),
  322.                            Point(-polygonL + moveX, -polygonL + moveY)));
  323.  
  324.  
  325.     for (int i = 0; i < polygon.size(); i++)
  326.         polygon[i].n = Point(polygon[i].p1.y -polygon[i].p0.y, -(polygon[i].p1.x - polygon[i].p0.x));
  327.  
  328.  
  329.     lines.push_back(Line(Point(0.8f, -0.3f), Point(-0.3f, 0.6f)));
  330.  
  331.     //lines.push_back(Line(Point(0.9f, -0.2f), Point(0.63f, 0.5f)));
  332.  
  333.     lines.push_back(Line(Point(0.7f, -0.1f), Point(-0.7f, 0.2f)));
  334.  
  335.     lines.push_back(Line(Point(-0.7f, 0.1f), Point(0.4f, -0.5f)));
  336.  
  337.     lines.push_back(Line(Point(-0.5f, -0.3f), Point(0.4f, 0.5f)));
  338.  
  339.  
  340.  
  341.  
  342.  
  343.     while (!glfwWindowShouldClose(window)) {
  344.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  345.         glPushMatrix();
  346.         glRotated(-35.26f, 1, 0, 0);
  347.         glRotated(-45.0f, 0, 1, 0);
  348.  
  349.  
  350.         //glTranslated(0, -0.5f, 0);
  351.         glBegin(GL_LINES);
  352.  
  353.         glColor3d(0.7, 0.0, 0.0);
  354.         glVertex3d(0.0, 0.0, 0.0);
  355.         glVertex3d(1, 0.0, 0.0);
  356.  
  357.         glColor3d(0.0, 0.7, 0.0);
  358.         glVertex3d(0.0, 0.0, 0.0);
  359.         glVertex3d(0.0, 1, 0.0);
  360.  
  361.         glColor3d(0.0, 0.0, 0.7);
  362.         glVertex3d(0.0, 0.0, 0.0);
  363.         glVertex3d(0.0, 0.0, 1);
  364.  
  365.  
  366.         glColor3d(0.0, 0.0, 0.7);
  367.         for (int i = 0; i < polygon.size(); i++) {
  368.             polygon[i].display(false);
  369.         }
  370.  
  371.         glColor3d(1, 1, 1);
  372.         if (clippMode) {
  373.             for (int i = 0; i < lines.size(); i++) {
  374.                 double num, denom;
  375.                 double tIn = 0.0, tOut = 1.0;
  376.                 Point c = Point(lines[i].p1.x - lines[i].p0.x, lines[i].p1.y - lines[i].p0.y);
  377.  
  378.                 for (int j = 0; j < polygon.size(); j++) {
  379.                     Point tmp = Point(polygon[j].p0.x - lines[i].p0.x, polygon[j].p0.y - lines[i].p0.y);
  380.                     num = dot(polygon[j].n, tmp);
  381.                     denom = dot(polygon[j].n, c);
  382.                     if (!chopCI(tIn, tOut, num, denom)) break;
  383.                 }
  384.                 if(tOut < 1.0){
  385.                     lines[i].p1.x = lines[i].p0.x + c.x * tOut;
  386.                     lines[i].p1.y = lines[i].p0.y + c.y * tOut;
  387.                 }
  388.  
  389.                 if(tIn > 0.0){
  390.                     lines[i].p0.x = lines[i].p0.x + c.x * tIn;
  391.                     lines[i].p0.y = lines[i].p0.y + c.y * tIn;
  392.                 }
  393.  
  394.                 for (int i = 0; i < lines.size(); i++) {
  395.                     lines[i].display(false);
  396.                 }
  397.  
  398.             }
  399.         } else {
  400.             for (int i = 0; i < lines.size(); i++) {
  401.                 lines[i].display(false);
  402.             }
  403.         }
  404.  
  405.  
  406.         glEnd();
  407.         glPopMatrix();
  408.  
  409.         glfwSwapBuffers(window);
  410.         glfwPollEvents();
  411.     }
  412.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement