Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include <windows.h>
  2. #include <glut.h>
  3. #include <stdio.h>
  4. #include <vector>
  5. #pragma comment (lib, "opengl32.lib")
  6. #pragma comment (lib, "glu32.lib")
  7. #pragma comment(lib,"glut32.lib")
  8.  
  9. using namespace std;
  10.  
  11. struct type_color {
  12.    int r;
  13.    int g;
  14.    int b;
  15. };
  16. type_color colors;
  17.  
  18. struct type_pair {
  19.    double x;
  20.    double y;
  21. };
  22. vector<vector<type_pair>> points;
  23. int area = 1;
  24.  
  25. void display() {
  26.    glPointSize(10);
  27.    glClearColor(0, 0, 0, 0);
  28.    glColor3ub(colors.r, colors.g, colors.b);
  29.    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  30.  
  31.    for (int j = 0; j < area; j++) {
  32.       glBegin(GL_LINE_LOOP);
  33.       for (int i = 0; i < points[j].size(); i++) {
  34.          glVertex2f(points[j][i].x, points[j][i].y);
  35.       }
  36.       glEnd();
  37.  
  38.       glBegin(GL_POINTS);
  39.       for (int i = 0; i < points[j].size(); i++)
  40.          glVertex2f(points[j][i].x, points[j][i].y);
  41.       glEnd();
  42.    }
  43.    glutSwapBuffers();
  44. }
  45.  
  46. void changeSize(int w, int h) {
  47.    glViewport(0, 0, w, h);
  48.    glMatrixMode(GL_PROJECTION);
  49.    glLoadIdentity();
  50.    glOrtho(0, w, 0, h, -1.0, 1.0);
  51.    glMatrixMode(GL_MODELVIEW);
  52.    glLoadIdentity();
  53. }
  54.  
  55. void mouseButton(int button, int state, int x, int y) {
  56.    if (button == GLUT_LEFT_BUTTON) {
  57.       if (state == GLUT_DOWN) {
  58.          type_pair elem;
  59.          elem.x = (x - 333.0) / 333;
  60.          elem.y = (333.0 - y) / 333;
  61.          points[area - 1].push_back(elem);
  62.       }
  63.    }
  64.    if (button == GLUT_RIGHT_BUTTON) {
  65.       if (state == GLUT_DOWN) {
  66.          area++;
  67.          points.resize(area);
  68.       }
  69.    }
  70.    glutPostRedisplay();
  71. }
  72.  
  73. void processNormalKeys(unsigned char key, int x, int y) {
  74.    printf("%d %d %d\n", key, x, y);
  75.    switch (key) {
  76.    case '1':
  77.       colors.r = colors.g = colors.g = 255;
  78.       printf("white");
  79.       break;
  80.    case '2':
  81.       colors.r = 255;
  82.       colors.g = colors.g = 0;
  83.       printf("red");
  84.       break;
  85.    }
  86. }
  87.  
  88. void processSpecialKeys(int key, int x, int y) {
  89.    switch (key) {
  90.    case GLUT_KEY_F1:
  91.       glPointSize(10);
  92.          glBegin(GL_POINTS);
  93.          glVertex2f(0, 0.5);
  94.       glEnd();
  95.    case GLUT_KEY_F2:
  96.       glPointSize(10);
  97.       glBegin(GL_POINTS);
  98.       glVertex2f(0, 0);
  99.       glEnd();
  100.    case GLUT_KEY_F3:
  101.       glPointSize(10);
  102.       glBegin(GL_POINTS);
  103.       glVertex2f(-0.5, 0.5);
  104.       glEnd();
  105.    }
  106. }
  107.  
  108. int main(int argc, char** argv) {
  109.    glutInit(&argc, argv);
  110.    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
  111.    glutInitWindowPosition(400, 400);
  112.    glutInitWindowSize(666, 666);
  113.    glutCreateWindow("New");
  114.  
  115.    points.resize(1);
  116.    colors.r = colors.g = colors.b = 255;
  117.  
  118.    //glutReshapeFunc(changeSize);
  119.    glutMouseFunc(mouseButton);
  120.    //glutSpecialFunc(processSpecialKeys);
  121.    glutKeyboardFunc(processNormalKeys);
  122.  
  123.    glutDisplayFunc(display);
  124.    
  125.    glutMainLoop();
  126.  
  127.    return 1;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement