Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.01 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.  
  21. void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
  22.     if (action == GLFW_PRESS || action == GLFW_REPEAT) {
  23.         switch (key) {
  24.             case GLFW_KEY_C:
  25.                 clippMode = !clippMode;
  26.                 break;
  27.         }
  28.     }
  29. }
  30.  
  31.  
  32. class Point {
  33. public:
  34.     GLfloat x;
  35.     GLfloat y;
  36.     GLfloat z;
  37.  
  38.     Point(GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) {}
  39. };
  40.  
  41. class Line {
  42. public:
  43.     Point p0;
  44.     Point p1;
  45.  
  46.     Point n;
  47.  
  48.     bool disp = true;
  49.  
  50.     Line(const Point &p0, const Point &p1) : p0(p0), p1(p1), n(Point(0, 0, 0)) {}
  51.  
  52.     void display() {
  53.         if (disp) {
  54.             glColor3d(0.65f, 0.2f, 0.1f);
  55.             glVertex3d(p0.x, p0.y, p0.z);
  56.             glVertex3d(p1.x, p1.y, p1.z);
  57.         }
  58.     }
  59.  
  60. };
  61.  
  62. class Plane {
  63. public:
  64.     Point A;
  65.     Point B;
  66.     Point C;
  67.     Point D;
  68.  
  69.     Point n;
  70.  
  71.     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)) {}
  72.  
  73.     void display() {
  74.         glColor3d(0.3f, 0.75f, 0.15f);
  75.         glVertex3d(A.x, A.y, A.z);
  76.         glVertex3d(B.x, B.y, B.z);
  77.  
  78.         glVertex3d(B.x, B.y, B.z);
  79.         glVertex3d(C.x, C.y, C.z);
  80.  
  81.         glVertex3d(C.x, C.y, C.z);
  82.         glVertex3d(D.x, D.y, D.z);
  83.  
  84.         glVertex3d(D.x, D.y, D.z);
  85.         glVertex3d(A.x, A.y, A.z);
  86.  
  87.     }
  88. };
  89.  
  90. double dot(Point p1, Point p2) {
  91.     return p1.x * p2.x + p1.y * p2.y + p1.z * p2.z;
  92. }
  93.  
  94.  
  95. int chopCI(double &tIn, double &tOut, double num, double denom) {
  96.     double tHit;
  97.     if (denom < 0) {
  98.         tHit = num / denom;
  99.         if (tHit > tOut) return 0;
  100.         else if (tHit > tIn) tIn = tHit;
  101.     } else if (denom > 0) {
  102.         tHit = num / denom;
  103.         if (tHit < tOut) tOut = tHit;
  104.     } else {
  105.         if (num <= 0) return 0;
  106.         return 1;
  107.     }
  108. }
  109.  
  110.  
  111. int main() {
  112.     GLFWwindow *window;
  113.  
  114.     if (!glfwInit()) {
  115.         return -1;
  116.     }
  117.  
  118.     window = glfwCreateWindow(640, 480, "wowWindowWow", NULL, NULL);
  119.     glfwSetKeyCallback(window, keyCallback);
  120.  
  121.     int screenWidth, screenHeight;
  122.     glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
  123.     glfwMakeContextCurrent(window);
  124.  
  125.     glMatrixMode(GL_MODELVIEW);
  126.     glLoadIdentity();
  127.  
  128.     vector<Plane> polygon;
  129.     vector<Line> lines;
  130.  
  131.     polygon.push_back(Plane(Point(-0.3f, +0.3f, +0.3f),
  132.                             Point(+0.3f, +0.3f, +0.3f),
  133.                             Point(+0.3f, -0.3f, +0.3f),
  134.                             Point(-0.3f, -0.3f, +0.3f)));
  135.     polygon[0].n = Point(0.0f, 0, 0.5f);
  136.  
  137.     polygon.push_back(Plane(Point(-0.3f, +0.3f, -0.3f),
  138.                             Point(+0.3f, +0.3f, -0.3f),
  139.                             Point(+0.3f, -0.3f, -0.3f),
  140.                             Point(-0.3f, -0.3f, -0.3f)));
  141.     polygon[1].n = Point(0.0f, 0, -0.5f);
  142.  
  143.     polygon.push_back(Plane(Point(-0.3f, +0.3f, +0.3f),
  144.                             Point(-0.3f, +0.3f, -0.3f),
  145.                             Point(-0.3f, -0.3f, -0.3f),
  146.                             Point(-0.3f, -0.3f, +0.3f)));
  147.     polygon[2].n = Point(-0.5f, 0, 0);
  148.  
  149.     polygon.push_back(Plane(Point(+0.3f, +0.3f, +0.3f),
  150.                             Point(+0.3f, +0.3f, -0.3f),
  151.                             Point(+0.3f, -0.3f, -0.3f),
  152.                             Point(+0.3f, -0.3f, +0.3f)));
  153.     polygon[3].n = Point(0.5f, 0, 0);
  154.  
  155.     polygon.push_back(Plane(Point(-0.3f, +0.3f, +0.3f),
  156.                             Point(-0.3f, +0.3f, -0.3f),
  157.                             Point(+0.3f, +0.3f, -0.3f),
  158.                             Point(+0.3f, +0.3f, +0.3f)));
  159.     polygon[4].n = Point(0, 0.5f, 0);
  160.  
  161.     polygon.push_back(Plane(Point(-0.3f, -0.3f, +0.3f),
  162.                             Point(-0.3f, -0.3f, -0.3f),
  163.                             Point(+0.3f, -0.3f, -0.3f),
  164.                             Point(+0.3f, -0.3f, +0.3f)));
  165.     polygon[5].n = Point(0, -0.5f, 0);
  166.  
  167.  
  168.     lines.push_back(Line(Point(0.8f, -0.3f, 0.213f), Point(-0.3f, 0.6f, 0.4f)));
  169.  
  170.     lines.push_back(Line(Point(0.9f, -0.2f, 0.2f), Point(0.63f, 0.5f, 0.5f)));
  171.  
  172.     lines.push_back(Line(Point(0.7f, -0.1f, -0.3f), Point(-0.7f, 0.2f, 0.4f)));
  173.  
  174.     lines.push_back(Line(Point(-0.7f, 0.1f, 0.7f), Point(0.4f, -0.5f, -0.35f)));
  175.  
  176.     lines.push_back(Line(Point(-0.5f, -0.3f, 0.2f), Point(0.4f, 0.5f, -0.1f)));
  177.  
  178.     lines.push_back(Line(Point(-0.3f, -0.5f, 0.1f), Point(0.3f, -0.5f, 0.5f)));
  179.  
  180.     lines.push_back(Line(Point(-0.2f, -0.1f, 0.2f), Point(0.2f, 0.1f, 0.6f)));
  181.  
  182.     while (!glfwWindowShouldClose(window)) {
  183.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  184.         glPushMatrix();
  185.  
  186.         glRotated(-25.0f, 1, 0, 0);
  187.         glRotated(38.4f, 0, 1, 0);
  188.  
  189.         glRotatef( rotationX, 1, 0, 0 );
  190.         glRotatef( rotationY, 0, 1, 0 );
  191.  
  192.         glBegin(GL_LINES);
  193.  
  194.         for (int i = 0; i < polygon.size(); i++) {
  195.             polygon[i].display();
  196.         }
  197.         if (!clippMode) {
  198.             for (int i = 0; i < lines.size(); i++) {
  199.                 lines[i].display();
  200.             }
  201.         }
  202.  
  203.         if (clippMode) {
  204.             for (int i = 0; i < lines.size(); i++) {
  205.                 double num, denom;
  206.                 double tIn = 0.0, tOut = 1.0;
  207.                 Point c = Point(lines[i].p1.x - lines[i].p0.x,
  208.                                 lines[i].p1.y - lines[i].p0.y,
  209.                                 lines[i].p1.z - lines[i].p0.z);
  210.  
  211.                 for (int j = 0; j < polygon.size(); j++) {
  212.                     Point tmp = Point(polygon[j].A.x - lines[i].p0.x,
  213.                                       polygon[j].A.y - lines[i].p0.y,
  214.                                       polygon[j].A.z - lines[i].p0.z);
  215.  
  216.                     num = dot(polygon[j].n, tmp);
  217.                     denom = dot(polygon[j].n, c);
  218.                     if (!chopCI(tIn, tOut, num, denom)) {
  219.                         lines[i].disp = false;
  220.                         break;
  221.                     }
  222.                 }
  223.                 if (tOut < 1.0) {
  224.                     lines[i].p1.x = lines[i].p0.x + c.x * tOut;
  225.                     lines[i].p1.y = lines[i].p0.y + c.y * tOut;
  226.                     lines[i].p1.z = lines[i].p0.z + c.z * tOut;
  227.                 }
  228.  
  229.                 if (tIn > 0.0) {
  230.                     lines[i].p0.x = lines[i].p0.x + c.x * tIn;
  231.                     lines[i].p0.y = lines[i].p0.y + c.y * tIn;
  232.                     lines[i].p0.z = lines[i].p0.z + c.z * tIn;
  233.                 }
  234.  
  235.                 for (int i = 0; i < lines.size(); i++) {
  236.                     lines[i].display();
  237.                 }
  238.  
  239.             }
  240.         }
  241.  
  242.         glEnd();
  243.         glPopMatrix();
  244.  
  245.         glfwSwapBuffers(window);
  246.         glfwPollEvents();
  247.     }
  248.  
  249.     return 0;
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement