Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 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. const int scrW = 1280, scrH = 960;
  9.  
  10. //СНАЧАЛА y, ПОТОМ x
  11. GLubyte vertexContainer[scrH][scrW][3] = {0};
  12.  
  13. // Y снизу вверх
  14. // X слева направо
  15.  
  16. struct vertex {
  17. GLdouble x, y;
  18. };
  19.  
  20. bool isPaintedOver(const GLubyte *mas) {
  21. return mas[0] || mas[1] || mas[2];
  22. }
  23.  
  24. vector<vertex> mas;
  25.  
  26. int flag = 0;
  27. int flagCanDraw = 1;
  28. int someHappend = 0;
  29.  
  30. void drawPixel(int x, int y) {
  31. vertexContainer[y][x][0] = 0;
  32. vertexContainer[y][x][1] = 255;
  33. vertexContainer[y][x][2] = 255;
  34. }
  35.  
  36. void drawline(int x1, int y1, int x2, int y2) {
  37. const int deltaX = abs(x2 - x1);
  38. const int deltaY = abs(y2 - y1);
  39. const int signX = x1 < x2 ? 1 : -1;
  40. const int signY = y1 < y2 ? 1 : -1;
  41. int error = deltaX - deltaY;
  42. drawPixel(x2, y2);
  43. while(x1 != x2 || y1 != y2) {
  44. drawPixel(x1, y1);
  45. const int error2 = error * 2;
  46. if (error2 > -deltaY) {
  47. error -= deltaY;
  48. x1 += signX;
  49. }
  50. if (error2 < deltaX) {
  51. error += deltaX;
  52. y1 += signY;
  53. }
  54. }
  55.  
  56. }
  57.  
  58.  
  59. //void draw() {
  60. //
  61. // for (int x = 0; x < scrW; x++) {
  62. // for (int y = 0; y < scrH; y++) {
  63. // vertexContainer[x][y][0] = 0;
  64. // vertexContainer[x][y][1] = 255;
  65. // vertexContainer[x][y][2] = 255;
  66. // }
  67. // }
  68. //
  69. // glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  70. // glPixelStorei(GL_UNPACK_ROW_LENGTH, scrW); //длина строки
  71. // glPixelStorei(GL_UNPACK_SKIP_ROWS, 0); // сколько строк пропустить?
  72. // glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0); // сколько пикселов пропустить в каждой строке?
  73. //
  74. // glRasterPos2i(0, 0);
  75. // glDrawPixels(scrW, scrH, GL_RGB, GL_UNSIGNED_BYTE, vertexContainer);
  76. //
  77. //}
  78.  
  79. void fill() {
  80. for (int y = scrH - 1; y >= 0; y--) {
  81. int flagDraw = 0;
  82.  
  83. int countConstDraw = 0;
  84. int countDraw = 0;
  85.  
  86. for (int x = 0; x < scrW; x++) {
  87. if (isPaintedOver(vertexContainer[y][x])) {
  88. countConstDraw++;
  89. }
  90. }
  91.  
  92. for (int x = 0; x < scrW; x++) {
  93. if (isPaintedOver(vertexContainer[y][x])) {
  94. if (flagDraw == 0) {
  95. flagDraw = 1;
  96. countDraw++;
  97. } else if (flagDraw == 1) {
  98. flagDraw = 1;
  99. countDraw++;
  100. } else if (flagDraw == 2) {
  101. flagDraw = 1;
  102. countDraw++;
  103. }
  104. } else {
  105. if (flagDraw == 0) {
  106. flagDraw = 0;
  107. } else if (flagDraw == 1) {
  108. if (isPaintedOver(vertexContainer[y + 1][x])) {
  109. flagDraw = 2;
  110. } else {
  111. flagDraw = 0;
  112. }
  113. } else if (flagDraw == 2) {
  114. flagDraw = 2;
  115. }
  116. }
  117.  
  118. if (y < scrH - 1) {
  119. if (flagDraw == 2 && countConstDraw != countDraw) {
  120. drawPixel(x, y);
  121. }
  122. }
  123.  
  124. }
  125. }
  126.  
  127. cout << "I fill" << endl;
  128. someHappend = 0;
  129. }
  130.  
  131. void drawEdges() {
  132.  
  133. if (mas.size() > 1) {
  134. for (int i = 0; i < mas.size() - 1; i++) {
  135. drawline(mas[i].x, mas[i].y, mas[i + 1].x, mas[i + 1].y);
  136. }
  137.  
  138. if (flag == 1) {
  139. drawline(mas[mas.size() - 1].x, mas[mas.size() - 1].y, mas[0].x, mas[0].y);
  140. }
  141.  
  142. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  143. glPixelStorei(GL_UNPACK_ROW_LENGTH, scrW);
  144. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  145. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  146.  
  147. glRasterPos2i(0, 0);
  148. glDrawPixels(scrW, scrH, GL_RGB, GL_UNSIGNED_BYTE, vertexContainer);
  149.  
  150. if (flag == 2 && someHappend == 1) {
  151. fill();
  152. }
  153. }
  154.  
  155. }
  156.  
  157. void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
  158. if (action == GLFW_PRESS) {
  159. switch (key) {
  160. case GLFW_KEY_1:
  161. flag = 1;
  162. flagCanDraw = 0;
  163. break;
  164. case GLFW_KEY_2:
  165. flag = 2;
  166. flagCanDraw = 0;
  167. someHappend = 1;
  168. break;
  169. case GLFW_KEY_C:
  170. mas.clear();
  171. for (int y = 0; y < scrH; y++) {
  172. for (int x = 0; x < scrW; x++) {
  173. vertexContainer[y][x][0] = 0;
  174. vertexContainer[y][x][1] = 0;
  175. vertexContainer[y][x][2] = 0;
  176. }
  177. }
  178. flag = 0;
  179. flagCanDraw = 1;
  180. break;
  181. case GLFW_KEY_P:
  182. for (int y = scrH - 1; y >= 0; y--){
  183. cout << "stroka y: " << y << endl;
  184. for (int x = 0; x < scrW; x++) {
  185. cout << int(isPaintedOver(vertexContainer[y][x])) << " ";
  186. }
  187. cout << endl;
  188. }
  189. }
  190. }
  191. }
  192.  
  193. void mouseCallback(GLFWwindow *window, int button, int action, int mods) {
  194. if (flagCanDraw) {
  195. if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
  196. GLdouble x, y;
  197. glfwGetCursorPos(window, &x, &y);
  198. vertex vertexLeftBut;
  199. vertexLeftBut.x = x;
  200. vertexLeftBut.y = scrH - y;
  201. mas.push_back(vertexLeftBut);
  202. }
  203. }
  204.  
  205. }
  206.  
  207.  
  208. int main() {
  209. GLFWwindow *window;
  210.  
  211.  
  212. if (!glfwInit()) {
  213. return -1;
  214. }
  215.  
  216. window = glfwCreateWindow(scrW, scrH, "Lab4", NULL, NULL);
  217.  
  218. glfwSetKeyCallback(window, keyCallback);
  219. glfwSetMouseButtonCallback(window, mouseCallback);
  220.  
  221. glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);
  222.  
  223. int screenW, screenH;
  224. glfwGetFramebufferSize(window, &screenW, &screenH);
  225.  
  226. if (!window) {
  227. glfwTerminate();
  228. return -1;
  229. }
  230.  
  231. glfwMakeContextCurrent(window);
  232. glMatrixMode(GL_PROJECTION);
  233. glLoadIdentity();
  234. glMatrixMode(GL_MODELVIEW);
  235. glLoadIdentity();
  236.  
  237.  
  238. while (!glfwWindowShouldClose(window)) {
  239.  
  240. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  241. glPushMatrix();
  242. glOrtho(0.0, scrW, 0.0, scrH, -1.0, 1.0);
  243. glViewport(0, 0, screenW, screenH);
  244.  
  245. drawEdges();
  246.  
  247. glEnd();
  248. glPopMatrix();
  249.  
  250. glfwSwapBuffers(window);
  251. glfwPollEvents();
  252. }
  253.  
  254. glfwTerminate();
  255.  
  256. return 0;
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement