Advertisement
Guest User

Untitled

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