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++ 4.42 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. double x, y, xOrigin = 0;
  12.  
  13. struct type_pair {
  14.    double x;
  15.    double y;
  16. };
  17. vector<vector<type_pair>> points;
  18. int area = 1;
  19.  
  20. void renderScene() {
  21.    glPointSize(10);
  22.    glClearColor(0, 0, 0, 0);
  23.    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  24.  
  25.    /*glBegin(GL_TRIANGLES);
  26.    glVertex3f(-0.5, -0.5, 0.0);
  27.    glVertex3f(0.0, 0.5, 0.0);
  28.    glVertex3f(0.5, -0.5, 0.0);
  29.    glEnd();*/
  30.    for (int j = 0; j < area; j++) {
  31.       glBegin(GL_LINE_LOOP);
  32.       /*glColor3d(1, 0, 0);
  33.       glVertex3d(1, 3, 0);
  34.       glVertex3d(4, 3, 0);
  35.       glColor3d(0, 1, 0);
  36.       glVertex3d(3, 2.7, 0);
  37.       glColor3d(0, 0, 1);
  38.       glVertex3d(2.5, 3.7, 0);*/
  39.       /*glVertex2f(0, 0);
  40.       glVertex2f(0, 0.5);
  41.       glVertex2f(0.5, 0);
  42.       glVertex2f(-0.7, -0.3);*/
  43.  
  44.       //glPointSize(10);
  45.       //glBegin(GL_POINTS);
  46.  
  47.       for (int i = 0; i < points[j].size(); i++) {
  48.          glVertex2f(points[j][i].x, points[j][i].y);
  49.       }
  50.  
  51.       glEnd();
  52.  
  53.       glBegin(GL_POINTS);
  54.  
  55.       for (int i = 0; i < points[j].size(); i++) {
  56.          glVertex2f(points[j][i].x, points[j][i].y);
  57.       }
  58.       glEnd();
  59.    }
  60.    glutSwapBuffers();
  61. }
  62.  
  63. void changeSize(int w, int h) {
  64.  
  65.    //// предупредим деление на ноль
  66.    //// если окно сильно перетянуто будет
  67.    //if (h == 0)
  68.    //   h = 1;
  69.    //float ratio = 1.0 * w / h;
  70.  
  71.    //// используем матрицу проекции
  72.    //glMatrixMode(GL_PROJECTION);
  73.  
  74.    //// Reset матрицы
  75.    //glLoadIdentity();
  76.  
  77.    //// определяем окно просмотра
  78.    //glViewport(0, 0, w, h);
  79.  
  80.    //// установить корректную перспективу.
  81.    //gluPerspective(45, ratio, 1, 1000);
  82.  
  83.    //// вернуться к модели
  84.    //glMatrixMode(GL_MODELVIEW);
  85.    //Width = w;    Height = h;
  86.    glViewport(0, 0, w, h);
  87.    glMatrixMode(GL_PROJECTION);
  88.    glLoadIdentity();
  89.    glOrtho(0, w, 0, h, -1.0, 1.0);
  90.    glMatrixMode(GL_MODELVIEW);
  91.    glLoadIdentity();
  92. }
  93.  
  94. void mouseButton(int button, int state, int x, int y) {
  95.    printf("%d %d %d %d\n", button, state, x, y);
  96.    // только при начале движения, если нажата левая кнопка
  97.    if (button == GLUT_LEFT_BUTTON) {
  98.  
  99.       // когда кнопка отпущена
  100.       if (state == GLUT_UP) {
  101.          printf("up\n");
  102.          glVertex2f(0, 0);
  103.       }
  104.       else {// state = GLUT_DOWN
  105.          printf("down\n");
  106.          type_pair elem;
  107.          elem.x = (x - 333.0) / 333;
  108.          elem.y = (333.0 - y) / 333;
  109.          if (area != points.size()) {
  110.             /*points.resize(area + 1);*/
  111.          }
  112.          points[area - 1].push_back(elem);
  113.       }
  114.    }
  115.    if (button == GLUT_RIGHT_BUTTON) {
  116.  
  117.       // когда кнопка отпущена
  118.       if (state == GLUT_UP) {
  119.          printf("up\n");
  120.       }
  121.       else {// state = GLUT_DOWN
  122.          printf("down\n");
  123.          area++;
  124.          points.resize(area);
  125.       }
  126.    }
  127.    glutPostRedisplay();
  128. }
  129.  
  130. void processNormalKeys(unsigned char key, int x, int y) {
  131.    printf("%d %d %d\n", key, x, y);
  132.    if (key == 'r') {
  133.       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  134.       glPointSize(10);
  135.       glBegin(GL_POINTS);
  136.       glVertex2f(0, 0);
  137.       glEnd();
  138.    }
  139. }
  140.  
  141. void processSpecialKeys(int key, int x, int y) {
  142.    switch (key) {
  143.    case GLUT_KEY_F1:
  144.       glPointSize(10);
  145.          glBegin(GL_POINTS);
  146.          glVertex2f(0, 0.5);
  147.       glEnd();
  148.    case GLUT_KEY_F2:
  149.       glPointSize(10);
  150.       glBegin(GL_POINTS);
  151.       glVertex2f(0, 0);
  152.       glEnd();
  153.    case GLUT_KEY_F3:
  154.       glPointSize(10);
  155.       glBegin(GL_POINTS);
  156.       glVertex2f(-0.5, 0.5);
  157.       glEnd();
  158.    }
  159. }
  160.  
  161. int main(int argc, char** argv) {
  162.    glutInit(&argc, argv);
  163.    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
  164.    glutInitWindowPosition(400, 400);
  165.    glutInitWindowSize(666, 666);
  166.    glutCreateWindow("New");
  167.  
  168.    x = 0.5;
  169.    y = 0;
  170.    points.resize(1);
  171.  
  172.    //glutReshapeFunc(changeSize);
  173.  
  174.    glutMouseFunc(mouseButton);
  175.    glutSpecialFunc(processSpecialKeys);
  176.    glutKeyboardFunc(processNormalKeys);
  177.  
  178.    glutDisplayFunc(renderScene);
  179.    
  180.    glutMainLoop();
  181.  
  182.  
  183.    return 1;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement