Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GLFW/glfw3.h>
  3. #include <cstdio>
  4. #include <iostream>
  5. #include <vector>
  6. #include <math.h>
  7. #include <algorithm>
  8. #include <set>
  9. using namespace std;
  10.  
  11. struct point {
  12. int x, y;
  13. bool operator==(const point &p) const {
  14. return (x == p.x) && (y == p.y);
  15. }
  16. };
  17.  
  18. struct edge {
  19. point first, second;
  20. bool operator<(const edge &e) const {
  21. return first.y < e.first.y;
  22. }
  23. void resize(float kW, float kH) {
  24. first.x *= kW;
  25. first.y *= kH;
  26. second.x *= kW;
  27. second.y *= kH;
  28. }
  29. };
  30.  
  31. GLint windowWidth, windowHeight;
  32. const unsigned int bcolor = 0x000c1d3d, dcolor = 0x00AD0255;
  33. point v0;
  34. vector <edge> edg;
  35. vector <unsigned int> pixels(1024 * 620);
  36. vector <unsigned int> oldp;
  37.  
  38. bool f = false;
  39.  
  40. void drawRustFill(GLFWwindow *window) {
  41. vector<edge> ESY = edg;
  42. sort(ESY.begin(), ESY.end());
  43. int go = 0;
  44. vector <edge> CAP;
  45.  
  46. for (int i = 0; i < windowHeight; i++) {
  47. for (;go < ESY.size();)
  48. if (ESY[go].first.y == i) {
  49. CAP.push_back(ESY[go]);
  50. ++go;
  51. } else break;
  52.  
  53. for (int j = 0; j < CAP.size(); j++)
  54. if (CAP[j].second.y == i)
  55. CAP.erase(CAP.begin() + j--);
  56.  
  57. vector <int> xes;
  58. for (int j = 0; j < CAP.size(); j++) {
  59. edge e = CAP[j];
  60. if (e.second.y == e.first.y) break;
  61. else xes.push_back(e.first.x + (float)((float)(e.second.x - e.first.x) / (float)(e.second.y - e.first.y)) * (float)(i - e.first.y));
  62. }
  63.  
  64. sort(xes.begin(), xes.end());
  65.  
  66. for (int j = 1; j < xes.size(); j+=2)
  67. for (int k = xes[j-1]; k <= xes[j]; k++)
  68. pixels[(windowHeight - i - 1) * windowWidth + k] = dcolor;
  69. }
  70. oldp = pixels;
  71. }
  72.  
  73. void filter(GLFWwindow * window) {
  74. if (f) {
  75. f = false;
  76. pixels = oldp;
  77. }
  78. else {
  79. vector <unsigned int> save = pixels;
  80. f = true;
  81. const unsigned int base[] = { 0, 1, 0, 1, 2, 1, 0, 1, 0 };
  82.  
  83. for (int y = 1; y < windowHeight - 1; y++)
  84. for (int x = 1; x < windowWidth - 1; x++) {
  85. unsigned int c = 0;
  86. for (int i = x - 1; i <= x + 1; i++)
  87. for (int j = y - 1; j <= y + 1; j++) {
  88. for (unsigned int k = 0; k < 3; ++k)
  89. c += (unsigned int)((((pixels[i + j * windowWidth]) >> (k * 8)) % 256) * base[i - x + 1 + j - y + 1]/6 ) << (k * 8);
  90. c += (unsigned int)(((pixels[i + j * windowWidth]) >> (3 * 8)) * base[i - x + 1 + j - y + 1]/6 ) << (3 * 8);
  91. }
  92. save[x + y * windowWidth] = c;
  93. }
  94. pixels = save;
  95. }
  96. }
  97.  
  98. void error(int error, const char * description) {
  99. fputs(description, stderr);
  100. }
  101.  
  102. void key(GLFWwindow * window, int key, int scancode, int action, int mods) {
  103. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  104. glfwSetWindowShouldClose(window, GL_TRUE);
  105. if (key == GLFW_KEY_DELETE && action == GLFW_PRESS) {
  106. edg.clear();
  107. fill(pixels.begin(), pixels.end(), bcolor);
  108. }
  109. if (key == GLFW_KEY_ENTER && action == GLFW_PRESS)
  110. filter(window);
  111. }
  112.  
  113. void mouseb(GLFWwindow * window, int button, int action, int mode) {
  114. if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
  115. double xpos, ypos;
  116. glfwGetCursorPos(window, &xpos, &ypos);
  117. point p;
  118. p.x = xpos;
  119. p.y = ypos;
  120.  
  121. edge e;
  122. if (!edg.empty()) {
  123. if (edg.at(edg.size() - 1).first == v0)
  124. edg.at(edg.size() - 1).first = p;
  125. else edg.at(edg.size() - 1).second = p;
  126. if (edg.at(edg.size() - 1).first.y > edg.at(edg.size() - 1).second.y)
  127. swap(edg.at(edg.size() - 1).first, edg.at(edg.size() - 1).second);
  128. e.first = p;
  129. e.second = v0;
  130. if (e.first.y > e.second.y)
  131. swap(e.first, e.second);
  132. } else {
  133. e.first = e.second = v0 = p;
  134. }
  135. edg.push_back(e);
  136. for (int i = 0; i < windowHeight; i++)
  137. for (int j = 0; j < windowWidth; j++)
  138. pixels[(windowHeight - i - 1) * windowWidth + j] = bcolor;
  139. drawRustFill(window);
  140. }
  141. }
  142.  
  143. void resizeCallback(GLFWwindow* window, int W, int H) {
  144. int w = windowWidth, h = windowHeight;
  145. glfwGetWindowSize(window, &windowWidth, &windowHeight);
  146. float kW = (float)windowWidth / w;
  147. float kH = (float)windowHeight / h;
  148.  
  149. for (int i = 0; i < edg.size(); i++)
  150. edg[i].resize(kW, kH);
  151.  
  152. pixels.resize(windowWidth * windowHeight);
  153. v0.x *= kW;
  154. v0.y *= kH;
  155. fill(pixels.begin(), pixels.end(), bcolor);
  156.  
  157. drawRustFill(window);
  158. glViewport(0, 0, windowWidth, windowHeight);
  159. }
  160.  
  161. int main() {
  162. glfwSetErrorCallback(error);
  163. if (!glfwInit()) exit(1);
  164.  
  165. GLFWwindow * window = glfwCreateWindow(800, 500, "Lab 4", NULL, NULL);
  166. if (!window) {
  167. glfwTerminate();
  168. exit(1);
  169. }
  170. glfwMakeContextCurrent(window);
  171. glfwSetKeyCallback(window, key);
  172. glfwSetMouseButtonCallback(window, mouseb);
  173. glfwSetWindowSizeCallback(window, resizeCallback);
  174. glfwGetWindowSize(window, &windowWidth, &windowHeight);
  175.  
  176. while (!glfwWindowShouldClose(window)) {
  177. glClear(GL_COLOR_BUFFER_BIT);
  178. glDrawPixels(windowWidth, windowHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, pixels.data());
  179. glfwSwapBuffers(window);
  180. glfwPollEvents();
  181. }
  182. glfwDestroyWindow(window);
  183. glfwTerminate();
  184. return 0;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement