Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 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. int lastDraw = 0;
  86.  
  87. for (int x = 0; x < scrW; x++) {
  88. if (isPaintedOver(vertexContainer[y][x])) {
  89. countConstDraw++;
  90. }
  91. }
  92.  
  93. for (int x = 0; x < scrW; x++) {
  94. if (isPaintedOver(vertexContainer[y][x])) {
  95. if (flagDraw == 0) {
  96. if (x > 0) {
  97. if (lastDraw == x - 1) {
  98. flagDraw = 0;
  99. } else {
  100. flagDraw = 1;
  101. }
  102. } else {
  103. flagDraw = 1;
  104. }
  105. countDraw++;
  106. lastDraw = x;
  107. } else if (flagDraw == 1) {
  108. flagDraw = 1;
  109. countDraw++;
  110. lastDraw = x;
  111. } else if (flagDraw == 2) {
  112. flagDraw = 0;
  113. countDraw++;
  114. lastDraw = x;
  115. }
  116. } else {
  117. if (flagDraw == 0) {
  118. flagDraw = 0;
  119. } else if (flagDraw == 1) {
  120. if (isPaintedOver(vertexContainer[y + 1][x])) {
  121. flagDraw = 2;
  122. } else {
  123. flagDraw = 0;
  124. }
  125. } else if (flagDraw == 2) {
  126. flagDraw = 2;
  127. }
  128. }
  129.  
  130. if (y < scrH - 1) {
  131. if (flagDraw == 2 && countConstDraw != countDraw) {
  132. drawPixel(x, y);
  133. }
  134. }
  135.  
  136. }
  137. }
  138. cout << "I fill" << endl;
  139. someHappend = 0;
  140. }
  141.  
  142. void drawEdges() {
  143.  
  144. if (mas.size() > 1) {
  145. for (int i = 0; i < mas.size() - 1; i++) {
  146. drawline(mas[i].x, mas[i].y, mas[i + 1].x, mas[i + 1].y);
  147. }
  148.  
  149. if (flag == 1) {
  150. drawline(mas[mas.size() - 1].x, mas[mas.size() - 1].y, mas[0].x, mas[0].y);
  151. }
  152.  
  153. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  154. glPixelStorei(GL_UNPACK_ROW_LENGTH, scrW);
  155. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  156. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  157.  
  158. glRasterPos2i(0, 0);
  159. glDrawPixels(scrW, scrH, GL_RGB, GL_UNSIGNED_BYTE, vertexContainer);
  160.  
  161. if (flag == 2 && someHappend == 1) {
  162. fill();
  163. }
  164. }
  165.  
  166. }
  167.  
  168. void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
  169. if (action == GLFW_PRESS) {
  170. switch (key) {
  171. case GLFW_KEY_1:
  172. flag = 1;
  173. flagCanDraw = 0;
  174. break;
  175. case GLFW_KEY_2:
  176. flag = 2;
  177. flagCanDraw = 0;
  178. someHappend = 1;
  179. break;
  180. case GLFW_KEY_C:
  181. mas.clear();
  182. for (int y = 0; y < scrH; y++) {
  183. for (int x = 0; x < scrW; x++) {
  184. vertexContainer[y][x][0] = 0;
  185. vertexContainer[y][x][1] = 0;
  186. vertexContainer[y][x][2] = 0;
  187. }
  188. }
  189. flag = 0;
  190. flagCanDraw = 1;
  191. break;
  192. case GLFW_KEY_P:
  193. for (int y = scrH - 1; y >= 0; y--){
  194. cout << "stroka y: " << y << endl;
  195. for (int x = 0; x < scrW; x++) {
  196. cout << int(isPaintedOver(vertexContainer[y][x])) << " ";
  197. }
  198. cout << endl;
  199. }
  200. }
  201. }
  202. }
  203.  
  204. void mouseCallback(GLFWwindow *window, int button, int action, int mods) {
  205. if (flagCanDraw) {
  206. if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
  207. GLdouble x, y;
  208. glfwGetCursorPos(window, &x, &y);
  209. vertex vertexLeftBut;
  210. vertexLeftBut.x = x;
  211. vertexLeftBut.y = scrH - y;
  212. mas.push_back(vertexLeftBut);
  213. }
  214. }
  215.  
  216. }
  217.  
  218.  
  219. int main() {
  220. GLFWwindow *window;
  221.  
  222.  
  223. if (!glfwInit()) {
  224. return -1;
  225. }
  226.  
  227. window = glfwCreateWindow(scrW, scrH, "Lab4", NULL, NULL);
  228.  
  229. glfwSetKeyCallback(window, keyCallback);
  230. glfwSetMouseButtonCallback(window, mouseCallback);
  231.  
  232. glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);
  233.  
  234. int screenW, screenH;
  235. glfwGetFramebufferSize(window, &screenW, &screenH);
  236.  
  237. if (!window) {
  238. glfwTerminate();
  239. return -1;
  240. }
  241.  
  242. glfwMakeContextCurrent(window);
  243. glMatrixMode(GL_PROJECTION);
  244. glLoadIdentity();
  245. glMatrixMode(GL_MODELVIEW);
  246. glLoadIdentity();
  247.  
  248.  
  249. while (!glfwWindowShouldClose(window)) {
  250.  
  251. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  252. glPushMatrix();
  253. glOrtho(0.0, scrW, 0.0, scrH, -1.0, 1.0);
  254. glViewport(0, 0, screenW, screenH);
  255.  
  256. drawEdges();
  257.  
  258. glEnd();
  259. glPopMatrix();
  260.  
  261. glfwSwapBuffers(window);
  262. glfwPollEvents();
  263. }
  264.  
  265. glfwTerminate();
  266.  
  267. return 0;
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement