Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define GL_SILENCE_DEPRECATION //эпл не любит опенгл :с
- #include <algorithm>
- #include <vector>
- #include <stdio.h>
- #include <stdlib.h>
- #include <vector>
- #include <GLFW/glfw3.h>
- #include <cmath>
- #include <math.h>
- #define PI 3.14159265
- struct point{
- int x, y, n;
- };
- union pixel{
- unsigned int a;
- char b[4];
- };
- static double posx, posy =0;
- static int px, py=0, x_bound= 540;
- static int a=1080;
- static int b=1080;
- static bool modF, mod=false;
- static int zoom=2;
- static union pixel* byts_arr;
- 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;
- static unsigned int *buff;
- static bool flag=false;
- int sign(int a) {return 1-2*(a>>31&1);};
- int find_center();
- void filter(int x, int y);
- void game_loop_drow();
- void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos);
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
- void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
- void drow_line(int x0, int y0, int x1, int y1);
- void build(int x, int y);
- std::vector<point> polygon;
- int main(int argc, const char *argv[]){
- if (!glfwInit()){
- std::cout << "faild\n";
- return 0;
- }
- GLFWwindow* window = glfwCreateWindow(a, b, "lol", NULL, NULL);
- if (!window){
- std::cout<< "faild\n";
- return 0;
- }
- glfwMakeContextCurrent(window);
- glfwSetCursorPosCallback(window, cursor_pos_callback);
- glfwSetKeyCallback(window, key_callback);
- glfwSetMouseButtonCallback(window, mouse_button_callback);
- byts_arr= new union pixel[a*b];
- buff= new unsigned int[a*b];
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
- while (!glfwWindowShouldClose(window)){
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glEnable(GL_DEPTH_TEST);
- game_loop_drow();
- glFlush();
- glfwSwapBuffers(window);
- glfwWaitEvents();
- }
- glfwDestroyWindow(window);
- glfwTerminate();
- delete [] byts_arr;
- delete [] buff;
- return(0);
- }
- void game_loop_drow(){
- // for (int i=0; i< a*b*4; byts_arr[i].a=color, i++);
- //заполняю фон
- for (int i=0; i<a*b; i++) {
- byts_arr[i].a=color;
- }
- for (int i=0; i< polygon.size(); i++){
- int s0=(i==0? polygon[(polygon.size()-1)].y : polygon[(i-1)%(polygon.size())].y);
- int s1=polygon[i].y;
- int s2=polygon[(i+1)%polygon.size()].y;
- flag=false;
- if (s1-s0>0 && s1-s2>0 || s1-s0<0 && s1-s2<0) flag=true;
- drow_line(polygon[i].x, polygon[i].y, polygon[(i+1)%polygon.size()].x, polygon[(i+1)%polygon.size()].y);
- }
- for (int y=1; y<b-1; byts_arr[(a-y++)*a+x_bound].a=bound_color); //прямая
- glRasterPos2f(-1, -1);
- glPixelZoom(zoom, zoom);
- glDrawPixels(a, b, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, byts_arr);
- }
- void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos){
- px= xpos;
- py= ypos;
- //byts_arr[a*b*4 - 4*a*py + 2*px].a = 0;
- return;
- }
- void mouse_button_callback(GLFWwindow* window, int button, int action, int mods){
- if (action==0 && mods==0){
- polygon.push_back((point) {px, py, (int)polygon.size()});
- x_bound=find_center();
- }
- return;
- }
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
- //std::cout<< key<< std::endl;
- if( action == 1) return;
- switch (key ){
- case 66: // b
- mod^=true;
- break;
- case 82: // r
- polygon.clear();
- mod=false;
- modF=false;
- break;
- case 61: //+
- zoom++;
- break;
- case 70: // f
- modF^=true;
- break;
- case 45:// -
- zoom--;
- abs(zoom);
- break;
- case GLFW_KEY_ESCAPE:
- if (glfwGetInputMode(window, GLFW_CURSOR) != GLFW_CURSOR_DISABLED){
- glfwSetWindowShouldClose(window, GLFW_TRUE);
- }
- break;
- default:
- break;
- }
- return;
- }
- void drow_line(int x1, int y1, int x2, int y2){
- int dx= (x2- x1);
- int dy= (y2- y1);
- int sx= x1< x2? 1: -1;
- int sy= y1 < y2? 1: -1;
- if (dx==0) return; //значит перпендикулярно линии
- double tg= (double) dy/dx;
- bool intense = tg>0;
- bool swaped = -1>tg || tg>1;
- if (swaped) std::swap(x1,y1), std::swap(x2,y2), std::swap(dx,dy), std::swap(sx,sy);
- dx= abs(dx);
- dy= abs(dy);
- int I = 255;
- int e = I*dx;
- int m= 2*dy*I;
- double w = 2*e;
- int error= dx- dy;
- int t_color = mod? polygon_color : line_color;
- //byts_arr[(a-y2-50)*a+x2].a=255;
- //std::cout<< m << " " << w << std::endl;
- if (swaped) {
- byts_arr[(a-y2-150)*a+x2].a=255;
- } else byts_arr[(a-x2-150)*a+y2].a=255;
- if (mod && swaped && flag) build(y1, x1);
- else if (mod && flag) build(x1, y1);
- //build(x1, y1);
- //build(x2, y2);
- //std:: cout<< flag << std::endl;
- //рисую ребра
- while (x1!= x2 || y1!= y2) {
- int error2= error* 2;
- //byts_arr[(a-y1-150)*a+x1].a=t_color;
- if (modF){
- if (swaped) byts_arr[(a- x1- 150)* a+ y1].b[0]= (unsigned char)(!intense ? e/(2*dx) : 255 - e/(2*dx));
- else byts_arr[(a- y1- 150)* a+ x1].b[0]=(unsigned char)((intense) ? e/(2*dx) : 255 - e/(2*dx));
- }else {
- if (swaped) byts_arr[(a- x1- 150)* a+ y1].a=255;
- else byts_arr[(a- y1- 150)* a+ x1].a=255;
- }
- //else byts_arr[(a- y1- 150)* a+ x1].a=255; //линии
- //std::cout <<(double)(e/(2*dx)) << std::endl;
- if (error2> -dy){
- error-= dy;
- x1+= sx;
- e+=m;
- }
- if (error2< dx){
- error+= dx;
- y1+= sy;
- e-=w;
- if (swaped && mod) build(y1,x1);
- else
- if (mod) build(x1, y1);
- }
- }
- return;
- }
- void build(int x, int y){
- int dx= sign(x_bound-x);
- x+=dx;
- dx= sign(x_bound-x);
- for (; x!=x_bound; x+=dx ){
- //byts_arr[(a-y-150)*a+x].a= byts_arr[(a-y-150)*a+x].a == line_color? color : line_color; //исправить нули на колор
- 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;
- }
- return;
- }
- int find_center(){
- int min=1200, max=-1;
- for (std::vector<point>::iterator it=polygon.begin(); it!=polygon.end(); ++it){
- min=(*it).x<min ? (*it).x : min;
- max=(*it).x>max ? (*it).x : max;
- }
- return (int) (max+min)/2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement