Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 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. // Y сверху вниз
  9. // X слева направо
  10.  
  11. struct vertex {
  12. GLfloat x, y;
  13. };
  14.  
  15. vector<vertex> mas;
  16.  
  17. int flag = 0;
  18.  
  19. GLfloat centerScreenW, centerScreenH;
  20.  
  21.  
  22.  
  23. //void paintOver() {
  24. // sort(mas, )
  25. //
  26. // vertex array[vertexCount] = {};
  27. //}
  28.  
  29.  
  30. void drawEdges() {
  31. if (flag) {
  32. glBegin(GL_LINE_LOOP);
  33. } else {
  34. glBegin(GL_LINE_STRIP);
  35. }
  36. glColor3f(1, 1, 1);
  37. for (int i = 0; i < mas.size(); i++) {
  38. glVertex2f(mas[i].x, mas[i].y);
  39. }
  40. }
  41.  
  42. void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
  43. if (action == GLFW_PRESS) {
  44. switch (key) {
  45. case GLFW_KEY_1:
  46. flag = 1;
  47. break;
  48. case GLFW_KEY_C:
  49. mas.clear();
  50. flag = 0;
  51. break;
  52. }
  53. }
  54. }
  55.  
  56. void mouseCallback(GLFWwindow* window, int button, int action, int mods) {
  57. if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
  58. double x, y;
  59. glfwGetCursorPos(window, &x, &y);
  60. vertex vertexLeftBut;
  61. vertexLeftBut.x = x;
  62. vertexLeftBut.y = y;
  63. mas.push_back(vertexLeftBut);
  64. }
  65. }
  66.  
  67.  
  68. int main() {
  69. GLFWwindow *window;
  70.  
  71. GLfloat scrW = 1280, scrH = 960;
  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. centerScreenW = scrW / 2;
  99. centerScreenH = scrH / 2;
  100.  
  101. while (!glfwWindowShouldClose(window)) {
  102.  
  103. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  104. glPushMatrix();
  105. glOrtho(0.0, scrW, scrH, 0.0, -1.0, 1.0);
  106. glViewport(0, 0, screenW, screenH);
  107.  
  108. drawEdges();
  109.  
  110. glEnd();
  111. glPopMatrix();
  112.  
  113. glfwSwapBuffers(window);
  114. glfwPollEvents();
  115. }
  116.  
  117. glfwTerminate();
  118.  
  119. return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement