Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. #include <GLFW/glfw3.h>
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. GLfloat scrW = 500, scrH = 500;
  9.  
  10. // Y сверху вниз
  11. // X слева направо
  12.  
  13. struct vertex {
  14. GLdouble x, y;
  15. };
  16.  
  17. vector<vertex> mas;
  18.  
  19. int flag = 0;
  20. int flagCanDraw = 1;
  21.  
  22.  
  23.  
  24. void drawEdges() {
  25. if (flag) {
  26. glBegin(GL_LINE_LOOP);
  27. } else {
  28. glBegin(GL_LINE_STRIP);
  29. }
  30. glColor3f(1, 1, 1);
  31. for (int i = 0; i < mas.size(); i++) {
  32. // glVertex2f(mas[i].x, mas[i].y);
  33. glRasterPos2d(mas[i].x, mas[i].y);
  34. }
  35. glRasterPos2d(mas[0].x, mas[0].y);
  36. }
  37.  
  38. void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
  39. if (action == GLFW_PRESS) {
  40. switch (key) {
  41. case GLFW_KEY_1:
  42. flag = 1;
  43. flagCanDraw = 0;
  44. break;
  45. case GLFW_KEY_C:
  46. mas.clear();
  47. flag = 0;
  48. flagCanDraw = 1;
  49. break;
  50. }
  51. }
  52. }
  53.  
  54. void mouseCallback(GLFWwindow* window, int button, int action, int mods) {
  55. if (flagCanDraw) {
  56. if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
  57. GLdouble x, y;
  58. glfwGetCursorPos(window, &x, &y);
  59. vertex vertexLeftBut;
  60. vertexLeftBut.x = x;
  61. vertexLeftBut.y = y;
  62. mas.push_back(vertexLeftBut);
  63. }
  64. }
  65.  
  66. }
  67.  
  68.  
  69. int main() {
  70. GLFWwindow *window;
  71.  
  72.  
  73. if (!glfwInit()) {
  74. return -1;
  75. }
  76.  
  77. window = glfwCreateWindow(scrW, scrH, "Lab4", NULL, NULL);
  78.  
  79. glfwSetKeyCallback(window, keyCallback);
  80. glfwSetMouseButtonCallback(window, mouseCallback);
  81.  
  82. glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);
  83.  
  84. int screenW, screenH;
  85. glfwGetFramebufferSize(window, &screenW, &screenH);
  86.  
  87. if (!window) {
  88. glfwTerminate();
  89. return -1;
  90. }
  91.  
  92. glfwMakeContextCurrent(window);
  93. glMatrixMode(GL_PROJECTION);
  94. glLoadIdentity();
  95. glMatrixMode(GL_MODELVIEW);
  96. glLoadIdentity();
  97.  
  98.  
  99. while (!glfwWindowShouldClose(window)) {
  100.  
  101. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  102. glPushMatrix();
  103. glOrtho(0.0, scrW, scrH, 0.0, -1.0, 1.0);
  104. glViewport(0, 0, screenW, screenH);
  105.  
  106. drawEdges();
  107.  
  108. glEnd();
  109. glPopMatrix();
  110.  
  111. glfwSwapBuffers(window);
  112. glfwPollEvents();
  113. }
  114.  
  115. glfwTerminate();
  116.  
  117. return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement