Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 KB | None | 0 0
  1. #include <iostream>
  2. #define GL_SILENCE_DEPRECATION //эпл не любит опенгл :с
  3. #include <algorithm>
  4. #include <vector>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <vector>
  8. #include <GLFW/glfw3.h>
  9. #include <cmath>
  10. #include <math.h>
  11.  
  12. #define PI 3.14159265
  13.  
  14. struct point{
  15. int x, y, n;
  16. };
  17.  
  18. union pixel{
  19. unsigned int a;
  20. char b[4];
  21. };
  22.  
  23. static double posx, posy =0;
  24. static int px, py=0, x_bound= 540;
  25. static int a=1080;
  26. static int b=1080;
  27. static bool modF, mod=false;
  28. static int zoom=2;
  29. static union pixel* byts_arr;
  30. static int line_color=255, color =255| 250<<16 | 250<<8 | 255<<24, bound_color =255 | 255 <<8, polygon_color=155<<16 | 155<<8 | 155<<24 | 25;
  31. static unsigned int *buff;
  32. static bool flag=false;
  33.  
  34. int sign(int a) {return 1-2*(a>>31&1);};
  35. int find_center();
  36. void filter(int x, int y);
  37. void game_loop_drow();
  38. void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos);
  39. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
  40. void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
  41. void drow_line(int x0, int y0, int x1, int y1);
  42. void build(int x, int y);
  43.  
  44. std::vector<point> polygon;
  45.  
  46. int main(int argc, const char *argv[]){
  47. if (!glfwInit()){
  48. std::cout << "faild\n";
  49. return 0;
  50. }
  51.  
  52. GLFWwindow* window = glfwCreateWindow(a, b, "lol", NULL, NULL);
  53.  
  54. if (!window){
  55. std::cout<< "faild\n";
  56. return 0;
  57. }
  58.  
  59. glfwMakeContextCurrent(window);
  60.  
  61. glfwSetCursorPosCallback(window, cursor_pos_callback);
  62. glfwSetKeyCallback(window, key_callback);
  63. glfwSetMouseButtonCallback(window, mouse_button_callback);
  64.  
  65. byts_arr= new union pixel[a*b];
  66. buff= new unsigned int[a*b];
  67.  
  68. glEnable(GL_BLEND);
  69. glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  70. while (!glfwWindowShouldClose(window)){
  71. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  72. glEnable(GL_DEPTH_TEST);
  73.  
  74. game_loop_drow();
  75.  
  76. glFlush();
  77. glfwSwapBuffers(window);
  78. glfwWaitEvents();
  79.  
  80. }
  81.  
  82. glfwDestroyWindow(window);
  83. glfwTerminate();
  84.  
  85. delete [] byts_arr;
  86. delete [] buff;
  87.  
  88. return(0);
  89. }
  90.  
  91. void game_loop_drow(){
  92. // for (int i=0; i< a*b*4; byts_arr[i].a=color, i++);
  93. //заполняю фон
  94. for (int i=0; i<a*b; i++) {
  95. byts_arr[i].a=color;
  96. }
  97.  
  98. for (int i=0; i< polygon.size(); i++){
  99. int s0=(i==0? polygon[(polygon.size()-1)].y : polygon[(i-1)%(polygon.size())].y);
  100. int s1=polygon[i].y;
  101. int s2=polygon[(i+1)%polygon.size()].y;
  102. flag=false;
  103. if (s1-s0>0 && s1-s2>0 || s1-s0<0 && s1-s2<0) flag=true;
  104. drow_line(polygon[i].x, polygon[i].y, polygon[(i+1)%polygon.size()].x, polygon[(i+1)%polygon.size()].y);
  105. }
  106.  
  107. for (int y=1; y<b-1; byts_arr[(a-y++)*a+x_bound].a=bound_color); //прямая
  108.  
  109.  
  110. glRasterPos2f(-1, -1);
  111. glPixelZoom(zoom, zoom);
  112. glDrawPixels(a, b, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, byts_arr);
  113.  
  114. }
  115.  
  116. void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos){
  117. px= xpos;
  118. py= ypos;
  119. //byts_arr[a*b*4 - 4*a*py + 2*px].a = 0;
  120. return;
  121. }
  122.  
  123. void mouse_button_callback(GLFWwindow* window, int button, int action, int mods){
  124. if (action==0 && mods==0){
  125. polygon.push_back((point) {px, py, (int)polygon.size()});
  126. x_bound=find_center();
  127. }
  128. return;
  129. }
  130.  
  131. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
  132. //std::cout<< key<< std::endl;
  133. if( action == 1) return;
  134. switch (key ){
  135. case 66: // b
  136. mod^=true;
  137. break;
  138. case 82: // r
  139. polygon.clear();
  140. mod=false;
  141. modF=false;
  142. break;
  143. case 61: //+
  144. zoom++;
  145. break;
  146. case 70: // f
  147. modF^=true;
  148. break;
  149. case 45:// -
  150. zoom--;
  151. abs(zoom);
  152. break;
  153. case GLFW_KEY_ESCAPE:
  154. if (glfwGetInputMode(window, GLFW_CURSOR) != GLFW_CURSOR_DISABLED){
  155. glfwSetWindowShouldClose(window, GLFW_TRUE);
  156. }
  157. break;
  158. default:
  159. break;
  160. }
  161. return;
  162. }
  163.  
  164.  
  165.  
  166.  
  167. void drow_line(int x1, int y1, int x2, int y2){
  168. int dx= (x2- x1);
  169. int dy= (y2- y1);
  170. int sx= x1< x2? 1: -1;
  171. int sy= y1 < y2? 1: -1;
  172. if (dx==0) return; //значит перпендикулярно линии
  173. double tg= (double) dy/dx;
  174. bool intense = tg>0;
  175. bool swaped = -1>tg || tg>1;
  176. if (swaped) std::swap(x1,y1), std::swap(x2,y2), std::swap(dx,dy), std::swap(sx,sy);
  177. dx= abs(dx);
  178. dy= abs(dy);
  179. int I = 255;
  180. int e = I*dx;
  181. int m= 2*dy*I;
  182. double w = 2*e;
  183. int error= dx- dy;
  184.  
  185. int t_color = mod? polygon_color : line_color;
  186.  
  187.  
  188. //byts_arr[(a-y2-50)*a+x2].a=255;
  189. //std::cout<< m << " " << w << std::endl;
  190. if (swaped) {
  191. byts_arr[(a-y2-150)*a+x2].a=255;
  192. } else byts_arr[(a-x2-150)*a+y2].a=255;
  193.  
  194. if (mod && swaped && flag) build(y1, x1);
  195. else if (mod && flag) build(x1, y1);
  196. //build(x1, y1);
  197. //build(x2, y2);
  198. //std:: cout<< flag << std::endl;
  199.  
  200.  
  201. //рисую ребра
  202. while (x1!= x2 || y1!= y2) {
  203. int error2= error* 2;
  204. //byts_arr[(a-y1-150)*a+x1].a=t_color;
  205.  
  206. if (modF){
  207. if (swaped) byts_arr[(a- x1- 150)* a+ y1].b[0]= (unsigned char)(!intense ? e/(2*dx) : 255 - e/(2*dx));
  208. else byts_arr[(a- y1- 150)* a+ x1].b[0]=(unsigned char)((intense) ? e/(2*dx) : 255 - e/(2*dx));
  209. }else {
  210. if (swaped) byts_arr[(a- x1- 150)* a+ y1].a=255;
  211. else byts_arr[(a- y1- 150)* a+ x1].a=255;
  212. }
  213. //else byts_arr[(a- y1- 150)* a+ x1].a=255; //линии
  214. //std::cout <<(double)(e/(2*dx)) << std::endl;
  215. if (error2> -dy){
  216. error-= dy;
  217. x1+= sx;
  218. e+=m;
  219.  
  220. }
  221. if (error2< dx){
  222. error+= dx;
  223. y1+= sy;
  224. e-=w;
  225. if (swaped && mod) build(y1,x1);
  226. else
  227. if (mod) build(x1, y1);
  228. }
  229.  
  230.  
  231. }
  232. return;
  233. }
  234.  
  235. void build(int x, int y){
  236. int dx= sign(x_bound-x);
  237. x+=dx;
  238. dx= sign(x_bound-x);
  239. for (; x!=x_bound; x+=dx ){
  240. //byts_arr[(a-y-150)*a+x].a= byts_arr[(a-y-150)*a+x].a == line_color? color : line_color; //исправить нули на колор
  241. byts_arr[(a-y-150)*a+x].a= byts_arr[(a-y-150)*a+x].a == color || byts_arr[(a-y-150)*a+x].a == line_color? polygon_color : color;
  242. }
  243. return;
  244. }
  245.  
  246. int find_center(){
  247. int min=1200, max=-1;
  248. for (std::vector<point>::iterator it=polygon.begin(); it!=polygon.end(); ++it){
  249. min=(*it).x<min ? (*it).x : min;
  250. max=(*it).x>max ? (*it).x : max;
  251. }
  252. return (int) (max+min)/2;
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement