Advertisement
Guest User

Untitled

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