Xom9ik

Lab_6/15var (IV semester) CG

Apr 7th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.84 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdlib.h>
  3. #include "glut.h"
  4. #include <vector>
  5. #include <array>
  6. #include <fstream>
  7. #include <iostream>
  8.  
  9. GLint Width = 512, Height = 512;
  10. bool load = false;
  11. int vp_width = 512;
  12. int vp_height = 512;
  13. std::array<int, 2> currentPt;
  14. std::vector<std::array<int, 2>> pts;
  15. bool closed = false;
  16. int input = 0;
  17.  
  18. void DisplayDefault()
  19. {
  20.     if (!(load)) {
  21.         glClearColor(0, 0.58823, 0.53333, 1.0);
  22.         glClear(GL_COLOR_BUFFER_BIT);
  23.         glColor3ub(205, 220, 57);
  24.         glBegin(GL_LINE_STRIP);
  25.         std::ifstream input_file("polygonCoordinates.txt");
  26.         if (input_file) {
  27.             std::istream_iterator<int> start(input_file), end;
  28.             std::vector<int> coordinates(start, end);
  29.             std::cout << "Read " << coordinates.size() << " numbers" << std::endl;
  30.             for (int i = 0; i<coordinates.size(); i += 2)
  31.                 pts.push_back(std::array<int, 2>{coordinates[i], coordinates[i + 1]});
  32.         }
  33.         else {
  34.             std::cout << "polygonCoordinates.txt is Empty" << std::endl;
  35.             pts.push_back(std::array<int, 2>{72, 323});
  36.             pts.push_back(std::array<int, 2>{73, 252});
  37.             pts.push_back(std::array<int, 2>{73, 252});
  38.             pts.push_back(std::array<int, 2>{112, 251});
  39.             pts.push_back(std::array<int, 2>{112, 251});
  40.             pts.push_back(std::array<int, 2>{128, 275});
  41.             pts.push_back(std::array<int, 2>{128, 275});
  42.             pts.push_back(std::array<int, 2>{96, 225});
  43.             pts.push_back(std::array<int, 2>{96, 225});
  44.             pts.push_back(std::array<int, 2>{124, 224});
  45.             pts.push_back(std::array<int, 2>{124, 224});
  46.             pts.push_back(std::array<int, 2>{154, 271});
  47.             pts.push_back(std::array<int, 2>{154, 271});
  48.             pts.push_back(std::array<int, 2>{143, 253});
  49.             pts.push_back(std::array<int, 2>{143, 253});
  50.             pts.push_back(std::array<int, 2>{253, 253});
  51.             pts.push_back(std::array<int, 2>{253, 253});
  52.             pts.push_back(std::array<int, 2>{221, 296});
  53.             pts.push_back(std::array<int, 2>{221, 296});
  54.             pts.push_back(std::array<int, 2>{220, 275});
  55.             pts.push_back(std::array<int, 2>{220, 275});
  56.             pts.push_back(std::array<int, 2>{237, 274});
  57.             pts.push_back(std::array<int, 2>{237, 274});
  58.             pts.push_back(std::array<int, 2>{221, 297});
  59.             pts.push_back(std::array<int, 2>{221, 297});
  60.             pts.push_back(std::array<int, 2>{108, 296});
  61.             pts.push_back(std::array<int, 2>{108, 296});
  62.             pts.push_back(std::array<int, 2>{95, 324});
  63.             pts.push_back(std::array<int, 2>{95, 324});
  64.             pts.push_back(std::array<int, 2>{73, 323});    
  65.         }
  66.         for (int i = 0; i<pts.size(); i++)
  67.             glVertex3f(pts[i][0], pts[i][1], 0);
  68.         glEnd();
  69.         glFinish();
  70.         load = true;
  71.     }
  72. }
  73. void Reshape(GLint w, GLint h)
  74. {
  75.     load = false;
  76.     Width = w; Height = h;
  77.     glViewport(0, 0, w, h);
  78.     glMatrixMode(GL_PROJECTION);
  79.     glLoadIdentity();
  80.     glOrtho(0, w, 0, h, -1.0, 1.0);
  81.     glMatrixMode(GL_MODELVIEW);
  82.     glLoadIdentity();
  83. }
  84. void Mouse_move(int x, int y)
  85. {
  86.     currentPt = std::array<int, 2>{x, vp_height - y};
  87.     glutPostRedisplay();
  88. }
  89. void Save()
  90. {
  91.     std::ofstream output_file("polygonCoordinates.txt", std::ofstream::out);
  92.         for (int i = 0; i<pts.size(); i++) {
  93.             if (i == 0)
  94.                 output_file << (float)pts[i][0] << " " << (float)pts[i][1] << std::endl;
  95.             else if (i<pts.size() - 1) {
  96.                 output_file << (float)pts[i][0] << " " << (float)pts[i][1] << std::endl;
  97.                 output_file << (float)pts[i][0] << " " << (float)pts[i][1] << std::endl;
  98.             }
  99.             else
  100.                 output_file << (float)pts[i][0] << " " << (float)pts[i][1] << std::endl;
  101.             glVertex2f((float)pts[i][0], (float)pts[i][1]);
  102.         }
  103.         output_file.flush();
  104.         output_file.close();
  105. }
  106. void Draw_polygon_line(int button, int state, int x, int y)
  107. {
  108.     if (input == 1) {
  109.         currentPt = std::array<int, 2>{x, vp_height - y};
  110.         if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  111.         {
  112.             if (closed)
  113.                 pts.clear();
  114.             closed = false;
  115.             pts.push_back(currentPt);
  116.         }
  117.         if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
  118.             closed = true;
  119.         std::cout << pts.size() << ") " << currentPt[0] << ", " << currentPt[1] << std::endl;
  120.         Save();
  121.     }
  122. }
  123. void DrawYourself() {
  124.     glClearColor(0.6196, 0.6196, 0.6196, 1.0);
  125.     glClear(GL_COLOR_BUFFER_BIT);
  126.     if (!pts.empty()) {
  127.         glBegin(GL_LINE_STRIP);
  128.         for (int i = 0; i<pts.size(); i++) {
  129.             glVertex2f((float)pts[i][0], (float)pts[i][1]);
  130.         }
  131.         auto &endPt = closed ? pts.front() : currentPt;
  132.         glVertex2f((float)endPt[0], (float)endPt[1]);
  133.         glEnd();
  134.         glFinish();
  135.     }
  136.     glutSwapBuffers();
  137. }
  138. void Keyboard(unsigned char key, int x, int y)
  139. {
  140.     std::cout << "Key pressed: " << key << std::endl;
  141.     int speed = 5;
  142.     switch (key) {
  143.     case 'd':
  144.         for (int i = 0; i<pts.size(); i++)
  145.             pts[i][0] = pts[i][0] + speed;
  146.         break;
  147.     case 'a':
  148.         for (int i = 0; i<pts.size(); i++)
  149.             pts[i][0] = pts[i][0] - speed;
  150.         break;
  151.     case 'w':
  152.         for (int i = 0; i<pts.size(); i++)
  153.             pts[i][1] = pts[i][1] + speed;
  154.         break;
  155.     case 's':
  156.         for (int i = 0; i<pts.size(); i++)
  157.             pts[i][1] = pts[i][1] - speed;
  158.         break;
  159.     default:
  160.         break;
  161.     }
  162.     glClearColor(0, 0.58823, 0.53333, 1.0);
  163.     glClear(GL_COLOR_BUFFER_BIT);
  164.     glColor3ub(205, 220, 57);
  165.     glBegin(GL_LINE_STRIP);
  166.     for (int i = 0; i < pts.size(); i++)
  167.         glVertex3f(pts[i][0], pts[i][1], 0);
  168.     glEnd();
  169.     glFinish();
  170. }
  171.  
  172. int main(int argc, char *argv[]) {
  173.     glutInit(&argc, argv);
  174.     glutInitDisplayMode(GLUT_RGB);
  175.     glutInitWindowSize(Width, Height);
  176.     glutCreateWindow("Computer Graphics #6");
  177.     glutReshapeFunc(Reshape);
  178.     glutMouseFunc(Draw_polygon_line);
  179.     glLineWidth(3.0f);
  180.     std::cout << "1-Draw by yourself" << std::endl;
  181.     std::cout << "2-Display Default Polygon (Control buttons (W, S, A, D))" << std::endl;
  182.     std::cin >> input;
  183.     switch (input) {
  184.     case 1:
  185.         glutDisplayFunc(DrawYourself);
  186.         DrawYourself();
  187.         break;
  188.     case 2:
  189.         glutDisplayFunc(DisplayDefault);
  190.         break;
  191.     default:
  192.         return 0;
  193.     }
  194.     glutPassiveMotionFunc(Mouse_move);
  195.     glMatrixMode(GL_PROJECTION);
  196.     glutKeyboardFunc(Keyboard);
  197.     glOrtho(0.0f, (float)vp_width, 0.0f, (float)vp_height, -1.0, 1.0);
  198.     glutMainLoop();
  199. }
Advertisement
Add Comment
Please, Sign In to add comment